xref: /titanic_52/usr/src/cmd/boot/bootadm/bootadm.c (revision cedc7e57759c3abc0d051e6b986d12b2e840e85b)
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 
2167c478bd9Sstevel@tonic-gate /* Globals */
217ae115bc7Smrj int bam_verbose;
218ae115bc7Smrj int bam_force;
219eb2bd662Svikram int bam_debug;
2207c478bd9Sstevel@tonic-gate static char *prog;
2217c478bd9Sstevel@tonic-gate static subcmd_t bam_cmd;
2227c478bd9Sstevel@tonic-gate static char *bam_root;
2237c478bd9Sstevel@tonic-gate static int bam_rootlen;
2247c478bd9Sstevel@tonic-gate static int bam_root_readonly;
22540541d5dSvikram static int bam_alt_root;
22648847494SEnrico Perla - Sun Microsystems static int bam_extend = 0;
22748847494SEnrico Perla - Sun Microsystems static int bam_purge = 0;
2287c478bd9Sstevel@tonic-gate static char *bam_subcmd;
2297c478bd9Sstevel@tonic-gate static char *bam_opt;
2307c478bd9Sstevel@tonic-gate static char **bam_argv;
2317c478bd9Sstevel@tonic-gate static int bam_argc;
2327c478bd9Sstevel@tonic-gate static int bam_check;
2337c478bd9Sstevel@tonic-gate static int bam_smf_check;
2347c478bd9Sstevel@tonic-gate static int bam_lock_fd = -1;
235e7cbe64fSgw25295 static int bam_zfs;
2367c478bd9Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/";
237b610f78eSvikram static int bam_update_all;
238d876c67dSjg static int bam_alt_platform;
239d876c67dSjg static char *bam_platform;
240*cedc7e57SEnrico Perla - Sun Microsystems static char *bam_home_env = NULL;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /* function prototypes */
243986fd29aSsetje static void parse_args_internal(int, char *[]);
244986fd29aSsetje static void parse_args(int, char *argv[]);
245986fd29aSsetje static error_t bam_menu(char *, char *, int, char *[]);
246986fd29aSsetje static error_t bam_archive(char *, char *);
2477c478bd9Sstevel@tonic-gate 
248986fd29aSsetje static void bam_exit(int);
2497c478bd9Sstevel@tonic-gate static void bam_lock(void);
2507c478bd9Sstevel@tonic-gate static void bam_unlock(void);
2517c478bd9Sstevel@tonic-gate 
252986fd29aSsetje static int exec_cmd(char *, filelist_t *);
253986fd29aSsetje static error_t read_globals(menu_t *, char *, char *, int);
254eb2bd662Svikram static int menu_on_bootdisk(char *os_root, char *menu_root);
255986fd29aSsetje static menu_t *menu_read(char *);
256986fd29aSsetje static error_t menu_write(char *, menu_t *);
257986fd29aSsetje static void linelist_free(line_t *);
258986fd29aSsetje static void menu_free(menu_t *);
259986fd29aSsetje static void filelist_free(filelist_t *);
260986fd29aSsetje static error_t list2file(char *, char *, char *, line_t *);
261986fd29aSsetje static error_t list_entry(menu_t *, char *, char *);
262986fd29aSsetje static error_t delete_all_entries(menu_t *, char *, char *);
263eb2bd662Svikram static error_t update_entry(menu_t *mp, char *menu_root, char *opt);
264eb2bd662Svikram static error_t update_temp(menu_t *mp, char *dummy, char *opt);
2657c478bd9Sstevel@tonic-gate 
266986fd29aSsetje static error_t update_archive(char *, char *);
267986fd29aSsetje static error_t list_archive(char *, char *);
268986fd29aSsetje static error_t update_all(char *, char *);
269986fd29aSsetje static error_t read_list(char *, filelist_t *);
270986fd29aSsetje static error_t set_global(menu_t *, char *, int);
271986fd29aSsetje static error_t set_option(menu_t *, char *, char *);
272986fd29aSsetje static error_t set_kernel(menu_t *, menu_cmd_t, char *, char *, size_t);
273eb2bd662Svikram static error_t get_kernel(menu_t *, menu_cmd_t, char *, size_t);
274986fd29aSsetje static char *expand_path(const char *);
2757c478bd9Sstevel@tonic-gate 
276986fd29aSsetje static long s_strtol(char *);
277986fd29aSsetje static int s_fputs(char *, FILE *);
2787c478bd9Sstevel@tonic-gate 
279eb2bd662Svikram static int is_zfs(char *root);
280eb2bd662Svikram static int is_ufs(char *root);
281eb2bd662Svikram static int is_pcfs(char *root);
2827c478bd9Sstevel@tonic-gate static int is_amd64(void);
28379755401Ssetje static char *get_machine(void);
2847c478bd9Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *);
285eb2bd662Svikram static char *mount_top_dataset(char *pool, zfs_mnted_t *mnted);
286eb2bd662Svikram static int umount_top_dataset(char *pool, zfs_mnted_t mnted, char *mntpt);
287eb2bd662Svikram static int ufs_add_to_sign_list(char *sign);
288963390b4Svikram static error_t synchronize_BE_menu(void);
2897c478bd9Sstevel@tonic-gate 
290986fd29aSsetje #if !defined(_OPB)
2912449e17fSsherrym static void ucode_install();
2922449e17fSsherrym #endif
2932449e17fSsherrym 
2947c478bd9Sstevel@tonic-gate /* Menu related sub commands */
2957c478bd9Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = {
296eb2bd662Svikram 	"set_option",		OPT_ABSENT,	set_option, 0,	/* PUB */
2971a97e40eSvikram 	"list_entry",		OPT_OPTIONAL,	list_entry, 1,	/* PUB */
2981a97e40eSvikram 	"delete_all_entries",	OPT_ABSENT,	delete_all_entries, 0, /* PVT */
2991a97e40eSvikram 	"update_entry",		OPT_REQ,	update_entry, 0, /* menu */
3001a97e40eSvikram 	"update_temp",		OPT_OPTIONAL,	update_temp, 0,	/* reboot */
301ae115bc7Smrj 	"upgrade",		OPT_ABSENT,	upgrade_menu, 0, /* menu */
3021a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
3037c478bd9Sstevel@tonic-gate };
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate /* Archive related sub commands */
3067c478bd9Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = {
3071a97e40eSvikram 	"update",		OPT_ABSENT,	update_archive, 0, /* PUB */
3081a97e40eSvikram 	"update_all",		OPT_ABSENT,	update_all, 0,	/* PVT */
3091a97e40eSvikram 	"list",			OPT_OPTIONAL,	list_archive, 1, /* PUB */
3101a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
3117c478bd9Sstevel@tonic-gate };
3127c478bd9Sstevel@tonic-gate 
31348847494SEnrico Perla - Sun Microsystems enum dircache_copy_opt {
31448847494SEnrico Perla - Sun Microsystems 	FILE32 = 0,
31548847494SEnrico Perla - Sun Microsystems 	FILE64,
31648847494SEnrico Perla - Sun Microsystems 	CACHEDIR_NUM
31748847494SEnrico Perla - Sun Microsystems };
31848847494SEnrico Perla - Sun Microsystems 
31948847494SEnrico Perla - Sun Microsystems /*
32048847494SEnrico Perla - Sun Microsystems  * Directory specific flags:
32148847494SEnrico Perla - Sun Microsystems  * NEED_UPDATE : the specified archive needs to be updated
32248847494SEnrico Perla - Sun Microsystems  * NO_MULTI : don't extend the specified archive, but recreate it
32348847494SEnrico Perla - Sun Microsystems  */
32448847494SEnrico Perla - Sun Microsystems #define	NEED_UPDATE		0x00000001
32548847494SEnrico Perla - Sun Microsystems #define	NO_MULTI		0x00000002
32648847494SEnrico Perla - Sun Microsystems 
32748847494SEnrico Perla - Sun Microsystems #define	set_dir_flag(id, f)	(walk_arg.dirinfo[id].flags |= f)
32848847494SEnrico Perla - Sun Microsystems #define	unset_dir_flag(id, f)	(walk_arg.dirinfo[id].flags &= ~f)
32948847494SEnrico Perla - Sun Microsystems #define	is_dir_flag_on(id, f)	(walk_arg.dirinfo[id].flags & f ? 1 : 0)
33048847494SEnrico Perla - Sun Microsystems 
33148847494SEnrico Perla - Sun Microsystems #define	get_cachedir(id)	(walk_arg.dirinfo[id].cdir_path)
33248847494SEnrico Perla - Sun Microsystems #define	get_updatedir(id)	(walk_arg.dirinfo[id].update_path)
33348847494SEnrico Perla - Sun Microsystems #define	get_count(id)		(walk_arg.dirinfo[id].count)
33448847494SEnrico Perla - Sun Microsystems #define	has_cachedir(id)	(walk_arg.dirinfo[id].has_dir)
33548847494SEnrico Perla - Sun Microsystems #define	set_dir_present(id)	(walk_arg.dirinfo[id].has_dir = 1)
33648847494SEnrico Perla - Sun Microsystems 
33748847494SEnrico Perla - Sun Microsystems /*
33848847494SEnrico Perla - Sun Microsystems  * dirinfo_t (specific cache directory information):
33948847494SEnrico Perla - Sun Microsystems  * cdir_path:   path to the archive cache directory
34048847494SEnrico Perla - Sun Microsystems  * update_path: path to the update directory (contains the files that will be
34148847494SEnrico Perla - Sun Microsystems  *              used to extend the archive)
34248847494SEnrico Perla - Sun Microsystems  * has_dir:	the specified cache directory is active
34348847494SEnrico Perla - Sun Microsystems  * count:	the number of files to update
34448847494SEnrico Perla - Sun Microsystems  * flags:	directory specific flags
34548847494SEnrico Perla - Sun Microsystems  */
34648847494SEnrico Perla - Sun Microsystems typedef struct _dirinfo {
34748847494SEnrico Perla - Sun Microsystems 	char	cdir_path[PATH_MAX];
34848847494SEnrico Perla - Sun Microsystems 	char	update_path[PATH_MAX];
34948847494SEnrico Perla - Sun Microsystems 	int	has_dir;
35048847494SEnrico Perla - Sun Microsystems 	int	count;
35148847494SEnrico Perla - Sun Microsystems 	int	flags;
35248847494SEnrico Perla - Sun Microsystems } dirinfo_t;
35348847494SEnrico Perla - Sun Microsystems 
35448847494SEnrico Perla - Sun Microsystems /*
35548847494SEnrico Perla - Sun Microsystems  * Update flags:
35648847494SEnrico Perla - Sun Microsystems  * NEED_CACHE_DIR : cache directory is missing and needs to be created
35748847494SEnrico Perla - Sun Microsystems  * IS_SPARC_TARGET : the target mountpoint is a SPARC environment
35848847494SEnrico Perla - Sun Microsystems  * UPDATE_ERROR : an error occourred while traversing the list of files
35948847494SEnrico Perla - Sun Microsystems  * RDONLY_FSCHK : the target filesystem is read-only
36048847494SEnrico Perla - Sun Microsystems  * RAMDSK_FSCHK : the target filesystem is on a ramdisk
36148847494SEnrico Perla - Sun Microsystems  */
36248847494SEnrico Perla - Sun Microsystems #define	NEED_CACHE_DIR		0x00000001
36348847494SEnrico Perla - Sun Microsystems #define	IS_SPARC_TARGET		0x00000002
36448847494SEnrico Perla - Sun Microsystems #define	UPDATE_ERROR		0x00000004
36548847494SEnrico Perla - Sun Microsystems #define	RDONLY_FSCHK		0x00000008
36648847494SEnrico Perla - Sun Microsystems 
36748847494SEnrico Perla - Sun Microsystems #define	is_flag_on(flag)	(walk_arg.update_flags & flag ? 1 : 0)
36848847494SEnrico Perla - Sun Microsystems #define	set_flag(flag)		(walk_arg.update_flags |= flag)
36948847494SEnrico Perla - Sun Microsystems #define	unset_flag(flag)	(walk_arg.update_flags &= ~flag)
37048847494SEnrico Perla - Sun Microsystems 
37148847494SEnrico Perla - Sun Microsystems /*
37248847494SEnrico Perla - Sun Microsystems  * struct walk_arg :
37348847494SEnrico Perla - Sun Microsystems  * update_flags: flags related to the current updating process
37448847494SEnrico Perla - Sun Microsystems  * new_nvlp/old_nvlp: new and old list of archive-files / attributes pairs
37548847494SEnrico Perla - Sun Microsystems  * sparcfile: list of file paths for mkisofs -path-list (SPARC only)
37648847494SEnrico Perla - Sun Microsystems  */
3777c478bd9Sstevel@tonic-gate static struct {
37848847494SEnrico Perla - Sun Microsystems 	int 		update_flags;
3797c478bd9Sstevel@tonic-gate 	nvlist_t 	*new_nvlp;
3807c478bd9Sstevel@tonic-gate 	nvlist_t 	*old_nvlp;
38148847494SEnrico Perla - Sun Microsystems 	FILE 		*sparcfile;
38248847494SEnrico Perla - Sun Microsystems 	dirinfo_t	dirinfo[CACHEDIR_NUM];
3837c478bd9Sstevel@tonic-gate } walk_arg;
3847c478bd9Sstevel@tonic-gate 
3859632cfadSsetje struct safefile {
38658091fd8Ssetje 	char *name;
38758091fd8Ssetje 	struct safefile *next;
38858091fd8Ssetje };
38958091fd8Ssetje 
390ae115bc7Smrj static struct safefile *safefiles = NULL;
39158091fd8Ssetje #define	NEED_UPDATE_FILE "/etc/svc/volatile/boot_archive_needs_update"
39258091fd8Ssetje 
39348847494SEnrico Perla - Sun Microsystems /* Thanks growisofs */
39448847494SEnrico Perla - Sun Microsystems #define	CD_BLOCK	((off64_t)2048)
39548847494SEnrico Perla - Sun Microsystems #define	VOLDESC_OFF	16
39648847494SEnrico Perla - Sun Microsystems #define	DVD_BLOCK	(32*1024)
39748847494SEnrico Perla - Sun Microsystems #define	MAX_IVDs	16
39848847494SEnrico Perla - Sun Microsystems 
39948847494SEnrico Perla - Sun Microsystems struct iso_pdesc {
40048847494SEnrico Perla - Sun Microsystems     unsigned char type	[1];
40148847494SEnrico Perla - Sun Microsystems     unsigned char id	[5];
40248847494SEnrico Perla - Sun Microsystems     unsigned char void1	[80-5-1];
40348847494SEnrico Perla - Sun Microsystems     unsigned char volume_space_size [8];
40448847494SEnrico Perla - Sun Microsystems     unsigned char void2	[2048-80-8];
40548847494SEnrico Perla - Sun Microsystems };
40648847494SEnrico Perla - Sun Microsystems 
40748847494SEnrico Perla - Sun Microsystems /*
40848847494SEnrico Perla - Sun Microsystems  * COUNT_MAX:	maximum number of changed files to justify a multisession update
40948847494SEnrico Perla - Sun Microsystems  * BA_SIZE_MAX:	maximum size of the boot_archive to justify a multisession
41048847494SEnrico Perla - Sun Microsystems  * 		update
41148847494SEnrico Perla - Sun Microsystems  */
41248847494SEnrico Perla - Sun Microsystems #define	COUNT_MAX		50
41348847494SEnrico Perla - Sun Microsystems #define	BA_SIZE_MAX		(50 * 1024 * 1024)
41448847494SEnrico Perla - Sun Microsystems 
41548847494SEnrico Perla - Sun Microsystems #define	bam_nowrite()		(bam_check || bam_smf_check)
41648847494SEnrico Perla - Sun Microsystems 
41719397407SSherry Moore static int sync_menu = 1;	/* whether we need to sync the BE menus */
41819397407SSherry Moore 
4197c478bd9Sstevel@tonic-gate static void
4207c478bd9Sstevel@tonic-gate usage(void)
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "USAGE:\n");
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	/* archive usage */
425d876c67dSjg 	(void) fprintf(stderr,
426d876c67dSjg 	    "\t%s update-archive [-vn] [-R altroot [-p platform>]]\n", prog);
427d876c67dSjg 	(void) fprintf(stderr,
428d876c67dSjg 	    "\t%s list-archive [-R altroot [-p platform>]]\n", prog);
429986fd29aSsetje #if !defined(_OPB)
4307c478bd9Sstevel@tonic-gate 	/* x86 only */
4317c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog);
4327c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog);
4337c478bd9Sstevel@tonic-gate #endif
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
436*cedc7e57SEnrico Perla - Sun Microsystems /*
437*cedc7e57SEnrico Perla - Sun Microsystems  * Best effort attempt to restore the $HOME value.
438*cedc7e57SEnrico Perla - Sun Microsystems  */
439*cedc7e57SEnrico Perla - Sun Microsystems static void
440*cedc7e57SEnrico Perla - Sun Microsystems restore_env()
441*cedc7e57SEnrico Perla - Sun Microsystems {
442*cedc7e57SEnrico Perla - Sun Microsystems 	char	home_env[PATH_MAX];
443*cedc7e57SEnrico Perla - Sun Microsystems 
444*cedc7e57SEnrico Perla - Sun Microsystems 	if (bam_home_env) {
445*cedc7e57SEnrico Perla - Sun Microsystems 		(void) snprintf(home_env, sizeof (home_env), "HOME=%s",
446*cedc7e57SEnrico Perla - Sun Microsystems 		    bam_home_env);
447*cedc7e57SEnrico Perla - Sun Microsystems 		(void) putenv(home_env);
448*cedc7e57SEnrico Perla - Sun Microsystems 	}
449*cedc7e57SEnrico Perla - Sun Microsystems }
450*cedc7e57SEnrico Perla - Sun Microsystems 
451*cedc7e57SEnrico Perla - Sun Microsystems 
452*cedc7e57SEnrico Perla - Sun Microsystems #define		SLEEP_TIME	5
453*cedc7e57SEnrico Perla - Sun Microsystems #define		MAX_TRIES	4
454*cedc7e57SEnrico Perla - Sun Microsystems 
455*cedc7e57SEnrico Perla - Sun Microsystems /*
456*cedc7e57SEnrico Perla - Sun Microsystems  * Sanitize the environment in which bootadm will execute its sub-processes
457*cedc7e57SEnrico Perla - Sun Microsystems  * (ex. mkisofs). This is done to prevent those processes from attempting
458*cedc7e57SEnrico Perla - Sun Microsystems  * to access files (ex. .mkisofsrc) or stat paths that might be on NFS
459*cedc7e57SEnrico Perla - Sun Microsystems  * or, potentially, insecure.
460*cedc7e57SEnrico Perla - Sun Microsystems  */
461*cedc7e57SEnrico Perla - Sun Microsystems static void
462*cedc7e57SEnrico Perla - Sun Microsystems sanitize_env()
463*cedc7e57SEnrico Perla - Sun Microsystems {
464*cedc7e57SEnrico Perla - Sun Microsystems 	int	stry = 0;
465*cedc7e57SEnrico Perla - Sun Microsystems 
466*cedc7e57SEnrico Perla - Sun Microsystems 	/* don't depend on caller umask */
467*cedc7e57SEnrico Perla - Sun Microsystems 	(void) umask(0022);
468*cedc7e57SEnrico Perla - Sun Microsystems 
469*cedc7e57SEnrico Perla - Sun Microsystems 	/* move away from a potential unsafe current working directory */
470*cedc7e57SEnrico Perla - Sun Microsystems 	while (chdir("/") == -1) {
471*cedc7e57SEnrico Perla - Sun Microsystems 		if (errno != EINTR) {
472*cedc7e57SEnrico Perla - Sun Microsystems 			bam_print("WARNING: unable to chdir to /");
473*cedc7e57SEnrico Perla - Sun Microsystems 			break;
474*cedc7e57SEnrico Perla - Sun Microsystems 		}
475*cedc7e57SEnrico Perla - Sun Microsystems 	}
476*cedc7e57SEnrico Perla - Sun Microsystems 
477*cedc7e57SEnrico Perla - Sun Microsystems 	bam_home_env = getenv("HOME");
478*cedc7e57SEnrico Perla - Sun Microsystems 	while (bam_home_env != NULL && putenv("HOME=/") == -1) {
479*cedc7e57SEnrico Perla - Sun Microsystems 		if (errno == ENOMEM) {
480*cedc7e57SEnrico Perla - Sun Microsystems 			/* retry no more than MAX_TRIES times */
481*cedc7e57SEnrico Perla - Sun Microsystems 			if (++stry > MAX_TRIES) {
482*cedc7e57SEnrico Perla - Sun Microsystems 				bam_print("WARNING: unable to recover from "
483*cedc7e57SEnrico Perla - Sun Microsystems 				    "system memory pressure... aborting \n");
484*cedc7e57SEnrico Perla - Sun Microsystems 				bam_exit(EXIT_FAILURE);
485*cedc7e57SEnrico Perla - Sun Microsystems 			}
486*cedc7e57SEnrico Perla - Sun Microsystems 			/* memory is tight, try to sleep */
487*cedc7e57SEnrico Perla - Sun Microsystems 			bam_print("Attempting to recover from memory pressure: "
488*cedc7e57SEnrico Perla - Sun Microsystems 			    "sleeping for %d seconds\n", SLEEP_TIME * stry);
489*cedc7e57SEnrico Perla - Sun Microsystems 			(void) sleep(SLEEP_TIME * stry);
490*cedc7e57SEnrico Perla - Sun Microsystems 		} else {
491*cedc7e57SEnrico Perla - Sun Microsystems 			bam_print("WARNING: unable to sanitize HOME\n");
492*cedc7e57SEnrico Perla - Sun Microsystems 		}
493*cedc7e57SEnrico Perla - Sun Microsystems 	}
494*cedc7e57SEnrico Perla - Sun Microsystems }
495*cedc7e57SEnrico Perla - Sun Microsystems 
4967c478bd9Sstevel@tonic-gate int
4977c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	error_t ret;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
5027c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
5057c478bd9Sstevel@tonic-gate 		prog = argv[0];
5067c478bd9Sstevel@tonic-gate 	} else {
5077c478bd9Sstevel@tonic-gate 		prog++;
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 
510eb2bd662Svikram 	INJECT_ERROR1("ASSERT_ON", assert(0))
5117c478bd9Sstevel@tonic-gate 
512*cedc7e57SEnrico Perla - Sun Microsystems 	sanitize_env();
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	switch (bam_cmd) {
5177c478bd9Sstevel@tonic-gate 		case BAM_MENU:
5187c478bd9Sstevel@tonic-gate 			ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv);
5197c478bd9Sstevel@tonic-gate 			break;
5207c478bd9Sstevel@tonic-gate 		case BAM_ARCHIVE:
5217c478bd9Sstevel@tonic-gate 			ret = bam_archive(bam_subcmd, bam_opt);
5227c478bd9Sstevel@tonic-gate 			break;
5237c478bd9Sstevel@tonic-gate 		default:
5247c478bd9Sstevel@tonic-gate 			usage();
5257c478bd9Sstevel@tonic-gate 			bam_exit(1);
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS)
5297c478bd9Sstevel@tonic-gate 		bam_exit(1);
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	bam_unlock();
5327c478bd9Sstevel@tonic-gate 	return (0);
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate /*
5367c478bd9Sstevel@tonic-gate  * Equivalence of public and internal commands:
5377c478bd9Sstevel@tonic-gate  *	update-archive  -- -a update
5387c478bd9Sstevel@tonic-gate  *	list-archive	-- -a list
5397c478bd9Sstevel@tonic-gate  *	set-menu	-- -m set_option
5407c478bd9Sstevel@tonic-gate  *	list-menu	-- -m list_entry
5417c478bd9Sstevel@tonic-gate  *	update-menu	-- -m update_entry
5427c478bd9Sstevel@tonic-gate  */
5437c478bd9Sstevel@tonic-gate static struct cmd_map {
5447c478bd9Sstevel@tonic-gate 	char *bam_cmdname;
5457c478bd9Sstevel@tonic-gate 	int bam_cmd;
5467c478bd9Sstevel@tonic-gate 	char *bam_subcmd;
5477c478bd9Sstevel@tonic-gate } cmd_map[] = {
5487c478bd9Sstevel@tonic-gate 	{ "update-archive",	BAM_ARCHIVE,	"update"},
5497c478bd9Sstevel@tonic-gate 	{ "list-archive",	BAM_ARCHIVE,	"list"},
5507c478bd9Sstevel@tonic-gate 	{ "set-menu",		BAM_MENU,	"set_option"},
5517c478bd9Sstevel@tonic-gate 	{ "list-menu",		BAM_MENU,	"list_entry"},
5527c478bd9Sstevel@tonic-gate 	{ "update-menu",	BAM_MENU,	"update_entry"},
5537c478bd9Sstevel@tonic-gate 	{ NULL,			0,		NULL}
5547c478bd9Sstevel@tonic-gate };
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate  * Commands syntax published in bootadm(1M) are parsed here
5587c478bd9Sstevel@tonic-gate  */
5597c478bd9Sstevel@tonic-gate static void
5607c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[])
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate 	struct cmd_map *cmp = cmd_map;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	/* command conforming to the final spec */
5657c478bd9Sstevel@tonic-gate 	if (argc > 1 && argv[1][0] != '-') {
5667c478bd9Sstevel@tonic-gate 		/*
5677c478bd9Sstevel@tonic-gate 		 * Map commands to internal table.
5687c478bd9Sstevel@tonic-gate 		 */
5697c478bd9Sstevel@tonic-gate 		while (cmp->bam_cmdname) {
5707c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], cmp->bam_cmdname) == 0) {
5717c478bd9Sstevel@tonic-gate 				bam_cmd = cmp->bam_cmd;
5727c478bd9Sstevel@tonic-gate 				bam_subcmd = cmp->bam_subcmd;
5737c478bd9Sstevel@tonic-gate 				break;
5747c478bd9Sstevel@tonic-gate 			}
5757c478bd9Sstevel@tonic-gate 			cmp++;
5767c478bd9Sstevel@tonic-gate 		}
5777c478bd9Sstevel@tonic-gate 		if (cmp->bam_cmdname == NULL) {
5787c478bd9Sstevel@tonic-gate 			usage();
5797c478bd9Sstevel@tonic-gate 			bam_exit(1);
5807c478bd9Sstevel@tonic-gate 		}
5817c478bd9Sstevel@tonic-gate 		argc--;
5827c478bd9Sstevel@tonic-gate 		argv++;
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	parse_args_internal(argc, argv);
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate /*
5897c478bd9Sstevel@tonic-gate  * A combination of public and private commands are parsed here.
5907c478bd9Sstevel@tonic-gate  * The internal syntax and the corresponding functionality are:
5917c478bd9Sstevel@tonic-gate  *	-a update	-- update-archive
5927c478bd9Sstevel@tonic-gate  *	-a list		-- list-archive
5937c478bd9Sstevel@tonic-gate  *	-a update-all	-- (reboot to sync all mounted OS archive)
5947c478bd9Sstevel@tonic-gate  *	-m update_entry	-- update-menu
5957c478bd9Sstevel@tonic-gate  *	-m list_entry	-- list-menu
5967c478bd9Sstevel@tonic-gate  *	-m update_temp	-- (reboot -- [boot-args])
5977c478bd9Sstevel@tonic-gate  *	-m delete_all_entries -- (called from install)
59848847494SEnrico Perla - Sun Microsystems  * A set of private flags is there too:
59948847494SEnrico Perla - Sun Microsystems  *	-F		-- purge the cache directories and rebuild them
60048847494SEnrico Perla - Sun Microsystems  *	-e		-- use the (faster) archive update approach (used by
60148847494SEnrico Perla - Sun Microsystems  *			   reboot)
6027c478bd9Sstevel@tonic-gate  */
6037c478bd9Sstevel@tonic-gate static void
6047c478bd9Sstevel@tonic-gate parse_args_internal(int argc, char *argv[])
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	int c, error;
6077c478bd9Sstevel@tonic-gate 	extern char *optarg;
6087c478bd9Sstevel@tonic-gate 	extern int optind, opterr;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	/* Suppress error message from getopt */
6117c478bd9Sstevel@tonic-gate 	opterr = 0;
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	error = 0;
61448847494SEnrico Perla - Sun Microsystems 	while ((c = getopt(argc, argv, "a:d:fm:no:veFCR:p:Z")) != -1) {
6157c478bd9Sstevel@tonic-gate 		switch (c) {
6167c478bd9Sstevel@tonic-gate 		case 'a':
6177c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
6187c478bd9Sstevel@tonic-gate 				error = 1;
6197c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
6207c478bd9Sstevel@tonic-gate 			}
6217c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_ARCHIVE;
6227c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
6237c478bd9Sstevel@tonic-gate 			break;
6247c478bd9Sstevel@tonic-gate 		case 'd':
6257c478bd9Sstevel@tonic-gate 			if (bam_debug) {
6267c478bd9Sstevel@tonic-gate 				error = 1;
6277c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
6287c478bd9Sstevel@tonic-gate 			}
6297c478bd9Sstevel@tonic-gate 			bam_debug = s_strtol(optarg);
6307c478bd9Sstevel@tonic-gate 			break;
6317c478bd9Sstevel@tonic-gate 		case 'f':
6327c478bd9Sstevel@tonic-gate 			bam_force = 1;
6337c478bd9Sstevel@tonic-gate 			break;
63448847494SEnrico Perla - Sun Microsystems 		case 'F':
63548847494SEnrico Perla - Sun Microsystems 			bam_purge = 1;
63648847494SEnrico Perla - Sun Microsystems 			break;
6377c478bd9Sstevel@tonic-gate 		case 'm':
6387c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
6397c478bd9Sstevel@tonic-gate 				error = 1;
6407c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
6417c478bd9Sstevel@tonic-gate 			}
6427c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_MENU;
6437c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
6447c478bd9Sstevel@tonic-gate 			break;
6457c478bd9Sstevel@tonic-gate 		case 'n':
6467c478bd9Sstevel@tonic-gate 			bam_check = 1;
6477c478bd9Sstevel@tonic-gate 			break;
6487c478bd9Sstevel@tonic-gate 		case 'o':
6497c478bd9Sstevel@tonic-gate 			if (bam_opt) {
6507c478bd9Sstevel@tonic-gate 				error = 1;
6517c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
6527c478bd9Sstevel@tonic-gate 			}
6537c478bd9Sstevel@tonic-gate 			bam_opt = optarg;
6547c478bd9Sstevel@tonic-gate 			break;
6557c478bd9Sstevel@tonic-gate 		case 'v':
6567c478bd9Sstevel@tonic-gate 			bam_verbose = 1;
6577c478bd9Sstevel@tonic-gate 			break;
6588c1b6884Sszhou 		case 'C':
6598c1b6884Sszhou 			bam_smf_check = 1;
6608c1b6884Sszhou 			break;
6617c478bd9Sstevel@tonic-gate 		case 'R':
6627c478bd9Sstevel@tonic-gate 			if (bam_root) {
6637c478bd9Sstevel@tonic-gate 				error = 1;
6647c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
6657c478bd9Sstevel@tonic-gate 				break;
6667c478bd9Sstevel@tonic-gate 			} else if (realpath(optarg, rootbuf) == NULL) {
6677c478bd9Sstevel@tonic-gate 				error = 1;
6687c478bd9Sstevel@tonic-gate 				bam_error(CANT_RESOLVE, optarg,
6697c478bd9Sstevel@tonic-gate 				    strerror(errno));
6707c478bd9Sstevel@tonic-gate 				break;
6717c478bd9Sstevel@tonic-gate 			}
67240541d5dSvikram 			bam_alt_root = 1;
6737c478bd9Sstevel@tonic-gate 			bam_root = rootbuf;
6747c478bd9Sstevel@tonic-gate 			bam_rootlen = strlen(rootbuf);
6757c478bd9Sstevel@tonic-gate 			break;
676d876c67dSjg 		case 'p':
677d876c67dSjg 			bam_alt_platform = 1;
678d876c67dSjg 			bam_platform = optarg;
679d876c67dSjg 			if ((strcmp(bam_platform, "i86pc") != 0) &&
680d876c67dSjg 			    (strcmp(bam_platform, "sun4u") != 0) &&
681d876c67dSjg 			    (strcmp(bam_platform, "sun4v") != 0)) {
682d876c67dSjg 				error = 1;
683d876c67dSjg 				bam_error(INVALID_PLAT, bam_platform);
684d876c67dSjg 			}
685d876c67dSjg 			break;
686e7cbe64fSgw25295 		case 'Z':
687e7cbe64fSgw25295 			bam_zfs = 1;
688e7cbe64fSgw25295 			break;
68948847494SEnrico Perla - Sun Microsystems 		case 'e':
69048847494SEnrico Perla - Sun Microsystems 			bam_extend = 1;
69148847494SEnrico Perla - Sun Microsystems 			break;
6927c478bd9Sstevel@tonic-gate 		case '?':
6937c478bd9Sstevel@tonic-gate 			error = 1;
6947c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, optopt);
6957c478bd9Sstevel@tonic-gate 			break;
6967c478bd9Sstevel@tonic-gate 		default :
6977c478bd9Sstevel@tonic-gate 			error = 1;
6987c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, c);
6997c478bd9Sstevel@tonic-gate 			break;
7007c478bd9Sstevel@tonic-gate 		}
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	/*
704d876c67dSjg 	 * An alternate platform requires an alternate root
705d876c67dSjg 	 */
706d876c67dSjg 	if (bam_alt_platform && bam_alt_root == 0) {
707d876c67dSjg 		usage();
708d876c67dSjg 		bam_exit(0);
709d876c67dSjg 	}
710d876c67dSjg 
711d876c67dSjg 	/*
7127c478bd9Sstevel@tonic-gate 	 * A command option must be specfied
7137c478bd9Sstevel@tonic-gate 	 */
7147c478bd9Sstevel@tonic-gate 	if (!bam_cmd) {
7157c478bd9Sstevel@tonic-gate 		if (bam_opt && strcmp(bam_opt, "all") == 0) {
7167c478bd9Sstevel@tonic-gate 			usage();
7177c478bd9Sstevel@tonic-gate 			bam_exit(0);
7187c478bd9Sstevel@tonic-gate 		}
7197c478bd9Sstevel@tonic-gate 		bam_error(NEED_CMD);
7207c478bd9Sstevel@tonic-gate 		error = 1;
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	if (error) {
7247c478bd9Sstevel@tonic-gate 		usage();
7257c478bd9Sstevel@tonic-gate 		bam_exit(1);
7267c478bd9Sstevel@tonic-gate 	}
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	if (optind > argc) {
7297c478bd9Sstevel@tonic-gate 		bam_error(INT_ERROR, "parse_args");
7307c478bd9Sstevel@tonic-gate 		bam_exit(1);
7317c478bd9Sstevel@tonic-gate 	} else if (optind < argc) {
7327c478bd9Sstevel@tonic-gate 		bam_argv = &argv[optind];
7337c478bd9Sstevel@tonic-gate 		bam_argc = argc - optind;
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	/*
7377c478bd9Sstevel@tonic-gate 	 * -n implies verbose mode
7387c478bd9Sstevel@tonic-gate 	 */
7397c478bd9Sstevel@tonic-gate 	if (bam_check)
7407c478bd9Sstevel@tonic-gate 		bam_verbose = 1;
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate static error_t
7447c478bd9Sstevel@tonic-gate check_subcmd_and_options(
7457c478bd9Sstevel@tonic-gate 	char *subcmd,
7467c478bd9Sstevel@tonic-gate 	char *opt,
7477c478bd9Sstevel@tonic-gate 	subcmd_defn_t *table,
7487c478bd9Sstevel@tonic-gate 	error_t (**fp)())
7497c478bd9Sstevel@tonic-gate {
7507c478bd9Sstevel@tonic-gate 	int i;
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	if (subcmd == NULL) {
7537c478bd9Sstevel@tonic-gate 		bam_error(NEED_SUBCMD);
7547c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 
757eb2bd662Svikram 	if (strcmp(subcmd, "set_option") == 0) {
758eb2bd662Svikram 		if (bam_argc == 0 || bam_argv == NULL || bam_argv[0] == NULL) {
759eb2bd662Svikram 			bam_error(MISSING_ARG);
760eb2bd662Svikram 			usage();
761eb2bd662Svikram 			return (BAM_ERROR);
762eb2bd662Svikram 		} else if (bam_argc > 1 || bam_argv[1] != NULL) {
7631a97e40eSvikram 			bam_error(TRAILING_ARGS);
7641a97e40eSvikram 			usage();
7651a97e40eSvikram 			return (BAM_ERROR);
7661a97e40eSvikram 		}
76719397407SSherry Moore 	} else if (strcmp(subcmd, "update_all") == 0) {
76819397407SSherry Moore 		/*
76919397407SSherry Moore 		 * The only option we accept for the "update_all"
77019397407SSherry Moore 		 * subcmd is "fastboot".
77119397407SSherry Moore 		 */
77219397407SSherry Moore 		if (bam_argc > 1 || (bam_argc == 1 &&
77319397407SSherry Moore 		    strcmp(bam_argv[0], "fastboot") != 0)) {
77419397407SSherry Moore 			bam_error(TRAILING_ARGS);
77519397407SSherry Moore 			usage();
77619397407SSherry Moore 			return (BAM_ERROR);
77719397407SSherry Moore 		}
77819397407SSherry Moore 		if (bam_argc == 1)
77919397407SSherry Moore 			sync_menu = 0;
780eb2bd662Svikram 	} else if (bam_argc || bam_argv) {
781eb2bd662Svikram 		bam_error(TRAILING_ARGS);
782eb2bd662Svikram 		usage();
783eb2bd662Svikram 		return (BAM_ERROR);
7841a97e40eSvikram 	}
7851a97e40eSvikram 
7867c478bd9Sstevel@tonic-gate 	if (bam_root == NULL) {
7877c478bd9Sstevel@tonic-gate 		bam_root = rootbuf;
7887c478bd9Sstevel@tonic-gate 		bam_rootlen = 1;
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	/* verify that subcmd is valid */
7927c478bd9Sstevel@tonic-gate 	for (i = 0; table[i].subcmd != NULL; i++) {
7937c478bd9Sstevel@tonic-gate 		if (strcmp(table[i].subcmd, subcmd) == 0)
7947c478bd9Sstevel@tonic-gate 			break;
7957c478bd9Sstevel@tonic-gate 	}
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	if (table[i].subcmd == NULL) {
7987c478bd9Sstevel@tonic-gate 		bam_error(INVALID_SUBCMD, subcmd);
7997c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
8007c478bd9Sstevel@tonic-gate 	}
8017c478bd9Sstevel@tonic-gate 
8021a97e40eSvikram 	if (table[i].unpriv == 0 && geteuid() != 0) {
8031a97e40eSvikram 		bam_error(MUST_BE_ROOT);
8041a97e40eSvikram 		return (BAM_ERROR);
8051a97e40eSvikram 	}
8061a97e40eSvikram 
8071a97e40eSvikram 	/*
8081a97e40eSvikram 	 * Currently only privileged commands need a lock
8091a97e40eSvikram 	 */
8101a97e40eSvikram 	if (table[i].unpriv == 0)
8111a97e40eSvikram 		bam_lock();
8121a97e40eSvikram 
8137c478bd9Sstevel@tonic-gate 	/* subcmd verifies that opt is appropriate */
8147c478bd9Sstevel@tonic-gate 	if (table[i].option != OPT_OPTIONAL) {
8157c478bd9Sstevel@tonic-gate 		if ((table[i].option == OPT_REQ) ^ (opt != NULL)) {
8167c478bd9Sstevel@tonic-gate 			if (opt)
8177c478bd9Sstevel@tonic-gate 				bam_error(NO_OPT_REQ, subcmd);
8187c478bd9Sstevel@tonic-gate 			else
8197c478bd9Sstevel@tonic-gate 				bam_error(MISS_OPT, subcmd);
8207c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
8217c478bd9Sstevel@tonic-gate 		}
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	*fp = table[i].handler;
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate 
82940541d5dSvikram /*
83040541d5dSvikram  * NOTE: A single "/" is also considered a trailing slash and will
83140541d5dSvikram  * be deleted.
83240541d5dSvikram  */
83340541d5dSvikram static void
83440541d5dSvikram elide_trailing_slash(const char *src, char *dst, size_t dstsize)
83540541d5dSvikram {
83640541d5dSvikram 	size_t dstlen;
83740541d5dSvikram 
83840541d5dSvikram 	assert(src);
83940541d5dSvikram 	assert(dst);
84040541d5dSvikram 
84140541d5dSvikram 	(void) strlcpy(dst, src, dstsize);
84240541d5dSvikram 
84340541d5dSvikram 	dstlen = strlen(dst);
84440541d5dSvikram 	if (dst[dstlen - 1] == '/') {
84540541d5dSvikram 		dst[dstlen - 1] = '\0';
84640541d5dSvikram 	}
84740541d5dSvikram }
84840541d5dSvikram 
849*cedc7e57SEnrico Perla - Sun Microsystems static int
850*cedc7e57SEnrico Perla - Sun Microsystems is_safe_exec(char *path)
851*cedc7e57SEnrico Perla - Sun Microsystems {
852*cedc7e57SEnrico Perla - Sun Microsystems 	struct stat	sb;
853*cedc7e57SEnrico Perla - Sun Microsystems 
854*cedc7e57SEnrico Perla - Sun Microsystems 	if (lstat(path, &sb) != 0) {
855*cedc7e57SEnrico Perla - Sun Microsystems 		bam_error(STAT_FAIL, path, strerror(errno));
856*cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
857*cedc7e57SEnrico Perla - Sun Microsystems 	}
858*cedc7e57SEnrico Perla - Sun Microsystems 
859*cedc7e57SEnrico Perla - Sun Microsystems 	if (!S_ISREG(sb.st_mode)) {
860*cedc7e57SEnrico Perla - Sun Microsystems 		bam_error(PATH_EXEC_LINK, path);
861*cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
862*cedc7e57SEnrico Perla - Sun Microsystems 	}
863*cedc7e57SEnrico Perla - Sun Microsystems 
864*cedc7e57SEnrico Perla - Sun Microsystems 	if (sb.st_uid != getuid()) {
865*cedc7e57SEnrico Perla - Sun Microsystems 		bam_error(PATH_EXEC_OWNER, path, getuid());
866*cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
867*cedc7e57SEnrico Perla - Sun Microsystems 	}
868*cedc7e57SEnrico Perla - Sun Microsystems 
869*cedc7e57SEnrico Perla - Sun Microsystems 	if (sb.st_mode & S_IWOTH || sb.st_mode & S_IWGRP) {
870*cedc7e57SEnrico Perla - Sun Microsystems 		bam_error(PATH_EXEC_PERMS, path);
871*cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
872*cedc7e57SEnrico Perla - Sun Microsystems 	}
873*cedc7e57SEnrico Perla - Sun Microsystems 
874*cedc7e57SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
875*cedc7e57SEnrico Perla - Sun Microsystems }
876*cedc7e57SEnrico Perla - Sun Microsystems 
8777c478bd9Sstevel@tonic-gate static error_t
8787c478bd9Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[])
8797c478bd9Sstevel@tonic-gate {
8807c478bd9Sstevel@tonic-gate 	error_t			ret;
8817c478bd9Sstevel@tonic-gate 	char			menu_path[PATH_MAX];
882eb2bd662Svikram 	char			clean_menu_root[PATH_MAX];
883ae115bc7Smrj 	char			path[PATH_MAX];
8847c478bd9Sstevel@tonic-gate 	menu_t			*menu;
885eb2bd662Svikram 	char			menu_root[PATH_MAX];
886b610f78eSvikram 	struct stat		sb;
8877c478bd9Sstevel@tonic-gate 	error_t (*f)(menu_t *mp, char *menu_path, char *opt);
888eb2bd662Svikram 	char			*special;
889eb2bd662Svikram 	char			*pool = NULL;
890eb2bd662Svikram 	zfs_mnted_t		zmnted;
891eb2bd662Svikram 	char			*zmntpt;
892eb2bd662Svikram 	char			*osdev;
893eb2bd662Svikram 	char			*osroot;
894eb2bd662Svikram 	const char		*fcn = "bam_menu()";
895986fd29aSsetje 
896986fd29aSsetje 	/*
897986fd29aSsetje 	 * Menu sub-command only applies to GRUB (i.e. x86)
898986fd29aSsetje 	 */
899eb2bd662Svikram 	if (!is_grub(bam_alt_root ? bam_root : "/")) {
900eb2bd662Svikram 		bam_error(NOT_GRUB_BOOT);
901986fd29aSsetje 		return (BAM_ERROR);
902986fd29aSsetje 	}
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	/*
9057c478bd9Sstevel@tonic-gate 	 * Check arguments
9067c478bd9Sstevel@tonic-gate 	 */
9077c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
9087c478bd9Sstevel@tonic-gate 	if (ret == BAM_ERROR) {
9097c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 
912eb2bd662Svikram 	assert(bam_root);
913eb2bd662Svikram 
914eb2bd662Svikram 	(void) strlcpy(menu_root, bam_root, sizeof (menu_root));
915eb2bd662Svikram 	osdev = osroot = NULL;
916eb2bd662Svikram 
917eb2bd662Svikram 	if (strcmp(subcmd, "update_entry") == 0) {
918eb2bd662Svikram 		assert(opt);
919eb2bd662Svikram 
920eb2bd662Svikram 		osdev = strtok(opt, ",");
921eb2bd662Svikram 		assert(osdev);
922eb2bd662Svikram 		osroot = strtok(NULL, ",");
923eb2bd662Svikram 		if (osroot) {
924eb2bd662Svikram 			/* fixup bam_root so that it points at osroot */
925eb2bd662Svikram 			if (realpath(osroot, rootbuf) == NULL) {
926eb2bd662Svikram 				bam_error(CANT_RESOLVE, osroot,
927eb2bd662Svikram 				    strerror(errno));
928eb2bd662Svikram 				return (BAM_ERROR);
929eb2bd662Svikram 			}
930eb2bd662Svikram 			bam_alt_root = 1;
931eb2bd662Svikram 			bam_root  = rootbuf;
932eb2bd662Svikram 			bam_rootlen = strlen(rootbuf);
933eb2bd662Svikram 		}
934eb2bd662Svikram 	}
93540541d5dSvikram 
93640541d5dSvikram 	/*
937eb2bd662Svikram 	 * We support menu on PCFS (under certain conditions), but
938eb2bd662Svikram 	 * not the OS root
93940541d5dSvikram 	 */
940eb2bd662Svikram 	if (is_pcfs(bam_root)) {
941eb2bd662Svikram 		bam_error(PCFS_ROOT_NOTSUP, bam_root);
942eb2bd662Svikram 		return (BAM_ERROR);
943ae115bc7Smrj 	}
944ae115bc7Smrj 
945eb2bd662Svikram 	if (stat(menu_root, &sb) == -1) {
94640541d5dSvikram 		bam_error(CANNOT_LOCATE_GRUB_MENU);
94740541d5dSvikram 		return (BAM_ERROR);
94840541d5dSvikram 	}
94940541d5dSvikram 
950eb2bd662Svikram 	BAM_DPRINTF((D_MENU_ROOT, fcn, menu_root));
951e7cbe64fSgw25295 
952eb2bd662Svikram 	/*
953eb2bd662Svikram 	 * We no longer use the GRUB slice file. If it exists, then
954eb2bd662Svikram 	 * the user is doing something that is unsupported (such as
955eb2bd662Svikram 	 * standard upgrading an old Live Upgrade BE). If that
956eb2bd662Svikram 	 * happens, mimic existing behavior i.e. pretend that it is
957eb2bd662Svikram 	 * not a BE. Emit a warning though.
958eb2bd662Svikram 	 */
959eb2bd662Svikram 	if (bam_alt_root) {
960eb2bd662Svikram 		(void) snprintf(path, sizeof (path), "%s%s", bam_root,
961eb2bd662Svikram 		    GRUB_slice);
962eb2bd662Svikram 	} else {
963eb2bd662Svikram 		(void) snprintf(path, sizeof (path), "%s", GRUB_slice);
964e7cbe64fSgw25295 	}
965e7cbe64fSgw25295 
96637eb779cSVikram Hegde 	if (bam_verbose && stat(path, &sb) == 0)
967eb2bd662Svikram 		bam_error(GRUB_SLICE_FILE_EXISTS, path);
968eb2bd662Svikram 
969eb2bd662Svikram 	if (is_zfs(menu_root)) {
970eb2bd662Svikram 		assert(strcmp(menu_root, bam_root) == 0);
971eb2bd662Svikram 		special = get_special(menu_root);
972eb2bd662Svikram 		INJECT_ERROR1("Z_MENU_GET_SPECIAL", special = NULL);
973eb2bd662Svikram 		if (special == NULL) {
974eb2bd662Svikram 			bam_error(CANT_FIND_SPECIAL, menu_root);
975eb2bd662Svikram 			return (BAM_ERROR);
976eb2bd662Svikram 		}
977eb2bd662Svikram 		pool = strtok(special, "/");
978eb2bd662Svikram 		INJECT_ERROR1("Z_MENU_GET_POOL", pool = NULL);
979eb2bd662Svikram 		if (pool == NULL) {
980eb2bd662Svikram 			free(special);
981eb2bd662Svikram 			bam_error(CANT_FIND_POOL, menu_root);
982eb2bd662Svikram 			return (BAM_ERROR);
983eb2bd662Svikram 		}
984eb2bd662Svikram 		BAM_DPRINTF((D_Z_MENU_GET_POOL_FROM_SPECIAL, fcn, pool));
985eb2bd662Svikram 
986eb2bd662Svikram 		zmntpt = mount_top_dataset(pool, &zmnted);
987eb2bd662Svikram 		INJECT_ERROR1("Z_MENU_MOUNT_TOP_DATASET", zmntpt = NULL);
988eb2bd662Svikram 		if (zmntpt == NULL) {
989eb2bd662Svikram 			bam_error(CANT_MOUNT_POOL_DATASET, pool);
990eb2bd662Svikram 			free(special);
991eb2bd662Svikram 			return (BAM_ERROR);
992eb2bd662Svikram 		}
993eb2bd662Svikram 		BAM_DPRINTF((D_Z_GET_MENU_MOUNT_TOP_DATASET, fcn, zmntpt));
994eb2bd662Svikram 
995eb2bd662Svikram 		(void) strlcpy(menu_root, zmntpt, sizeof (menu_root));
996eb2bd662Svikram 		BAM_DPRINTF((D_Z_GET_MENU_MENU_ROOT, fcn, menu_root));
997eb2bd662Svikram 	}
998eb2bd662Svikram 
999eb2bd662Svikram 	elide_trailing_slash(menu_root, clean_menu_root,
1000eb2bd662Svikram 	    sizeof (clean_menu_root));
1001eb2bd662Svikram 
1002eb2bd662Svikram 	BAM_DPRINTF((D_CLEAN_MENU_ROOT, fcn, clean_menu_root));
1003eb2bd662Svikram 
1004eb2bd662Svikram 	(void) strlcpy(menu_path, clean_menu_root, sizeof (menu_path));
100540541d5dSvikram 	(void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path));
100640541d5dSvikram 
1007eb2bd662Svikram 	BAM_DPRINTF((D_MENU_PATH, fcn, menu_path));
1008eb2bd662Svikram 
100940541d5dSvikram 	/*
1010eb2bd662Svikram 	 * If listing the menu, display the menu location
101140541d5dSvikram 	 */
101240541d5dSvikram 	if (strcmp(subcmd, "list_entry") == 0) {
1013eb2bd662Svikram 		bam_print(GRUB_MENU_PATH, menu_path);
101440541d5dSvikram 	}
10157c478bd9Sstevel@tonic-gate 
1016eb2bd662Svikram 
10177c478bd9Sstevel@tonic-gate 	menu = menu_read(menu_path);
10187c478bd9Sstevel@tonic-gate 	assert(menu);
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	/*
1021eb2bd662Svikram 	 * We already checked the following case in
1022eb2bd662Svikram 	 * check_subcmd_and_suboptions() above. Complete the
1023eb2bd662Svikram 	 * final step now.
10247c478bd9Sstevel@tonic-gate 	 */
10257c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "set_option") == 0) {
1026eb2bd662Svikram 		assert(largc == 1 && largv[0] && largv[1] == NULL);
10277c478bd9Sstevel@tonic-gate 		opt = largv[0];
1028eb2bd662Svikram 	} else {
1029eb2bd662Svikram 		assert(largc == 0 && largv == NULL);
10307c478bd9Sstevel@tonic-gate 	}
10317c478bd9Sstevel@tonic-gate 
1032eb2bd662Svikram 	ret = get_boot_cap(bam_root);
1033eb2bd662Svikram 	if (ret != BAM_SUCCESS) {
1034eb2bd662Svikram 		BAM_DPRINTF((D_BOOT_GET_CAP_FAILED, fcn));
1035eb2bd662Svikram 		goto out;
1036eb2bd662Svikram 	}
1037ae115bc7Smrj 
10387c478bd9Sstevel@tonic-gate 	/*
10397c478bd9Sstevel@tonic-gate 	 * Once the sub-cmd handler has run
10407c478bd9Sstevel@tonic-gate 	 * only the line field is guaranteed to have valid values
10417c478bd9Sstevel@tonic-gate 	 */
1042eb2bd662Svikram 	if (strcmp(subcmd, "update_entry") == 0)
1043eb2bd662Svikram 		ret = f(menu, menu_root, osdev);
1044eb2bd662Svikram 	else if (strcmp(subcmd, "upgrade") == 0)
1045eb2bd662Svikram 		ret = f(menu, bam_root, menu_root);
1046eb2bd662Svikram 	else if (strcmp(subcmd, "list_entry") == 0)
10477c478bd9Sstevel@tonic-gate 		ret = f(menu, menu_path, opt);
1048eb2bd662Svikram 	else
1049eb2bd662Svikram 		ret = f(menu, NULL, opt);
1050eb2bd662Svikram 
10517c478bd9Sstevel@tonic-gate 	if (ret == BAM_WRITE) {
1052eb2bd662Svikram 		BAM_DPRINTF((D_WRITING_MENU_ROOT, fcn, clean_menu_root));
1053eb2bd662Svikram 		ret = menu_write(clean_menu_root, menu);
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate 
1056eb2bd662Svikram out:
1057eb2bd662Svikram 	INJECT_ERROR1("POOL_SET", pool = "/pooldata");
1058eb2bd662Svikram 	assert((is_zfs(menu_root)) ^ (pool == NULL));
1059eb2bd662Svikram 	if (pool) {
1060eb2bd662Svikram 		(void) umount_top_dataset(pool, zmnted, zmntpt);
1061eb2bd662Svikram 		free(special);
1062eb2bd662Svikram 	}
10637c478bd9Sstevel@tonic-gate 	menu_free(menu);
10647c478bd9Sstevel@tonic-gate 	return (ret);
10657c478bd9Sstevel@tonic-gate }
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate static error_t
10697c478bd9Sstevel@tonic-gate bam_archive(
10707c478bd9Sstevel@tonic-gate 	char *subcmd,
10717c478bd9Sstevel@tonic-gate 	char *opt)
10727c478bd9Sstevel@tonic-gate {
10737c478bd9Sstevel@tonic-gate 	error_t			ret;
10747c478bd9Sstevel@tonic-gate 	error_t			(*f)(char *root, char *opt);
1075eb2bd662Svikram 	const char		*fcn = "bam_archive()";
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	/*
10788c1b6884Sszhou 	 * Add trailing / for archive subcommands
10798c1b6884Sszhou 	 */
10808c1b6884Sszhou 	if (rootbuf[strlen(rootbuf) - 1] != '/')
10818c1b6884Sszhou 		(void) strcat(rootbuf, "/");
10828c1b6884Sszhou 	bam_rootlen = strlen(rootbuf);
10838c1b6884Sszhou 
10848c1b6884Sszhou 	/*
10857c478bd9Sstevel@tonic-gate 	 * Check arguments
10867c478bd9Sstevel@tonic-gate 	 */
10877c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f);
10887c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS) {
10897c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
10907c478bd9Sstevel@tonic-gate 	}
10917c478bd9Sstevel@tonic-gate 
1092eb2bd662Svikram 	ret = get_boot_cap(rootbuf);
1093eb2bd662Svikram 	if (ret != BAM_SUCCESS) {
1094eb2bd662Svikram 		BAM_DPRINTF((D_BOOT_GET_CAP_FAILED, fcn));
1095ae115bc7Smrj 		return (ret);
1096eb2bd662Svikram 	}
1097ae115bc7Smrj 
10987c478bd9Sstevel@tonic-gate 	/*
10997c478bd9Sstevel@tonic-gate 	 * Check archive not supported with update_all
11007c478bd9Sstevel@tonic-gate 	 * since it is awkward to display out-of-sync
11017c478bd9Sstevel@tonic-gate 	 * information for each BE.
11027c478bd9Sstevel@tonic-gate 	 */
11037c478bd9Sstevel@tonic-gate 	if (bam_check && strcmp(subcmd, "update_all") == 0) {
11047c478bd9Sstevel@tonic-gate 		bam_error(CHECK_NOT_SUPPORTED, subcmd);
11057c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
11067c478bd9Sstevel@tonic-gate 	}
11077c478bd9Sstevel@tonic-gate 
1108b610f78eSvikram 	if (strcmp(subcmd, "update_all") == 0)
1109b610f78eSvikram 		bam_update_all = 1;
1110b610f78eSvikram 
1111986fd29aSsetje #if !defined(_OPB)
11122449e17fSsherrym 	ucode_install(bam_root);
11132449e17fSsherrym #endif
11142449e17fSsherrym 
1115b610f78eSvikram 	ret = f(bam_root, opt);
1116b610f78eSvikram 
1117b610f78eSvikram 	bam_update_all = 0;
1118b610f78eSvikram 
1119b610f78eSvikram 	return (ret);
11207c478bd9Sstevel@tonic-gate }
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1123ae115bc7Smrj void
11247c478bd9Sstevel@tonic-gate bam_error(char *format, ...)
11257c478bd9Sstevel@tonic-gate {
11267c478bd9Sstevel@tonic-gate 	va_list ap;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	va_start(ap, format);
11297c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", prog);
11307c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
11317c478bd9Sstevel@tonic-gate 	va_end(ap);
11327c478bd9Sstevel@tonic-gate }
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1135eb2bd662Svikram void
1136eb2bd662Svikram bam_derror(char *format, ...)
1137eb2bd662Svikram {
1138eb2bd662Svikram 	va_list ap;
1139eb2bd662Svikram 
1140eb2bd662Svikram 	assert(bam_debug);
1141eb2bd662Svikram 
1142eb2bd662Svikram 	va_start(ap, format);
1143eb2bd662Svikram 	(void) fprintf(stderr, "DEBUG: ");
1144eb2bd662Svikram 	(void) vfprintf(stderr, format, ap);
1145eb2bd662Svikram 	va_end(ap);
1146eb2bd662Svikram }
1147eb2bd662Svikram 
1148eb2bd662Svikram /*PRINTFLIKE1*/
1149eb2bd662Svikram void
11507c478bd9Sstevel@tonic-gate bam_print(char *format, ...)
11517c478bd9Sstevel@tonic-gate {
11527c478bd9Sstevel@tonic-gate 	va_list ap;
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	va_start(ap, format);
11557c478bd9Sstevel@tonic-gate 	(void) vfprintf(stdout, format, ap);
11567c478bd9Sstevel@tonic-gate 	va_end(ap);
11577c478bd9Sstevel@tonic-gate }
11587c478bd9Sstevel@tonic-gate 
1159ae115bc7Smrj /*PRINTFLIKE1*/
1160ae115bc7Smrj void
1161ae115bc7Smrj bam_print_stderr(char *format, ...)
1162ae115bc7Smrj {
1163ae115bc7Smrj 	va_list ap;
1164ae115bc7Smrj 
1165ae115bc7Smrj 	va_start(ap, format);
1166ae115bc7Smrj 	(void) vfprintf(stderr, format, ap);
1167ae115bc7Smrj 	va_end(ap);
1168ae115bc7Smrj }
1169ae115bc7Smrj 
11707c478bd9Sstevel@tonic-gate static void
11717c478bd9Sstevel@tonic-gate bam_exit(int excode)
11727c478bd9Sstevel@tonic-gate {
1173*cedc7e57SEnrico Perla - Sun Microsystems 	restore_env();
11747c478bd9Sstevel@tonic-gate 	bam_unlock();
11757c478bd9Sstevel@tonic-gate 	exit(excode);
11767c478bd9Sstevel@tonic-gate }
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate static void
11797c478bd9Sstevel@tonic-gate bam_lock(void)
11807c478bd9Sstevel@tonic-gate {
11817c478bd9Sstevel@tonic-gate 	struct flock lock;
11827c478bd9Sstevel@tonic-gate 	pid_t pid;
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS);
11857c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
11867c478bd9Sstevel@tonic-gate 		/*
11877c478bd9Sstevel@tonic-gate 		 * We may be invoked early in boot for archive verification.
11887c478bd9Sstevel@tonic-gate 		 * In this case, root is readonly and /var/run may not exist.
11897c478bd9Sstevel@tonic-gate 		 * Proceed without the lock
11907c478bd9Sstevel@tonic-gate 		 */
11917c478bd9Sstevel@tonic-gate 		if (errno == EROFS || errno == ENOENT) {
11927c478bd9Sstevel@tonic-gate 			bam_root_readonly = 1;
11937c478bd9Sstevel@tonic-gate 			return;
11947c478bd9Sstevel@tonic-gate 		}
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno));
11977c478bd9Sstevel@tonic-gate 		bam_exit(1);
11987c478bd9Sstevel@tonic-gate 	}
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
12017c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
12027c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
12037c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) {
12067c478bd9Sstevel@tonic-gate 		if (errno != EACCES && errno != EAGAIN) {
12077c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
12087c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
12097c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
12107c478bd9Sstevel@tonic-gate 			bam_exit(1);
12117c478bd9Sstevel@tonic-gate 		}
12127c478bd9Sstevel@tonic-gate 		pid = 0;
12137c478bd9Sstevel@tonic-gate 		(void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0);
12147c478bd9Sstevel@tonic-gate 		bam_print(FILE_LOCKED, pid);
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 		lock.l_type = F_WRLCK;
12177c478bd9Sstevel@tonic-gate 		lock.l_whence = SEEK_SET;
12187c478bd9Sstevel@tonic-gate 		lock.l_start = 0;
12197c478bd9Sstevel@tonic-gate 		lock.l_len = 0;
12207c478bd9Sstevel@tonic-gate 		if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) {
12217c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
12227c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
12237c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
12247c478bd9Sstevel@tonic-gate 			bam_exit(1);
12257c478bd9Sstevel@tonic-gate 		}
12267c478bd9Sstevel@tonic-gate 	}
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	/* We own the lock now */
12297c478bd9Sstevel@tonic-gate 	pid = getpid();
12307c478bd9Sstevel@tonic-gate 	(void) write(bam_lock_fd, &pid, sizeof (pid));
12317c478bd9Sstevel@tonic-gate }
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate static void
12347c478bd9Sstevel@tonic-gate bam_unlock(void)
12357c478bd9Sstevel@tonic-gate {
12367c478bd9Sstevel@tonic-gate 	struct flock unlock;
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	/*
12397c478bd9Sstevel@tonic-gate 	 * NOP if we don't hold the lock
12407c478bd9Sstevel@tonic-gate 	 */
12417c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
12427c478bd9Sstevel@tonic-gate 		return;
12437c478bd9Sstevel@tonic-gate 	}
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
12467c478bd9Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
12477c478bd9Sstevel@tonic-gate 	unlock.l_start = 0;
12487c478bd9Sstevel@tonic-gate 	unlock.l_len = 0;
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) {
12517c478bd9Sstevel@tonic-gate 		bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
12527c478bd9Sstevel@tonic-gate 	}
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	if (close(bam_lock_fd) == -1) {
12557c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno));
12567c478bd9Sstevel@tonic-gate 	}
12577c478bd9Sstevel@tonic-gate 	bam_lock_fd = -1;
12587c478bd9Sstevel@tonic-gate }
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate static error_t
12617c478bd9Sstevel@tonic-gate list_archive(char *root, char *opt)
12627c478bd9Sstevel@tonic-gate {
12637c478bd9Sstevel@tonic-gate 	filelist_t flist;
12647c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
12657c478bd9Sstevel@tonic-gate 	line_t *lp;
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	assert(root);
12687c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
12717c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
12727c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12737c478bd9Sstevel@tonic-gate 	}
12747c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
12777c478bd9Sstevel@tonic-gate 		bam_print(PRINT, lp->line);
12787c478bd9Sstevel@tonic-gate 	}
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
12837c478bd9Sstevel@tonic-gate }
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate /*
12867c478bd9Sstevel@tonic-gate  * This routine writes a list of lines to a file.
12877c478bd9Sstevel@tonic-gate  * The list is *not* freed
12887c478bd9Sstevel@tonic-gate  */
12897c478bd9Sstevel@tonic-gate static error_t
12907c478bd9Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start)
12917c478bd9Sstevel@tonic-gate {
12927c478bd9Sstevel@tonic-gate 	char		tmpfile[PATH_MAX];
12937c478bd9Sstevel@tonic-gate 	char		path[PATH_MAX];
12947c478bd9Sstevel@tonic-gate 	FILE		*fp;
12957c478bd9Sstevel@tonic-gate 	int		ret;
12967c478bd9Sstevel@tonic-gate 	struct stat	sb;
12977c478bd9Sstevel@tonic-gate 	mode_t		mode;
12987c478bd9Sstevel@tonic-gate 	uid_t		root_uid;
12997c478bd9Sstevel@tonic-gate 	gid_t		sys_gid;
13007c478bd9Sstevel@tonic-gate 	struct passwd	*pw;
13017c478bd9Sstevel@tonic-gate 	struct group	*gp;
1302eb2bd662Svikram 	const char	*fcn = "list2file()";
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, final);
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	if (start == NULL) {
1307eb2bd662Svikram 		/* Empty GRUB menu */
13087c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != -1) {
13097c478bd9Sstevel@tonic-gate 			bam_print(UNLINK_EMPTY, path);
13107c478bd9Sstevel@tonic-gate 			if (unlink(path) != 0) {
13117c478bd9Sstevel@tonic-gate 				bam_error(UNLINK_FAIL, path, strerror(errno));
13127c478bd9Sstevel@tonic-gate 				return (BAM_ERROR);
13137c478bd9Sstevel@tonic-gate 			} else {
13147c478bd9Sstevel@tonic-gate 				return (BAM_SUCCESS);
13157c478bd9Sstevel@tonic-gate 			}
13167c478bd9Sstevel@tonic-gate 		}
1317eb2bd662Svikram 		return (BAM_SUCCESS);
13187c478bd9Sstevel@tonic-gate 	}
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	/*
13217c478bd9Sstevel@tonic-gate 	 * Preserve attributes of existing file if possible,
13227c478bd9Sstevel@tonic-gate 	 * otherwise ask the system for uid/gid of root/sys.
13237c478bd9Sstevel@tonic-gate 	 * If all fails, fall back on hard-coded defaults.
13247c478bd9Sstevel@tonic-gate 	 */
13257c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != -1) {
13267c478bd9Sstevel@tonic-gate 		mode = sb.st_mode;
13277c478bd9Sstevel@tonic-gate 		root_uid = sb.st_uid;
13287c478bd9Sstevel@tonic-gate 		sys_gid = sb.st_gid;
13297c478bd9Sstevel@tonic-gate 	} else {
13307c478bd9Sstevel@tonic-gate 		mode = DEFAULT_DEV_MODE;
13317c478bd9Sstevel@tonic-gate 		if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
13327c478bd9Sstevel@tonic-gate 			root_uid = pw->pw_uid;
13337c478bd9Sstevel@tonic-gate 		} else {
13347c478bd9Sstevel@tonic-gate 			bam_error(CANT_FIND_USER,
13357c478bd9Sstevel@tonic-gate 			    DEFAULT_DEV_USER, DEFAULT_DEV_UID);
13367c478bd9Sstevel@tonic-gate 			root_uid = (uid_t)DEFAULT_DEV_UID;
13377c478bd9Sstevel@tonic-gate 		}
13387c478bd9Sstevel@tonic-gate 		if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
13397c478bd9Sstevel@tonic-gate 			sys_gid = gp->gr_gid;
13407c478bd9Sstevel@tonic-gate 		} else {
13417c478bd9Sstevel@tonic-gate 			bam_error(CANT_FIND_GROUP,
13427c478bd9Sstevel@tonic-gate 			    DEFAULT_DEV_GROUP, DEFAULT_DEV_GID);
13437c478bd9Sstevel@tonic-gate 			sys_gid = (gid_t)DEFAULT_DEV_GID;
13447c478bd9Sstevel@tonic-gate 		}
13457c478bd9Sstevel@tonic-gate 	}
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	(void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp);
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 	/* Truncate tmpfile first */
13507c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "w");
13517c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
13527c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
13537c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13547c478bd9Sstevel@tonic-gate 	}
1355963390b4Svikram 	ret = fclose(fp);
1356963390b4Svikram 	INJECT_ERROR1("LIST2FILE_TRUNC_FCLOSE", ret = EOF);
1357963390b4Svikram 	if (ret == EOF) {
13587c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
13597c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13607c478bd9Sstevel@tonic-gate 	}
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	/* Now open it in append mode */
13637c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "a");
13647c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
13657c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
13667c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13677c478bd9Sstevel@tonic-gate 	}
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 	for (; start; start = start->next) {
1370963390b4Svikram 		ret = s_fputs(start->line, fp);
1371963390b4Svikram 		INJECT_ERROR1("LIST2FILE_FPUTS", ret = EOF);
1372963390b4Svikram 		if (ret == EOF) {
13737c478bd9Sstevel@tonic-gate 			bam_error(WRITE_FAIL, tmpfile, strerror(errno));
13747c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
13757c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
13767c478bd9Sstevel@tonic-gate 		}
13777c478bd9Sstevel@tonic-gate 	}
13787c478bd9Sstevel@tonic-gate 
1379963390b4Svikram 	ret = fclose(fp);
1380963390b4Svikram 	INJECT_ERROR1("LIST2FILE_APPEND_FCLOSE", ret = EOF);
1381963390b4Svikram 	if (ret == EOF) {
13827c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
13837c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13847c478bd9Sstevel@tonic-gate 	}
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	/*
1387de531be4Sjg 	 * Set up desired attributes.  Ignore failures on filesystems
1388de531be4Sjg 	 * not supporting these operations - pcfs reports unsupported
1389de531be4Sjg 	 * operations as EINVAL.
13907c478bd9Sstevel@tonic-gate 	 */
13917c478bd9Sstevel@tonic-gate 	ret = chmod(tmpfile, mode);
1392de531be4Sjg 	if (ret == -1 &&
1393de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
13947c478bd9Sstevel@tonic-gate 		bam_error(CHMOD_FAIL, tmpfile, strerror(errno));
13957c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	ret = chown(tmpfile, root_uid, sys_gid);
1399de531be4Sjg 	if (ret == -1 &&
1400de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
14017c478bd9Sstevel@tonic-gate 		bam_error(CHOWN_FAIL, tmpfile, strerror(errno));
14027c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	/*
14077c478bd9Sstevel@tonic-gate 	 * Do an atomic rename
14087c478bd9Sstevel@tonic-gate 	 */
14097c478bd9Sstevel@tonic-gate 	ret = rename(tmpfile, path);
1410963390b4Svikram 	INJECT_ERROR1("LIST2FILE_RENAME", ret = -1);
14117c478bd9Sstevel@tonic-gate 	if (ret != 0) {
14127c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path, strerror(errno));
14137c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
14147c478bd9Sstevel@tonic-gate 	}
14157c478bd9Sstevel@tonic-gate 
1416eb2bd662Svikram 	BAM_DPRINTF((D_WROTE_FILE, fcn, path));
14177c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
14187c478bd9Sstevel@tonic-gate }
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate /*
142148847494SEnrico Perla - Sun Microsystems  * Checks if the path specified (without the file name at the end) exists
142248847494SEnrico Perla - Sun Microsystems  * and creates it if not. If the path exists and is not a directory, an attempt
142348847494SEnrico Perla - Sun Microsystems  * to unlink is made.
14247c478bd9Sstevel@tonic-gate  */
142548847494SEnrico Perla - Sun Microsystems static int
142648847494SEnrico Perla - Sun Microsystems setup_path(char *path)
142748847494SEnrico Perla - Sun Microsystems {
142848847494SEnrico Perla - Sun Microsystems 	char 		*p;
142948847494SEnrico Perla - Sun Microsystems 	int		ret;
143048847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
143148847494SEnrico Perla - Sun Microsystems 
143248847494SEnrico Perla - Sun Microsystems 	p = strrchr(path, '/');
143348847494SEnrico Perla - Sun Microsystems 	if (p != NULL) {
143448847494SEnrico Perla - Sun Microsystems 		*p = '\0';
143548847494SEnrico Perla - Sun Microsystems 		if (stat(path, &sb) != 0 || !(S_ISDIR(sb.st_mode))) {
143648847494SEnrico Perla - Sun Microsystems 			/* best effort attempt, mkdirp will catch the error */
143748847494SEnrico Perla - Sun Microsystems 			(void) unlink(path);
143848847494SEnrico Perla - Sun Microsystems 			if (bam_verbose)
143948847494SEnrico Perla - Sun Microsystems 				bam_print(NEED_DIRPATH, path);
144048847494SEnrico Perla - Sun Microsystems 			ret = mkdirp(path, DIR_PERMS);
144148847494SEnrico Perla - Sun Microsystems 			if (ret == -1) {
144248847494SEnrico Perla - Sun Microsystems 				bam_error(MKDIR_FAILED, path, strerror(errno));
144348847494SEnrico Perla - Sun Microsystems 				*p = '/';
144448847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
144548847494SEnrico Perla - Sun Microsystems 			}
144648847494SEnrico Perla - Sun Microsystems 		}
144748847494SEnrico Perla - Sun Microsystems 		*p = '/';
144848847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
144948847494SEnrico Perla - Sun Microsystems 	}
145048847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
145148847494SEnrico Perla - Sun Microsystems }
145248847494SEnrico Perla - Sun Microsystems 
145348847494SEnrico Perla - Sun Microsystems typedef union {
145448847494SEnrico Perla - Sun Microsystems 	gzFile	gzfile;
145548847494SEnrico Perla - Sun Microsystems 	int	fdfile;
145648847494SEnrico Perla - Sun Microsystems } outfile;
145748847494SEnrico Perla - Sun Microsystems 
145848847494SEnrico Perla - Sun Microsystems typedef struct {
145948847494SEnrico Perla - Sun Microsystems 	char		path[PATH_MAX];
146048847494SEnrico Perla - Sun Microsystems 	outfile		out;
146148847494SEnrico Perla - Sun Microsystems } cachefile;
146248847494SEnrico Perla - Sun Microsystems 
146348847494SEnrico Perla - Sun Microsystems static int
146448847494SEnrico Perla - Sun Microsystems setup_file(char *base, const char *path, cachefile *cf)
146548847494SEnrico Perla - Sun Microsystems {
146648847494SEnrico Perla - Sun Microsystems 	int	ret;
146748847494SEnrico Perla - Sun Microsystems 	char	*strip;
146848847494SEnrico Perla - Sun Microsystems 
146948847494SEnrico Perla - Sun Microsystems 	/* init gzfile or fdfile in case we fail before opening */
147048847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT)
147148847494SEnrico Perla - Sun Microsystems 		cf->out.gzfile = NULL;
147248847494SEnrico Perla - Sun Microsystems 	else
147348847494SEnrico Perla - Sun Microsystems 		cf->out.fdfile = -1;
147448847494SEnrico Perla - Sun Microsystems 
147548847494SEnrico Perla - Sun Microsystems 	/* strip the trailing altroot path */
147648847494SEnrico Perla - Sun Microsystems 	strip = (char *)path + strlen(rootbuf);
147748847494SEnrico Perla - Sun Microsystems 
147848847494SEnrico Perla - Sun Microsystems 	ret = snprintf(cf->path, sizeof (cf->path), "%s/%s", base, strip);
147948847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (cf->path)) {
148048847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
148148847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
148248847494SEnrico Perla - Sun Microsystems 	}
148348847494SEnrico Perla - Sun Microsystems 
148448847494SEnrico Perla - Sun Microsystems 	/* Check if path is present in the archive cache directory */
148548847494SEnrico Perla - Sun Microsystems 	if (setup_path(cf->path) == BAM_ERROR)
148648847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
148748847494SEnrico Perla - Sun Microsystems 
148848847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT) {
148948847494SEnrico Perla - Sun Microsystems 		if ((cf->out.gzfile = gzopen(cf->path, "wb")) == NULL) {
1490*cedc7e57SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, cf->path, strerror(errno));
149148847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
149248847494SEnrico Perla - Sun Microsystems 		}
149348847494SEnrico Perla - Sun Microsystems 		(void) gzsetparams(cf->out.gzfile, Z_BEST_SPEED,
149448847494SEnrico Perla - Sun Microsystems 		    Z_DEFAULT_STRATEGY);
149548847494SEnrico Perla - Sun Microsystems 	} else {
149648847494SEnrico Perla - Sun Microsystems 		if ((cf->out.fdfile = open(cf->path, O_WRONLY | O_CREAT, 0644))
149748847494SEnrico Perla - Sun Microsystems 		    == -1) {
149848847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, cf->path, strerror(errno));
149948847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
150048847494SEnrico Perla - Sun Microsystems 		}
150148847494SEnrico Perla - Sun Microsystems 	}
150248847494SEnrico Perla - Sun Microsystems 
150348847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
150448847494SEnrico Perla - Sun Microsystems }
150548847494SEnrico Perla - Sun Microsystems 
150648847494SEnrico Perla - Sun Microsystems static int
150748847494SEnrico Perla - Sun Microsystems cache_write(cachefile cf, char *buf, int size)
150848847494SEnrico Perla - Sun Microsystems {
150948847494SEnrico Perla - Sun Microsystems 	int	err;
151048847494SEnrico Perla - Sun Microsystems 
151148847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT) {
151248847494SEnrico Perla - Sun Microsystems 		if (gzwrite(cf.out.gzfile, buf, size) < 1) {
151348847494SEnrico Perla - Sun Microsystems 			bam_error(GZ_WRITE_FAIL, gzerror(cf.out.gzfile, &err));
1514*cedc7e57SEnrico Perla - Sun Microsystems 			if (err == Z_ERRNO && bam_verbose) {
151548847494SEnrico Perla - Sun Microsystems 				bam_error(WRITE_FAIL, cf.path, strerror(errno));
1516*cedc7e57SEnrico Perla - Sun Microsystems 			}
151748847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
151848847494SEnrico Perla - Sun Microsystems 		}
151948847494SEnrico Perla - Sun Microsystems 	} else {
152048847494SEnrico Perla - Sun Microsystems 		if (write(cf.out.fdfile, buf, size) < 1) {
152148847494SEnrico Perla - Sun Microsystems 			bam_error(WRITE_FAIL, cf.path, strerror(errno));
152248847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
152348847494SEnrico Perla - Sun Microsystems 		}
152448847494SEnrico Perla - Sun Microsystems 	}
152548847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
152648847494SEnrico Perla - Sun Microsystems }
152748847494SEnrico Perla - Sun Microsystems 
152848847494SEnrico Perla - Sun Microsystems static int
152948847494SEnrico Perla - Sun Microsystems cache_close(cachefile cf)
153048847494SEnrico Perla - Sun Microsystems {
153148847494SEnrico Perla - Sun Microsystems 	int	ret;
153248847494SEnrico Perla - Sun Microsystems 
153348847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT) {
153448847494SEnrico Perla - Sun Microsystems 		if (cf.out.gzfile) {
153548847494SEnrico Perla - Sun Microsystems 			ret = gzclose(cf.out.gzfile);
1536*cedc7e57SEnrico Perla - Sun Microsystems 			if (ret != Z_OK) {
153748847494SEnrico Perla - Sun Microsystems 				bam_error(CLOSE_FAIL, cf.path, strerror(errno));
153848847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
153948847494SEnrico Perla - Sun Microsystems 			}
1540*cedc7e57SEnrico Perla - Sun Microsystems 		}
154148847494SEnrico Perla - Sun Microsystems 	} else {
154248847494SEnrico Perla - Sun Microsystems 		if (cf.out.fdfile != -1) {
154348847494SEnrico Perla - Sun Microsystems 			ret = close(cf.out.fdfile);
154448847494SEnrico Perla - Sun Microsystems 			if (ret != 0) {
154548847494SEnrico Perla - Sun Microsystems 				bam_error(CLOSE_FAIL, cf.path, strerror(errno));
154648847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
154748847494SEnrico Perla - Sun Microsystems 			}
154848847494SEnrico Perla - Sun Microsystems 		}
154948847494SEnrico Perla - Sun Microsystems 	}
155048847494SEnrico Perla - Sun Microsystems 
155148847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
155248847494SEnrico Perla - Sun Microsystems }
155348847494SEnrico Perla - Sun Microsystems 
155448847494SEnrico Perla - Sun Microsystems static int
155548847494SEnrico Perla - Sun Microsystems dircache_updatefile(const char *path, int what)
155648847494SEnrico Perla - Sun Microsystems {
155748847494SEnrico Perla - Sun Microsystems 	int 		ret, exitcode;
155848847494SEnrico Perla - Sun Microsystems 	char 		buf[4096 * 4];
155948847494SEnrico Perla - Sun Microsystems 	FILE 		*infile;
156048847494SEnrico Perla - Sun Microsystems 	cachefile 	outfile, outupdt;
156148847494SEnrico Perla - Sun Microsystems 
1562*cedc7e57SEnrico Perla - Sun Microsystems 	if (bam_nowrite()) {
1563*cedc7e57SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NEED_UPDATE);
1564*cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
1565*cedc7e57SEnrico Perla - Sun Microsystems 	}
1566*cedc7e57SEnrico Perla - Sun Microsystems 
1567*cedc7e57SEnrico Perla - Sun Microsystems 	if (!has_cachedir(what))
156848847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
156948847494SEnrico Perla - Sun Microsystems 
157048847494SEnrico Perla - Sun Microsystems 	if ((infile = fopen(path, "rb")) == NULL) {
157148847494SEnrico Perla - Sun Microsystems 		bam_error(OPEN_FAIL, path, strerror(errno));
157248847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
157348847494SEnrico Perla - Sun Microsystems 	}
157448847494SEnrico Perla - Sun Microsystems 
157548847494SEnrico Perla - Sun Microsystems 	ret = setup_file(get_cachedir(what), path, &outfile);
157648847494SEnrico Perla - Sun Microsystems 	if (ret == BAM_ERROR) {
157748847494SEnrico Perla - Sun Microsystems 		exitcode = BAM_ERROR;
157848847494SEnrico Perla - Sun Microsystems 		goto out;
157948847494SEnrico Perla - Sun Microsystems 	}
158048847494SEnrico Perla - Sun Microsystems 	if (!is_dir_flag_on(what, NO_MULTI)) {
158148847494SEnrico Perla - Sun Microsystems 		ret = setup_file(get_updatedir(what), path, &outupdt);
158248847494SEnrico Perla - Sun Microsystems 		if (ret == BAM_ERROR)
158348847494SEnrico Perla - Sun Microsystems 			set_dir_flag(what, NO_MULTI);
158448847494SEnrico Perla - Sun Microsystems 	}
158548847494SEnrico Perla - Sun Microsystems 
158648847494SEnrico Perla - Sun Microsystems 	while ((ret = fread(buf, 1, sizeof (buf), infile)) > 0) {
158748847494SEnrico Perla - Sun Microsystems 		if (cache_write(outfile, buf, ret) == BAM_ERROR) {
158848847494SEnrico Perla - Sun Microsystems 			exitcode = BAM_ERROR;
158948847494SEnrico Perla - Sun Microsystems 			goto out;
159048847494SEnrico Perla - Sun Microsystems 		}
159148847494SEnrico Perla - Sun Microsystems 		if (!is_dir_flag_on(what, NO_MULTI))
159248847494SEnrico Perla - Sun Microsystems 			if (cache_write(outupdt, buf, ret) == BAM_ERROR)
159348847494SEnrico Perla - Sun Microsystems 				set_dir_flag(what, NO_MULTI);
159448847494SEnrico Perla - Sun Microsystems 	}
159548847494SEnrico Perla - Sun Microsystems 
159648847494SEnrico Perla - Sun Microsystems 	set_dir_flag(what, NEED_UPDATE);
159748847494SEnrico Perla - Sun Microsystems 	get_count(what)++;
1598*cedc7e57SEnrico Perla - Sun Microsystems 	if (get_count(what) > COUNT_MAX)
1599*cedc7e57SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
160048847494SEnrico Perla - Sun Microsystems 	exitcode = BAM_SUCCESS;
160148847494SEnrico Perla - Sun Microsystems out:
160248847494SEnrico Perla - Sun Microsystems 	(void) fclose(infile);
160348847494SEnrico Perla - Sun Microsystems 	if (cache_close(outfile) == BAM_ERROR)
160448847494SEnrico Perla - Sun Microsystems 		exitcode = BAM_ERROR;
160548847494SEnrico Perla - Sun Microsystems 	if (!is_dir_flag_on(what, NO_MULTI) &&
160648847494SEnrico Perla - Sun Microsystems 	    cache_close(outupdt) == BAM_ERROR)
160748847494SEnrico Perla - Sun Microsystems 		exitcode = BAM_ERROR;
160848847494SEnrico Perla - Sun Microsystems 	if (exitcode == BAM_ERROR)
160948847494SEnrico Perla - Sun Microsystems 		set_flag(UPDATE_ERROR);
161048847494SEnrico Perla - Sun Microsystems 	return (exitcode);
161148847494SEnrico Perla - Sun Microsystems }
161248847494SEnrico Perla - Sun Microsystems 
161348847494SEnrico Perla - Sun Microsystems static int
161448847494SEnrico Perla - Sun Microsystems dircache_updatedir(const char *path, int what, int updt)
161548847494SEnrico Perla - Sun Microsystems {
161648847494SEnrico Perla - Sun Microsystems 	int		ret;
161748847494SEnrico Perla - Sun Microsystems 	char		dpath[PATH_MAX];
161848847494SEnrico Perla - Sun Microsystems 	char		*strip;
161948847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
162048847494SEnrico Perla - Sun Microsystems 
162148847494SEnrico Perla - Sun Microsystems 	strip = (char *)path + strlen(rootbuf);
162248847494SEnrico Perla - Sun Microsystems 
162348847494SEnrico Perla - Sun Microsystems 	ret = snprintf(dpath, sizeof (dpath), "%s/%s", updt ?
162448847494SEnrico Perla - Sun Microsystems 	    get_updatedir(what) : get_cachedir(what), strip);
162548847494SEnrico Perla - Sun Microsystems 
162648847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (dpath)) {
162748847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
162848847494SEnrico Perla - Sun Microsystems 		set_flag(UPDATE_ERROR);
162948847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
163048847494SEnrico Perla - Sun Microsystems 	}
163148847494SEnrico Perla - Sun Microsystems 
163248847494SEnrico Perla - Sun Microsystems 	if (stat(dpath, &sb) == 0 && S_ISDIR(sb.st_mode))
163348847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
163448847494SEnrico Perla - Sun Microsystems 
163548847494SEnrico Perla - Sun Microsystems 	if (updt) {
163648847494SEnrico Perla - Sun Microsystems 		if (!is_dir_flag_on(what, NO_MULTI))
163748847494SEnrico Perla - Sun Microsystems 			if (!bam_nowrite() && mkdirp(dpath, DIR_PERMS) == -1)
163848847494SEnrico Perla - Sun Microsystems 				set_dir_flag(what, NO_MULTI);
163948847494SEnrico Perla - Sun Microsystems 	} else {
164048847494SEnrico Perla - Sun Microsystems 		if (!bam_nowrite() && mkdirp(dpath, DIR_PERMS) == -1) {
164148847494SEnrico Perla - Sun Microsystems 			set_flag(UPDATE_ERROR);
164248847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
164348847494SEnrico Perla - Sun Microsystems 		}
164448847494SEnrico Perla - Sun Microsystems 	}
164548847494SEnrico Perla - Sun Microsystems 
164648847494SEnrico Perla - Sun Microsystems 	set_dir_flag(what, NEED_UPDATE);
164748847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
164848847494SEnrico Perla - Sun Microsystems }
164948847494SEnrico Perla - Sun Microsystems 
165048847494SEnrico Perla - Sun Microsystems #define	DO_CACHE_DIR	0
165148847494SEnrico Perla - Sun Microsystems #define	DO_UPDATE_DIR	1
165248847494SEnrico Perla - Sun Microsystems 
165348847494SEnrico Perla - Sun Microsystems #if defined(_LP64) || defined(_LONGLONG_TYPE)
165448847494SEnrico Perla - Sun Microsystems typedef		Elf64_Ehdr	_elfhdr;
165548847494SEnrico Perla - Sun Microsystems #else
165648847494SEnrico Perla - Sun Microsystems typedef		Elf32_Ehdr	_elfhdr;
165748847494SEnrico Perla - Sun Microsystems #endif
165848847494SEnrico Perla - Sun Microsystems 
165948847494SEnrico Perla - Sun Microsystems /*
166048847494SEnrico Perla - Sun Microsystems  * This routine updates the contents of the cache directory
166148847494SEnrico Perla - Sun Microsystems  */
166248847494SEnrico Perla - Sun Microsystems static int
166348847494SEnrico Perla - Sun Microsystems update_dircache(const char *path, int flags)
166448847494SEnrico Perla - Sun Microsystems {
166548847494SEnrico Perla - Sun Microsystems 	int rc = BAM_SUCCESS;
166648847494SEnrico Perla - Sun Microsystems 
166748847494SEnrico Perla - Sun Microsystems 	switch (flags) {
166848847494SEnrico Perla - Sun Microsystems 	case FTW_F:
166948847494SEnrico Perla - Sun Microsystems 		{
167048847494SEnrico Perla - Sun Microsystems 		int	fd;
167148847494SEnrico Perla - Sun Microsystems 		_elfhdr	elf;
167248847494SEnrico Perla - Sun Microsystems 
167348847494SEnrico Perla - Sun Microsystems 		if ((fd = open(path, O_RDONLY)) < 0) {
167448847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, path, strerror(errno));
167548847494SEnrico Perla - Sun Microsystems 			set_flag(UPDATE_ERROR);
167648847494SEnrico Perla - Sun Microsystems 			rc = BAM_ERROR;
167748847494SEnrico Perla - Sun Microsystems 			break;
167848847494SEnrico Perla - Sun Microsystems 		}
167948847494SEnrico Perla - Sun Microsystems 
168048847494SEnrico Perla - Sun Microsystems 		/*
168148847494SEnrico Perla - Sun Microsystems 		 * libelf and gelf would be a cleaner and easier way to handle
168248847494SEnrico Perla - Sun Microsystems 		 * this, but libelf fails compilation if _ILP32 is defined &&
168348847494SEnrico Perla - Sun Microsystems 		 * _FILE_OFFSET_BITS is != 32 ...
168448847494SEnrico Perla - Sun Microsystems 		 */
168548847494SEnrico Perla - Sun Microsystems 		if (read(fd, (void *)&elf, sizeof (_elfhdr)) < 0) {
168648847494SEnrico Perla - Sun Microsystems 			bam_error(READ_FAIL, path, strerror(errno));
168748847494SEnrico Perla - Sun Microsystems 			set_flag(UPDATE_ERROR);
168848847494SEnrico Perla - Sun Microsystems 			(void) close(fd);
168948847494SEnrico Perla - Sun Microsystems 			rc = BAM_ERROR;
169048847494SEnrico Perla - Sun Microsystems 			break;
169148847494SEnrico Perla - Sun Microsystems 		}
169248847494SEnrico Perla - Sun Microsystems 		(void) close(fd);
169348847494SEnrico Perla - Sun Microsystems 
169448847494SEnrico Perla - Sun Microsystems 		/*
169548847494SEnrico Perla - Sun Microsystems 		 * If the file is not an executable and is not inside an amd64
169648847494SEnrico Perla - Sun Microsystems 		 * directory, we copy it in both the cache directories,
169748847494SEnrico Perla - Sun Microsystems 		 * otherwise, we only copy it inside the 64-bit one.
169848847494SEnrico Perla - Sun Microsystems 		 */
169948847494SEnrico Perla - Sun Microsystems 		if (memcmp(elf.e_ident, ELFMAG, 4) != 0) {
170048847494SEnrico Perla - Sun Microsystems 			if (strstr(path, "/amd64")) {
170148847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE64);
170248847494SEnrico Perla - Sun Microsystems 			} else {
170348847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE32);
1704*cedc7e57SEnrico Perla - Sun Microsystems 				if (rc == BAM_SUCCESS)
1705*cedc7e57SEnrico Perla - Sun Microsystems 					rc = dircache_updatefile(path, FILE64);
170648847494SEnrico Perla - Sun Microsystems 			}
170748847494SEnrico Perla - Sun Microsystems 		} else {
170848847494SEnrico Perla - Sun Microsystems 			/*
170948847494SEnrico Perla - Sun Microsystems 			 * Based on the ELF class we copy the file in the 32-bit
171048847494SEnrico Perla - Sun Microsystems 			 * or the 64-bit cache directory.
171148847494SEnrico Perla - Sun Microsystems 			 */
1712*cedc7e57SEnrico Perla - Sun Microsystems 			if (elf.e_ident[EI_CLASS] == ELFCLASS32) {
171348847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE32);
1714*cedc7e57SEnrico Perla - Sun Microsystems 			} else if (elf.e_ident[EI_CLASS] == ELFCLASS64) {
171548847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE64);
1716*cedc7e57SEnrico Perla - Sun Microsystems 			} else {
171748847494SEnrico Perla - Sun Microsystems 				bam_print(NO3264ELF, path);
171848847494SEnrico Perla - Sun Microsystems 				/* paranoid */
171948847494SEnrico Perla - Sun Microsystems 				rc  = dircache_updatefile(path, FILE32);
172048847494SEnrico Perla - Sun Microsystems 				if (rc == BAM_SUCCESS)
172148847494SEnrico Perla - Sun Microsystems 					rc = dircache_updatefile(path, FILE64);
172248847494SEnrico Perla - Sun Microsystems 			}
172348847494SEnrico Perla - Sun Microsystems 		}
172448847494SEnrico Perla - Sun Microsystems 		break;
172548847494SEnrico Perla - Sun Microsystems 		}
172648847494SEnrico Perla - Sun Microsystems 	case FTW_D:
172748847494SEnrico Perla - Sun Microsystems 		if (strstr(path, "/amd64") == NULL) {
172848847494SEnrico Perla - Sun Microsystems 			rc = dircache_updatedir(path, FILE32, DO_UPDATE_DIR);
172948847494SEnrico Perla - Sun Microsystems 			if (rc == BAM_SUCCESS)
173048847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatedir(path, FILE32,
173148847494SEnrico Perla - Sun Microsystems 				    DO_CACHE_DIR);
173248847494SEnrico Perla - Sun Microsystems 		} else {
173348847494SEnrico Perla - Sun Microsystems 			if (has_cachedir(FILE64)) {
173448847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatedir(path, FILE64,
173548847494SEnrico Perla - Sun Microsystems 				    DO_UPDATE_DIR);
173648847494SEnrico Perla - Sun Microsystems 				if (rc == BAM_SUCCESS)
173748847494SEnrico Perla - Sun Microsystems 					rc = dircache_updatedir(path, FILE64,
173848847494SEnrico Perla - Sun Microsystems 					    DO_CACHE_DIR);
173948847494SEnrico Perla - Sun Microsystems 			}
174048847494SEnrico Perla - Sun Microsystems 		}
174148847494SEnrico Perla - Sun Microsystems 		break;
174248847494SEnrico Perla - Sun Microsystems 	default:
174348847494SEnrico Perla - Sun Microsystems 		rc = BAM_ERROR;
174448847494SEnrico Perla - Sun Microsystems 		break;
174548847494SEnrico Perla - Sun Microsystems 	}
174648847494SEnrico Perla - Sun Microsystems 
174748847494SEnrico Perla - Sun Microsystems 	return (rc);
174848847494SEnrico Perla - Sun Microsystems }
174948847494SEnrico Perla - Sun Microsystems 
17507c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17517c478bd9Sstevel@tonic-gate static int
17527c478bd9Sstevel@tonic-gate cmpstat(
17537c478bd9Sstevel@tonic-gate 	const char *file,
175448847494SEnrico Perla - Sun Microsystems 	const struct stat *st,
17557c478bd9Sstevel@tonic-gate 	int flags,
17567c478bd9Sstevel@tonic-gate 	struct FTW *ftw)
17577c478bd9Sstevel@tonic-gate {
17587c478bd9Sstevel@tonic-gate 	uint_t 		sz;
17597c478bd9Sstevel@tonic-gate 	uint64_t 	*value;
17607c478bd9Sstevel@tonic-gate 	uint64_t 	filestat[2];
176148847494SEnrico Perla - Sun Microsystems 	int 		error, ret;
17627c478bd9Sstevel@tonic-gate 
176358091fd8Ssetje 	struct safefile *safefilep;
176458091fd8Ssetje 	FILE 		*fp;
176548847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
176658091fd8Ssetje 
17677c478bd9Sstevel@tonic-gate 	/*
176848847494SEnrico Perla - Sun Microsystems 	 * On SPARC we create/update links too.
17697c478bd9Sstevel@tonic-gate 	 */
177048847494SEnrico Perla - Sun Microsystems 	if (flags != FTW_F && flags != FTW_D && (flags == FTW_SL &&
177148847494SEnrico Perla - Sun Microsystems 	    !is_flag_on(IS_SPARC_TARGET)))
177248847494SEnrico Perla - Sun Microsystems 		return (0);
177348847494SEnrico Perla - Sun Microsystems 
177448847494SEnrico Perla - Sun Microsystems 	/*
177548847494SEnrico Perla - Sun Microsystems 	 * Ignore broken links
177648847494SEnrico Perla - Sun Microsystems 	 */
177748847494SEnrico Perla - Sun Microsystems 	if (flags == FTW_SL && stat(file, &sb) < 0)
17787c478bd9Sstevel@tonic-gate 		return (0);
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	/*
17817c478bd9Sstevel@tonic-gate 	 * new_nvlp may be NULL if there were errors earlier
17827c478bd9Sstevel@tonic-gate 	 * but this is not fatal to update determination.
17837c478bd9Sstevel@tonic-gate 	 */
17847c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp) {
178548847494SEnrico Perla - Sun Microsystems 		filestat[0] = st->st_size;
178648847494SEnrico Perla - Sun Microsystems 		filestat[1] = st->st_mtime;
17877c478bd9Sstevel@tonic-gate 		error = nvlist_add_uint64_array(walk_arg.new_nvlp,
17887c478bd9Sstevel@tonic-gate 		    file + bam_rootlen, filestat, 2);
17897c478bd9Sstevel@tonic-gate 		if (error)
17907c478bd9Sstevel@tonic-gate 			bam_error(NVADD_FAIL, file, strerror(error));
17917c478bd9Sstevel@tonic-gate 	}
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	/*
1794d876c67dSjg 	 * If we are invoked as part of system/filesystem/boot-archive, then
179558091fd8Ssetje 	 * there are a number of things we should not worry about
17967c478bd9Sstevel@tonic-gate 	 */
179758091fd8Ssetje 	if (bam_smf_check) {
179858091fd8Ssetje 		/* ignore amd64 modules unless we are booted amd64. */
179958091fd8Ssetje 		if (!is_amd64() && strstr(file, "/amd64/") != 0)
18007c478bd9Sstevel@tonic-gate 			return (0);
18017c478bd9Sstevel@tonic-gate 
180258091fd8Ssetje 		/* read in list of safe files */
180358091fd8Ssetje 		if (safefiles == NULL)
180458091fd8Ssetje 			if (fp = fopen("/boot/solaris/filelist.safe", "r")) {
180558091fd8Ssetje 				safefiles = s_calloc(1,
180658091fd8Ssetje 				    sizeof (struct safefile));
180758091fd8Ssetje 				safefilep = safefiles;
180858091fd8Ssetje 				safefilep->name = s_calloc(1, MAXPATHLEN +
180958091fd8Ssetje 				    MAXNAMELEN);
181058091fd8Ssetje 				safefilep->next = NULL;
181158091fd8Ssetje 				while (s_fgets(safefilep->name, MAXPATHLEN +
181258091fd8Ssetje 				    MAXNAMELEN, fp) != NULL) {
181358091fd8Ssetje 					safefilep->next = s_calloc(1,
181458091fd8Ssetje 					    sizeof (struct safefile));
181558091fd8Ssetje 					safefilep = safefilep->next;
181658091fd8Ssetje 					safefilep->name = s_calloc(1,
181758091fd8Ssetje 					    MAXPATHLEN + MAXNAMELEN);
181858091fd8Ssetje 					safefilep->next = NULL;
181958091fd8Ssetje 				}
182058091fd8Ssetje 				(void) fclose(fp);
182158091fd8Ssetje 			}
182258091fd8Ssetje 	}
182358091fd8Ssetje 
18247c478bd9Sstevel@tonic-gate 	/*
182548847494SEnrico Perla - Sun Microsystems 	 * On SPARC we create a -path-list file for mkisofs
182648847494SEnrico Perla - Sun Microsystems 	 */
182748847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET) && !bam_nowrite()) {
182848847494SEnrico Perla - Sun Microsystems 		if (flags != FTW_D) {
182948847494SEnrico Perla - Sun Microsystems 			char	*strip;
183048847494SEnrico Perla - Sun Microsystems 
183148847494SEnrico Perla - Sun Microsystems 			strip = (char *)file + strlen(rootbuf);
183248847494SEnrico Perla - Sun Microsystems 			(void) fprintf(walk_arg.sparcfile, "/%s=%s\n", strip,
183348847494SEnrico Perla - Sun Microsystems 			    file);
183448847494SEnrico Perla - Sun Microsystems 		}
183548847494SEnrico Perla - Sun Microsystems 	}
183648847494SEnrico Perla - Sun Microsystems 
183748847494SEnrico Perla - Sun Microsystems 	/*
183848847494SEnrico Perla - Sun Microsystems 	 * We are transitioning from the old model to the dircache or the cache
183948847494SEnrico Perla - Sun Microsystems 	 * directory was removed: create the entry without further checkings.
184048847494SEnrico Perla - Sun Microsystems 	 */
184148847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(NEED_CACHE_DIR)) {
184248847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
184348847494SEnrico Perla - Sun Microsystems 			bam_print(PARSEABLE_NEW_FILE, file);
184448847494SEnrico Perla - Sun Microsystems 
184548847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(IS_SPARC_TARGET)) {
184648847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
184748847494SEnrico Perla - Sun Microsystems 			return (0);
184848847494SEnrico Perla - Sun Microsystems 		}
184948847494SEnrico Perla - Sun Microsystems 
185048847494SEnrico Perla - Sun Microsystems 		ret = update_dircache(file, flags);
185148847494SEnrico Perla - Sun Microsystems 		if (ret == BAM_ERROR) {
185248847494SEnrico Perla - Sun Microsystems 			bam_error(UPDT_CACHE_FAIL, file);
185348847494SEnrico Perla - Sun Microsystems 			return (-1);
185448847494SEnrico Perla - Sun Microsystems 		}
185548847494SEnrico Perla - Sun Microsystems 
185648847494SEnrico Perla - Sun Microsystems 		return (0);
185748847494SEnrico Perla - Sun Microsystems 	}
185848847494SEnrico Perla - Sun Microsystems 
185948847494SEnrico Perla - Sun Microsystems 	/*
18607c478bd9Sstevel@tonic-gate 	 * We need an update if file doesn't exist in old archive
18617c478bd9Sstevel@tonic-gate 	 */
18627c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp == NULL ||
18637c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint64_array(walk_arg.old_nvlp,
18647c478bd9Sstevel@tonic-gate 	    file + bam_rootlen, &value, &sz) != 0) {
18657c478bd9Sstevel@tonic-gate 		if (bam_smf_check)	/* ignore new during smf check */
18667c478bd9Sstevel@tonic-gate 			return (0);
186748847494SEnrico Perla - Sun Microsystems 
186848847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(IS_SPARC_TARGET)) {
186948847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
187048847494SEnrico Perla - Sun Microsystems 		} else {
187148847494SEnrico Perla - Sun Microsystems 			ret = update_dircache(file, flags);
187248847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_ERROR) {
187348847494SEnrico Perla - Sun Microsystems 				bam_error(UPDT_CACHE_FAIL, file);
187448847494SEnrico Perla - Sun Microsystems 				return (-1);
187548847494SEnrico Perla - Sun Microsystems 			}
187648847494SEnrico Perla - Sun Microsystems 		}
187748847494SEnrico Perla - Sun Microsystems 
18787c478bd9Sstevel@tonic-gate 		if (bam_verbose)
18797c478bd9Sstevel@tonic-gate 			bam_print(PARSEABLE_NEW_FILE, file);
18807c478bd9Sstevel@tonic-gate 		return (0);
18817c478bd9Sstevel@tonic-gate 	}
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 	/*
188448847494SEnrico Perla - Sun Microsystems 	 * If we got there, the file is already listed as to be included in the
188548847494SEnrico Perla - Sun Microsystems 	 * iso image. We just need to know if we are going to rebuild it or not
188648847494SEnrico Perla - Sun Microsystems 	 */
188748847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET) &&
1888*cedc7e57SEnrico Perla - Sun Microsystems 	    is_dir_flag_on(FILE64, NEED_UPDATE) && !bam_nowrite())
188948847494SEnrico Perla - Sun Microsystems 		return (0);
189048847494SEnrico Perla - Sun Microsystems 	/*
18917c478bd9Sstevel@tonic-gate 	 * File exists in old archive. Check if file has changed
18927c478bd9Sstevel@tonic-gate 	 */
18937c478bd9Sstevel@tonic-gate 	assert(sz == 2);
18947c478bd9Sstevel@tonic-gate 	bcopy(value, filestat, sizeof (filestat));
18957c478bd9Sstevel@tonic-gate 
189648847494SEnrico Perla - Sun Microsystems 	if (flags != FTW_D && (filestat[0] != st->st_size ||
189748847494SEnrico Perla - Sun Microsystems 	    filestat[1] != st->st_mtime)) {
18987c609327Ssetje 		if (bam_smf_check) {
18997c609327Ssetje 			safefilep = safefiles;
19007c609327Ssetje 			while (safefilep != NULL) {
19017c609327Ssetje 				if (strcmp(file + bam_rootlen,
19027c609327Ssetje 				    safefilep->name) == 0) {
19037c609327Ssetje 					(void) creat(NEED_UPDATE_FILE, 0644);
19047c609327Ssetje 					return (0);
19057c609327Ssetje 				}
19067c609327Ssetje 				safefilep = safefilep->next;
19077c609327Ssetje 			}
19087c609327Ssetje 		}
190948847494SEnrico Perla - Sun Microsystems 
191048847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(IS_SPARC_TARGET)) {
191148847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
191248847494SEnrico Perla - Sun Microsystems 		} else {
191348847494SEnrico Perla - Sun Microsystems 			ret = update_dircache(file, flags);
191448847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_ERROR) {
191548847494SEnrico Perla - Sun Microsystems 				bam_error(UPDT_CACHE_FAIL, file);
191648847494SEnrico Perla - Sun Microsystems 				return (-1);
191748847494SEnrico Perla - Sun Microsystems 			}
191848847494SEnrico Perla - Sun Microsystems 		}
191948847494SEnrico Perla - Sun Microsystems 
19207c478bd9Sstevel@tonic-gate 		if (bam_verbose)
19217c478bd9Sstevel@tonic-gate 			if (bam_smf_check)
19227c478bd9Sstevel@tonic-gate 				bam_print("    %s\n", file);
19237c478bd9Sstevel@tonic-gate 			else
19247c478bd9Sstevel@tonic-gate 				bam_print(PARSEABLE_OUT_DATE, file);
19257c478bd9Sstevel@tonic-gate 	}
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 	return (0);
19287c478bd9Sstevel@tonic-gate }
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate /*
193148847494SEnrico Perla - Sun Microsystems  * Remove a directory path recursively
193248847494SEnrico Perla - Sun Microsystems  */
193348847494SEnrico Perla - Sun Microsystems static int
193448847494SEnrico Perla - Sun Microsystems rmdir_r(char *path)
193548847494SEnrico Perla - Sun Microsystems {
193648847494SEnrico Perla - Sun Microsystems 	struct dirent 	*d = NULL;
193748847494SEnrico Perla - Sun Microsystems 	DIR 		*dir = NULL;
193848847494SEnrico Perla - Sun Microsystems 	char 		tpath[PATH_MAX];
193948847494SEnrico Perla - Sun Microsystems 	struct stat 	sb;
194048847494SEnrico Perla - Sun Microsystems 
194148847494SEnrico Perla - Sun Microsystems 	if ((dir = opendir(path)) == NULL)
194248847494SEnrico Perla - Sun Microsystems 		return (-1);
194348847494SEnrico Perla - Sun Microsystems 
194448847494SEnrico Perla - Sun Microsystems 	while (d = readdir(dir)) {
194548847494SEnrico Perla - Sun Microsystems 		if ((strcmp(d->d_name, ".") != 0) &&
194648847494SEnrico Perla - Sun Microsystems 		    (strcmp(d->d_name, "..") != 0)) {
194748847494SEnrico Perla - Sun Microsystems 			(void) snprintf(tpath, sizeof (tpath), "%s/%s",
194848847494SEnrico Perla - Sun Microsystems 			    path, d->d_name);
194948847494SEnrico Perla - Sun Microsystems 			if (stat(tpath, &sb) == 0) {
195048847494SEnrico Perla - Sun Microsystems 				if (sb.st_mode & S_IFDIR)
195148847494SEnrico Perla - Sun Microsystems 					(void) rmdir_r(tpath);
195248847494SEnrico Perla - Sun Microsystems 				else
195348847494SEnrico Perla - Sun Microsystems 					(void) remove(tpath);
195448847494SEnrico Perla - Sun Microsystems 			}
195548847494SEnrico Perla - Sun Microsystems 		}
195648847494SEnrico Perla - Sun Microsystems 	}
195748847494SEnrico Perla - Sun Microsystems 	return (remove(path));
195848847494SEnrico Perla - Sun Microsystems }
195948847494SEnrico Perla - Sun Microsystems 
196048847494SEnrico Perla - Sun Microsystems /*
196148847494SEnrico Perla - Sun Microsystems  * Check if cache directory exists and, if not, create it and update flags
196248847494SEnrico Perla - Sun Microsystems  * accordingly. If the path exists, but it's not a directory, a best effort
196348847494SEnrico Perla - Sun Microsystems  * attempt to remove and recreate it is made.
196448847494SEnrico Perla - Sun Microsystems  * If the user requested a 'purge', always recreate the directory from scratch.
196548847494SEnrico Perla - Sun Microsystems  */
196648847494SEnrico Perla - Sun Microsystems static int
196748847494SEnrico Perla - Sun Microsystems set_cache_dir(char *root, int what)
196848847494SEnrico Perla - Sun Microsystems {
196948847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
197048847494SEnrico Perla - Sun Microsystems 	int		ret = 0;
197148847494SEnrico Perla - Sun Microsystems 
197248847494SEnrico Perla - Sun Microsystems 	ret = snprintf(get_cachedir(what), sizeof (get_cachedir(what)),
197348847494SEnrico Perla - Sun Microsystems 	    "%s%s%s%s%s", root, ARCHIVE_PREFIX, get_machine(), what == FILE64 ?
197448847494SEnrico Perla - Sun Microsystems 	    "/amd64" : "", CACHEDIR_SUFFIX);
197548847494SEnrico Perla - Sun Microsystems 
197648847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (get_cachedir(what))) {
197748847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
197848847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
197948847494SEnrico Perla - Sun Microsystems 	}
198048847494SEnrico Perla - Sun Microsystems 
198148847494SEnrico Perla - Sun Microsystems 	if (bam_purge)
198248847494SEnrico Perla - Sun Microsystems 		(void) rmdir_r(get_cachedir(what));
198348847494SEnrico Perla - Sun Microsystems 
198448847494SEnrico Perla - Sun Microsystems 	if (stat(get_cachedir(what), &sb) != 0 || !(S_ISDIR(sb.st_mode))) {
198548847494SEnrico Perla - Sun Microsystems 		/* best effort unlink attempt, mkdir will catch errors */
198648847494SEnrico Perla - Sun Microsystems 		(void) unlink(get_cachedir(what));
198748847494SEnrico Perla - Sun Microsystems 
198848847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
198948847494SEnrico Perla - Sun Microsystems 			bam_print(UPDATE_CDIR_MISS, get_cachedir(what));
199048847494SEnrico Perla - Sun Microsystems 		ret = mkdir(get_cachedir(what), DIR_PERMS);
199148847494SEnrico Perla - Sun Microsystems 		if (ret < 0) {
199248847494SEnrico Perla - Sun Microsystems 			bam_error(MKDIR_FAILED, get_cachedir(what),
199348847494SEnrico Perla - Sun Microsystems 			    strerror(errno));
199448847494SEnrico Perla - Sun Microsystems 			get_cachedir(what)[0] = '\0';
199548847494SEnrico Perla - Sun Microsystems 			return (ret);
199648847494SEnrico Perla - Sun Microsystems 		}
199748847494SEnrico Perla - Sun Microsystems 		set_flag(NEED_CACHE_DIR);
199848847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
199948847494SEnrico Perla - Sun Microsystems 	}
200048847494SEnrico Perla - Sun Microsystems 
200148847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
200248847494SEnrico Perla - Sun Microsystems }
200348847494SEnrico Perla - Sun Microsystems 
200448847494SEnrico Perla - Sun Microsystems static int
200548847494SEnrico Perla - Sun Microsystems set_update_dir(char *root, int what)
200648847494SEnrico Perla - Sun Microsystems {
200748847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
200848847494SEnrico Perla - Sun Microsystems 	int		ret;
200948847494SEnrico Perla - Sun Microsystems 
201048847494SEnrico Perla - Sun Microsystems 	if (is_dir_flag_on(what, NO_MULTI))
201148847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
201248847494SEnrico Perla - Sun Microsystems 
201348847494SEnrico Perla - Sun Microsystems 	if (!bam_extend) {
201448847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
201548847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
201648847494SEnrico Perla - Sun Microsystems 	}
201748847494SEnrico Perla - Sun Microsystems 
201848847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
201948847494SEnrico Perla - Sun Microsystems 		ret = snprintf(get_updatedir(what),
202048847494SEnrico Perla - Sun Microsystems 		    sizeof (get_updatedir(what)), "%s%s%s/amd64%s", root,
202148847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_PREFIX, get_machine(), UPDATEDIR_SUFFIX);
202248847494SEnrico Perla - Sun Microsystems 	else
202348847494SEnrico Perla - Sun Microsystems 		ret = snprintf(get_updatedir(what),
202448847494SEnrico Perla - Sun Microsystems 		    sizeof (get_updatedir(what)), "%s%s%s%s", root,
202548847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_PREFIX, get_machine(), UPDATEDIR_SUFFIX);
202648847494SEnrico Perla - Sun Microsystems 
202748847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (get_updatedir(what))) {
202848847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
202948847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
203048847494SEnrico Perla - Sun Microsystems 	}
203148847494SEnrico Perla - Sun Microsystems 
203248847494SEnrico Perla - Sun Microsystems 	if (stat(get_updatedir(what), &sb) == 0) {
203348847494SEnrico Perla - Sun Microsystems 		if (S_ISDIR(sb.st_mode))
203448847494SEnrico Perla - Sun Microsystems 			ret = rmdir_r(get_updatedir(what));
203548847494SEnrico Perla - Sun Microsystems 		else
203648847494SEnrico Perla - Sun Microsystems 			ret = unlink(get_updatedir(what));
203748847494SEnrico Perla - Sun Microsystems 
203848847494SEnrico Perla - Sun Microsystems 		if (ret != 0)
203948847494SEnrico Perla - Sun Microsystems 			set_dir_flag(what, NO_MULTI);
204048847494SEnrico Perla - Sun Microsystems 	}
204148847494SEnrico Perla - Sun Microsystems 
204248847494SEnrico Perla - Sun Microsystems 	if (mkdir(get_updatedir(what), DIR_PERMS) < 0)
204348847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
204448847494SEnrico Perla - Sun Microsystems 
204548847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
204648847494SEnrico Perla - Sun Microsystems }
204748847494SEnrico Perla - Sun Microsystems 
204848847494SEnrico Perla - Sun Microsystems static int
204948847494SEnrico Perla - Sun Microsystems is_valid_archive(char *root, int what)
205048847494SEnrico Perla - Sun Microsystems {
205148847494SEnrico Perla - Sun Microsystems 	char 		archive_path[PATH_MAX];
205248847494SEnrico Perla - Sun Microsystems 	struct stat 	sb;
205348847494SEnrico Perla - Sun Microsystems 	int 		ret;
205448847494SEnrico Perla - Sun Microsystems 
205548847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
205648847494SEnrico Perla - Sun Microsystems 		ret = snprintf(archive_path, sizeof (archive_path),
205748847494SEnrico Perla - Sun Microsystems 		    "%s%s%s/amd64%s", root, ARCHIVE_PREFIX, get_machine(),
205848847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_SUFFIX);
205948847494SEnrico Perla - Sun Microsystems 	else
206048847494SEnrico Perla - Sun Microsystems 		ret = snprintf(archive_path, sizeof (archive_path), "%s%s%s%s",
206148847494SEnrico Perla - Sun Microsystems 		    root, ARCHIVE_PREFIX, get_machine(), ARCHIVE_SUFFIX);
206248847494SEnrico Perla - Sun Microsystems 
206348847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (archive_path)) {
206448847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
206548847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
206648847494SEnrico Perla - Sun Microsystems 	}
206748847494SEnrico Perla - Sun Microsystems 
206848847494SEnrico Perla - Sun Microsystems 	if (stat(archive_path, &sb) != 0) {
206948847494SEnrico Perla - Sun Microsystems 		if (bam_verbose && !bam_check)
207048847494SEnrico Perla - Sun Microsystems 			bam_print(UPDATE_ARCH_MISS, archive_path);
207148847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NEED_UPDATE);
207248847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
207348847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
207448847494SEnrico Perla - Sun Microsystems 	}
207548847494SEnrico Perla - Sun Microsystems 
207648847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET))
207748847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
207848847494SEnrico Perla - Sun Microsystems 
207948847494SEnrico Perla - Sun Microsystems 	if (bam_extend && sb.st_size > BA_SIZE_MAX) {
208048847494SEnrico Perla - Sun Microsystems 		if (bam_verbose && !bam_check)
208148847494SEnrico Perla - Sun Microsystems 			bam_print(MULTI_SIZE, archive_path, BA_SIZE_MAX);
208248847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
208348847494SEnrico Perla - Sun Microsystems 	}
208448847494SEnrico Perla - Sun Microsystems 
208548847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
208648847494SEnrico Perla - Sun Microsystems }
208748847494SEnrico Perla - Sun Microsystems 
208848847494SEnrico Perla - Sun Microsystems /*
208948847494SEnrico Perla - Sun Microsystems  * Check flags and presence of required files and directories.
20907c478bd9Sstevel@tonic-gate  * The force flag and/or absence of files should
20917c478bd9Sstevel@tonic-gate  * trigger an update.
20927c478bd9Sstevel@tonic-gate  * Suppress stdout output if check (-n) option is set
20937c478bd9Sstevel@tonic-gate  * (as -n should only produce parseable output.)
20947c478bd9Sstevel@tonic-gate  */
209548847494SEnrico Perla - Sun Microsystems static int
20967c478bd9Sstevel@tonic-gate check_flags_and_files(char *root)
20977c478bd9Sstevel@tonic-gate {
209848847494SEnrico Perla - Sun Microsystems 
20997c478bd9Sstevel@tonic-gate 	struct stat 	sb;
210048847494SEnrico Perla - Sun Microsystems 	int 		ret;
210148847494SEnrico Perla - Sun Microsystems 
210248847494SEnrico Perla - Sun Microsystems 	/*
210348847494SEnrico Perla - Sun Microsystems 	 * If archive is missing, create archive
210448847494SEnrico Perla - Sun Microsystems 	 */
210548847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET)) {
210648847494SEnrico Perla - Sun Microsystems 		ret = is_valid_archive(root, FILE64);
2107*cedc7e57SEnrico Perla - Sun Microsystems 		if (ret == BAM_ERROR)
2108*cedc7e57SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
210948847494SEnrico Perla - Sun Microsystems 	} else {
211048847494SEnrico Perla - Sun Microsystems 		int	what = FILE32;
211148847494SEnrico Perla - Sun Microsystems 		do {
211248847494SEnrico Perla - Sun Microsystems 			ret = is_valid_archive(root, what);
211348847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_ERROR)
211448847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
211548847494SEnrico Perla - Sun Microsystems 			what++;
211648847494SEnrico Perla - Sun Microsystems 		} while (bam_direct == BAM_DIRECT_DBOOT && what < CACHEDIR_NUM);
211748847494SEnrico Perla - Sun Microsystems 	}
211848847494SEnrico Perla - Sun Microsystems 
211948847494SEnrico Perla - Sun Microsystems 	if (bam_nowrite())
212048847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
212148847494SEnrico Perla - Sun Microsystems 
212248847494SEnrico Perla - Sun Microsystems 
212348847494SEnrico Perla - Sun Microsystems 	/*
212448847494SEnrico Perla - Sun Microsystems 	 * check if cache directories exist on x86.
212548847494SEnrico Perla - Sun Microsystems 	 * check (and always open) the cache file on SPARC.
212648847494SEnrico Perla - Sun Microsystems 	 */
212748847494SEnrico Perla - Sun Microsystems 	if (is_sparc()) {
212848847494SEnrico Perla - Sun Microsystems 		ret = snprintf(get_cachedir(FILE64),
212948847494SEnrico Perla - Sun Microsystems 		    sizeof (get_cachedir(FILE64)), "%s%s%s/%s", root,
213048847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_PREFIX, get_machine(), CACHEDIR_SUFFIX);
213148847494SEnrico Perla - Sun Microsystems 
213248847494SEnrico Perla - Sun Microsystems 		if (ret >= sizeof (get_cachedir(FILE64))) {
213348847494SEnrico Perla - Sun Microsystems 			bam_error(PATH_TOO_LONG, rootbuf);
213448847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
213548847494SEnrico Perla - Sun Microsystems 		}
213648847494SEnrico Perla - Sun Microsystems 
213748847494SEnrico Perla - Sun Microsystems 		if (stat(get_cachedir(FILE64), &sb) != 0) {
213848847494SEnrico Perla - Sun Microsystems 			set_flag(NEED_CACHE_DIR);
213948847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
214048847494SEnrico Perla - Sun Microsystems 		}
214148847494SEnrico Perla - Sun Microsystems 
214248847494SEnrico Perla - Sun Microsystems 		walk_arg.sparcfile = fopen(get_cachedir(FILE64), "w");
214348847494SEnrico Perla - Sun Microsystems 		if (walk_arg.sparcfile == NULL) {
214448847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, get_cachedir(FILE64),
214548847494SEnrico Perla - Sun Microsystems 			    strerror(errno));
214648847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
214748847494SEnrico Perla - Sun Microsystems 		}
214848847494SEnrico Perla - Sun Microsystems 
214948847494SEnrico Perla - Sun Microsystems 		set_dir_present(FILE64);
215048847494SEnrico Perla - Sun Microsystems 	} else {
215148847494SEnrico Perla - Sun Microsystems 		int	what = FILE32;
215248847494SEnrico Perla - Sun Microsystems 
215348847494SEnrico Perla - Sun Microsystems 		do {
215448847494SEnrico Perla - Sun Microsystems 			if (set_cache_dir(root, what) != 0)
215548847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
215648847494SEnrico Perla - Sun Microsystems 
215748847494SEnrico Perla - Sun Microsystems 			set_dir_present(what);
215848847494SEnrico Perla - Sun Microsystems 
215948847494SEnrico Perla - Sun Microsystems 			if (set_update_dir(root, what) != 0)
216048847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
216148847494SEnrico Perla - Sun Microsystems 			what++;
216248847494SEnrico Perla - Sun Microsystems 		} while (bam_direct == BAM_DIRECT_DBOOT && what < CACHEDIR_NUM);
216348847494SEnrico Perla - Sun Microsystems 	}
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 	/*
21667c478bd9Sstevel@tonic-gate 	 * if force, create archive unconditionally
21677c478bd9Sstevel@tonic-gate 	 */
21687c478bd9Sstevel@tonic-gate 	if (bam_force) {
216948847494SEnrico Perla - Sun Microsystems 		if (!is_sparc())
217048847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE32, NEED_UPDATE);
217148847494SEnrico Perla - Sun Microsystems 		set_dir_flag(FILE64, NEED_UPDATE);
217248847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
21737c478bd9Sstevel@tonic-gate 			bam_print(UPDATE_FORCE);
217448847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
21757c478bd9Sstevel@tonic-gate 	}
21767c478bd9Sstevel@tonic-gate 
217748847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
21787c478bd9Sstevel@tonic-gate }
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate static error_t
21817c478bd9Sstevel@tonic-gate read_one_list(char *root, filelist_t  *flistp, char *filelist)
21827c478bd9Sstevel@tonic-gate {
21837c478bd9Sstevel@tonic-gate 	char 		path[PATH_MAX];
21847c478bd9Sstevel@tonic-gate 	FILE 		*fp;
21857c478bd9Sstevel@tonic-gate 	char 		buf[BAM_MAXLINE];
2186eb2bd662Svikram 	const char 	*fcn = "read_one_list()";
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, filelist);
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 	fp = fopen(path, "r");
21917c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
2192eb2bd662Svikram 		BAM_DPRINTF((D_FLIST_FAIL, fcn, path, strerror(errno)));
21937c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
21947c478bd9Sstevel@tonic-gate 	}
21957c478bd9Sstevel@tonic-gate 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
2196b610f78eSvikram 		/* skip blank lines */
2197b610f78eSvikram 		if (strspn(buf, " \t") == strlen(buf))
2198b610f78eSvikram 			continue;
21997c478bd9Sstevel@tonic-gate 		append_to_flist(flistp, buf);
22007c478bd9Sstevel@tonic-gate 	}
22017c478bd9Sstevel@tonic-gate 	if (fclose(fp) != 0) {
22027c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, path, strerror(errno));
22037c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
22047c478bd9Sstevel@tonic-gate 	}
22057c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
22067c478bd9Sstevel@tonic-gate }
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate static error_t
22097c478bd9Sstevel@tonic-gate read_list(char *root, filelist_t  *flistp)
22107c478bd9Sstevel@tonic-gate {
2211986fd29aSsetje 	char 		path[PATH_MAX];
2212986fd29aSsetje 	char 		cmd[PATH_MAX];
2213986fd29aSsetje 	struct stat 	sb;
2214986fd29aSsetje 	int 		n, rval;
2215eb2bd662Svikram 	const char 	*fcn = "read_list()";
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 	/*
2220986fd29aSsetje 	 * build and check path to extract_boot_filelist.ksh
2221986fd29aSsetje 	 */
2222986fd29aSsetje 	n = snprintf(path, sizeof (path), "%s%s", root, EXTRACT_BOOT_FILELIST);
2223986fd29aSsetje 	if (n >= sizeof (path)) {
2224986fd29aSsetje 		bam_error(NO_FLIST);
2225986fd29aSsetje 		return (BAM_ERROR);
2226986fd29aSsetje 	}
2227986fd29aSsetje 
2228*cedc7e57SEnrico Perla - Sun Microsystems 	if (is_safe_exec(path) == BAM_ERROR)
2229*cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
2230*cedc7e57SEnrico Perla - Sun Microsystems 
2231986fd29aSsetje 	/*
2232986fd29aSsetje 	 * If extract_boot_filelist is present, exec it, otherwise read
2233986fd29aSsetje 	 * the filelists directly, for compatibility with older images.
2234986fd29aSsetje 	 */
2235986fd29aSsetje 	if (stat(path, &sb) == 0) {
2236986fd29aSsetje 		/*
2237986fd29aSsetje 		 * build arguments to exec extract_boot_filelist.ksh
2238986fd29aSsetje 		 */
2239d876c67dSjg 		char *rootarg, *platarg;
2240d876c67dSjg 		int platarglen = 1, rootarglen = 1;
2241d876c67dSjg 		if (strlen(root) > 1)
2242d876c67dSjg 			rootarglen += strlen(root) + strlen("-R ");
2243d876c67dSjg 		if (bam_alt_platform)
2244d876c67dSjg 			platarglen += strlen(bam_platform) + strlen("-p ");
2245d876c67dSjg 		platarg = s_calloc(1, platarglen);
2246d876c67dSjg 		rootarg = s_calloc(1, rootarglen);
2247d876c67dSjg 		*platarg = 0;
2248d876c67dSjg 		*rootarg = 0;
2249d876c67dSjg 
2250986fd29aSsetje 		if (strlen(root) > 1) {
2251d876c67dSjg 			(void) snprintf(rootarg, rootarglen,
2252d876c67dSjg 			    "-R %s", root);
2253986fd29aSsetje 		}
2254d876c67dSjg 		if (bam_alt_platform) {
2255d876c67dSjg 			(void) snprintf(platarg, platarglen,
2256d876c67dSjg 			    "-p %s", bam_platform);
2257d876c67dSjg 		}
2258d876c67dSjg 		n = snprintf(cmd, sizeof (cmd), "%s %s %s /%s /%s",
2259d876c67dSjg 		    path, rootarg, platarg, BOOT_FILE_LIST, ETC_FILE_LIST);
2260d876c67dSjg 		free(platarg);
2261d876c67dSjg 		free(rootarg);
2262986fd29aSsetje 		if (n >= sizeof (cmd)) {
2263986fd29aSsetje 			bam_error(NO_FLIST);
2264986fd29aSsetje 			return (BAM_ERROR);
2265986fd29aSsetje 		}
2266986fd29aSsetje 		if (exec_cmd(cmd, flistp) != 0) {
2267eb2bd662Svikram 			BAM_DPRINTF((D_FLIST_FAIL, fcn, path, strerror(errno)));
2268986fd29aSsetje 			return (BAM_ERROR);
2269986fd29aSsetje 		}
2270986fd29aSsetje 	} else {
2271986fd29aSsetje 		/*
22727c478bd9Sstevel@tonic-gate 		 * Read current lists of files - only the first is mandatory
22737c478bd9Sstevel@tonic-gate 		 */
22747c478bd9Sstevel@tonic-gate 		rval = read_one_list(root, flistp, BOOT_FILE_LIST);
22757c478bd9Sstevel@tonic-gate 		if (rval != BAM_SUCCESS)
22767c478bd9Sstevel@tonic-gate 			return (rval);
22777c478bd9Sstevel@tonic-gate 		(void) read_one_list(root, flistp, ETC_FILE_LIST);
2278986fd29aSsetje 	}
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL) {
22817c478bd9Sstevel@tonic-gate 		bam_error(NO_FLIST);
22827c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
22837c478bd9Sstevel@tonic-gate 	}
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
22867c478bd9Sstevel@tonic-gate }
22877c478bd9Sstevel@tonic-gate 
22887c478bd9Sstevel@tonic-gate static void
22897c478bd9Sstevel@tonic-gate getoldstat(char *root)
22907c478bd9Sstevel@tonic-gate {
22917c478bd9Sstevel@tonic-gate 	char 		path[PATH_MAX];
22927c478bd9Sstevel@tonic-gate 	int 		fd, error;
22937c478bd9Sstevel@tonic-gate 	struct stat 	sb;
22947c478bd9Sstevel@tonic-gate 	char 		*ostat;
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
22977c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDONLY);
22987c478bd9Sstevel@tonic-gate 	if (fd == -1) {
22997c478bd9Sstevel@tonic-gate 		if (bam_verbose)
23007c478bd9Sstevel@tonic-gate 			bam_print(OPEN_FAIL, path, strerror(errno));
230148847494SEnrico Perla - Sun Microsystems 		goto out_err;
23027c478bd9Sstevel@tonic-gate 	}
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate 	if (fstat(fd, &sb) != 0) {
23057c478bd9Sstevel@tonic-gate 		bam_error(STAT_FAIL, path, strerror(errno));
230648847494SEnrico Perla - Sun Microsystems 		goto out_err;
23077c478bd9Sstevel@tonic-gate 	}
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 	ostat = s_calloc(1, sb.st_size);
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate 	if (read(fd, ostat, sb.st_size) != sb.st_size) {
23127c478bd9Sstevel@tonic-gate 		bam_error(READ_FAIL, path, strerror(errno));
23137c478bd9Sstevel@tonic-gate 		free(ostat);
231448847494SEnrico Perla - Sun Microsystems 		goto out_err;
23157c478bd9Sstevel@tonic-gate 	}
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 	(void) close(fd);
231848847494SEnrico Perla - Sun Microsystems 	fd = -1;
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
23217c478bd9Sstevel@tonic-gate 	error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0);
23227c478bd9Sstevel@tonic-gate 
23237c478bd9Sstevel@tonic-gate 	free(ostat);
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 	if (error) {
23267c478bd9Sstevel@tonic-gate 		bam_error(UNPACK_FAIL, path, strerror(error));
23277c478bd9Sstevel@tonic-gate 		walk_arg.old_nvlp = NULL;
232848847494SEnrico Perla - Sun Microsystems 		goto out_err;
232948847494SEnrico Perla - Sun Microsystems 	} else {
23307c478bd9Sstevel@tonic-gate 		return;
23317c478bd9Sstevel@tonic-gate 	}
233248847494SEnrico Perla - Sun Microsystems 
233348847494SEnrico Perla - Sun Microsystems out_err:
233448847494SEnrico Perla - Sun Microsystems 	if (fd != -1)
233548847494SEnrico Perla - Sun Microsystems 		(void) close(fd);
2336*cedc7e57SEnrico Perla - Sun Microsystems 	if (!is_flag_on(IS_SPARC_TARGET))
233748847494SEnrico Perla - Sun Microsystems 		set_dir_flag(FILE32, NEED_UPDATE);
233848847494SEnrico Perla - Sun Microsystems 	set_dir_flag(FILE64, NEED_UPDATE);
233948847494SEnrico Perla - Sun Microsystems }
234048847494SEnrico Perla - Sun Microsystems 
234148847494SEnrico Perla - Sun Microsystems /* Best effort stale entry removal */
234248847494SEnrico Perla - Sun Microsystems static void
234348847494SEnrico Perla - Sun Microsystems delete_stale(char *file, int what)
234448847494SEnrico Perla - Sun Microsystems {
234548847494SEnrico Perla - Sun Microsystems 	char		path[PATH_MAX];
234648847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
234748847494SEnrico Perla - Sun Microsystems 
234848847494SEnrico Perla - Sun Microsystems 	(void) snprintf(path, sizeof (path), "%s/%s", get_cachedir(what), file);
234948847494SEnrico Perla - Sun Microsystems 	if (!bam_check && stat(path, &sb) == 0) {
235048847494SEnrico Perla - Sun Microsystems 		if (sb.st_mode & S_IFDIR)
235148847494SEnrico Perla - Sun Microsystems 			(void) rmdir_r(path);
235248847494SEnrico Perla - Sun Microsystems 		else
235348847494SEnrico Perla - Sun Microsystems 			(void) unlink(path);
235448847494SEnrico Perla - Sun Microsystems 
235548847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, (NEED_UPDATE | NO_MULTI));
235648847494SEnrico Perla - Sun Microsystems 	}
23577c478bd9Sstevel@tonic-gate }
23587c478bd9Sstevel@tonic-gate 
2359a28d77b8Svikram /*
2360a28d77b8Svikram  * Checks if a file in the current (old) archive has
2361a28d77b8Svikram  * been deleted from the root filesystem. This is needed for
2362a28d77b8Svikram  * software like Trusted Extensions (TX) that switch early
2363a28d77b8Svikram  * in boot based on presence/absence of a kernel module.
2364a28d77b8Svikram  */
2365a28d77b8Svikram static void
2366a28d77b8Svikram check4stale(char *root)
2367a28d77b8Svikram {
2368a28d77b8Svikram 	nvpair_t	*nvp;
2369a28d77b8Svikram 	nvlist_t	*nvlp;
2370a28d77b8Svikram 	char 		*file;
2371a28d77b8Svikram 	char		path[PATH_MAX];
2372a28d77b8Svikram 
2373a28d77b8Svikram 	/*
2374a28d77b8Svikram 	 * Skip stale file check during smf check
2375a28d77b8Svikram 	 */
2376a28d77b8Svikram 	if (bam_smf_check)
2377a28d77b8Svikram 		return;
2378a28d77b8Svikram 
237948847494SEnrico Perla - Sun Microsystems 	/*
238048847494SEnrico Perla - Sun Microsystems 	 * If we need to (re)create the cache, there's no need to check for
238148847494SEnrico Perla - Sun Microsystems 	 * stale files
238248847494SEnrico Perla - Sun Microsystems 	 */
238348847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(NEED_CACHE_DIR))
238448847494SEnrico Perla - Sun Microsystems 		return;
238548847494SEnrico Perla - Sun Microsystems 
2386a28d77b8Svikram 	/* Nothing to do if no old stats */
2387a28d77b8Svikram 	if ((nvlp = walk_arg.old_nvlp) == NULL)
2388a28d77b8Svikram 		return;
2389a28d77b8Svikram 
2390a28d77b8Svikram 	for (nvp = nvlist_next_nvpair(nvlp, NULL); nvp;
2391a28d77b8Svikram 	    nvp = nvlist_next_nvpair(nvlp, nvp)) {
2392a28d77b8Svikram 		file = nvpair_name(nvp);
2393a28d77b8Svikram 		if (file == NULL)
2394a28d77b8Svikram 			continue;
2395a28d77b8Svikram 		(void) snprintf(path, sizeof (path), "%s/%s",
2396a28d77b8Svikram 		    root, file);
239748847494SEnrico Perla - Sun Microsystems 		if (access(path, F_OK) < 0) {
239848847494SEnrico Perla - Sun Microsystems 			int	what;
239948847494SEnrico Perla - Sun Microsystems 
2400*cedc7e57SEnrico Perla - Sun Microsystems 			if (bam_verbose)
2401*cedc7e57SEnrico Perla - Sun Microsystems 				bam_print(PARSEABLE_STALE_FILE, path);
240248847494SEnrico Perla - Sun Microsystems 
2403*cedc7e57SEnrico Perla - Sun Microsystems 			if (is_flag_on(IS_SPARC_TARGET)) {
2404*cedc7e57SEnrico Perla - Sun Microsystems 				set_dir_flag(FILE64, NEED_UPDATE);
2405*cedc7e57SEnrico Perla - Sun Microsystems 			} else {
240648847494SEnrico Perla - Sun Microsystems 				for (what = FILE32; what < CACHEDIR_NUM; what++)
240748847494SEnrico Perla - Sun Microsystems 					if (has_cachedir(what))
240848847494SEnrico Perla - Sun Microsystems 						delete_stale(file, what);
2409a28d77b8Svikram 			}
2410a28d77b8Svikram 		}
2411a28d77b8Svikram 	}
2412*cedc7e57SEnrico Perla - Sun Microsystems }
2413a28d77b8Svikram 
24147c478bd9Sstevel@tonic-gate static void
24157c478bd9Sstevel@tonic-gate create_newstat(void)
24167c478bd9Sstevel@tonic-gate {
24177c478bd9Sstevel@tonic-gate 	int error;
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate 	error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0);
24207c478bd9Sstevel@tonic-gate 	if (error) {
24217c478bd9Sstevel@tonic-gate 		/*
24227c478bd9Sstevel@tonic-gate 		 * Not fatal - we can still create archive
24237c478bd9Sstevel@tonic-gate 		 */
24247c478bd9Sstevel@tonic-gate 		walk_arg.new_nvlp = NULL;
24257c478bd9Sstevel@tonic-gate 		bam_error(NVALLOC_FAIL, strerror(error));
24267c478bd9Sstevel@tonic-gate 	}
24277c478bd9Sstevel@tonic-gate }
24287c478bd9Sstevel@tonic-gate 
242948847494SEnrico Perla - Sun Microsystems static int
24307c478bd9Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp)
24317c478bd9Sstevel@tonic-gate {
24327c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
24337c478bd9Sstevel@tonic-gate 	line_t *lp;
24347c478bd9Sstevel@tonic-gate 
24357c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
2436986fd29aSsetje 		/*
2437986fd29aSsetje 		 * Don't follow symlinks.  A symlink must refer to
2438986fd29aSsetje 		 * a file that would appear in the archive through
2439986fd29aSsetje 		 * a direct reference.  This matches the archive
2440986fd29aSsetje 		 * construction behavior.
2441986fd29aSsetje 		 */
24427c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, lp->line);
2443986fd29aSsetje 		if (nftw(path, cmpstat, 20, FTW_PHYS) == -1) {
244448847494SEnrico Perla - Sun Microsystems 			if (is_flag_on(UPDATE_ERROR))
244548847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
24467c478bd9Sstevel@tonic-gate 			/*
24477c478bd9Sstevel@tonic-gate 			 * Some files may not exist.
24487c478bd9Sstevel@tonic-gate 			 * For example: etc/rtc_config on a x86 diskless system
24497c478bd9Sstevel@tonic-gate 			 * Emit verbose message only
24507c478bd9Sstevel@tonic-gate 			 */
24517c478bd9Sstevel@tonic-gate 			if (bam_verbose)
24527c478bd9Sstevel@tonic-gate 				bam_print(NFTW_FAIL, path, strerror(errno));
24537c478bd9Sstevel@tonic-gate 		}
24547c478bd9Sstevel@tonic-gate 	}
245548847494SEnrico Perla - Sun Microsystems 
245648847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
24577c478bd9Sstevel@tonic-gate }
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate static void
24607c478bd9Sstevel@tonic-gate savenew(char *root)
24617c478bd9Sstevel@tonic-gate {
24627c478bd9Sstevel@tonic-gate 	char 	path[PATH_MAX];
24637c478bd9Sstevel@tonic-gate 	char 	path2[PATH_MAX];
24647c478bd9Sstevel@tonic-gate 	size_t 	sz;
24657c478bd9Sstevel@tonic-gate 	char 	*nstat;
24667c478bd9Sstevel@tonic-gate 	int 	fd, wrote, error;
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 	nstat = NULL;
24697c478bd9Sstevel@tonic-gate 	sz = 0;
24707c478bd9Sstevel@tonic-gate 	error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz,
24717c478bd9Sstevel@tonic-gate 	    NV_ENCODE_XDR, 0);
24727c478bd9Sstevel@tonic-gate 	if (error) {
24737c478bd9Sstevel@tonic-gate 		bam_error(PACK_FAIL, strerror(error));
24747c478bd9Sstevel@tonic-gate 		return;
24757c478bd9Sstevel@tonic-gate 	}
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP);
24787c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE);
24797c478bd9Sstevel@tonic-gate 	if (fd == -1) {
24807c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, path, strerror(errno));
24817c478bd9Sstevel@tonic-gate 		free(nstat);
24827c478bd9Sstevel@tonic-gate 		return;
24837c478bd9Sstevel@tonic-gate 	}
24847c478bd9Sstevel@tonic-gate 	wrote = write(fd, nstat, sz);
24857c478bd9Sstevel@tonic-gate 	if (wrote != sz) {
24867c478bd9Sstevel@tonic-gate 		bam_error(WRITE_FAIL, path, strerror(errno));
24877c478bd9Sstevel@tonic-gate 		(void) close(fd);
24887c478bd9Sstevel@tonic-gate 		free(nstat);
24897c478bd9Sstevel@tonic-gate 		return;
24907c478bd9Sstevel@tonic-gate 	}
24917c478bd9Sstevel@tonic-gate 	(void) close(fd);
24927c478bd9Sstevel@tonic-gate 	free(nstat);
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate 	(void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT);
24957c478bd9Sstevel@tonic-gate 	if (rename(path, path2) != 0) {
24967c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path2, strerror(errno));
24977c478bd9Sstevel@tonic-gate 	}
24987c478bd9Sstevel@tonic-gate }
24997c478bd9Sstevel@tonic-gate 
250048847494SEnrico Perla - Sun Microsystems #define	init_walk_args()	bzero(&walk_arg, sizeof (walk_arg))
250148847494SEnrico Perla - Sun Microsystems 
25027c478bd9Sstevel@tonic-gate static void
25037c478bd9Sstevel@tonic-gate clear_walk_args(void)
25047c478bd9Sstevel@tonic-gate {
25057c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp)
25067c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.old_nvlp);
25077c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp)
25087c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.new_nvlp);
250948847494SEnrico Perla - Sun Microsystems 	if (walk_arg.sparcfile)
251048847494SEnrico Perla - Sun Microsystems 		(void) fclose(walk_arg.sparcfile);
25117c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
25127c478bd9Sstevel@tonic-gate 	walk_arg.new_nvlp = NULL;
251348847494SEnrico Perla - Sun Microsystems 	walk_arg.sparcfile = NULL;
25147c478bd9Sstevel@tonic-gate }
25157c478bd9Sstevel@tonic-gate 
25167c478bd9Sstevel@tonic-gate /*
25177c478bd9Sstevel@tonic-gate  * Returns:
25187c478bd9Sstevel@tonic-gate  *	0 - no update necessary
25197c478bd9Sstevel@tonic-gate  *	1 - update required.
25207c478bd9Sstevel@tonic-gate  *	BAM_ERROR (-1) - An error occurred
25217c478bd9Sstevel@tonic-gate  *
25227c478bd9Sstevel@tonic-gate  * Special handling for check (-n):
25237c478bd9Sstevel@tonic-gate  * ================================
25247c478bd9Sstevel@tonic-gate  * The check (-n) option produces parseable output.
25257c478bd9Sstevel@tonic-gate  * To do this, we suppress all stdout messages unrelated
25267c478bd9Sstevel@tonic-gate  * to out of sync files.
25277c478bd9Sstevel@tonic-gate  * All stderr messages are still printed though.
25287c478bd9Sstevel@tonic-gate  *
25297c478bd9Sstevel@tonic-gate  */
25307c478bd9Sstevel@tonic-gate static int
25317c478bd9Sstevel@tonic-gate update_required(char *root)
25327c478bd9Sstevel@tonic-gate {
25337c478bd9Sstevel@tonic-gate 	struct stat 	sb;
25347c478bd9Sstevel@tonic-gate 	char 		path[PATH_MAX];
25357c478bd9Sstevel@tonic-gate 	filelist_t 	flist;
25367c478bd9Sstevel@tonic-gate 	filelist_t 	*flistp = &flist;
253748847494SEnrico Perla - Sun Microsystems 	int 		ret;
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
25407c478bd9Sstevel@tonic-gate 
254148847494SEnrico Perla - Sun Microsystems 	if (is_sparc())
254248847494SEnrico Perla - Sun Microsystems 		set_flag(IS_SPARC_TARGET);
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate 	/*
254548847494SEnrico Perla - Sun Microsystems 	 * Check if cache directories and archives are present
25467c478bd9Sstevel@tonic-gate 	 */
254748847494SEnrico Perla - Sun Microsystems 
254848847494SEnrico Perla - Sun Microsystems 	ret = check_flags_and_files(root);
254948847494SEnrico Perla - Sun Microsystems 	if (ret < 0)
255048847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
25517c478bd9Sstevel@tonic-gate 
25527c478bd9Sstevel@tonic-gate 	/*
25537c478bd9Sstevel@tonic-gate 	 * In certain deployment scenarios, filestat may not
25547c478bd9Sstevel@tonic-gate 	 * exist. Ignore it during boot-archive SMF check.
25557c478bd9Sstevel@tonic-gate 	 */
25567c478bd9Sstevel@tonic-gate 	if (bam_smf_check) {
25577c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
25587c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != 0)
25597c478bd9Sstevel@tonic-gate 			return (0);
25607c478bd9Sstevel@tonic-gate 	}
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 	getoldstat(root);
25637c478bd9Sstevel@tonic-gate 
25647c478bd9Sstevel@tonic-gate 	/*
2565a28d77b8Svikram 	 * Check if the archive contains files that are no longer
2566a28d77b8Svikram 	 * present on the root filesystem.
2567a28d77b8Svikram 	 */
2568a28d77b8Svikram 	check4stale(root);
2569a28d77b8Svikram 
2570a28d77b8Svikram 	/*
25717c478bd9Sstevel@tonic-gate 	 * read list of files
25727c478bd9Sstevel@tonic-gate 	 */
25737c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
25747c478bd9Sstevel@tonic-gate 		clear_walk_args();
25757c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
25767c478bd9Sstevel@tonic-gate 	}
25777c478bd9Sstevel@tonic-gate 
25787c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
25797c478bd9Sstevel@tonic-gate 
25807c478bd9Sstevel@tonic-gate 	/*
25817c478bd9Sstevel@tonic-gate 	 * At this point either the update is required
25827c478bd9Sstevel@tonic-gate 	 * or the decision is pending. In either case
25837c478bd9Sstevel@tonic-gate 	 * we need to create new stat nvlist
25847c478bd9Sstevel@tonic-gate 	 */
25857c478bd9Sstevel@tonic-gate 	create_newstat();
25867c478bd9Sstevel@tonic-gate 	/*
25877c478bd9Sstevel@tonic-gate 	 * This walk does 2 things:
25887c478bd9Sstevel@tonic-gate 	 *  	- gets new stat data for every file
25897c478bd9Sstevel@tonic-gate 	 *	- (optional) compare old and new stat data
25907c478bd9Sstevel@tonic-gate 	 */
259148847494SEnrico Perla - Sun Microsystems 	ret = walk_list(root, &flist);
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate 	/* done with the file list */
25947c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
25957c478bd9Sstevel@tonic-gate 
259648847494SEnrico Perla - Sun Microsystems 	/* something went wrong */
259748847494SEnrico Perla - Sun Microsystems 
259848847494SEnrico Perla - Sun Microsystems 	if (ret == BAM_ERROR) {
259948847494SEnrico Perla - Sun Microsystems 		bam_error(CACHE_FAIL);
260048847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
26017c478bd9Sstevel@tonic-gate 	}
26027c478bd9Sstevel@tonic-gate 
260348847494SEnrico Perla - Sun Microsystems 	if (walk_arg.new_nvlp == NULL) {
2604*cedc7e57SEnrico Perla - Sun Microsystems 		if (walk_arg.sparcfile != NULL)
260548847494SEnrico Perla - Sun Microsystems 			(void) fclose(walk_arg.sparcfile);
260648847494SEnrico Perla - Sun Microsystems 		bam_error(NO_NEW_STAT);
260748847494SEnrico Perla - Sun Microsystems 	}
26087c478bd9Sstevel@tonic-gate 
260948847494SEnrico Perla - Sun Microsystems 	/* If nothing was updated, discard newstat. */
261048847494SEnrico Perla - Sun Microsystems 
261148847494SEnrico Perla - Sun Microsystems 	if (!is_dir_flag_on(FILE32, NEED_UPDATE) &&
261248847494SEnrico Perla - Sun Microsystems 	    !is_dir_flag_on(FILE64, NEED_UPDATE)) {
26137c478bd9Sstevel@tonic-gate 		clear_walk_args();
26147c478bd9Sstevel@tonic-gate 		return (0);
26157c478bd9Sstevel@tonic-gate 	}
26167c478bd9Sstevel@tonic-gate 
2617*cedc7e57SEnrico Perla - Sun Microsystems 	if (walk_arg.sparcfile != NULL)
261848847494SEnrico Perla - Sun Microsystems 		(void) fclose(walk_arg.sparcfile);
261948847494SEnrico Perla - Sun Microsystems 
26207c478bd9Sstevel@tonic-gate 	return (1);
26217c478bd9Sstevel@tonic-gate }
26227c478bd9Sstevel@tonic-gate 
262348847494SEnrico Perla - Sun Microsystems static int
262448847494SEnrico Perla - Sun Microsystems flushfs(char *root)
262548847494SEnrico Perla - Sun Microsystems {
262648847494SEnrico Perla - Sun Microsystems 	char	cmd[PATH_MAX + 30];
262748847494SEnrico Perla - Sun Microsystems 
262848847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmd, sizeof (cmd), "%s -f \"%s\" 2>/dev/null",
262948847494SEnrico Perla - Sun Microsystems 	    LOCKFS_PATH, root);
263048847494SEnrico Perla - Sun Microsystems 
263148847494SEnrico Perla - Sun Microsystems 	return (exec_cmd(cmd, NULL));
263248847494SEnrico Perla - Sun Microsystems }
263348847494SEnrico Perla - Sun Microsystems 
263448847494SEnrico Perla - Sun Microsystems static int
263548847494SEnrico Perla - Sun Microsystems do_archive_copy(char *source, char *dest)
263648847494SEnrico Perla - Sun Microsystems {
263748847494SEnrico Perla - Sun Microsystems 
2638*cedc7e57SEnrico Perla - Sun Microsystems 	sync();
263948847494SEnrico Perla - Sun Microsystems 
264048847494SEnrico Perla - Sun Microsystems 	/* the equivalent of mv archive-new-$pid boot_archive */
2641*cedc7e57SEnrico Perla - Sun Microsystems 	if (rename(source, dest) != 0) {
2642*cedc7e57SEnrico Perla - Sun Microsystems 		(void) unlink(source);
264348847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
2644*cedc7e57SEnrico Perla - Sun Microsystems 	}
264548847494SEnrico Perla - Sun Microsystems 
264648847494SEnrico Perla - Sun Microsystems 	if (flushfs(bam_root) != 0)
2647*cedc7e57SEnrico Perla - Sun Microsystems 		sync();
264848847494SEnrico Perla - Sun Microsystems 
264948847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
265048847494SEnrico Perla - Sun Microsystems }
265148847494SEnrico Perla - Sun Microsystems 
265248847494SEnrico Perla - Sun Microsystems static int
265348847494SEnrico Perla - Sun Microsystems check_cmdline(filelist_t flist)
265448847494SEnrico Perla - Sun Microsystems {
265548847494SEnrico Perla - Sun Microsystems 	line_t	*lp;
265648847494SEnrico Perla - Sun Microsystems 
265748847494SEnrico Perla - Sun Microsystems 	for (lp = flist.head; lp; lp = lp->next) {
265848847494SEnrico Perla - Sun Microsystems 		if (strstr(lp->line, "Error:") != NULL ||
265948847494SEnrico Perla - Sun Microsystems 		    strstr(lp->line, "Inode number overflow") != NULL) {
2660*cedc7e57SEnrico Perla - Sun Microsystems 			(void) fprintf(stderr, "%s\n", lp->line);
266148847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
266248847494SEnrico Perla - Sun Microsystems 		}
266348847494SEnrico Perla - Sun Microsystems 	}
266448847494SEnrico Perla - Sun Microsystems 
266548847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
266648847494SEnrico Perla - Sun Microsystems }
266748847494SEnrico Perla - Sun Microsystems 
2668*cedc7e57SEnrico Perla - Sun Microsystems static void
2669*cedc7e57SEnrico Perla - Sun Microsystems dump_errormsg(filelist_t flist)
2670*cedc7e57SEnrico Perla - Sun Microsystems {
2671*cedc7e57SEnrico Perla - Sun Microsystems 	line_t	*lp;
2672*cedc7e57SEnrico Perla - Sun Microsystems 
2673*cedc7e57SEnrico Perla - Sun Microsystems 	for (lp = flist.head; lp; lp = lp->next)
2674*cedc7e57SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, "%s\n", lp->line);
2675*cedc7e57SEnrico Perla - Sun Microsystems }
2676*cedc7e57SEnrico Perla - Sun Microsystems 
267748847494SEnrico Perla - Sun Microsystems static int
267848847494SEnrico Perla - Sun Microsystems check_archive(char *dest)
267948847494SEnrico Perla - Sun Microsystems {
268048847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
268148847494SEnrico Perla - Sun Microsystems 
268248847494SEnrico Perla - Sun Microsystems 	if (stat(dest, &sb) != 0 || !S_ISREG(sb.st_mode) ||
268348847494SEnrico Perla - Sun Microsystems 	    sb.st_size < 10000) {
268448847494SEnrico Perla - Sun Microsystems 		bam_error(ARCHIVE_BAD, dest);
268548847494SEnrico Perla - Sun Microsystems 		(void) unlink(dest);
268648847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
268748847494SEnrico Perla - Sun Microsystems 	}
268848847494SEnrico Perla - Sun Microsystems 
268948847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
269048847494SEnrico Perla - Sun Microsystems }
269148847494SEnrico Perla - Sun Microsystems 
269248847494SEnrico Perla - Sun Microsystems /*
269348847494SEnrico Perla - Sun Microsystems  * Returns 1 if mkiso is in the expected PATH, 0 otherwise
269448847494SEnrico Perla - Sun Microsystems  */
269548847494SEnrico Perla - Sun Microsystems static int
269648847494SEnrico Perla - Sun Microsystems is_mkisofs()
269748847494SEnrico Perla - Sun Microsystems {
2698*cedc7e57SEnrico Perla - Sun Microsystems 	if (access(MKISOFS_PATH, X_OK) == 0)
269948847494SEnrico Perla - Sun Microsystems 		return (1);
270048847494SEnrico Perla - Sun Microsystems 	return (0);
270148847494SEnrico Perla - Sun Microsystems }
270248847494SEnrico Perla - Sun Microsystems 
270348847494SEnrico Perla - Sun Microsystems #define	MKISO_PARAMS	" -quiet -graft-points -dlrDJN -relaxed-filenames "
270448847494SEnrico Perla - Sun Microsystems 
270548847494SEnrico Perla - Sun Microsystems static int
270648847494SEnrico Perla - Sun Microsystems create_sparc_archive(char *archive, char *tempname, char *bootblk, char *list)
270748847494SEnrico Perla - Sun Microsystems {
270848847494SEnrico Perla - Sun Microsystems 	int		ret;
270948847494SEnrico Perla - Sun Microsystems 	char		cmdline[3 * PATH_MAX + 64];
271048847494SEnrico Perla - Sun Microsystems 	filelist_t	flist = {0};
271148847494SEnrico Perla - Sun Microsystems 	const char	*func = "create_sparc_archive()";
271248847494SEnrico Perla - Sun Microsystems 
271348847494SEnrico Perla - Sun Microsystems 	if (access(bootblk, R_OK) == 1) {
271448847494SEnrico Perla - Sun Microsystems 		bam_error(BOOTBLK_FAIL, bootblk);
271548847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
271648847494SEnrico Perla - Sun Microsystems 	}
271748847494SEnrico Perla - Sun Microsystems 
271848847494SEnrico Perla - Sun Microsystems 	/*
271948847494SEnrico Perla - Sun Microsystems 	 * Prepare mkisofs command line and execute it
272048847494SEnrico Perla - Sun Microsystems 	 */
272148847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s -G %s -o \"%s\" "
2722*cedc7e57SEnrico Perla - Sun Microsystems 	    "-path-list \"%s\" 2>&1", MKISOFS_PATH, MKISO_PARAMS, bootblk,
272348847494SEnrico Perla - Sun Microsystems 	    tempname, list);
272448847494SEnrico Perla - Sun Microsystems 
272548847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
272648847494SEnrico Perla - Sun Microsystems 
272748847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
2728*cedc7e57SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
2729*cedc7e57SEnrico Perla - Sun Microsystems 		dump_errormsg(flist);
273048847494SEnrico Perla - Sun Microsystems 		goto out_err;
2731*cedc7e57SEnrico Perla - Sun Microsystems 	}
273248847494SEnrico Perla - Sun Microsystems 
273348847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
273448847494SEnrico Perla - Sun Microsystems 
273548847494SEnrico Perla - Sun Microsystems 	/*
273648847494SEnrico Perla - Sun Microsystems 	 * Prepare dd command line to copy the bootblk on the new archive and
273748847494SEnrico Perla - Sun Microsystems 	 * execute it
273848847494SEnrico Perla - Sun Microsystems 	 */
273948847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s if=\"%s\" of=\"%s\""
2740*cedc7e57SEnrico Perla - Sun Microsystems 	    " bs=1b oseek=1 count=15 conv=notrunc conv=sync 2>&1", DD_PATH_USR,
274148847494SEnrico Perla - Sun Microsystems 	    bootblk, tempname);
274248847494SEnrico Perla - Sun Microsystems 
274348847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
274448847494SEnrico Perla - Sun Microsystems 
274548847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
274648847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR)
274748847494SEnrico Perla - Sun Microsystems 		goto out_err;
274848847494SEnrico Perla - Sun Microsystems 
274948847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
275048847494SEnrico Perla - Sun Microsystems 
275148847494SEnrico Perla - Sun Microsystems 	/* Did we get a valid archive ? */
275248847494SEnrico Perla - Sun Microsystems 	if (check_archive(tempname) == BAM_ERROR)
275348847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
275448847494SEnrico Perla - Sun Microsystems 
275548847494SEnrico Perla - Sun Microsystems 	return (do_archive_copy(tempname, archive));
275648847494SEnrico Perla - Sun Microsystems 
275748847494SEnrico Perla - Sun Microsystems out_err:
275848847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
275948847494SEnrico Perla - Sun Microsystems 	bam_error(ARCHIVE_FAIL, cmdline);
276048847494SEnrico Perla - Sun Microsystems 	(void) unlink(tempname);
276148847494SEnrico Perla - Sun Microsystems 	return (BAM_ERROR);
276248847494SEnrico Perla - Sun Microsystems }
276348847494SEnrico Perla - Sun Microsystems 
276448847494SEnrico Perla - Sun Microsystems static unsigned int
276548847494SEnrico Perla - Sun Microsystems from_733(unsigned char *s)
276648847494SEnrico Perla - Sun Microsystems {
276748847494SEnrico Perla - Sun Microsystems 	int		i;
276848847494SEnrico Perla - Sun Microsystems 	unsigned int	ret = 0;
276948847494SEnrico Perla - Sun Microsystems 
277048847494SEnrico Perla - Sun Microsystems 	for (i = 0; i < 4; i++)
277148847494SEnrico Perla - Sun Microsystems 		ret |= s[i] << (8 * i);
277248847494SEnrico Perla - Sun Microsystems 
277348847494SEnrico Perla - Sun Microsystems 	return (ret);
277448847494SEnrico Perla - Sun Microsystems }
277548847494SEnrico Perla - Sun Microsystems 
277648847494SEnrico Perla - Sun Microsystems static void
277748847494SEnrico Perla - Sun Microsystems to_733(unsigned char *s, unsigned int val)
277848847494SEnrico Perla - Sun Microsystems {
277948847494SEnrico Perla - Sun Microsystems 	int	i;
278048847494SEnrico Perla - Sun Microsystems 
278148847494SEnrico Perla - Sun Microsystems 	for (i = 0; i < 4; i++)
278248847494SEnrico Perla - Sun Microsystems 		s[i] = s[7-i] = (val >> (8 * i)) & 0xFF;
278348847494SEnrico Perla - Sun Microsystems }
278448847494SEnrico Perla - Sun Microsystems 
278548847494SEnrico Perla - Sun Microsystems /*
278648847494SEnrico Perla - Sun Microsystems  * Extends the current boot archive without recreating it from scratch
278748847494SEnrico Perla - Sun Microsystems  */
278848847494SEnrico Perla - Sun Microsystems static int
278948847494SEnrico Perla - Sun Microsystems extend_iso_archive(char *archive, char *tempname, char *update_dir)
279048847494SEnrico Perla - Sun Microsystems {
279148847494SEnrico Perla - Sun Microsystems 	int			fd = -1, newfd = -1, ret, i;
279248847494SEnrico Perla - Sun Microsystems 	int			next_session = 0, new_size = 0;
279348847494SEnrico Perla - Sun Microsystems 	char			cmdline[3 * PATH_MAX + 64];
279448847494SEnrico Perla - Sun Microsystems 	const char		*func = "extend_iso_archive()";
279548847494SEnrico Perla - Sun Microsystems 	filelist_t		flist = {0};
279648847494SEnrico Perla - Sun Microsystems 	struct iso_pdesc	saved_desc[MAX_IVDs];
279748847494SEnrico Perla - Sun Microsystems 
279848847494SEnrico Perla - Sun Microsystems 	fd = open(archive, O_RDWR);
279948847494SEnrico Perla - Sun Microsystems 	if (fd == -1) {
2800*cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
280148847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, archive, strerror(errno));
280248847494SEnrico Perla - Sun Microsystems 		goto out_err;
280348847494SEnrico Perla - Sun Microsystems 	}
280448847494SEnrico Perla - Sun Microsystems 
280548847494SEnrico Perla - Sun Microsystems 	/*
280648847494SEnrico Perla - Sun Microsystems 	 * A partial read is likely due to a corrupted file
280748847494SEnrico Perla - Sun Microsystems 	 */
280848847494SEnrico Perla - Sun Microsystems 	ret = pread64(fd, saved_desc, sizeof (saved_desc),
280948847494SEnrico Perla - Sun Microsystems 	    VOLDESC_OFF * CD_BLOCK);
281048847494SEnrico Perla - Sun Microsystems 	if (ret != sizeof (saved_desc)) {
2811*cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
281248847494SEnrico Perla - Sun Microsystems 			bam_error(READ_FAIL, archive, strerror(errno));
281348847494SEnrico Perla - Sun Microsystems 		goto out_err;
281448847494SEnrico Perla - Sun Microsystems 	}
281548847494SEnrico Perla - Sun Microsystems 
281648847494SEnrico Perla - Sun Microsystems 	if (memcmp(saved_desc[0].type, "\1CD001", 6)) {
2817*cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
281848847494SEnrico Perla - Sun Microsystems 			bam_error(SIGN_FAIL, archive);
281948847494SEnrico Perla - Sun Microsystems 		goto out_err;
282048847494SEnrico Perla - Sun Microsystems 	}
282148847494SEnrico Perla - Sun Microsystems 
282248847494SEnrico Perla - Sun Microsystems 	/*
282348847494SEnrico Perla - Sun Microsystems 	 * Read primary descriptor and locate next_session offset (it should
282448847494SEnrico Perla - Sun Microsystems 	 * point to the end of the archive)
282548847494SEnrico Perla - Sun Microsystems 	 */
282648847494SEnrico Perla - Sun Microsystems 	next_session = P2ROUNDUP(from_733(saved_desc[0].volume_space_size), 16);
282748847494SEnrico Perla - Sun Microsystems 
282848847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s -C 16,%d -M %s %s -o \""
2829*cedc7e57SEnrico Perla - Sun Microsystems 	    "%s\" \"%s\" 2>&1", MKISOFS_PATH, next_session, archive,
2830*cedc7e57SEnrico Perla - Sun Microsystems 	    MKISO_PARAMS, tempname, update_dir);
283148847494SEnrico Perla - Sun Microsystems 
283248847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
283348847494SEnrico Perla - Sun Microsystems 
283448847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
283548847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
2836*cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose) {
283748847494SEnrico Perla - Sun Microsystems 			bam_error(MULTI_FAIL, cmdline);
2838*cedc7e57SEnrico Perla - Sun Microsystems 			dump_errormsg(flist);
2839*cedc7e57SEnrico Perla - Sun Microsystems 		}
284048847494SEnrico Perla - Sun Microsystems 		goto out_flist_err;
284148847494SEnrico Perla - Sun Microsystems 	}
284248847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
284348847494SEnrico Perla - Sun Microsystems 
284448847494SEnrico Perla - Sun Microsystems 	newfd = open(tempname, O_RDONLY);
284548847494SEnrico Perla - Sun Microsystems 	if (newfd == -1) {
2846*cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
284748847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, archive, strerror(errno));
284848847494SEnrico Perla - Sun Microsystems 		goto out_err;
284948847494SEnrico Perla - Sun Microsystems 	}
285048847494SEnrico Perla - Sun Microsystems 
285148847494SEnrico Perla - Sun Microsystems 	ret = pread64(newfd, saved_desc, sizeof (saved_desc),
285248847494SEnrico Perla - Sun Microsystems 	    VOLDESC_OFF * CD_BLOCK);
285348847494SEnrico Perla - Sun Microsystems 	if (ret != sizeof (saved_desc)) {
2854*cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
285548847494SEnrico Perla - Sun Microsystems 			bam_error(READ_FAIL, archive, strerror(errno));
285648847494SEnrico Perla - Sun Microsystems 		goto out_err;
285748847494SEnrico Perla - Sun Microsystems 	}
285848847494SEnrico Perla - Sun Microsystems 
285948847494SEnrico Perla - Sun Microsystems 	if (memcmp(saved_desc[0].type, "\1CD001", 6)) {
2860*cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
286148847494SEnrico Perla - Sun Microsystems 			bam_error(SIGN_FAIL, archive);
286248847494SEnrico Perla - Sun Microsystems 		goto out_err;
286348847494SEnrico Perla - Sun Microsystems 	}
286448847494SEnrico Perla - Sun Microsystems 
286548847494SEnrico Perla - Sun Microsystems 	new_size = from_733(saved_desc[0].volume_space_size) + next_session;
286648847494SEnrico Perla - Sun Microsystems 	to_733(saved_desc[0].volume_space_size, new_size);
286748847494SEnrico Perla - Sun Microsystems 
286848847494SEnrico Perla - Sun Microsystems 	for (i = 1; i < MAX_IVDs; i++) {
286948847494SEnrico Perla - Sun Microsystems 		if (saved_desc[i].type[0] == (unsigned char)255)
287048847494SEnrico Perla - Sun Microsystems 			break;
287148847494SEnrico Perla - Sun Microsystems 		if (memcmp(saved_desc[i].id, "CD001", 5))
287248847494SEnrico Perla - Sun Microsystems 			break;
287348847494SEnrico Perla - Sun Microsystems 
287448847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
287548847494SEnrico Perla - Sun Microsystems 			bam_print("%s: Updating descriptor entry [%d]\n", func,
287648847494SEnrico Perla - Sun Microsystems 			    i);
287748847494SEnrico Perla - Sun Microsystems 
287848847494SEnrico Perla - Sun Microsystems 		to_733(saved_desc[i].volume_space_size, new_size);
287948847494SEnrico Perla - Sun Microsystems 	}
288048847494SEnrico Perla - Sun Microsystems 
288148847494SEnrico Perla - Sun Microsystems 	ret = pwrite64(fd, saved_desc, DVD_BLOCK, VOLDESC_OFF*CD_BLOCK);
288248847494SEnrico Perla - Sun Microsystems 	if (ret != DVD_BLOCK) {
2883*cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
288448847494SEnrico Perla - Sun Microsystems 			bam_error(WRITE_FAIL, archive, strerror(errno));
288548847494SEnrico Perla - Sun Microsystems 		goto out_err;
288648847494SEnrico Perla - Sun Microsystems 	}
288748847494SEnrico Perla - Sun Microsystems 	(void) close(newfd);
288848847494SEnrico Perla - Sun Microsystems 	newfd = -1;
288948847494SEnrico Perla - Sun Microsystems 
2890*cedc7e57SEnrico Perla - Sun Microsystems 	ret = fsync(fd);
2891*cedc7e57SEnrico Perla - Sun Microsystems 	if (ret != 0)
2892*cedc7e57SEnrico Perla - Sun Microsystems 		sync();
2893*cedc7e57SEnrico Perla - Sun Microsystems 
289448847494SEnrico Perla - Sun Microsystems 	ret = close(fd);
289548847494SEnrico Perla - Sun Microsystems 	if (ret != 0) {
2896*cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
289748847494SEnrico Perla - Sun Microsystems 			bam_error(CLOSE_FAIL, archive, strerror(errno));
289848847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
289948847494SEnrico Perla - Sun Microsystems 	}
290048847494SEnrico Perla - Sun Microsystems 	fd = -1;
290148847494SEnrico Perla - Sun Microsystems 
290248847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s if=%s of=%s bs=32k "
2903*cedc7e57SEnrico Perla - Sun Microsystems 	    "seek=%d conv=sync 2>&1", DD_PATH_USR, tempname, archive,
290448847494SEnrico Perla - Sun Microsystems 	    (next_session/16));
290548847494SEnrico Perla - Sun Microsystems 
290648847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
290748847494SEnrico Perla - Sun Microsystems 
290848847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
290948847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
2910*cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
291148847494SEnrico Perla - Sun Microsystems 			bam_error(MULTI_FAIL, cmdline);
291248847494SEnrico Perla - Sun Microsystems 		goto out_flist_err;
291348847494SEnrico Perla - Sun Microsystems 	}
291448847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
291548847494SEnrico Perla - Sun Microsystems 
291648847494SEnrico Perla - Sun Microsystems 	(void) unlink(tempname);
291748847494SEnrico Perla - Sun Microsystems 
2918*cedc7e57SEnrico Perla - Sun Microsystems 	if (flushfs(bam_root) != 0)
2919*cedc7e57SEnrico Perla - Sun Microsystems 		sync();
2920*cedc7e57SEnrico Perla - Sun Microsystems 
292148847494SEnrico Perla - Sun Microsystems 	if (bam_verbose)
292248847494SEnrico Perla - Sun Microsystems 		bam_print("boot archive updated successfully\n");
292348847494SEnrico Perla - Sun Microsystems 
292448847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
292548847494SEnrico Perla - Sun Microsystems 
292648847494SEnrico Perla - Sun Microsystems out_flist_err:
292748847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
292848847494SEnrico Perla - Sun Microsystems out_err:
292948847494SEnrico Perla - Sun Microsystems 	if (fd != -1)
293048847494SEnrico Perla - Sun Microsystems 		(void) close(fd);
293148847494SEnrico Perla - Sun Microsystems 	if (newfd != -1)
293248847494SEnrico Perla - Sun Microsystems 		(void) close(newfd);
293348847494SEnrico Perla - Sun Microsystems 	return (BAM_ERROR);
293448847494SEnrico Perla - Sun Microsystems }
293548847494SEnrico Perla - Sun Microsystems 
293648847494SEnrico Perla - Sun Microsystems static int
293748847494SEnrico Perla - Sun Microsystems create_x86_archive(char *archive, char *tempname, char *update_dir)
293848847494SEnrico Perla - Sun Microsystems {
293948847494SEnrico Perla - Sun Microsystems 	int		ret;
294048847494SEnrico Perla - Sun Microsystems 	char		cmdline[3 * PATH_MAX + 64];
294148847494SEnrico Perla - Sun Microsystems 	filelist_t	flist = {0};
294248847494SEnrico Perla - Sun Microsystems 	const char	*func = "create_x86_archive()";
294348847494SEnrico Perla - Sun Microsystems 
294448847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s -o \"%s\" \"%s\" "
2945*cedc7e57SEnrico Perla - Sun Microsystems 	    "2>&1", MKISOFS_PATH, MKISO_PARAMS, tempname, update_dir);
294648847494SEnrico Perla - Sun Microsystems 
294748847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
294848847494SEnrico Perla - Sun Microsystems 
294948847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
295048847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
295148847494SEnrico Perla - Sun Microsystems 		bam_error(ARCHIVE_FAIL, cmdline);
2952*cedc7e57SEnrico Perla - Sun Microsystems 		dump_errormsg(flist);
295348847494SEnrico Perla - Sun Microsystems 		filelist_free(&flist);
295448847494SEnrico Perla - Sun Microsystems 		(void) unlink(tempname);
295548847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
295648847494SEnrico Perla - Sun Microsystems 	}
295748847494SEnrico Perla - Sun Microsystems 
295848847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
295948847494SEnrico Perla - Sun Microsystems 
296048847494SEnrico Perla - Sun Microsystems 	if (check_archive(tempname) == BAM_ERROR)
296148847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
296248847494SEnrico Perla - Sun Microsystems 
296348847494SEnrico Perla - Sun Microsystems 	return (do_archive_copy(tempname, archive));
296448847494SEnrico Perla - Sun Microsystems }
296548847494SEnrico Perla - Sun Microsystems 
296648847494SEnrico Perla - Sun Microsystems static int
296748847494SEnrico Perla - Sun Microsystems mkisofs_archive(char *root, int what)
296848847494SEnrico Perla - Sun Microsystems {
296948847494SEnrico Perla - Sun Microsystems 	int		ret;
297048847494SEnrico Perla - Sun Microsystems 	char		temp[PATH_MAX];
297148847494SEnrico Perla - Sun Microsystems 	char 		bootblk[PATH_MAX];
297248847494SEnrico Perla - Sun Microsystems 	char		boot_archive[PATH_MAX];
297348847494SEnrico Perla - Sun Microsystems 
297448847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
297548847494SEnrico Perla - Sun Microsystems 		ret = snprintf(temp, sizeof (temp),
297648847494SEnrico Perla - Sun Microsystems 		    "%s%s%s/amd64/archive-new-%d", root, ARCHIVE_PREFIX,
297748847494SEnrico Perla - Sun Microsystems 		    get_machine(), getpid());
297848847494SEnrico Perla - Sun Microsystems 	else
297948847494SEnrico Perla - Sun Microsystems 		ret = snprintf(temp, sizeof (temp), "%s%s%s/archive-new-%d",
298048847494SEnrico Perla - Sun Microsystems 		    root, ARCHIVE_PREFIX, get_machine(), getpid());
298148847494SEnrico Perla - Sun Microsystems 
298248847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (temp))
298348847494SEnrico Perla - Sun Microsystems 		goto out_path_err;
298448847494SEnrico Perla - Sun Microsystems 
298548847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
298648847494SEnrico Perla - Sun Microsystems 		ret = snprintf(boot_archive, sizeof (boot_archive),
298748847494SEnrico Perla - Sun Microsystems 		    "%s%s%s/amd64%s", root, ARCHIVE_PREFIX, get_machine(),
298848847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_SUFFIX);
298948847494SEnrico Perla - Sun Microsystems 	else
299048847494SEnrico Perla - Sun Microsystems 		ret = snprintf(boot_archive, sizeof (boot_archive),
299148847494SEnrico Perla - Sun Microsystems 		    "%s%s%s%s", root, ARCHIVE_PREFIX, get_machine(),
299248847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_SUFFIX);
299348847494SEnrico Perla - Sun Microsystems 
299448847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (boot_archive))
299548847494SEnrico Perla - Sun Microsystems 		goto out_path_err;
299648847494SEnrico Perla - Sun Microsystems 
299748847494SEnrico Perla - Sun Microsystems 	bam_print("updating %s\n", boot_archive);
299848847494SEnrico Perla - Sun Microsystems 
299948847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET)) {
300048847494SEnrico Perla - Sun Microsystems 		ret = snprintf(bootblk, sizeof (bootblk),
300148847494SEnrico Perla - Sun Microsystems 		    "%s/platform/%s/lib/fs/hsfs/bootblk", root, get_machine());
300248847494SEnrico Perla - Sun Microsystems 		if (ret >= sizeof (bootblk))
300348847494SEnrico Perla - Sun Microsystems 			goto out_path_err;
300448847494SEnrico Perla - Sun Microsystems 
300548847494SEnrico Perla - Sun Microsystems 		ret = create_sparc_archive(boot_archive, temp, bootblk,
300648847494SEnrico Perla - Sun Microsystems 		    get_cachedir(what));
300748847494SEnrico Perla - Sun Microsystems 	} else {
300848847494SEnrico Perla - Sun Microsystems 		if (!is_dir_flag_on(what, NO_MULTI)) {
300948847494SEnrico Perla - Sun Microsystems 			if (bam_verbose)
301048847494SEnrico Perla - Sun Microsystems 				bam_print("Attempting to extend x86 archive: "
301148847494SEnrico Perla - Sun Microsystems 				    "%s\n", boot_archive);
301248847494SEnrico Perla - Sun Microsystems 
301348847494SEnrico Perla - Sun Microsystems 			ret = extend_iso_archive(boot_archive, temp,
301448847494SEnrico Perla - Sun Microsystems 			    get_updatedir(what));
301548847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_SUCCESS) {
301648847494SEnrico Perla - Sun Microsystems 				if (bam_verbose)
301748847494SEnrico Perla - Sun Microsystems 					bam_print("Successfully extended %s\n",
301848847494SEnrico Perla - Sun Microsystems 					    boot_archive);
301948847494SEnrico Perla - Sun Microsystems 
302048847494SEnrico Perla - Sun Microsystems 				(void) rmdir_r(get_updatedir(what));
302148847494SEnrico Perla - Sun Microsystems 				return (BAM_SUCCESS);
302248847494SEnrico Perla - Sun Microsystems 			}
302348847494SEnrico Perla - Sun Microsystems 		}
302448847494SEnrico Perla - Sun Microsystems 		/*
302548847494SEnrico Perla - Sun Microsystems 		 * The boot archive will be recreated from scratch. We get here
302648847494SEnrico Perla - Sun Microsystems 		 * if at least one of these conditions is true:
302748847494SEnrico Perla - Sun Microsystems 		 * - bootadm was called without the -e switch
302848847494SEnrico Perla - Sun Microsystems 		 * - the archive (or the archive cache) doesn't exist
302948847494SEnrico Perla - Sun Microsystems 		 * - archive size is bigger than BA_SIZE_MAX
303048847494SEnrico Perla - Sun Microsystems 		 * - more than COUNT_MAX files need to be updated
303148847494SEnrico Perla - Sun Microsystems 		 * - an error occourred either populating the /updates directory
303248847494SEnrico Perla - Sun Microsystems 		 *   or extend_iso_archive() failed
303348847494SEnrico Perla - Sun Microsystems 		 */
303448847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
303548847494SEnrico Perla - Sun Microsystems 			bam_print("Unable to extend %s... rebuilding archive\n",
303648847494SEnrico Perla - Sun Microsystems 			    boot_archive);
303748847494SEnrico Perla - Sun Microsystems 
303848847494SEnrico Perla - Sun Microsystems 		if (get_updatedir(what)[0] != '\0')
303948847494SEnrico Perla - Sun Microsystems 			(void) rmdir_r(get_updatedir(what));
304048847494SEnrico Perla - Sun Microsystems 
304148847494SEnrico Perla - Sun Microsystems 
304248847494SEnrico Perla - Sun Microsystems 		ret = create_x86_archive(boot_archive, temp,
304348847494SEnrico Perla - Sun Microsystems 		    get_cachedir(what));
304448847494SEnrico Perla - Sun Microsystems 	}
304548847494SEnrico Perla - Sun Microsystems 
304648847494SEnrico Perla - Sun Microsystems 	if (ret == BAM_SUCCESS && bam_verbose)
304748847494SEnrico Perla - Sun Microsystems 		bam_print("Successfully created %s\n", boot_archive);
304848847494SEnrico Perla - Sun Microsystems 
304948847494SEnrico Perla - Sun Microsystems 	return (ret);
305048847494SEnrico Perla - Sun Microsystems 
305148847494SEnrico Perla - Sun Microsystems out_path_err:
305248847494SEnrico Perla - Sun Microsystems 	bam_error(PATH_TOO_LONG, root);
305348847494SEnrico Perla - Sun Microsystems 	return (BAM_ERROR);
305448847494SEnrico Perla - Sun Microsystems }
305548847494SEnrico Perla - Sun Microsystems 
30567c478bd9Sstevel@tonic-gate static error_t
30577c478bd9Sstevel@tonic-gate create_ramdisk(char *root)
30587c478bd9Sstevel@tonic-gate {
30597c478bd9Sstevel@tonic-gate 	char *cmdline, path[PATH_MAX];
30607c478bd9Sstevel@tonic-gate 	size_t len;
30617c478bd9Sstevel@tonic-gate 	struct stat sb;
3062*cedc7e57SEnrico Perla - Sun Microsystems 	int ret, what, status = BAM_SUCCESS;
306348847494SEnrico Perla - Sun Microsystems 
306448847494SEnrico Perla - Sun Microsystems 	/* If there is mkisofs, use it to create the required archives */
306548847494SEnrico Perla - Sun Microsystems 	if (is_mkisofs()) {
306648847494SEnrico Perla - Sun Microsystems 		for (what = FILE32; what < CACHEDIR_NUM; what++) {
3067*cedc7e57SEnrico Perla - Sun Microsystems 			if (has_cachedir(what) && is_dir_flag_on(what,
3068*cedc7e57SEnrico Perla - Sun Microsystems 			    NEED_UPDATE)) {
306948847494SEnrico Perla - Sun Microsystems 				ret = mkisofs_archive(root, what);
307048847494SEnrico Perla - Sun Microsystems 				if (ret != 0)
3071*cedc7e57SEnrico Perla - Sun Microsystems 					status = BAM_ERROR;
307248847494SEnrico Perla - Sun Microsystems 			}
307348847494SEnrico Perla - Sun Microsystems 		}
3074*cedc7e57SEnrico Perla - Sun Microsystems 		return (status);
307548847494SEnrico Perla - Sun Microsystems 	}
30767c478bd9Sstevel@tonic-gate 
30777c478bd9Sstevel@tonic-gate 	/*
307848847494SEnrico Perla - Sun Microsystems 	 * Else setup command args for create_ramdisk.ksh for the UFS archives
30797c478bd9Sstevel@tonic-gate 	 */
308048847494SEnrico Perla - Sun Microsystems 	if (bam_verbose)
308148847494SEnrico Perla - Sun Microsystems 		bam_print("mkisofs not found, creating UFS archive\n");
308248847494SEnrico Perla - Sun Microsystems 
3083986fd29aSsetje 	(void) snprintf(path, sizeof (path), "%s/%s", root, CREATE_RAMDISK);
30847c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
30857c478bd9Sstevel@tonic-gate 		bam_error(ARCH_EXEC_MISS, path, strerror(errno));
30867c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
30877c478bd9Sstevel@tonic-gate 	}
30887c478bd9Sstevel@tonic-gate 
3089*cedc7e57SEnrico Perla - Sun Microsystems 	if (is_safe_exec(path) == BAM_ERROR)
3090*cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
3091*cedc7e57SEnrico Perla - Sun Microsystems 
30927c478bd9Sstevel@tonic-gate 	len = strlen(path) + strlen(root) + 10;	/* room for space + -R */
3093d876c67dSjg 	if (bam_alt_platform)
3094d876c67dSjg 		len += strlen(bam_platform) + strlen("-p ");
30957c478bd9Sstevel@tonic-gate 	cmdline = s_calloc(1, len);
30967c478bd9Sstevel@tonic-gate 
3097d876c67dSjg 	if (bam_alt_platform) {
3098d876c67dSjg 		assert(strlen(root) > 1);
3099d876c67dSjg 		(void) snprintf(cmdline, len, "%s -p %s -R %s",
3100d876c67dSjg 		    path, bam_platform, root);
3101d876c67dSjg 		/* chop off / at the end */
3102d876c67dSjg 		cmdline[strlen(cmdline) - 1] = '\0';
3103d876c67dSjg 	} else if (strlen(root) > 1) {
31047c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s -R %s", path, root);
31057c478bd9Sstevel@tonic-gate 		/* chop off / at the end */
31067c478bd9Sstevel@tonic-gate 		cmdline[strlen(cmdline) - 1] = '\0';
31077c478bd9Sstevel@tonic-gate 	} else
31087c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s", path);
31097c478bd9Sstevel@tonic-gate 
3110986fd29aSsetje 	if (exec_cmd(cmdline, NULL) != 0) {
31117c478bd9Sstevel@tonic-gate 		bam_error(ARCHIVE_FAIL, cmdline);
31127c478bd9Sstevel@tonic-gate 		free(cmdline);
31137c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
31147c478bd9Sstevel@tonic-gate 	}
31157c478bd9Sstevel@tonic-gate 	free(cmdline);
31167c478bd9Sstevel@tonic-gate 	/*
3117986fd29aSsetje 	 * The existence of the expected archives used to be
3118986fd29aSsetje 	 * verified here. This check is done in create_ramdisk as
3119986fd29aSsetje 	 * it needs to be in sync with the altroot operated upon.
31207c478bd9Sstevel@tonic-gate 	 */
31217c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
31227c478bd9Sstevel@tonic-gate }
31237c478bd9Sstevel@tonic-gate 
31247c478bd9Sstevel@tonic-gate /*
31257c478bd9Sstevel@tonic-gate  * Checks if target filesystem is on a ramdisk
31267c478bd9Sstevel@tonic-gate  * 1 - is miniroot
31277c478bd9Sstevel@tonic-gate  * 0 - is not
31287c478bd9Sstevel@tonic-gate  * When in doubt assume it is not a ramdisk.
31297c478bd9Sstevel@tonic-gate  */
31307c478bd9Sstevel@tonic-gate static int
31317c478bd9Sstevel@tonic-gate is_ramdisk(char *root)
31327c478bd9Sstevel@tonic-gate {
31337c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
31347c478bd9Sstevel@tonic-gate 	FILE *fp;
31357c478bd9Sstevel@tonic-gate 	int found;
3136b610f78eSvikram 	char mntpt[PATH_MAX];
3137b610f78eSvikram 	char *cp;
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 	/*
31407c478bd9Sstevel@tonic-gate 	 * There are 3 situations where creating archive is
31417c478bd9Sstevel@tonic-gate 	 * of dubious value:
3142b610f78eSvikram 	 *	- create boot_archive on a lofi-mounted boot_archive
31437c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk which is the root filesystem
31447c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk mounted somewhere else
31457c478bd9Sstevel@tonic-gate 	 * The first is not easy to detect and checking for it is not
31467c478bd9Sstevel@tonic-gate 	 * worth it.
31477c478bd9Sstevel@tonic-gate 	 * The other two conditions are handled here
31487c478bd9Sstevel@tonic-gate 	 */
31497c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
31507c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
31517c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
31527c478bd9Sstevel@tonic-gate 		return (0);
31537c478bd9Sstevel@tonic-gate 	}
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
31567c478bd9Sstevel@tonic-gate 
3157b610f78eSvikram 	/*
3158b610f78eSvikram 	 * Remove any trailing / from the mount point
3159b610f78eSvikram 	 */
3160b610f78eSvikram 	(void) strlcpy(mntpt, root, sizeof (mntpt));
3161b610f78eSvikram 	if (strcmp(root, "/") != 0) {
3162b610f78eSvikram 		cp = mntpt + strlen(mntpt) - 1;
3163b610f78eSvikram 		if (*cp == '/')
3164b610f78eSvikram 			*cp = '\0';
3165b610f78eSvikram 	}
31667c478bd9Sstevel@tonic-gate 	found = 0;
31677c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
3168b610f78eSvikram 		if (strcmp(mnt.mnt_mountp, mntpt) == 0) {
31697c478bd9Sstevel@tonic-gate 			found = 1;
31707c478bd9Sstevel@tonic-gate 			break;
31717c478bd9Sstevel@tonic-gate 		}
31727c478bd9Sstevel@tonic-gate 	}
31737c478bd9Sstevel@tonic-gate 
31747c478bd9Sstevel@tonic-gate 	if (!found) {
31757c478bd9Sstevel@tonic-gate 		if (bam_verbose)
3176b610f78eSvikram 			bam_error(NOT_IN_MNTTAB, mntpt);
31777c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
31787c478bd9Sstevel@tonic-gate 		return (0);
31797c478bd9Sstevel@tonic-gate 	}
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate 	if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) {
31827c478bd9Sstevel@tonic-gate 		if (bam_verbose)
31837c478bd9Sstevel@tonic-gate 			bam_error(IS_RAMDISK, bam_root);
31847c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
31857c478bd9Sstevel@tonic-gate 		return (1);
31867c478bd9Sstevel@tonic-gate 	}
31877c478bd9Sstevel@tonic-gate 
31887c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
31897c478bd9Sstevel@tonic-gate 
31907c478bd9Sstevel@tonic-gate 	return (0);
31917c478bd9Sstevel@tonic-gate }
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate static int
3194986fd29aSsetje is_boot_archive(char *root)
31957c478bd9Sstevel@tonic-gate {
31967c478bd9Sstevel@tonic-gate 	char		path[PATH_MAX];
31977c478bd9Sstevel@tonic-gate 	struct stat	sb;
3198eb2bd662Svikram 	int		error;
3199eb2bd662Svikram 	const char	*fcn = "is_boot_archive()";
32007c478bd9Sstevel@tonic-gate 
32017c478bd9Sstevel@tonic-gate 	/*
3202986fd29aSsetje 	 * We can't create an archive without the create_ramdisk script
32037c478bd9Sstevel@tonic-gate 	 */
3204986fd29aSsetje 	(void) snprintf(path, sizeof (path), "%s/%s", root, CREATE_RAMDISK);
3205eb2bd662Svikram 	error = stat(path, &sb);
3206eb2bd662Svikram 	INJECT_ERROR1("NOT_ARCHIVE_BASED", error = -1);
3207eb2bd662Svikram 	if (error == -1) {
32087c478bd9Sstevel@tonic-gate 		if (bam_verbose)
32097c478bd9Sstevel@tonic-gate 			bam_print(FILE_MISS, path);
3210eb2bd662Svikram 		BAM_DPRINTF((D_NOT_ARCHIVE_BOOT, fcn, root));
32117c478bd9Sstevel@tonic-gate 		return (0);
32127c478bd9Sstevel@tonic-gate 	}
32137c478bd9Sstevel@tonic-gate 
3214eb2bd662Svikram 	BAM_DPRINTF((D_IS_ARCHIVE_BOOT, fcn, root));
3215986fd29aSsetje 	return (1);
3216986fd29aSsetje }
3217986fd29aSsetje 
32187c478bd9Sstevel@tonic-gate /*
3219986fd29aSsetje  * Need to call this for anything that operates on the GRUB menu
3220963390b4Svikram  * In the x86 live upgrade case the directory /boot/grub may be present
3221963390b4Svikram  * even on pre-newboot BEs. The authoritative way to check for a GRUB target
3222963390b4Svikram  * is to check for the presence of the stage2 binary which is present
3223963390b4Svikram  * only on GRUB targets (even on x86 boot partitions). Checking for the
3224963390b4Svikram  * presence of the multiboot binary is not correct as it is not present
3225963390b4Svikram  * on x86 boot partitions.
3226986fd29aSsetje  */
3227986fd29aSsetje int
3228986fd29aSsetje is_grub(const char *root)
3229986fd29aSsetje {
3230986fd29aSsetje 	char path[PATH_MAX];
3231986fd29aSsetje 	struct stat sb;
3232eb2bd662Svikram 	const char *fcn = "is_grub()";
3233986fd29aSsetje 
3234963390b4Svikram 	(void) snprintf(path, sizeof (path), "%s%s", root, GRUB_STAGE2);
32357c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
3236eb2bd662Svikram 		BAM_DPRINTF((D_NO_GRUB_DIR, fcn, path));
32377c478bd9Sstevel@tonic-gate 		return (0);
32387c478bd9Sstevel@tonic-gate 	}
32397c478bd9Sstevel@tonic-gate 
32407c478bd9Sstevel@tonic-gate 	return (1);
32417c478bd9Sstevel@tonic-gate }
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate static int
3244eb2bd662Svikram is_zfs(char *root)
3245eb2bd662Svikram {
3246eb2bd662Svikram 	struct statvfs		vfs;
3247eb2bd662Svikram 	int			ret;
3248eb2bd662Svikram 	const char		*fcn = "is_zfs()";
3249eb2bd662Svikram 
3250eb2bd662Svikram 	ret = statvfs(root, &vfs);
3251eb2bd662Svikram 	INJECT_ERROR1("STATVFS_ZFS", ret = 1);
3252eb2bd662Svikram 	if (ret != 0) {
3253eb2bd662Svikram 		bam_error(STATVFS_FAIL, root, strerror(errno));
3254eb2bd662Svikram 		return (0);
3255eb2bd662Svikram 	}
3256eb2bd662Svikram 
3257eb2bd662Svikram 	if (strncmp(vfs.f_basetype, "zfs", strlen("zfs")) == 0) {
3258eb2bd662Svikram 		BAM_DPRINTF((D_IS_ZFS, fcn, root));
3259eb2bd662Svikram 		return (1);
3260eb2bd662Svikram 	} else {
3261eb2bd662Svikram 		BAM_DPRINTF((D_IS_NOT_ZFS, fcn, root));
3262eb2bd662Svikram 		return (0);
3263eb2bd662Svikram 	}
3264eb2bd662Svikram }
3265eb2bd662Svikram 
3266eb2bd662Svikram static int
3267eb2bd662Svikram is_ufs(char *root)
3268eb2bd662Svikram {
3269eb2bd662Svikram 	struct statvfs		vfs;
3270eb2bd662Svikram 	int			ret;
3271eb2bd662Svikram 	const char		*fcn = "is_ufs()";
3272eb2bd662Svikram 
3273eb2bd662Svikram 	ret = statvfs(root, &vfs);
3274eb2bd662Svikram 	INJECT_ERROR1("STATVFS_UFS", ret = 1);
3275eb2bd662Svikram 	if (ret != 0) {
3276eb2bd662Svikram 		bam_error(STATVFS_FAIL, root, strerror(errno));
3277eb2bd662Svikram 		return (0);
3278eb2bd662Svikram 	}
3279eb2bd662Svikram 
3280eb2bd662Svikram 	if (strncmp(vfs.f_basetype, "ufs", strlen("ufs")) == 0) {
3281eb2bd662Svikram 		BAM_DPRINTF((D_IS_UFS, fcn, root));
3282eb2bd662Svikram 		return (1);
3283eb2bd662Svikram 	} else {
3284eb2bd662Svikram 		BAM_DPRINTF((D_IS_NOT_UFS, fcn, root));
3285eb2bd662Svikram 		return (0);
3286eb2bd662Svikram 	}
3287eb2bd662Svikram }
3288eb2bd662Svikram 
3289eb2bd662Svikram static int
3290eb2bd662Svikram is_pcfs(char *root)
3291eb2bd662Svikram {
3292eb2bd662Svikram 	struct statvfs		vfs;
3293eb2bd662Svikram 	int			ret;
3294eb2bd662Svikram 	const char		*fcn = "is_pcfs()";
3295eb2bd662Svikram 
3296eb2bd662Svikram 	ret = statvfs(root, &vfs);
3297eb2bd662Svikram 	INJECT_ERROR1("STATVFS_PCFS", ret = 1);
3298eb2bd662Svikram 	if (ret != 0) {
3299eb2bd662Svikram 		bam_error(STATVFS_FAIL, root, strerror(errno));
3300eb2bd662Svikram 		return (0);
3301eb2bd662Svikram 	}
3302eb2bd662Svikram 
3303eb2bd662Svikram 	if (strncmp(vfs.f_basetype, "pcfs", strlen("pcfs")) == 0) {
3304eb2bd662Svikram 		BAM_DPRINTF((D_IS_PCFS, fcn, root));
3305eb2bd662Svikram 		return (1);
3306eb2bd662Svikram 	} else {
3307eb2bd662Svikram 		BAM_DPRINTF((D_IS_NOT_PCFS, fcn, root));
3308eb2bd662Svikram 		return (0);
3309eb2bd662Svikram 	}
3310eb2bd662Svikram }
3311eb2bd662Svikram 
3312eb2bd662Svikram static int
33137c478bd9Sstevel@tonic-gate is_readonly(char *root)
33147c478bd9Sstevel@tonic-gate {
3315eb2bd662Svikram 	int		fd;
3316eb2bd662Svikram 	int		error;
3317eb2bd662Svikram 	char		testfile[PATH_MAX];
3318eb2bd662Svikram 	const char	*fcn = "is_readonly()";
33197c478bd9Sstevel@tonic-gate 
33207c478bd9Sstevel@tonic-gate 	/*
3321eb2bd662Svikram 	 * Using statvfs() to check for a read-only filesystem is not
3322eb2bd662Svikram 	 * reliable. The only way to reliably test is to attempt to
3323eb2bd662Svikram 	 * create a file
33247c478bd9Sstevel@tonic-gate 	 */
3325eb2bd662Svikram 	(void) snprintf(testfile, sizeof (testfile), "%s/%s.%d",
3326eb2bd662Svikram 	    root, BOOTADM_RDONLY_TEST, getpid());
33277c478bd9Sstevel@tonic-gate 
3328eb2bd662Svikram 	(void) unlink(testfile);
3329eb2bd662Svikram 
3330eb2bd662Svikram 	errno = 0;
3331eb2bd662Svikram 	fd = open(testfile, O_RDWR|O_CREAT|O_EXCL, 0644);
3332eb2bd662Svikram 	error = errno;
3333eb2bd662Svikram 	INJECT_ERROR2("RDONLY_TEST_ERROR", fd = -1, error = EACCES);
3334eb2bd662Svikram 	if (fd == -1 && error == EROFS) {
3335eb2bd662Svikram 		BAM_DPRINTF((D_RDONLY_FS, fcn, root));
33367c478bd9Sstevel@tonic-gate 		return (1);
3337eb2bd662Svikram 	} else if (fd == -1) {
3338eb2bd662Svikram 		bam_error(RDONLY_TEST_ERROR, root, strerror(error));
33397c478bd9Sstevel@tonic-gate 	}
33407c478bd9Sstevel@tonic-gate 
3341eb2bd662Svikram 	(void) close(fd);
3342eb2bd662Svikram 	(void) unlink(testfile);
33437c478bd9Sstevel@tonic-gate 
3344eb2bd662Svikram 	BAM_DPRINTF((D_RDWR_FS, fcn, root));
3345e7cbe64fSgw25295 	return (0);
3346e7cbe64fSgw25295 }
3347e7cbe64fSgw25295 
33487c478bd9Sstevel@tonic-gate static error_t
33497c478bd9Sstevel@tonic-gate update_archive(char *root, char *opt)
33507c478bd9Sstevel@tonic-gate {
33517c478bd9Sstevel@tonic-gate 	error_t ret;
33527c478bd9Sstevel@tonic-gate 
33537c478bd9Sstevel@tonic-gate 	assert(root);
33547c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
33557c478bd9Sstevel@tonic-gate 
335648847494SEnrico Perla - Sun Microsystems 	init_walk_args();
335748847494SEnrico Perla - Sun Microsystems 	(void) umask(022);
335848847494SEnrico Perla - Sun Microsystems 
33597c478bd9Sstevel@tonic-gate 	/*
3360986fd29aSsetje 	 * root must belong to a boot archive based OS,
33617c478bd9Sstevel@tonic-gate 	 */
3362986fd29aSsetje 	if (!is_boot_archive(root)) {
3363b610f78eSvikram 		/*
3364b610f78eSvikram 		 * Emit message only if not in context of update_all.
3365b610f78eSvikram 		 * If in update_all, emit only if verbose flag is set.
3366b610f78eSvikram 		 */
3367b610f78eSvikram 		if (!bam_update_all || bam_verbose)
3368eb2bd662Svikram 			bam_print(NOT_ARCHIVE_BOOT, root);
336948847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
33707c478bd9Sstevel@tonic-gate 	}
33717c478bd9Sstevel@tonic-gate 
33727c478bd9Sstevel@tonic-gate 	/*
33738c1b6884Sszhou 	 * If smf check is requested when / is writable (can happen
33748c1b6884Sszhou 	 * on first reboot following an upgrade because service
33758c1b6884Sszhou 	 * dependency is messed up), skip the check.
33768c1b6884Sszhou 	 */
337748847494SEnrico Perla - Sun Microsystems 	if (bam_smf_check && !bam_root_readonly && !is_zfs(root))
33788c1b6884Sszhou 		return (BAM_SUCCESS);
33798c1b6884Sszhou 
33808c1b6884Sszhou 	/*
33818c1b6884Sszhou 	 * root must be writable. This check applies to alternate
33828c1b6884Sszhou 	 * root (-R option); bam_root_readonly applies to '/' only.
338348847494SEnrico Perla - Sun Microsystems 	 * The behaviour translates into being the one of a 'check'.
33847c478bd9Sstevel@tonic-gate 	 */
338561cb17bdSsetje 	if (!bam_smf_check && !bam_check && is_readonly(root)) {
338648847494SEnrico Perla - Sun Microsystems 		set_flag(RDONLY_FSCHK);
338748847494SEnrico Perla - Sun Microsystems 		bam_check = 1;
33887c478bd9Sstevel@tonic-gate 	}
33897c478bd9Sstevel@tonic-gate 
33907c478bd9Sstevel@tonic-gate 	/*
3391*cedc7e57SEnrico Perla - Sun Microsystems 	 * Don't generate archive on ramdisk.
33927c478bd9Sstevel@tonic-gate 	 */
3393*cedc7e57SEnrico Perla - Sun Microsystems 	if (is_ramdisk(root))
3394*cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
33957c478bd9Sstevel@tonic-gate 
33967c478bd9Sstevel@tonic-gate 	/*
3397*cedc7e57SEnrico Perla - Sun Microsystems 	 * Now check if an update is really needed.
33987c478bd9Sstevel@tonic-gate 	 */
33997c478bd9Sstevel@tonic-gate 	ret = update_required(root);
34007c478bd9Sstevel@tonic-gate 
34017c478bd9Sstevel@tonic-gate 	/*
3402*cedc7e57SEnrico Perla - Sun Microsystems 	 * The check command (-n) is *not* a dry run.
34037c478bd9Sstevel@tonic-gate 	 * It only checks if the archive is in sync.
3404*cedc7e57SEnrico Perla - Sun Microsystems 	 * A readonly filesystem has to be considered an error only if an update
3405*cedc7e57SEnrico Perla - Sun Microsystems 	 * is required.
34067c478bd9Sstevel@tonic-gate 	 */
3407*cedc7e57SEnrico Perla - Sun Microsystems 	if (bam_nowrite()) {
340848847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(RDONLY_FSCHK)) {
340948847494SEnrico Perla - Sun Microsystems 			if (ret > 0)
341048847494SEnrico Perla - Sun Microsystems 				bam_error(RDONLY_FS, root);
341148847494SEnrico Perla - Sun Microsystems 			if (bam_update_all)
341248847494SEnrico Perla - Sun Microsystems 				return ((ret != 0) ? BAM_ERROR : BAM_SUCCESS);
341348847494SEnrico Perla - Sun Microsystems 		}
341448847494SEnrico Perla - Sun Microsystems 
34157c478bd9Sstevel@tonic-gate 		bam_exit((ret != 0) ? 1 : 0);
34167c478bd9Sstevel@tonic-gate 	}
34177c478bd9Sstevel@tonic-gate 
34187c478bd9Sstevel@tonic-gate 	if (ret == 1) {
34197c478bd9Sstevel@tonic-gate 		/* create the ramdisk */
34207c478bd9Sstevel@tonic-gate 		ret = create_ramdisk(root);
34217c478bd9Sstevel@tonic-gate 	}
3422986fd29aSsetje 
3423986fd29aSsetje 	/* if the archive is updated, save the new stat data */
3424986fd29aSsetje 	if (ret == 0 && walk_arg.new_nvlp != NULL) {
3425986fd29aSsetje 		savenew(root);
3426986fd29aSsetje 	}
3427986fd29aSsetje 
3428986fd29aSsetje 	clear_walk_args();
3429986fd29aSsetje 
34307c478bd9Sstevel@tonic-gate 	return (ret);
34317c478bd9Sstevel@tonic-gate }
34327c478bd9Sstevel@tonic-gate 
3433963390b4Svikram static error_t
3434963390b4Svikram synchronize_BE_menu(void)
3435fbac2b2bSvikram {
3436fbac2b2bSvikram 	struct stat	sb;
3437963390b4Svikram 	char		cmdline[PATH_MAX];
3438963390b4Svikram 	char		cksum_line[PATH_MAX];
3439963390b4Svikram 	filelist_t	flist = {0};
3440963390b4Svikram 	char		*old_cksum_str;
3441963390b4Svikram 	char		*old_size_str;
3442963390b4Svikram 	char		*old_file;
3443963390b4Svikram 	char		*curr_cksum_str;
3444963390b4Svikram 	char		*curr_size_str;
3445963390b4Svikram 	char		*curr_file;
3446963390b4Svikram 	FILE		*cfp;
3447963390b4Svikram 	int		found;
3448963390b4Svikram 	int		ret;
3449963390b4Svikram 	const char	*fcn = "synchronize_BE_menu()";
3450fbac2b2bSvikram 
3451963390b4Svikram 	BAM_DPRINTF((D_FUNC_ENTRY0, fcn));
3452fbac2b2bSvikram 
3453963390b4Svikram 	/* Check if findroot enabled LU BE */
3454963390b4Svikram 	if (stat(FINDROOT_INSTALLGRUB, &sb) != 0) {
3455963390b4Svikram 		BAM_DPRINTF((D_NOT_LU_BE, fcn));
3456963390b4Svikram 		return (BAM_SUCCESS);
3457fbac2b2bSvikram 	}
3458fbac2b2bSvikram 
3459963390b4Svikram 	if (stat(LU_MENU_CKSUM, &sb) != 0) {
3460963390b4Svikram 		BAM_DPRINTF((D_NO_CKSUM_FILE, fcn, LU_MENU_CKSUM));
3461963390b4Svikram 		goto menu_sync;
3462fbac2b2bSvikram 	}
3463963390b4Svikram 
3464963390b4Svikram 	cfp = fopen(LU_MENU_CKSUM, "r");
3465963390b4Svikram 	INJECT_ERROR1("CKSUM_FILE_MISSING", cfp = NULL);
3466963390b4Svikram 	if (cfp == NULL) {
3467963390b4Svikram 		bam_error(CANNOT_READ_LU_CKSUM, LU_MENU_CKSUM);
3468963390b4Svikram 		goto menu_sync;
3469963390b4Svikram 	}
3470963390b4Svikram 	BAM_DPRINTF((D_CKSUM_FILE_OPENED, fcn, LU_MENU_CKSUM));
3471963390b4Svikram 
3472963390b4Svikram 	found = 0;
3473963390b4Svikram 	while (s_fgets(cksum_line, sizeof (cksum_line), cfp) != NULL) {
3474963390b4Svikram 		INJECT_ERROR1("MULTIPLE_CKSUM", found = 1);
3475963390b4Svikram 		if (found) {
3476963390b4Svikram 			bam_error(MULTIPLE_LU_CKSUM, LU_MENU_CKSUM);
3477963390b4Svikram 			(void) fclose(cfp);
3478963390b4Svikram 			goto menu_sync;
3479963390b4Svikram 		}
3480963390b4Svikram 		found = 1;
3481963390b4Svikram 	}
3482963390b4Svikram 	BAM_DPRINTF((D_CKSUM_FILE_READ, fcn, LU_MENU_CKSUM));
3483963390b4Svikram 
3484963390b4Svikram 
3485963390b4Svikram 	old_cksum_str = strtok(cksum_line, " \t");
3486963390b4Svikram 	old_size_str = strtok(NULL, " \t");
3487963390b4Svikram 	old_file = strtok(NULL, " \t");
3488963390b4Svikram 
3489963390b4Svikram 	INJECT_ERROR1("OLD_CKSUM_NULL", old_cksum_str = NULL);
3490963390b4Svikram 	INJECT_ERROR1("OLD_SIZE_NULL", old_size_str = NULL);
3491963390b4Svikram 	INJECT_ERROR1("OLD_FILE_NULL", old_file = NULL);
3492963390b4Svikram 	if (old_cksum_str == NULL || old_size_str == NULL || old_file == NULL) {
3493963390b4Svikram 		bam_error(CANNOT_PARSE_LU_CKSUM, LU_MENU_CKSUM);
3494963390b4Svikram 		goto menu_sync;
3495963390b4Svikram 	}
3496963390b4Svikram 	BAM_DPRINTF((D_CKSUM_FILE_PARSED, fcn, LU_MENU_CKSUM));
3497963390b4Svikram 
3498963390b4Svikram 	/* Get checksum of current menu */
3499963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s",
3500963390b4Svikram 	    CKSUM, GRUB_MENU);
3501963390b4Svikram 	ret = exec_cmd(cmdline, &flist);
3502963390b4Svikram 	INJECT_ERROR1("GET_CURR_CKSUM", ret = 1);
3503963390b4Svikram 	if (ret != 0) {
3504963390b4Svikram 		bam_error(MENU_CKSUM_FAIL);
3505963390b4Svikram 		return (BAM_ERROR);
3506963390b4Svikram 	}
3507963390b4Svikram 	BAM_DPRINTF((D_CKSUM_GEN_SUCCESS, fcn));
3508963390b4Svikram 
3509963390b4Svikram 	INJECT_ERROR1("GET_CURR_CKSUM_OUTPUT", flist.head = NULL);
3510963390b4Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
3511963390b4Svikram 		bam_error(BAD_CKSUM);
3512963390b4Svikram 		filelist_free(&flist);
3513963390b4Svikram 		return (BAM_ERROR);
3514963390b4Svikram 	}
3515963390b4Svikram 	BAM_DPRINTF((D_CKSUM_GEN_OUTPUT_VALID, fcn));
3516963390b4Svikram 
3517963390b4Svikram 	curr_cksum_str = strtok(flist.head->line, " \t");
3518963390b4Svikram 	curr_size_str = strtok(NULL, " \t");
3519963390b4Svikram 	curr_file = strtok(NULL, " \t");
3520963390b4Svikram 
3521963390b4Svikram 	INJECT_ERROR1("CURR_CKSUM_NULL", curr_cksum_str = NULL);
3522963390b4Svikram 	INJECT_ERROR1("CURR_SIZE_NULL", curr_size_str = NULL);
3523963390b4Svikram 	INJECT_ERROR1("CURR_FILE_NULL", curr_file = NULL);
3524963390b4Svikram 	if (curr_cksum_str == NULL || curr_size_str == NULL ||
3525963390b4Svikram 	    curr_file == NULL) {
3526963390b4Svikram 		bam_error(BAD_CKSUM_PARSE);
3527963390b4Svikram 		filelist_free(&flist);
3528963390b4Svikram 		return (BAM_ERROR);
3529963390b4Svikram 	}
3530963390b4Svikram 	BAM_DPRINTF((D_CKSUM_GEN_PARSED, fcn));
3531963390b4Svikram 
3532963390b4Svikram 	if (strcmp(old_cksum_str, curr_cksum_str) == 0 &&
3533963390b4Svikram 	    strcmp(old_size_str, curr_size_str) == 0 &&
3534963390b4Svikram 	    strcmp(old_file, curr_file) == 0) {
3535963390b4Svikram 		filelist_free(&flist);
3536963390b4Svikram 		BAM_DPRINTF((D_CKSUM_NO_CHANGE, fcn));
3537963390b4Svikram 		return (BAM_SUCCESS);
3538963390b4Svikram 	}
3539963390b4Svikram 
3540963390b4Svikram 	filelist_free(&flist);
3541963390b4Svikram 
3542963390b4Svikram 	/* cksum doesn't match - the menu has changed */
3543963390b4Svikram 	BAM_DPRINTF((D_CKSUM_HAS_CHANGED, fcn));
3544963390b4Svikram 
3545963390b4Svikram menu_sync:
3546963390b4Svikram 	bam_print(PROP_GRUB_MENU);
3547963390b4Svikram 
3548963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
3549a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s yes > /dev/null'",
3550963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, GRUB_MENU);
3551963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
3552963390b4Svikram 	INJECT_ERROR1("PROPAGATE_MENU", ret = 1);
3553963390b4Svikram 	if (ret != 0) {
3554963390b4Svikram 		bam_error(MENU_PROP_FAIL);
3555963390b4Svikram 		return (BAM_ERROR);
3556963390b4Svikram 	}
3557963390b4Svikram 	BAM_DPRINTF((D_PROPAGATED_MENU, fcn));
3558963390b4Svikram 
3559a6968364Svikram 	(void) snprintf(cmdline, sizeof (cmdline), "/bin/cp %s %s > /dev/null",
3560963390b4Svikram 	    GRUB_MENU, GRUB_BACKUP_MENU);
3561963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
3562963390b4Svikram 	INJECT_ERROR1("CREATE_BACKUP", ret = 1);
3563963390b4Svikram 	if (ret != 0) {
3564963390b4Svikram 		bam_error(MENU_BACKUP_FAIL, GRUB_BACKUP_MENU);
3565963390b4Svikram 		return (BAM_ERROR);
3566963390b4Svikram 	}
3567963390b4Svikram 	BAM_DPRINTF((D_CREATED_BACKUP, fcn, GRUB_BACKUP_MENU));
3568963390b4Svikram 
3569963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
3570a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s no > /dev/null'",
3571963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, GRUB_BACKUP_MENU);
3572963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
3573963390b4Svikram 	INJECT_ERROR1("PROPAGATE_BACKUP", ret = 1);
3574963390b4Svikram 	if (ret != 0) {
3575963390b4Svikram 		bam_error(BACKUP_PROP_FAIL, GRUB_BACKUP_MENU);
3576963390b4Svikram 		return (BAM_ERROR);
3577963390b4Svikram 	}
3578963390b4Svikram 	BAM_DPRINTF((D_PROPAGATED_BACKUP, fcn, GRUB_BACKUP_MENU));
3579963390b4Svikram 
3580963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s > %s",
3581963390b4Svikram 	    CKSUM, GRUB_MENU, LU_MENU_CKSUM);
3582963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
3583963390b4Svikram 	INJECT_ERROR1("CREATE_CKSUM_FILE", ret = 1);
3584963390b4Svikram 	if (ret != 0) {
3585963390b4Svikram 		bam_error(MENU_CKSUM_WRITE_FAIL, LU_MENU_CKSUM);
3586963390b4Svikram 		return (BAM_ERROR);
3587963390b4Svikram 	}
3588963390b4Svikram 	BAM_DPRINTF((D_CREATED_CKSUM_FILE, fcn, LU_MENU_CKSUM));
3589963390b4Svikram 
3590963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
3591a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s no > /dev/null'",
3592963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, LU_MENU_CKSUM);
3593963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
3594963390b4Svikram 	INJECT_ERROR1("PROPAGATE_MENU_CKSUM_FILE", ret = 1);
3595963390b4Svikram 	if (ret != 0) {
3596963390b4Svikram 		bam_error(MENU_CKSUM_PROP_FAIL, LU_MENU_CKSUM);
3597963390b4Svikram 		return (BAM_ERROR);
3598963390b4Svikram 	}
3599963390b4Svikram 	BAM_DPRINTF((D_PROPAGATED_CKSUM_FILE, fcn, LU_MENU_CKSUM));
3600963390b4Svikram 
3601963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
3602a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s no > /dev/null'",
3603963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, BOOTADM);
3604963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
3605963390b4Svikram 	INJECT_ERROR1("PROPAGATE_BOOTADM_FILE", ret = 1);
3606963390b4Svikram 	if (ret != 0) {
3607963390b4Svikram 		bam_error(BOOTADM_PROP_FAIL, BOOTADM);
3608963390b4Svikram 		return (BAM_ERROR);
3609963390b4Svikram 	}
3610963390b4Svikram 	BAM_DPRINTF((D_PROPAGATED_BOOTADM, fcn, BOOTADM));
3611963390b4Svikram 
3612963390b4Svikram 	return (BAM_SUCCESS);
3613fbac2b2bSvikram }
3614fbac2b2bSvikram 
36157c478bd9Sstevel@tonic-gate static error_t
36167c478bd9Sstevel@tonic-gate update_all(char *root, char *opt)
36177c478bd9Sstevel@tonic-gate {
36187c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
36197c478bd9Sstevel@tonic-gate 	struct stat sb;
36207c478bd9Sstevel@tonic-gate 	FILE *fp;
36217c478bd9Sstevel@tonic-gate 	char multibt[PATH_MAX];
3622986fd29aSsetje 	char creatram[PATH_MAX];
36237c478bd9Sstevel@tonic-gate 	error_t ret = BAM_SUCCESS;
36247c478bd9Sstevel@tonic-gate 
362540541d5dSvikram 	assert(root);
36267c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
36277c478bd9Sstevel@tonic-gate 
362840541d5dSvikram 	if (bam_rootlen != 1 || *root != '/') {
362940541d5dSvikram 		elide_trailing_slash(root, multibt, sizeof (multibt));
363040541d5dSvikram 		bam_error(ALT_ROOT_INVALID, multibt);
363140541d5dSvikram 		return (BAM_ERROR);
363240541d5dSvikram 	}
363340541d5dSvikram 
36347c478bd9Sstevel@tonic-gate 	/*
363598892a30Snadkarni 	 * Check to see if we are in the midst of safemode patching
363698892a30Snadkarni 	 * If so skip building the archive for /. Instead build it
363798892a30Snadkarni 	 * against the latest bits obtained by creating a fresh lofs
363898892a30Snadkarni 	 * mount of root.
363998892a30Snadkarni 	 */
364098892a30Snadkarni 	if (stat(LOFS_PATCH_FILE, &sb) == 0)  {
364148847494SEnrico Perla - Sun Microsystems 		if (mkdir(LOFS_PATCH_MNT, DIR_PERMS) == -1 &&
364298892a30Snadkarni 		    errno != EEXIST) {
364398892a30Snadkarni 			bam_error(MKDIR_FAILED, "%s", LOFS_PATCH_MNT,
364498892a30Snadkarni 			    strerror(errno));
364598892a30Snadkarni 			ret = BAM_ERROR;
364698892a30Snadkarni 			goto out;
364798892a30Snadkarni 		}
364898892a30Snadkarni 		(void) snprintf(multibt, sizeof (multibt),
364998892a30Snadkarni 		    "/sbin/mount -F lofs -o nosub /  %s", LOFS_PATCH_MNT);
3650986fd29aSsetje 		if (exec_cmd(multibt, NULL) != 0) {
365198892a30Snadkarni 			bam_error(MOUNT_FAILED, LOFS_PATCH_MNT, "lofs");
365298892a30Snadkarni 			ret = BAM_ERROR;
365398892a30Snadkarni 		}
365498892a30Snadkarni 		if (ret != BAM_ERROR) {
365598892a30Snadkarni 			(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
365698892a30Snadkarni 			    LOFS_PATCH_MNT);
365798892a30Snadkarni 			bam_rootlen = strlen(rootbuf);
365898892a30Snadkarni 			if (update_archive(rootbuf, opt) != BAM_SUCCESS)
365998892a30Snadkarni 				ret = BAM_ERROR;
366095b1e0e9Snadkarni 			/*
366195b1e0e9Snadkarni 			 * unmount the lofs mount since there could be
366295b1e0e9Snadkarni 			 * multiple invocations of bootadm -a update_all
366395b1e0e9Snadkarni 			 */
366495b1e0e9Snadkarni 			(void) snprintf(multibt, sizeof (multibt),
366595b1e0e9Snadkarni 			    "/sbin/umount %s", LOFS_PATCH_MNT);
3666986fd29aSsetje 			if (exec_cmd(multibt, NULL) != 0) {
366795b1e0e9Snadkarni 				bam_error(UMOUNT_FAILED, LOFS_PATCH_MNT);
366895b1e0e9Snadkarni 				ret = BAM_ERROR;
366995b1e0e9Snadkarni 			}
367098892a30Snadkarni 		}
367198892a30Snadkarni 	} else {
367298892a30Snadkarni 		/*
36737c478bd9Sstevel@tonic-gate 		 * First update archive for current root
36747c478bd9Sstevel@tonic-gate 		 */
36757c478bd9Sstevel@tonic-gate 		if (update_archive(root, opt) != BAM_SUCCESS)
36767c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
367798892a30Snadkarni 	}
367898892a30Snadkarni 
367998892a30Snadkarni 	if (ret == BAM_ERROR)
368098892a30Snadkarni 		goto out;
36817c478bd9Sstevel@tonic-gate 
36827c478bd9Sstevel@tonic-gate 	/*
36837c478bd9Sstevel@tonic-gate 	 * Now walk the mount table, performing archive update
36847c478bd9Sstevel@tonic-gate 	 * for all mounted Newboot root filesystems
36857c478bd9Sstevel@tonic-gate 	 */
36867c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
36877c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
36887c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
3689b610f78eSvikram 		ret = BAM_ERROR;
3690b610f78eSvikram 		goto out;
36917c478bd9Sstevel@tonic-gate 	}
36927c478bd9Sstevel@tonic-gate 
36937c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
36947c478bd9Sstevel@tonic-gate 
36957c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
36967c478bd9Sstevel@tonic-gate 		if (mnt.mnt_special == NULL)
36977c478bd9Sstevel@tonic-gate 			continue;
3698f904d32dSJerry Gilliam 		if ((strcmp(mnt.mnt_fstype, MNTTYPE_ZFS) != 0) &&
3699f904d32dSJerry Gilliam 		    (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0))
37007c478bd9Sstevel@tonic-gate 			continue;
37017c478bd9Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, "/") == 0)
37027c478bd9Sstevel@tonic-gate 			continue;
37037c478bd9Sstevel@tonic-gate 
3704986fd29aSsetje 		(void) snprintf(creatram, sizeof (creatram), "%s/%s",
3705986fd29aSsetje 		    mnt.mnt_mountp, CREATE_RAMDISK);
37067c478bd9Sstevel@tonic-gate 
3707986fd29aSsetje 		if (stat(creatram, &sb) == -1)
37087c478bd9Sstevel@tonic-gate 			continue;
37097c478bd9Sstevel@tonic-gate 
37107c478bd9Sstevel@tonic-gate 		/*
37117c478bd9Sstevel@tonic-gate 		 * We put a trailing slash to be consistent with root = "/"
37127c478bd9Sstevel@tonic-gate 		 * case, such that we don't have to print // in some cases.
37137c478bd9Sstevel@tonic-gate 		 */
37147c478bd9Sstevel@tonic-gate 		(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
37157c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp);
37167c478bd9Sstevel@tonic-gate 		bam_rootlen = strlen(rootbuf);
3717ae115bc7Smrj 
3718ae115bc7Smrj 		/*
3719ae115bc7Smrj 		 * It's possible that other mounts may be an alternate boot
3720ae115bc7Smrj 		 * architecture, so check it again.
3721ae115bc7Smrj 		 */
3722eb2bd662Svikram 		if ((get_boot_cap(rootbuf) != BAM_SUCCESS) ||
3723ae115bc7Smrj 		    (update_archive(rootbuf, opt) != BAM_SUCCESS))
37247c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
37257c478bd9Sstevel@tonic-gate 	}
37267c478bd9Sstevel@tonic-gate 
37277c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
37287c478bd9Sstevel@tonic-gate 
3729b610f78eSvikram out:
3730fbac2b2bSvikram 	/*
3731963390b4Svikram 	 * We no longer use biosdev for Live Upgrade. Hence
3732963390b4Svikram 	 * there is no need to defer (to shutdown time) any fdisk
3733963390b4Svikram 	 * updates
3734fbac2b2bSvikram 	 */
3735963390b4Svikram 	if (stat(GRUB_fdisk, &sb) == 0 || stat(GRUB_fdisk_target, &sb) == 0) {
3736963390b4Svikram 		bam_error(FDISK_FILES_FOUND, GRUB_fdisk, GRUB_fdisk_target);
3737fbac2b2bSvikram 	}
3738fbac2b2bSvikram 
3739963390b4Svikram 	/*
3740963390b4Svikram 	 * If user has updated menu in current BE, propagate the
3741963390b4Svikram 	 * updates to all BEs.
3742963390b4Svikram 	 */
374319397407SSherry Moore 	if (sync_menu && synchronize_BE_menu() != BAM_SUCCESS)
3744963390b4Svikram 		ret = BAM_ERROR;
3745963390b4Svikram 
37467c478bd9Sstevel@tonic-gate 	return (ret);
37477c478bd9Sstevel@tonic-gate }
37487c478bd9Sstevel@tonic-gate 
37497c478bd9Sstevel@tonic-gate static void
37507c478bd9Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp)
37517c478bd9Sstevel@tonic-gate {
37527c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
37537c478bd9Sstevel@tonic-gate 		mp->start = lp;
37547c478bd9Sstevel@tonic-gate 	} else {
37557c478bd9Sstevel@tonic-gate 		mp->end->next = lp;
37568c1b6884Sszhou 		lp->prev = mp->end;
37577c478bd9Sstevel@tonic-gate 	}
37587c478bd9Sstevel@tonic-gate 	mp->end = lp;
37597c478bd9Sstevel@tonic-gate }
37607c478bd9Sstevel@tonic-gate 
3761eb2bd662Svikram void
37628c1b6884Sszhou unlink_line(menu_t *mp, line_t *lp)
37638c1b6884Sszhou {
37648c1b6884Sszhou 	/* unlink from list */
37658c1b6884Sszhou 	if (lp->prev)
37668c1b6884Sszhou 		lp->prev->next = lp->next;
37678c1b6884Sszhou 	else
37688c1b6884Sszhou 		mp->start = lp->next;
37698c1b6884Sszhou 	if (lp->next)
37708c1b6884Sszhou 		lp->next->prev = lp->prev;
37718c1b6884Sszhou 	else
37728c1b6884Sszhou 		mp->end = lp->prev;
37738c1b6884Sszhou }
37748c1b6884Sszhou 
37758c1b6884Sszhou static entry_t *
37768c1b6884Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end)
37778c1b6884Sszhou {
37788c1b6884Sszhou 	entry_t *ent, *prev;
3779eb2bd662Svikram 	const char *fcn = "boot_entry_new()";
3780eb2bd662Svikram 
3781eb2bd662Svikram 	assert(mp);
3782eb2bd662Svikram 	assert(start);
3783eb2bd662Svikram 	assert(end);
37848c1b6884Sszhou 
37858c1b6884Sszhou 	ent = s_calloc(1, sizeof (entry_t));
3786eb2bd662Svikram 	BAM_DPRINTF((D_ENTRY_NEW, fcn));
37878c1b6884Sszhou 	ent->start = start;
37888c1b6884Sszhou 	ent->end = end;
37898c1b6884Sszhou 
37908c1b6884Sszhou 	if (mp->entries == NULL) {
37918c1b6884Sszhou 		mp->entries = ent;
3792eb2bd662Svikram 		BAM_DPRINTF((D_ENTRY_NEW_FIRST, fcn));
37938c1b6884Sszhou 		return (ent);
37948c1b6884Sszhou 	}
37958c1b6884Sszhou 
37968c1b6884Sszhou 	prev = mp->entries;
37978c1b6884Sszhou 	while (prev->next)
37988c1b6884Sszhou 		prev = prev->next;
37998c1b6884Sszhou 	prev->next = ent;
38008c1b6884Sszhou 	ent->prev = prev;
3801eb2bd662Svikram 	BAM_DPRINTF((D_ENTRY_NEW_LINKED, fcn));
38028c1b6884Sszhou 	return (ent);
38038c1b6884Sszhou }
38048c1b6884Sszhou 
38058c1b6884Sszhou static void
38068c1b6884Sszhou boot_entry_addline(entry_t *ent, line_t *lp)
38078c1b6884Sszhou {
38088c1b6884Sszhou 	if (ent)
38098c1b6884Sszhou 		ent->end = lp;
38108c1b6884Sszhou }
38118c1b6884Sszhou 
38127c478bd9Sstevel@tonic-gate /*
3813843e1988Sjohnlev  * Check whether cmd matches the one indexed by which, and whether arg matches
3814843e1988Sjohnlev  * str.  which must be either KERNEL_CMD or MODULE_CMD, and a match to the
3815843e1988Sjohnlev  * respective *_DOLLAR_CMD is also acceptable.  The arg is searched using
3816843e1988Sjohnlev  * strstr(), so it can be a partial match.
3817843e1988Sjohnlev  */
3818843e1988Sjohnlev static int
3819843e1988Sjohnlev check_cmd(const char *cmd, const int which, const char *arg, const char *str)
3820843e1988Sjohnlev {
3821eb2bd662Svikram 	int			ret;
3822eb2bd662Svikram 	const char		*fcn = "check_cmd()";
3823eb2bd662Svikram 
3824eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, arg, str));
3825eb2bd662Svikram 
3826843e1988Sjohnlev 	if ((strcmp(cmd, menu_cmds[which]) != 0) &&
3827843e1988Sjohnlev 	    (strcmp(cmd, menu_cmds[which + 1]) != 0)) {
3828eb2bd662Svikram 		BAM_DPRINTF((D_CHECK_CMD_CMD_NOMATCH,
3829eb2bd662Svikram 		    fcn, cmd, menu_cmds[which]));
3830843e1988Sjohnlev 		return (0);
3831843e1988Sjohnlev 	}
3832eb2bd662Svikram 	ret = (strstr(arg, str) != NULL);
3833eb2bd662Svikram 
3834eb2bd662Svikram 	if (ret) {
3835eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
3836eb2bd662Svikram 	} else {
3837eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
3838eb2bd662Svikram 	}
3839eb2bd662Svikram 
3840eb2bd662Svikram 	return (ret);
3841eb2bd662Svikram }
3842eb2bd662Svikram 
3843eb2bd662Svikram static error_t
3844eb2bd662Svikram kernel_parser(entry_t *entry, char *cmd, char *arg, int linenum)
3845eb2bd662Svikram {
3846eb2bd662Svikram 	const char		*fcn  = "kernel_parser()";
3847eb2bd662Svikram 
3848eb2bd662Svikram 	assert(entry);
3849eb2bd662Svikram 	assert(cmd);
3850eb2bd662Svikram 	assert(arg);
3851eb2bd662Svikram 
3852eb2bd662Svikram 	if (strcmp(cmd, menu_cmds[KERNEL_CMD]) != 0 &&
3853eb2bd662Svikram 	    strcmp(cmd, menu_cmds[KERNEL_DOLLAR_CMD]) != 0) {
3854eb2bd662Svikram 		BAM_DPRINTF((D_NOT_KERNEL_CMD, fcn, cmd));
3855eb2bd662Svikram 		return (BAM_ERROR);
3856eb2bd662Svikram 	}
3857eb2bd662Svikram 
3858eb2bd662Svikram 	if (strncmp(arg, DIRECT_BOOT_32, sizeof (DIRECT_BOOT_32) - 1) == 0) {
3859eb2bd662Svikram 		BAM_DPRINTF((D_SET_DBOOT_32, fcn, arg));
3860eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_32BIT;
3861eb2bd662Svikram 	} else if (strncmp(arg, DIRECT_BOOT_KERNEL,
3862eb2bd662Svikram 	    sizeof (DIRECT_BOOT_KERNEL) - 1) == 0) {
3863eb2bd662Svikram 		BAM_DPRINTF((D_SET_DBOOT, fcn, arg));
3864eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT;
3865eb2bd662Svikram 	} else if (strncmp(arg, DIRECT_BOOT_64,
3866eb2bd662Svikram 	    sizeof (DIRECT_BOOT_64) - 1) == 0) {
3867eb2bd662Svikram 		BAM_DPRINTF((D_SET_DBOOT_64, fcn, arg));
3868eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_64BIT;
3869eb2bd662Svikram 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_KERNEL,
3870eb2bd662Svikram 	    sizeof (DIRECT_BOOT_FAILSAFE_KERNEL) - 1) == 0) {
3871eb2bd662Svikram 		BAM_DPRINTF((D_SET_DBOOT_FAILSAFE, fcn, arg));
3872eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE;
3873bbcc54bdSEnrico Perla - Sun Microsystems 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_32,
3874bbcc54bdSEnrico Perla - Sun Microsystems 	    sizeof (DIRECT_BOOT_FAILSAFE_32) - 1) == 0) {
3875bbcc54bdSEnrico Perla - Sun Microsystems 		BAM_DPRINTF((D_SET_DBOOT_FAILSAFE_32, fcn, arg));
3876bbcc54bdSEnrico Perla - Sun Microsystems 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE
3877bbcc54bdSEnrico Perla - Sun Microsystems 		    | BAM_ENTRY_32BIT;
3878bbcc54bdSEnrico Perla - Sun Microsystems 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_64,
3879bbcc54bdSEnrico Perla - Sun Microsystems 	    sizeof (DIRECT_BOOT_FAILSAFE_64) - 1) == 0) {
3880bbcc54bdSEnrico Perla - Sun Microsystems 		BAM_DPRINTF((D_SET_DBOOT_FAILSAFE_64, fcn, arg));
3881bbcc54bdSEnrico Perla - Sun Microsystems 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE
3882bbcc54bdSEnrico Perla - Sun Microsystems 		    | BAM_ENTRY_64BIT;
3883eb2bd662Svikram 	} else if (strncmp(arg, MULTI_BOOT, sizeof (MULTI_BOOT) - 1) == 0) {
3884eb2bd662Svikram 		BAM_DPRINTF((D_SET_MULTIBOOT, fcn, arg));
3885eb2bd662Svikram 		entry->flags |= BAM_ENTRY_MULTIBOOT;
3886eb2bd662Svikram 	} else if (strncmp(arg, MULTI_BOOT_FAILSAFE,
3887eb2bd662Svikram 	    sizeof (MULTI_BOOT_FAILSAFE) - 1) == 0) {
3888eb2bd662Svikram 		BAM_DPRINTF((D_SET_MULTIBOOT_FAILSAFE, fcn, arg));
3889eb2bd662Svikram 		entry->flags |= BAM_ENTRY_MULTIBOOT | BAM_ENTRY_FAILSAFE;
3890eb2bd662Svikram 	} else if (strstr(arg, XEN_KERNEL_SUBSTR)) {
3891eb2bd662Svikram 		BAM_DPRINTF((D_SET_HV, fcn, arg));
3892eb2bd662Svikram 		entry->flags |= BAM_ENTRY_HV;
3893eb2bd662Svikram 	} else if (!(entry->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU))) {
3894eb2bd662Svikram 		BAM_DPRINTF((D_SET_HAND_KERNEL, fcn, arg));
3895eb2bd662Svikram 		return (BAM_ERROR);
389637eb779cSVikram Hegde 	} else if (strncmp(arg, KERNEL_PREFIX, strlen(KERNEL_PREFIX)) == 0 &&
389737eb779cSVikram Hegde 	    strstr(arg, UNIX_SPACE)) {
389837eb779cSVikram Hegde 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_32BIT;
389937eb779cSVikram Hegde 	} else if (strncmp(arg, KERNEL_PREFIX, strlen(KERNEL_PREFIX)) == 0 &&
390037eb779cSVikram Hegde 	    strstr(arg, AMD_UNIX_SPACE)) {
390137eb779cSVikram Hegde 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_64BIT;
3902eb2bd662Svikram 	} else {
3903eb2bd662Svikram 		BAM_DPRINTF((D_IS_UNKNOWN_KERNEL, fcn, arg));
3904eb2bd662Svikram 		bam_error(UNKNOWN_KERNEL_LINE, linenum);
3905eb2bd662Svikram 		return (BAM_ERROR);
3906eb2bd662Svikram 	}
3907eb2bd662Svikram 
3908eb2bd662Svikram 	return (BAM_SUCCESS);
3909eb2bd662Svikram }
3910eb2bd662Svikram 
3911eb2bd662Svikram static error_t
3912eb2bd662Svikram module_parser(entry_t *entry, char *cmd, char *arg, int linenum)
3913eb2bd662Svikram {
3914eb2bd662Svikram 	const char		*fcn = "module_parser()";
3915eb2bd662Svikram 
3916eb2bd662Svikram 	assert(entry);
3917eb2bd662Svikram 	assert(cmd);
3918eb2bd662Svikram 	assert(arg);
3919eb2bd662Svikram 
3920eb2bd662Svikram 	if (strcmp(cmd, menu_cmds[MODULE_CMD]) != 0 &&
3921eb2bd662Svikram 	    strcmp(cmd, menu_cmds[MODULE_DOLLAR_CMD]) != 0) {
3922eb2bd662Svikram 		BAM_DPRINTF((D_NOT_MODULE_CMD, fcn, cmd));
3923eb2bd662Svikram 		return (BAM_ERROR);
3924eb2bd662Svikram 	}
3925eb2bd662Svikram 
3926eb2bd662Svikram 	if (strcmp(arg, DIRECT_BOOT_ARCHIVE) == 0 ||
3927eb2bd662Svikram 	    strcmp(arg, DIRECT_BOOT_ARCHIVE_32) == 0 ||
3928eb2bd662Svikram 	    strcmp(arg, DIRECT_BOOT_ARCHIVE_64) == 0 ||
3929eb2bd662Svikram 	    strcmp(arg, MULTIBOOT_ARCHIVE) == 0 ||
3930eb2bd662Svikram 	    strcmp(arg, FAILSAFE_ARCHIVE) == 0 ||
3931bbcc54bdSEnrico Perla - Sun Microsystems 	    strcmp(arg, FAILSAFE_ARCHIVE_32) == 0 ||
3932bbcc54bdSEnrico Perla - Sun Microsystems 	    strcmp(arg, FAILSAFE_ARCHIVE_64) == 0 ||
3933eb2bd662Svikram 	    strcmp(arg, XEN_KERNEL_MODULE_LINE) == 0 ||
3934eb2bd662Svikram 	    strcmp(arg, XEN_KERNEL_MODULE_LINE_ZFS) == 0) {
3935eb2bd662Svikram 		BAM_DPRINTF((D_BOOTADM_LU_MODULE, fcn, arg));
3936eb2bd662Svikram 		return (BAM_SUCCESS);
3937eb2bd662Svikram 	} else if (!(entry->flags & BAM_ENTRY_BOOTADM) &&
3938eb2bd662Svikram 	    !(entry->flags & BAM_ENTRY_LU)) {
3939eb2bd662Svikram 		/* don't emit warning for hand entries */
3940eb2bd662Svikram 		BAM_DPRINTF((D_IS_HAND_MODULE, fcn, arg));
3941eb2bd662Svikram 		return (BAM_ERROR);
3942eb2bd662Svikram 	} else {
3943eb2bd662Svikram 		BAM_DPRINTF((D_IS_UNKNOWN_MODULE, fcn, arg));
3944eb2bd662Svikram 		bam_error(UNKNOWN_MODULE_LINE, linenum);
3945eb2bd662Svikram 		return (BAM_ERROR);
3946eb2bd662Svikram 	}
3947843e1988Sjohnlev }
3948843e1988Sjohnlev 
3949843e1988Sjohnlev /*
39507c478bd9Sstevel@tonic-gate  * A line in menu.lst looks like
39517c478bd9Sstevel@tonic-gate  * [ ]*<cmd>[ \t=]*<arg>*
39527c478bd9Sstevel@tonic-gate  */
39537c478bd9Sstevel@tonic-gate static void
39547c478bd9Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum)
39557c478bd9Sstevel@tonic-gate {
39567c478bd9Sstevel@tonic-gate 	/*
39577c478bd9Sstevel@tonic-gate 	 * save state across calls. This is so that
39587c478bd9Sstevel@tonic-gate 	 * header gets the right entry# after title has
39597c478bd9Sstevel@tonic-gate 	 * been processed
39607c478bd9Sstevel@tonic-gate 	 */
39618c1b6884Sszhou 	static line_t *prev = NULL;
39628c1b6884Sszhou 	static entry_t *curr_ent = NULL;
3963ae115bc7Smrj 	static int in_liveupgrade = 0;
39647c478bd9Sstevel@tonic-gate 
39657c478bd9Sstevel@tonic-gate 	line_t	*lp;
39667c478bd9Sstevel@tonic-gate 	char *cmd, *sep, *arg;
39677c478bd9Sstevel@tonic-gate 	char save, *cp, *line;
39687c478bd9Sstevel@tonic-gate 	menu_flag_t flag = BAM_INVALID;
3969eb2bd662Svikram 	const char *fcn = "line_parser()";
39707c478bd9Sstevel@tonic-gate 
39717c478bd9Sstevel@tonic-gate 	if (str == NULL) {
39727c478bd9Sstevel@tonic-gate 		return;
39737c478bd9Sstevel@tonic-gate 	}
39747c478bd9Sstevel@tonic-gate 
39757c478bd9Sstevel@tonic-gate 	/*
39767c478bd9Sstevel@tonic-gate 	 * First save a copy of the entire line.
39777c478bd9Sstevel@tonic-gate 	 * We use this later to set the line field.
39787c478bd9Sstevel@tonic-gate 	 */
39797c478bd9Sstevel@tonic-gate 	line = s_strdup(str);
39807c478bd9Sstevel@tonic-gate 
39817c478bd9Sstevel@tonic-gate 	/* Eat up leading whitespace */
39827c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
39837c478bd9Sstevel@tonic-gate 		str++;
39847c478bd9Sstevel@tonic-gate 
39857c478bd9Sstevel@tonic-gate 	if (*str == '#') {		/* comment */
39867c478bd9Sstevel@tonic-gate 		cmd = s_strdup("#");
39877c478bd9Sstevel@tonic-gate 		sep = NULL;
39887c478bd9Sstevel@tonic-gate 		arg = s_strdup(str + 1);
39897c478bd9Sstevel@tonic-gate 		flag = BAM_COMMENT;
3990ae115bc7Smrj 		if (strstr(arg, BAM_LU_HDR) != NULL) {
3991ae115bc7Smrj 			in_liveupgrade = 1;
3992ae115bc7Smrj 		} else if (strstr(arg, BAM_LU_FTR) != NULL) {
3993ae115bc7Smrj 			in_liveupgrade = 0;
3994ae115bc7Smrj 		}
39957c478bd9Sstevel@tonic-gate 	} else if (*str == '\0') {	/* blank line */
39967c478bd9Sstevel@tonic-gate 		cmd = sep = arg = NULL;
39977c478bd9Sstevel@tonic-gate 		flag = BAM_EMPTY;
39987c478bd9Sstevel@tonic-gate 	} else {
39997c478bd9Sstevel@tonic-gate 		/*
40007c478bd9Sstevel@tonic-gate 		 * '=' is not a documented separator in grub syntax.
40017c478bd9Sstevel@tonic-gate 		 * However various development bits use '=' as a
40027c478bd9Sstevel@tonic-gate 		 * separator. In addition, external users also
40037c478bd9Sstevel@tonic-gate 		 * use = as a separator. So we will allow that usage.
40047c478bd9Sstevel@tonic-gate 		 */
40057c478bd9Sstevel@tonic-gate 		cp = str;
40067c478bd9Sstevel@tonic-gate 		while (*str != ' ' && *str != '\t' && *str != '=') {
40077c478bd9Sstevel@tonic-gate 			if (*str == '\0') {
40087c478bd9Sstevel@tonic-gate 				cmd = s_strdup(cp);
40097c478bd9Sstevel@tonic-gate 				sep = arg = NULL;
40107c478bd9Sstevel@tonic-gate 				break;
40117c478bd9Sstevel@tonic-gate 			}
40127c478bd9Sstevel@tonic-gate 			str++;
40137c478bd9Sstevel@tonic-gate 		}
40147c478bd9Sstevel@tonic-gate 
40157c478bd9Sstevel@tonic-gate 		if (*str != '\0') {
40167c478bd9Sstevel@tonic-gate 			save = *str;
40177c478bd9Sstevel@tonic-gate 			*str = '\0';
40187c478bd9Sstevel@tonic-gate 			cmd = s_strdup(cp);
40197c478bd9Sstevel@tonic-gate 			*str = save;
40207c478bd9Sstevel@tonic-gate 
40217c478bd9Sstevel@tonic-gate 			str++;
40227c478bd9Sstevel@tonic-gate 			save = *str;
40237c478bd9Sstevel@tonic-gate 			*str = '\0';
40247c478bd9Sstevel@tonic-gate 			sep = s_strdup(str - 1);
40257c478bd9Sstevel@tonic-gate 			*str = save;
40267c478bd9Sstevel@tonic-gate 
40277c478bd9Sstevel@tonic-gate 			while (*str == ' ' || *str == '\t')
40287c478bd9Sstevel@tonic-gate 				str++;
40297c478bd9Sstevel@tonic-gate 			if (*str == '\0')
40307c478bd9Sstevel@tonic-gate 				arg = NULL;
40317c478bd9Sstevel@tonic-gate 			else
40327c478bd9Sstevel@tonic-gate 				arg = s_strdup(str);
40337c478bd9Sstevel@tonic-gate 		}
40347c478bd9Sstevel@tonic-gate 	}
40357c478bd9Sstevel@tonic-gate 
40367c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
40377c478bd9Sstevel@tonic-gate 
40387c478bd9Sstevel@tonic-gate 	lp->cmd = cmd;
40397c478bd9Sstevel@tonic-gate 	lp->sep = sep;
40407c478bd9Sstevel@tonic-gate 	lp->arg = arg;
40417c478bd9Sstevel@tonic-gate 	lp->line = line;
40427c478bd9Sstevel@tonic-gate 	lp->lineNum = ++(*lineNum);
40437c478bd9Sstevel@tonic-gate 	if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) {
40447c478bd9Sstevel@tonic-gate 		lp->entryNum = ++(*entryNum);
40457c478bd9Sstevel@tonic-gate 		lp->flags = BAM_TITLE;
40467c478bd9Sstevel@tonic-gate 		if (prev && prev->flags == BAM_COMMENT &&
4047ae115bc7Smrj 		    prev->arg && strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
40487c478bd9Sstevel@tonic-gate 			prev->entryNum = lp->entryNum;
40498c1b6884Sszhou 			curr_ent = boot_entry_new(mp, prev, lp);
4050eb2bd662Svikram 			curr_ent->flags |= BAM_ENTRY_BOOTADM;
4051eb2bd662Svikram 			BAM_DPRINTF((D_IS_BOOTADM_ENTRY, fcn, arg));
40528c1b6884Sszhou 		} else {
40538c1b6884Sszhou 			curr_ent = boot_entry_new(mp, lp, lp);
4054ae115bc7Smrj 			if (in_liveupgrade) {
4055eb2bd662Svikram 				curr_ent->flags |= BAM_ENTRY_LU;
4056eb2bd662Svikram 				BAM_DPRINTF((D_IS_LU_ENTRY, fcn, arg));
40578c1b6884Sszhou 			}
4058ae115bc7Smrj 		}
4059ae115bc7Smrj 		curr_ent->entryNum = *entryNum;
40607c478bd9Sstevel@tonic-gate 	} else if (flag != BAM_INVALID) {
40617c478bd9Sstevel@tonic-gate 		/*
40627c478bd9Sstevel@tonic-gate 		 * For header comments, the entry# is "fixed up"
40637c478bd9Sstevel@tonic-gate 		 * by the subsequent title
40647c478bd9Sstevel@tonic-gate 		 */
40657c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
40667c478bd9Sstevel@tonic-gate 		lp->flags = flag;
40677c478bd9Sstevel@tonic-gate 	} else {
40687c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
4069ae115bc7Smrj 
4070ae115bc7Smrj 		if (*entryNum == ENTRY_INIT) {
4071ae115bc7Smrj 			lp->flags = BAM_GLOBAL;
4072ae115bc7Smrj 		} else {
4073ae115bc7Smrj 			lp->flags = BAM_ENTRY;
4074ae115bc7Smrj 
4075ae115bc7Smrj 			if (cmd && arg) {
4076eb2bd662Svikram 				if (strcmp(cmd, menu_cmds[ROOT_CMD]) == 0) {
4077eb2bd662Svikram 					BAM_DPRINTF((D_IS_ROOT_CMD, fcn, arg));
4078ae115bc7Smrj 					curr_ent->flags |= BAM_ENTRY_ROOT;
4079eb2bd662Svikram 				} else if (strcmp(cmd, menu_cmds[FINDROOT_CMD])
4080eb2bd662Svikram 				    == 0) {
4081eb2bd662Svikram 					BAM_DPRINTF((D_IS_FINDROOT_CMD, fcn,
4082eb2bd662Svikram 					    arg));
4083eb2bd662Svikram 					curr_ent->flags |= BAM_ENTRY_FINDROOT;
4084eb2bd662Svikram 				} else if (strcmp(cmd,
4085eb2bd662Svikram 				    menu_cmds[CHAINLOADER_CMD]) == 0) {
4086eb2bd662Svikram 					BAM_DPRINTF((D_IS_CHAINLOADER_CMD, fcn,
4087eb2bd662Svikram 					    arg));
4088ae115bc7Smrj 					curr_ent->flags |=
4089ae115bc7Smrj 					    BAM_ENTRY_CHAINLOADER;
4090eb2bd662Svikram 				} else if (kernel_parser(curr_ent, cmd, arg,
4091eb2bd662Svikram 				    lp->lineNum) != BAM_SUCCESS) {
4092eb2bd662Svikram 					(void) module_parser(curr_ent, cmd,
4093eb2bd662Svikram 					    arg, lp->lineNum);
4094eb2bd662Svikram 				}
4095ae115bc7Smrj 			}
4096ae115bc7Smrj 		}
40977c478bd9Sstevel@tonic-gate 	}
40987c478bd9Sstevel@tonic-gate 
40998c1b6884Sszhou 	/* record default, old default, and entry line ranges */
41008c1b6884Sszhou 	if (lp->flags == BAM_GLOBAL &&
41018c1b6884Sszhou 	    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) {
41028c1b6884Sszhou 		mp->curdefault = lp;
41038c1b6884Sszhou 	} else if (lp->flags == BAM_COMMENT &&
41048c1b6884Sszhou 	    strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) {
41058c1b6884Sszhou 		mp->olddefault = lp;
4106ae115bc7Smrj 	} else if (lp->flags == BAM_COMMENT &&
4107ae115bc7Smrj 	    strncmp(lp->arg, BAM_OLD_RC_DEF, strlen(BAM_OLD_RC_DEF)) == 0) {
4108ae115bc7Smrj 		mp->old_rc_default = lp;
41098c1b6884Sszhou 	} else if (lp->flags == BAM_ENTRY ||
4110ae115bc7Smrj 	    (lp->flags == BAM_COMMENT &&
4111ae115bc7Smrj 	    strcmp(lp->arg, BAM_BOOTADM_FTR) == 0)) {
41128c1b6884Sszhou 		boot_entry_addline(curr_ent, lp);
41138c1b6884Sszhou 	}
41147c478bd9Sstevel@tonic-gate 	append_line(mp, lp);
41157c478bd9Sstevel@tonic-gate 
41167c478bd9Sstevel@tonic-gate 	prev = lp;
41177c478bd9Sstevel@tonic-gate }
41187c478bd9Sstevel@tonic-gate 
4119eb2bd662Svikram void
412040541d5dSvikram update_numbering(menu_t *mp)
412140541d5dSvikram {
412240541d5dSvikram 	int lineNum;
412340541d5dSvikram 	int entryNum;
412440541d5dSvikram 	int old_default_value;
412540541d5dSvikram 	line_t *lp, *prev, *default_lp, *default_entry;
412640541d5dSvikram 	char buf[PATH_MAX];
412740541d5dSvikram 
412840541d5dSvikram 	if (mp->start == NULL) {
412940541d5dSvikram 		return;
413040541d5dSvikram 	}
413140541d5dSvikram 
413240541d5dSvikram 	lineNum = LINE_INIT;
413340541d5dSvikram 	entryNum = ENTRY_INIT;
413440541d5dSvikram 	old_default_value = ENTRY_INIT;
413540541d5dSvikram 	lp = default_lp = default_entry = NULL;
413640541d5dSvikram 
413740541d5dSvikram 	prev = NULL;
413840541d5dSvikram 	for (lp = mp->start; lp; prev = lp, lp = lp->next) {
413940541d5dSvikram 		lp->lineNum = ++lineNum;
414040541d5dSvikram 
414140541d5dSvikram 		/*
414240541d5dSvikram 		 * Get the value of the default command
414340541d5dSvikram 		 */
414440541d5dSvikram 		if (lp->entryNum == ENTRY_INIT && lp->cmd &&
414540541d5dSvikram 		    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 &&
414640541d5dSvikram 		    lp->arg) {
414740541d5dSvikram 			old_default_value = atoi(lp->arg);
414840541d5dSvikram 			default_lp = lp;
414940541d5dSvikram 		}
415040541d5dSvikram 
415140541d5dSvikram 		/*
4152eb2bd662Svikram 		 * If not a booting entry, nothing else to fix for this
415340541d5dSvikram 		 * entry
415440541d5dSvikram 		 */
415540541d5dSvikram 		if (lp->entryNum == ENTRY_INIT)
415640541d5dSvikram 			continue;
415740541d5dSvikram 
415840541d5dSvikram 		/*
415940541d5dSvikram 		 * Record the position of the default entry.
416040541d5dSvikram 		 * The following works because global
416140541d5dSvikram 		 * commands like default and timeout should precede
416240541d5dSvikram 		 * actual boot entries, so old_default_value
416340541d5dSvikram 		 * is already known (or default cmd is missing).
416440541d5dSvikram 		 */
416540541d5dSvikram 		if (default_entry == NULL &&
416640541d5dSvikram 		    old_default_value != ENTRY_INIT &&
416740541d5dSvikram 		    lp->entryNum == old_default_value) {
416840541d5dSvikram 			default_entry = lp;
416940541d5dSvikram 		}
417040541d5dSvikram 
417140541d5dSvikram 		/*
417240541d5dSvikram 		 * Now fixup the entry number
417340541d5dSvikram 		 */
417440541d5dSvikram 		if (lp->cmd && strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) {
417540541d5dSvikram 			lp->entryNum = ++entryNum;
417640541d5dSvikram 			/* fixup the bootadm header */
417740541d5dSvikram 			if (prev && prev->flags == BAM_COMMENT &&
4178ae115bc7Smrj 			    prev->arg &&
4179ae115bc7Smrj 			    strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
418040541d5dSvikram 				prev->entryNum = lp->entryNum;
418140541d5dSvikram 			}
418240541d5dSvikram 		} else {
418340541d5dSvikram 			lp->entryNum = entryNum;
418440541d5dSvikram 		}
418540541d5dSvikram 	}
418640541d5dSvikram 
418740541d5dSvikram 	/*
418840541d5dSvikram 	 * No default command in menu, simply return
418940541d5dSvikram 	 */
419040541d5dSvikram 	if (default_lp == NULL) {
419140541d5dSvikram 		return;
419240541d5dSvikram 	}
419340541d5dSvikram 
419440541d5dSvikram 	free(default_lp->arg);
419540541d5dSvikram 	free(default_lp->line);
419640541d5dSvikram 
419740541d5dSvikram 	if (default_entry == NULL) {
419840541d5dSvikram 		default_lp->arg = s_strdup("0");
419940541d5dSvikram 	} else {
420040541d5dSvikram 		(void) snprintf(buf, sizeof (buf), "%d",
420140541d5dSvikram 		    default_entry->entryNum);
420240541d5dSvikram 		default_lp->arg = s_strdup(buf);
420340541d5dSvikram 	}
420440541d5dSvikram 
420540541d5dSvikram 	/*
420640541d5dSvikram 	 * The following is required since only the line field gets
420740541d5dSvikram 	 * written back to menu.lst
420840541d5dSvikram 	 */
420940541d5dSvikram 	(void) snprintf(buf, sizeof (buf), "%s%s%s",
421040541d5dSvikram 	    menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg);
421140541d5dSvikram 	default_lp->line = s_strdup(buf);
421240541d5dSvikram }
421340541d5dSvikram 
421440541d5dSvikram 
42157c478bd9Sstevel@tonic-gate static menu_t *
42167c478bd9Sstevel@tonic-gate menu_read(char *menu_path)
42177c478bd9Sstevel@tonic-gate {
42187c478bd9Sstevel@tonic-gate 	FILE *fp;
42197c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE], *cp;
42207c478bd9Sstevel@tonic-gate 	menu_t *mp;
42217c478bd9Sstevel@tonic-gate 	int line, entry, len, n;
42227c478bd9Sstevel@tonic-gate 
42237c478bd9Sstevel@tonic-gate 	mp = s_calloc(1, sizeof (menu_t));
42247c478bd9Sstevel@tonic-gate 
42257c478bd9Sstevel@tonic-gate 	fp = fopen(menu_path, "r");
42267c478bd9Sstevel@tonic-gate 	if (fp == NULL) { /* Let the caller handle this error */
42277c478bd9Sstevel@tonic-gate 		return (mp);
42287c478bd9Sstevel@tonic-gate 	}
42297c478bd9Sstevel@tonic-gate 
42307c478bd9Sstevel@tonic-gate 
42317c478bd9Sstevel@tonic-gate 	/* Note: GRUB boot entry number starts with 0 */
42327c478bd9Sstevel@tonic-gate 	line = LINE_INIT;
42337c478bd9Sstevel@tonic-gate 	entry = ENTRY_INIT;
42347c478bd9Sstevel@tonic-gate 	cp = buf;
42357c478bd9Sstevel@tonic-gate 	len = sizeof (buf);
42367c478bd9Sstevel@tonic-gate 	while (s_fgets(cp, len, fp) != NULL) {
42377c478bd9Sstevel@tonic-gate 		n = strlen(cp);
42387c478bd9Sstevel@tonic-gate 		if (cp[n - 1] == '\\') {
42397c478bd9Sstevel@tonic-gate 			len -= n - 1;
42407c478bd9Sstevel@tonic-gate 			assert(len >= 2);
42417c478bd9Sstevel@tonic-gate 			cp += n - 1;
42427c478bd9Sstevel@tonic-gate 			continue;
42437c478bd9Sstevel@tonic-gate 		}
42447c478bd9Sstevel@tonic-gate 		line_parser(mp, buf, &line, &entry);
42457c478bd9Sstevel@tonic-gate 		cp = buf;
42467c478bd9Sstevel@tonic-gate 		len = sizeof (buf);
42477c478bd9Sstevel@tonic-gate 	}
42487c478bd9Sstevel@tonic-gate 
42497c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
42507c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, menu_path, strerror(errno));
42517c478bd9Sstevel@tonic-gate 	}
42527c478bd9Sstevel@tonic-gate 
42537c478bd9Sstevel@tonic-gate 	return (mp);
42547c478bd9Sstevel@tonic-gate }
42557c478bd9Sstevel@tonic-gate 
42567c478bd9Sstevel@tonic-gate static error_t
42577c478bd9Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title)
42587c478bd9Sstevel@tonic-gate {
42597c478bd9Sstevel@tonic-gate 	char *eq;
42607c478bd9Sstevel@tonic-gate 	char *opt_dup;
42617c478bd9Sstevel@tonic-gate 	int entryNum;
42627c478bd9Sstevel@tonic-gate 
42637c478bd9Sstevel@tonic-gate 	assert(mp);
42647c478bd9Sstevel@tonic-gate 	assert(mp->start);
42657c478bd9Sstevel@tonic-gate 	assert(opt);
42667c478bd9Sstevel@tonic-gate 
42677c478bd9Sstevel@tonic-gate 	opt_dup = s_strdup(opt);
42687c478bd9Sstevel@tonic-gate 
42697c478bd9Sstevel@tonic-gate 	if (entry)
42707c478bd9Sstevel@tonic-gate 		*entry = ENTRY_INIT;
42717c478bd9Sstevel@tonic-gate 	if (title)
42727c478bd9Sstevel@tonic-gate 		*title = NULL;
42737c478bd9Sstevel@tonic-gate 
42747c478bd9Sstevel@tonic-gate 	eq = strchr(opt_dup, '=');
42757c478bd9Sstevel@tonic-gate 	if (eq == NULL) {
42767c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
42777c478bd9Sstevel@tonic-gate 		free(opt_dup);
42787c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
42797c478bd9Sstevel@tonic-gate 	}
42807c478bd9Sstevel@tonic-gate 
42817c478bd9Sstevel@tonic-gate 	*eq = '\0';
42827c478bd9Sstevel@tonic-gate 	if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) {
42837c478bd9Sstevel@tonic-gate 		assert(mp->end);
42847c478bd9Sstevel@tonic-gate 		entryNum = s_strtol(eq + 1);
42857c478bd9Sstevel@tonic-gate 		if (entryNum < 0 || entryNum > mp->end->entryNum) {
42867c478bd9Sstevel@tonic-gate 			bam_error(INVALID_ENTRY, eq + 1);
42877c478bd9Sstevel@tonic-gate 			free(opt_dup);
42887c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
42897c478bd9Sstevel@tonic-gate 		}
42907c478bd9Sstevel@tonic-gate 		*entry = entryNum;
42917c478bd9Sstevel@tonic-gate 	} else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) {
42927c478bd9Sstevel@tonic-gate 		*title = opt + (eq - opt_dup) + 1;
42937c478bd9Sstevel@tonic-gate 	} else {
42947c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
42957c478bd9Sstevel@tonic-gate 		free(opt_dup);
42967c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
42977c478bd9Sstevel@tonic-gate 	}
42987c478bd9Sstevel@tonic-gate 
42997c478bd9Sstevel@tonic-gate 	free(opt_dup);
43007c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
43017c478bd9Sstevel@tonic-gate }
43027c478bd9Sstevel@tonic-gate 
43037c478bd9Sstevel@tonic-gate /*
43047c478bd9Sstevel@tonic-gate  * If invoked with no titles/entries (opt == NULL)
43057c478bd9Sstevel@tonic-gate  * only title lines in file are printed.
43067c478bd9Sstevel@tonic-gate  *
43077c478bd9Sstevel@tonic-gate  * If invoked with a title or entry #, all
43087c478bd9Sstevel@tonic-gate  * lines in *every* matching entry are listed
43097c478bd9Sstevel@tonic-gate  */
43107c478bd9Sstevel@tonic-gate static error_t
43117c478bd9Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt)
43127c478bd9Sstevel@tonic-gate {
43137c478bd9Sstevel@tonic-gate 	line_t *lp;
43147c478bd9Sstevel@tonic-gate 	int entry = ENTRY_INIT;
43157c478bd9Sstevel@tonic-gate 	int found;
43167c478bd9Sstevel@tonic-gate 	char *title = NULL;
43177c478bd9Sstevel@tonic-gate 
43187c478bd9Sstevel@tonic-gate 	assert(mp);
43197c478bd9Sstevel@tonic-gate 	assert(menu_path);
43207c478bd9Sstevel@tonic-gate 
4321eb2bd662Svikram 	/* opt is optional */
4322eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, "list_entry", menu_path,
4323eb2bd662Svikram 	    opt ? opt : "<NULL>"));
4324eb2bd662Svikram 
43257c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
43267c478bd9Sstevel@tonic-gate 		bam_error(NO_MENU, menu_path);
43277c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
43287c478bd9Sstevel@tonic-gate 	}
43297c478bd9Sstevel@tonic-gate 
43307c478bd9Sstevel@tonic-gate 	if (opt != NULL) {
43317c478bd9Sstevel@tonic-gate 		if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
43327c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
43337c478bd9Sstevel@tonic-gate 		}
43347c478bd9Sstevel@tonic-gate 		assert((entry != ENTRY_INIT) ^ (title != NULL));
43357c478bd9Sstevel@tonic-gate 	} else {
43367c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0);
43377c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0);
43387c478bd9Sstevel@tonic-gate 	}
43397c478bd9Sstevel@tonic-gate 
43407c478bd9Sstevel@tonic-gate 	found = 0;
43417c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
43427c478bd9Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY)
43437c478bd9Sstevel@tonic-gate 			continue;
43447c478bd9Sstevel@tonic-gate 		if (opt == NULL && lp->flags == BAM_TITLE) {
43457c478bd9Sstevel@tonic-gate 			bam_print(PRINT_TITLE, lp->entryNum,
43467c478bd9Sstevel@tonic-gate 			    lp->arg);
43477c478bd9Sstevel@tonic-gate 			found = 1;
43487c478bd9Sstevel@tonic-gate 			continue;
43497c478bd9Sstevel@tonic-gate 		}
43507c478bd9Sstevel@tonic-gate 		if (entry != ENTRY_INIT && lp->entryNum == entry) {
43517c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
43527c478bd9Sstevel@tonic-gate 			found = 1;
43537c478bd9Sstevel@tonic-gate 			continue;
43547c478bd9Sstevel@tonic-gate 		}
43557c478bd9Sstevel@tonic-gate 
43567c478bd9Sstevel@tonic-gate 		/*
43577c478bd9Sstevel@tonic-gate 		 * We set the entry value here so that all lines
43587c478bd9Sstevel@tonic-gate 		 * in entry get printed. If we subsequently match
43597c478bd9Sstevel@tonic-gate 		 * title in other entries, all lines in those
43607c478bd9Sstevel@tonic-gate 		 * entries get printed as well.
43617c478bd9Sstevel@tonic-gate 		 */
43627c478bd9Sstevel@tonic-gate 		if (title && lp->flags == BAM_TITLE && lp->arg &&
43637c478bd9Sstevel@tonic-gate 		    strncmp(title, lp->arg, strlen(title)) == 0) {
43647c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
43657c478bd9Sstevel@tonic-gate 			entry = lp->entryNum;
43667c478bd9Sstevel@tonic-gate 			found = 1;
43677c478bd9Sstevel@tonic-gate 			continue;
43687c478bd9Sstevel@tonic-gate 		}
43697c478bd9Sstevel@tonic-gate 	}
43707c478bd9Sstevel@tonic-gate 
43717c478bd9Sstevel@tonic-gate 	if (!found) {
43727c478bd9Sstevel@tonic-gate 		bam_error(NO_MATCH_ENTRY);
43737c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
43747c478bd9Sstevel@tonic-gate 	}
43757c478bd9Sstevel@tonic-gate 
43767c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
43777c478bd9Sstevel@tonic-gate }
43787c478bd9Sstevel@tonic-gate 
4379843e1988Sjohnlev int
43807c478bd9Sstevel@tonic-gate add_boot_entry(menu_t *mp,
43817c478bd9Sstevel@tonic-gate 	char *title,
4382eb2bd662Svikram 	char *findroot,
43837c478bd9Sstevel@tonic-gate 	char *kernel,
4384843e1988Sjohnlev 	char *mod_kernel,
43857c478bd9Sstevel@tonic-gate 	char *module)
43867c478bd9Sstevel@tonic-gate {
4387eb2bd662Svikram 	int		lineNum;
4388eb2bd662Svikram 	int		entryNum;
43897c478bd9Sstevel@tonic-gate 	char		linebuf[BAM_MAXLINE];
4390eb2bd662Svikram 	menu_cmd_t	k_cmd;
4391eb2bd662Svikram 	menu_cmd_t	m_cmd;
4392eb2bd662Svikram 	const char	*fcn = "add_boot_entry()";
43937c478bd9Sstevel@tonic-gate 
43947c478bd9Sstevel@tonic-gate 	assert(mp);
43957c478bd9Sstevel@tonic-gate 
4396eb2bd662Svikram 	INJECT_ERROR1("ADD_BOOT_ENTRY_FINDROOT_NULL", findroot = NULL);
4397eb2bd662Svikram 	if (findroot == NULL) {
4398eb2bd662Svikram 		bam_error(NULL_FINDROOT);
4399eb2bd662Svikram 		return (BAM_ERROR);
4400eb2bd662Svikram 	}
4401eb2bd662Svikram 
44027c478bd9Sstevel@tonic-gate 	if (title == NULL) {
4403ee3b8144Sszhou 		title = "Solaris";	/* default to Solaris */
44047c478bd9Sstevel@tonic-gate 	}
44057c478bd9Sstevel@tonic-gate 	if (kernel == NULL) {
44067c478bd9Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]);
44077c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
44087c478bd9Sstevel@tonic-gate 	}
44097c478bd9Sstevel@tonic-gate 	if (module == NULL) {
4410ae115bc7Smrj 		if (bam_direct != BAM_DIRECT_DBOOT) {
44117c478bd9Sstevel@tonic-gate 			bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]);
44127c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
44137c478bd9Sstevel@tonic-gate 		}
44147c478bd9Sstevel@tonic-gate 
4415ae115bc7Smrj 		/* Figure the commands out from the kernel line */
4416ae115bc7Smrj 		if (strstr(kernel, "$ISADIR") != NULL) {
4417ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE;
4418ae115bc7Smrj 			k_cmd = KERNEL_DOLLAR_CMD;
4419ae115bc7Smrj 			m_cmd = MODULE_DOLLAR_CMD;
4420ae115bc7Smrj 		} else if (strstr(kernel, "amd64") != NULL) {
4421ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE_64;
4422ae115bc7Smrj 			k_cmd = KERNEL_CMD;
4423ae115bc7Smrj 			m_cmd = MODULE_CMD;
4424ae115bc7Smrj 		} else {
4425ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE_32;
4426ae115bc7Smrj 			k_cmd = KERNEL_CMD;
4427ae115bc7Smrj 			m_cmd = MODULE_CMD;
4428ae115bc7Smrj 		}
4429ae115bc7Smrj 	} else if ((bam_direct == BAM_DIRECT_DBOOT) &&
4430ae115bc7Smrj 	    (strstr(kernel, "$ISADIR") != NULL)) {
4431ae115bc7Smrj 		/*
4432ae115bc7Smrj 		 * If it's a non-failsafe dboot kernel, use the "kernel$"
4433ae115bc7Smrj 		 * command.  Otherwise, use "kernel".
4434ae115bc7Smrj 		 */
4435ae115bc7Smrj 		k_cmd = KERNEL_DOLLAR_CMD;
4436ae115bc7Smrj 		m_cmd = MODULE_DOLLAR_CMD;
4437ae115bc7Smrj 	} else {
4438ae115bc7Smrj 		k_cmd = KERNEL_CMD;
4439ae115bc7Smrj 		m_cmd = MODULE_CMD;
4440ae115bc7Smrj 	}
4441ae115bc7Smrj 
44427c478bd9Sstevel@tonic-gate 	if (mp->start) {
44437c478bd9Sstevel@tonic-gate 		lineNum = mp->end->lineNum;
44447c478bd9Sstevel@tonic-gate 		entryNum = mp->end->entryNum;
44457c478bd9Sstevel@tonic-gate 	} else {
44467c478bd9Sstevel@tonic-gate 		lineNum = LINE_INIT;
44477c478bd9Sstevel@tonic-gate 		entryNum = ENTRY_INIT;
44487c478bd9Sstevel@tonic-gate 	}
44497c478bd9Sstevel@tonic-gate 
44507c478bd9Sstevel@tonic-gate 	/*
44517c478bd9Sstevel@tonic-gate 	 * No separator for comment (HDR/FTR) commands
44527c478bd9Sstevel@tonic-gate 	 * The syntax for comments is #<comment>
44537c478bd9Sstevel@tonic-gate 	 */
44547c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
4455ae115bc7Smrj 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_HDR);
44568c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
44577c478bd9Sstevel@tonic-gate 
44587c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
44597c478bd9Sstevel@tonic-gate 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
44608c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
44617c478bd9Sstevel@tonic-gate 
44627c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
4463eb2bd662Svikram 	    menu_cmds[FINDROOT_CMD], menu_cmds[SEP_CMD], findroot);
44648c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
4465eb2bd662Svikram 	BAM_DPRINTF((D_ADD_FINDROOT_NUM, fcn, lineNum, entryNum));
44667c478bd9Sstevel@tonic-gate 
44677c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
4468ae115bc7Smrj 	    menu_cmds[k_cmd], menu_cmds[SEP_CMD], kernel);
44698c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
44707c478bd9Sstevel@tonic-gate 
4471843e1988Sjohnlev 	if (mod_kernel != NULL) {
4472843e1988Sjohnlev 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
4473843e1988Sjohnlev 		    menu_cmds[m_cmd], menu_cmds[SEP_CMD], mod_kernel);
4474843e1988Sjohnlev 		line_parser(mp, linebuf, &lineNum, &entryNum);
4475843e1988Sjohnlev 	}
4476843e1988Sjohnlev 
44777c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
4478ae115bc7Smrj 	    menu_cmds[m_cmd], menu_cmds[SEP_CMD], module);
44798c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
44807c478bd9Sstevel@tonic-gate 
44817c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
4482ae115bc7Smrj 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_FTR);
44838c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
44847c478bd9Sstevel@tonic-gate 
44857c478bd9Sstevel@tonic-gate 	return (entryNum);
44867c478bd9Sstevel@tonic-gate }
44877c478bd9Sstevel@tonic-gate 
44887c478bd9Sstevel@tonic-gate static error_t
44897c478bd9Sstevel@tonic-gate do_delete(menu_t *mp, int entryNum)
44907c478bd9Sstevel@tonic-gate {
4491eb2bd662Svikram 	line_t		*lp;
4492eb2bd662Svikram 	line_t		*freed;
4493eb2bd662Svikram 	entry_t		*ent;
4494eb2bd662Svikram 	entry_t		*tmp;
44957c478bd9Sstevel@tonic-gate 	int		deleted;
4496eb2bd662Svikram 	const char	*fcn = "do_delete()";
44977c478bd9Sstevel@tonic-gate 
44987c478bd9Sstevel@tonic-gate 	assert(entryNum != ENTRY_INIT);
44997c478bd9Sstevel@tonic-gate 
4500eb2bd662Svikram 	tmp = NULL;
4501eb2bd662Svikram 
45028c1b6884Sszhou 	ent = mp->entries;
45038c1b6884Sszhou 	while (ent) {
45048c1b6884Sszhou 		lp = ent->start;
45058c1b6884Sszhou 		/* check entry number and make sure it's a bootadm entry */
45068c1b6884Sszhou 		if (lp->flags != BAM_COMMENT ||
4507ae115bc7Smrj 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0 ||
45088c1b6884Sszhou 		    (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) {
45098c1b6884Sszhou 			ent = ent->next;
45107c478bd9Sstevel@tonic-gate 			continue;
45117c478bd9Sstevel@tonic-gate 		}
45127c478bd9Sstevel@tonic-gate 
45138c1b6884Sszhou 		/* free the entry content */
45148c1b6884Sszhou 		do {
45158c1b6884Sszhou 			freed = lp;
45168c1b6884Sszhou 			lp = lp->next;	/* prev stays the same */
4517eb2bd662Svikram 			BAM_DPRINTF((D_FREEING_LINE, fcn, freed->lineNum));
45188c1b6884Sszhou 			unlink_line(mp, freed);
45198c1b6884Sszhou 			line_free(freed);
45208c1b6884Sszhou 		} while (freed != ent->end);
45217c478bd9Sstevel@tonic-gate 
45228c1b6884Sszhou 		/* free the entry_t structure */
4523eb2bd662Svikram 		assert(tmp == NULL);
45248c1b6884Sszhou 		tmp = ent;
45258c1b6884Sszhou 		ent = ent->next;
45268c1b6884Sszhou 		if (tmp->prev)
45278c1b6884Sszhou 			tmp->prev->next = ent;
45287c478bd9Sstevel@tonic-gate 		else
45298c1b6884Sszhou 			mp->entries = ent;
45308c1b6884Sszhou 		if (ent)
45318c1b6884Sszhou 			ent->prev = tmp->prev;
4532eb2bd662Svikram 		BAM_DPRINTF((D_FREEING_ENTRY, fcn, tmp->entryNum));
4533eb2bd662Svikram 		free(tmp);
4534eb2bd662Svikram 		tmp = NULL;
45357c478bd9Sstevel@tonic-gate 		deleted = 1;
45367c478bd9Sstevel@tonic-gate 	}
45377c478bd9Sstevel@tonic-gate 
4538eb2bd662Svikram 	assert(tmp == NULL);
4539eb2bd662Svikram 
45407c478bd9Sstevel@tonic-gate 	if (!deleted && entryNum != ALL_ENTRIES) {
45417c478bd9Sstevel@tonic-gate 		bam_error(NO_BOOTADM_MATCH);
45427c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
45437c478bd9Sstevel@tonic-gate 	}
45447c478bd9Sstevel@tonic-gate 
454540541d5dSvikram 	/*
454640541d5dSvikram 	 * Now that we have deleted an entry, update
454740541d5dSvikram 	 * the entry numbering and the default cmd.
454840541d5dSvikram 	 */
454940541d5dSvikram 	update_numbering(mp);
455040541d5dSvikram 
45517c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
45527c478bd9Sstevel@tonic-gate }
45537c478bd9Sstevel@tonic-gate 
45547c478bd9Sstevel@tonic-gate static error_t
4555eb2bd662Svikram delete_all_entries(menu_t *mp, char *dummy, char *opt)
45567c478bd9Sstevel@tonic-gate {
45577c478bd9Sstevel@tonic-gate 	assert(mp);
4558eb2bd662Svikram 	assert(dummy == NULL);
45597c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
45607c478bd9Sstevel@tonic-gate 
4561eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY0, "delete_all_entries"));
4562eb2bd662Svikram 
45637c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
4564eb2bd662Svikram 		bam_print(EMPTY_MENU);
45657c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
45667c478bd9Sstevel@tonic-gate 	}
45677c478bd9Sstevel@tonic-gate 
45687c478bd9Sstevel@tonic-gate 	if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) {
45697c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
45707c478bd9Sstevel@tonic-gate 	}
45717c478bd9Sstevel@tonic-gate 
45727c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
45737c478bd9Sstevel@tonic-gate }
45747c478bd9Sstevel@tonic-gate 
45757c478bd9Sstevel@tonic-gate static FILE *
4576eb2bd662Svikram create_diskmap(char *osroot)
45777c478bd9Sstevel@tonic-gate {
45787c478bd9Sstevel@tonic-gate 	FILE *fp;
4579*cedc7e57SEnrico Perla - Sun Microsystems 	char cmd[PATH_MAX + 16];
4580*cedc7e57SEnrico Perla - Sun Microsystems 	char path[PATH_MAX];
4581eb2bd662Svikram 	const char *fcn = "create_diskmap()";
45827c478bd9Sstevel@tonic-gate 
45837c478bd9Sstevel@tonic-gate 	/* make sure we have a map file */
45847c478bd9Sstevel@tonic-gate 	fp = fopen(GRUBDISK_MAP, "r");
45857c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
4586*cedc7e57SEnrico Perla - Sun Microsystems 		int	ret;
4587*cedc7e57SEnrico Perla - Sun Microsystems 
4588*cedc7e57SEnrico Perla - Sun Microsystems 		ret = snprintf(path, sizeof (path), "%s/%s", osroot,
4589*cedc7e57SEnrico Perla - Sun Microsystems 		    CREATE_DISKMAP);
4590*cedc7e57SEnrico Perla - Sun Microsystems 		if (ret >= sizeof (path)) {
4591*cedc7e57SEnrico Perla - Sun Microsystems 			bam_error(PATH_TOO_LONG, osroot);
4592*cedc7e57SEnrico Perla - Sun Microsystems 			return (NULL);
4593*cedc7e57SEnrico Perla - Sun Microsystems 		}
4594*cedc7e57SEnrico Perla - Sun Microsystems 		if (is_safe_exec(path) == BAM_ERROR)
4595*cedc7e57SEnrico Perla - Sun Microsystems 			return (NULL);
4596*cedc7e57SEnrico Perla - Sun Microsystems 
45977c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd),
4598eb2bd662Svikram 		    "%s/%s > /dev/null", osroot, CREATE_DISKMAP);
4599eb2bd662Svikram 		if (exec_cmd(cmd, NULL) != 0)
4600eb2bd662Svikram 			return (NULL);
46017c478bd9Sstevel@tonic-gate 		fp = fopen(GRUBDISK_MAP, "r");
4602eb2bd662Svikram 		INJECT_ERROR1("DISKMAP_CREATE_FAIL", fp = NULL);
4603eb2bd662Svikram 		if (fp) {
4604eb2bd662Svikram 			BAM_DPRINTF((D_CREATED_DISKMAP, fcn, GRUBDISK_MAP));
4605eb2bd662Svikram 		} else {
4606eb2bd662Svikram 			BAM_DPRINTF((D_CREATE_DISKMAP_FAIL, fcn, GRUBDISK_MAP));
4607eb2bd662Svikram 		}
46087c478bd9Sstevel@tonic-gate 	}
46097c478bd9Sstevel@tonic-gate 	return (fp);
46107c478bd9Sstevel@tonic-gate }
46117c478bd9Sstevel@tonic-gate 
46127c478bd9Sstevel@tonic-gate #define	SECTOR_SIZE	512
46137c478bd9Sstevel@tonic-gate 
46147c478bd9Sstevel@tonic-gate static int
46157c478bd9Sstevel@tonic-gate get_partition(char *device)
46167c478bd9Sstevel@tonic-gate {
46177c478bd9Sstevel@tonic-gate 	int i, fd, is_pcfs, partno = -1;
46187c478bd9Sstevel@tonic-gate 	struct mboot *mboot;
46197c478bd9Sstevel@tonic-gate 	char boot_sect[SECTOR_SIZE];
46207c478bd9Sstevel@tonic-gate 	char *wholedisk, *slice;
46217c478bd9Sstevel@tonic-gate 
46227c478bd9Sstevel@tonic-gate 	/* form whole disk (p0) */
46237c478bd9Sstevel@tonic-gate 	slice = device + strlen(device) - 2;
46247c478bd9Sstevel@tonic-gate 	is_pcfs = (*slice != 's');
46257c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
46267c478bd9Sstevel@tonic-gate 		*slice = '\0';
46277c478bd9Sstevel@tonic-gate 	wholedisk = s_calloc(1, strlen(device) + 3);
46287c478bd9Sstevel@tonic-gate 	(void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device);
46297c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
46307c478bd9Sstevel@tonic-gate 		*slice = 's';
46317c478bd9Sstevel@tonic-gate 
46327c478bd9Sstevel@tonic-gate 	/* read boot sector */
46337c478bd9Sstevel@tonic-gate 	fd = open(wholedisk, O_RDONLY);
46347c478bd9Sstevel@tonic-gate 	free(wholedisk);
46357c478bd9Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
46367c478bd9Sstevel@tonic-gate 		return (partno);
46377c478bd9Sstevel@tonic-gate 	}
46387c478bd9Sstevel@tonic-gate 	(void) close(fd);
46397c478bd9Sstevel@tonic-gate 
46407c478bd9Sstevel@tonic-gate 	/* parse fdisk table */
46417c478bd9Sstevel@tonic-gate 	mboot = (struct mboot *)((void *)boot_sect);
46427c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
46437c478bd9Sstevel@tonic-gate 		struct ipart *part =
46447c478bd9Sstevel@tonic-gate 		    (struct ipart *)(uintptr_t)mboot->parts + i;
46457c478bd9Sstevel@tonic-gate 		if (is_pcfs) {	/* looking for solaris boot part */
46467c478bd9Sstevel@tonic-gate 			if (part->systid == 0xbe) {
46477c478bd9Sstevel@tonic-gate 				partno = i;
46487c478bd9Sstevel@tonic-gate 				break;
46497c478bd9Sstevel@tonic-gate 			}
46507c478bd9Sstevel@tonic-gate 		} else {	/* look for solaris partition, old and new */
46517c478bd9Sstevel@tonic-gate 			if (part->systid == SUNIXOS ||
46527c478bd9Sstevel@tonic-gate 			    part->systid == SUNIXOS2) {
46537c478bd9Sstevel@tonic-gate 				partno = i;
46547c478bd9Sstevel@tonic-gate 				break;
46557c478bd9Sstevel@tonic-gate 			}
46567c478bd9Sstevel@tonic-gate 		}
46577c478bd9Sstevel@tonic-gate 	}
46587c478bd9Sstevel@tonic-gate 	return (partno);
46597c478bd9Sstevel@tonic-gate }
46607c478bd9Sstevel@tonic-gate 
4661eb2bd662Svikram char *
4662eb2bd662Svikram get_grubroot(char *osroot, char *osdev, char *menu_root)
46637c478bd9Sstevel@tonic-gate {
4664eb2bd662Svikram 	char		*grubroot;	/* (hd#,#,#) */
46657c478bd9Sstevel@tonic-gate 	char		*slice;
46667c478bd9Sstevel@tonic-gate 	char		*grubhd;
46677c478bd9Sstevel@tonic-gate 	int		fdiskpart;
46687c478bd9Sstevel@tonic-gate 	int		found = 0;
4669eb2bd662Svikram 	char		*devname;
4670eb2bd662Svikram 	char		*ctdname = strstr(osdev, "dsk/");
46717c478bd9Sstevel@tonic-gate 	char		linebuf[PATH_MAX];
4672eb2bd662Svikram 	FILE		*fp;
46737c478bd9Sstevel@tonic-gate 
4674eb2bd662Svikram 	INJECT_ERROR1("GRUBROOT_INVALID_OSDEV", ctdname = NULL);
4675eb2bd662Svikram 	if (ctdname == NULL) {
4676eb2bd662Svikram 		bam_error(INVALID_DEV_DSK, osdev);
46777c478bd9Sstevel@tonic-gate 		return (NULL);
4678eb2bd662Svikram 	}
4679eb2bd662Svikram 
4680eb2bd662Svikram 	if (menu_root && !menu_on_bootdisk(osroot, menu_root)) {
4681eb2bd662Svikram 		/* menu bears no resemblance to our reality */
468237eb779cSVikram Hegde 		bam_error(CANNOT_GRUBROOT_BOOTDISK, osdev);
4683eb2bd662Svikram 		return (NULL);
4684eb2bd662Svikram 	}
46857c478bd9Sstevel@tonic-gate 
46867c478bd9Sstevel@tonic-gate 	ctdname += strlen("dsk/");
46877c478bd9Sstevel@tonic-gate 	slice = strrchr(ctdname, 's');
46887c478bd9Sstevel@tonic-gate 	if (slice)
46897c478bd9Sstevel@tonic-gate 		*slice = '\0';
46907c478bd9Sstevel@tonic-gate 
4691eb2bd662Svikram 	fp = create_diskmap(osroot);
4692eb2bd662Svikram 	if (fp == NULL) {
4693eb2bd662Svikram 		bam_error(DISKMAP_FAIL, osroot);
4694eb2bd662Svikram 		return (NULL);
4695eb2bd662Svikram 	}
4696eb2bd662Svikram 
46977c478bd9Sstevel@tonic-gate 	rewind(fp);
46987c478bd9Sstevel@tonic-gate 	while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) {
46997c478bd9Sstevel@tonic-gate 		grubhd = strtok(linebuf, " \t\n");
47007c478bd9Sstevel@tonic-gate 		if (grubhd)
47017c478bd9Sstevel@tonic-gate 			devname = strtok(NULL, " \t\n");
47027c478bd9Sstevel@tonic-gate 		else
47037c478bd9Sstevel@tonic-gate 			devname = NULL;
47047c478bd9Sstevel@tonic-gate 		if (devname && strcmp(devname, ctdname) == 0) {
47057c478bd9Sstevel@tonic-gate 			found = 1;
47067c478bd9Sstevel@tonic-gate 			break;
47077c478bd9Sstevel@tonic-gate 		}
47087c478bd9Sstevel@tonic-gate 	}
47097c478bd9Sstevel@tonic-gate 
47107c478bd9Sstevel@tonic-gate 	if (slice)
47117c478bd9Sstevel@tonic-gate 		*slice = 's';
47127c478bd9Sstevel@tonic-gate 
4713eb2bd662Svikram 	(void) fclose(fp);
4714eb2bd662Svikram 	fp = NULL;
4715eb2bd662Svikram 
4716eb2bd662Svikram 	INJECT_ERROR1("GRUBROOT_BIOSDEV_FAIL", found = 0);
47177c478bd9Sstevel@tonic-gate 	if (found == 0) {
471837eb779cSVikram Hegde 		bam_error(BIOSDEV_SKIP, osdev);
4719eb2bd662Svikram 		return (NULL);
47207c478bd9Sstevel@tonic-gate 	}
47217c478bd9Sstevel@tonic-gate 
4722eb2bd662Svikram 	fdiskpart = get_partition(osdev);
4723eb2bd662Svikram 	INJECT_ERROR1("GRUBROOT_FDISK_FAIL", fdiskpart = -1);
4724eb2bd662Svikram 	if (fdiskpart == -1) {
4725eb2bd662Svikram 		bam_error(FDISKPART_FAIL, osdev);
47267c478bd9Sstevel@tonic-gate 		return (NULL);
4727eb2bd662Svikram 	}
47287c478bd9Sstevel@tonic-gate 
4729eb2bd662Svikram 	grubroot = s_calloc(1, 10);
47307c478bd9Sstevel@tonic-gate 	if (slice) {
4731eb2bd662Svikram 		(void) snprintf(grubroot, 10, "(hd%s,%d,%c)",
47327c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart, slice[1] + 'a' - '0');
47337c478bd9Sstevel@tonic-gate 	} else
4734eb2bd662Svikram 		(void) snprintf(grubroot, 10, "(hd%s,%d)",
47357c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart);
47367c478bd9Sstevel@tonic-gate 
4737eb2bd662Svikram 	assert(fp == NULL);
4738eb2bd662Svikram 	assert(strncmp(grubroot, "(hd", strlen("(hd")) == 0);
4739eb2bd662Svikram 	return (grubroot);
4740eb2bd662Svikram }
4741eb2bd662Svikram 
4742eb2bd662Svikram static char *
4743eb2bd662Svikram find_primary_common(char *mntpt, char *fstype)
4744eb2bd662Svikram {
4745eb2bd662Svikram 	char		signdir[PATH_MAX];
4746eb2bd662Svikram 	char		tmpsign[MAXNAMELEN + 1];
4747eb2bd662Svikram 	char		*lu;
4748eb2bd662Svikram 	char		*ufs;
4749eb2bd662Svikram 	char		*zfs;
4750eb2bd662Svikram 	DIR		*dirp = NULL;
4751eb2bd662Svikram 	struct dirent	*entp;
4752eb2bd662Svikram 	struct stat	sb;
4753eb2bd662Svikram 	const char	*fcn = "find_primary_common()";
4754eb2bd662Svikram 
4755eb2bd662Svikram 	(void) snprintf(signdir, sizeof (signdir), "%s/%s",
4756eb2bd662Svikram 	    mntpt, GRUBSIGN_DIR);
4757eb2bd662Svikram 
4758eb2bd662Svikram 	if (stat(signdir, &sb) == -1) {
4759eb2bd662Svikram 		BAM_DPRINTF((D_NO_SIGNDIR, fcn, signdir));
4760eb2bd662Svikram 		return (NULL);
4761eb2bd662Svikram 	}
4762eb2bd662Svikram 
4763eb2bd662Svikram 	dirp = opendir(signdir);
4764eb2bd662Svikram 	INJECT_ERROR1("SIGNDIR_OPENDIR_FAIL", dirp = NULL);
4765eb2bd662Svikram 	if (dirp == NULL) {
4766eb2bd662Svikram 		bam_error(OPENDIR_FAILED, signdir, strerror(errno));
4767eb2bd662Svikram 		return (NULL);
4768eb2bd662Svikram 	}
4769eb2bd662Svikram 
4770eb2bd662Svikram 	ufs = zfs = lu = NULL;
4771eb2bd662Svikram 
4772eb2bd662Svikram 	while (entp = readdir(dirp)) {
4773eb2bd662Svikram 		if (strcmp(entp->d_name, ".") == 0 ||
4774eb2bd662Svikram 		    strcmp(entp->d_name, "..") == 0)
4775eb2bd662Svikram 			continue;
4776eb2bd662Svikram 
4777eb2bd662Svikram 		(void) snprintf(tmpsign, sizeof (tmpsign), "%s", entp->d_name);
4778eb2bd662Svikram 
4779eb2bd662Svikram 		if (lu == NULL &&
4780eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_LU_PREFIX,
4781eb2bd662Svikram 		    strlen(GRUBSIGN_LU_PREFIX)) == 0) {
4782eb2bd662Svikram 			lu = s_strdup(tmpsign);
4783eb2bd662Svikram 		}
4784eb2bd662Svikram 
4785eb2bd662Svikram 		if (ufs == NULL &&
4786eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
4787eb2bd662Svikram 		    strlen(GRUBSIGN_UFS_PREFIX)) == 0) {
4788eb2bd662Svikram 			ufs = s_strdup(tmpsign);
4789eb2bd662Svikram 		}
4790eb2bd662Svikram 
4791eb2bd662Svikram 		if (zfs == NULL &&
4792eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_ZFS_PREFIX,
4793eb2bd662Svikram 		    strlen(GRUBSIGN_ZFS_PREFIX)) == 0) {
4794eb2bd662Svikram 			zfs = s_strdup(tmpsign);
4795eb2bd662Svikram 		}
4796eb2bd662Svikram 	}
4797eb2bd662Svikram 
4798eb2bd662Svikram 	BAM_DPRINTF((D_EXIST_PRIMARY_SIGNS, fcn,
4799eb2bd662Svikram 	    zfs ? zfs : "NULL",
4800eb2bd662Svikram 	    ufs ? ufs : "NULL",
4801eb2bd662Svikram 	    lu ? lu : "NULL"));
4802eb2bd662Svikram 
4803eb2bd662Svikram 	if (dirp) {
4804eb2bd662Svikram 		(void) closedir(dirp);
4805eb2bd662Svikram 		dirp = NULL;
4806eb2bd662Svikram 	}
4807eb2bd662Svikram 
4808eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0 && zfs) {
4809eb2bd662Svikram 		bam_error(SIGN_FSTYPE_MISMATCH, zfs, "ufs");
4810eb2bd662Svikram 		free(zfs);
4811eb2bd662Svikram 		zfs = NULL;
4812eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0 && ufs) {
4813eb2bd662Svikram 		bam_error(SIGN_FSTYPE_MISMATCH, ufs, "zfs");
4814eb2bd662Svikram 		free(ufs);
4815eb2bd662Svikram 		ufs = NULL;
4816eb2bd662Svikram 	}
4817eb2bd662Svikram 
4818eb2bd662Svikram 	assert(dirp == NULL);
4819eb2bd662Svikram 
4820eb2bd662Svikram 	/* For now, we let Live Upgrade take care of its signature itself */
4821eb2bd662Svikram 	if (lu) {
4822eb2bd662Svikram 		BAM_DPRINTF((D_FREEING_LU_SIGNS, fcn, lu));
4823eb2bd662Svikram 		free(lu);
4824eb2bd662Svikram 		lu = NULL;
4825eb2bd662Svikram 	}
4826eb2bd662Svikram 
4827eb2bd662Svikram 	return (zfs ? zfs : ufs);
4828eb2bd662Svikram }
4829eb2bd662Svikram 
4830eb2bd662Svikram static char *
4831eb2bd662Svikram find_backup_common(char *mntpt, char *fstype)
4832eb2bd662Svikram {
4833eb2bd662Svikram 	FILE		*bfp = NULL;
4834eb2bd662Svikram 	char		tmpsign[MAXNAMELEN + 1];
4835eb2bd662Svikram 	char		backup[PATH_MAX];
4836eb2bd662Svikram 	char		*ufs;
4837eb2bd662Svikram 	char		*zfs;
4838eb2bd662Svikram 	char		*lu;
4839eb2bd662Svikram 	int		error;
4840eb2bd662Svikram 	const char	*fcn = "find_backup_common()";
4841eb2bd662Svikram 
4842eb2bd662Svikram 	/*
4843eb2bd662Svikram 	 * We didn't find it in the primary directory.
4844eb2bd662Svikram 	 * Look at the backup
4845eb2bd662Svikram 	 */
4846eb2bd662Svikram 	(void) snprintf(backup, sizeof (backup), "%s%s",
4847eb2bd662Svikram 	    mntpt, GRUBSIGN_BACKUP);
4848eb2bd662Svikram 
4849eb2bd662Svikram 	bfp = fopen(backup, "r");
4850eb2bd662Svikram 	if (bfp == NULL) {
4851eb2bd662Svikram 		error = errno;
4852eb2bd662Svikram 		if (bam_verbose) {
4853eb2bd662Svikram 			bam_error(OPEN_FAIL, backup, strerror(error));
4854eb2bd662Svikram 		}
4855eb2bd662Svikram 		BAM_DPRINTF((D_OPEN_FAIL, fcn, backup, strerror(error)));
4856eb2bd662Svikram 		return (NULL);
4857eb2bd662Svikram 	}
4858eb2bd662Svikram 
4859eb2bd662Svikram 	ufs = zfs = lu = NULL;
4860eb2bd662Svikram 
4861eb2bd662Svikram 	while (s_fgets(tmpsign, sizeof (tmpsign), bfp) != NULL) {
4862eb2bd662Svikram 
4863eb2bd662Svikram 		if (lu == NULL &&
4864eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_LU_PREFIX,
4865eb2bd662Svikram 		    strlen(GRUBSIGN_LU_PREFIX)) == 0) {
4866eb2bd662Svikram 			lu = s_strdup(tmpsign);
4867eb2bd662Svikram 		}
4868eb2bd662Svikram 
4869eb2bd662Svikram 		if (ufs == NULL &&
4870eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
4871eb2bd662Svikram 		    strlen(GRUBSIGN_UFS_PREFIX)) == 0) {
4872eb2bd662Svikram 			ufs = s_strdup(tmpsign);
4873eb2bd662Svikram 		}
4874eb2bd662Svikram 
4875eb2bd662Svikram 		if (zfs == NULL &&
4876eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_ZFS_PREFIX,
4877eb2bd662Svikram 		    strlen(GRUBSIGN_ZFS_PREFIX)) == 0) {
4878eb2bd662Svikram 			zfs = s_strdup(tmpsign);
4879eb2bd662Svikram 		}
4880eb2bd662Svikram 	}
4881eb2bd662Svikram 
4882eb2bd662Svikram 	BAM_DPRINTF((D_EXIST_BACKUP_SIGNS, fcn,
4883eb2bd662Svikram 	    zfs ? zfs : "NULL",
4884eb2bd662Svikram 	    ufs ? ufs : "NULL",
4885eb2bd662Svikram 	    lu ? lu : "NULL"));
4886eb2bd662Svikram 
4887eb2bd662Svikram 	if (bfp) {
4888eb2bd662Svikram 		(void) fclose(bfp);
4889eb2bd662Svikram 		bfp = NULL;
4890eb2bd662Svikram 	}
4891eb2bd662Svikram 
4892eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0 && zfs) {
4893eb2bd662Svikram 		bam_error(SIGN_FSTYPE_MISMATCH, zfs, "ufs");
4894eb2bd662Svikram 		free(zfs);
4895eb2bd662Svikram 		zfs = NULL;
4896eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0 && ufs) {
4897eb2bd662Svikram 		bam_error(SIGN_FSTYPE_MISMATCH, ufs, "zfs");
4898eb2bd662Svikram 		free(ufs);
4899eb2bd662Svikram 		ufs = NULL;
4900eb2bd662Svikram 	}
4901eb2bd662Svikram 
4902eb2bd662Svikram 	assert(bfp == NULL);
4903eb2bd662Svikram 
4904eb2bd662Svikram 	/* For now, we let Live Upgrade take care of its signature itself */
4905eb2bd662Svikram 	if (lu) {
4906eb2bd662Svikram 		BAM_DPRINTF((D_FREEING_LU_SIGNS, fcn, lu));
4907eb2bd662Svikram 		free(lu);
4908eb2bd662Svikram 		lu = NULL;
4909eb2bd662Svikram 	}
4910eb2bd662Svikram 
4911eb2bd662Svikram 	return (zfs ? zfs : ufs);
4912eb2bd662Svikram }
4913eb2bd662Svikram 
4914eb2bd662Svikram static char *
4915eb2bd662Svikram find_ufs_existing(char *osroot)
4916eb2bd662Svikram {
4917eb2bd662Svikram 	char		*sign;
4918eb2bd662Svikram 	const char	*fcn = "find_ufs_existing()";
4919eb2bd662Svikram 
4920eb2bd662Svikram 	sign = find_primary_common(osroot, "ufs");
4921eb2bd662Svikram 	if (sign == NULL) {
4922eb2bd662Svikram 		sign = find_backup_common(osroot, "ufs");
4923eb2bd662Svikram 		BAM_DPRINTF((D_EXIST_BACKUP_SIGN, fcn, sign ? sign : "NULL"));
4924eb2bd662Svikram 	} else {
4925eb2bd662Svikram 		BAM_DPRINTF((D_EXIST_PRIMARY_SIGN, fcn, sign));
4926eb2bd662Svikram 	}
4927eb2bd662Svikram 
4928eb2bd662Svikram 	return (sign);
4929eb2bd662Svikram }
4930eb2bd662Svikram 
4931eb2bd662Svikram char *
4932eb2bd662Svikram get_mountpoint(char *special, char *fstype)
4933eb2bd662Svikram {
4934eb2bd662Svikram 	FILE		*mntfp;
4935eb2bd662Svikram 	struct mnttab	mp = {0};
4936eb2bd662Svikram 	struct mnttab	mpref = {0};
4937eb2bd662Svikram 	int		error;
4938eb2bd662Svikram 	int		ret;
4939eb2bd662Svikram 	const char	*fcn = "get_mountpoint()";
4940eb2bd662Svikram 
4941eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, special, fstype));
4942eb2bd662Svikram 
4943eb2bd662Svikram 	mntfp = fopen(MNTTAB, "r");
4944eb2bd662Svikram 	error = errno;
4945eb2bd662Svikram 	INJECT_ERROR1("MNTTAB_ERR_GET_MNTPT", mntfp = NULL);
4946eb2bd662Svikram 	if (mntfp == NULL) {
4947eb2bd662Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
4948eb2bd662Svikram 		return (NULL);
4949eb2bd662Svikram 	}
4950eb2bd662Svikram 
4951eb2bd662Svikram 	mpref.mnt_special = special;
4952eb2bd662Svikram 	mpref.mnt_fstype = fstype;
4953eb2bd662Svikram 
4954eb2bd662Svikram 	ret = getmntany(mntfp, &mp, &mpref);
4955eb2bd662Svikram 	INJECT_ERROR1("GET_MOUNTPOINT_MNTANY", ret = 1);
4956eb2bd662Svikram 	if (ret != 0) {
4957eb2bd662Svikram 		(void) fclose(mntfp);
4958eb2bd662Svikram 		BAM_DPRINTF((D_NO_MNTPT, fcn, special, fstype));
4959eb2bd662Svikram 		return (NULL);
4960eb2bd662Svikram 	}
4961eb2bd662Svikram 	(void) fclose(mntfp);
4962eb2bd662Svikram 
4963eb2bd662Svikram 	assert(mp.mnt_mountp);
4964eb2bd662Svikram 
4965eb2bd662Svikram 	BAM_DPRINTF((D_GET_MOUNTPOINT_RET, fcn, special, mp.mnt_mountp));
4966eb2bd662Svikram 
4967eb2bd662Svikram 	return (s_strdup(mp.mnt_mountp));
4968eb2bd662Svikram }
4969eb2bd662Svikram 
4970eb2bd662Svikram /*
4971eb2bd662Svikram  * Mounts a "legacy" top dataset (if needed)
4972eb2bd662Svikram  * Returns:	The mountpoint of the legacy top dataset or NULL on error
4973eb2bd662Svikram  * 		mnted returns one of the above values defined for zfs_mnted_t
4974eb2bd662Svikram  */
4975eb2bd662Svikram static char *
4976eb2bd662Svikram mount_legacy_dataset(char *pool, zfs_mnted_t *mnted)
4977eb2bd662Svikram {
4978eb2bd662Svikram 	char		cmd[PATH_MAX];
4979eb2bd662Svikram 	char		tmpmnt[PATH_MAX];
4980eb2bd662Svikram 	filelist_t	flist = {0};
4981eb2bd662Svikram 	char		*is_mounted;
4982eb2bd662Svikram 	struct stat	sb;
4983eb2bd662Svikram 	int		ret;
4984eb2bd662Svikram 	const char	*fcn = "mount_legacy_dataset()";
4985eb2bd662Svikram 
4986eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, pool));
4987eb2bd662Svikram 
4988eb2bd662Svikram 	*mnted = ZFS_MNT_ERROR;
4989eb2bd662Svikram 
4990eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
4991eb2bd662Svikram 	    "/sbin/zfs get -Ho value mounted %s",
4992eb2bd662Svikram 	    pool);
4993eb2bd662Svikram 
4994eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
4995eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_CMD", ret = 1);
4996eb2bd662Svikram 	if (ret != 0) {
4997eb2bd662Svikram 		bam_error(ZFS_MNTED_FAILED, pool);
4998eb2bd662Svikram 		return (NULL);
4999eb2bd662Svikram 	}
5000eb2bd662Svikram 
5001eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_OUT", flist.head = NULL);
5002eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5003eb2bd662Svikram 		bam_error(BAD_ZFS_MNTED, pool);
5004eb2bd662Svikram 		filelist_free(&flist);
5005eb2bd662Svikram 		return (NULL);
5006eb2bd662Svikram 	}
5007eb2bd662Svikram 
5008eb2bd662Svikram 	is_mounted = strtok(flist.head->line, " \t\n");
5009eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_STRTOK_YES", is_mounted = "yes");
5010eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_STRTOK_NO", is_mounted = "no");
5011eb2bd662Svikram 	if (strcmp(is_mounted, "no") != 0) {
5012eb2bd662Svikram 		filelist_free(&flist);
5013eb2bd662Svikram 		*mnted = LEGACY_ALREADY;
5014eb2bd662Svikram 		/* get_mountpoint returns a strdup'ed string */
5015eb2bd662Svikram 		BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_ALREADY, fcn, pool));
5016eb2bd662Svikram 		return (get_mountpoint(pool, "zfs"));
5017eb2bd662Svikram 	}
5018eb2bd662Svikram 
5019eb2bd662Svikram 	filelist_free(&flist);
5020eb2bd662Svikram 
5021eb2bd662Svikram 	/*
5022eb2bd662Svikram 	 * legacy top dataset is not mounted. Mount it now
5023eb2bd662Svikram 	 * First create a mountpoint.
5024eb2bd662Svikram 	 */
5025eb2bd662Svikram 	(void) snprintf(tmpmnt, sizeof (tmpmnt), "%s.%d",
5026eb2bd662Svikram 	    ZFS_LEGACY_MNTPT, getpid());
5027eb2bd662Svikram 
5028eb2bd662Svikram 	ret = stat(tmpmnt, &sb);
5029eb2bd662Svikram 	if (ret == -1) {
5030eb2bd662Svikram 		BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_MNTPT_ABS, fcn, pool, tmpmnt));
503148847494SEnrico Perla - Sun Microsystems 		ret = mkdirp(tmpmnt, DIR_PERMS);
5032eb2bd662Svikram 		INJECT_ERROR1("Z_MOUNT_TOP_LEG_MNTPT_MKDIRP", ret = -1);
5033eb2bd662Svikram 		if (ret == -1) {
5034eb2bd662Svikram 			bam_error(MKDIR_FAILED, tmpmnt, strerror(errno));
5035eb2bd662Svikram 			return (NULL);
5036eb2bd662Svikram 		}
5037eb2bd662Svikram 	} else {
5038eb2bd662Svikram 		BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_MNTPT_PRES, fcn, pool, tmpmnt));
5039eb2bd662Svikram 	}
5040eb2bd662Svikram 
5041eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5042eb2bd662Svikram 	    "/sbin/mount -F zfs %s %s",
5043eb2bd662Svikram 	    pool, tmpmnt);
5044eb2bd662Svikram 
5045eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
5046eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_LEG_MOUNT_CMD", ret = 1);
5047eb2bd662Svikram 	if (ret != 0) {
5048eb2bd662Svikram 		bam_error(ZFS_MOUNT_FAILED, pool);
5049eb2bd662Svikram 		(void) rmdir(tmpmnt);
5050eb2bd662Svikram 		return (NULL);
5051eb2bd662Svikram 	}
5052eb2bd662Svikram 
5053eb2bd662Svikram 	*mnted = LEGACY_MOUNTED;
5054eb2bd662Svikram 	BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_MOUNTED, fcn, pool, tmpmnt));
5055eb2bd662Svikram 	return (s_strdup(tmpmnt));
5056eb2bd662Svikram }
5057eb2bd662Svikram 
5058eb2bd662Svikram /*
5059eb2bd662Svikram  * Mounts the top dataset (if needed)
5060eb2bd662Svikram  * Returns:	The mountpoint of the top dataset or NULL on error
5061eb2bd662Svikram  * 		mnted returns one of the above values defined for zfs_mnted_t
5062eb2bd662Svikram  */
5063eb2bd662Svikram static char *
5064eb2bd662Svikram mount_top_dataset(char *pool, zfs_mnted_t *mnted)
5065eb2bd662Svikram {
5066eb2bd662Svikram 	char		cmd[PATH_MAX];
5067eb2bd662Svikram 	filelist_t	flist = {0};
5068eb2bd662Svikram 	char		*is_mounted;
5069eb2bd662Svikram 	char		*mntpt;
5070eb2bd662Svikram 	char		*zmntpt;
5071eb2bd662Svikram 	int		ret;
5072eb2bd662Svikram 	const char	*fcn = "mount_top_dataset()";
5073eb2bd662Svikram 
5074eb2bd662Svikram 	*mnted = ZFS_MNT_ERROR;
5075eb2bd662Svikram 
5076eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, pool));
5077eb2bd662Svikram 
5078eb2bd662Svikram 	/*
5079eb2bd662Svikram 	 * First check if the top dataset is a "legacy" dataset
5080eb2bd662Svikram 	 */
5081eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5082eb2bd662Svikram 	    "/sbin/zfs get -Ho value mountpoint %s",
5083eb2bd662Svikram 	    pool);
5084eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5085eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_GET_MNTPT", ret = 1);
5086eb2bd662Svikram 	if (ret != 0) {
5087eb2bd662Svikram 		bam_error(ZFS_MNTPT_FAILED, pool);
5088eb2bd662Svikram 		return (NULL);
5089eb2bd662Svikram 	}
5090eb2bd662Svikram 
5091eb2bd662Svikram 	if (flist.head && (flist.head == flist.tail)) {
5092eb2bd662Svikram 		char *legacy = strtok(flist.head->line, " \t\n");
5093eb2bd662Svikram 		if (legacy && strcmp(legacy, "legacy") == 0) {
5094eb2bd662Svikram 			filelist_free(&flist);
5095eb2bd662Svikram 			BAM_DPRINTF((D_Z_IS_LEGACY, fcn, pool));
5096eb2bd662Svikram 			return (mount_legacy_dataset(pool, mnted));
5097eb2bd662Svikram 		}
5098eb2bd662Svikram 	}
5099eb2bd662Svikram 
5100eb2bd662Svikram 	filelist_free(&flist);
5101eb2bd662Svikram 
5102eb2bd662Svikram 	BAM_DPRINTF((D_Z_IS_NOT_LEGACY, fcn, pool));
5103eb2bd662Svikram 
5104eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5105eb2bd662Svikram 	    "/sbin/zfs get -Ho value mounted %s",
5106eb2bd662Svikram 	    pool);
5107eb2bd662Svikram 
5108eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5109eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED", ret = 1);
5110eb2bd662Svikram 	if (ret != 0) {
5111eb2bd662Svikram 		bam_error(ZFS_MNTED_FAILED, pool);
5112eb2bd662Svikram 		return (NULL);
5113eb2bd662Svikram 	}
5114eb2bd662Svikram 
5115eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_VAL", flist.head = NULL);
5116eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5117eb2bd662Svikram 		bam_error(BAD_ZFS_MNTED, pool);
5118eb2bd662Svikram 		filelist_free(&flist);
5119eb2bd662Svikram 		return (NULL);
5120eb2bd662Svikram 	}
5121eb2bd662Svikram 
5122eb2bd662Svikram 	is_mounted = strtok(flist.head->line, " \t\n");
5123eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_YES", is_mounted = "yes");
5124eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_NO", is_mounted = "no");
5125eb2bd662Svikram 	if (strcmp(is_mounted, "no") != 0) {
5126eb2bd662Svikram 		filelist_free(&flist);
5127eb2bd662Svikram 		*mnted = ZFS_ALREADY;
5128eb2bd662Svikram 		BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MOUNTED_ALREADY, fcn, pool));
5129eb2bd662Svikram 		goto mounted;
5130eb2bd662Svikram 	}
5131eb2bd662Svikram 
5132eb2bd662Svikram 	filelist_free(&flist);
5133eb2bd662Svikram 	BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MOUNTED_NOT_ALREADY, fcn, pool));
5134eb2bd662Svikram 
5135eb2bd662Svikram 	/* top dataset is not mounted. Mount it now */
5136eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5137eb2bd662Svikram 	    "/sbin/zfs mount %s", pool);
5138eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
5139eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_MOUNT_CMD", ret = 1);
5140eb2bd662Svikram 	if (ret != 0) {
5141eb2bd662Svikram 		bam_error(ZFS_MOUNT_FAILED, pool);
5142eb2bd662Svikram 		return (NULL);
5143eb2bd662Svikram 	}
5144eb2bd662Svikram 	*mnted = ZFS_MOUNTED;
5145eb2bd662Svikram 	BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MOUNTED_NOW, fcn, pool));
5146eb2bd662Svikram 	/*FALLTHRU*/
5147eb2bd662Svikram mounted:
5148eb2bd662Svikram 	/*
5149eb2bd662Svikram 	 * Now get the mountpoint
5150eb2bd662Svikram 	 */
5151eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5152eb2bd662Svikram 	    "/sbin/zfs get -Ho value mountpoint %s",
5153eb2bd662Svikram 	    pool);
5154eb2bd662Svikram 
5155eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5156eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_CMD", ret = 1);
5157eb2bd662Svikram 	if (ret != 0) {
5158eb2bd662Svikram 		bam_error(ZFS_MNTPT_FAILED, pool);
5159eb2bd662Svikram 		goto error;
5160eb2bd662Svikram 	}
5161eb2bd662Svikram 
5162eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_OUT", flist.head = NULL);
5163eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5164eb2bd662Svikram 		bam_error(NULL_ZFS_MNTPT, pool);
5165eb2bd662Svikram 		goto error;
5166eb2bd662Svikram 	}
5167eb2bd662Svikram 
5168eb2bd662Svikram 	mntpt = strtok(flist.head->line, " \t\n");
5169eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_STRTOK", mntpt = "foo");
5170eb2bd662Svikram 	if (*mntpt != '/') {
5171eb2bd662Svikram 		bam_error(BAD_ZFS_MNTPT, pool, mntpt);
5172eb2bd662Svikram 		goto error;
5173eb2bd662Svikram 	}
5174eb2bd662Svikram 	zmntpt = s_strdup(mntpt);
5175eb2bd662Svikram 
5176eb2bd662Svikram 	filelist_free(&flist);
5177eb2bd662Svikram 
5178eb2bd662Svikram 	BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MNTPT, fcn, pool, zmntpt));
5179eb2bd662Svikram 
5180eb2bd662Svikram 	return (zmntpt);
5181eb2bd662Svikram 
5182eb2bd662Svikram error:
5183eb2bd662Svikram 	filelist_free(&flist);
5184eb2bd662Svikram 	(void) umount_top_dataset(pool, *mnted, NULL);
5185eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_FAILURE, fcn));
5186eb2bd662Svikram 	return (NULL);
5187eb2bd662Svikram }
5188eb2bd662Svikram 
5189eb2bd662Svikram static int
5190eb2bd662Svikram umount_top_dataset(char *pool, zfs_mnted_t mnted, char *mntpt)
5191eb2bd662Svikram {
5192eb2bd662Svikram 	char		cmd[PATH_MAX];
5193eb2bd662Svikram 	int		ret;
5194eb2bd662Svikram 	const char	*fcn = "umount_top_dataset()";
5195eb2bd662Svikram 
5196eb2bd662Svikram 	INJECT_ERROR1("Z_UMOUNT_TOP_INVALID_STATE", mnted = ZFS_MNT_ERROR);
5197eb2bd662Svikram 	switch (mnted) {
5198eb2bd662Svikram 	case LEGACY_ALREADY:
5199eb2bd662Svikram 	case ZFS_ALREADY:
5200eb2bd662Svikram 		/* nothing to do */
5201eb2bd662Svikram 		BAM_DPRINTF((D_Z_UMOUNT_TOP_ALREADY_NOP, fcn, pool,
5202eb2bd662Svikram 		    mntpt ? mntpt : "NULL"));
5203eb2bd662Svikram 		free(mntpt);
5204eb2bd662Svikram 		return (BAM_SUCCESS);
5205eb2bd662Svikram 	case LEGACY_MOUNTED:
5206eb2bd662Svikram 		(void) snprintf(cmd, sizeof (cmd),
5207eb2bd662Svikram 		    "/sbin/umount %s", pool);
5208eb2bd662Svikram 		ret = exec_cmd(cmd, NULL);
5209eb2bd662Svikram 		INJECT_ERROR1("Z_UMOUNT_TOP_LEGACY_UMOUNT_FAIL", ret = 1);
5210eb2bd662Svikram 		if (ret != 0) {
5211eb2bd662Svikram 			bam_error(UMOUNT_FAILED, pool);
5212eb2bd662Svikram 			free(mntpt);
5213eb2bd662Svikram 			return (BAM_ERROR);
5214eb2bd662Svikram 		}
5215eb2bd662Svikram 		if (mntpt)
5216eb2bd662Svikram 			(void) rmdir(mntpt);
5217eb2bd662Svikram 		free(mntpt);
5218eb2bd662Svikram 		BAM_DPRINTF((D_Z_UMOUNT_TOP_LEGACY, fcn, pool));
5219eb2bd662Svikram 		return (BAM_SUCCESS);
5220eb2bd662Svikram 	case ZFS_MOUNTED:
5221eb2bd662Svikram 		free(mntpt);
5222eb2bd662Svikram 		(void) snprintf(cmd, sizeof (cmd),
5223eb2bd662Svikram 		    "/sbin/zfs unmount %s", pool);
5224eb2bd662Svikram 		ret = exec_cmd(cmd, NULL);
5225eb2bd662Svikram 		INJECT_ERROR1("Z_UMOUNT_TOP_NONLEG_UMOUNT_FAIL", ret = 1);
5226eb2bd662Svikram 		if (ret != 0) {
5227eb2bd662Svikram 			bam_error(UMOUNT_FAILED, pool);
5228eb2bd662Svikram 			return (BAM_ERROR);
5229eb2bd662Svikram 		}
5230eb2bd662Svikram 		BAM_DPRINTF((D_Z_UMOUNT_TOP_NONLEG, fcn, pool));
5231eb2bd662Svikram 		return (BAM_SUCCESS);
5232eb2bd662Svikram 	default:
5233eb2bd662Svikram 		bam_error(INT_BAD_MNTSTATE, pool);
5234eb2bd662Svikram 		return (BAM_ERROR);
5235eb2bd662Svikram 	}
5236eb2bd662Svikram 	/*NOTREACHED*/
5237eb2bd662Svikram }
5238eb2bd662Svikram 
5239eb2bd662Svikram /*
5240eb2bd662Svikram  * For ZFS, osdev can be one of two forms
5241eb2bd662Svikram  * It can be a "special" file as seen in mnttab: rpool/ROOT/szboot_0402
5242eb2bd662Svikram  * It can be a /dev/[r]dsk special file. We handle both instances
5243eb2bd662Svikram  */
5244eb2bd662Svikram static char *
5245eb2bd662Svikram get_pool(char *osdev)
5246eb2bd662Svikram {
5247eb2bd662Svikram 	char		cmd[PATH_MAX];
5248eb2bd662Svikram 	char		buf[PATH_MAX];
5249eb2bd662Svikram 	filelist_t	flist = {0};
5250eb2bd662Svikram 	char		*pool;
5251eb2bd662Svikram 	char		*cp;
5252eb2bd662Svikram 	char		*slash;
5253eb2bd662Svikram 	int		ret;
5254eb2bd662Svikram 	const char	*fcn = "get_pool()";
5255eb2bd662Svikram 
5256eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_OSDEV", osdev = NULL);
5257eb2bd662Svikram 	if (osdev == NULL) {
5258eb2bd662Svikram 		bam_error(GET_POOL_OSDEV_NULL);
5259eb2bd662Svikram 		return (NULL);
5260eb2bd662Svikram 	}
5261eb2bd662Svikram 
5262eb2bd662Svikram 	BAM_DPRINTF((D_GET_POOL_OSDEV, fcn, osdev));
5263eb2bd662Svikram 
5264eb2bd662Svikram 	if (osdev[0] != '/') {
5265eb2bd662Svikram 		(void) strlcpy(buf, osdev, sizeof (buf));
5266eb2bd662Svikram 		slash = strchr(buf, '/');
5267eb2bd662Svikram 		if (slash)
5268eb2bd662Svikram 			*slash = '\0';
5269eb2bd662Svikram 		pool = s_strdup(buf);
5270eb2bd662Svikram 		BAM_DPRINTF((D_GET_POOL_RET, fcn, pool));
5271eb2bd662Svikram 		return (pool);
5272eb2bd662Svikram 	} else if (strncmp(osdev, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
5273eb2bd662Svikram 	    strncmp(osdev, "/dev/rdsk/", strlen("/dev/rdsk/")) != 0) {
5274eb2bd662Svikram 		bam_error(GET_POOL_BAD_OSDEV, osdev);
5275eb2bd662Svikram 		return (NULL);
5276eb2bd662Svikram 	}
5277eb2bd662Svikram 
5278eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5279eb2bd662Svikram 	    "/usr/sbin/fstyp -a %s 2>/dev/null | /bin/grep '^name:'",
5280eb2bd662Svikram 	    osdev);
5281eb2bd662Svikram 
5282eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5283eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_FSTYP", ret = 1);
5284eb2bd662Svikram 	if (ret != 0) {
5285eb2bd662Svikram 		bam_error(FSTYP_A_FAILED, osdev);
5286eb2bd662Svikram 		return (NULL);
5287eb2bd662Svikram 	}
5288eb2bd662Svikram 
5289eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_FSTYP_OUT", flist.head = NULL);
5290eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5291eb2bd662Svikram 		bam_error(NULL_FSTYP_A, osdev);
5292eb2bd662Svikram 		filelist_free(&flist);
5293eb2bd662Svikram 		return (NULL);
5294eb2bd662Svikram 	}
5295eb2bd662Svikram 
5296eb2bd662Svikram 	(void) strtok(flist.head->line, "'");
5297eb2bd662Svikram 	cp = strtok(NULL, "'");
5298eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_FSTYP_STRTOK", cp = NULL);
5299eb2bd662Svikram 	if (cp == NULL) {
5300eb2bd662Svikram 		bam_error(BAD_FSTYP_A, osdev);
5301eb2bd662Svikram 		filelist_free(&flist);
5302eb2bd662Svikram 		return (NULL);
5303eb2bd662Svikram 	}
5304eb2bd662Svikram 
5305eb2bd662Svikram 	pool = s_strdup(cp);
5306eb2bd662Svikram 
5307eb2bd662Svikram 	filelist_free(&flist);
5308eb2bd662Svikram 
5309eb2bd662Svikram 	BAM_DPRINTF((D_GET_POOL_RET, fcn, pool));
5310eb2bd662Svikram 
5311eb2bd662Svikram 	return (pool);
5312eb2bd662Svikram }
5313eb2bd662Svikram 
5314eb2bd662Svikram static char *
5315eb2bd662Svikram find_zfs_existing(char *osdev)
5316eb2bd662Svikram {
5317eb2bd662Svikram 	char		*pool;
5318eb2bd662Svikram 	zfs_mnted_t	mnted;
5319eb2bd662Svikram 	char		*mntpt;
5320eb2bd662Svikram 	char		*sign;
5321eb2bd662Svikram 	const char	*fcn = "find_zfs_existing()";
5322eb2bd662Svikram 
5323eb2bd662Svikram 	pool = get_pool(osdev);
5324eb2bd662Svikram 	INJECT_ERROR1("ZFS_FIND_EXIST_POOL", pool = NULL);
5325eb2bd662Svikram 	if (pool == NULL) {
5326eb2bd662Svikram 		bam_error(ZFS_GET_POOL_FAILED, osdev);
5327eb2bd662Svikram 		return (NULL);
5328eb2bd662Svikram 	}
5329eb2bd662Svikram 
5330eb2bd662Svikram 	mntpt = mount_top_dataset(pool, &mnted);
5331eb2bd662Svikram 	INJECT_ERROR1("ZFS_FIND_EXIST_MOUNT_TOP", mntpt = NULL);
5332eb2bd662Svikram 	if (mntpt == NULL) {
5333eb2bd662Svikram 		bam_error(ZFS_MOUNT_TOP_DATASET_FAILED, pool);
5334eb2bd662Svikram 		free(pool);
5335eb2bd662Svikram 		return (NULL);
5336eb2bd662Svikram 	}
5337eb2bd662Svikram 
5338eb2bd662Svikram 	sign = find_primary_common(mntpt, "zfs");
5339eb2bd662Svikram 	if (sign == NULL) {
5340eb2bd662Svikram 		sign = find_backup_common(mntpt, "zfs");
5341eb2bd662Svikram 		BAM_DPRINTF((D_EXIST_BACKUP_SIGN, fcn, sign ? sign : "NULL"));
5342eb2bd662Svikram 	} else {
5343eb2bd662Svikram 		BAM_DPRINTF((D_EXIST_PRIMARY_SIGN, fcn, sign));
5344eb2bd662Svikram 	}
5345eb2bd662Svikram 
5346eb2bd662Svikram 	(void) umount_top_dataset(pool, mnted, mntpt);
5347eb2bd662Svikram 
5348eb2bd662Svikram 	free(pool);
5349eb2bd662Svikram 
5350eb2bd662Svikram 	return (sign);
5351eb2bd662Svikram }
5352eb2bd662Svikram 
5353eb2bd662Svikram static char *
5354eb2bd662Svikram find_existing_sign(char *osroot, char *osdev, char *fstype)
5355eb2bd662Svikram {
5356eb2bd662Svikram 	const char		*fcn = "find_existing_sign()";
5357eb2bd662Svikram 
5358eb2bd662Svikram 	INJECT_ERROR1("FIND_EXIST_NOTSUP_FS", fstype = "foofs");
5359eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0) {
5360eb2bd662Svikram 		BAM_DPRINTF((D_CHECK_UFS_EXIST_SIGN, fcn));
5361eb2bd662Svikram 		return (find_ufs_existing(osroot));
5362eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0) {
5363eb2bd662Svikram 		BAM_DPRINTF((D_CHECK_ZFS_EXIST_SIGN, fcn));
5364eb2bd662Svikram 		return (find_zfs_existing(osdev));
5365eb2bd662Svikram 	} else {
5366eb2bd662Svikram 		bam_error(GRUBSIGN_NOTSUP, fstype);
5367eb2bd662Svikram 		return (NULL);
5368eb2bd662Svikram 	}
5369eb2bd662Svikram }
5370eb2bd662Svikram 
5371eb2bd662Svikram #define	MH_HASH_SZ	16
5372eb2bd662Svikram 
5373eb2bd662Svikram typedef enum {
5374eb2bd662Svikram 	MH_ERROR = -1,
5375eb2bd662Svikram 	MH_NOMATCH,
5376eb2bd662Svikram 	MH_MATCH
5377eb2bd662Svikram } mh_search_t;
5378eb2bd662Svikram 
5379eb2bd662Svikram typedef struct mcache {
5380eb2bd662Svikram 	char	*mc_special;
5381eb2bd662Svikram 	char	*mc_mntpt;
5382eb2bd662Svikram 	char	*mc_fstype;
5383eb2bd662Svikram 	struct mcache *mc_next;
5384eb2bd662Svikram } mcache_t;
5385eb2bd662Svikram 
5386eb2bd662Svikram typedef struct mhash {
5387eb2bd662Svikram 	mcache_t *mh_hash[MH_HASH_SZ];
5388eb2bd662Svikram } mhash_t;
5389eb2bd662Svikram 
5390eb2bd662Svikram static int
5391eb2bd662Svikram mhash_fcn(char *key)
5392eb2bd662Svikram {
5393eb2bd662Svikram 	int		i;
5394eb2bd662Svikram 	uint64_t	sum = 0;
5395eb2bd662Svikram 
5396eb2bd662Svikram 	for (i = 0; key[i] != '\0'; i++) {
5397eb2bd662Svikram 		sum += (uchar_t)key[i];
5398eb2bd662Svikram 	}
5399eb2bd662Svikram 
5400eb2bd662Svikram 	sum %= MH_HASH_SZ;
5401eb2bd662Svikram 
5402eb2bd662Svikram 	assert(sum < MH_HASH_SZ);
5403eb2bd662Svikram 
5404eb2bd662Svikram 	return (sum);
5405eb2bd662Svikram }
5406eb2bd662Svikram 
5407eb2bd662Svikram static mhash_t *
5408eb2bd662Svikram cache_mnttab(void)
5409eb2bd662Svikram {
5410eb2bd662Svikram 	FILE		*mfp;
5411eb2bd662Svikram 	struct extmnttab mnt;
5412eb2bd662Svikram 	mcache_t	*mcp;
5413eb2bd662Svikram 	mhash_t		*mhp;
5414eb2bd662Svikram 	char		*ctds;
5415eb2bd662Svikram 	int		idx;
5416eb2bd662Svikram 	int		error;
5417eb2bd662Svikram 	char		*special_dup;
5418eb2bd662Svikram 	const char	*fcn = "cache_mnttab()";
5419eb2bd662Svikram 
5420eb2bd662Svikram 	mfp = fopen(MNTTAB, "r");
5421eb2bd662Svikram 	error = errno;
5422eb2bd662Svikram 	INJECT_ERROR1("CACHE_MNTTAB_MNTTAB_ERR", mfp = NULL);
5423eb2bd662Svikram 	if (mfp == NULL) {
5424eb2bd662Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
5425eb2bd662Svikram 		return (NULL);
5426eb2bd662Svikram 	}
5427eb2bd662Svikram 
5428eb2bd662Svikram 	mhp = s_calloc(1, sizeof (mhash_t));
5429eb2bd662Svikram 
5430eb2bd662Svikram 	resetmnttab(mfp);
5431eb2bd662Svikram 
5432eb2bd662Svikram 	while (getextmntent(mfp, &mnt, sizeof (mnt)) == 0) {
5433eb2bd662Svikram 		/* only cache ufs */
5434eb2bd662Svikram 		if (strcmp(mnt.mnt_fstype, "ufs") != 0)
5435eb2bd662Svikram 			continue;
5436eb2bd662Svikram 
5437eb2bd662Svikram 		/* basename() modifies its arg, so dup it */
5438eb2bd662Svikram 		special_dup = s_strdup(mnt.mnt_special);
5439eb2bd662Svikram 		ctds = basename(special_dup);
5440eb2bd662Svikram 
5441eb2bd662Svikram 		mcp = s_calloc(1, sizeof (mcache_t));
5442eb2bd662Svikram 		mcp->mc_special = s_strdup(ctds);
5443eb2bd662Svikram 		mcp->mc_mntpt = s_strdup(mnt.mnt_mountp);
5444eb2bd662Svikram 		mcp->mc_fstype = s_strdup(mnt.mnt_fstype);
5445eb2bd662Svikram 		BAM_DPRINTF((D_CACHE_MNTS, fcn, ctds,
5446eb2bd662Svikram 		    mnt.mnt_mountp, mnt.mnt_fstype));
5447eb2bd662Svikram 		idx = mhash_fcn(ctds);
5448eb2bd662Svikram 		mcp->mc_next = mhp->mh_hash[idx];
5449eb2bd662Svikram 		mhp->mh_hash[idx] = mcp;
5450eb2bd662Svikram 		free(special_dup);
5451eb2bd662Svikram 	}
5452eb2bd662Svikram 
5453eb2bd662Svikram 	(void) fclose(mfp);
5454eb2bd662Svikram 
5455eb2bd662Svikram 	return (mhp);
5456eb2bd662Svikram }
5457eb2bd662Svikram 
5458eb2bd662Svikram static void
5459eb2bd662Svikram free_mnttab(mhash_t *mhp)
5460eb2bd662Svikram {
5461eb2bd662Svikram 	mcache_t	*mcp;
5462eb2bd662Svikram 	int		i;
5463eb2bd662Svikram 
5464eb2bd662Svikram 	for (i = 0; i < MH_HASH_SZ; i++) {
5465eb2bd662Svikram 		/*LINTED*/
5466eb2bd662Svikram 		while (mcp = mhp->mh_hash[i]) {
5467eb2bd662Svikram 			mhp->mh_hash[i] = mcp->mc_next;
5468eb2bd662Svikram 			free(mcp->mc_special);
5469eb2bd662Svikram 			free(mcp->mc_mntpt);
5470eb2bd662Svikram 			free(mcp->mc_fstype);
5471eb2bd662Svikram 			free(mcp);
5472eb2bd662Svikram 		}
5473eb2bd662Svikram 	}
5474eb2bd662Svikram 
5475eb2bd662Svikram 	for (i = 0; i < MH_HASH_SZ; i++) {
5476eb2bd662Svikram 		assert(mhp->mh_hash[i] == NULL);
5477eb2bd662Svikram 	}
5478eb2bd662Svikram 	free(mhp);
5479eb2bd662Svikram }
5480eb2bd662Svikram 
5481eb2bd662Svikram static mh_search_t
5482eb2bd662Svikram search_hash(mhash_t *mhp, char *special, char **mntpt)
5483eb2bd662Svikram {
5484eb2bd662Svikram 	int		idx;
5485eb2bd662Svikram 	mcache_t	*mcp;
5486eb2bd662Svikram 	const char 	*fcn = "search_hash()";
5487eb2bd662Svikram 
5488eb2bd662Svikram 	assert(mntpt);
5489eb2bd662Svikram 
5490eb2bd662Svikram 	*mntpt = NULL;
5491eb2bd662Svikram 
5492eb2bd662Svikram 	INJECT_ERROR1("SEARCH_HASH_FULL_PATH", special = "/foo");
5493eb2bd662Svikram 	if (strchr(special, '/')) {
5494eb2bd662Svikram 		bam_error(INVALID_MHASH_KEY, special);
5495eb2bd662Svikram 		return (MH_ERROR);
5496eb2bd662Svikram 	}
5497eb2bd662Svikram 
5498eb2bd662Svikram 	idx = mhash_fcn(special);
5499eb2bd662Svikram 
5500eb2bd662Svikram 	for (mcp = mhp->mh_hash[idx]; mcp; mcp = mcp->mc_next) {
5501eb2bd662Svikram 		if (strcmp(mcp->mc_special, special) == 0)
5502eb2bd662Svikram 			break;
5503eb2bd662Svikram 	}
5504eb2bd662Svikram 
5505eb2bd662Svikram 	if (mcp == NULL) {
5506eb2bd662Svikram 		BAM_DPRINTF((D_MNTTAB_HASH_NOMATCH, fcn, special));
5507eb2bd662Svikram 		return (MH_NOMATCH);
5508eb2bd662Svikram 	}
5509eb2bd662Svikram 
5510eb2bd662Svikram 	assert(strcmp(mcp->mc_fstype, "ufs") == 0);
5511eb2bd662Svikram 	*mntpt = mcp->mc_mntpt;
5512eb2bd662Svikram 	BAM_DPRINTF((D_MNTTAB_HASH_MATCH, fcn, special));
5513eb2bd662Svikram 	return (MH_MATCH);
5514eb2bd662Svikram }
5515eb2bd662Svikram 
5516eb2bd662Svikram static int
5517eb2bd662Svikram check_add_ufs_sign_to_list(FILE *tfp, char *mntpt)
5518eb2bd662Svikram {
5519eb2bd662Svikram 	char		*sign;
5520eb2bd662Svikram 	char		*signline;
5521eb2bd662Svikram 	char		signbuf[MAXNAMELEN];
5522eb2bd662Svikram 	int		len;
5523eb2bd662Svikram 	int		error;
5524eb2bd662Svikram 	const char	*fcn = "check_add_ufs_sign_to_list()";
5525eb2bd662Svikram 
5526eb2bd662Svikram 	/* safe to specify NULL as "osdev" arg for UFS */
5527eb2bd662Svikram 	sign = find_existing_sign(mntpt, NULL, "ufs");
5528eb2bd662Svikram 	if (sign == NULL) {
5529eb2bd662Svikram 		/* No existing signature, nothing to add to list */
5530eb2bd662Svikram 		BAM_DPRINTF((D_NO_SIGN_TO_LIST, fcn, mntpt));
5531eb2bd662Svikram 		return (0);
5532eb2bd662Svikram 	}
5533eb2bd662Svikram 
5534eb2bd662Svikram 	(void) snprintf(signbuf, sizeof (signbuf), "%s\n", sign);
5535eb2bd662Svikram 	signline = signbuf;
5536eb2bd662Svikram 
5537eb2bd662Svikram 	INJECT_ERROR1("UFS_MNTPT_SIGN_NOTUFS", signline = "pool_rpool10\n");
5538eb2bd662Svikram 	if (strncmp(signline, GRUBSIGN_UFS_PREFIX,
5539eb2bd662Svikram 	    strlen(GRUBSIGN_UFS_PREFIX))) {
5540eb2bd662Svikram 		bam_error(INVALID_UFS_SIGNATURE, sign);
5541eb2bd662Svikram 		free(sign);
5542eb2bd662Svikram 		/* ignore invalid signatures */
5543eb2bd662Svikram 		return (0);
5544eb2bd662Svikram 	}
5545eb2bd662Svikram 
5546eb2bd662Svikram 	len = fputs(signline, tfp);
5547eb2bd662Svikram 	error = errno;
5548eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_PUTS_ERROR", len = 0);
5549eb2bd662Svikram 	if (len != strlen(signline)) {
5550eb2bd662Svikram 		bam_error(SIGN_LIST_FPUTS_ERR, sign, strerror(error));
5551eb2bd662Svikram 		free(sign);
5552eb2bd662Svikram 		return (-1);
5553eb2bd662Svikram 	}
5554eb2bd662Svikram 
5555eb2bd662Svikram 	free(sign);
5556eb2bd662Svikram 
5557eb2bd662Svikram 	BAM_DPRINTF((D_SIGN_LIST_PUTS_DONE, fcn, mntpt));
5558eb2bd662Svikram 	return (0);
5559eb2bd662Svikram }
5560eb2bd662Svikram 
5561eb2bd662Svikram /*
5562eb2bd662Svikram  * slice is a basename not a full pathname
5563eb2bd662Svikram  */
5564eb2bd662Svikram static int
5565eb2bd662Svikram process_slice_common(char *slice, FILE *tfp, mhash_t *mhp, char *tmpmnt)
5566eb2bd662Svikram {
5567eb2bd662Svikram 	int		ret;
5568eb2bd662Svikram 	char		cmd[PATH_MAX];
5569eb2bd662Svikram 	char		path[PATH_MAX];
5570eb2bd662Svikram 	struct stat	sbuf;
5571eb2bd662Svikram 	char		*mntpt;
5572eb2bd662Svikram 	filelist_t	flist = {0};
5573eb2bd662Svikram 	char		*fstype;
5574eb2bd662Svikram 	char		blkslice[PATH_MAX];
5575eb2bd662Svikram 	const char	*fcn = "process_slice_common()";
5576eb2bd662Svikram 
5577eb2bd662Svikram 
5578eb2bd662Svikram 	ret = search_hash(mhp, slice, &mntpt);
5579eb2bd662Svikram 	switch (ret) {
5580eb2bd662Svikram 		case MH_MATCH:
5581eb2bd662Svikram 			if (check_add_ufs_sign_to_list(tfp, mntpt) == -1)
5582eb2bd662Svikram 				return (-1);
5583eb2bd662Svikram 			else
5584eb2bd662Svikram 				return (0);
5585eb2bd662Svikram 		case MH_NOMATCH:
5586eb2bd662Svikram 			break;
5587eb2bd662Svikram 		case MH_ERROR:
5588eb2bd662Svikram 		default:
5589eb2bd662Svikram 			return (-1);
5590eb2bd662Svikram 	}
5591eb2bd662Svikram 
5592eb2bd662Svikram 	(void) snprintf(path, sizeof (path), "/dev/rdsk/%s", slice);
5593eb2bd662Svikram 	if (stat(path, &sbuf) == -1) {
5594eb2bd662Svikram 		BAM_DPRINTF((D_SLICE_ENOENT, fcn, path));
5595eb2bd662Svikram 		return (0);
5596eb2bd662Svikram 	}
5597eb2bd662Svikram 
5598eb2bd662Svikram 	/* Check if ufs */
5599eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5600eb2bd662Svikram 	    "/usr/sbin/fstyp /dev/rdsk/%s 2>/dev/null",
5601eb2bd662Svikram 	    slice);
5602eb2bd662Svikram 
5603eb2bd662Svikram 	if (exec_cmd(cmd, &flist) != 0) {
5604eb2bd662Svikram 		if (bam_verbose)
5605eb2bd662Svikram 			bam_print(FSTYP_FAILED, slice);
5606eb2bd662Svikram 		return (0);
5607eb2bd662Svikram 	}
5608eb2bd662Svikram 
5609eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5610eb2bd662Svikram 		if (bam_verbose)
5611eb2bd662Svikram 			bam_print(FSTYP_BAD, slice);
5612eb2bd662Svikram 		filelist_free(&flist);
5613eb2bd662Svikram 		return (0);
5614eb2bd662Svikram 	}
5615eb2bd662Svikram 
5616eb2bd662Svikram 	fstype = strtok(flist.head->line, " \t\n");
5617eb2bd662Svikram 	if (fstype == NULL || strcmp(fstype, "ufs") != 0) {
5618eb2bd662Svikram 		if (bam_verbose)
5619eb2bd662Svikram 			bam_print(NOT_UFS_SLICE, slice, fstype);
5620eb2bd662Svikram 		filelist_free(&flist);
5621eb2bd662Svikram 		return (0);
5622eb2bd662Svikram 	}
5623eb2bd662Svikram 
5624eb2bd662Svikram 	filelist_free(&flist);
5625eb2bd662Svikram 
5626eb2bd662Svikram 	/*
5627eb2bd662Svikram 	 * Since we are mounting the filesystem read-only, the
5628eb2bd662Svikram 	 * the last mount field of the superblock is unchanged
5629eb2bd662Svikram 	 * and does not need to be fixed up post-mount;
5630eb2bd662Svikram 	 */
5631eb2bd662Svikram 
5632eb2bd662Svikram 	(void) snprintf(blkslice, sizeof (blkslice), "/dev/dsk/%s",
5633eb2bd662Svikram 	    slice);
5634eb2bd662Svikram 
5635eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5636eb2bd662Svikram 	    "/usr/sbin/mount -F ufs -o ro %s %s "
5637eb2bd662Svikram 	    "> /dev/null 2>&1", blkslice, tmpmnt);
5638eb2bd662Svikram 
5639eb2bd662Svikram 	if (exec_cmd(cmd, NULL) != 0) {
5640eb2bd662Svikram 		if (bam_verbose)
5641eb2bd662Svikram 			bam_print(MOUNT_FAILED, blkslice, "ufs");
5642eb2bd662Svikram 		return (0);
5643eb2bd662Svikram 	}
5644eb2bd662Svikram 
5645eb2bd662Svikram 	ret = check_add_ufs_sign_to_list(tfp, tmpmnt);
5646eb2bd662Svikram 
5647eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5648eb2bd662Svikram 	    "/usr/sbin/umount -f %s > /dev/null 2>&1",
5649eb2bd662Svikram 	    tmpmnt);
5650eb2bd662Svikram 
5651eb2bd662Svikram 	if (exec_cmd(cmd, NULL) != 0) {
5652eb2bd662Svikram 		bam_print(UMOUNT_FAILED, slice);
5653eb2bd662Svikram 		return (0);
5654eb2bd662Svikram 	}
5655eb2bd662Svikram 
5656eb2bd662Svikram 	return (ret);
5657eb2bd662Svikram }
5658eb2bd662Svikram 
5659eb2bd662Svikram static int
5660eb2bd662Svikram process_vtoc_slices(
5661eb2bd662Svikram 	char *s0,
5662eb2bd662Svikram 	struct vtoc *vtoc,
5663eb2bd662Svikram 	FILE *tfp,
5664eb2bd662Svikram 	mhash_t *mhp,
5665eb2bd662Svikram 	char *tmpmnt)
5666eb2bd662Svikram {
5667eb2bd662Svikram 	int		idx;
5668eb2bd662Svikram 	char		slice[PATH_MAX];
5669eb2bd662Svikram 	size_t		len;
5670eb2bd662Svikram 	char		*cp;
5671eb2bd662Svikram 	const char	*fcn = "process_vtoc_slices()";
5672eb2bd662Svikram 
5673eb2bd662Svikram 	len = strlen(s0);
5674eb2bd662Svikram 
5675eb2bd662Svikram 	assert(s0[len - 2] == 's' && s0[len - 1] == '0');
5676eb2bd662Svikram 
5677eb2bd662Svikram 	s0[len - 1] = '\0';
5678eb2bd662Svikram 
5679eb2bd662Svikram 	(void) strlcpy(slice, s0, sizeof (slice));
5680eb2bd662Svikram 
5681eb2bd662Svikram 	s0[len - 1] = '0';
5682eb2bd662Svikram 
5683eb2bd662Svikram 	cp = slice + len - 1;
5684eb2bd662Svikram 
5685eb2bd662Svikram 	for (idx = 0; idx < vtoc->v_nparts; idx++) {
5686eb2bd662Svikram 
5687eb2bd662Svikram 		(void) snprintf(cp, sizeof (slice) - (len - 1), "%u", idx);
5688eb2bd662Svikram 
5689eb2bd662Svikram 		if (vtoc->v_part[idx].p_size == 0) {
5690eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_SIZE_ZERO, fcn, slice));
5691eb2bd662Svikram 			continue;
5692eb2bd662Svikram 		}
5693eb2bd662Svikram 
5694eb2bd662Svikram 		/* Skip "SWAP", "USR", "BACKUP", "VAR", "HOME", "ALTSCTR" */
5695eb2bd662Svikram 		switch (vtoc->v_part[idx].p_tag) {
5696eb2bd662Svikram 		case V_SWAP:
5697eb2bd662Svikram 		case V_USR:
5698eb2bd662Svikram 		case V_BACKUP:
5699eb2bd662Svikram 		case V_VAR:
5700eb2bd662Svikram 		case V_HOME:
5701eb2bd662Svikram 		case V_ALTSCTR:
5702eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_NOT_ROOT_TAG, fcn, slice));
5703eb2bd662Svikram 			continue;
5704eb2bd662Svikram 		default:
5705eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_ROOT_TAG, fcn, slice));
5706eb2bd662Svikram 			break;
5707eb2bd662Svikram 		}
5708eb2bd662Svikram 
5709eb2bd662Svikram 		/* skip unmountable and readonly slices */
5710eb2bd662Svikram 		switch (vtoc->v_part[idx].p_flag) {
5711eb2bd662Svikram 		case V_UNMNT:
5712eb2bd662Svikram 		case V_RONLY:
5713eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_NOT_RDWR_FLAG, fcn, slice));
5714eb2bd662Svikram 			continue;
5715eb2bd662Svikram 		default:
5716eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_RDWR_FLAG, fcn, slice));
5717eb2bd662Svikram 			break;
5718eb2bd662Svikram 		}
5719eb2bd662Svikram 
5720eb2bd662Svikram 		if (process_slice_common(slice, tfp, mhp, tmpmnt) == -1) {
5721eb2bd662Svikram 			return (-1);
5722eb2bd662Svikram 		}
5723eb2bd662Svikram 	}
5724eb2bd662Svikram 
5725eb2bd662Svikram 	return (0);
5726eb2bd662Svikram }
5727eb2bd662Svikram 
5728eb2bd662Svikram static int
5729eb2bd662Svikram process_efi_slices(
5730eb2bd662Svikram 	char *s0,
5731eb2bd662Svikram 	struct dk_gpt *efi,
5732eb2bd662Svikram 	FILE *tfp,
5733eb2bd662Svikram 	mhash_t *mhp,
5734eb2bd662Svikram 	char *tmpmnt)
5735eb2bd662Svikram {
5736eb2bd662Svikram 	int		idx;
5737eb2bd662Svikram 	char		slice[PATH_MAX];
5738eb2bd662Svikram 	size_t		len;
5739eb2bd662Svikram 	char		*cp;
5740eb2bd662Svikram 	const char	*fcn = "process_efi_slices()";
5741eb2bd662Svikram 
5742eb2bd662Svikram 	len = strlen(s0);
5743eb2bd662Svikram 
5744eb2bd662Svikram 	assert(s0[len - 2] == 's' && s0[len - 1] == '0');
5745eb2bd662Svikram 
5746eb2bd662Svikram 	s0[len - 1] = '\0';
5747eb2bd662Svikram 
5748eb2bd662Svikram 	(void) strlcpy(slice, s0, sizeof (slice));
5749eb2bd662Svikram 
5750eb2bd662Svikram 	s0[len - 1] = '0';
5751eb2bd662Svikram 
5752eb2bd662Svikram 	cp = slice + len - 1;
5753eb2bd662Svikram 
5754eb2bd662Svikram 	for (idx = 0; idx < efi->efi_nparts; idx++) {
5755eb2bd662Svikram 
5756eb2bd662Svikram 		(void) snprintf(cp, sizeof (slice) - (len - 1), "%u", idx);
5757eb2bd662Svikram 
5758eb2bd662Svikram 		if (efi->efi_parts[idx].p_size == 0) {
5759eb2bd662Svikram 			BAM_DPRINTF((D_EFI_SIZE_ZERO, fcn, slice));
5760eb2bd662Svikram 			continue;
5761eb2bd662Svikram 		}
5762eb2bd662Svikram 
5763eb2bd662Svikram 		/* Skip "SWAP", "USR", "BACKUP", "VAR", "HOME", "ALTSCTR" */
5764eb2bd662Svikram 		switch (efi->efi_parts[idx].p_tag) {
5765eb2bd662Svikram 		case V_SWAP:
5766eb2bd662Svikram 		case V_USR:
5767eb2bd662Svikram 		case V_BACKUP:
5768eb2bd662Svikram 		case V_VAR:
5769eb2bd662Svikram 		case V_HOME:
5770eb2bd662Svikram 		case V_ALTSCTR:
5771eb2bd662Svikram 			BAM_DPRINTF((D_EFI_NOT_ROOT_TAG, fcn, slice));
5772eb2bd662Svikram 			continue;
5773eb2bd662Svikram 		default:
5774eb2bd662Svikram 			BAM_DPRINTF((D_EFI_ROOT_TAG, fcn, slice));
5775eb2bd662Svikram 			break;
5776eb2bd662Svikram 		}
5777eb2bd662Svikram 
5778eb2bd662Svikram 		/* skip unmountable and readonly slices */
5779eb2bd662Svikram 		switch (efi->efi_parts[idx].p_flag) {
5780eb2bd662Svikram 		case V_UNMNT:
5781eb2bd662Svikram 		case V_RONLY:
5782eb2bd662Svikram 			BAM_DPRINTF((D_EFI_NOT_RDWR_FLAG, fcn, slice));
5783eb2bd662Svikram 			continue;
5784eb2bd662Svikram 		default:
5785eb2bd662Svikram 			BAM_DPRINTF((D_EFI_RDWR_FLAG, fcn, slice));
5786eb2bd662Svikram 			break;
5787eb2bd662Svikram 		}
5788eb2bd662Svikram 
5789eb2bd662Svikram 		if (process_slice_common(slice, tfp, mhp, tmpmnt) == -1) {
5790eb2bd662Svikram 			return (-1);
5791eb2bd662Svikram 		}
5792eb2bd662Svikram 	}
5793eb2bd662Svikram 
5794eb2bd662Svikram 	return (0);
5795eb2bd662Svikram }
5796eb2bd662Svikram 
5797eb2bd662Svikram /*
5798eb2bd662Svikram  * s0 is a basename not a full path
5799eb2bd662Svikram  */
5800eb2bd662Svikram static int
5801eb2bd662Svikram process_slice0(char *s0, FILE *tfp, mhash_t *mhp, char *tmpmnt)
5802eb2bd662Svikram {
5803eb2bd662Svikram 	struct vtoc		vtoc;
5804eb2bd662Svikram 	struct dk_gpt		*efi;
5805eb2bd662Svikram 	char			s0path[PATH_MAX];
5806eb2bd662Svikram 	struct stat		sbuf;
5807eb2bd662Svikram 	int			e_flag;
5808eb2bd662Svikram 	int			v_flag;
5809eb2bd662Svikram 	int			retval;
5810eb2bd662Svikram 	int			err;
5811eb2bd662Svikram 	int			fd;
5812eb2bd662Svikram 	const char		*fcn = "process_slice0()";
5813eb2bd662Svikram 
5814eb2bd662Svikram 	(void) snprintf(s0path, sizeof (s0path), "/dev/rdsk/%s", s0);
5815eb2bd662Svikram 
5816eb2bd662Svikram 	if (stat(s0path, &sbuf) == -1) {
5817eb2bd662Svikram 		BAM_DPRINTF((D_SLICE0_ENOENT, fcn, s0path));
5818eb2bd662Svikram 		return (0);
5819eb2bd662Svikram 	}
5820eb2bd662Svikram 
5821eb2bd662Svikram 	fd = open(s0path, O_NONBLOCK|O_RDONLY);
5822eb2bd662Svikram 	if (fd == -1) {
5823eb2bd662Svikram 		bam_error(OPEN_FAIL, s0path, strerror(errno));
5824eb2bd662Svikram 		return (0);
5825eb2bd662Svikram 	}
5826eb2bd662Svikram 
5827eb2bd662Svikram 	e_flag = v_flag = 0;
5828eb2bd662Svikram 	retval = ((err = read_vtoc(fd, &vtoc)) >= 0) ? 0 : err;
5829eb2bd662Svikram 	switch (retval) {
5830eb2bd662Svikram 		case VT_EIO:
5831eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_READ_FAIL, fcn, s0path));
5832eb2bd662Svikram 			break;
5833eb2bd662Svikram 		case VT_EINVAL:
5834eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_INVALID, fcn, s0path));
5835eb2bd662Svikram 			break;
5836eb2bd662Svikram 		case VT_ERROR:
5837eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_UNKNOWN_ERR, fcn, s0path));
5838eb2bd662Svikram 			break;
5839eb2bd662Svikram 		case VT_ENOTSUP:
5840eb2bd662Svikram 			e_flag = 1;
5841eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_NOTSUP, fcn, s0path));
5842eb2bd662Svikram 			break;
5843eb2bd662Svikram 		case 0:
5844eb2bd662Svikram 			v_flag = 1;
5845eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_READ_SUCCESS, fcn, s0path));
5846eb2bd662Svikram 			break;
5847eb2bd662Svikram 		default:
5848eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_UNKNOWN_RETCODE, fcn, s0path));
5849eb2bd662Svikram 			break;
5850eb2bd662Svikram 	}
5851eb2bd662Svikram 
5852eb2bd662Svikram 
5853eb2bd662Svikram 	if (e_flag) {
5854eb2bd662Svikram 		e_flag = 0;
5855eb2bd662Svikram 		retval = ((err = efi_alloc_and_read(fd, &efi)) >= 0) ? 0 : err;
5856eb2bd662Svikram 		switch (retval) {
5857eb2bd662Svikram 		case VT_EIO:
5858eb2bd662Svikram 			BAM_DPRINTF((D_EFI_READ_FAIL, fcn, s0path));
5859eb2bd662Svikram 			break;
5860eb2bd662Svikram 		case VT_EINVAL:
5861eb2bd662Svikram 			BAM_DPRINTF((D_EFI_INVALID, fcn, s0path));
5862eb2bd662Svikram 			break;
5863eb2bd662Svikram 		case VT_ERROR:
5864eb2bd662Svikram 			BAM_DPRINTF((D_EFI_UNKNOWN_ERR, fcn, s0path));
5865eb2bd662Svikram 			break;
5866eb2bd662Svikram 		case VT_ENOTSUP:
5867eb2bd662Svikram 			BAM_DPRINTF((D_EFI_NOTSUP, fcn, s0path));
5868eb2bd662Svikram 			break;
5869eb2bd662Svikram 		case 0:
5870eb2bd662Svikram 			e_flag = 1;
5871eb2bd662Svikram 			BAM_DPRINTF((D_EFI_READ_SUCCESS, fcn, s0path));
5872eb2bd662Svikram 			break;
5873eb2bd662Svikram 		default:
5874eb2bd662Svikram 			BAM_DPRINTF((D_EFI_UNKNOWN_RETCODE, fcn, s0path));
5875eb2bd662Svikram 			break;
5876eb2bd662Svikram 		}
5877eb2bd662Svikram 	}
5878eb2bd662Svikram 
5879eb2bd662Svikram 	(void) close(fd);
5880eb2bd662Svikram 
5881eb2bd662Svikram 	if (v_flag) {
5882eb2bd662Svikram 		retval = process_vtoc_slices(s0,
5883eb2bd662Svikram 		    &vtoc, tfp, mhp, tmpmnt);
5884eb2bd662Svikram 	} else if (e_flag) {
5885eb2bd662Svikram 		retval = process_efi_slices(s0,
5886eb2bd662Svikram 		    efi, tfp, mhp, tmpmnt);
5887eb2bd662Svikram 	} else {
5888eb2bd662Svikram 		BAM_DPRINTF((D_NOT_VTOC_OR_EFI, fcn, s0path));
5889eb2bd662Svikram 		return (0);
5890eb2bd662Svikram 	}
5891eb2bd662Svikram 
5892eb2bd662Svikram 	return (retval);
5893eb2bd662Svikram }
5894eb2bd662Svikram 
5895eb2bd662Svikram /*
5896eb2bd662Svikram  * Find and create a list of all existing UFS boot signatures
5897eb2bd662Svikram  */
5898eb2bd662Svikram static int
5899eb2bd662Svikram FindAllUfsSignatures(void)
5900eb2bd662Svikram {
5901eb2bd662Svikram 	mhash_t		*mnttab_hash;
5902eb2bd662Svikram 	DIR		*dirp = NULL;
5903eb2bd662Svikram 	struct dirent	*dp;
5904eb2bd662Svikram 	char		tmpmnt[PATH_MAX];
5905eb2bd662Svikram 	char		cmd[PATH_MAX];
5906eb2bd662Svikram 	struct stat	sb;
5907eb2bd662Svikram 	int		fd;
5908eb2bd662Svikram 	FILE		*tfp;
5909eb2bd662Svikram 	size_t		len;
5910eb2bd662Svikram 	int		ret;
5911eb2bd662Svikram 	int		error;
5912eb2bd662Svikram 	const char	*fcn = "FindAllUfsSignatures()";
5913eb2bd662Svikram 
5914eb2bd662Svikram 	if (stat(UFS_SIGNATURE_LIST, &sb) != -1)  {
5915eb2bd662Svikram 		bam_print(SIGNATURE_LIST_EXISTS, UFS_SIGNATURE_LIST);
5916eb2bd662Svikram 		return (0);
5917eb2bd662Svikram 	}
5918eb2bd662Svikram 
5919eb2bd662Svikram 	fd = open(UFS_SIGNATURE_LIST".tmp",
5920eb2bd662Svikram 	    O_RDWR|O_CREAT|O_TRUNC, 0644);
5921eb2bd662Svikram 	error = errno;
5922eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_TMP_TRUNC", fd = -1);
5923eb2bd662Svikram 	if (fd == -1) {
5924eb2bd662Svikram 		bam_error(OPEN_FAIL, UFS_SIGNATURE_LIST".tmp", strerror(error));
5925eb2bd662Svikram 		return (-1);
5926eb2bd662Svikram 	}
5927eb2bd662Svikram 
5928eb2bd662Svikram 	ret = close(fd);
5929eb2bd662Svikram 	error = errno;
5930eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_TMP_CLOSE", ret = -1);
5931eb2bd662Svikram 	if (ret == -1) {
5932eb2bd662Svikram 		bam_error(CLOSE_FAIL, UFS_SIGNATURE_LIST".tmp",
5933eb2bd662Svikram 		    strerror(error));
5934eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
5935eb2bd662Svikram 		return (-1);
5936eb2bd662Svikram 	}
5937eb2bd662Svikram 
5938eb2bd662Svikram 	tfp = fopen(UFS_SIGNATURE_LIST".tmp", "a");
5939eb2bd662Svikram 	error = errno;
5940eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_APPEND_FOPEN", tfp = NULL);
5941eb2bd662Svikram 	if (tfp == NULL) {
5942eb2bd662Svikram 		bam_error(OPEN_FAIL, UFS_SIGNATURE_LIST".tmp", strerror(error));
5943eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
5944eb2bd662Svikram 		return (-1);
5945eb2bd662Svikram 	}
5946eb2bd662Svikram 
5947eb2bd662Svikram 	mnttab_hash = cache_mnttab();
5948eb2bd662Svikram 	INJECT_ERROR1("CACHE_MNTTAB_ERROR", mnttab_hash = NULL);
5949eb2bd662Svikram 	if (mnttab_hash == NULL) {
5950eb2bd662Svikram 		(void) fclose(tfp);
5951eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
5952eb2bd662Svikram 		bam_error(CACHE_MNTTAB_FAIL, fcn);
5953eb2bd662Svikram 		return (-1);
5954eb2bd662Svikram 	}
5955eb2bd662Svikram 
5956eb2bd662Svikram 	(void) snprintf(tmpmnt, sizeof (tmpmnt),
5957eb2bd662Svikram 	    "/tmp/bootadm_ufs_sign_mnt.%d", getpid());
5958eb2bd662Svikram 	(void) unlink(tmpmnt);
5959eb2bd662Svikram 
596048847494SEnrico Perla - Sun Microsystems 	ret = mkdirp(tmpmnt, DIR_PERMS);
5961eb2bd662Svikram 	error = errno;
5962eb2bd662Svikram 	INJECT_ERROR1("MKDIRP_SIGN_MNT", ret = -1);
5963eb2bd662Svikram 	if (ret == -1) {
5964eb2bd662Svikram 		bam_error(MKDIR_FAILED, tmpmnt, strerror(error));
5965eb2bd662Svikram 		free_mnttab(mnttab_hash);
5966eb2bd662Svikram 		(void) fclose(tfp);
5967eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
5968eb2bd662Svikram 		return (-1);
5969eb2bd662Svikram 	}
5970eb2bd662Svikram 
5971eb2bd662Svikram 	dirp = opendir("/dev/rdsk");
5972eb2bd662Svikram 	error = errno;
5973eb2bd662Svikram 	INJECT_ERROR1("OPENDIR_DEV_RDSK", dirp = NULL);
5974eb2bd662Svikram 	if (dirp == NULL) {
5975eb2bd662Svikram 		bam_error(OPENDIR_FAILED, "/dev/rdsk", strerror(error));
5976eb2bd662Svikram 		goto fail;
5977eb2bd662Svikram 	}
5978eb2bd662Svikram 
5979eb2bd662Svikram 	while (dp = readdir(dirp)) {
5980eb2bd662Svikram 		if (strcmp(dp->d_name, ".") == 0 ||
5981eb2bd662Svikram 		    strcmp(dp->d_name, "..") == 0)
5982eb2bd662Svikram 			continue;
5983eb2bd662Svikram 
5984eb2bd662Svikram 		/*
5985eb2bd662Svikram 		 * we only look for the s0 slice. This is guranteed to
5986eb2bd662Svikram 		 * have 's' at len - 2.
5987eb2bd662Svikram 		 */
5988eb2bd662Svikram 		len = strlen(dp->d_name);
5989eb2bd662Svikram 		if (dp->d_name[len - 2 ] != 's' || dp->d_name[len - 1] != '0') {
5990eb2bd662Svikram 			BAM_DPRINTF((D_SKIP_SLICE_NOTZERO, fcn, dp->d_name));
5991eb2bd662Svikram 			continue;
5992eb2bd662Svikram 		}
5993eb2bd662Svikram 
5994eb2bd662Svikram 		ret = process_slice0(dp->d_name, tfp, mnttab_hash, tmpmnt);
5995eb2bd662Svikram 		INJECT_ERROR1("PROCESS_S0_FAIL", ret = -1);
5996eb2bd662Svikram 		if (ret == -1)
5997eb2bd662Svikram 			goto fail;
5998eb2bd662Svikram 	}
5999eb2bd662Svikram 
6000eb2bd662Svikram 	(void) closedir(dirp);
6001eb2bd662Svikram 	free_mnttab(mnttab_hash);
6002eb2bd662Svikram 	(void) rmdir(tmpmnt);
6003eb2bd662Svikram 
6004eb2bd662Svikram 	ret = fclose(tfp);
6005eb2bd662Svikram 	error = errno;
6006eb2bd662Svikram 	INJECT_ERROR1("FCLOSE_SIGNLIST_TMP", ret = EOF);
6007eb2bd662Svikram 	if (ret == EOF) {
6008eb2bd662Svikram 		bam_error(CLOSE_FAIL, UFS_SIGNATURE_LIST".tmp",
6009eb2bd662Svikram 		    strerror(error));
6010eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6011eb2bd662Svikram 		return (-1);
6012eb2bd662Svikram 	}
6013eb2bd662Svikram 
6014eb2bd662Svikram 	/* We have a list of existing GRUB signatures. Sort it first */
6015eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6016eb2bd662Svikram 	    "/usr/bin/sort -u %s.tmp > %s.sorted",
6017eb2bd662Svikram 	    UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST);
6018eb2bd662Svikram 
6019eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
6020eb2bd662Svikram 	INJECT_ERROR1("SORT_SIGN_LIST", ret = 1);
6021eb2bd662Svikram 	if (ret != 0) {
6022eb2bd662Svikram 		bam_error(GRUBSIGN_SORT_FAILED);
6023eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
6024eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6025eb2bd662Svikram 		return (-1);
6026eb2bd662Svikram 	}
6027eb2bd662Svikram 
6028eb2bd662Svikram 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
6029eb2bd662Svikram 
6030eb2bd662Svikram 	ret = rename(UFS_SIGNATURE_LIST".sorted", UFS_SIGNATURE_LIST);
6031eb2bd662Svikram 	error = errno;
6032eb2bd662Svikram 	INJECT_ERROR1("RENAME_TMP_SIGNLIST", ret = -1);
6033eb2bd662Svikram 	if (ret == -1) {
6034eb2bd662Svikram 		bam_error(RENAME_FAIL, UFS_SIGNATURE_LIST, strerror(error));
6035eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
6036eb2bd662Svikram 		return (-1);
6037eb2bd662Svikram 	}
6038eb2bd662Svikram 
6039eb2bd662Svikram 	if (stat(UFS_SIGNATURE_LIST, &sb) == 0 && sb.st_size == 0) {
6040eb2bd662Svikram 		BAM_DPRINTF((D_ZERO_LEN_SIGNLIST, fcn, UFS_SIGNATURE_LIST));
6041eb2bd662Svikram 	}
6042eb2bd662Svikram 
6043eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6044eb2bd662Svikram 	return (0);
6045eb2bd662Svikram 
6046eb2bd662Svikram fail:
6047eb2bd662Svikram 	if (dirp)
6048eb2bd662Svikram 		(void) closedir(dirp);
6049eb2bd662Svikram 	free_mnttab(mnttab_hash);
6050eb2bd662Svikram 	(void) rmdir(tmpmnt);
6051eb2bd662Svikram 	(void) fclose(tfp);
6052eb2bd662Svikram 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
6053eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6054eb2bd662Svikram 	return (-1);
6055eb2bd662Svikram }
6056eb2bd662Svikram 
6057eb2bd662Svikram static char *
6058eb2bd662Svikram create_ufs_sign(void)
6059eb2bd662Svikram {
6060eb2bd662Svikram 	struct stat	sb;
6061eb2bd662Svikram 	int		signnum = -1;
6062eb2bd662Svikram 	char		tmpsign[MAXNAMELEN + 1];
6063eb2bd662Svikram 	char		*numstr;
6064eb2bd662Svikram 	int		i;
6065eb2bd662Svikram 	FILE		*tfp;
6066eb2bd662Svikram 	int		ret;
6067eb2bd662Svikram 	int		error;
6068eb2bd662Svikram 	const char	*fcn = "create_ufs_sign()";
6069eb2bd662Svikram 
6070eb2bd662Svikram 	bam_print(SEARCHING_UFS_SIGN);
6071eb2bd662Svikram 
6072eb2bd662Svikram 	ret = FindAllUfsSignatures();
6073eb2bd662Svikram 	INJECT_ERROR1("FIND_ALL_UFS", ret = -1);
6074eb2bd662Svikram 	if (ret == -1) {
6075eb2bd662Svikram 		bam_error(ERR_FIND_UFS_SIGN);
6076eb2bd662Svikram 		return (NULL);
6077eb2bd662Svikram 	}
6078eb2bd662Svikram 
6079eb2bd662Svikram 	/* Make sure the list exists and is owned by root */
6080eb2bd662Svikram 	INJECT_ERROR1("SIGNLIST_NOT_CREATED",
6081eb2bd662Svikram 	    (void) unlink(UFS_SIGNATURE_LIST));
6082eb2bd662Svikram 	if (stat(UFS_SIGNATURE_LIST, &sb) == -1 || sb.st_uid != 0) {
6083eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6084eb2bd662Svikram 		bam_error(UFS_SIGNATURE_LIST_MISS, UFS_SIGNATURE_LIST);
6085eb2bd662Svikram 		return (NULL);
6086eb2bd662Svikram 	}
6087eb2bd662Svikram 
6088eb2bd662Svikram 	if (sb.st_size == 0) {
6089eb2bd662Svikram 		bam_print(GRUBSIGN_UFS_NONE);
6090eb2bd662Svikram 		i = 0;
6091eb2bd662Svikram 		goto found;
6092eb2bd662Svikram 	}
6093eb2bd662Svikram 
6094eb2bd662Svikram 	/* The signature list was sorted when it was created */
6095eb2bd662Svikram 	tfp = fopen(UFS_SIGNATURE_LIST, "r");
6096eb2bd662Svikram 	error = errno;
6097eb2bd662Svikram 	INJECT_ERROR1("FOPEN_SIGN_LIST", tfp = NULL);
6098eb2bd662Svikram 	if (tfp == NULL) {
6099eb2bd662Svikram 		bam_error(UFS_SIGNATURE_LIST_OPENERR,
6100eb2bd662Svikram 		    UFS_SIGNATURE_LIST, strerror(error));
6101eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6102eb2bd662Svikram 		return (NULL);
6103eb2bd662Svikram 	}
6104eb2bd662Svikram 
6105eb2bd662Svikram 	for (i = 0; s_fgets(tmpsign, sizeof (tmpsign), tfp); i++) {
6106eb2bd662Svikram 
6107eb2bd662Svikram 		if (strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
6108eb2bd662Svikram 		    strlen(GRUBSIGN_UFS_PREFIX)) != 0) {
6109eb2bd662Svikram 			(void) fclose(tfp);
6110eb2bd662Svikram 			(void) unlink(UFS_SIGNATURE_LIST);
6111eb2bd662Svikram 			bam_error(UFS_BADSIGN, tmpsign);
6112eb2bd662Svikram 			return (NULL);
6113eb2bd662Svikram 		}
6114eb2bd662Svikram 		numstr = tmpsign + strlen(GRUBSIGN_UFS_PREFIX);
6115eb2bd662Svikram 
6116eb2bd662Svikram 		if (numstr[0] == '\0' || !isdigit(numstr[0])) {
6117eb2bd662Svikram 			(void) fclose(tfp);
6118eb2bd662Svikram 			(void) unlink(UFS_SIGNATURE_LIST);
6119eb2bd662Svikram 			bam_error(UFS_BADSIGN, tmpsign);
6120eb2bd662Svikram 			return (NULL);
6121eb2bd662Svikram 		}
6122eb2bd662Svikram 
6123eb2bd662Svikram 		signnum = atoi(numstr);
6124eb2bd662Svikram 		INJECT_ERROR1("NEGATIVE_SIGN", signnum = -1);
6125eb2bd662Svikram 		if (signnum < 0) {
6126eb2bd662Svikram 			(void) fclose(tfp);
6127eb2bd662Svikram 			(void) unlink(UFS_SIGNATURE_LIST);
6128eb2bd662Svikram 			bam_error(UFS_BADSIGN, tmpsign);
6129eb2bd662Svikram 			return (NULL);
6130eb2bd662Svikram 		}
6131eb2bd662Svikram 
6132eb2bd662Svikram 		if (i != signnum) {
6133eb2bd662Svikram 			BAM_DPRINTF((D_FOUND_HOLE_SIGNLIST, fcn, i));
6134eb2bd662Svikram 			break;
6135eb2bd662Svikram 		}
6136eb2bd662Svikram 	}
6137eb2bd662Svikram 
6138eb2bd662Svikram 	(void) fclose(tfp);
6139eb2bd662Svikram 
6140eb2bd662Svikram found:
6141eb2bd662Svikram 	(void) snprintf(tmpsign, sizeof (tmpsign), "rootfs%d", i);
6142eb2bd662Svikram 
6143eb2bd662Svikram 	/* add the ufs signature to the /var/run list of signatures */
6144eb2bd662Svikram 	ret = ufs_add_to_sign_list(tmpsign);
6145eb2bd662Svikram 	INJECT_ERROR1("UFS_ADD_TO_SIGN_LIST", ret = -1);
6146eb2bd662Svikram 	if (ret == -1) {
6147eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6148eb2bd662Svikram 		bam_error(FAILED_ADD_SIGNLIST, tmpsign);
6149eb2bd662Svikram 		return (NULL);
6150eb2bd662Svikram 	}
6151eb2bd662Svikram 
6152eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6153eb2bd662Svikram 
6154eb2bd662Svikram 	return (s_strdup(tmpsign));
6155eb2bd662Svikram }
6156eb2bd662Svikram 
6157eb2bd662Svikram static char *
6158eb2bd662Svikram get_fstype(char *osroot)
6159eb2bd662Svikram {
6160eb2bd662Svikram 	FILE		*mntfp;
6161eb2bd662Svikram 	struct mnttab	mp = {0};
6162eb2bd662Svikram 	struct mnttab	mpref = {0};
6163eb2bd662Svikram 	int		error;
6164eb2bd662Svikram 	int		ret;
6165eb2bd662Svikram 	const char	*fcn = "get_fstype()";
6166eb2bd662Svikram 
6167eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_OSROOT", osroot = NULL);
6168eb2bd662Svikram 	if (osroot == NULL) {
6169eb2bd662Svikram 		bam_error(GET_FSTYPE_ARGS);
6170eb2bd662Svikram 		return (NULL);
6171eb2bd662Svikram 	}
6172eb2bd662Svikram 
6173eb2bd662Svikram 	mntfp = fopen(MNTTAB, "r");
6174eb2bd662Svikram 	error = errno;
6175eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_FOPEN", mntfp = NULL);
6176eb2bd662Svikram 	if (mntfp == NULL) {
6177eb2bd662Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
6178eb2bd662Svikram 		return (NULL);
6179eb2bd662Svikram 	}
6180eb2bd662Svikram 
6181eb2bd662Svikram 	if (*osroot == '\0')
6182eb2bd662Svikram 		mpref.mnt_mountp = "/";
6183eb2bd662Svikram 	else
6184eb2bd662Svikram 		mpref.mnt_mountp = osroot;
6185eb2bd662Svikram 
6186eb2bd662Svikram 	ret = getmntany(mntfp, &mp, &mpref);
6187eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_GETMNTANY", ret = 1);
6188eb2bd662Svikram 	if (ret != 0) {
6189eb2bd662Svikram 		bam_error(MNTTAB_MNTPT_NOT_FOUND, osroot, MNTTAB);
6190eb2bd662Svikram 		(void) fclose(mntfp);
6191eb2bd662Svikram 		return (NULL);
6192eb2bd662Svikram 	}
6193eb2bd662Svikram 	(void) fclose(mntfp);
6194eb2bd662Svikram 
6195eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_NULL", mp.mnt_fstype = NULL);
6196eb2bd662Svikram 	if (mp.mnt_fstype == NULL) {
6197eb2bd662Svikram 		bam_error(MNTTAB_FSTYPE_NULL, osroot);
6198eb2bd662Svikram 		return (NULL);
6199eb2bd662Svikram 	}
6200eb2bd662Svikram 
6201eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6202eb2bd662Svikram 
6203eb2bd662Svikram 	return (s_strdup(mp.mnt_fstype));
6204eb2bd662Svikram }
6205eb2bd662Svikram 
6206eb2bd662Svikram static char *
6207eb2bd662Svikram create_zfs_sign(char *osdev)
6208eb2bd662Svikram {
6209eb2bd662Svikram 	char		tmpsign[PATH_MAX];
6210eb2bd662Svikram 	char		*pool;
6211eb2bd662Svikram 	const char	*fcn = "create_zfs_sign()";
6212eb2bd662Svikram 
6213eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, osdev));
6214eb2bd662Svikram 
6215eb2bd662Svikram 	/*
6216eb2bd662Svikram 	 * First find the pool name
6217eb2bd662Svikram 	 */
6218eb2bd662Svikram 	pool = get_pool(osdev);
6219eb2bd662Svikram 	INJECT_ERROR1("CREATE_ZFS_SIGN_GET_POOL", pool = NULL);
6220eb2bd662Svikram 	if (pool == NULL) {
6221eb2bd662Svikram 		bam_error(GET_POOL_FAILED, osdev);
6222eb2bd662Svikram 		return (NULL);
6223eb2bd662Svikram 	}
6224eb2bd662Svikram 
6225eb2bd662Svikram 	(void) snprintf(tmpsign, sizeof (tmpsign), "pool_%s", pool);
6226eb2bd662Svikram 
6227eb2bd662Svikram 	BAM_DPRINTF((D_CREATED_ZFS_SIGN, fcn, tmpsign));
6228eb2bd662Svikram 
6229eb2bd662Svikram 	free(pool);
6230eb2bd662Svikram 
6231eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6232eb2bd662Svikram 
6233eb2bd662Svikram 	return (s_strdup(tmpsign));
6234eb2bd662Svikram }
6235eb2bd662Svikram 
6236eb2bd662Svikram static char *
6237eb2bd662Svikram create_new_sign(char *osdev, char *fstype)
6238eb2bd662Svikram {
6239eb2bd662Svikram 	char		*sign;
6240eb2bd662Svikram 	const char	*fcn = "create_new_sign()";
6241eb2bd662Svikram 
6242eb2bd662Svikram 	INJECT_ERROR1("NEW_SIGN_FSTYPE", fstype = "foofs");
6243eb2bd662Svikram 
6244eb2bd662Svikram 	if (strcmp(fstype, "zfs") == 0) {
6245eb2bd662Svikram 		BAM_DPRINTF((D_CREATE_NEW_ZFS, fcn));
6246eb2bd662Svikram 		sign = create_zfs_sign(osdev);
6247eb2bd662Svikram 	} else if (strcmp(fstype, "ufs") == 0) {
6248eb2bd662Svikram 		BAM_DPRINTF((D_CREATE_NEW_UFS, fcn));
6249eb2bd662Svikram 		sign = create_ufs_sign();
6250eb2bd662Svikram 	} else {
6251eb2bd662Svikram 		bam_error(GRUBSIGN_NOTSUP, fstype);
6252eb2bd662Svikram 		sign = NULL;
6253eb2bd662Svikram 	}
6254eb2bd662Svikram 
6255eb2bd662Svikram 	BAM_DPRINTF((D_CREATED_NEW_SIGN, fcn, sign ? sign : "<NULL>"));
6256eb2bd662Svikram 	return (sign);
6257eb2bd662Svikram }
6258eb2bd662Svikram 
6259eb2bd662Svikram static int
6260eb2bd662Svikram set_backup_common(char *mntpt, char *sign)
6261eb2bd662Svikram {
6262eb2bd662Svikram 	FILE		*bfp;
6263eb2bd662Svikram 	char		backup[PATH_MAX];
6264eb2bd662Svikram 	char		tmpsign[PATH_MAX];
6265eb2bd662Svikram 	int		error;
6266eb2bd662Svikram 	char		*bdir;
6267eb2bd662Svikram 	char		*backup_dup;
6268eb2bd662Svikram 	struct stat	sb;
6269eb2bd662Svikram 	int		ret;
6270eb2bd662Svikram 	const char	*fcn = "set_backup_common()";
6271eb2bd662Svikram 
6272eb2bd662Svikram 	(void) snprintf(backup, sizeof (backup), "%s%s",
6273eb2bd662Svikram 	    mntpt, GRUBSIGN_BACKUP);
6274eb2bd662Svikram 
6275eb2bd662Svikram 	/* First read the backup */
6276eb2bd662Svikram 	bfp = fopen(backup, "r");
6277eb2bd662Svikram 	if (bfp != NULL) {
6278eb2bd662Svikram 		while (s_fgets(tmpsign, sizeof (tmpsign), bfp)) {
6279eb2bd662Svikram 			if (strcmp(tmpsign, sign) == 0) {
6280eb2bd662Svikram 				BAM_DPRINTF((D_FOUND_IN_BACKUP, fcn, sign));
6281eb2bd662Svikram 				(void) fclose(bfp);
6282eb2bd662Svikram 				return (0);
6283eb2bd662Svikram 			}
6284eb2bd662Svikram 		}
6285eb2bd662Svikram 		(void) fclose(bfp);
6286eb2bd662Svikram 		BAM_DPRINTF((D_NOT_FOUND_IN_EXIST_BACKUP, fcn, sign));
6287eb2bd662Svikram 	} else {
6288eb2bd662Svikram 		BAM_DPRINTF((D_BACKUP_NOT_EXIST, fcn, backup));
6289eb2bd662Svikram 	}
6290eb2bd662Svikram 
6291eb2bd662Svikram 	/*
6292eb2bd662Svikram 	 * Didn't find the correct signature. First create
6293eb2bd662Svikram 	 * the directory if necessary.
6294eb2bd662Svikram 	 */
6295eb2bd662Svikram 
6296eb2bd662Svikram 	/* dirname() modifies its argument so dup it */
6297eb2bd662Svikram 	backup_dup = s_strdup(backup);
6298eb2bd662Svikram 	bdir = dirname(backup_dup);
6299eb2bd662Svikram 	assert(bdir);
6300eb2bd662Svikram 
6301eb2bd662Svikram 	ret = stat(bdir, &sb);
6302eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_STAT", ret = -1);
6303eb2bd662Svikram 	if (ret == -1) {
6304eb2bd662Svikram 		BAM_DPRINTF((D_BACKUP_DIR_NOEXIST, fcn, bdir));
630548847494SEnrico Perla - Sun Microsystems 		ret = mkdirp(bdir, DIR_PERMS);
6306eb2bd662Svikram 		error = errno;
6307eb2bd662Svikram 		INJECT_ERROR1("SET_BACKUP_MKDIRP", ret = -1);
6308eb2bd662Svikram 		if (ret == -1) {
6309eb2bd662Svikram 			bam_error(GRUBSIGN_BACKUP_MKDIRERR,
6310eb2bd662Svikram 			    GRUBSIGN_BACKUP, strerror(error));
6311eb2bd662Svikram 			free(backup_dup);
6312eb2bd662Svikram 			return (-1);
6313eb2bd662Svikram 		}
6314eb2bd662Svikram 	}
6315eb2bd662Svikram 	free(backup_dup);
6316eb2bd662Svikram 
6317eb2bd662Svikram 	/*
6318eb2bd662Svikram 	 * Open the backup in append mode to add the correct
6319eb2bd662Svikram 	 * signature;
6320eb2bd662Svikram 	 */
6321eb2bd662Svikram 	bfp = fopen(backup, "a");
6322eb2bd662Svikram 	error = errno;
6323eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_FOPEN_A", bfp = NULL);
6324eb2bd662Svikram 	if (bfp == NULL) {
6325eb2bd662Svikram 		bam_error(GRUBSIGN_BACKUP_OPENERR,
6326eb2bd662Svikram 		    GRUBSIGN_BACKUP, strerror(error));
6327eb2bd662Svikram 		return (-1);
6328eb2bd662Svikram 	}
6329eb2bd662Svikram 
6330eb2bd662Svikram 	(void) snprintf(tmpsign, sizeof (tmpsign), "%s\n", sign);
6331eb2bd662Svikram 
6332eb2bd662Svikram 	ret = fputs(tmpsign, bfp);
6333eb2bd662Svikram 	error = errno;
6334eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_FPUTS", ret = 0);
6335eb2bd662Svikram 	if (ret != strlen(tmpsign)) {
6336eb2bd662Svikram 		bam_error(GRUBSIGN_BACKUP_WRITEERR,
6337eb2bd662Svikram 		    GRUBSIGN_BACKUP, strerror(error));
6338eb2bd662Svikram 		(void) fclose(bfp);
6339eb2bd662Svikram 		return (-1);
6340eb2bd662Svikram 	}
6341eb2bd662Svikram 
6342eb2bd662Svikram 	(void) fclose(bfp);
6343eb2bd662Svikram 
6344eb2bd662Svikram 	if (bam_verbose)
6345eb2bd662Svikram 		bam_print(GRUBSIGN_BACKUP_UPDATED, GRUBSIGN_BACKUP);
6346eb2bd662Svikram 
6347eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6348eb2bd662Svikram 
6349eb2bd662Svikram 	return (0);
6350eb2bd662Svikram }
6351eb2bd662Svikram 
6352eb2bd662Svikram static int
6353eb2bd662Svikram set_backup_ufs(char *osroot, char *sign)
6354eb2bd662Svikram {
6355eb2bd662Svikram 	const char	*fcn = "set_backup_ufs()";
6356eb2bd662Svikram 
6357eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, sign));
6358eb2bd662Svikram 	return (set_backup_common(osroot, sign));
6359eb2bd662Svikram }
6360eb2bd662Svikram 
6361eb2bd662Svikram static int
6362eb2bd662Svikram set_backup_zfs(char *osdev, char *sign)
6363eb2bd662Svikram {
6364eb2bd662Svikram 	char		*pool;
6365eb2bd662Svikram 	char		*mntpt;
6366eb2bd662Svikram 	zfs_mnted_t	mnted;
6367eb2bd662Svikram 	int		ret;
6368eb2bd662Svikram 	const char	*fcn = "set_backup_zfs()";
6369eb2bd662Svikram 
6370eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osdev, sign));
6371eb2bd662Svikram 
6372eb2bd662Svikram 	pool = get_pool(osdev);
6373eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_GET_POOL", pool = NULL);
6374eb2bd662Svikram 	if (pool == NULL) {
6375eb2bd662Svikram 		bam_error(GET_POOL_FAILED, osdev);
6376eb2bd662Svikram 		return (-1);
6377eb2bd662Svikram 	}
6378eb2bd662Svikram 
6379eb2bd662Svikram 	mntpt = mount_top_dataset(pool, &mnted);
6380eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_MOUNT_DATASET", mntpt = NULL);
6381eb2bd662Svikram 	if (mntpt == NULL) {
6382eb2bd662Svikram 		bam_error(FAIL_MNT_TOP_DATASET, pool);
6383eb2bd662Svikram 		free(pool);
6384eb2bd662Svikram 		return (-1);
6385eb2bd662Svikram 	}
6386eb2bd662Svikram 
6387eb2bd662Svikram 	ret = set_backup_common(mntpt, sign);
6388eb2bd662Svikram 
6389eb2bd662Svikram 	(void) umount_top_dataset(pool, mnted, mntpt);
6390eb2bd662Svikram 
6391eb2bd662Svikram 	free(pool);
6392eb2bd662Svikram 
6393eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_ZFS_FAIL", ret = 1);
6394eb2bd662Svikram 	if (ret == 0) {
6395eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6396eb2bd662Svikram 	} else {
6397eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6398eb2bd662Svikram 	}
6399eb2bd662Svikram 
6400eb2bd662Svikram 	return (ret);
6401eb2bd662Svikram }
6402eb2bd662Svikram 
6403eb2bd662Svikram static int
6404eb2bd662Svikram set_backup(char *osroot, char *osdev, char *sign, char *fstype)
6405eb2bd662Svikram {
6406eb2bd662Svikram 	const char	*fcn = "set_backup()";
6407eb2bd662Svikram 	int		ret;
6408eb2bd662Svikram 
6409eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_FSTYPE", fstype = "foofs");
6410eb2bd662Svikram 
6411eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0) {
6412eb2bd662Svikram 		BAM_DPRINTF((D_SET_BACKUP_UFS, fcn));
6413eb2bd662Svikram 		ret = set_backup_ufs(osroot, sign);
6414eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0) {
6415eb2bd662Svikram 		BAM_DPRINTF((D_SET_BACKUP_ZFS, fcn));
6416eb2bd662Svikram 		ret = set_backup_zfs(osdev, sign);
6417eb2bd662Svikram 	} else {
6418eb2bd662Svikram 		bam_error(GRUBSIGN_NOTSUP, fstype);
6419eb2bd662Svikram 		ret = -1;
6420eb2bd662Svikram 	}
6421eb2bd662Svikram 
6422eb2bd662Svikram 	if (ret == 0) {
6423eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6424eb2bd662Svikram 	} else {
6425eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6426eb2bd662Svikram 	}
6427eb2bd662Svikram 
6428eb2bd662Svikram 	return (ret);
6429eb2bd662Svikram }
6430eb2bd662Svikram 
6431eb2bd662Svikram static int
6432eb2bd662Svikram set_primary_common(char *mntpt, char *sign)
6433eb2bd662Svikram {
6434eb2bd662Svikram 	char		signfile[PATH_MAX];
6435eb2bd662Svikram 	char		signdir[PATH_MAX];
6436eb2bd662Svikram 	struct stat	sb;
6437eb2bd662Svikram 	int		fd;
6438eb2bd662Svikram 	int		error;
6439eb2bd662Svikram 	int		ret;
6440eb2bd662Svikram 	const char	*fcn = "set_primary_common()";
6441eb2bd662Svikram 
6442eb2bd662Svikram 	(void) snprintf(signfile, sizeof (signfile), "%s/%s/%s",
6443eb2bd662Svikram 	    mntpt, GRUBSIGN_DIR, sign);
6444eb2bd662Svikram 
6445eb2bd662Svikram 	if (stat(signfile, &sb) != -1) {
6446eb2bd662Svikram 		if (bam_verbose)
6447eb2bd662Svikram 			bam_print(PRIMARY_SIGN_EXISTS, sign);
6448eb2bd662Svikram 		return (0);
6449eb2bd662Svikram 	} else {
6450eb2bd662Svikram 		BAM_DPRINTF((D_PRIMARY_NOT_EXIST, fcn, signfile));
6451eb2bd662Svikram 	}
6452eb2bd662Svikram 
6453eb2bd662Svikram 	(void) snprintf(signdir, sizeof (signdir), "%s/%s",
6454eb2bd662Svikram 	    mntpt, GRUBSIGN_DIR);
6455eb2bd662Svikram 
6456eb2bd662Svikram 	if (stat(signdir, &sb) == -1) {
6457eb2bd662Svikram 		BAM_DPRINTF((D_PRIMARY_DIR_NOEXIST, fcn, signdir));
645848847494SEnrico Perla - Sun Microsystems 		ret = mkdirp(signdir, DIR_PERMS);
6459eb2bd662Svikram 		error = errno;
6460eb2bd662Svikram 		INJECT_ERROR1("SET_PRIMARY_MKDIRP", ret = -1);
6461eb2bd662Svikram 		if (ret == -1) {
6462eb2bd662Svikram 			bam_error(GRUBSIGN_MKDIR_ERR, signdir, strerror(errno));
6463eb2bd662Svikram 			return (-1);
6464eb2bd662Svikram 		}
6465eb2bd662Svikram 	}
6466eb2bd662Svikram 
6467eb2bd662Svikram 	fd = open(signfile, O_RDWR|O_CREAT|O_TRUNC, 0444);
6468eb2bd662Svikram 	error = errno;
6469eb2bd662Svikram 	INJECT_ERROR1("PRIMARY_SIGN_CREAT", fd = -1);
6470eb2bd662Svikram 	if (fd == -1) {
6471eb2bd662Svikram 		bam_error(GRUBSIGN_PRIMARY_CREATERR, signfile, strerror(error));
6472eb2bd662Svikram 		return (-1);
6473eb2bd662Svikram 	}
6474eb2bd662Svikram 
6475eb2bd662Svikram 	ret = fsync(fd);
6476eb2bd662Svikram 	error = errno;
6477eb2bd662Svikram 	INJECT_ERROR1("PRIMARY_FSYNC", ret = -1);
6478eb2bd662Svikram 	if (ret != 0) {
6479eb2bd662Svikram 		bam_error(GRUBSIGN_PRIMARY_SYNCERR, signfile, strerror(error));
6480eb2bd662Svikram 	}
6481eb2bd662Svikram 
6482eb2bd662Svikram 	(void) close(fd);
6483eb2bd662Svikram 
6484eb2bd662Svikram 	if (bam_verbose)
6485eb2bd662Svikram 		bam_print(GRUBSIGN_CREATED_PRIMARY, signfile);
6486eb2bd662Svikram 
6487eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6488eb2bd662Svikram 
6489eb2bd662Svikram 	return (0);
6490eb2bd662Svikram }
6491eb2bd662Svikram 
6492eb2bd662Svikram static int
6493eb2bd662Svikram set_primary_ufs(char *osroot, char *sign)
6494eb2bd662Svikram {
6495eb2bd662Svikram 	const char	*fcn = "set_primary_ufs()";
6496eb2bd662Svikram 
6497eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, sign));
6498eb2bd662Svikram 	return (set_primary_common(osroot, sign));
6499eb2bd662Svikram }
6500eb2bd662Svikram 
6501eb2bd662Svikram static int
6502eb2bd662Svikram set_primary_zfs(char *osdev, char *sign)
6503eb2bd662Svikram {
6504eb2bd662Svikram 	char		*pool;
6505eb2bd662Svikram 	char		*mntpt;
6506eb2bd662Svikram 	zfs_mnted_t	mnted;
6507eb2bd662Svikram 	int		ret;
6508eb2bd662Svikram 	const char	*fcn = "set_primary_zfs()";
6509eb2bd662Svikram 
6510eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osdev, sign));
6511eb2bd662Svikram 
6512eb2bd662Svikram 	pool = get_pool(osdev);
6513eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_GET_POOL", pool = NULL);
6514eb2bd662Svikram 	if (pool == NULL) {
6515eb2bd662Svikram 		bam_error(GET_POOL_FAILED, osdev);
6516eb2bd662Svikram 		return (-1);
6517eb2bd662Svikram 	}
6518eb2bd662Svikram 
6519eb2bd662Svikram 	/* Pool name must exist in the sign */
6520eb2bd662Svikram 	ret = (strstr(sign, pool) != NULL);
6521eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_POOL_SIGN_INCOMPAT", ret = 0);
6522eb2bd662Svikram 	if (ret == 0) {
6523eb2bd662Svikram 		bam_error(POOL_SIGN_INCOMPAT, pool, sign);
6524eb2bd662Svikram 		free(pool);
6525eb2bd662Svikram 		return (-1);
6526eb2bd662Svikram 	}
6527eb2bd662Svikram 
6528eb2bd662Svikram 	mntpt = mount_top_dataset(pool, &mnted);
6529eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_MOUNT_DATASET", mntpt = NULL);
6530eb2bd662Svikram 	if (mntpt == NULL) {
6531eb2bd662Svikram 		bam_error(FAIL_MNT_TOP_DATASET, pool);
6532eb2bd662Svikram 		free(pool);
6533eb2bd662Svikram 		return (-1);
6534eb2bd662Svikram 	}
6535eb2bd662Svikram 
6536eb2bd662Svikram 	ret = set_primary_common(mntpt, sign);
6537eb2bd662Svikram 
6538eb2bd662Svikram 	(void) umount_top_dataset(pool, mnted, mntpt);
6539eb2bd662Svikram 
6540eb2bd662Svikram 	free(pool);
6541eb2bd662Svikram 
6542eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_FAIL", ret = 1);
6543eb2bd662Svikram 	if (ret == 0) {
6544eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6545eb2bd662Svikram 	} else {
6546eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6547eb2bd662Svikram 	}
6548eb2bd662Svikram 
6549eb2bd662Svikram 	return (ret);
6550eb2bd662Svikram }
6551eb2bd662Svikram 
6552eb2bd662Svikram static int
6553eb2bd662Svikram set_primary(char *osroot, char *osdev, char *sign, char *fstype)
6554eb2bd662Svikram {
6555eb2bd662Svikram 	const char	*fcn = "set_primary()";
6556eb2bd662Svikram 	int		ret;
6557eb2bd662Svikram 
6558eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_FSTYPE", fstype = "foofs");
6559eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0) {
6560eb2bd662Svikram 		BAM_DPRINTF((D_SET_PRIMARY_UFS, fcn));
6561eb2bd662Svikram 		ret = set_primary_ufs(osroot, sign);
6562eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0) {
6563eb2bd662Svikram 		BAM_DPRINTF((D_SET_PRIMARY_ZFS, fcn));
6564eb2bd662Svikram 		ret = set_primary_zfs(osdev, sign);
6565eb2bd662Svikram 	} else {
6566eb2bd662Svikram 		bam_error(GRUBSIGN_NOTSUP, fstype);
6567eb2bd662Svikram 		ret = -1;
6568eb2bd662Svikram 	}
6569eb2bd662Svikram 
6570eb2bd662Svikram 	if (ret == 0) {
6571eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6572eb2bd662Svikram 	} else {
6573eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6574eb2bd662Svikram 	}
6575eb2bd662Svikram 
6576eb2bd662Svikram 	return (ret);
6577eb2bd662Svikram }
6578eb2bd662Svikram 
6579eb2bd662Svikram static int
6580eb2bd662Svikram ufs_add_to_sign_list(char *sign)
6581eb2bd662Svikram {
6582eb2bd662Svikram 	FILE		*tfp;
6583eb2bd662Svikram 	char		signline[MAXNAMELEN];
6584eb2bd662Svikram 	char		cmd[PATH_MAX];
6585eb2bd662Svikram 	int		ret;
6586eb2bd662Svikram 	int		error;
6587eb2bd662Svikram 	const char	*fcn = "ufs_add_to_sign_list()";
6588eb2bd662Svikram 
6589eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_NOT_UFS", sign = "pool_rpool5");
6590eb2bd662Svikram 	if (strncmp(sign, GRUBSIGN_UFS_PREFIX,
6591eb2bd662Svikram 	    strlen(GRUBSIGN_UFS_PREFIX)) != 0) {
6592eb2bd662Svikram 		bam_error(INVALID_UFS_SIGN, sign);
6593eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6594eb2bd662Svikram 		return (-1);
6595eb2bd662Svikram 	}
6596eb2bd662Svikram 
6597eb2bd662Svikram 	/*
6598eb2bd662Svikram 	 * most failures in this routine are not a fatal error
6599eb2bd662Svikram 	 * We simply unlink the /var/run file and continue
6600eb2bd662Svikram 	 */
6601eb2bd662Svikram 
6602eb2bd662Svikram 	ret = rename(UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST".tmp");
6603eb2bd662Svikram 	error = errno;
6604eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_RENAME", ret = -1);
6605eb2bd662Svikram 	if (ret == -1) {
6606eb2bd662Svikram 		bam_error(RENAME_FAIL, UFS_SIGNATURE_LIST".tmp",
6607eb2bd662Svikram 		    strerror(error));
6608eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6609eb2bd662Svikram 		return (0);
6610eb2bd662Svikram 	}
6611eb2bd662Svikram 
6612eb2bd662Svikram 	tfp = fopen(UFS_SIGNATURE_LIST".tmp", "a");
6613eb2bd662Svikram 	error = errno;
6614eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FOPEN", tfp = NULL);
6615eb2bd662Svikram 	if (tfp == NULL) {
6616eb2bd662Svikram 		bam_error(OPEN_FAIL, UFS_SIGNATURE_LIST".tmp", strerror(error));
6617eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6618eb2bd662Svikram 		return (0);
6619eb2bd662Svikram 	}
6620eb2bd662Svikram 
6621eb2bd662Svikram 	(void) snprintf(signline, sizeof (signline), "%s\n", sign);
6622eb2bd662Svikram 
6623eb2bd662Svikram 	ret = fputs(signline, tfp);
6624eb2bd662Svikram 	error = errno;
6625eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FPUTS", ret = 0);
6626eb2bd662Svikram 	if (ret != strlen(signline)) {
6627eb2bd662Svikram 		bam_error(SIGN_LIST_FPUTS_ERR, sign, strerror(error));
6628eb2bd662Svikram 		(void) fclose(tfp);
6629eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6630eb2bd662Svikram 		return (0);
6631eb2bd662Svikram 	}
6632eb2bd662Svikram 
6633eb2bd662Svikram 	ret = fclose(tfp);
6634eb2bd662Svikram 	error = errno;
6635eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FCLOSE", ret = EOF);
6636eb2bd662Svikram 	if (ret == EOF) {
6637eb2bd662Svikram 		bam_error(CLOSE_FAIL, UFS_SIGNATURE_LIST".tmp",
6638eb2bd662Svikram 		    strerror(error));
6639eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6640eb2bd662Svikram 		return (0);
6641eb2bd662Svikram 	}
6642eb2bd662Svikram 
6643eb2bd662Svikram 	/* Sort the list again */
6644eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6645eb2bd662Svikram 	    "/usr/bin/sort -u %s.tmp > %s.sorted",
6646eb2bd662Svikram 	    UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST);
6647eb2bd662Svikram 
6648eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
6649eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_SORT", ret = 1);
6650eb2bd662Svikram 	if (ret != 0) {
6651eb2bd662Svikram 		bam_error(GRUBSIGN_SORT_FAILED);
6652eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
6653eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6654eb2bd662Svikram 		return (0);
6655eb2bd662Svikram 	}
6656eb2bd662Svikram 
6657eb2bd662Svikram 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
6658eb2bd662Svikram 
6659eb2bd662Svikram 	ret = rename(UFS_SIGNATURE_LIST".sorted", UFS_SIGNATURE_LIST);
6660eb2bd662Svikram 	error = errno;
6661eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_RENAME2", ret = -1);
6662eb2bd662Svikram 	if (ret == -1) {
6663eb2bd662Svikram 		bam_error(RENAME_FAIL, UFS_SIGNATURE_LIST, strerror(error));
6664eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
6665eb2bd662Svikram 		return (0);
6666eb2bd662Svikram 	}
6667eb2bd662Svikram 
6668eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6669eb2bd662Svikram 
6670eb2bd662Svikram 	return (0);
6671eb2bd662Svikram }
6672eb2bd662Svikram 
6673eb2bd662Svikram static int
6674eb2bd662Svikram set_signature(char *osroot, char *osdev, char *sign, char *fstype)
6675eb2bd662Svikram {
6676eb2bd662Svikram 	int		ret;
6677eb2bd662Svikram 	const char	*fcn = "set_signature()";
6678eb2bd662Svikram 
6679eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY4, fcn, osroot, osdev, sign, fstype));
6680eb2bd662Svikram 
6681eb2bd662Svikram 	ret = set_backup(osroot, osdev, sign, fstype);
6682eb2bd662Svikram 	INJECT_ERROR1("SET_SIGNATURE_BACKUP", ret = -1);
6683eb2bd662Svikram 	if (ret == -1) {
6684eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6685eb2bd662Svikram 		bam_error(SET_BACKUP_FAILED, sign, osroot, osdev);
6686eb2bd662Svikram 		return (-1);
6687eb2bd662Svikram 	}
6688eb2bd662Svikram 
6689eb2bd662Svikram 	ret = set_primary(osroot, osdev, sign, fstype);
6690eb2bd662Svikram 	INJECT_ERROR1("SET_SIGNATURE_PRIMARY", ret = -1);
6691eb2bd662Svikram 
6692eb2bd662Svikram 	if (ret == 0) {
6693eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6694eb2bd662Svikram 	} else {
6695eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6696eb2bd662Svikram 		bam_error(SET_PRIMARY_FAILED, sign, osroot, osdev);
6697eb2bd662Svikram 
6698eb2bd662Svikram 	}
6699eb2bd662Svikram 	return (ret);
6700eb2bd662Svikram }
6701eb2bd662Svikram 
6702eb2bd662Svikram char *
6703eb2bd662Svikram get_grubsign(char *osroot, char *osdev)
6704eb2bd662Svikram {
6705eb2bd662Svikram 	char		*grubsign;	/* (<sign>,#,#) */
6706eb2bd662Svikram 	char		*slice;
6707eb2bd662Svikram 	int		fdiskpart;
6708eb2bd662Svikram 	char		*sign;
6709eb2bd662Svikram 	char		*fstype;
6710eb2bd662Svikram 	int		ret;
6711eb2bd662Svikram 	const char	*fcn = "get_grubsign()";
6712eb2bd662Svikram 
6713eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, osdev));
6714eb2bd662Svikram 
6715eb2bd662Svikram 	fstype = get_fstype(osroot);
6716eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBSIGN_FSTYPE", fstype = NULL);
6717eb2bd662Svikram 	if (fstype == NULL) {
6718eb2bd662Svikram 		bam_error(GET_FSTYPE_FAILED, osroot);
6719eb2bd662Svikram 		return (NULL);
6720eb2bd662Svikram 	}
6721eb2bd662Svikram 
6722eb2bd662Svikram 	sign = find_existing_sign(osroot, osdev, fstype);
6723eb2bd662Svikram 	INJECT_ERROR1("FIND_EXISTING_SIGN", sign = NULL);
6724eb2bd662Svikram 	if (sign == NULL) {
6725eb2bd662Svikram 		BAM_DPRINTF((D_GET_GRUBSIGN_NO_EXISTING, fcn, osroot, osdev));
6726eb2bd662Svikram 		sign = create_new_sign(osdev, fstype);
6727eb2bd662Svikram 		INJECT_ERROR1("CREATE_NEW_SIGN", sign = NULL);
6728eb2bd662Svikram 		if (sign == NULL) {
6729eb2bd662Svikram 			bam_error(GRUBSIGN_CREATE_FAIL, osdev);
6730eb2bd662Svikram 			free(fstype);
6731eb2bd662Svikram 			return (NULL);
6732eb2bd662Svikram 		}
6733eb2bd662Svikram 	}
6734eb2bd662Svikram 
6735eb2bd662Svikram 	ret = set_signature(osroot, osdev, sign, fstype);
6736eb2bd662Svikram 	INJECT_ERROR1("SET_SIGNATURE_FAIL", ret = -1);
6737eb2bd662Svikram 	if (ret == -1) {
6738eb2bd662Svikram 		bam_error(GRUBSIGN_WRITE_FAIL, osdev);
6739eb2bd662Svikram 		free(sign);
6740eb2bd662Svikram 		free(fstype);
6741eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6742eb2bd662Svikram 		return (NULL);
6743eb2bd662Svikram 	}
6744eb2bd662Svikram 
6745eb2bd662Svikram 	free(fstype);
6746eb2bd662Svikram 
6747eb2bd662Svikram 	if (bam_verbose)
6748eb2bd662Svikram 		bam_print(GRUBSIGN_FOUND_OR_CREATED, sign, osdev);
6749eb2bd662Svikram 
6750eb2bd662Svikram 	fdiskpart = get_partition(osdev);
6751eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBSIGN_FDISK", fdiskpart = -1);
6752eb2bd662Svikram 	if (fdiskpart == -1) {
6753eb2bd662Svikram 		bam_error(FDISKPART_FAIL, osdev);
6754eb2bd662Svikram 		free(sign);
6755eb2bd662Svikram 		return (NULL);
6756eb2bd662Svikram 	}
6757eb2bd662Svikram 
6758eb2bd662Svikram 	slice = strrchr(osdev, 's');
6759eb2bd662Svikram 
6760eb2bd662Svikram 	grubsign = s_calloc(1, MAXNAMELEN + 10);
6761eb2bd662Svikram 	if (slice) {
6762eb2bd662Svikram 		(void) snprintf(grubsign, MAXNAMELEN + 10, "(%s,%d,%c)",
6763eb2bd662Svikram 		    sign, fdiskpart, slice[1] + 'a' - '0');
6764eb2bd662Svikram 	} else
6765eb2bd662Svikram 		(void) snprintf(grubsign, MAXNAMELEN + 10, "(%s,%d)",
6766eb2bd662Svikram 		    sign, fdiskpart);
6767eb2bd662Svikram 
6768eb2bd662Svikram 	free(sign);
6769eb2bd662Svikram 
6770eb2bd662Svikram 	BAM_DPRINTF((D_GET_GRUBSIGN_SUCCESS, fcn, grubsign));
6771eb2bd662Svikram 
6772eb2bd662Svikram 	return (grubsign);
67737c478bd9Sstevel@tonic-gate }
67747c478bd9Sstevel@tonic-gate 
6775ee3b8144Sszhou static char *
6776ee3b8144Sszhou get_title(char *rootdir)
67777c478bd9Sstevel@tonic-gate {
6778eb2bd662Svikram 	static char	title[80];
6779eb2bd662Svikram 	char		*cp = NULL;
6780eb2bd662Svikram 	char		release[PATH_MAX];
67817c478bd9Sstevel@tonic-gate 	FILE		*fp;
6782eb2bd662Svikram 	const char	*fcn = "get_title()";
67837c478bd9Sstevel@tonic-gate 
67847c478bd9Sstevel@tonic-gate 	/* open the /etc/release file */
67857c478bd9Sstevel@tonic-gate 	(void) snprintf(release, sizeof (release), "%s/etc/release", rootdir);
67867c478bd9Sstevel@tonic-gate 
67877c478bd9Sstevel@tonic-gate 	fp = fopen(release, "r");
6788eb2bd662Svikram 	if (fp == NULL) {
6789eb2bd662Svikram 		bam_error(OPEN_FAIL, release, strerror(errno));
6790eb2bd662Svikram 		cp = NULL;
6791eb2bd662Svikram 		goto out;
6792eb2bd662Svikram 	}
67937c478bd9Sstevel@tonic-gate 
67947c478bd9Sstevel@tonic-gate 	while (s_fgets(title, sizeof (title), fp) != NULL) {
67957c478bd9Sstevel@tonic-gate 		cp = strstr(title, "Solaris");
67967c478bd9Sstevel@tonic-gate 		if (cp)
67977c478bd9Sstevel@tonic-gate 			break;
67987c478bd9Sstevel@tonic-gate 	}
67997c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
6800eb2bd662Svikram 
6801eb2bd662Svikram out:
6802eb2bd662Svikram 	cp = cp ? cp : "Solaris";
6803eb2bd662Svikram 
6804eb2bd662Svikram 	BAM_DPRINTF((D_GET_TITLE, fcn, cp));
6805eb2bd662Svikram 
6806eb2bd662Svikram 	return (cp);
68077c478bd9Sstevel@tonic-gate }
68087c478bd9Sstevel@tonic-gate 
6809ae115bc7Smrj char *
68107c478bd9Sstevel@tonic-gate get_special(char *mountp)
68117c478bd9Sstevel@tonic-gate {
68127c478bd9Sstevel@tonic-gate 	FILE		*mntfp;
6813eb2bd662Svikram 	struct mnttab	mp = {0};
6814eb2bd662Svikram 	struct mnttab	mpref = {0};
6815eb2bd662Svikram 	int		error;
6816eb2bd662Svikram 	int		ret;
6817eb2bd662Svikram 	const char 	*fcn = "get_special()";
6818eb2bd662Svikram 
6819eb2bd662Svikram 	INJECT_ERROR1("GET_SPECIAL_MNTPT", mountp = NULL);
6820eb2bd662Svikram 	if (mountp == NULL) {
6821eb2bd662Svikram 		bam_error(GET_SPECIAL_NULL_MNTPT);
6822eb2bd662Svikram 		return (NULL);
6823eb2bd662Svikram 	}
68247c478bd9Sstevel@tonic-gate 
68257c478bd9Sstevel@tonic-gate 	mntfp = fopen(MNTTAB, "r");
6826eb2bd662Svikram 	error = errno;
6827eb2bd662Svikram 	INJECT_ERROR1("GET_SPECIAL_MNTTAB_OPEN", mntfp = NULL);
68287c478bd9Sstevel@tonic-gate 	if (mntfp == NULL) {
6829eb2bd662Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
6830eb2bd662Svikram 		return (NULL);
68317c478bd9Sstevel@tonic-gate 	}
68327c478bd9Sstevel@tonic-gate 
68337c478bd9Sstevel@tonic-gate 	if (*mountp == '\0')
68347c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = "/";
68357c478bd9Sstevel@tonic-gate 	else
68367c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = mountp;
6837eb2bd662Svikram 
6838eb2bd662Svikram 	ret = getmntany(mntfp, &mp, &mpref);
6839eb2bd662Svikram 	INJECT_ERROR1("GET_SPECIAL_MNTTAB_SEARCH", ret = 1);
6840eb2bd662Svikram 	if (ret != 0) {
68417c478bd9Sstevel@tonic-gate 		(void) fclose(mntfp);
6842eb2bd662Svikram 		BAM_DPRINTF((D_GET_SPECIAL_NOT_IN_MNTTAB, fcn, mountp));
68437c478bd9Sstevel@tonic-gate 		return (NULL);
68447c478bd9Sstevel@tonic-gate 	}
68457c478bd9Sstevel@tonic-gate 	(void) fclose(mntfp);
68467c478bd9Sstevel@tonic-gate 
6847eb2bd662Svikram 	BAM_DPRINTF((D_GET_SPECIAL, fcn, mp.mnt_special));
6848eb2bd662Svikram 
68497c478bd9Sstevel@tonic-gate 	return (s_strdup(mp.mnt_special));
68507c478bd9Sstevel@tonic-gate }
68517c478bd9Sstevel@tonic-gate 
6852eb2bd662Svikram static void
6853eb2bd662Svikram free_physarray(char **physarray, int n)
68547c478bd9Sstevel@tonic-gate {
6855eb2bd662Svikram 	int			i;
6856eb2bd662Svikram 	const char		*fcn = "free_physarray()";
68577c478bd9Sstevel@tonic-gate 
6858eb2bd662Svikram 	assert(physarray);
6859eb2bd662Svikram 	assert(n);
6860eb2bd662Svikram 
6861eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY_N1, fcn, n));
6862eb2bd662Svikram 
6863eb2bd662Svikram 	for (i = 0; i < n; i++) {
6864eb2bd662Svikram 		free(physarray[i]);
68657c478bd9Sstevel@tonic-gate 	}
6866eb2bd662Svikram 	free(physarray);
6867eb2bd662Svikram 
6868eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6869eb2bd662Svikram }
6870eb2bd662Svikram 
6871eb2bd662Svikram static int
6872eb2bd662Svikram zfs_get_physical(char *special, char ***physarray, int *n)
6873eb2bd662Svikram {
6874eb2bd662Svikram 	char			sdup[PATH_MAX];
6875eb2bd662Svikram 	char			cmd[PATH_MAX];
6876eb2bd662Svikram 	char			dsk[PATH_MAX];
6877eb2bd662Svikram 	char			*pool;
6878eb2bd662Svikram 	filelist_t		flist = {0};
6879eb2bd662Svikram 	line_t			*lp;
6880eb2bd662Svikram 	line_t			*startlp;
6881eb2bd662Svikram 	char			*comp1;
6882eb2bd662Svikram 	int			i;
6883eb2bd662Svikram 	int			ret;
6884eb2bd662Svikram 	const char		*fcn = "zfs_get_physical()";
6885eb2bd662Svikram 
6886eb2bd662Svikram 	assert(special);
6887eb2bd662Svikram 
6888eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, special));
6889eb2bd662Svikram 
6890eb2bd662Svikram 	INJECT_ERROR1("INVALID_ZFS_SPECIAL", special = "/foo");
6891eb2bd662Svikram 	if (special[0] == '/') {
6892eb2bd662Svikram 		bam_error(INVALID_ZFS_SPECIAL, special);
6893eb2bd662Svikram 		return (-1);
6894eb2bd662Svikram 	}
6895eb2bd662Svikram 
6896eb2bd662Svikram 	(void) strlcpy(sdup, special, sizeof (sdup));
6897eb2bd662Svikram 
6898eb2bd662Svikram 	pool = strtok(sdup, "/");
6899eb2bd662Svikram 	INJECT_ERROR1("ZFS_GET_PHYS_POOL", pool = NULL);
6900eb2bd662Svikram 	if (pool == NULL) {
6901eb2bd662Svikram 		bam_error(CANT_FIND_POOL_FROM_SPECIAL, special);
6902eb2bd662Svikram 		return (-1);
6903eb2bd662Svikram 	}
6904eb2bd662Svikram 
6905eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/zpool status %s", pool);
6906eb2bd662Svikram 
6907eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
6908eb2bd662Svikram 	INJECT_ERROR1("ZFS_GET_PHYS_STATUS", ret = 1);
6909eb2bd662Svikram 	if (ret != 0) {
6910eb2bd662Svikram 		bam_error(ZFS_GET_POOL_STATUS, pool);
6911eb2bd662Svikram 		return (-1);
6912eb2bd662Svikram 	}
6913eb2bd662Svikram 
6914eb2bd662Svikram 	INJECT_ERROR1("ZFS_GET_PHYS_STATUS_OUT", flist.head = NULL);
6915eb2bd662Svikram 	if (flist.head == NULL) {
6916eb2bd662Svikram 		bam_error(BAD_ZPOOL_STATUS, pool);
6917eb2bd662Svikram 		filelist_free(&flist);
6918eb2bd662Svikram 		return (-1);
6919eb2bd662Svikram 	}
6920eb2bd662Svikram 
6921eb2bd662Svikram 	for (lp = flist.head; lp; lp = lp->next) {
6922eb2bd662Svikram 		BAM_DPRINTF((D_STRTOK_ZPOOL_STATUS, fcn, lp->line));
6923eb2bd662Svikram 		comp1 = strtok(lp->line, " \t");
6924eb2bd662Svikram 		if (comp1 == NULL) {
6925eb2bd662Svikram 			free(lp->line);
6926eb2bd662Svikram 			lp->line = NULL;
6927eb2bd662Svikram 		} else {
6928eb2bd662Svikram 			comp1 = s_strdup(comp1);
6929eb2bd662Svikram 			free(lp->line);
6930eb2bd662Svikram 			lp->line = comp1;
6931eb2bd662Svikram 		}
6932eb2bd662Svikram 	}
6933eb2bd662Svikram 
6934eb2bd662Svikram 	for (lp = flist.head; lp; lp = lp->next) {
6935eb2bd662Svikram 		if (lp->line == NULL)
6936eb2bd662Svikram 			continue;
6937eb2bd662Svikram 		if (strcmp(lp->line, pool) == 0) {
6938eb2bd662Svikram 			BAM_DPRINTF((D_FOUND_POOL_IN_ZPOOL_STATUS, fcn, pool));
6939eb2bd662Svikram 			break;
6940eb2bd662Svikram 		}
6941eb2bd662Svikram 	}
6942eb2bd662Svikram 
6943eb2bd662Svikram 	if (lp == NULL) {
6944eb2bd662Svikram 		bam_error(NO_POOL_IN_ZPOOL_STATUS, pool);
6945eb2bd662Svikram 		filelist_free(&flist);
6946eb2bd662Svikram 		return (-1);
6947eb2bd662Svikram 	}
6948eb2bd662Svikram 
6949eb2bd662Svikram 	startlp = lp->next;
6950eb2bd662Svikram 	for (i = 0, lp = startlp; lp; lp = lp->next) {
6951eb2bd662Svikram 		if (lp->line == NULL)
6952eb2bd662Svikram 			continue;
6953eb2bd662Svikram 		if (strcmp(lp->line, "mirror") == 0)
6954eb2bd662Svikram 			continue;
6955eb2bd662Svikram 		if (lp->line[0] == '\0' || strcmp(lp->line, "errors:") == 0)
6956eb2bd662Svikram 			break;
6957eb2bd662Svikram 		i++;
6958eb2bd662Svikram 		BAM_DPRINTF((D_COUNTING_ZFS_PHYS, fcn, i));
6959eb2bd662Svikram 	}
6960eb2bd662Svikram 
6961eb2bd662Svikram 	if (i == 0) {
6962eb2bd662Svikram 		bam_error(NO_PHYS_IN_ZPOOL_STATUS, pool);
6963eb2bd662Svikram 		filelist_free(&flist);
6964eb2bd662Svikram 		return (-1);
6965eb2bd662Svikram 	}
6966eb2bd662Svikram 
6967eb2bd662Svikram 	*n = i;
6968eb2bd662Svikram 	*physarray = s_calloc(*n, sizeof (char *));
6969eb2bd662Svikram 	for (i = 0, lp = startlp; lp; lp = lp->next) {
6970eb2bd662Svikram 		if (lp->line == NULL)
6971eb2bd662Svikram 			continue;
6972eb2bd662Svikram 		if (strcmp(lp->line, "mirror") == 0)
6973eb2bd662Svikram 			continue;
6974eb2bd662Svikram 		if (strcmp(lp->line, "errors:") == 0)
6975eb2bd662Svikram 			break;
6976eb2bd662Svikram 		if (strncmp(lp->line, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
6977eb2bd662Svikram 		    strncmp(lp->line, "/dev/rdsk/",
6978eb2bd662Svikram 		    strlen("/dev/rdsk/")) != 0)  {
6979eb2bd662Svikram 			(void) snprintf(dsk, sizeof (dsk), "/dev/dsk/%s",
6980eb2bd662Svikram 			    lp->line);
6981eb2bd662Svikram 		} else {
6982eb2bd662Svikram 			(void) strlcpy(dsk, lp->line, sizeof (dsk));
6983eb2bd662Svikram 		}
6984eb2bd662Svikram 		BAM_DPRINTF((D_ADDING_ZFS_PHYS, fcn, dsk, pool));
6985eb2bd662Svikram 		(*physarray)[i++] = s_strdup(dsk);
6986eb2bd662Svikram 	}
6987eb2bd662Svikram 
6988eb2bd662Svikram 	assert(i == *n);
6989eb2bd662Svikram 
6990eb2bd662Svikram 	filelist_free(&flist);
6991eb2bd662Svikram 
6992eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6993eb2bd662Svikram 	return (0);
6994eb2bd662Svikram }
6995eb2bd662Svikram 
6996963390b4Svikram /*
6997963390b4Svikram  * Certain services needed to run metastat successfully may not
6998963390b4Svikram  * be enabled. Enable them now.
6999963390b4Svikram  */
7000963390b4Svikram /*
7001963390b4Svikram  * Checks if the specified service is online
7002963390b4Svikram  * Returns: 	1 if the service is online
7003963390b4Svikram  *		0 if the service is not online
7004963390b4Svikram  *		-1 on error
7005963390b4Svikram  */
7006963390b4Svikram static int
7007963390b4Svikram is_svc_online(char *svc)
7008963390b4Svikram {
7009963390b4Svikram 	char			*state;
7010963390b4Svikram 	const char		*fcn = "is_svc_online()";
7011963390b4Svikram 
7012963390b4Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, svc));
7013963390b4Svikram 
7014963390b4Svikram 	state = smf_get_state(svc);
7015963390b4Svikram 	INJECT_ERROR2("GET_SVC_STATE", free(state), state = NULL);
7016963390b4Svikram 	if (state == NULL) {
7017963390b4Svikram 		bam_error(GET_SVC_STATE_ERR, svc);
7018963390b4Svikram 		return (-1);
7019963390b4Svikram 	}
7020963390b4Svikram 	BAM_DPRINTF((D_GOT_SVC_STATUS, fcn, svc));
7021963390b4Svikram 
7022963390b4Svikram 	if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0) {
7023963390b4Svikram 		BAM_DPRINTF((D_SVC_ONLINE, fcn, svc));
7024963390b4Svikram 		free(state);
7025963390b4Svikram 		return (1);
7026963390b4Svikram 	}
7027963390b4Svikram 
7028963390b4Svikram 	BAM_DPRINTF((D_SVC_NOT_ONLINE, fcn, state, svc));
7029963390b4Svikram 
7030963390b4Svikram 	free(state);
7031963390b4Svikram 
7032963390b4Svikram 	return (0);
7033963390b4Svikram }
7034963390b4Svikram 
7035963390b4Svikram static int
7036963390b4Svikram enable_svc(char *svc)
7037963390b4Svikram {
7038963390b4Svikram 	int			ret;
7039963390b4Svikram 	int			sleeptime;
7040963390b4Svikram 	const char		*fcn = "enable_svc()";
7041963390b4Svikram 
7042963390b4Svikram 	ret = is_svc_online(svc);
7043963390b4Svikram 	if (ret == -1) {
7044963390b4Svikram 		bam_error(SVC_IS_ONLINE_FAILED, svc);
7045963390b4Svikram 		return (-1);
7046963390b4Svikram 	} else if (ret == 1) {
7047963390b4Svikram 		BAM_DPRINTF((D_SVC_ALREADY_ONLINE, fcn, svc));
7048963390b4Svikram 		return (0);
7049963390b4Svikram 	}
7050963390b4Svikram 
7051963390b4Svikram 	/* Service is not enabled. Enable it now. */
7052963390b4Svikram 	ret = smf_enable_instance(svc, 0);
7053963390b4Svikram 	INJECT_ERROR1("ENABLE_SVC_FAILED", ret = -1);
7054963390b4Svikram 	if (ret != 0) {
7055963390b4Svikram 		bam_error(ENABLE_SVC_FAILED, svc);
7056963390b4Svikram 		return (-1);
7057963390b4Svikram 	}
7058963390b4Svikram 
7059963390b4Svikram 	BAM_DPRINTF((D_SVC_ONLINE_INITIATED, fcn, svc));
7060963390b4Svikram 
7061963390b4Svikram 	sleeptime = 0;
7062963390b4Svikram 	do {
7063963390b4Svikram 		ret = is_svc_online(svc);
7064963390b4Svikram 		INJECT_ERROR1("SVC_ONLINE_SUCCESS", ret = 1);
7065963390b4Svikram 		INJECT_ERROR1("SVC_ONLINE_FAILURE", ret = -1);
7066963390b4Svikram 		INJECT_ERROR1("SVC_ONLINE_NOTYET", ret = 0);
7067963390b4Svikram 		if (ret == -1) {
7068963390b4Svikram 			bam_error(ERR_SVC_GET_ONLINE, svc);
7069963390b4Svikram 			return (-1);
7070963390b4Svikram 		} else if (ret == 1) {
7071963390b4Svikram 			BAM_DPRINTF((D_SVC_NOW_ONLINE, fcn, svc));
7072963390b4Svikram 			return (1);
7073963390b4Svikram 		}
7074963390b4Svikram 		(void) sleep(1);
7075963390b4Svikram 	} while (sleeptime < 60);
7076963390b4Svikram 
7077963390b4Svikram 	bam_error(TIMEOUT_ENABLE_SVC, svc);
7078963390b4Svikram 
7079963390b4Svikram 	return (-1);
7080963390b4Svikram }
7081963390b4Svikram 
7082eb2bd662Svikram static int
7083eb2bd662Svikram ufs_get_physical(char *special, char ***physarray, int *n)
7084eb2bd662Svikram {
7085eb2bd662Svikram 	char			cmd[PATH_MAX];
7086eb2bd662Svikram 	char			*shortname;
7087eb2bd662Svikram 	filelist_t		flist = {0};
7088eb2bd662Svikram 	char			*meta;
7089eb2bd662Svikram 	char			*type;
7090eb2bd662Svikram 	char			*comp1;
7091eb2bd662Svikram 	char			*comp2;
7092eb2bd662Svikram 	char			*comp3;
7093eb2bd662Svikram 	char			*comp4;
7094eb2bd662Svikram 	int			i;
7095eb2bd662Svikram 	line_t			*lp;
7096eb2bd662Svikram 	int			ret;
7097963390b4Svikram 	char			*svc;
7098eb2bd662Svikram 	const char		*fcn = "ufs_get_physical()";
7099eb2bd662Svikram 
7100eb2bd662Svikram 	assert(special);
7101eb2bd662Svikram 
7102eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, special));
7103eb2bd662Svikram 
7104eb2bd662Svikram 	if (strncmp(special, "/dev/md/", strlen("/dev/md/")) != 0) {
7105eb2bd662Svikram 		bam_error(UFS_GET_PHYS_NOT_SVM, special);
7106eb2bd662Svikram 		return (-1);
7107eb2bd662Svikram 	}
7108eb2bd662Svikram 
7109eb2bd662Svikram 	if (strncmp(special, "/dev/md/dsk/", strlen("/dev/md/dsk/")) == 0) {
7110eb2bd662Svikram 		shortname = special + strlen("/dev/md/dsk/");
7111eb2bd662Svikram 	} else if (strncmp(special, "/dev/md/rdsk/",
7112eb2bd662Svikram 	    strlen("/dev/md/rdsk/")) == 0) {
7113eb2bd662Svikram 		shortname = special + strlen("/dev/md/rdsk");
7114eb2bd662Svikram 	} else {
7115eb2bd662Svikram 		bam_error(UFS_GET_PHYS_INVALID_SVM, special);
7116eb2bd662Svikram 		return (-1);
7117eb2bd662Svikram 	}
7118eb2bd662Svikram 
7119eb2bd662Svikram 	BAM_DPRINTF((D_UFS_SVM_SHORT, fcn, special, shortname));
7120eb2bd662Svikram 
7121963390b4Svikram 	svc = "network/rpc/meta:default";
7122963390b4Svikram 	if (enable_svc(svc) == -1) {
7123963390b4Svikram 		bam_error(UFS_SVM_METASTAT_SVC_ERR, svc);
7124963390b4Svikram 	}
7125963390b4Svikram 
7126eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/metastat -p %s", shortname);
7127eb2bd662Svikram 
7128eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
7129eb2bd662Svikram 	INJECT_ERROR1("UFS_SVM_METASTAT", ret = 1);
7130eb2bd662Svikram 	if (ret != 0) {
7131eb2bd662Svikram 		bam_error(UFS_SVM_METASTAT_ERR, shortname);
7132eb2bd662Svikram 		return (-1);
7133eb2bd662Svikram 	}
7134eb2bd662Svikram 
7135eb2bd662Svikram 	INJECT_ERROR1("UFS_SVM_METASTAT_OUT", flist.head = NULL);
7136eb2bd662Svikram 	if (flist.head == NULL) {
7137eb2bd662Svikram 		bam_error(BAD_UFS_SVM_METASTAT, shortname);
7138eb2bd662Svikram 		filelist_free(&flist);
7139eb2bd662Svikram 		return (-1);
71407c478bd9Sstevel@tonic-gate 	}
71417c478bd9Sstevel@tonic-gate 
71427c478bd9Sstevel@tonic-gate 	/*
7143eb2bd662Svikram 	 * Check if not a mirror. We only parse a single metadevice
7144eb2bd662Svikram 	 * if not a mirror
7145eb2bd662Svikram 	 */
7146eb2bd662Svikram 	meta = strtok(flist.head->line, " \t");
7147eb2bd662Svikram 	type = strtok(NULL, " \t");
7148eb2bd662Svikram 	if (meta == NULL || type == NULL) {
7149eb2bd662Svikram 		bam_error(ERROR_PARSE_UFS_SVM_METASTAT, shortname);
7150eb2bd662Svikram 		filelist_free(&flist);
7151eb2bd662Svikram 		return (-1);
7152eb2bd662Svikram 	}
7153eb2bd662Svikram 	if (strcmp(type, "-m") != 0) {
7154eb2bd662Svikram 		comp1 = strtok(NULL, " \t");
7155eb2bd662Svikram 		comp2 = strtok(NULL, " \t");
7156eb2bd662Svikram 		if (comp1 == NULL || comp2 != NULL) {
7157eb2bd662Svikram 			bam_error(INVALID_UFS_SVM_METASTAT, shortname);
7158eb2bd662Svikram 			filelist_free(&flist);
7159eb2bd662Svikram 			return (-1);
7160eb2bd662Svikram 		}
7161eb2bd662Svikram 		BAM_DPRINTF((D_UFS_SVM_ONE_COMP, fcn, comp1, shortname));
7162eb2bd662Svikram 		*physarray = s_calloc(1, sizeof (char *));
7163eb2bd662Svikram 		(*physarray)[0] = s_strdup(comp1);
7164eb2bd662Svikram 		*n = 1;
7165eb2bd662Svikram 		filelist_free(&flist);
7166eb2bd662Svikram 		return (0);
7167eb2bd662Svikram 	}
7168eb2bd662Svikram 
7169eb2bd662Svikram 	/*
7170eb2bd662Svikram 	 * Okay we have a mirror. Everything after the first line
7171eb2bd662Svikram 	 * is a submirror
7172eb2bd662Svikram 	 */
7173eb2bd662Svikram 	for (i = 0, lp = flist.head->next; lp; lp = lp->next) {
7174eb2bd662Svikram 		if (strstr(lp->line, "/dev/dsk/") == NULL &&
7175eb2bd662Svikram 		    strstr(lp->line, "/dev/rdsk/") == NULL) {
7176eb2bd662Svikram 			bam_error(CANNOT_PARSE_UFS_SVM_METASTAT, shortname);
7177eb2bd662Svikram 			filelist_free(&flist);
7178eb2bd662Svikram 			return (-1);
7179eb2bd662Svikram 		}
7180eb2bd662Svikram 		i++;
7181eb2bd662Svikram 	}
7182eb2bd662Svikram 
7183eb2bd662Svikram 	*physarray = s_calloc(i, sizeof (char *));
7184eb2bd662Svikram 	*n = i;
7185eb2bd662Svikram 
7186eb2bd662Svikram 	for (i = 0, lp = flist.head->next; lp; lp = lp->next) {
7187eb2bd662Svikram 		comp1 = strtok(lp->line, " \t");
7188eb2bd662Svikram 		comp2 = strtok(NULL, " \t");
7189eb2bd662Svikram 		comp3 = strtok(NULL, " \t");
7190eb2bd662Svikram 		comp4 = strtok(NULL, " \t");
7191eb2bd662Svikram 
7192eb2bd662Svikram 		if (comp3 == NULL || comp4 == NULL ||
7193eb2bd662Svikram 		    (strncmp(comp4, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
7194eb2bd662Svikram 		    strncmp(comp4, "/dev/rdsk/", strlen("/dev/rdsk/")) != 0)) {
7195eb2bd662Svikram 			bam_error(CANNOT_PARSE_UFS_SVM_SUBMIRROR, shortname);
7196eb2bd662Svikram 			filelist_free(&flist);
7197eb2bd662Svikram 			free_physarray(*physarray, *n);
7198eb2bd662Svikram 			return (-1);
7199eb2bd662Svikram 		}
7200eb2bd662Svikram 
7201eb2bd662Svikram 		(*physarray)[i++] = s_strdup(comp4);
7202eb2bd662Svikram 	}
7203eb2bd662Svikram 
7204eb2bd662Svikram 	assert(i == *n);
7205eb2bd662Svikram 
7206eb2bd662Svikram 	filelist_free(&flist);
7207eb2bd662Svikram 
7208eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7209eb2bd662Svikram 	return (0);
7210eb2bd662Svikram }
7211eb2bd662Svikram 
7212eb2bd662Svikram static int
7213eb2bd662Svikram get_physical(char *menu_root, char ***physarray, int *n)
7214eb2bd662Svikram {
7215eb2bd662Svikram 	char			*special;
7216eb2bd662Svikram 	int			ret;
7217eb2bd662Svikram 	const char		*fcn = "get_physical()";
7218eb2bd662Svikram 
7219eb2bd662Svikram 	assert(menu_root);
7220eb2bd662Svikram 	assert(physarray);
7221eb2bd662Svikram 	assert(n);
7222eb2bd662Svikram 
7223eb2bd662Svikram 	*physarray = NULL;
7224eb2bd662Svikram 	*n = 0;
7225eb2bd662Svikram 
7226eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, menu_root));
7227eb2bd662Svikram 
7228eb2bd662Svikram 	/* First get the device special file from /etc/mnttab */
7229eb2bd662Svikram 	special = get_special(menu_root);
7230eb2bd662Svikram 	INJECT_ERROR1("GET_PHYSICAL_SPECIAL", special = NULL);
7231eb2bd662Svikram 	if (special == NULL) {
7232eb2bd662Svikram 		bam_error(GET_SPECIAL_NULL, menu_root);
7233eb2bd662Svikram 		return (-1);
7234eb2bd662Svikram 	}
7235eb2bd662Svikram 
7236eb2bd662Svikram 	/* If already a physical device nothing to do */
7237eb2bd662Svikram 	if (strncmp(special, "/dev/dsk/", strlen("/dev/dsk/")) == 0 ||
7238eb2bd662Svikram 	    strncmp(special, "/dev/rdsk/", strlen("/dev/rdsk/")) == 0) {
7239eb2bd662Svikram 		BAM_DPRINTF((D_GET_PHYSICAL_ALREADY, fcn, menu_root, special));
7240eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7241eb2bd662Svikram 		*physarray = s_calloc(1, sizeof (char *));
7242eb2bd662Svikram 		(*physarray)[0] = special;
7243eb2bd662Svikram 		*n = 1;
7244eb2bd662Svikram 		return (0);
7245eb2bd662Svikram 	}
7246eb2bd662Svikram 
7247eb2bd662Svikram 	if (is_zfs(menu_root)) {
7248eb2bd662Svikram 		ret = zfs_get_physical(special, physarray, n);
7249eb2bd662Svikram 	} else if (is_ufs(menu_root)) {
7250eb2bd662Svikram 		ret = ufs_get_physical(special, physarray, n);
7251eb2bd662Svikram 	} else {
7252eb2bd662Svikram 		bam_error(GET_PHYSICAL_NOTSUP_FSTYPE, menu_root, special);
7253eb2bd662Svikram 		ret = -1;
7254eb2bd662Svikram 	}
7255eb2bd662Svikram 
7256eb2bd662Svikram 	free(special);
7257eb2bd662Svikram 
7258eb2bd662Svikram 	INJECT_ERROR1("GET_PHYSICAL_RET", ret = -1);
7259eb2bd662Svikram 	if (ret == -1) {
7260eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7261eb2bd662Svikram 	} else {
7262eb2bd662Svikram 		int	i;
7263eb2bd662Svikram 		assert (*n > 0);
7264eb2bd662Svikram 		for (i = 0; i < *n; i++) {
7265eb2bd662Svikram 			BAM_DPRINTF((D_GET_PHYSICAL_RET, fcn, (*physarray)[i]));
7266eb2bd662Svikram 		}
7267eb2bd662Svikram 	}
7268eb2bd662Svikram 
7269eb2bd662Svikram 	return (ret);
7270eb2bd662Svikram }
7271eb2bd662Svikram 
7272eb2bd662Svikram static int
7273eb2bd662Svikram is_bootdisk(char *osroot, char *physical)
7274eb2bd662Svikram {
7275eb2bd662Svikram 	int			ret;
7276eb2bd662Svikram 	char			*grubroot;
7277eb2bd662Svikram 	char			*bootp;
7278eb2bd662Svikram 	const char		*fcn = "is_bootdisk()";
7279eb2bd662Svikram 
7280eb2bd662Svikram 	assert(osroot);
7281eb2bd662Svikram 	assert(physical);
7282eb2bd662Svikram 
7283eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, physical));
7284eb2bd662Svikram 
7285eb2bd662Svikram 	bootp = strstr(physical, "p0:boot");
7286eb2bd662Svikram 	if (bootp)
7287eb2bd662Svikram 		*bootp = '\0';
7288eb2bd662Svikram 	/*
7289eb2bd662Svikram 	 * We just want the BIOS mapping for menu disk.
7290eb2bd662Svikram 	 * Don't pass menu_root to get_grubroot() as the
7291eb2bd662Svikram 	 * check that it is used for is not relevant here.
7292eb2bd662Svikram 	 * The osroot is immaterial as well - it is only used to
7293eb2bd662Svikram 	 * to find create_diskmap script. Everything hinges on
7294eb2bd662Svikram 	 * "physical"
7295eb2bd662Svikram 	 */
7296eb2bd662Svikram 	grubroot = get_grubroot(osroot, physical, NULL);
7297eb2bd662Svikram 
7298eb2bd662Svikram 	INJECT_ERROR1("IS_BOOTDISK_GRUBROOT", grubroot = NULL);
7299eb2bd662Svikram 	if (grubroot == NULL) {
730037eb779cSVikram Hegde 		if (bam_verbose)
730137eb779cSVikram Hegde 			bam_error(NO_GRUBROOT_FOR_DISK, physical);
7302eb2bd662Svikram 		return (0);
7303eb2bd662Svikram 	}
7304eb2bd662Svikram 	ret = grubroot[3] == '0';
7305eb2bd662Svikram 	free(grubroot);
7306eb2bd662Svikram 
7307eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_RET, fcn, ret));
7308eb2bd662Svikram 
7309eb2bd662Svikram 	return (ret);
7310eb2bd662Svikram }
7311eb2bd662Svikram 
7312eb2bd662Svikram /*
7313eb2bd662Svikram  * Check if menu is on the boot device
73147c478bd9Sstevel@tonic-gate  * Return 0 (false) on error
73157c478bd9Sstevel@tonic-gate  */
73167c478bd9Sstevel@tonic-gate static int
7317eb2bd662Svikram menu_on_bootdisk(char *osroot, char *menu_root)
73187c478bd9Sstevel@tonic-gate {
7319eb2bd662Svikram 	char		**physarray;
73207c478bd9Sstevel@tonic-gate 	int		ret;
7321eb2bd662Svikram 	int		n;
7322eb2bd662Svikram 	int		i;
7323eb2bd662Svikram 	int		on_bootdisk;
7324eb2bd662Svikram 	const char	*fcn = "menu_on_bootdisk()";
73257c478bd9Sstevel@tonic-gate 
7326eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, menu_root));
73277c478bd9Sstevel@tonic-gate 
7328eb2bd662Svikram 	ret = get_physical(menu_root, &physarray, &n);
7329eb2bd662Svikram 	INJECT_ERROR1("MENU_ON_BOOTDISK_PHYSICAL", ret = -1);
7330eb2bd662Svikram 	if (ret != 0) {
7331eb2bd662Svikram 		bam_error(GET_PHYSICAL_MENU_NULL, menu_root);
73327c478bd9Sstevel@tonic-gate 		return (0);
7333eb2bd662Svikram 	}
7334eb2bd662Svikram 
7335eb2bd662Svikram 	assert(physarray);
7336eb2bd662Svikram 	assert(n > 0);
7337eb2bd662Svikram 
7338eb2bd662Svikram 	on_bootdisk = 0;
7339eb2bd662Svikram 	for (i = 0; i < n; i++) {
7340eb2bd662Svikram 		assert(strncmp(physarray[i], "/dev/dsk/",
7341eb2bd662Svikram 		    strlen("/dev/dsk/")) == 0 ||
7342eb2bd662Svikram 		    strncmp(physarray[i], "/dev/rdsk/",
7343eb2bd662Svikram 		    strlen("/dev/rdsk/")) == 0);
7344eb2bd662Svikram 
7345eb2bd662Svikram 		BAM_DPRINTF((D_CHECK_ON_BOOTDISK, fcn, physarray[i]));
7346eb2bd662Svikram 		if (is_bootdisk(osroot, physarray[i])) {
7347eb2bd662Svikram 			on_bootdisk = 1;
7348eb2bd662Svikram 			BAM_DPRINTF((D_IS_ON_BOOTDISK, fcn, physarray[i]));
7349eb2bd662Svikram 		}
7350eb2bd662Svikram 	}
7351eb2bd662Svikram 
7352eb2bd662Svikram 	free_physarray(physarray, n);
7353eb2bd662Svikram 
7354eb2bd662Svikram 	INJECT_ERROR1("ON_BOOTDISK_YES", on_bootdisk = 1);
7355eb2bd662Svikram 	INJECT_ERROR1("ON_BOOTDISK_NO", on_bootdisk = 0);
7356eb2bd662Svikram 	if (on_bootdisk) {
7357eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7358eb2bd662Svikram 	} else {
7359eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7360eb2bd662Svikram 	}
7361eb2bd662Svikram 
7362eb2bd662Svikram 	return (on_bootdisk);
7363eb2bd662Svikram }
7364eb2bd662Svikram 
7365eb2bd662Svikram void
7366eb2bd662Svikram bam_add_line(menu_t *mp, entry_t *entry, line_t *prev, line_t *lp)
7367eb2bd662Svikram {
7368eb2bd662Svikram 	const char	*fcn = "bam_add_line()";
7369eb2bd662Svikram 
7370eb2bd662Svikram 	assert(mp);
7371eb2bd662Svikram 	assert(entry);
7372eb2bd662Svikram 	assert(prev);
7373eb2bd662Svikram 	assert(lp);
7374eb2bd662Svikram 
7375eb2bd662Svikram 	lp->next = prev->next;
7376eb2bd662Svikram 	if (prev->next) {
7377eb2bd662Svikram 		BAM_DPRINTF((D_ADD_LINE_PREV_NEXT, fcn));
7378eb2bd662Svikram 		prev->next->prev = lp;
7379eb2bd662Svikram 	} else {
7380eb2bd662Svikram 		BAM_DPRINTF((D_ADD_LINE_NOT_PREV_NEXT, fcn));
7381eb2bd662Svikram 	}
7382eb2bd662Svikram 	prev->next = lp;
7383eb2bd662Svikram 	lp->prev = prev;
7384eb2bd662Svikram 
7385eb2bd662Svikram 	if (entry->end == prev) {
7386eb2bd662Svikram 		BAM_DPRINTF((D_ADD_LINE_LAST_LINE_IN_ENTRY, fcn));
7387eb2bd662Svikram 		entry->end = lp;
7388eb2bd662Svikram 	}
7389eb2bd662Svikram 	if (mp->end == prev) {
7390eb2bd662Svikram 		assert(lp->next == NULL);
7391eb2bd662Svikram 		mp->end = lp;
7392eb2bd662Svikram 		BAM_DPRINTF((D_ADD_LINE_LAST_LINE_IN_MENU, fcn));
7393eb2bd662Svikram 	}
73947c478bd9Sstevel@tonic-gate }
73957c478bd9Sstevel@tonic-gate 
73968c1b6884Sszhou /*
73978c1b6884Sszhou  * look for matching bootadm entry with specified parameters
73988c1b6884Sszhou  * Here are the rules (based on existing usage):
73998c1b6884Sszhou  * - If title is specified, match on title only
7400eb2bd662Svikram  * - Else, match on root/findroot, kernel, and module.
7401eb2bd662Svikram  *   Note that, if root_opt is non-zero, the absence of
7402eb2bd662Svikram  *   root line is considered a match.
74038c1b6884Sszhou  */
74048c1b6884Sszhou static entry_t *
7405eb2bd662Svikram find_boot_entry(
7406eb2bd662Svikram 	menu_t *mp,
7407eb2bd662Svikram 	char *title,
7408eb2bd662Svikram 	char *kernel,
7409eb2bd662Svikram 	char *findroot,
7410eb2bd662Svikram 	char *root,
7411eb2bd662Svikram 	char *module,
7412eb2bd662Svikram 	int root_opt,
7413eb2bd662Svikram 	int *entry_num)
74148c1b6884Sszhou {
74158c1b6884Sszhou 	int		i;
74168c1b6884Sszhou 	line_t		*lp;
74178c1b6884Sszhou 	entry_t		*ent;
7418eb2bd662Svikram 	const char	*fcn = "find_boot_entry()";
7419eb2bd662Svikram 
7420eb2bd662Svikram 	if (entry_num)
7421eb2bd662Svikram 		*entry_num = BAM_ERROR;
74228c1b6884Sszhou 
74238c1b6884Sszhou 	/* find matching entry */
74248c1b6884Sszhou 	for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) {
74258c1b6884Sszhou 		lp = ent->start;
74268c1b6884Sszhou 
74278c1b6884Sszhou 		/* first line of entry must be bootadm comment */
74288c1b6884Sszhou 		lp = ent->start;
7429ae115bc7Smrj 		if (lp->flags != BAM_COMMENT ||
7430ae115bc7Smrj 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0) {
74318c1b6884Sszhou 			continue;
74328c1b6884Sszhou 		}
74338c1b6884Sszhou 
74348c1b6884Sszhou 		/* advance to title line */
74358c1b6884Sszhou 		lp = lp->next;
74368c1b6884Sszhou 		if (title) {
74378c1b6884Sszhou 			if (lp->flags == BAM_TITLE && lp->arg &&
7438eb2bd662Svikram 			    strcmp(lp->arg, title) == 0) {
7439eb2bd662Svikram 				BAM_DPRINTF((D_MATCHED_TITLE, fcn, title));
74408c1b6884Sszhou 				break;
7441eb2bd662Svikram 			}
7442eb2bd662Svikram 			BAM_DPRINTF((D_NOMATCH_TITLE, fcn, title, lp->arg));
74438c1b6884Sszhou 			continue;	/* check title only */
74448c1b6884Sszhou 		}
74458c1b6884Sszhou 
74468c1b6884Sszhou 		lp = lp->next;	/* advance to root line */
7447843e1988Sjohnlev 		if (lp == NULL) {
7448843e1988Sjohnlev 			continue;
7449eb2bd662Svikram 		} else if (strcmp(lp->cmd, menu_cmds[FINDROOT_CMD]) == 0) {
7450eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_NULL_FINDROOT",
7451eb2bd662Svikram 			    findroot = NULL);
7452eb2bd662Svikram 			if (findroot == NULL) {
7453eb2bd662Svikram 				BAM_DPRINTF((D_NOMATCH_FINDROOT_NULL,
7454eb2bd662Svikram 				    fcn, lp->arg));
74558c1b6884Sszhou 				continue;
74568c1b6884Sszhou 			}
7457eb2bd662Svikram 			/* findroot command found, try match  */
7458eb2bd662Svikram 			if (strcmp(lp->arg, findroot) != 0) {
7459eb2bd662Svikram 				BAM_DPRINTF((D_NOMATCH_FINDROOT,
7460eb2bd662Svikram 				    fcn, findroot, lp->arg));
7461eb2bd662Svikram 				continue;
7462eb2bd662Svikram 			}
7463eb2bd662Svikram 			BAM_DPRINTF((D_MATCHED_FINDROOT, fcn, findroot));
7464eb2bd662Svikram 			lp = lp->next;	/* advance to kernel line */
7465eb2bd662Svikram 		} else if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
7466eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_NULL_ROOT", root = NULL);
7467eb2bd662Svikram 			if (root == NULL) {
7468eb2bd662Svikram 				BAM_DPRINTF((D_NOMATCH_ROOT_NULL,
7469eb2bd662Svikram 				    fcn, lp->arg));
7470eb2bd662Svikram 				continue;
7471eb2bd662Svikram 			}
7472eb2bd662Svikram 			/* root cmd found, try match */
7473eb2bd662Svikram 			if (strcmp(lp->arg, root) != 0) {
7474eb2bd662Svikram 				BAM_DPRINTF((D_NOMATCH_ROOT,
7475eb2bd662Svikram 				    fcn, root, lp->arg));
7476eb2bd662Svikram 				continue;
7477eb2bd662Svikram 			}
7478eb2bd662Svikram 			BAM_DPRINTF((D_MATCHED_ROOT, fcn, root));
74798c1b6884Sszhou 			lp = lp->next;	/* advance to kernel line */
74808c1b6884Sszhou 		} else {
7481eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_ROOT_OPT_NO",
7482eb2bd662Svikram 			    root_opt = 0);
7483eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_ROOT_OPT_YES",
7484eb2bd662Svikram 			    root_opt = 1);
74858c1b6884Sszhou 			/* no root command, see if root is optional */
74868c1b6884Sszhou 			if (root_opt == 0) {
7487eb2bd662Svikram 				BAM_DPRINTF((D_NO_ROOT_OPT, fcn));
74888c1b6884Sszhou 				continue;
74898c1b6884Sszhou 			}
7490eb2bd662Svikram 			BAM_DPRINTF((D_ROOT_OPT, fcn));
74918c1b6884Sszhou 		}
74928c1b6884Sszhou 
74938c1b6884Sszhou 		if (lp == NULL || lp->next == NULL) {
74948c1b6884Sszhou 			continue;
74958c1b6884Sszhou 		}
74968c1b6884Sszhou 
7497843e1988Sjohnlev 		if (kernel &&
7498843e1988Sjohnlev 		    (!check_cmd(lp->cmd, KERNEL_CMD, lp->arg, kernel))) {
7499bbcc54bdSEnrico Perla - Sun Microsystems 			if (!(ent->flags & BAM_ENTRY_FAILSAFE) ||
7500bbcc54bdSEnrico Perla - Sun Microsystems 			    !(ent->flags & BAM_ENTRY_DBOOT) ||
7501bbcc54bdSEnrico Perla - Sun Microsystems 			    strcmp(kernel, DIRECT_BOOT_FAILSAFE_LINE) != 0)
75028c1b6884Sszhou 				continue;
7503bbcc54bdSEnrico Perla - Sun Microsystems 
7504bbcc54bdSEnrico Perla - Sun Microsystems 			ent->flags |= BAM_ENTRY_UPGFSKERNEL;
7505bbcc54bdSEnrico Perla - Sun Microsystems 
75068c1b6884Sszhou 		}
7507eb2bd662Svikram 		BAM_DPRINTF((D_KERNEL_MATCH, fcn, kernel, lp->arg));
7508843e1988Sjohnlev 
7509843e1988Sjohnlev 		/*
7510843e1988Sjohnlev 		 * Check for matching module entry (failsafe or normal).
7511843e1988Sjohnlev 		 * If it fails to match, we go around the loop again.
7512843e1988Sjohnlev 		 * For xpv entries, there are two module lines, so we
7513843e1988Sjohnlev 		 * do the check twice.
7514843e1988Sjohnlev 		 */
7515843e1988Sjohnlev 		lp = lp->next;	/* advance to module line */
7516843e1988Sjohnlev 		if (check_cmd(lp->cmd, MODULE_CMD, lp->arg, module) ||
7517843e1988Sjohnlev 		    (((lp = lp->next) != NULL) &&
7518843e1988Sjohnlev 		    check_cmd(lp->cmd, MODULE_CMD, lp->arg, module))) {
7519843e1988Sjohnlev 			/* match found */
7520eb2bd662Svikram 			BAM_DPRINTF((D_MODULE_MATCH, fcn, module, lp->arg));
7521843e1988Sjohnlev 			break;
7522843e1988Sjohnlev 		}
7523bbcc54bdSEnrico Perla - Sun Microsystems 
7524bbcc54bdSEnrico Perla - Sun Microsystems 		if (strcmp(module, FAILSAFE_ARCHIVE) == 0 &&
7525bbcc54bdSEnrico Perla - Sun Microsystems 		    (strcmp(lp->prev->arg, FAILSAFE_ARCHIVE_32) == 0 ||
7526bbcc54bdSEnrico Perla - Sun Microsystems 		    strcmp(lp->prev->arg, FAILSAFE_ARCHIVE_64) == 0)) {
7527bbcc54bdSEnrico Perla - Sun Microsystems 			ent->flags |= BAM_ENTRY_UPGFSMODULE;
7528bbcc54bdSEnrico Perla - Sun Microsystems 			break;
7529bbcc54bdSEnrico Perla - Sun Microsystems 		}
7530bbcc54bdSEnrico Perla - Sun Microsystems 
75318c1b6884Sszhou 	}
75328c1b6884Sszhou 
7533eb2bd662Svikram 	if (ent && entry_num) {
75348c1b6884Sszhou 		*entry_num = i;
7535843e1988Sjohnlev 	}
7536eb2bd662Svikram 
7537eb2bd662Svikram 	if (ent) {
7538eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_RET, fcn, i));
7539eb2bd662Svikram 	} else {
7540eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_RET, fcn, BAM_ERROR));
7541eb2bd662Svikram 	}
75428c1b6884Sszhou 	return (ent);
75438c1b6884Sszhou }
75448c1b6884Sszhou 
75458c1b6884Sszhou static int
7546eb2bd662Svikram update_boot_entry(menu_t *mp, char *title, char *findroot, char *root,
7547eb2bd662Svikram     char *kernel, char *mod_kernel, char *module, int root_opt)
75488c1b6884Sszhou {
7549eb2bd662Svikram 	int		i;
7550eb2bd662Svikram 	int		change_kernel = 0;
75518c1b6884Sszhou 	entry_t		*ent;
75528c1b6884Sszhou 	line_t		*lp;
7553eb2bd662Svikram 	line_t		*tlp;
75548c1b6884Sszhou 	char		linebuf[BAM_MAXLINE];
7555eb2bd662Svikram 	const char	*fcn = "update_boot_entry()";
75568c1b6884Sszhou 
75578c1b6884Sszhou 	/* note: don't match on title, it's updated on upgrade */
7558eb2bd662Svikram 	ent = find_boot_entry(mp, NULL, kernel, findroot, root, module,
7559eb2bd662Svikram 	    root_opt, &i);
7560ae115bc7Smrj 	if ((ent == NULL) && (bam_direct == BAM_DIRECT_DBOOT)) {
7561ae115bc7Smrj 		/*
7562ae115bc7Smrj 		 * We may be upgrading a kernel from multiboot to
7563eb2bd662Svikram 		 * directboot.  Look for a multiboot entry. A multiboot
7564eb2bd662Svikram 		 * entry will not have a findroot line.
7565ae115bc7Smrj 		 */
7566eb2bd662Svikram 		ent = find_boot_entry(mp, NULL, "multiboot", NULL, root,
7567eb2bd662Svikram 		    MULTIBOOT_ARCHIVE, root_opt, &i);
7568ae115bc7Smrj 		if (ent != NULL) {
7569eb2bd662Svikram 			BAM_DPRINTF((D_UPGRADE_FROM_MULTIBOOT, fcn, root));
7570ae115bc7Smrj 			change_kernel = 1;
7571ae115bc7Smrj 		}
7572eb2bd662Svikram 	} else if (ent) {
7573eb2bd662Svikram 		BAM_DPRINTF((D_FOUND_FINDROOT, fcn, findroot));
7574ae115bc7Smrj 	}
75758c1b6884Sszhou 
7576eb2bd662Svikram 	if (ent == NULL) {
7577eb2bd662Svikram 		BAM_DPRINTF((D_ENTRY_NOT_FOUND_CREATING, fcn, findroot));
7578eb2bd662Svikram 		return (add_boot_entry(mp, title, findroot,
7579eb2bd662Svikram 		    kernel, mod_kernel, module));
7580eb2bd662Svikram 	}
7581eb2bd662Svikram 
7582eb2bd662Svikram 	/* replace title of existing entry and update findroot line */
75838c1b6884Sszhou 	lp = ent->start;
75848c1b6884Sszhou 	lp = lp->next;	/* title line */
75858c1b6884Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
75868c1b6884Sszhou 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
75878c1b6884Sszhou 	free(lp->arg);
75888c1b6884Sszhou 	free(lp->line);
75898c1b6884Sszhou 	lp->arg = s_strdup(title);
75908c1b6884Sszhou 	lp->line = s_strdup(linebuf);
7591eb2bd662Svikram 	BAM_DPRINTF((D_CHANGING_TITLE, fcn, title));
75928c1b6884Sszhou 
7593eb2bd662Svikram 	tlp = lp;	/* title line */
75948c1b6884Sszhou 	lp = lp->next;	/* root line */
7595eb2bd662Svikram 
7596eb2bd662Svikram 	/* if no root or findroot command, create a new line_t */
7597eb2bd662Svikram 	if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) != 0 &&
7598eb2bd662Svikram 	    strcmp(lp->cmd, menu_cmds[FINDROOT_CMD]) != 0) {
7599eb2bd662Svikram 		lp = s_calloc(1, sizeof (line_t));
7600eb2bd662Svikram 		bam_add_line(mp, ent, tlp, lp);
7601eb2bd662Svikram 	} else {
7602eb2bd662Svikram 		free(lp->cmd);
7603eb2bd662Svikram 		free(lp->sep);
7604eb2bd662Svikram 		free(lp->arg);
7605eb2bd662Svikram 		free(lp->line);
76068c1b6884Sszhou 	}
7607ae115bc7Smrj 
7608eb2bd662Svikram 	lp->cmd = s_strdup(menu_cmds[FINDROOT_CMD]);
7609eb2bd662Svikram 	lp->sep = s_strdup(menu_cmds[SEP_CMD]);
7610eb2bd662Svikram 	lp->arg = s_strdup(findroot);
7611eb2bd662Svikram 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
7612eb2bd662Svikram 	    menu_cmds[FINDROOT_CMD], menu_cmds[SEP_CMD], findroot);
7613eb2bd662Svikram 	lp->line = s_strdup(linebuf);
7614eb2bd662Svikram 	BAM_DPRINTF((D_ADDING_FINDROOT_LINE, fcn, findroot));
7615eb2bd662Svikram 
7616eb2bd662Svikram 	/* kernel line */
7617eb2bd662Svikram 	lp = lp->next;
7618eb2bd662Svikram 
7619bbcc54bdSEnrico Perla - Sun Microsystems 	if (ent->flags & BAM_ENTRY_UPGFSKERNEL) {
7620bbcc54bdSEnrico Perla - Sun Microsystems 		char		*params = NULL;
7621bbcc54bdSEnrico Perla - Sun Microsystems 
7622bbcc54bdSEnrico Perla - Sun Microsystems 		params = strstr(lp->line, "-s");
7623bbcc54bdSEnrico Perla - Sun Microsystems 		if (params != NULL)
7624bbcc54bdSEnrico Perla - Sun Microsystems 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s%s",
7625bbcc54bdSEnrico Perla - Sun Microsystems 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
7626bbcc54bdSEnrico Perla - Sun Microsystems 			    kernel, params+2);
7627bbcc54bdSEnrico Perla - Sun Microsystems 		else
7628bbcc54bdSEnrico Perla - Sun Microsystems 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
7629bbcc54bdSEnrico Perla - Sun Microsystems 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
7630bbcc54bdSEnrico Perla - Sun Microsystems 			    kernel);
7631bbcc54bdSEnrico Perla - Sun Microsystems 
7632bbcc54bdSEnrico Perla - Sun Microsystems 		free(lp->cmd);
7633bbcc54bdSEnrico Perla - Sun Microsystems 		free(lp->arg);
7634bbcc54bdSEnrico Perla - Sun Microsystems 		free(lp->line);
7635bbcc54bdSEnrico Perla - Sun Microsystems 		lp->cmd = s_strdup(menu_cmds[KERNEL_DOLLAR_CMD]);
7636bbcc54bdSEnrico Perla - Sun Microsystems 		lp->arg = s_strdup(strstr(linebuf, "/"));
7637bbcc54bdSEnrico Perla - Sun Microsystems 		lp->line = s_strdup(linebuf);
7638bbcc54bdSEnrico Perla - Sun Microsystems 		ent->flags &= ~BAM_ENTRY_UPGFSKERNEL;
7639bbcc54bdSEnrico Perla - Sun Microsystems 		BAM_DPRINTF((D_ADDING_KERNEL_DOLLAR, fcn, lp->prev->cmd));
7640bbcc54bdSEnrico Perla - Sun Microsystems 	}
7641bbcc54bdSEnrico Perla - Sun Microsystems 
7642ae115bc7Smrj 	if (change_kernel) {
7643ae115bc7Smrj 		/*
7644ae115bc7Smrj 		 * We're upgrading from multiboot to directboot.
7645ae115bc7Smrj 		 */
7646ae115bc7Smrj 		if (strcmp(lp->cmd, menu_cmds[KERNEL_CMD]) == 0) {
7647ae115bc7Smrj 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
7648ae115bc7Smrj 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
7649ae115bc7Smrj 			    kernel);
7650eb2bd662Svikram 			free(lp->cmd);
7651ae115bc7Smrj 			free(lp->arg);
7652ae115bc7Smrj 			free(lp->line);
7653eb2bd662Svikram 			lp->cmd = s_strdup(menu_cmds[KERNEL_DOLLAR_CMD]);
7654ae115bc7Smrj 			lp->arg = s_strdup(kernel);
7655ae115bc7Smrj 			lp->line = s_strdup(linebuf);
7656ae115bc7Smrj 			lp = lp->next;
7657eb2bd662Svikram 			BAM_DPRINTF((D_ADDING_KERNEL_DOLLAR, fcn, kernel));
7658ae115bc7Smrj 		}
7659ae115bc7Smrj 		if (strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) {
7660ae115bc7Smrj 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
7661ae115bc7Smrj 			    menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD],
7662ae115bc7Smrj 			    module);
7663eb2bd662Svikram 			free(lp->cmd);
7664ae115bc7Smrj 			free(lp->arg);
7665ae115bc7Smrj 			free(lp->line);
7666eb2bd662Svikram 			lp->cmd = s_strdup(menu_cmds[MODULE_DOLLAR_CMD]);
7667ae115bc7Smrj 			lp->arg = s_strdup(module);
7668ae115bc7Smrj 			lp->line = s_strdup(linebuf);
7669ae115bc7Smrj 			lp = lp->next;
7670eb2bd662Svikram 			BAM_DPRINTF((D_ADDING_MODULE_DOLLAR, fcn, module));
7671ae115bc7Smrj 		}
7672ae115bc7Smrj 	}
7673bbcc54bdSEnrico Perla - Sun Microsystems 
7674bbcc54bdSEnrico Perla - Sun Microsystems 	/* module line */
7675bbcc54bdSEnrico Perla - Sun Microsystems 	lp = lp->next;
7676bbcc54bdSEnrico Perla - Sun Microsystems 
7677bbcc54bdSEnrico Perla - Sun Microsystems 	if (ent->flags & BAM_ENTRY_UPGFSMODULE) {
7678bbcc54bdSEnrico Perla - Sun Microsystems 		if (strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) {
7679bbcc54bdSEnrico Perla - Sun Microsystems 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
7680bbcc54bdSEnrico Perla - Sun Microsystems 			    menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD],
7681bbcc54bdSEnrico Perla - Sun Microsystems 			    module);
7682bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->cmd);
7683bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->arg);
7684bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->line);
7685bbcc54bdSEnrico Perla - Sun Microsystems 			lp->cmd = s_strdup(menu_cmds[MODULE_DOLLAR_CMD]);
7686bbcc54bdSEnrico Perla - Sun Microsystems 			lp->arg = s_strdup(module);
7687bbcc54bdSEnrico Perla - Sun Microsystems 			lp->line = s_strdup(linebuf);
7688bbcc54bdSEnrico Perla - Sun Microsystems 			lp = lp->next;
7689bbcc54bdSEnrico Perla - Sun Microsystems 			ent->flags &= ~BAM_ENTRY_UPGFSMODULE;
7690bbcc54bdSEnrico Perla - Sun Microsystems 			BAM_DPRINTF((D_ADDING_MODULE_DOLLAR, fcn, module));
7691bbcc54bdSEnrico Perla - Sun Microsystems 		}
7692bbcc54bdSEnrico Perla - Sun Microsystems 	}
7693bbcc54bdSEnrico Perla - Sun Microsystems 
7694eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_RET, fcn, i));
76958c1b6884Sszhou 	return (i);
76968c1b6884Sszhou }
76978c1b6884Sszhou 
7698eb2bd662Svikram int
7699eb2bd662Svikram root_optional(char *osroot, char *menu_root)
7700eb2bd662Svikram {
7701eb2bd662Svikram 	char			*ospecial;
7702eb2bd662Svikram 	char			*mspecial;
7703eb2bd662Svikram 	char			*slash;
7704eb2bd662Svikram 	int			root_opt;
7705eb2bd662Svikram 	int			ret1;
7706eb2bd662Svikram 	int			ret2;
7707eb2bd662Svikram 	const char		*fcn = "root_optional()";
7708eb2bd662Svikram 
7709eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, menu_root));
7710eb2bd662Svikram 
7711eb2bd662Svikram 	/*
7712eb2bd662Svikram 	 * For all filesystems except ZFS, a straight compare of osroot
7713eb2bd662Svikram 	 * and menu_root will tell us if root is optional.
7714eb2bd662Svikram 	 * For ZFS, the situation is complicated by the fact that
7715eb2bd662Svikram 	 * menu_root and osroot are always different
7716eb2bd662Svikram 	 */
7717eb2bd662Svikram 	ret1 = is_zfs(osroot);
7718eb2bd662Svikram 	ret2 = is_zfs(menu_root);
7719eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPT_NOT_ZFS", ret1 = 0);
7720eb2bd662Svikram 	if (!ret1 || !ret2) {
7721eb2bd662Svikram 		BAM_DPRINTF((D_ROOT_OPT_NOT_ZFS, fcn, osroot, menu_root));
7722eb2bd662Svikram 		root_opt = (strcmp(osroot, menu_root) == 0);
7723eb2bd662Svikram 		goto out;
7724eb2bd662Svikram 	}
7725eb2bd662Svikram 
7726eb2bd662Svikram 	ospecial = get_special(osroot);
7727eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_OSPECIAL", ospecial = NULL);
7728eb2bd662Svikram 	if (ospecial == NULL) {
7729eb2bd662Svikram 		bam_error(GET_OSROOT_SPECIAL_ERR, osroot);
7730eb2bd662Svikram 		return (0);
7731eb2bd662Svikram 	}
7732eb2bd662Svikram 	BAM_DPRINTF((D_ROOT_OPTIONAL_OSPECIAL, fcn, ospecial, osroot));
7733eb2bd662Svikram 
7734eb2bd662Svikram 	mspecial = get_special(menu_root);
7735eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_MSPECIAL", mspecial = NULL);
7736eb2bd662Svikram 	if (mspecial == NULL) {
7737eb2bd662Svikram 		bam_error(GET_MENU_ROOT_SPECIAL_ERR, menu_root);
7738eb2bd662Svikram 		free(ospecial);
7739eb2bd662Svikram 		return (0);
7740eb2bd662Svikram 	}
7741eb2bd662Svikram 	BAM_DPRINTF((D_ROOT_OPTIONAL_MSPECIAL, fcn, mspecial, menu_root));
7742eb2bd662Svikram 
7743eb2bd662Svikram 	slash = strchr(ospecial, '/');
7744eb2bd662Svikram 	if (slash)
7745eb2bd662Svikram 		*slash = '\0';
7746eb2bd662Svikram 	BAM_DPRINTF((D_ROOT_OPTIONAL_FIXED_OSPECIAL, fcn, ospecial, osroot));
7747eb2bd662Svikram 
7748eb2bd662Svikram 	root_opt = (strcmp(ospecial, mspecial) == 0);
7749eb2bd662Svikram 
7750eb2bd662Svikram 	free(ospecial);
7751eb2bd662Svikram 	free(mspecial);
7752eb2bd662Svikram 
7753eb2bd662Svikram out:
7754eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_NO", root_opt = 0);
7755eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_YES", root_opt = 1);
7756eb2bd662Svikram 	if (root_opt) {
7757eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7758eb2bd662Svikram 	} else {
7759eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7760eb2bd662Svikram 	}
7761eb2bd662Svikram 
7762eb2bd662Svikram 	return (root_opt);
7763eb2bd662Svikram }
7764eb2bd662Svikram 
77657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
77667c478bd9Sstevel@tonic-gate static error_t
7767eb2bd662Svikram update_entry(menu_t *mp, char *menu_root, char *osdev)
77687c478bd9Sstevel@tonic-gate {
77697c478bd9Sstevel@tonic-gate 	int		entry;
7770eb2bd662Svikram 	char		*grubsign;
7771eb2bd662Svikram 	char		*grubroot;
7772eb2bd662Svikram 	char		*title;
7773eb2bd662Svikram 	char		osroot[PATH_MAX];
7774eb2bd662Svikram 	char		*failsafe_kernel = NULL;
77758c1b6884Sszhou 	struct stat	sbuf;
77768c1b6884Sszhou 	char		failsafe[256];
7777bbcc54bdSEnrico Perla - Sun Microsystems 	char		failsafe_64[256];
7778eb2bd662Svikram 	int		ret;
7779eb2bd662Svikram 	const char	*fcn = "update_entry()";
77807c478bd9Sstevel@tonic-gate 
77817c478bd9Sstevel@tonic-gate 	assert(mp);
7782eb2bd662Svikram 	assert(menu_root);
7783eb2bd662Svikram 	assert(osdev);
7784eb2bd662Svikram 	assert(bam_root);
77857c478bd9Sstevel@tonic-gate 
7786eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY3, fcn, menu_root, osdev, bam_root));
7787eb2bd662Svikram 
7788eb2bd662Svikram 	(void) strlcpy(osroot, bam_root, sizeof (osroot));
7789eb2bd662Svikram 
77907c478bd9Sstevel@tonic-gate 	title = get_title(osroot);
7791eb2bd662Svikram 	assert(title);
77927c478bd9Sstevel@tonic-gate 
7793eb2bd662Svikram 	grubsign = get_grubsign(osroot, osdev);
7794eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBSIGN_FAIL", grubsign = NULL);
7795eb2bd662Svikram 	if (grubsign == NULL) {
7796eb2bd662Svikram 		bam_error(GET_GRUBSIGN_ERROR, osroot, osdev);
77977c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
77987c478bd9Sstevel@tonic-gate 	}
7799eb2bd662Svikram 
7800eb2bd662Svikram 	/*
7801eb2bd662Svikram 	 * It is not a fatal error if get_grubroot() fails
7802eb2bd662Svikram 	 * We no longer rely on biosdev to populate the
7803eb2bd662Svikram 	 * menu
7804eb2bd662Svikram 	 */
7805eb2bd662Svikram 	grubroot = get_grubroot(osroot, osdev, menu_root);
7806eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBROOT_FAIL", grubroot = NULL);
7807eb2bd662Svikram 	if (grubroot) {
7808eb2bd662Svikram 		BAM_DPRINTF((D_GET_GRUBROOT_SUCCESS,
7809eb2bd662Svikram 		    fcn, osroot, osdev, menu_root));
7810eb2bd662Svikram 	} else {
7811eb2bd662Svikram 		BAM_DPRINTF((D_GET_GRUBROOT_FAILURE,
7812eb2bd662Svikram 		    fcn, osroot, osdev, menu_root));
78137c478bd9Sstevel@tonic-gate 	}
78147c478bd9Sstevel@tonic-gate 
78157c478bd9Sstevel@tonic-gate 	/* add the entry for normal Solaris */
7816eb2bd662Svikram 	INJECT_ERROR1("UPDATE_ENTRY_MULTIBOOT",
7817eb2bd662Svikram 	    bam_direct = BAM_DIRECT_MULTIBOOT);
7818ae115bc7Smrj 	if (bam_direct == BAM_DIRECT_DBOOT) {
7819eb2bd662Svikram 		entry = update_boot_entry(mp, title, grubsign, grubroot,
7820e7cbe64fSgw25295 		    (bam_zfs ? DIRECT_BOOT_KERNEL_ZFS : DIRECT_BOOT_KERNEL),
7821eb2bd662Svikram 		    NULL, DIRECT_BOOT_ARCHIVE,
7822eb2bd662Svikram 		    root_optional(osroot, menu_root));
7823eb2bd662Svikram 		BAM_DPRINTF((D_UPDATED_BOOT_ENTRY, fcn, bam_zfs, grubsign));
7824843e1988Sjohnlev 		if ((entry != BAM_ERROR) && (bam_is_hv == BAM_HV_PRESENT)) {
7825eb2bd662Svikram 			(void) update_boot_entry(mp, NEW_HV_ENTRY, grubsign,
7826eb2bd662Svikram 			    grubroot, XEN_MENU, bam_zfs ?
7827eb2bd662Svikram 			    XEN_KERNEL_MODULE_LINE_ZFS : XEN_KERNEL_MODULE_LINE,
7828eb2bd662Svikram 			    DIRECT_BOOT_ARCHIVE,
7829eb2bd662Svikram 			    root_optional(osroot, menu_root));
7830eb2bd662Svikram 			BAM_DPRINTF((D_UPDATED_HV_ENTRY,
7831eb2bd662Svikram 			    fcn, bam_zfs, grubsign));
7832ae115bc7Smrj 		}
7833843e1988Sjohnlev 	} else {
7834eb2bd662Svikram 		entry = update_boot_entry(mp, title, grubsign, grubroot,
7835eb2bd662Svikram 		    MULTI_BOOT, NULL, MULTIBOOT_ARCHIVE,
7836eb2bd662Svikram 		    root_optional(osroot, menu_root));
7837eb2bd662Svikram 
7838eb2bd662Svikram 		BAM_DPRINTF((D_UPDATED_MULTIBOOT_ENTRY, fcn, grubsign));
7839843e1988Sjohnlev 	}
78407c478bd9Sstevel@tonic-gate 
7841843e1988Sjohnlev 	/*
7842843e1988Sjohnlev 	 * Add the entry for failsafe archive.  On a bfu'd system, the
7843843e1988Sjohnlev 	 * failsafe may be different than the installed kernel.
7844843e1988Sjohnlev 	 */
7845eb2bd662Svikram 	(void) snprintf(failsafe, sizeof (failsafe), "%s%s",
7846bbcc54bdSEnrico Perla - Sun Microsystems 	    osroot, FAILSAFE_ARCHIVE_32);
7847bbcc54bdSEnrico Perla - Sun Microsystems 	(void) snprintf(failsafe_64, sizeof (failsafe_64), "%s%s",
7848bbcc54bdSEnrico Perla - Sun Microsystems 	    osroot, FAILSAFE_ARCHIVE_64);
7849bbcc54bdSEnrico Perla - Sun Microsystems 
7850bbcc54bdSEnrico Perla - Sun Microsystems 	/*
7851bbcc54bdSEnrico Perla - Sun Microsystems 	 * Check if at least one of the two archives exists
7852bbcc54bdSEnrico Perla - Sun Microsystems 	 * Using $ISADIR as the default line, we have an entry which works
7853bbcc54bdSEnrico Perla - Sun Microsystems 	 * for both the cases.
7854bbcc54bdSEnrico Perla - Sun Microsystems 	 */
7855bbcc54bdSEnrico Perla - Sun Microsystems 
7856bbcc54bdSEnrico Perla - Sun Microsystems 	if (stat(failsafe, &sbuf) == 0 || stat(failsafe_64, &sbuf) == 0) {
785760d0a590Srscott 
785860d0a590Srscott 		/* Figure out where the kernel line should point */
785960d0a590Srscott 		(void) snprintf(failsafe, sizeof (failsafe), "%s%s", osroot,
7860bbcc54bdSEnrico Perla - Sun Microsystems 		    DIRECT_BOOT_FAILSAFE_32);
7861bbcc54bdSEnrico Perla - Sun Microsystems 		(void) snprintf(failsafe_64, sizeof (failsafe_64), "%s%s",
7862bbcc54bdSEnrico Perla - Sun Microsystems 		    osroot, DIRECT_BOOT_FAILSAFE_64);
7863bbcc54bdSEnrico Perla - Sun Microsystems 		if (stat(failsafe, &sbuf) == 0 ||
7864bbcc54bdSEnrico Perla - Sun Microsystems 		    stat(failsafe_64, &sbuf) == 0) {
7865963390b4Svikram 			failsafe_kernel = DIRECT_BOOT_FAILSAFE_LINE;
786660d0a590Srscott 		} else {
786760d0a590Srscott 			(void) snprintf(failsafe, sizeof (failsafe), "%s%s",
786860d0a590Srscott 			    osroot, MULTI_BOOT_FAILSAFE);
786960d0a590Srscott 			if (stat(failsafe, &sbuf) == 0) {
787060d0a590Srscott 				failsafe_kernel = MULTI_BOOT_FAILSAFE_LINE;
787160d0a590Srscott 			}
787260d0a590Srscott 		}
787360d0a590Srscott 		if (failsafe_kernel != NULL) {
7874eb2bd662Svikram 			(void) update_boot_entry(mp, FAILSAFE_TITLE, grubsign,
7875eb2bd662Svikram 			    grubroot, failsafe_kernel, NULL, FAILSAFE_ARCHIVE,
7876eb2bd662Svikram 			    root_optional(osroot, menu_root));
7877eb2bd662Svikram 			BAM_DPRINTF((D_UPDATED_FAILSAFE_ENTRY, fcn,
7878eb2bd662Svikram 			    failsafe_kernel));
787960d0a590Srscott 		}
7880ae115bc7Smrj 	}
7881eb2bd662Svikram 	free(grubroot);
78827c478bd9Sstevel@tonic-gate 
7883eb2bd662Svikram 	INJECT_ERROR1("UPDATE_ENTRY_ERROR", entry = BAM_ERROR);
78847c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
7885eb2bd662Svikram 		bam_error(FAILED_TO_ADD_BOOT_ENTRY, title, grubsign);
7886eb2bd662Svikram 		free(grubsign);
78877c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
78887c478bd9Sstevel@tonic-gate 	}
7889eb2bd662Svikram 	free(grubsign);
7890eb2bd662Svikram 
7891eb2bd662Svikram 	update_numbering(mp);
7892eb2bd662Svikram 	ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
7893eb2bd662Svikram 	INJECT_ERROR1("SET_DEFAULT_ERROR", ret = BAM_ERROR);
7894eb2bd662Svikram 	if (ret == BAM_ERROR) {
7895eb2bd662Svikram 		bam_error(SET_DEFAULT_FAILED, entry);
7896eb2bd662Svikram 	}
7897eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
78987c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
78997c478bd9Sstevel@tonic-gate }
79007c478bd9Sstevel@tonic-gate 
79018c1b6884Sszhou static void
7902ae115bc7Smrj save_default_entry(menu_t *mp, const char *which)
79038c1b6884Sszhou {
7904eb2bd662Svikram 	int		lineNum;
7905eb2bd662Svikram 	int		entryNum;
79068c1b6884Sszhou 	int		entry = 0;	/* default is 0 */
79078c1b6884Sszhou 	char		linebuf[BAM_MAXLINE];
79088c1b6884Sszhou 	line_t		*lp = mp->curdefault;
7909eb2bd662Svikram 	const char	*fcn = "save_default_entry()";
79108c1b6884Sszhou 
7911bff269d0Svikram 	if (mp->start) {
7912bff269d0Svikram 		lineNum = mp->end->lineNum;
7913bff269d0Svikram 		entryNum = mp->end->entryNum;
7914bff269d0Svikram 	} else {
7915bff269d0Svikram 		lineNum = LINE_INIT;
7916bff269d0Svikram 		entryNum = ENTRY_INIT;
7917bff269d0Svikram 	}
7918bff269d0Svikram 
79198c1b6884Sszhou 	if (lp)
79208c1b6884Sszhou 		entry = s_strtol(lp->arg);
79218c1b6884Sszhou 
7922ae115bc7Smrj 	(void) snprintf(linebuf, sizeof (linebuf), "#%s%d", which, entry);
7923eb2bd662Svikram 	BAM_DPRINTF((D_SAVING_DEFAULT_TO, fcn, linebuf));
79248c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
7925eb2bd662Svikram 	BAM_DPRINTF((D_SAVED_DEFAULT_TO, fcn, lineNum, entryNum));
79268c1b6884Sszhou }
79278c1b6884Sszhou 
79288c1b6884Sszhou static void
7929ae115bc7Smrj restore_default_entry(menu_t *mp, const char *which, line_t *lp)
79308c1b6884Sszhou {
79318c1b6884Sszhou 	int		entry;
79328c1b6884Sszhou 	char		*str;
7933eb2bd662Svikram 	const char	*fcn = "restore_default_entry()";
79348c1b6884Sszhou 
7935eb2bd662Svikram 	if (lp == NULL) {
7936eb2bd662Svikram 		BAM_DPRINTF((D_RESTORE_DEFAULT_NULL, fcn));
79378c1b6884Sszhou 		return;		/* nothing to restore */
7938eb2bd662Svikram 	}
7939eb2bd662Svikram 
7940eb2bd662Svikram 	BAM_DPRINTF((D_RESTORE_DEFAULT_STR, fcn, which));
79418c1b6884Sszhou 
7942ae115bc7Smrj 	str = lp->arg + strlen(which);
79438c1b6884Sszhou 	entry = s_strtol(str);
79448c1b6884Sszhou 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
79458c1b6884Sszhou 
7946eb2bd662Svikram 	BAM_DPRINTF((D_RESTORED_DEFAULT_TO, fcn, entry));
7947eb2bd662Svikram 
79488c1b6884Sszhou 	/* delete saved old default line */
79498c1b6884Sszhou 	unlink_line(mp, lp);
79508c1b6884Sszhou 	line_free(lp);
79518c1b6884Sszhou }
79528c1b6884Sszhou 
79537c478bd9Sstevel@tonic-gate /*
79547c478bd9Sstevel@tonic-gate  * This function is for supporting reboot with args.
79557c478bd9Sstevel@tonic-gate  * The opt value can be:
79567c478bd9Sstevel@tonic-gate  * NULL		delete temp entry, if present
7957eb2bd662Svikram  * entry=<n>	switches default entry to <n>
79587c478bd9Sstevel@tonic-gate  * else		treated as boot-args and setup a temperary menu entry
79597c478bd9Sstevel@tonic-gate  *		and make it the default
7960eb2bd662Svikram  * Note that we are always rebooting the current OS instance
7961eb2bd662Svikram  * so osroot == / always.
79627c478bd9Sstevel@tonic-gate  */
79637c478bd9Sstevel@tonic-gate #define	REBOOT_TITLE	"Solaris_reboot_transient"
79647c478bd9Sstevel@tonic-gate 
79658c1b6884Sszhou /*ARGSUSED*/
79667c478bd9Sstevel@tonic-gate static error_t
7967eb2bd662Svikram update_temp(menu_t *mp, char *dummy, char *opt)
79687c478bd9Sstevel@tonic-gate {
79697c478bd9Sstevel@tonic-gate 	int		entry;
7970eb2bd662Svikram 	char		*osdev;
7971eb2bd662Svikram 	char		*fstype;
7972eb2bd662Svikram 	char		*sign;
7973eb2bd662Svikram 	char		*opt_ptr;
7974eb2bd662Svikram 	char		*path;
7975ae115bc7Smrj 	char		kernbuf[BUFSIZ];
7976ae115bc7Smrj 	char		args_buf[BUFSIZ];
7977eb2bd662Svikram 	char		signbuf[PATH_MAX];
7978eb2bd662Svikram 	int		ret;
7979eb2bd662Svikram 	const char	*fcn = "update_temp()";
79807c478bd9Sstevel@tonic-gate 
79817c478bd9Sstevel@tonic-gate 	assert(mp);
7982eb2bd662Svikram 	assert(dummy == NULL);
7983eb2bd662Svikram 
7984eb2bd662Svikram 	/* opt can be NULL */
7985eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, opt ? opt : "<NULL>"));
7986eb2bd662Svikram 	BAM_DPRINTF((D_BAM_ROOT, fcn, bam_alt_root, bam_root));
7987eb2bd662Svikram 
7988eb2bd662Svikram 	if (bam_alt_root || bam_rootlen != 1 ||
7989eb2bd662Svikram 	    strcmp(bam_root, "/") != 0 ||
7990eb2bd662Svikram 	    strcmp(rootbuf, "/") != 0) {
7991eb2bd662Svikram 		bam_error(ALT_ROOT_INVALID, bam_root);
7992eb2bd662Svikram 		return (BAM_ERROR);
7993eb2bd662Svikram 	}
79947c478bd9Sstevel@tonic-gate 
79958c1b6884Sszhou 	/* If no option, delete exiting reboot menu entry */
79968c1b6884Sszhou 	if (opt == NULL) {
7997eb2bd662Svikram 		entry_t		*ent;
7998eb2bd662Svikram 		BAM_DPRINTF((D_OPT_NULL, fcn));
7999eb2bd662Svikram 		ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL,
8000eb2bd662Svikram 		    NULL, NULL, 0, &entry);
8001eb2bd662Svikram 		if (ent == NULL) {	/* not found is ok */
8002eb2bd662Svikram 			BAM_DPRINTF((D_TRANSIENT_NOTFOUND, fcn));
80038c1b6884Sszhou 			return (BAM_SUCCESS);
8004eb2bd662Svikram 		}
80058c1b6884Sszhou 		(void) do_delete(mp, entry);
8006ae115bc7Smrj 		restore_default_entry(mp, BAM_OLDDEF, mp->olddefault);
8007ae115bc7Smrj 		mp->olddefault = NULL;
8008eb2bd662Svikram 		BAM_DPRINTF((D_RESTORED_DEFAULT, fcn));
8009eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
80108c1b6884Sszhou 		return (BAM_WRITE);
80118c1b6884Sszhou 	}
80128c1b6884Sszhou 
80138c1b6884Sszhou 	/* if entry= is specified, set the default entry */
8014eb2bd662Svikram 	if (strncmp(opt, "entry=", strlen("entry=")) == 0) {
8015eb2bd662Svikram 		int entryNum = s_strtol(opt + strlen("entry="));
8016eb2bd662Svikram 		BAM_DPRINTF((D_ENTRY_EQUALS, fcn, opt));
8017eb2bd662Svikram 		if (selector(mp, opt, &entry, NULL) == BAM_SUCCESS) {
80187c478bd9Sstevel@tonic-gate 			/* this is entry=# option */
8019eb2bd662Svikram 			ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
8020eb2bd662Svikram 			BAM_DPRINTF((D_ENTRY_SET_IS, fcn, entry, ret));
8021eb2bd662Svikram 			return (ret);
8022eb2bd662Svikram 		} else {
8023eb2bd662Svikram 			bam_error(SET_DEFAULT_FAILED, entryNum);
8024eb2bd662Svikram 			return (BAM_ERROR);
8025eb2bd662Svikram 		}
80267c478bd9Sstevel@tonic-gate 	}
80277c478bd9Sstevel@tonic-gate 
80287c478bd9Sstevel@tonic-gate 	/*
8029eb2bd662Svikram 	 * add a new menu entry based on opt and make it the default
8030b610f78eSvikram 	 */
8031eb2bd662Svikram 
8032eb2bd662Svikram 	fstype = get_fstype("/");
8033eb2bd662Svikram 	INJECT_ERROR1("REBOOT_FSTYPE_NULL", fstype = NULL);
8034eb2bd662Svikram 	if (fstype == NULL) {
8035eb2bd662Svikram 		bam_error(REBOOT_FSTYPE_FAILED);
8036eb2bd662Svikram 		return (BAM_ERROR);
80377c478bd9Sstevel@tonic-gate 	}
8038eb2bd662Svikram 
8039eb2bd662Svikram 	osdev = get_special("/");
8040eb2bd662Svikram 	INJECT_ERROR1("REBOOT_SPECIAL_NULL", osdev = NULL);
8041eb2bd662Svikram 	if (osdev == NULL) {
8042eb2bd662Svikram 		free(fstype);
8043eb2bd662Svikram 		bam_error(REBOOT_SPECIAL_FAILED);
8044eb2bd662Svikram 		return (BAM_ERROR);
8045b610f78eSvikram 	}
8046eb2bd662Svikram 
8047eb2bd662Svikram 	sign = find_existing_sign("/", osdev, fstype);
8048eb2bd662Svikram 	INJECT_ERROR1("REBOOT_SIGN_NULL", sign = NULL);
8049eb2bd662Svikram 	if (sign == NULL) {
8050eb2bd662Svikram 		free(fstype);
8051eb2bd662Svikram 		free(osdev);
8052eb2bd662Svikram 		bam_error(REBOOT_SIGN_FAILED);
8053eb2bd662Svikram 		return (BAM_ERROR);
8054eb2bd662Svikram 	}
8055eb2bd662Svikram 
8056eb2bd662Svikram 	free(osdev);
8057eb2bd662Svikram 	(void) strlcpy(signbuf, sign, sizeof (signbuf));
8058eb2bd662Svikram 	free(sign);
8059eb2bd662Svikram 
8060eb2bd662Svikram 	assert(strchr(signbuf, '(') == NULL && strchr(signbuf, ',') == NULL &&
8061eb2bd662Svikram 	    strchr(signbuf, ')') == NULL);
8062eb2bd662Svikram 
8063eb2bd662Svikram 	/*
8064eb2bd662Svikram 	 * There is no alternate root while doing reboot with args
8065eb2bd662Svikram 	 * This version of bootadm is only delivered with a DBOOT
8066eb2bd662Svikram 	 * version of Solaris.
8067eb2bd662Svikram 	 */
8068eb2bd662Svikram 	INJECT_ERROR1("REBOOT_NOT_DBOOT", bam_direct = BAM_DIRECT_MULTIBOOT);
8069eb2bd662Svikram 	if (bam_direct != BAM_DIRECT_DBOOT) {
80709bd6d269SLin Ling 		free(fstype);
8071eb2bd662Svikram 		bam_error(REBOOT_DIRECT_FAILED);
80727c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
80737c478bd9Sstevel@tonic-gate 	}
80747c478bd9Sstevel@tonic-gate 
80757c478bd9Sstevel@tonic-gate 	/* add an entry for Solaris reboot */
8076ae115bc7Smrj 	if (opt[0] == '-') {
8077ae115bc7Smrj 		/* It's an option - first see if boot-file is set */
8078eb2bd662Svikram 		ret = get_kernel(mp, KERNEL_CMD, kernbuf, sizeof (kernbuf));
8079eb2bd662Svikram 		INJECT_ERROR1("REBOOT_GET_KERNEL", ret = BAM_ERROR);
8080eb2bd662Svikram 		if (ret != BAM_SUCCESS) {
80819bd6d269SLin Ling 			free(fstype);
8082eb2bd662Svikram 			bam_error(REBOOT_GET_KERNEL_FAILED);
8083ae115bc7Smrj 			return (BAM_ERROR);
8084eb2bd662Svikram 		}
8085ae115bc7Smrj 		if (kernbuf[0] == '\0')
8086eb2bd662Svikram 			(void) strlcpy(kernbuf, DIRECT_BOOT_KERNEL,
8087eb2bd662Svikram 			    sizeof (kernbuf));
80889bd6d269SLin Ling 		/*
80899bd6d269SLin Ling 		 * If this is a zfs file system and kernbuf does not
80909bd6d269SLin Ling 		 * have "-B $ZFS-BOOTFS" string yet, add it.
80919bd6d269SLin Ling 		 */
80929bd6d269SLin Ling 		if (strcmp(fstype, "zfs") == 0 && !strstr(kernbuf, ZFS_BOOT)) {
80939bd6d269SLin Ling 			(void) strlcat(kernbuf, " ", sizeof (kernbuf));
80949bd6d269SLin Ling 			(void) strlcat(kernbuf, ZFS_BOOT, sizeof (kernbuf));
80959bd6d269SLin Ling 		}
8096eb2bd662Svikram 		(void) strlcat(kernbuf, " ", sizeof (kernbuf));
8097eb2bd662Svikram 		(void) strlcat(kernbuf, opt, sizeof (kernbuf));
8098eb2bd662Svikram 		BAM_DPRINTF((D_REBOOT_OPTION, fcn, kernbuf));
8099ae115bc7Smrj 	} else if (opt[0] == '/') {
8100455710d3Srscott 		/* It's a full path, so write it out. */
8101eb2bd662Svikram 		(void) strlcpy(kernbuf, opt, sizeof (kernbuf));
8102455710d3Srscott 
8103455710d3Srscott 		/*
8104455710d3Srscott 		 * If someone runs:
8105455710d3Srscott 		 *
8106455710d3Srscott 		 *	# eeprom boot-args='-kd'
8107455710d3Srscott 		 *	# reboot /platform/i86pc/kernel/unix
8108455710d3Srscott 		 *
8109455710d3Srscott 		 * we want to use the boot-args as part of the boot
8110455710d3Srscott 		 * line.  On the other hand, if someone runs:
8111455710d3Srscott 		 *
8112455710d3Srscott 		 *	# reboot "/platform/i86pc/kernel/unix -kd"
8113455710d3Srscott 		 *
8114455710d3Srscott 		 * we don't need to mess with boot-args.  If there's
8115455710d3Srscott 		 * no space in the options string, assume we're in the
8116455710d3Srscott 		 * first case.
8117455710d3Srscott 		 */
8118455710d3Srscott 		if (strchr(opt, ' ') == NULL) {
8119eb2bd662Svikram 			ret = get_kernel(mp, ARGS_CMD, args_buf,
8120eb2bd662Svikram 			    sizeof (args_buf));
8121eb2bd662Svikram 			INJECT_ERROR1("REBOOT_GET_ARGS", ret = BAM_ERROR);
8122eb2bd662Svikram 			if (ret != BAM_SUCCESS) {
81239bd6d269SLin Ling 				free(fstype);
8124eb2bd662Svikram 				bam_error(REBOOT_GET_ARGS_FAILED);
8125455710d3Srscott 				return (BAM_ERROR);
8126eb2bd662Svikram 			}
8127455710d3Srscott 
8128455710d3Srscott 			if (args_buf[0] != '\0') {
8129eb2bd662Svikram 				(void) strlcat(kernbuf, " ", sizeof (kernbuf));
8130455710d3Srscott 				(void) strlcat(kernbuf, args_buf,
8131eb2bd662Svikram 				    sizeof (kernbuf));
8132455710d3Srscott 			}
8133455710d3Srscott 		}
8134eb2bd662Svikram 		BAM_DPRINTF((D_REBOOT_ABSPATH, fcn, kernbuf));
8135ae115bc7Smrj 	} else {
8136455710d3Srscott 		/*
8137455710d3Srscott 		 * It may be a partial path, or it may be a partial
8138455710d3Srscott 		 * path followed by options.  Assume that only options
8139455710d3Srscott 		 * follow a space.  If someone sends us a kernel path
8140455710d3Srscott 		 * that includes a space, they deserve to be broken.
8141455710d3Srscott 		 */
8142455710d3Srscott 		opt_ptr = strchr(opt, ' ');
8143455710d3Srscott 		if (opt_ptr != NULL) {
8144455710d3Srscott 			*opt_ptr = '\0';
8145455710d3Srscott 		}
8146455710d3Srscott 
8147ae115bc7Smrj 		path = expand_path(opt);
8148ae115bc7Smrj 		if (path != NULL) {
8149eb2bd662Svikram 			(void) strlcpy(kernbuf, path, sizeof (kernbuf));
8150ae115bc7Smrj 			free(path);
8151455710d3Srscott 
8152455710d3Srscott 			/*
8153455710d3Srscott 			 * If there were options given, use those.
8154455710d3Srscott 			 * Otherwise, copy over the default options.
8155455710d3Srscott 			 */
8156455710d3Srscott 			if (opt_ptr != NULL) {
8157455710d3Srscott 				/* Restore the space in opt string */
8158455710d3Srscott 				*opt_ptr = ' ';
8159455710d3Srscott 				(void) strlcat(kernbuf, opt_ptr,
8160eb2bd662Svikram 				    sizeof (kernbuf));
8161455710d3Srscott 			} else {
8162eb2bd662Svikram 				ret = get_kernel(mp, ARGS_CMD, args_buf,
8163eb2bd662Svikram 				    sizeof (args_buf));
8164eb2bd662Svikram 				INJECT_ERROR1("UPDATE_TEMP_PARTIAL_ARGS",
8165eb2bd662Svikram 				    ret = BAM_ERROR);
8166eb2bd662Svikram 				if (ret != BAM_SUCCESS) {
81679bd6d269SLin Ling 					free(fstype);
8168eb2bd662Svikram 					bam_error(REBOOT_GET_ARGS_FAILED);
8169ae115bc7Smrj 					return (BAM_ERROR);
8170eb2bd662Svikram 				}
8171ae115bc7Smrj 
8172ae115bc7Smrj 				if (args_buf[0] != '\0') {
8173ae115bc7Smrj 					(void) strlcat(kernbuf, " ",
8174eb2bd662Svikram 					    sizeof (kernbuf));
8175ae115bc7Smrj 					(void) strlcat(kernbuf,
8176eb2bd662Svikram 					    args_buf, sizeof (kernbuf));
8177ae115bc7Smrj 				}
8178ae115bc7Smrj 			}
8179eb2bd662Svikram 			BAM_DPRINTF((D_REBOOT_RESOLVED_PARTIAL, fcn, kernbuf));
8180455710d3Srscott 		} else {
81819bd6d269SLin Ling 			free(fstype);
8182455710d3Srscott 			bam_error(UNKNOWN_KERNEL, opt);
8183455710d3Srscott 			bam_print_stderr(UNKNOWN_KERNEL_REBOOT);
8184455710d3Srscott 			return (BAM_ERROR);
8185ae115bc7Smrj 		}
8186ae115bc7Smrj 	}
81879bd6d269SLin Ling 	free(fstype);
8188eb2bd662Svikram 	entry = add_boot_entry(mp, REBOOT_TITLE, signbuf, kernbuf,
8189843e1988Sjohnlev 	    NULL, NULL);
8190eb2bd662Svikram 	INJECT_ERROR1("REBOOT_ADD_BOOT_ENTRY", entry = BAM_ERROR);
81917c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
8192eb2bd662Svikram 		bam_error(REBOOT_WITH_ARGS_ADD_ENTRY_FAILED);
81937c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
81947c478bd9Sstevel@tonic-gate 	}
81958c1b6884Sszhou 
8196ae115bc7Smrj 	save_default_entry(mp, BAM_OLDDEF);
8197eb2bd662Svikram 	ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
8198eb2bd662Svikram 	INJECT_ERROR1("REBOOT_SET_GLOBAL", ret = BAM_ERROR);
8199eb2bd662Svikram 	if (ret == BAM_ERROR) {
8200eb2bd662Svikram 		bam_error(REBOOT_SET_DEFAULT_FAILED, entry);
8201eb2bd662Svikram 	}
8202eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
82037c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
82047c478bd9Sstevel@tonic-gate }
82057c478bd9Sstevel@tonic-gate 
82067c478bd9Sstevel@tonic-gate static error_t
82077c478bd9Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val)
82087c478bd9Sstevel@tonic-gate {
8209eb2bd662Svikram 	line_t		*lp;
8210eb2bd662Svikram 	line_t		*found;
8211eb2bd662Svikram 	line_t		*last;
8212eb2bd662Svikram 	char		*cp;
8213eb2bd662Svikram 	char		*str;
82147c478bd9Sstevel@tonic-gate 	char		prefix[BAM_MAXLINE];
82157c478bd9Sstevel@tonic-gate 	size_t		len;
8216eb2bd662Svikram 	const char	*fcn = "set_global()";
82177c478bd9Sstevel@tonic-gate 
82187c478bd9Sstevel@tonic-gate 	assert(mp);
82197c478bd9Sstevel@tonic-gate 	assert(globalcmd);
82207c478bd9Sstevel@tonic-gate 
8221b610f78eSvikram 	if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) {
8222eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_VAL_NEG", val = -1);
8223eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_MENU_EMPTY", mp->end = NULL);
8224eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_VAL_TOO_BIG", val = 100);
8225b610f78eSvikram 		if (val < 0 || mp->end == NULL || val > mp->end->entryNum) {
8226b610f78eSvikram 			(void) snprintf(prefix, sizeof (prefix), "%d", val);
8227b610f78eSvikram 			bam_error(INVALID_ENTRY, prefix);
8228b610f78eSvikram 			return (BAM_ERROR);
8229b610f78eSvikram 		}
8230b610f78eSvikram 	}
8231b610f78eSvikram 
82327c478bd9Sstevel@tonic-gate 	found = last = NULL;
82337c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
82347c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
82357c478bd9Sstevel@tonic-gate 			continue;
82367c478bd9Sstevel@tonic-gate 
82377c478bd9Sstevel@tonic-gate 		last = lp; /* track the last global found */
82387c478bd9Sstevel@tonic-gate 
8239eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_NULL_CMD", lp->cmd = NULL);
82407c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
82417c478bd9Sstevel@tonic-gate 			bam_error(NO_CMD, lp->lineNum);
82427c478bd9Sstevel@tonic-gate 			continue;
82437c478bd9Sstevel@tonic-gate 		}
82447c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
82457c478bd9Sstevel@tonic-gate 			continue;
82467c478bd9Sstevel@tonic-gate 
8247eb2bd662Svikram 		BAM_DPRINTF((D_FOUND_GLOBAL, fcn, globalcmd));
8248eb2bd662Svikram 
82497c478bd9Sstevel@tonic-gate 		if (found) {
82507c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
82517c478bd9Sstevel@tonic-gate 		}
82527c478bd9Sstevel@tonic-gate 		found = lp;
82537c478bd9Sstevel@tonic-gate 	}
82547c478bd9Sstevel@tonic-gate 
82557c478bd9Sstevel@tonic-gate 	if (found == NULL) {
82567c478bd9Sstevel@tonic-gate 		lp = s_calloc(1, sizeof (line_t));
82577c478bd9Sstevel@tonic-gate 		if (last == NULL) {
82587c478bd9Sstevel@tonic-gate 			lp->next = mp->start;
82597c478bd9Sstevel@tonic-gate 			mp->start = lp;
82607c478bd9Sstevel@tonic-gate 			mp->end = (mp->end) ? mp->end : lp;
82617c478bd9Sstevel@tonic-gate 		} else {
82627c478bd9Sstevel@tonic-gate 			lp->next = last->next;
82637c478bd9Sstevel@tonic-gate 			last->next = lp;
82647c478bd9Sstevel@tonic-gate 			if (lp->next == NULL)
82657c478bd9Sstevel@tonic-gate 				mp->end = lp;
82667c478bd9Sstevel@tonic-gate 		}
82677c478bd9Sstevel@tonic-gate 		lp->flags = BAM_GLOBAL; /* other fields not needed for writes */
82687c478bd9Sstevel@tonic-gate 		len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]);
82697c478bd9Sstevel@tonic-gate 		len += 10;	/* val < 10 digits */
82707c478bd9Sstevel@tonic-gate 		lp->line = s_calloc(1, len);
82717c478bd9Sstevel@tonic-gate 		(void) snprintf(lp->line, len, "%s%s%d",
82727c478bd9Sstevel@tonic-gate 		    globalcmd, menu_cmds[SEP_CMD], val);
8273eb2bd662Svikram 		BAM_DPRINTF((D_SET_GLOBAL_WROTE_NEW, fcn, lp->line));
8274eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
82757c478bd9Sstevel@tonic-gate 		return (BAM_WRITE);
82767c478bd9Sstevel@tonic-gate 	}
82777c478bd9Sstevel@tonic-gate 
82787c478bd9Sstevel@tonic-gate 	/*
82797c478bd9Sstevel@tonic-gate 	 * We are changing an existing entry. Retain any prefix whitespace,
82807c478bd9Sstevel@tonic-gate 	 * but overwrite everything else. This preserves tabs added for
82817c478bd9Sstevel@tonic-gate 	 * readability.
82827c478bd9Sstevel@tonic-gate 	 */
82837c478bd9Sstevel@tonic-gate 	str = found->line;
82847c478bd9Sstevel@tonic-gate 	cp = prefix;
82857c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
82867c478bd9Sstevel@tonic-gate 		*(cp++) = *(str++);
82877c478bd9Sstevel@tonic-gate 	*cp = '\0'; /* Terminate prefix */
82887c478bd9Sstevel@tonic-gate 	len = strlen(prefix) + strlen(globalcmd);
82897c478bd9Sstevel@tonic-gate 	len += strlen(menu_cmds[SEP_CMD]) + 10;
82907c478bd9Sstevel@tonic-gate 
82917c478bd9Sstevel@tonic-gate 	free(found->line);
82927c478bd9Sstevel@tonic-gate 	found->line = s_calloc(1, len);
82937c478bd9Sstevel@tonic-gate 	(void) snprintf(found->line, len,
82947c478bd9Sstevel@tonic-gate 	    "%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val);
82957c478bd9Sstevel@tonic-gate 
8296eb2bd662Svikram 	BAM_DPRINTF((D_SET_GLOBAL_REPLACED, fcn, found->line));
8297eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
82987c478bd9Sstevel@tonic-gate 	return (BAM_WRITE); /* need a write to menu */
82997c478bd9Sstevel@tonic-gate }
83007c478bd9Sstevel@tonic-gate 
8301ae115bc7Smrj /*
8302ae115bc7Smrj  * partial_path may be anything like "kernel/unix" or "kmdb".  Try to
8303455710d3Srscott  * expand it to a full unix path.  The calling function is expected to
8304455710d3Srscott  * output a message if an error occurs and NULL is returned.
8305ae115bc7Smrj  */
8306ae115bc7Smrj static char *
8307ae115bc7Smrj expand_path(const char *partial_path)
8308ae115bc7Smrj {
8309ae115bc7Smrj 	int		new_path_len;
8310eb2bd662Svikram 	char		*new_path;
8311eb2bd662Svikram 	char		new_path2[PATH_MAX];
8312ae115bc7Smrj 	struct stat	sb;
8313eb2bd662Svikram 	const char	*fcn = "expand_path()";
8314ae115bc7Smrj 
8315ae115bc7Smrj 	new_path_len = strlen(partial_path) + 64;
8316ae115bc7Smrj 	new_path = s_calloc(1, new_path_len);
8317ae115bc7Smrj 
8318ae115bc7Smrj 	/* First, try the simplest case - something like "kernel/unix" */
8319ae115bc7Smrj 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s",
8320ae115bc7Smrj 	    partial_path);
8321ae115bc7Smrj 	if (stat(new_path, &sb) == 0) {
8322eb2bd662Svikram 		BAM_DPRINTF((D_EXPAND_PATH, fcn, new_path));
8323ae115bc7Smrj 		return (new_path);
8324ae115bc7Smrj 	}
8325ae115bc7Smrj 
8326ae115bc7Smrj 	if (strcmp(partial_path, "kmdb") == 0) {
8327ae115bc7Smrj 		(void) snprintf(new_path, new_path_len, "%s -k",
8328ae115bc7Smrj 		    DIRECT_BOOT_KERNEL);
8329eb2bd662Svikram 		BAM_DPRINTF((D_EXPAND_PATH, fcn, new_path));
8330ae115bc7Smrj 		return (new_path);
8331ae115bc7Smrj 	}
8332ae115bc7Smrj 
8333ae115bc7Smrj 	/*
8334ae115bc7Smrj 	 * We've quickly reached unsupported usage.  Try once more to
8335ae115bc7Smrj 	 * see if we were just given a glom name.
8336ae115bc7Smrj 	 */
8337ae115bc7Smrj 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s/unix",
8338ae115bc7Smrj 	    partial_path);
8339ae115bc7Smrj 	(void) snprintf(new_path2, PATH_MAX, "/platform/i86pc/%s/amd64/unix",
8340ae115bc7Smrj 	    partial_path);
8341ae115bc7Smrj 	if (stat(new_path, &sb) == 0) {
8342ae115bc7Smrj 		if (stat(new_path2, &sb) == 0) {
8343ae115bc7Smrj 			/*
8344ae115bc7Smrj 			 * We matched both, so we actually
8345ae115bc7Smrj 			 * want to write the $ISADIR version.
8346ae115bc7Smrj 			 */
8347ae115bc7Smrj 			(void) snprintf(new_path, new_path_len,
8348ae115bc7Smrj 			    "/platform/i86pc/kernel/%s/$ISADIR/unix",
8349ae115bc7Smrj 			    partial_path);
8350ae115bc7Smrj 		}
8351eb2bd662Svikram 		BAM_DPRINTF((D_EXPAND_PATH, fcn, new_path));
8352ae115bc7Smrj 		return (new_path);
8353ae115bc7Smrj 	}
8354ae115bc7Smrj 
8355ae115bc7Smrj 	free(new_path);
8356eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_FAILURE, fcn));
8357ae115bc7Smrj 	return (NULL);
8358ae115bc7Smrj }
8359ae115bc7Smrj 
8360ae115bc7Smrj /*
8361ae115bc7Smrj  * The kernel cmd and arg have been changed, so
8362ae115bc7Smrj  * check whether the archive line needs to change.
8363ae115bc7Smrj  */
8364ae115bc7Smrj static void
8365ae115bc7Smrj set_archive_line(entry_t *entryp, line_t *kernelp)
8366ae115bc7Smrj {
8367ae115bc7Smrj 	line_t		*lp = entryp->start;
8368ae115bc7Smrj 	char		*new_archive;
8369ae115bc7Smrj 	menu_cmd_t	m_cmd;
8370eb2bd662Svikram 	const char	*fcn = "set_archive_line()";
8371ae115bc7Smrj 
8372ae115bc7Smrj 	for (; lp != NULL; lp = lp->next) {
8373ae115bc7Smrj 		if (strncmp(lp->cmd, menu_cmds[MODULE_CMD],
8374ae115bc7Smrj 		    sizeof (menu_cmds[MODULE_CMD]) - 1) == 0) {
8375ae115bc7Smrj 			break;
8376ae115bc7Smrj 		}
8377eb2bd662Svikram 
8378eb2bd662Svikram 		INJECT_ERROR1("SET_ARCHIVE_LINE_END_ENTRY", lp = entryp->end);
8379eb2bd662Svikram 		if (lp == entryp->end) {
8380eb2bd662Svikram 			BAM_DPRINTF((D_ARCHIVE_LINE_NONE, fcn,
8381eb2bd662Svikram 			    entryp->entryNum));
8382ae115bc7Smrj 			return;
8383ae115bc7Smrj 		}
8384eb2bd662Svikram 	}
8385eb2bd662Svikram 	INJECT_ERROR1("SET_ARCHIVE_LINE_END_MENU", lp = NULL);
8386eb2bd662Svikram 	if (lp == NULL) {
8387eb2bd662Svikram 		BAM_DPRINTF((D_ARCHIVE_LINE_NONE, fcn, entryp->entryNum));
8388ae115bc7Smrj 		return;
8389eb2bd662Svikram 	}
8390ae115bc7Smrj 
8391ae115bc7Smrj 	if (strstr(kernelp->arg, "$ISADIR") != NULL) {
8392ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE;
8393ae115bc7Smrj 		m_cmd = MODULE_DOLLAR_CMD;
8394ae115bc7Smrj 	} else if (strstr(kernelp->arg, "amd64") != NULL) {
8395ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE_64;
8396ae115bc7Smrj 		m_cmd = MODULE_CMD;
8397ae115bc7Smrj 	} else {
8398ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE_32;
8399ae115bc7Smrj 		m_cmd = MODULE_CMD;
8400ae115bc7Smrj 	}
8401ae115bc7Smrj 
8402eb2bd662Svikram 	if (strcmp(lp->arg, new_archive) == 0) {
8403eb2bd662Svikram 		BAM_DPRINTF((D_ARCHIVE_LINE_NOCHANGE, fcn, lp->arg));
8404ae115bc7Smrj 		return;
8405eb2bd662Svikram 	}
8406ae115bc7Smrj 
8407ae115bc7Smrj 	if (strcmp(lp->cmd, menu_cmds[m_cmd]) != 0) {
8408ae115bc7Smrj 		free(lp->cmd);
8409ae115bc7Smrj 		lp->cmd = s_strdup(menu_cmds[m_cmd]);
8410ae115bc7Smrj 	}
8411ae115bc7Smrj 
8412ae115bc7Smrj 	free(lp->arg);
8413ae115bc7Smrj 	lp->arg = s_strdup(new_archive);
8414ae115bc7Smrj 	update_line(lp);
8415eb2bd662Svikram 	BAM_DPRINTF((D_ARCHIVE_LINE_REPLACED, fcn, lp->line));
8416ae115bc7Smrj }
8417ae115bc7Smrj 
8418ae115bc7Smrj /*
8419ae115bc7Smrj  * Title for an entry to set properties that once went in bootenv.rc.
8420ae115bc7Smrj  */
8421ae115bc7Smrj #define	BOOTENV_RC_TITLE	"Solaris bootenv rc"
8422ae115bc7Smrj 
8423ae115bc7Smrj /*
8424ae115bc7Smrj  * If path is NULL, return the kernel (optnum == KERNEL_CMD) or arguments
8425ae115bc7Smrj  * (optnum == ARGS_CMD) in the argument buf.  If path is a zero-length
8426ae115bc7Smrj  * string, reset the value to the default.  If path is a non-zero-length
8427ae115bc7Smrj  * string, set the kernel or arguments.
8428ae115bc7Smrj  */
8429ae115bc7Smrj static error_t
8430eb2bd662Svikram get_set_kernel(
8431eb2bd662Svikram 	menu_t *mp,
8432eb2bd662Svikram 	menu_cmd_t optnum,
8433eb2bd662Svikram 	char *path,
8434eb2bd662Svikram 	char *buf,
8435eb2bd662Svikram 	size_t bufsize)
8436ae115bc7Smrj {
8437eb2bd662Svikram 	int		entryNum;
8438eb2bd662Svikram 	int		rv = BAM_SUCCESS;
8439eb2bd662Svikram 	int		free_new_path = 0;
8440ae115bc7Smrj 	entry_t		*entryp;
8441eb2bd662Svikram 	line_t		*ptr;
8442eb2bd662Svikram 	line_t		*kernelp;
8443eb2bd662Svikram 	char		*new_arg;
8444eb2bd662Svikram 	char		*old_args;
8445eb2bd662Svikram 	char		*space;
8446eb2bd662Svikram 	char		*new_path;
8447ae115bc7Smrj 	char		old_space;
8448eb2bd662Svikram 	size_t		old_kernel_len;
8449eb2bd662Svikram 	size_t		new_str_len;
8450eb2bd662Svikram 	char		*fstype;
8451eb2bd662Svikram 	char		*osdev;
8452eb2bd662Svikram 	char		*sign;
8453eb2bd662Svikram 	char		signbuf[PATH_MAX];
8454eb2bd662Svikram 	int		ret;
8455eb2bd662Svikram 	const char	*fcn = "get_set_kernel()";
8456ae115bc7Smrj 
8457ae115bc7Smrj 	assert(bufsize > 0);
8458ae115bc7Smrj 
8459ae115bc7Smrj 	ptr = kernelp = NULL;
8460ae115bc7Smrj 	new_arg = old_args = space = NULL;
8461eb2bd662Svikram 	new_path = NULL;
8462ae115bc7Smrj 	buf[0] = '\0';
8463ae115bc7Smrj 
8464eb2bd662Svikram 	INJECT_ERROR1("GET_SET_KERNEL_NOT_DBOOT",
8465eb2bd662Svikram 	    bam_direct = BAM_DIRECT_MULTIBOOT);
8466ae115bc7Smrj 	if (bam_direct != BAM_DIRECT_DBOOT) {
8467ae115bc7Smrj 		bam_error(NOT_DBOOT, optnum == KERNEL_CMD ? "kernel" : "args");
8468ae115bc7Smrj 		return (BAM_ERROR);
8469ae115bc7Smrj 	}
8470ae115bc7Smrj 
8471ae115bc7Smrj 	/*
8472ae115bc7Smrj 	 * If a user changed the default entry to a non-bootadm controlled
8473ae115bc7Smrj 	 * one, we don't want to mess with it.  Just print an error and
8474ae115bc7Smrj 	 * return.
8475ae115bc7Smrj 	 */
8476ae115bc7Smrj 	if (mp->curdefault) {
8477ae115bc7Smrj 		entryNum = s_strtol(mp->curdefault->arg);
8478ae115bc7Smrj 		for (entryp = mp->entries; entryp; entryp = entryp->next) {
8479ae115bc7Smrj 			if (entryp->entryNum == entryNum)
8480ae115bc7Smrj 				break;
8481ae115bc7Smrj 		}
8482ae115bc7Smrj 		if ((entryp != NULL) &&
8483ae115bc7Smrj 		    ((entryp->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU)) == 0)) {
8484ae115bc7Smrj 			bam_error(DEFAULT_NOT_BAM);
8485ae115bc7Smrj 			return (BAM_ERROR);
8486ae115bc7Smrj 		}
8487ae115bc7Smrj 	}
8488ae115bc7Smrj 
8489eb2bd662Svikram 	entryp = find_boot_entry(mp, BOOTENV_RC_TITLE, NULL, NULL, NULL, NULL,
8490eb2bd662Svikram 	    0, &entryNum);
8491ae115bc7Smrj 
8492ae115bc7Smrj 	if (entryp != NULL) {
8493ae115bc7Smrj 		for (ptr = entryp->start; ptr && ptr != entryp->end;
8494ae115bc7Smrj 		    ptr = ptr->next) {
8495ae115bc7Smrj 			if (strncmp(ptr->cmd, menu_cmds[KERNEL_CMD],
8496ae115bc7Smrj 			    sizeof (menu_cmds[KERNEL_CMD]) - 1) == 0) {
8497ae115bc7Smrj 				kernelp = ptr;
8498ae115bc7Smrj 				break;
8499ae115bc7Smrj 			}
8500ae115bc7Smrj 		}
8501ae115bc7Smrj 		if (kernelp == NULL) {
8502ae115bc7Smrj 			bam_error(NO_KERNEL, entryNum);
8503ae115bc7Smrj 			return (BAM_ERROR);
8504ae115bc7Smrj 		}
8505ae115bc7Smrj 
8506ae115bc7Smrj 		old_kernel_len = strcspn(kernelp->arg, " \t");
8507ae115bc7Smrj 		space = old_args = kernelp->arg + old_kernel_len;
8508ae115bc7Smrj 		while ((*old_args == ' ') || (*old_args == '\t'))
8509ae115bc7Smrj 			old_args++;
8510ae115bc7Smrj 	}
8511ae115bc7Smrj 
8512ae115bc7Smrj 	if (path == NULL) {
8513eb2bd662Svikram 		if (entryp == NULL) {
8514eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_NO_RC, fcn));
8515eb2bd662Svikram 			BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
8516ae115bc7Smrj 			return (BAM_SUCCESS);
8517eb2bd662Svikram 		}
8518eb2bd662Svikram 		assert(kernelp);
8519ae115bc7Smrj 		if (optnum == ARGS_CMD) {
8520eb2bd662Svikram 			if (old_args[0] != '\0') {
8521ae115bc7Smrj 				(void) strlcpy(buf, old_args, bufsize);
8522eb2bd662Svikram 				BAM_DPRINTF((D_GET_SET_KERNEL_ARGS, fcn, buf));
8523eb2bd662Svikram 			}
8524ae115bc7Smrj 		} else {
8525ae115bc7Smrj 			/*
8526ae115bc7Smrj 			 * We need to print the kernel, so we just turn the
8527ae115bc7Smrj 			 * first space into a '\0' and print the beginning.
8528ae115bc7Smrj 			 * We don't print anything if it's the default kernel.
8529ae115bc7Smrj 			 */
8530ae115bc7Smrj 			old_space = *space;
8531ae115bc7Smrj 			*space = '\0';
8532eb2bd662Svikram 			if (strcmp(kernelp->arg, DIRECT_BOOT_KERNEL) != 0) {
8533ae115bc7Smrj 				(void) strlcpy(buf, kernelp->arg, bufsize);
8534eb2bd662Svikram 				BAM_DPRINTF((D_GET_SET_KERNEL_KERN, fcn, buf));
8535eb2bd662Svikram 			}
8536ae115bc7Smrj 			*space = old_space;
8537ae115bc7Smrj 		}
8538eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
8539ae115bc7Smrj 		return (BAM_SUCCESS);
8540ae115bc7Smrj 	}
8541ae115bc7Smrj 
8542ae115bc7Smrj 	/*
8543ae115bc7Smrj 	 * First, check if we're resetting an entry to the default.
8544ae115bc7Smrj 	 */
8545ae115bc7Smrj 	if ((path[0] == '\0') ||
8546ae115bc7Smrj 	    ((optnum == KERNEL_CMD) &&
8547ae115bc7Smrj 	    (strcmp(path, DIRECT_BOOT_KERNEL) == 0))) {
8548ae115bc7Smrj 		if ((entryp == NULL) || (kernelp == NULL)) {
8549ae115bc7Smrj 			/* No previous entry, it's already the default */
8550eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_ALREADY, fcn));
8551ae115bc7Smrj 			return (BAM_SUCCESS);
8552ae115bc7Smrj 		}
8553ae115bc7Smrj 
8554ae115bc7Smrj 		/*
8555ae115bc7Smrj 		 * Check if we can delete the entry.  If we're resetting the
8556ae115bc7Smrj 		 * kernel command, and the args is already empty, or if we're
8557ae115bc7Smrj 		 * resetting the args command, and the kernel is already the
8558ae115bc7Smrj 		 * default, we can restore the old default and delete the entry.
8559ae115bc7Smrj 		 */
8560ae115bc7Smrj 		if (((optnum == KERNEL_CMD) &&
8561ae115bc7Smrj 		    ((old_args == NULL) || (old_args[0] == '\0'))) ||
8562ae115bc7Smrj 		    ((optnum == ARGS_CMD) &&
8563ae115bc7Smrj 		    (strncmp(kernelp->arg, DIRECT_BOOT_KERNEL,
8564ae115bc7Smrj 		    sizeof (DIRECT_BOOT_KERNEL) - 1) == 0))) {
8565ae115bc7Smrj 			kernelp = NULL;
8566ae115bc7Smrj 			(void) do_delete(mp, entryNum);
8567ae115bc7Smrj 			restore_default_entry(mp, BAM_OLD_RC_DEF,
8568ae115bc7Smrj 			    mp->old_rc_default);
8569ae115bc7Smrj 			mp->old_rc_default = NULL;
8570ae115bc7Smrj 			rv = BAM_WRITE;
8571eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_RESTORE_DEFAULT, fcn));
8572ae115bc7Smrj 			goto done;
8573ae115bc7Smrj 		}
8574ae115bc7Smrj 
8575ae115bc7Smrj 		if (optnum == KERNEL_CMD) {
8576ae115bc7Smrj 			/*
8577ae115bc7Smrj 			 * At this point, we've already checked that old_args
8578ae115bc7Smrj 			 * and entryp are valid pointers.  The "+ 2" is for
8579ae115bc7Smrj 			 * a space a the string termination character.
8580ae115bc7Smrj 			 */
8581ae115bc7Smrj 			new_str_len = (sizeof (DIRECT_BOOT_KERNEL) - 1) +
8582ae115bc7Smrj 			    strlen(old_args) + 2;
8583ae115bc7Smrj 			new_arg = s_calloc(1, new_str_len);
8584ae115bc7Smrj 			(void) snprintf(new_arg, new_str_len, "%s %s",
8585ae115bc7Smrj 			    DIRECT_BOOT_KERNEL, old_args);
8586ae115bc7Smrj 			free(kernelp->arg);
8587ae115bc7Smrj 			kernelp->arg = new_arg;
8588ae115bc7Smrj 
8589ae115bc7Smrj 			/*
8590ae115bc7Smrj 			 * We have changed the kernel line, so we may need
8591ae115bc7Smrj 			 * to update the archive line as well.
8592ae115bc7Smrj 			 */
8593ae115bc7Smrj 			set_archive_line(entryp, kernelp);
8594eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_RESET_KERNEL_SET_ARG,
8595eb2bd662Svikram 			    fcn, kernelp->arg));
8596ae115bc7Smrj 		} else {
8597ae115bc7Smrj 			/*
8598ae115bc7Smrj 			 * We're resetting the boot args to nothing, so
8599ae115bc7Smrj 			 * we only need to copy the kernel.  We've already
8600ae115bc7Smrj 			 * checked that the kernel is not the default.
8601ae115bc7Smrj 			 */
8602ae115bc7Smrj 			new_arg = s_calloc(1, old_kernel_len + 1);
8603ae115bc7Smrj 			(void) snprintf(new_arg, old_kernel_len + 1, "%s",
8604ae115bc7Smrj 			    kernelp->arg);
8605ae115bc7Smrj 			free(kernelp->arg);
8606ae115bc7Smrj 			kernelp->arg = new_arg;
8607eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_RESET_ARG_SET_KERNEL,
8608eb2bd662Svikram 			    fcn, kernelp->arg));
8609ae115bc7Smrj 		}
8610ae115bc7Smrj 		rv = BAM_WRITE;
8611ae115bc7Smrj 		goto done;
8612ae115bc7Smrj 	}
8613ae115bc7Smrj 
8614ae115bc7Smrj 	/*
8615ae115bc7Smrj 	 * Expand the kernel file to a full path, if necessary
8616ae115bc7Smrj 	 */
8617ae115bc7Smrj 	if ((optnum == KERNEL_CMD) && (path[0] != '/')) {
8618ae115bc7Smrj 		new_path = expand_path(path);
8619ae115bc7Smrj 		if (new_path == NULL) {
8620455710d3Srscott 			bam_error(UNKNOWN_KERNEL, path);
8621eb2bd662Svikram 			BAM_DPRINTF((D_RETURN_FAILURE, fcn));
8622ae115bc7Smrj 			return (BAM_ERROR);
8623ae115bc7Smrj 		}
8624ae115bc7Smrj 		free_new_path = 1;
8625ae115bc7Smrj 	} else {
8626ae115bc7Smrj 		new_path = path;
8627ae115bc7Smrj 		free_new_path = 0;
8628ae115bc7Smrj 	}
8629ae115bc7Smrj 
8630ae115bc7Smrj 	/*
8631ae115bc7Smrj 	 * At this point, we know we're setting a new value.  First, take care
8632ae115bc7Smrj 	 * of the case where there was no previous entry.
8633ae115bc7Smrj 	 */
8634ae115bc7Smrj 	if (entryp == NULL) {
8635eb2bd662Svikram 
8636ae115bc7Smrj 		/* Similar to code in update_temp */
8637eb2bd662Svikram 		fstype = get_fstype("/");
8638eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_FSTYPE", fstype = NULL);
8639eb2bd662Svikram 		if (fstype == NULL) {
8640eb2bd662Svikram 			bam_error(BOOTENV_FSTYPE_FAILED);
8641ae115bc7Smrj 			rv = BAM_ERROR;
8642ae115bc7Smrj 			goto done;
8643ae115bc7Smrj 		}
8644eb2bd662Svikram 
8645eb2bd662Svikram 		osdev = get_special("/");
8646eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_SPECIAL", osdev = NULL);
8647eb2bd662Svikram 		if (osdev == NULL) {
8648eb2bd662Svikram 			free(fstype);
8649eb2bd662Svikram 			bam_error(BOOTENV_SPECIAL_FAILED);
8650eb2bd662Svikram 			rv = BAM_ERROR;
8651eb2bd662Svikram 			goto done;
8652eb2bd662Svikram 		}
8653eb2bd662Svikram 
8654eb2bd662Svikram 		sign = find_existing_sign("/", osdev, fstype);
8655eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_SIGN", sign = NULL);
8656eb2bd662Svikram 		if (sign == NULL) {
8657eb2bd662Svikram 			free(fstype);
8658eb2bd662Svikram 			free(osdev);
8659eb2bd662Svikram 			bam_error(BOOTENV_SIGN_FAILED);
8660eb2bd662Svikram 			rv = BAM_ERROR;
8661eb2bd662Svikram 			goto done;
8662eb2bd662Svikram 		}
8663eb2bd662Svikram 
8664eb2bd662Svikram 		free(fstype);
8665eb2bd662Svikram 		free(osdev);
8666eb2bd662Svikram 		(void) strlcpy(signbuf, sign, sizeof (signbuf));
8667eb2bd662Svikram 		free(sign);
8668eb2bd662Svikram 		assert(strchr(signbuf, '(') == NULL &&
8669eb2bd662Svikram 		    strchr(signbuf, ',') == NULL &&
8670eb2bd662Svikram 		    strchr(signbuf, ')') == NULL);
8671eb2bd662Svikram 
8672ae115bc7Smrj 		if (optnum == KERNEL_CMD) {
8673eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_NEW_KERN, fcn, new_path));
8674ae115bc7Smrj 			entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
8675eb2bd662Svikram 			    signbuf, new_path, NULL, NULL);
8676ae115bc7Smrj 		} else {
8677ae115bc7Smrj 			new_str_len = strlen(DIRECT_BOOT_KERNEL) +
8678ae115bc7Smrj 			    strlen(path) + 8;
8679ae115bc7Smrj 			new_arg = s_calloc(1, new_str_len);
8680ae115bc7Smrj 
8681ae115bc7Smrj 			(void) snprintf(new_arg, new_str_len, "%s %s",
8682ae115bc7Smrj 			    DIRECT_BOOT_KERNEL, path);
8683eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_NEW_ARG, fcn, new_arg));
8684ae115bc7Smrj 			entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
8685eb2bd662Svikram 			    signbuf, new_arg, NULL, DIRECT_BOOT_ARCHIVE);
8686eb2bd662Svikram 			free(new_arg);
8687eb2bd662Svikram 		}
8688eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_ADD_BOOT_ENTRY",
8689eb2bd662Svikram 		    entryNum = BAM_ERROR);
8690eb2bd662Svikram 		if (entryNum == BAM_ERROR) {
8691eb2bd662Svikram 			bam_error(GET_SET_KERNEL_ADD_BOOT_ENTRY,
8692eb2bd662Svikram 			    BOOTENV_RC_TITLE);
8693eb2bd662Svikram 			rv = BAM_ERROR;
8694eb2bd662Svikram 			goto done;
8695ae115bc7Smrj 		}
8696ae115bc7Smrj 		save_default_entry(mp, BAM_OLD_RC_DEF);
8697eb2bd662Svikram 		ret = set_global(mp, menu_cmds[DEFAULT_CMD], entryNum);
8698eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_SET_GLOBAL", ret = BAM_ERROR);
8699eb2bd662Svikram 		if (ret == BAM_ERROR) {
8700eb2bd662Svikram 			bam_error(GET_SET_KERNEL_SET_GLOBAL, entryNum);
8701eb2bd662Svikram 		}
8702ae115bc7Smrj 		rv = BAM_WRITE;
8703ae115bc7Smrj 		goto done;
8704ae115bc7Smrj 	}
8705ae115bc7Smrj 
8706ae115bc7Smrj 	/*
8707ae115bc7Smrj 	 * There was already an bootenv entry which we need to edit.
8708ae115bc7Smrj 	 */
8709ae115bc7Smrj 	if (optnum == KERNEL_CMD) {
8710ae115bc7Smrj 		new_str_len = strlen(new_path) + strlen(old_args) + 2;
8711ae115bc7Smrj 		new_arg = s_calloc(1, new_str_len);
8712ae115bc7Smrj 		(void) snprintf(new_arg, new_str_len, "%s %s", new_path,
8713ae115bc7Smrj 		    old_args);
8714ae115bc7Smrj 		free(kernelp->arg);
8715ae115bc7Smrj 		kernelp->arg = new_arg;
8716ae115bc7Smrj 
8717ae115bc7Smrj 		/*
8718ae115bc7Smrj 		 * If we have changed the kernel line, we may need to update
8719ae115bc7Smrj 		 * the archive line as well.
8720ae115bc7Smrj 		 */
8721ae115bc7Smrj 		set_archive_line(entryp, kernelp);
8722eb2bd662Svikram 		BAM_DPRINTF((D_GET_SET_KERNEL_REPLACED_KERNEL_SAME_ARG, fcn,
8723eb2bd662Svikram 		    kernelp->arg));
8724ae115bc7Smrj 	} else {
8725ae115bc7Smrj 		new_str_len = old_kernel_len + strlen(path) + 8;
8726ae115bc7Smrj 		new_arg = s_calloc(1, new_str_len);
8727ae115bc7Smrj 		(void) strncpy(new_arg, kernelp->arg, old_kernel_len);
8728ae115bc7Smrj 		(void) strlcat(new_arg, " ", new_str_len);
8729ae115bc7Smrj 		(void) strlcat(new_arg, path, new_str_len);
8730ae115bc7Smrj 		free(kernelp->arg);
8731ae115bc7Smrj 		kernelp->arg = new_arg;
8732eb2bd662Svikram 		BAM_DPRINTF((D_GET_SET_KERNEL_SAME_KERNEL_REPLACED_ARG, fcn,
8733eb2bd662Svikram 		    kernelp->arg));
8734ae115bc7Smrj 	}
8735ae115bc7Smrj 	rv = BAM_WRITE;
8736ae115bc7Smrj 
8737ae115bc7Smrj done:
8738ae115bc7Smrj 	if ((rv == BAM_WRITE) && kernelp)
8739ae115bc7Smrj 		update_line(kernelp);
8740ae115bc7Smrj 	if (free_new_path)
8741ae115bc7Smrj 		free(new_path);
8742eb2bd662Svikram 	if (rv == BAM_WRITE) {
8743eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
8744eb2bd662Svikram 	} else {
8745eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
8746eb2bd662Svikram 	}
8747ae115bc7Smrj 	return (rv);
8748ae115bc7Smrj }
8749ae115bc7Smrj 
8750eb2bd662Svikram static error_t
8751eb2bd662Svikram get_kernel(menu_t *mp, menu_cmd_t optnum, char *buf, size_t bufsize)
8752eb2bd662Svikram {
8753eb2bd662Svikram 	const char	*fcn = "get_kernel()";
8754eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, menu_cmds[optnum]));
8755eb2bd662Svikram 	return (get_set_kernel(mp, optnum, NULL, buf, bufsize));
8756eb2bd662Svikram }
8757eb2bd662Svikram 
8758eb2bd662Svikram static error_t
8759eb2bd662Svikram set_kernel(menu_t *mp, menu_cmd_t optnum, char *path, char *buf, size_t bufsize)
8760eb2bd662Svikram {
8761eb2bd662Svikram 	const char	*fcn = "set_kernel()";
8762eb2bd662Svikram 	assert(path != NULL);
8763eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, menu_cmds[optnum], path));
8764eb2bd662Svikram 	return (get_set_kernel(mp, optnum, path, buf, bufsize));
8765eb2bd662Svikram }
8766eb2bd662Svikram 
87677c478bd9Sstevel@tonic-gate /*ARGSUSED*/
87687c478bd9Sstevel@tonic-gate static error_t
8769eb2bd662Svikram set_option(menu_t *mp, char *dummy, char *opt)
87707c478bd9Sstevel@tonic-gate {
8771eb2bd662Svikram 	int		optnum;
8772eb2bd662Svikram 	int		optval;
87737c478bd9Sstevel@tonic-gate 	char		*val;
8774ae115bc7Smrj 	char		buf[BUFSIZ] = "";
8775ae115bc7Smrj 	error_t		rv;
8776eb2bd662Svikram 	const char	*fcn = "set_option()";
87777c478bd9Sstevel@tonic-gate 
87787c478bd9Sstevel@tonic-gate 	assert(mp);
87797c478bd9Sstevel@tonic-gate 	assert(opt);
8780eb2bd662Svikram 	assert(dummy == NULL);
8781eb2bd662Svikram 
8782eb2bd662Svikram 	/* opt is set from bam_argv[0] and is always non-NULL */
8783eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, opt));
87847c478bd9Sstevel@tonic-gate 
87857c478bd9Sstevel@tonic-gate 	val = strchr(opt, '=');
8786ae115bc7Smrj 	if (val != NULL) {
8787ae115bc7Smrj 		*val = '\0';
87887c478bd9Sstevel@tonic-gate 	}
87897c478bd9Sstevel@tonic-gate 
87907c478bd9Sstevel@tonic-gate 	if (strcmp(opt, "default") == 0) {
87917c478bd9Sstevel@tonic-gate 		optnum = DEFAULT_CMD;
87927c478bd9Sstevel@tonic-gate 	} else if (strcmp(opt, "timeout") == 0) {
87937c478bd9Sstevel@tonic-gate 		optnum = TIMEOUT_CMD;
8794ae115bc7Smrj 	} else if (strcmp(opt, menu_cmds[KERNEL_CMD]) == 0) {
8795ae115bc7Smrj 		optnum = KERNEL_CMD;
8796ae115bc7Smrj 	} else if (strcmp(opt, menu_cmds[ARGS_CMD]) == 0) {
8797ae115bc7Smrj 		optnum = ARGS_CMD;
87987c478bd9Sstevel@tonic-gate 	} else {
8799eb2bd662Svikram 		bam_error(INVALID_OPTION, opt);
88007c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
88017c478bd9Sstevel@tonic-gate 	}
88027c478bd9Sstevel@tonic-gate 
8803ae115bc7Smrj 	/*
8804ae115bc7Smrj 	 * kernel and args are allowed without "=new_value" strings.  All
8805ae115bc7Smrj 	 * others cause errors
8806ae115bc7Smrj 	 */
8807ae115bc7Smrj 	if ((val == NULL) && (optnum != KERNEL_CMD) && (optnum != ARGS_CMD)) {
8808eb2bd662Svikram 		bam_error(NO_OPTION_ARG, opt);
8809ae115bc7Smrj 		return (BAM_ERROR);
8810ae115bc7Smrj 	} else if (val != NULL) {
88117c478bd9Sstevel@tonic-gate 		*val = '=';
8812ae115bc7Smrj 	}
8813ae115bc7Smrj 
8814ae115bc7Smrj 	if ((optnum == KERNEL_CMD) || (optnum == ARGS_CMD)) {
8815eb2bd662Svikram 		BAM_DPRINTF((D_SET_OPTION, fcn, menu_cmds[optnum],
8816eb2bd662Svikram 		    val ? val + 1 : "NULL"));
8817eb2bd662Svikram 
8818eb2bd662Svikram 		if (val)
8819eb2bd662Svikram 			rv = set_kernel(mp, optnum, val + 1, buf, sizeof (buf));
8820eb2bd662Svikram 		else
8821eb2bd662Svikram 			rv = get_kernel(mp, optnum, buf, sizeof (buf));
8822ae115bc7Smrj 		if ((rv == BAM_SUCCESS) && (buf[0] != '\0'))
8823ae115bc7Smrj 			(void) printf("%s\n", buf);
8824ae115bc7Smrj 	} else {
8825ae115bc7Smrj 		optval = s_strtol(val + 1);
8826eb2bd662Svikram 		BAM_DPRINTF((D_SET_OPTION, fcn, menu_cmds[optnum], val + 1));
8827eb2bd662Svikram 		rv = set_global(mp, menu_cmds[optnum], optval);
88287c478bd9Sstevel@tonic-gate 	}
8829eb2bd662Svikram 
8830eb2bd662Svikram 	if (rv == BAM_WRITE || rv == BAM_SUCCESS) {
8831eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
8832eb2bd662Svikram 	} else {
8833eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
8834eb2bd662Svikram 	}
8835eb2bd662Svikram 
8836eb2bd662Svikram 	return (rv);
8837ae115bc7Smrj }
88387c478bd9Sstevel@tonic-gate 
88397c478bd9Sstevel@tonic-gate /*
88407c478bd9Sstevel@tonic-gate  * The quiet argument suppresses messages. This is used
88417c478bd9Sstevel@tonic-gate  * when invoked in the context of other commands (e.g. list_entry)
88427c478bd9Sstevel@tonic-gate  */
88437c478bd9Sstevel@tonic-gate static error_t
88447c478bd9Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet)
88457c478bd9Sstevel@tonic-gate {
88467c478bd9Sstevel@tonic-gate 	line_t *lp;
88477c478bd9Sstevel@tonic-gate 	char *arg;
88487c478bd9Sstevel@tonic-gate 	int done, ret = BAM_SUCCESS;
88497c478bd9Sstevel@tonic-gate 
88507c478bd9Sstevel@tonic-gate 	assert(mp);
88517c478bd9Sstevel@tonic-gate 	assert(menu_path);
88527c478bd9Sstevel@tonic-gate 	assert(globalcmd);
88537c478bd9Sstevel@tonic-gate 
88547c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
88557c478bd9Sstevel@tonic-gate 		if (!quiet)
88567c478bd9Sstevel@tonic-gate 			bam_error(NO_MENU, menu_path);
88577c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
88587c478bd9Sstevel@tonic-gate 	}
88597c478bd9Sstevel@tonic-gate 
88607c478bd9Sstevel@tonic-gate 	done = 0;
88617c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
88627c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
88637c478bd9Sstevel@tonic-gate 			continue;
88647c478bd9Sstevel@tonic-gate 
88657c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
88667c478bd9Sstevel@tonic-gate 			if (!quiet)
88677c478bd9Sstevel@tonic-gate 				bam_error(NO_CMD, lp->lineNum);
88687c478bd9Sstevel@tonic-gate 			continue;
88697c478bd9Sstevel@tonic-gate 		}
88707c478bd9Sstevel@tonic-gate 
88717c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
88727c478bd9Sstevel@tonic-gate 			continue;
88737c478bd9Sstevel@tonic-gate 
88747c478bd9Sstevel@tonic-gate 		/* Found global. Check for duplicates */
88757c478bd9Sstevel@tonic-gate 		if (done && !quiet) {
88767c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
88777c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
88787c478bd9Sstevel@tonic-gate 		}
88797c478bd9Sstevel@tonic-gate 
88807c478bd9Sstevel@tonic-gate 		arg = lp->arg ? lp->arg : "";
88817c478bd9Sstevel@tonic-gate 		bam_print(GLOBAL_CMD, globalcmd, arg);
88827c478bd9Sstevel@tonic-gate 		done = 1;
88837c478bd9Sstevel@tonic-gate 	}
88847c478bd9Sstevel@tonic-gate 
88857c478bd9Sstevel@tonic-gate 	if (!done && bam_verbose)
88867c478bd9Sstevel@tonic-gate 		bam_print(NO_ENTRY, globalcmd);
88877c478bd9Sstevel@tonic-gate 
88887c478bd9Sstevel@tonic-gate 	return (ret);
88897c478bd9Sstevel@tonic-gate }
88907c478bd9Sstevel@tonic-gate 
88917c478bd9Sstevel@tonic-gate static error_t
88927c478bd9Sstevel@tonic-gate menu_write(char *root, menu_t *mp)
88937c478bd9Sstevel@tonic-gate {
8894eb2bd662Svikram 	const char *fcn = "menu_write()";
8895eb2bd662Svikram 
8896eb2bd662Svikram 	BAM_DPRINTF((D_MENU_WRITE_ENTER, fcn, root));
88977c478bd9Sstevel@tonic-gate 	return (list2file(root, MENU_TMP, GRUB_MENU, mp->start));
88987c478bd9Sstevel@tonic-gate }
88997c478bd9Sstevel@tonic-gate 
8900eb2bd662Svikram void
89017c478bd9Sstevel@tonic-gate line_free(line_t *lp)
89027c478bd9Sstevel@tonic-gate {
89037c478bd9Sstevel@tonic-gate 	if (lp == NULL)
89047c478bd9Sstevel@tonic-gate 		return;
89057c478bd9Sstevel@tonic-gate 
89067c478bd9Sstevel@tonic-gate 	if (lp->cmd)
89077c478bd9Sstevel@tonic-gate 		free(lp->cmd);
89087c478bd9Sstevel@tonic-gate 	if (lp->sep)
89097c478bd9Sstevel@tonic-gate 		free(lp->sep);
89107c478bd9Sstevel@tonic-gate 	if (lp->arg)
89117c478bd9Sstevel@tonic-gate 		free(lp->arg);
89127c478bd9Sstevel@tonic-gate 	if (lp->line)
89137c478bd9Sstevel@tonic-gate 		free(lp->line);
89147c478bd9Sstevel@tonic-gate 	free(lp);
89157c478bd9Sstevel@tonic-gate }
89167c478bd9Sstevel@tonic-gate 
89177c478bd9Sstevel@tonic-gate static void
89187c478bd9Sstevel@tonic-gate linelist_free(line_t *start)
89197c478bd9Sstevel@tonic-gate {
89207c478bd9Sstevel@tonic-gate 	line_t *lp;
89217c478bd9Sstevel@tonic-gate 
89227c478bd9Sstevel@tonic-gate 	while (start) {
89237c478bd9Sstevel@tonic-gate 		lp = start;
89247c478bd9Sstevel@tonic-gate 		start = start->next;
89257c478bd9Sstevel@tonic-gate 		line_free(lp);
89267c478bd9Sstevel@tonic-gate 	}
89277c478bd9Sstevel@tonic-gate }
89287c478bd9Sstevel@tonic-gate 
89297c478bd9Sstevel@tonic-gate static void
89307c478bd9Sstevel@tonic-gate filelist_free(filelist_t *flistp)
89317c478bd9Sstevel@tonic-gate {
89327c478bd9Sstevel@tonic-gate 	linelist_free(flistp->head);
89337c478bd9Sstevel@tonic-gate 	flistp->head = NULL;
89347c478bd9Sstevel@tonic-gate 	flistp->tail = NULL;
89357c478bd9Sstevel@tonic-gate }
89367c478bd9Sstevel@tonic-gate 
89377c478bd9Sstevel@tonic-gate static void
89387c478bd9Sstevel@tonic-gate menu_free(menu_t *mp)
89397c478bd9Sstevel@tonic-gate {
89408c1b6884Sszhou 	entry_t *ent, *tmp;
89417c478bd9Sstevel@tonic-gate 	assert(mp);
89427c478bd9Sstevel@tonic-gate 
89437c478bd9Sstevel@tonic-gate 	if (mp->start)
89447c478bd9Sstevel@tonic-gate 		linelist_free(mp->start);
89458c1b6884Sszhou 	ent = mp->entries;
89468c1b6884Sszhou 	while (ent) {
89478c1b6884Sszhou 		tmp = ent;
89488c1b6884Sszhou 		ent = tmp->next;
89498c1b6884Sszhou 		free(tmp);
89508c1b6884Sszhou 	}
89517c478bd9Sstevel@tonic-gate 
89528c1b6884Sszhou 	free(mp);
89537c478bd9Sstevel@tonic-gate }
89547c478bd9Sstevel@tonic-gate 
89557c478bd9Sstevel@tonic-gate /*
89567c478bd9Sstevel@tonic-gate  * Utility routines
89577c478bd9Sstevel@tonic-gate  */
89587c478bd9Sstevel@tonic-gate 
89597c478bd9Sstevel@tonic-gate 
89607c478bd9Sstevel@tonic-gate /*
89617c478bd9Sstevel@tonic-gate  * Returns 0 on success
89627c478bd9Sstevel@tonic-gate  * Any other value indicates an error
89637c478bd9Sstevel@tonic-gate  */
89647c478bd9Sstevel@tonic-gate static int
8965986fd29aSsetje exec_cmd(char *cmdline, filelist_t *flistp)
89667c478bd9Sstevel@tonic-gate {
89677c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
89687c478bd9Sstevel@tonic-gate 	int ret;
89697c478bd9Sstevel@tonic-gate 	FILE *ptr;
89707c478bd9Sstevel@tonic-gate 	sigset_t set;
89717c478bd9Sstevel@tonic-gate 	void (*disp)(int);
89727c478bd9Sstevel@tonic-gate 
89737c478bd9Sstevel@tonic-gate 	/*
89747c478bd9Sstevel@tonic-gate 	 * For security
89757c478bd9Sstevel@tonic-gate 	 * - only absolute paths are allowed
89767c478bd9Sstevel@tonic-gate 	 * - set IFS to space and tab
89777c478bd9Sstevel@tonic-gate 	 */
89787c478bd9Sstevel@tonic-gate 	if (*cmdline != '/') {
89797c478bd9Sstevel@tonic-gate 		bam_error(ABS_PATH_REQ, cmdline);
89807c478bd9Sstevel@tonic-gate 		return (-1);
89817c478bd9Sstevel@tonic-gate 	}
89827c478bd9Sstevel@tonic-gate 	(void) putenv("IFS= \t");
89837c478bd9Sstevel@tonic-gate 
89847c478bd9Sstevel@tonic-gate 	/*
89857c478bd9Sstevel@tonic-gate 	 * We may have been exec'ed with SIGCHLD blocked
89867c478bd9Sstevel@tonic-gate 	 * unblock it here
89877c478bd9Sstevel@tonic-gate 	 */
89887c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
89897c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
89907c478bd9Sstevel@tonic-gate 	if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
89917c478bd9Sstevel@tonic-gate 		bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno));
89927c478bd9Sstevel@tonic-gate 		return (-1);
89937c478bd9Sstevel@tonic-gate 	}
89947c478bd9Sstevel@tonic-gate 
89957c478bd9Sstevel@tonic-gate 	/*
89967c478bd9Sstevel@tonic-gate 	 * Set SIGCHLD disposition to SIG_DFL for popen/pclose
89977c478bd9Sstevel@tonic-gate 	 */
89987c478bd9Sstevel@tonic-gate 	disp = sigset(SIGCHLD, SIG_DFL);
89997c478bd9Sstevel@tonic-gate 	if (disp == SIG_ERR) {
90007c478bd9Sstevel@tonic-gate 		bam_error(FAILED_SIG, strerror(errno));
90017c478bd9Sstevel@tonic-gate 		return (-1);
90027c478bd9Sstevel@tonic-gate 	}
90037c478bd9Sstevel@tonic-gate 	if (disp == SIG_HOLD) {
90047c478bd9Sstevel@tonic-gate 		bam_error(BLOCKED_SIG, cmdline);
90057c478bd9Sstevel@tonic-gate 		return (-1);
90067c478bd9Sstevel@tonic-gate 	}
90077c478bd9Sstevel@tonic-gate 
90087c478bd9Sstevel@tonic-gate 	ptr = popen(cmdline, "r");
90097c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
90107c478bd9Sstevel@tonic-gate 		bam_error(POPEN_FAIL, cmdline, strerror(errno));
90117c478bd9Sstevel@tonic-gate 		return (-1);
90127c478bd9Sstevel@tonic-gate 	}
90137c478bd9Sstevel@tonic-gate 
90147c478bd9Sstevel@tonic-gate 	/*
90157c478bd9Sstevel@tonic-gate 	 * If we simply do a pclose() following a popen(), pclose()
90167c478bd9Sstevel@tonic-gate 	 * will close the reader end of the pipe immediately even
90177c478bd9Sstevel@tonic-gate 	 * if the child process has not started/exited. pclose()
90187c478bd9Sstevel@tonic-gate 	 * does wait for cmd to terminate before returning though.
90197c478bd9Sstevel@tonic-gate 	 * When the executed command writes its output to the pipe
90207c478bd9Sstevel@tonic-gate 	 * there is no reader process and the command dies with
90217c478bd9Sstevel@tonic-gate 	 * SIGPIPE. To avoid this we read repeatedly until read
90227c478bd9Sstevel@tonic-gate 	 * terminates with EOF. This indicates that the command
90237c478bd9Sstevel@tonic-gate 	 * (writer) has closed the pipe and we can safely do a
90247c478bd9Sstevel@tonic-gate 	 * pclose().
90257c478bd9Sstevel@tonic-gate 	 *
90267c478bd9Sstevel@tonic-gate 	 * Since pclose() does wait for the command to exit,
90277c478bd9Sstevel@tonic-gate 	 * we can safely reap the exit status of the command
90287c478bd9Sstevel@tonic-gate 	 * from the value returned by pclose()
90297c478bd9Sstevel@tonic-gate 	 */
9030986fd29aSsetje 	while (s_fgets(buf, sizeof (buf), ptr) != NULL) {
9031986fd29aSsetje 		if (flistp == NULL) {
9032986fd29aSsetje 			/* s_fgets strips newlines, so insert them at the end */
9033986fd29aSsetje 			bam_print(PRINT, buf);
9034986fd29aSsetje 		} else {
9035986fd29aSsetje 			append_to_flist(flistp, buf);
90367c478bd9Sstevel@tonic-gate 		}
90377c478bd9Sstevel@tonic-gate 	}
90387c478bd9Sstevel@tonic-gate 
90397c478bd9Sstevel@tonic-gate 	ret = pclose(ptr);
90407c478bd9Sstevel@tonic-gate 	if (ret == -1) {
90417c478bd9Sstevel@tonic-gate 		bam_error(PCLOSE_FAIL, cmdline, strerror(errno));
90427c478bd9Sstevel@tonic-gate 		return (-1);
90437c478bd9Sstevel@tonic-gate 	}
90447c478bd9Sstevel@tonic-gate 
90457c478bd9Sstevel@tonic-gate 	if (WIFEXITED(ret)) {
90467c478bd9Sstevel@tonic-gate 		return (WEXITSTATUS(ret));
90477c478bd9Sstevel@tonic-gate 	} else {
90487c478bd9Sstevel@tonic-gate 		bam_error(EXEC_FAIL, cmdline, ret);
90497c478bd9Sstevel@tonic-gate 		return (-1);
90507c478bd9Sstevel@tonic-gate 	}
90517c478bd9Sstevel@tonic-gate }
90527c478bd9Sstevel@tonic-gate 
90537c478bd9Sstevel@tonic-gate /*
90547c478bd9Sstevel@tonic-gate  * Since this function returns -1 on error
90557c478bd9Sstevel@tonic-gate  * it cannot be used to convert -1. However,
90567c478bd9Sstevel@tonic-gate  * that is sufficient for what we need.
90577c478bd9Sstevel@tonic-gate  */
90587c478bd9Sstevel@tonic-gate static long
90597c478bd9Sstevel@tonic-gate s_strtol(char *str)
90607c478bd9Sstevel@tonic-gate {
90617c478bd9Sstevel@tonic-gate 	long l;
90627c478bd9Sstevel@tonic-gate 	char *res = NULL;
90637c478bd9Sstevel@tonic-gate 
90647c478bd9Sstevel@tonic-gate 	if (str == NULL) {
90657c478bd9Sstevel@tonic-gate 		return (-1);
90667c478bd9Sstevel@tonic-gate 	}
90677c478bd9Sstevel@tonic-gate 
90687c478bd9Sstevel@tonic-gate 	errno = 0;
90697c478bd9Sstevel@tonic-gate 	l = strtol(str, &res, 10);
90707c478bd9Sstevel@tonic-gate 	if (errno || *res != '\0') {
90717c478bd9Sstevel@tonic-gate 		return (-1);
90727c478bd9Sstevel@tonic-gate 	}
90737c478bd9Sstevel@tonic-gate 
90747c478bd9Sstevel@tonic-gate 	return (l);
90757c478bd9Sstevel@tonic-gate }
90767c478bd9Sstevel@tonic-gate 
90777c478bd9Sstevel@tonic-gate /*
90787c478bd9Sstevel@tonic-gate  * Wrapper around fputs, that adds a newline (since fputs doesn't)
90797c478bd9Sstevel@tonic-gate  */
90807c478bd9Sstevel@tonic-gate static int
90817c478bd9Sstevel@tonic-gate s_fputs(char *str, FILE *fp)
90827c478bd9Sstevel@tonic-gate {
90837c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
90847c478bd9Sstevel@tonic-gate 
90857c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s\n", str);
90867c478bd9Sstevel@tonic-gate 	return (fputs(linebuf, fp));
90877c478bd9Sstevel@tonic-gate }
90887c478bd9Sstevel@tonic-gate 
90897c478bd9Sstevel@tonic-gate /*
90907c478bd9Sstevel@tonic-gate  * Wrapper around fgets, that strips newlines returned by fgets
90917c478bd9Sstevel@tonic-gate  */
9092ae115bc7Smrj char *
90937c478bd9Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp)
90947c478bd9Sstevel@tonic-gate {
90957c478bd9Sstevel@tonic-gate 	int n;
90967c478bd9Sstevel@tonic-gate 
90977c478bd9Sstevel@tonic-gate 	buf = fgets(buf, buflen, fp);
90987c478bd9Sstevel@tonic-gate 	if (buf) {
90997c478bd9Sstevel@tonic-gate 		n = strlen(buf);
91007c478bd9Sstevel@tonic-gate 		if (n == buflen - 1 && buf[n-1] != '\n')
91017c478bd9Sstevel@tonic-gate 			bam_error(TOO_LONG, buflen - 1, buf);
91027c478bd9Sstevel@tonic-gate 		buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1];
91037c478bd9Sstevel@tonic-gate 	}
91047c478bd9Sstevel@tonic-gate 
91057c478bd9Sstevel@tonic-gate 	return (buf);
91067c478bd9Sstevel@tonic-gate }
91077c478bd9Sstevel@tonic-gate 
9108ae115bc7Smrj void *
91097c478bd9Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz)
91107c478bd9Sstevel@tonic-gate {
91117c478bd9Sstevel@tonic-gate 	void *ptr;
91127c478bd9Sstevel@tonic-gate 
91137c478bd9Sstevel@tonic-gate 	ptr = calloc(nelem, sz);
91147c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
91157c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, nelem*sz);
91167c478bd9Sstevel@tonic-gate 		bam_exit(1);
91177c478bd9Sstevel@tonic-gate 	}
91187c478bd9Sstevel@tonic-gate 	return (ptr);
91197c478bd9Sstevel@tonic-gate }
91207c478bd9Sstevel@tonic-gate 
9121ae115bc7Smrj void *
9122ae115bc7Smrj s_realloc(void *ptr, size_t sz)
9123ae115bc7Smrj {
9124ae115bc7Smrj 	ptr = realloc(ptr, sz);
9125ae115bc7Smrj 	if (ptr == NULL) {
9126ae115bc7Smrj 		bam_error(NO_MEM, sz);
9127ae115bc7Smrj 		bam_exit(1);
9128ae115bc7Smrj 	}
9129ae115bc7Smrj 	return (ptr);
9130ae115bc7Smrj }
9131ae115bc7Smrj 
9132eb2bd662Svikram char *
91337c478bd9Sstevel@tonic-gate s_strdup(char *str)
91347c478bd9Sstevel@tonic-gate {
91357c478bd9Sstevel@tonic-gate 	char *ptr;
91367c478bd9Sstevel@tonic-gate 
91377c478bd9Sstevel@tonic-gate 	if (str == NULL)
91387c478bd9Sstevel@tonic-gate 		return (NULL);
91397c478bd9Sstevel@tonic-gate 
91407c478bd9Sstevel@tonic-gate 	ptr = strdup(str);
91417c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
91427c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, strlen(str) + 1);
91437c478bd9Sstevel@tonic-gate 		bam_exit(1);
91447c478bd9Sstevel@tonic-gate 	}
91457c478bd9Sstevel@tonic-gate 	return (ptr);
91467c478bd9Sstevel@tonic-gate }
91477c478bd9Sstevel@tonic-gate 
91487c478bd9Sstevel@tonic-gate /*
91497c478bd9Sstevel@tonic-gate  * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients)
91507c478bd9Sstevel@tonic-gate  * Returns 0 otherwise
91517c478bd9Sstevel@tonic-gate  */
91527c478bd9Sstevel@tonic-gate static int
91537c478bd9Sstevel@tonic-gate is_amd64(void)
91547c478bd9Sstevel@tonic-gate {
91557c478bd9Sstevel@tonic-gate 	static int amd64 = -1;
91567c478bd9Sstevel@tonic-gate 	char isabuf[257];	/* from sysinfo(2) manpage */
91577c478bd9Sstevel@tonic-gate 
91587c478bd9Sstevel@tonic-gate 	if (amd64 != -1)
91597c478bd9Sstevel@tonic-gate 		return (amd64);
91607c478bd9Sstevel@tonic-gate 
9161d876c67dSjg 	if (bam_alt_platform) {
9162d876c67dSjg 		if (strcmp(bam_platform, "i86pc") == 0) {
91637c478bd9Sstevel@tonic-gate 			amd64 = 1;		/* diskless server */
9164d876c67dSjg 		}
9165d876c67dSjg 	} else {
9166d876c67dSjg 		if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 &&
9167d876c67dSjg 		    strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0) {
9168d876c67dSjg 			amd64 = 1;
9169d876c67dSjg 		} else if (strstr(isabuf, "i386") == NULL) {
9170d876c67dSjg 			amd64 = 1;		/* diskless server */
9171d876c67dSjg 		}
9172d876c67dSjg 	}
9173d876c67dSjg 	if (amd64 == -1)
91747c478bd9Sstevel@tonic-gate 		amd64 = 0;
91757c478bd9Sstevel@tonic-gate 
91767c478bd9Sstevel@tonic-gate 	return (amd64);
91777c478bd9Sstevel@tonic-gate }
91787c478bd9Sstevel@tonic-gate 
917979755401Ssetje static char *
918079755401Ssetje get_machine(void)
9181986fd29aSsetje {
918279755401Ssetje 	static int cached = -1;
918379755401Ssetje 	static char mbuf[257];	/* from sysinfo(2) manpage */
9184986fd29aSsetje 
918579755401Ssetje 	if (cached == 0)
918679755401Ssetje 		return (mbuf);
9187d876c67dSjg 
9188d876c67dSjg 	if (bam_alt_platform) {
918979755401Ssetje 		return (bam_platform);
9190d876c67dSjg 	} else {
919179755401Ssetje 		if (sysinfo(SI_MACHINE, mbuf, sizeof (mbuf)) > 0) {
919279755401Ssetje 			cached = 1;
9193d876c67dSjg 		}
9194d876c67dSjg 	}
919579755401Ssetje 	if (cached == -1) {
919679755401Ssetje 		mbuf[0] = '\0';
919779755401Ssetje 		cached = 0;
9198986fd29aSsetje 	}
9199986fd29aSsetje 
920079755401Ssetje 	return (mbuf);
9201986fd29aSsetje }
9202986fd29aSsetje 
9203eb2bd662Svikram int
9204eb2bd662Svikram is_sparc(void)
9205eb2bd662Svikram {
920679755401Ssetje 	static int issparc = -1;
920779755401Ssetje 	char mbuf[257];	/* from sysinfo(2) manpage */
920879755401Ssetje 
920979755401Ssetje 	if (issparc != -1)
921079755401Ssetje 		return (issparc);
921179755401Ssetje 
921279755401Ssetje 	if (bam_alt_platform) {
921379755401Ssetje 		if (strncmp(bam_platform, "sun4", 4) == 0) {
921479755401Ssetje 			issparc = 1;
921579755401Ssetje 		}
921679755401Ssetje 	} else {
921779755401Ssetje 		if (sysinfo(SI_ARCHITECTURE, mbuf, sizeof (mbuf)) > 0 &&
9218d7d0f93bSjg 		    strcmp(mbuf, "sparc") == 0) {
921979755401Ssetje 			issparc = 1;
922079755401Ssetje 		}
9221d7d0f93bSjg 	}
9222d7d0f93bSjg 	if (issparc == -1)
9223d7d0f93bSjg 		issparc = 0;
922479755401Ssetje 
922579755401Ssetje 	return (issparc);
9226eb2bd662Svikram }
9227986fd29aSsetje 
92287c478bd9Sstevel@tonic-gate static void
92297c478bd9Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s)
92307c478bd9Sstevel@tonic-gate {
92317c478bd9Sstevel@tonic-gate 	line_t *lp;
92327c478bd9Sstevel@tonic-gate 
92337c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
92347c478bd9Sstevel@tonic-gate 	lp->line = s_strdup(s);
92357c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL)
92367c478bd9Sstevel@tonic-gate 		flistp->head = lp;
92377c478bd9Sstevel@tonic-gate 	else
92387c478bd9Sstevel@tonic-gate 		flistp->tail->next = lp;
92397c478bd9Sstevel@tonic-gate 	flistp->tail = lp;
92407c478bd9Sstevel@tonic-gate }
92412449e17fSsherrym 
9242986fd29aSsetje #if !defined(_OPB)
92432449e17fSsherrym 
92442449e17fSsherrym UCODE_VENDORS;
92452449e17fSsherrym 
92462449e17fSsherrym /*ARGSUSED*/
92472449e17fSsherrym static void
92482449e17fSsherrym ucode_install(char *root)
92492449e17fSsherrym {
92502449e17fSsherrym 	int i;
92512449e17fSsherrym 
92522449e17fSsherrym 	for (i = 0; ucode_vendors[i].filestr != NULL; i++) {
92532449e17fSsherrym 		int cmd_len = PATH_MAX + 256;
92542449e17fSsherrym 		char cmd[PATH_MAX + 256];
92552449e17fSsherrym 		char file[PATH_MAX];
92562449e17fSsherrym 		char timestamp[PATH_MAX];
92572449e17fSsherrym 		struct stat fstatus, tstatus;
92582449e17fSsherrym 		struct utimbuf u_times;
92592449e17fSsherrym 
9260adc586deSMark Johnson 		(void) snprintf(file, PATH_MAX, "%s/%s/%s-ucode.%s",
9261adc586deSMark Johnson 		    bam_root, UCODE_INSTALL_PATH, ucode_vendors[i].filestr,
9262adc586deSMark Johnson 		    ucode_vendors[i].extstr);
92632449e17fSsherrym 
92642449e17fSsherrym 		if (stat(file, &fstatus) != 0 || !(S_ISREG(fstatus.st_mode)))
92652449e17fSsherrym 			continue;
92662449e17fSsherrym 
92672449e17fSsherrym 		(void) snprintf(timestamp, PATH_MAX, "%s.ts", file);
92682449e17fSsherrym 
92692449e17fSsherrym 		if (stat(timestamp, &tstatus) == 0 &&
92702449e17fSsherrym 		    fstatus.st_mtime <= tstatus.st_mtime)
92712449e17fSsherrym 			continue;
92722449e17fSsherrym 
92732449e17fSsherrym 		(void) snprintf(cmd, cmd_len, "/usr/sbin/ucodeadm -i -R "
92742449e17fSsherrym 		    "%s/%s/%s %s > /dev/null 2>&1", bam_root,
92752449e17fSsherrym 		    UCODE_INSTALL_PATH, ucode_vendors[i].vendorstr, file);
92762449e17fSsherrym 		if (system(cmd) != 0)
92772449e17fSsherrym 			return;
92782449e17fSsherrym 
92792449e17fSsherrym 		if (creat(timestamp, S_IRUSR | S_IWUSR) == -1)
92802449e17fSsherrym 			return;
92812449e17fSsherrym 
92822449e17fSsherrym 		u_times.actime = fstatus.st_atime;
92832449e17fSsherrym 		u_times.modtime = fstatus.st_mtime;
92842449e17fSsherrym 		(void) utime(timestamp, &u_times);
92852449e17fSsherrym 	}
92862449e17fSsherrym }
92872449e17fSsherrym #endif
9288