xref: /titanic_53/usr/src/cmd/boot/bootadm/bootadm.c (revision f64ca10231919db05e806441ccd6186ea9c6e734)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5fbac2b2bSvikram  * Common Development and Distribution License (the "License").
6fbac2b2bSvikram  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
23e373b6e4SYuri Pankov  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2440a5c998SMatthew Ahrens  * Copyright 2012 Milan Jurik. All rights reserved.
2540a5c998SMatthew Ahrens  * Copyright (c) 2015 by Delphix. All rights reserved.
26eeb2c267SToomas Soome  * Copyright 2016 Toomas Soome <tsoome@me.com>
27e373b6e4SYuri Pankov  * Copyright 2016 Nexenta Systems, Inc.
286debd3f5SAlexander Eremin  */
296debd3f5SAlexander Eremin 
306debd3f5SAlexander Eremin /*
317c478bd9Sstevel@tonic-gate  * bootadm(1M) is a new utility for managing bootability of
327c478bd9Sstevel@tonic-gate  * Solaris *Newboot* environments. It has two primary tasks:
337c478bd9Sstevel@tonic-gate  * 	- Allow end users to manage bootability of Newboot Solaris instances
347c478bd9Sstevel@tonic-gate  *	- Provide services to other subsystems in Solaris (primarily Install)
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /* Headers */
387c478bd9Sstevel@tonic-gate #include <stdio.h>
397c478bd9Sstevel@tonic-gate #include <errno.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include <sys/stat.h>
4544da779fSWilliam Kucharski #include <alloca.h>
467c478bd9Sstevel@tonic-gate #include <stdarg.h>
477c478bd9Sstevel@tonic-gate #include <limits.h>
487c478bd9Sstevel@tonic-gate #include <signal.h>
497c478bd9Sstevel@tonic-gate #include <sys/wait.h>
507c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
51f904d32dSJerry Gilliam #include <sys/mntent.h>
527c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
537c478bd9Sstevel@tonic-gate #include <libnvpair.h>
547c478bd9Sstevel@tonic-gate #include <ftw.h>
557c478bd9Sstevel@tonic-gate #include <fcntl.h>
567c478bd9Sstevel@tonic-gate #include <strings.h>
572449e17fSsherrym #include <utime.h>
587c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
597c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
6058091fd8Ssetje #include <sys/param.h>
61eb2bd662Svikram #include <dirent.h>
62eb2bd662Svikram #include <ctype.h>
63eb2bd662Svikram #include <libgen.h>
64e7cbe64fSgw25295 #include <sys/sysmacros.h>
6548847494SEnrico Perla - Sun Microsystems #include <sys/elf.h>
66963390b4Svikram #include <libscf.h>
6748847494SEnrico Perla - Sun Microsystems #include <zlib.h>
6848847494SEnrico Perla - Sun Microsystems #include <sys/lockfs.h>
6948847494SEnrico Perla - Sun Microsystems #include <sys/filio.h>
706debd3f5SAlexander Eremin #include <libbe.h>
71e998e519SSheshadri Vasudevan #ifdef i386
72e998e519SSheshadri Vasudevan #include <libfdisk.h>
73e998e519SSheshadri Vasudevan #endif
74986fd29aSsetje 
7579c28b70SToomas Soome #if !defined(_OBP)
762449e17fSsherrym #include <sys/ucode.h>
772449e17fSsherrym #endif
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #include <pwd.h>
807c478bd9Sstevel@tonic-gate #include <grp.h>
817c478bd9Sstevel@tonic-gate #include <device_info.h>
82eb2bd662Svikram #include <sys/vtoc.h>
83eb2bd662Svikram #include <sys/efi_partition.h>
8489c3ee43SGangadhar Mylapuram #include <regex.h>
857c478bd9Sstevel@tonic-gate #include <locale.h>
86c7c0ceafSToomas Soome #include <sys/mkdev.h>
877c478bd9Sstevel@tonic-gate 
88ae115bc7Smrj #include "bootadm.h"
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
917c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
927c478bd9Sstevel@tonic-gate #endif	/* TEXT_DOMAIN */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /* Type definitions */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /* Primary subcmds */
977c478bd9Sstevel@tonic-gate typedef enum {
987c478bd9Sstevel@tonic-gate 	BAM_MENU = 3,
99c7c0ceafSToomas Soome 	BAM_ARCHIVE,
100c7c0ceafSToomas Soome 	BAM_INSTALL
1017c478bd9Sstevel@tonic-gate } subcmd_t;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate #define	LINE_INIT	0	/* lineNum initial value */
1047c478bd9Sstevel@tonic-gate #define	ENTRY_INIT	-1	/* entryNum initial value */
1057c478bd9Sstevel@tonic-gate #define	ALL_ENTRIES	-2	/* selects all boot entries */
1067c478bd9Sstevel@tonic-gate 
1071a902ef8SHans Rosenfeld #define	PARTNO_NOTFOUND -1	/* Solaris partition not found */
1081a902ef8SHans Rosenfeld #define	PARTNO_EFI	-2	/* EFI partition table found */
1091a902ef8SHans Rosenfeld 
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"
11508db0dbcSAlexander Eremin #define	RAMDISK_SPECIAL		"/dev/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 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * Default file attributes
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_MODE	0644	/* default permissions */
1567c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_UID		0	/* user root */
1577c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_GID		3	/* group sys */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate  * Menu related
1617c478bd9Sstevel@tonic-gate  * menu_cmd_t and menu_cmds must be kept in sync
1627c478bd9Sstevel@tonic-gate  */
163ae115bc7Smrj char *menu_cmds[] = {
1647c478bd9Sstevel@tonic-gate 	"default",	/* DEFAULT_CMD */
1657c478bd9Sstevel@tonic-gate 	"timeout",	/* TIMEOUT_CMD */
1667c478bd9Sstevel@tonic-gate 	"title",	/* TITLE_CMD */
1677c478bd9Sstevel@tonic-gate 	"root",		/* ROOT_CMD */
1687c478bd9Sstevel@tonic-gate 	"kernel",	/* KERNEL_CMD */
169ae115bc7Smrj 	"kernel$",	/* KERNEL_DOLLAR_CMD */
1707c478bd9Sstevel@tonic-gate 	"module",	/* MODULE_CMD */
171ae115bc7Smrj 	"module$",	/* MODULE_DOLLAR_CMD */
1727c478bd9Sstevel@tonic-gate 	" ",		/* SEP_CMD */
1737c478bd9Sstevel@tonic-gate 	"#",		/* COMMENT_CMD */
174ae115bc7Smrj 	"chainloader",	/* CHAINLOADER_CMD */
175ae115bc7Smrj 	"args",		/* ARGS_CMD */
176eb2bd662Svikram 	"findroot",	/* FINDROOT_CMD */
17744da779fSWilliam Kucharski 	"bootfs",	/* BOOTFS_CMD */
1787c478bd9Sstevel@tonic-gate 	NULL
1797c478bd9Sstevel@tonic-gate };
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate #define	OPT_ENTRY_NUM	"entry"
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*
184eb2bd662Svikram  * exec_cmd related
1857c478bd9Sstevel@tonic-gate  */
1867c478bd9Sstevel@tonic-gate typedef struct {
1877c478bd9Sstevel@tonic-gate 	line_t *head;
1887c478bd9Sstevel@tonic-gate 	line_t *tail;
1897c478bd9Sstevel@tonic-gate } filelist_t;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate #define	BOOT_FILE_LIST	"boot/solaris/filelist.ramdisk"
1927c478bd9Sstevel@tonic-gate #define	ETC_FILE_LIST	"etc/boot/solaris/filelist.ramdisk"
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate #define	FILE_STAT	"boot/solaris/filestat.ramdisk"
1957c478bd9Sstevel@tonic-gate #define	FILE_STAT_TMP	"boot/solaris/filestat.ramdisk.tmp"
1967c478bd9Sstevel@tonic-gate #define	DIR_PERMS	(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
1977c478bd9Sstevel@tonic-gate #define	FILE_STAT_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
1987c478bd9Sstevel@tonic-gate 
1994a9df875SEnrico Perla - Sun Microsystems #define	FILE_STAT_TIMESTAMP	"boot/solaris/timestamp.cache"
2004a9df875SEnrico Perla - Sun Microsystems 
2017c478bd9Sstevel@tonic-gate /* Globals */
202ae115bc7Smrj int bam_verbose;
203ae115bc7Smrj int bam_force;
204eb2bd662Svikram int bam_debug;
2057c478bd9Sstevel@tonic-gate static char *prog;
2067c478bd9Sstevel@tonic-gate static subcmd_t bam_cmd;
207*f64ca102SToomas Soome char *bam_root;
208*f64ca102SToomas Soome int bam_rootlen;
2097c478bd9Sstevel@tonic-gate static int bam_root_readonly;
210*f64ca102SToomas Soome int bam_alt_root;
21148847494SEnrico Perla - Sun Microsystems static int bam_extend = 0;
21248847494SEnrico Perla - Sun Microsystems static int bam_purge = 0;
2137c478bd9Sstevel@tonic-gate static char *bam_subcmd;
2147c478bd9Sstevel@tonic-gate static char *bam_opt;
2157c478bd9Sstevel@tonic-gate static char **bam_argv;
216c7c0ceafSToomas Soome static char *bam_pool;
2177c478bd9Sstevel@tonic-gate static int bam_argc;
2187c478bd9Sstevel@tonic-gate static int bam_check;
2195eea6091SEnrico Perla - Sun Microsystems static int bam_saved_check;
2207c478bd9Sstevel@tonic-gate static int bam_smf_check;
2217c478bd9Sstevel@tonic-gate static int bam_lock_fd = -1;
222e7cbe64fSgw25295 static int bam_zfs;
223c7c0ceafSToomas Soome static int bam_mbr;
224*f64ca102SToomas Soome char rootbuf[PATH_MAX] = "/";
225b610f78eSvikram static int bam_update_all;
226d876c67dSjg static int bam_alt_platform;
227d876c67dSjg static char *bam_platform;
228cedc7e57SEnrico Perla - Sun Microsystems static char *bam_home_env = NULL;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate /* function prototypes */
231986fd29aSsetje static void parse_args_internal(int, char *[]);
232986fd29aSsetje static void parse_args(int, char *argv[]);
233986fd29aSsetje static error_t bam_menu(char *, char *, int, char *[]);
234c7c0ceafSToomas Soome static error_t bam_install(char *, char *);
235986fd29aSsetje static error_t bam_archive(char *, char *);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate static void bam_lock(void);
2387c478bd9Sstevel@tonic-gate static void bam_unlock(void);
2397c478bd9Sstevel@tonic-gate 
240986fd29aSsetje static int exec_cmd(char *, filelist_t *);
241986fd29aSsetje static error_t read_globals(menu_t *, char *, char *, int);
242eb2bd662Svikram static int menu_on_bootdisk(char *os_root, char *menu_root);
243986fd29aSsetje static menu_t *menu_read(char *);
244986fd29aSsetje static error_t menu_write(char *, menu_t *);
245986fd29aSsetje static void linelist_free(line_t *);
246986fd29aSsetje static void menu_free(menu_t *);
247986fd29aSsetje static void filelist_free(filelist_t *);
248986fd29aSsetje static error_t list2file(char *, char *, char *, line_t *);
249986fd29aSsetje static error_t list_entry(menu_t *, char *, char *);
25044da779fSWilliam Kucharski static error_t list_setting(menu_t *, char *, char *);
251986fd29aSsetje static error_t delete_all_entries(menu_t *, char *, char *);
252eb2bd662Svikram static error_t update_entry(menu_t *mp, char *menu_root, char *opt);
253eb2bd662Svikram static error_t update_temp(menu_t *mp, char *dummy, char *opt);
2547c478bd9Sstevel@tonic-gate 
255c7c0ceafSToomas Soome static error_t install_bootloader(void);
256986fd29aSsetje static error_t update_archive(char *, char *);
257986fd29aSsetje static error_t list_archive(char *, char *);
258986fd29aSsetje static error_t update_all(char *, char *);
259986fd29aSsetje static error_t read_list(char *, filelist_t *);
260986fd29aSsetje static error_t set_option(menu_t *, char *, char *);
261986fd29aSsetje static error_t set_kernel(menu_t *, menu_cmd_t, char *, char *, size_t);
262eb2bd662Svikram static error_t get_kernel(menu_t *, menu_cmd_t, char *, size_t);
263986fd29aSsetje static char *expand_path(const char *);
2647c478bd9Sstevel@tonic-gate 
265986fd29aSsetje static long s_strtol(char *);
266986fd29aSsetje static int s_fputs(char *, FILE *);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate static int is_amd64(void);
26979755401Ssetje static char *get_machine(void);
2707c478bd9Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *);
271eb2bd662Svikram static int ufs_add_to_sign_list(char *sign);
272963390b4Svikram static error_t synchronize_BE_menu(void);
2737c478bd9Sstevel@tonic-gate 
27479c28b70SToomas Soome #if !defined(_OBP)
2752449e17fSsherrym static void ucode_install();
2762449e17fSsherrym #endif
2772449e17fSsherrym 
2787c478bd9Sstevel@tonic-gate /* Menu related sub commands */
2797c478bd9Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = {
280eb2bd662Svikram 	"set_option",		OPT_ABSENT,	set_option, 0,	/* PUB */
2811a97e40eSvikram 	"list_entry",		OPT_OPTIONAL,	list_entry, 1,	/* PUB */
2821a97e40eSvikram 	"delete_all_entries",	OPT_ABSENT,	delete_all_entries, 0, /* PVT */
2831a97e40eSvikram 	"update_entry",		OPT_REQ,	update_entry, 0, /* menu */
2841a97e40eSvikram 	"update_temp",		OPT_OPTIONAL,	update_temp, 0,	/* reboot */
285ae115bc7Smrj 	"upgrade",		OPT_ABSENT,	upgrade_menu, 0, /* menu */
28644da779fSWilliam Kucharski 	"list_setting",		OPT_OPTIONAL,	list_setting, 1, /* menu */
28744da779fSWilliam Kucharski 	"disable_hypervisor",	OPT_ABSENT,	cvt_to_metal, 0, /* menu */
28844da779fSWilliam Kucharski 	"enable_hypervisor",	OPT_ABSENT,	cvt_to_hyper, 0, /* menu */
2891a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
2907c478bd9Sstevel@tonic-gate };
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /* Archive related sub commands */
2937c478bd9Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = {
2941a97e40eSvikram 	"update",		OPT_ABSENT,	update_archive, 0, /* PUB */
2951a97e40eSvikram 	"update_all",		OPT_ABSENT,	update_all, 0,	/* PVT */
2961a97e40eSvikram 	"list",			OPT_OPTIONAL,	list_archive, 1, /* PUB */
2971a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
2987c478bd9Sstevel@tonic-gate };
2997c478bd9Sstevel@tonic-gate 
300c7c0ceafSToomas Soome /* Install related sub commands */
301c7c0ceafSToomas Soome static subcmd_defn_t inst_subcmds[] = {
302c7c0ceafSToomas Soome 	"install_bootloader",	OPT_ABSENT,	install_bootloader, 0, /* PUB */
303c7c0ceafSToomas Soome 	NULL,			0,		NULL, 0	/* must be last */
304c7c0ceafSToomas Soome };
305c7c0ceafSToomas Soome 
30648847494SEnrico Perla - Sun Microsystems enum dircache_copy_opt {
30748847494SEnrico Perla - Sun Microsystems 	FILE32 = 0,
30848847494SEnrico Perla - Sun Microsystems 	FILE64,
30948847494SEnrico Perla - Sun Microsystems 	CACHEDIR_NUM
31048847494SEnrico Perla - Sun Microsystems };
31148847494SEnrico Perla - Sun Microsystems 
31248847494SEnrico Perla - Sun Microsystems /*
31348847494SEnrico Perla - Sun Microsystems  * Directory specific flags:
31448847494SEnrico Perla - Sun Microsystems  * NEED_UPDATE : the specified archive needs to be updated
31548847494SEnrico Perla - Sun Microsystems  * NO_MULTI : don't extend the specified archive, but recreate it
31648847494SEnrico Perla - Sun Microsystems  */
31748847494SEnrico Perla - Sun Microsystems #define	NEED_UPDATE		0x00000001
31848847494SEnrico Perla - Sun Microsystems #define	NO_MULTI		0x00000002
31948847494SEnrico Perla - Sun Microsystems 
32048847494SEnrico Perla - Sun Microsystems #define	set_dir_flag(id, f)	(walk_arg.dirinfo[id].flags |= f)
32148847494SEnrico Perla - Sun Microsystems #define	unset_dir_flag(id, f)	(walk_arg.dirinfo[id].flags &= ~f)
32248847494SEnrico Perla - Sun Microsystems #define	is_dir_flag_on(id, f)	(walk_arg.dirinfo[id].flags & f ? 1 : 0)
32348847494SEnrico Perla - Sun Microsystems 
32448847494SEnrico Perla - Sun Microsystems #define	get_cachedir(id)	(walk_arg.dirinfo[id].cdir_path)
32548847494SEnrico Perla - Sun Microsystems #define	get_updatedir(id)	(walk_arg.dirinfo[id].update_path)
32648847494SEnrico Perla - Sun Microsystems #define	get_count(id)		(walk_arg.dirinfo[id].count)
32748847494SEnrico Perla - Sun Microsystems #define	has_cachedir(id)	(walk_arg.dirinfo[id].has_dir)
32848847494SEnrico Perla - Sun Microsystems #define	set_dir_present(id)	(walk_arg.dirinfo[id].has_dir = 1)
32948847494SEnrico Perla - Sun Microsystems 
33048847494SEnrico Perla - Sun Microsystems /*
33148847494SEnrico Perla - Sun Microsystems  * dirinfo_t (specific cache directory information):
33248847494SEnrico Perla - Sun Microsystems  * cdir_path:   path to the archive cache directory
33348847494SEnrico Perla - Sun Microsystems  * update_path: path to the update directory (contains the files that will be
33448847494SEnrico Perla - Sun Microsystems  *              used to extend the archive)
33548847494SEnrico Perla - Sun Microsystems  * has_dir:	the specified cache directory is active
33648847494SEnrico Perla - Sun Microsystems  * count:	the number of files to update
33748847494SEnrico Perla - Sun Microsystems  * flags:	directory specific flags
33848847494SEnrico Perla - Sun Microsystems  */
33948847494SEnrico Perla - Sun Microsystems typedef struct _dirinfo {
34048847494SEnrico Perla - Sun Microsystems 	char	cdir_path[PATH_MAX];
34148847494SEnrico Perla - Sun Microsystems 	char	update_path[PATH_MAX];
34248847494SEnrico Perla - Sun Microsystems 	int	has_dir;
34348847494SEnrico Perla - Sun Microsystems 	int	count;
34448847494SEnrico Perla - Sun Microsystems 	int	flags;
34548847494SEnrico Perla - Sun Microsystems } dirinfo_t;
34648847494SEnrico Perla - Sun Microsystems 
34748847494SEnrico Perla - Sun Microsystems /*
34848847494SEnrico Perla - Sun Microsystems  * Update flags:
34948847494SEnrico Perla - Sun Microsystems  * NEED_CACHE_DIR : cache directory is missing and needs to be created
35048847494SEnrico Perla - Sun Microsystems  * IS_SPARC_TARGET : the target mountpoint is a SPARC environment
35148847494SEnrico Perla - Sun Microsystems  * UPDATE_ERROR : an error occourred while traversing the list of files
35248847494SEnrico Perla - Sun Microsystems  * RDONLY_FSCHK : the target filesystem is read-only
35348847494SEnrico Perla - Sun Microsystems  * RAMDSK_FSCHK : the target filesystem is on a ramdisk
35448847494SEnrico Perla - Sun Microsystems  */
35548847494SEnrico Perla - Sun Microsystems #define	NEED_CACHE_DIR		0x00000001
35648847494SEnrico Perla - Sun Microsystems #define	IS_SPARC_TARGET		0x00000002
35748847494SEnrico Perla - Sun Microsystems #define	UPDATE_ERROR		0x00000004
35848847494SEnrico Perla - Sun Microsystems #define	RDONLY_FSCHK		0x00000008
3594a9df875SEnrico Perla - Sun Microsystems #define	INVALIDATE_CACHE	0x00000010
36048847494SEnrico Perla - Sun Microsystems 
36148847494SEnrico Perla - Sun Microsystems #define	is_flag_on(flag)	(walk_arg.update_flags & flag ? 1 : 0)
36248847494SEnrico Perla - Sun Microsystems #define	set_flag(flag)		(walk_arg.update_flags |= flag)
36348847494SEnrico Perla - Sun Microsystems #define	unset_flag(flag)	(walk_arg.update_flags &= ~flag)
36448847494SEnrico Perla - Sun Microsystems 
36548847494SEnrico Perla - Sun Microsystems /*
36648847494SEnrico Perla - Sun Microsystems  * struct walk_arg :
36748847494SEnrico Perla - Sun Microsystems  * update_flags: flags related to the current updating process
36848847494SEnrico Perla - Sun Microsystems  * new_nvlp/old_nvlp: new and old list of archive-files / attributes pairs
36948847494SEnrico Perla - Sun Microsystems  * sparcfile: list of file paths for mkisofs -path-list (SPARC only)
37048847494SEnrico Perla - Sun Microsystems  */
3717c478bd9Sstevel@tonic-gate static struct {
37248847494SEnrico Perla - Sun Microsystems 	int 		update_flags;
3737c478bd9Sstevel@tonic-gate 	nvlist_t 	*new_nvlp;
3747c478bd9Sstevel@tonic-gate 	nvlist_t 	*old_nvlp;
37548847494SEnrico Perla - Sun Microsystems 	FILE 		*sparcfile;
37648847494SEnrico Perla - Sun Microsystems 	dirinfo_t	dirinfo[CACHEDIR_NUM];
3777c478bd9Sstevel@tonic-gate } walk_arg;
3787c478bd9Sstevel@tonic-gate 
3799632cfadSsetje struct safefile {
38058091fd8Ssetje 	char *name;
38158091fd8Ssetje 	struct safefile *next;
38258091fd8Ssetje };
38358091fd8Ssetje 
384ae115bc7Smrj static struct safefile *safefiles = NULL;
3853b133becSGangadhar Mylapuram 
3863b133becSGangadhar Mylapuram /*
3873b133becSGangadhar Mylapuram  * svc:/system/filesystem/usr:default service checks for this file and
3883b133becSGangadhar Mylapuram  * does a boot archive update and then reboot the system.
3893b133becSGangadhar Mylapuram  */
39058091fd8Ssetje #define	NEED_UPDATE_FILE "/etc/svc/volatile/boot_archive_needs_update"
39158091fd8Ssetje 
3923b133becSGangadhar Mylapuram /*
3933b133becSGangadhar Mylapuram  * svc:/system/boot-archive-update:default checks for this file and
3943b133becSGangadhar Mylapuram  * updates the boot archive.
3953b133becSGangadhar Mylapuram  */
3963b133becSGangadhar Mylapuram #define	NEED_UPDATE_SAFE_FILE "/etc/svc/volatile/boot_archive_safefile_update"
3973b133becSGangadhar Mylapuram 
39848847494SEnrico Perla - Sun Microsystems /* Thanks growisofs */
39948847494SEnrico Perla - Sun Microsystems #define	CD_BLOCK	((off64_t)2048)
40048847494SEnrico Perla - Sun Microsystems #define	VOLDESC_OFF	16
40148847494SEnrico Perla - Sun Microsystems #define	DVD_BLOCK	(32*1024)
40248847494SEnrico Perla - Sun Microsystems #define	MAX_IVDs	16
40348847494SEnrico Perla - Sun Microsystems 
40448847494SEnrico Perla - Sun Microsystems struct iso_pdesc {
40548847494SEnrico Perla - Sun Microsystems     unsigned char type	[1];
40648847494SEnrico Perla - Sun Microsystems     unsigned char id	[5];
40748847494SEnrico Perla - Sun Microsystems     unsigned char void1	[80-5-1];
40848847494SEnrico Perla - Sun Microsystems     unsigned char volume_space_size [8];
40948847494SEnrico Perla - Sun Microsystems     unsigned char void2	[2048-80-8];
41048847494SEnrico Perla - Sun Microsystems };
41148847494SEnrico Perla - Sun Microsystems 
41248847494SEnrico Perla - Sun Microsystems /*
41348847494SEnrico Perla - Sun Microsystems  * COUNT_MAX:	maximum number of changed files to justify a multisession update
41448847494SEnrico Perla - Sun Microsystems  * BA_SIZE_MAX:	maximum size of the boot_archive to justify a multisession
41548847494SEnrico Perla - Sun Microsystems  * 		update
41648847494SEnrico Perla - Sun Microsystems  */
41748847494SEnrico Perla - Sun Microsystems #define	COUNT_MAX		50
41848847494SEnrico Perla - Sun Microsystems #define	BA_SIZE_MAX		(50 * 1024 * 1024)
41948847494SEnrico Perla - Sun Microsystems 
42048847494SEnrico Perla - Sun Microsystems #define	bam_nowrite()		(bam_check || bam_smf_check)
42148847494SEnrico Perla - Sun Microsystems 
42219397407SSherry Moore static int sync_menu = 1;	/* whether we need to sync the BE menus */
42319397407SSherry Moore 
4247c478bd9Sstevel@tonic-gate static void
4257c478bd9Sstevel@tonic-gate usage(void)
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "USAGE:\n");
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	/* archive usage */
430d876c67dSjg 	(void) fprintf(stderr,
431c365cc98SToomas Soome 	    "\t%s update-archive [-vn] [-R altroot [-p platform]]\n", prog);
432d876c67dSjg 	(void) fprintf(stderr,
433c365cc98SToomas Soome 	    "\t%s list-archive [-R altroot [-p platform]]\n", prog);
43479c28b70SToomas Soome #if defined(_OBP)
435c7c0ceafSToomas Soome 	(void) fprintf(stderr,
436c7c0ceafSToomas Soome 	    "\t%s install-bootloader [-fv] [-R altroot] [-P pool]\n", prog);
437c7c0ceafSToomas Soome #else
438c7c0ceafSToomas Soome 	(void) fprintf(stderr,
439c7c0ceafSToomas Soome 	    "\t%s install-bootloader [-Mfv] [-R altroot] [-P pool]\n", prog);
440c7c0ceafSToomas Soome #endif
44179c28b70SToomas Soome #if !defined(_OBP)
4427c478bd9Sstevel@tonic-gate 	/* x86 only */
4437c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog);
4447c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog);
4457c478bd9Sstevel@tonic-gate #endif
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate 
448cedc7e57SEnrico Perla - Sun Microsystems /*
449cedc7e57SEnrico Perla - Sun Microsystems  * Best effort attempt to restore the $HOME value.
450cedc7e57SEnrico Perla - Sun Microsystems  */
451cedc7e57SEnrico Perla - Sun Microsystems static void
452cedc7e57SEnrico Perla - Sun Microsystems restore_env()
453cedc7e57SEnrico Perla - Sun Microsystems {
454cedc7e57SEnrico Perla - Sun Microsystems 	char	home_env[PATH_MAX];
455cedc7e57SEnrico Perla - Sun Microsystems 
456cedc7e57SEnrico Perla - Sun Microsystems 	if (bam_home_env) {
457cedc7e57SEnrico Perla - Sun Microsystems 		(void) snprintf(home_env, sizeof (home_env), "HOME=%s",
458cedc7e57SEnrico Perla - Sun Microsystems 		    bam_home_env);
459cedc7e57SEnrico Perla - Sun Microsystems 		(void) putenv(home_env);
460cedc7e57SEnrico Perla - Sun Microsystems 	}
461cedc7e57SEnrico Perla - Sun Microsystems }
462cedc7e57SEnrico Perla - Sun Microsystems 
463cedc7e57SEnrico Perla - Sun Microsystems 
464cedc7e57SEnrico Perla - Sun Microsystems #define		SLEEP_TIME	5
465cedc7e57SEnrico Perla - Sun Microsystems #define		MAX_TRIES	4
466cedc7e57SEnrico Perla - Sun Microsystems 
467cedc7e57SEnrico Perla - Sun Microsystems /*
468cedc7e57SEnrico Perla - Sun Microsystems  * Sanitize the environment in which bootadm will execute its sub-processes
469cedc7e57SEnrico Perla - Sun Microsystems  * (ex. mkisofs). This is done to prevent those processes from attempting
470cedc7e57SEnrico Perla - Sun Microsystems  * to access files (ex. .mkisofsrc) or stat paths that might be on NFS
471cedc7e57SEnrico Perla - Sun Microsystems  * or, potentially, insecure.
472cedc7e57SEnrico Perla - Sun Microsystems  */
473cedc7e57SEnrico Perla - Sun Microsystems static void
474cedc7e57SEnrico Perla - Sun Microsystems sanitize_env()
475cedc7e57SEnrico Perla - Sun Microsystems {
476cedc7e57SEnrico Perla - Sun Microsystems 	int	stry = 0;
477cedc7e57SEnrico Perla - Sun Microsystems 
478cedc7e57SEnrico Perla - Sun Microsystems 	/* don't depend on caller umask */
479cedc7e57SEnrico Perla - Sun Microsystems 	(void) umask(0022);
480cedc7e57SEnrico Perla - Sun Microsystems 
481cedc7e57SEnrico Perla - Sun Microsystems 	/* move away from a potential unsafe current working directory */
482cedc7e57SEnrico Perla - Sun Microsystems 	while (chdir("/") == -1) {
483cedc7e57SEnrico Perla - Sun Microsystems 		if (errno != EINTR) {
484cedc7e57SEnrico Perla - Sun Microsystems 			bam_print("WARNING: unable to chdir to /");
485cedc7e57SEnrico Perla - Sun Microsystems 			break;
486cedc7e57SEnrico Perla - Sun Microsystems 		}
487cedc7e57SEnrico Perla - Sun Microsystems 	}
488cedc7e57SEnrico Perla - Sun Microsystems 
489cedc7e57SEnrico Perla - Sun Microsystems 	bam_home_env = getenv("HOME");
490cedc7e57SEnrico Perla - Sun Microsystems 	while (bam_home_env != NULL && putenv("HOME=/") == -1) {
491cedc7e57SEnrico Perla - Sun Microsystems 		if (errno == ENOMEM) {
492cedc7e57SEnrico Perla - Sun Microsystems 			/* retry no more than MAX_TRIES times */
493cedc7e57SEnrico Perla - Sun Microsystems 			if (++stry > MAX_TRIES) {
494cedc7e57SEnrico Perla - Sun Microsystems 				bam_print("WARNING: unable to recover from "
495cedc7e57SEnrico Perla - Sun Microsystems 				    "system memory pressure... aborting \n");
496cedc7e57SEnrico Perla - Sun Microsystems 				bam_exit(EXIT_FAILURE);
497cedc7e57SEnrico Perla - Sun Microsystems 			}
498cedc7e57SEnrico Perla - Sun Microsystems 			/* memory is tight, try to sleep */
499cedc7e57SEnrico Perla - Sun Microsystems 			bam_print("Attempting to recover from memory pressure: "
500cedc7e57SEnrico Perla - Sun Microsystems 			    "sleeping for %d seconds\n", SLEEP_TIME * stry);
501cedc7e57SEnrico Perla - Sun Microsystems 			(void) sleep(SLEEP_TIME * stry);
502cedc7e57SEnrico Perla - Sun Microsystems 		} else {
503cedc7e57SEnrico Perla - Sun Microsystems 			bam_print("WARNING: unable to sanitize HOME\n");
504cedc7e57SEnrico Perla - Sun Microsystems 		}
505cedc7e57SEnrico Perla - Sun Microsystems 	}
506cedc7e57SEnrico Perla - Sun Microsystems }
507cedc7e57SEnrico Perla - Sun Microsystems 
5087c478bd9Sstevel@tonic-gate int
5097c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
5107c478bd9Sstevel@tonic-gate {
511110570ceSToomas Soome 	error_t ret = BAM_SUCCESS;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
5147c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
5177c478bd9Sstevel@tonic-gate 		prog = argv[0];
5187c478bd9Sstevel@tonic-gate 	} else {
5197c478bd9Sstevel@tonic-gate 		prog++;
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 
522eb2bd662Svikram 	INJECT_ERROR1("ASSERT_ON", assert(0))
5237c478bd9Sstevel@tonic-gate 
524cedc7e57SEnrico Perla - Sun Microsystems 	sanitize_env();
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	switch (bam_cmd) {
5297c478bd9Sstevel@tonic-gate 		case BAM_MENU:
530*f64ca102SToomas Soome 			if (is_grub(bam_alt_root ? bam_root : "/")) {
531*f64ca102SToomas Soome 				ret = bam_menu(bam_subcmd, bam_opt,
532*f64ca102SToomas Soome 				    bam_argc, bam_argv);
533*f64ca102SToomas Soome 			} else {
534*f64ca102SToomas Soome 				ret = bam_loader_menu(bam_subcmd, bam_opt,
535*f64ca102SToomas Soome 				    bam_argc, bam_argv);
536*f64ca102SToomas Soome 			}
5377c478bd9Sstevel@tonic-gate 			break;
5387c478bd9Sstevel@tonic-gate 		case BAM_ARCHIVE:
5397c478bd9Sstevel@tonic-gate 			ret = bam_archive(bam_subcmd, bam_opt);
5407c478bd9Sstevel@tonic-gate 			break;
541c7c0ceafSToomas Soome 		case BAM_INSTALL:
542c7c0ceafSToomas Soome 			ret = bam_install(bam_subcmd, bam_opt);
543c7c0ceafSToomas Soome 			break;
5447c478bd9Sstevel@tonic-gate 		default:
5457c478bd9Sstevel@tonic-gate 			usage();
5467c478bd9Sstevel@tonic-gate 			bam_exit(1);
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS)
55044da779fSWilliam Kucharski 		bam_exit((ret == BAM_NOCHANGE) ? 2 : 1);
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	bam_unlock();
5537c478bd9Sstevel@tonic-gate 	return (0);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate  * Equivalence of public and internal commands:
5587c478bd9Sstevel@tonic-gate  *	update-archive  -- -a update
5597c478bd9Sstevel@tonic-gate  *	list-archive	-- -a list
5607c478bd9Sstevel@tonic-gate  *	set-menu	-- -m set_option
5617c478bd9Sstevel@tonic-gate  *	list-menu	-- -m list_entry
5627c478bd9Sstevel@tonic-gate  *	update-menu	-- -m update_entry
563c7c0ceafSToomas Soome  *	install-bootloader	-- -i install_bootloader
5647c478bd9Sstevel@tonic-gate  */
5657c478bd9Sstevel@tonic-gate static struct cmd_map {
5667c478bd9Sstevel@tonic-gate 	char *bam_cmdname;
5677c478bd9Sstevel@tonic-gate 	int bam_cmd;
5687c478bd9Sstevel@tonic-gate 	char *bam_subcmd;
5697c478bd9Sstevel@tonic-gate } cmd_map[] = {
5707c478bd9Sstevel@tonic-gate 	{ "update-archive",	BAM_ARCHIVE,	"update"},
5717c478bd9Sstevel@tonic-gate 	{ "list-archive",	BAM_ARCHIVE,	"list"},
5727c478bd9Sstevel@tonic-gate 	{ "set-menu",		BAM_MENU,	"set_option"},
5737c478bd9Sstevel@tonic-gate 	{ "list-menu",		BAM_MENU,	"list_entry"},
5747c478bd9Sstevel@tonic-gate 	{ "update-menu",	BAM_MENU,	"update_entry"},
575c7c0ceafSToomas Soome 	{ "install-bootloader",	BAM_INSTALL,	"install_bootloader"},
5767c478bd9Sstevel@tonic-gate 	{ NULL,			0,		NULL}
5777c478bd9Sstevel@tonic-gate };
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate /*
5807c478bd9Sstevel@tonic-gate  * Commands syntax published in bootadm(1M) are parsed here
5817c478bd9Sstevel@tonic-gate  */
5827c478bd9Sstevel@tonic-gate static void
5837c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[])
5847c478bd9Sstevel@tonic-gate {
5857c478bd9Sstevel@tonic-gate 	struct cmd_map *cmp = cmd_map;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	/* command conforming to the final spec */
5887c478bd9Sstevel@tonic-gate 	if (argc > 1 && argv[1][0] != '-') {
5897c478bd9Sstevel@tonic-gate 		/*
5907c478bd9Sstevel@tonic-gate 		 * Map commands to internal table.
5917c478bd9Sstevel@tonic-gate 		 */
5927c478bd9Sstevel@tonic-gate 		while (cmp->bam_cmdname) {
5937c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], cmp->bam_cmdname) == 0) {
5947c478bd9Sstevel@tonic-gate 				bam_cmd = cmp->bam_cmd;
5957c478bd9Sstevel@tonic-gate 				bam_subcmd = cmp->bam_subcmd;
5967c478bd9Sstevel@tonic-gate 				break;
5977c478bd9Sstevel@tonic-gate 			}
5987c478bd9Sstevel@tonic-gate 			cmp++;
5997c478bd9Sstevel@tonic-gate 		}
6007c478bd9Sstevel@tonic-gate 		if (cmp->bam_cmdname == NULL) {
6017c478bd9Sstevel@tonic-gate 			usage();
6027c478bd9Sstevel@tonic-gate 			bam_exit(1);
6037c478bd9Sstevel@tonic-gate 		}
6047c478bd9Sstevel@tonic-gate 		argc--;
6057c478bd9Sstevel@tonic-gate 		argv++;
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	parse_args_internal(argc, argv);
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate /*
6127c478bd9Sstevel@tonic-gate  * A combination of public and private commands are parsed here.
6137c478bd9Sstevel@tonic-gate  * The internal syntax and the corresponding functionality are:
6147c478bd9Sstevel@tonic-gate  *	-a update			-- update-archive
6157c478bd9Sstevel@tonic-gate  *	-a list				-- list-archive
61644da779fSWilliam Kucharski  *	-a update-all			-- (reboot to sync all mnted OS archive)
617c7c0ceafSToomas Soome  *	-i install_bootloader		-- install-bootloader
6187c478bd9Sstevel@tonic-gate  *	-m update_entry			-- update-menu
6197c478bd9Sstevel@tonic-gate  *	-m list_entry			-- list-menu
6207c478bd9Sstevel@tonic-gate  *	-m update_temp			-- (reboot -- [boot-args])
6217c478bd9Sstevel@tonic-gate  *	-m delete_all_entries		-- (called from install)
62244da779fSWilliam Kucharski  *	-m enable_hypervisor [args]	-- cvt_to_hyper
62344da779fSWilliam Kucharski  *	-m disable_hypervisor		-- cvt_to_metal
62444da779fSWilliam Kucharski  *	-m list_setting [entry] [value]	-- list_setting
62544da779fSWilliam Kucharski  *
62648847494SEnrico Perla - Sun Microsystems  * A set of private flags is there too:
62748847494SEnrico Perla - Sun Microsystems  *	-F		-- purge the cache directories and rebuild them
62848847494SEnrico Perla - Sun Microsystems  *	-e		-- use the (faster) archive update approach (used by
62948847494SEnrico Perla - Sun Microsystems  *			   reboot)
6307c478bd9Sstevel@tonic-gate  */
6317c478bd9Sstevel@tonic-gate static void
6327c478bd9Sstevel@tonic-gate parse_args_internal(int argc, char *argv[])
6337c478bd9Sstevel@tonic-gate {
6347c478bd9Sstevel@tonic-gate 	int c, error;
6357c478bd9Sstevel@tonic-gate 	extern char *optarg;
6367c478bd9Sstevel@tonic-gate 	extern int optind, opterr;
63779c28b70SToomas Soome #if defined(_OBP)
638c7c0ceafSToomas Soome 	const char *optstring = "a:d:fi:m:no:veFCR:p:P:XZ";
639c7c0ceafSToomas Soome #else
640c7c0ceafSToomas Soome 	const char *optstring = "a:d:fi:m:no:veFCMR:p:P:XZ";
641c7c0ceafSToomas Soome #endif
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	/* Suppress error message from getopt */
6447c478bd9Sstevel@tonic-gate 	opterr = 0;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	error = 0;
647c7c0ceafSToomas Soome 	while ((c = getopt(argc, argv, optstring)) != -1) {
6487c478bd9Sstevel@tonic-gate 		switch (c) {
6497c478bd9Sstevel@tonic-gate 		case 'a':
6507c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
6517c478bd9Sstevel@tonic-gate 				error = 1;
652*f64ca102SToomas Soome 				bam_error(
653*f64ca102SToomas Soome 				    _("multiple commands specified: -%c\n"), c);
6547c478bd9Sstevel@tonic-gate 			}
6557c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_ARCHIVE;
6567c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
6577c478bd9Sstevel@tonic-gate 			break;
6587c478bd9Sstevel@tonic-gate 		case 'd':
6597c478bd9Sstevel@tonic-gate 			if (bam_debug) {
6607c478bd9Sstevel@tonic-gate 				error = 1;
661*f64ca102SToomas Soome 				bam_error(
662*f64ca102SToomas Soome 				    _("duplicate options specified: -%c\n"), c);
6637c478bd9Sstevel@tonic-gate 			}
6647c478bd9Sstevel@tonic-gate 			bam_debug = s_strtol(optarg);
6657c478bd9Sstevel@tonic-gate 			break;
6667c478bd9Sstevel@tonic-gate 		case 'f':
6677c478bd9Sstevel@tonic-gate 			bam_force = 1;
6687c478bd9Sstevel@tonic-gate 			break;
66948847494SEnrico Perla - Sun Microsystems 		case 'F':
67048847494SEnrico Perla - Sun Microsystems 			bam_purge = 1;
67148847494SEnrico Perla - Sun Microsystems 			break;
672c7c0ceafSToomas Soome 		case 'i':
673c7c0ceafSToomas Soome 			if (bam_cmd) {
674c7c0ceafSToomas Soome 				error = 1;
675*f64ca102SToomas Soome 				bam_error(
676*f64ca102SToomas Soome 				    _("multiple commands specified: -%c\n"), c);
677c7c0ceafSToomas Soome 			}
678c7c0ceafSToomas Soome 			bam_cmd = BAM_INSTALL;
679c7c0ceafSToomas Soome 			bam_subcmd = optarg;
680c7c0ceafSToomas Soome 			break;
6817c478bd9Sstevel@tonic-gate 		case 'm':
6827c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
6837c478bd9Sstevel@tonic-gate 				error = 1;
684*f64ca102SToomas Soome 				bam_error(
685*f64ca102SToomas Soome 				    _("multiple commands specified: -%c\n"), c);
6867c478bd9Sstevel@tonic-gate 			}
6877c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_MENU;
6887c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
6897c478bd9Sstevel@tonic-gate 			break;
69079c28b70SToomas Soome #if !defined(_OBP)
691c7c0ceafSToomas Soome 		case 'M':
692c7c0ceafSToomas Soome 			bam_mbr = 1;
693c7c0ceafSToomas Soome 			break;
694c7c0ceafSToomas Soome #endif
6957c478bd9Sstevel@tonic-gate 		case 'n':
6967c478bd9Sstevel@tonic-gate 			bam_check = 1;
6975eea6091SEnrico Perla - Sun Microsystems 			/*
6985eea6091SEnrico Perla - Sun Microsystems 			 * We save the original value of bam_check. The new
6995eea6091SEnrico Perla - Sun Microsystems 			 * approach in case of a read-only filesystem is to
7005eea6091SEnrico Perla - Sun Microsystems 			 * behave as a check, so we need a way to restore the
7015eea6091SEnrico Perla - Sun Microsystems 			 * original value after the evaluation of the read-only
7025eea6091SEnrico Perla - Sun Microsystems 			 * filesystem has been done.
7035eea6091SEnrico Perla - Sun Microsystems 			 * Even if we don't allow at the moment a check with
7045eea6091SEnrico Perla - Sun Microsystems 			 * update_all, this approach is more robust than
7055eea6091SEnrico Perla - Sun Microsystems 			 * simply resetting bam_check to zero.
7065eea6091SEnrico Perla - Sun Microsystems 			 */
7075eea6091SEnrico Perla - Sun Microsystems 			bam_saved_check = 1;
7087c478bd9Sstevel@tonic-gate 			break;
7097c478bd9Sstevel@tonic-gate 		case 'o':
7107c478bd9Sstevel@tonic-gate 			if (bam_opt) {
7117c478bd9Sstevel@tonic-gate 				error = 1;
712*f64ca102SToomas Soome 				bam_error(
713*f64ca102SToomas Soome 				    _("duplicate options specified: -%c\n"), c);
7147c478bd9Sstevel@tonic-gate 			}
7157c478bd9Sstevel@tonic-gate 			bam_opt = optarg;
7167c478bd9Sstevel@tonic-gate 			break;
7177c478bd9Sstevel@tonic-gate 		case 'v':
7187c478bd9Sstevel@tonic-gate 			bam_verbose = 1;
7197c478bd9Sstevel@tonic-gate 			break;
7208c1b6884Sszhou 		case 'C':
7218c1b6884Sszhou 			bam_smf_check = 1;
7228c1b6884Sszhou 			break;
723c7c0ceafSToomas Soome 		case 'P':
724c7c0ceafSToomas Soome 			if (bam_pool != NULL) {
725c7c0ceafSToomas Soome 				error = 1;
726*f64ca102SToomas Soome 				bam_error(
727*f64ca102SToomas Soome 				    _("duplicate options specified: -%c\n"), c);
728c7c0ceafSToomas Soome 			}
729c7c0ceafSToomas Soome 			bam_pool = optarg;
730c7c0ceafSToomas Soome 			break;
7317c478bd9Sstevel@tonic-gate 		case 'R':
7327c478bd9Sstevel@tonic-gate 			if (bam_root) {
7337c478bd9Sstevel@tonic-gate 				error = 1;
734*f64ca102SToomas Soome 				bam_error(
735*f64ca102SToomas Soome 				    _("duplicate options specified: -%c\n"), c);
7367c478bd9Sstevel@tonic-gate 				break;
7377c478bd9Sstevel@tonic-gate 			} else if (realpath(optarg, rootbuf) == NULL) {
7387c478bd9Sstevel@tonic-gate 				error = 1;
739*f64ca102SToomas Soome 				bam_error(_("cannot resolve path %s: %s\n"),
740*f64ca102SToomas Soome 				    optarg, strerror(errno));
7417c478bd9Sstevel@tonic-gate 				break;
7427c478bd9Sstevel@tonic-gate 			}
74340541d5dSvikram 			bam_alt_root = 1;
7447c478bd9Sstevel@tonic-gate 			bam_root = rootbuf;
7457c478bd9Sstevel@tonic-gate 			bam_rootlen = strlen(rootbuf);
7467c478bd9Sstevel@tonic-gate 			break;
747d876c67dSjg 		case 'p':
748d876c67dSjg 			bam_alt_platform = 1;
749d876c67dSjg 			bam_platform = optarg;
750d876c67dSjg 			if ((strcmp(bam_platform, "i86pc") != 0) &&
751d876c67dSjg 			    (strcmp(bam_platform, "sun4u") != 0) &&
752d876c67dSjg 			    (strcmp(bam_platform, "sun4v") != 0)) {
753d876c67dSjg 				error = 1;
754*f64ca102SToomas Soome 				bam_error(_("invalid platform %s - must be "
755*f64ca102SToomas Soome 				    "one of sun4u, sun4v or i86pc\n"),
756*f64ca102SToomas Soome 				    bam_platform);
757d876c67dSjg 			}
758d876c67dSjg 			break;
7593028dfd6SFrank Van Der Linden 		case 'X':
7603028dfd6SFrank Van Der Linden 			bam_is_hv = BAM_HV_PRESENT;
7613028dfd6SFrank Van Der Linden 			break;
762e7cbe64fSgw25295 		case 'Z':
763e7cbe64fSgw25295 			bam_zfs = 1;
764e7cbe64fSgw25295 			break;
76548847494SEnrico Perla - Sun Microsystems 		case 'e':
76648847494SEnrico Perla - Sun Microsystems 			bam_extend = 1;
76748847494SEnrico Perla - Sun Microsystems 			break;
7687c478bd9Sstevel@tonic-gate 		case '?':
7697c478bd9Sstevel@tonic-gate 			error = 1;
770*f64ca102SToomas Soome 			bam_error(_("invalid option or missing option "
771*f64ca102SToomas Soome 			    "argument: -%c\n"), optopt);
7727c478bd9Sstevel@tonic-gate 			break;
7737c478bd9Sstevel@tonic-gate 		default :
7747c478bd9Sstevel@tonic-gate 			error = 1;
775*f64ca102SToomas Soome 			bam_error(_("invalid option or missing option "
776*f64ca102SToomas Soome 			    "argument: -%c\n"), c);
7777c478bd9Sstevel@tonic-gate 			break;
7787c478bd9Sstevel@tonic-gate 		}
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	/*
782d876c67dSjg 	 * An alternate platform requires an alternate root
783d876c67dSjg 	 */
784d876c67dSjg 	if (bam_alt_platform && bam_alt_root == 0) {
785d876c67dSjg 		usage();
786d876c67dSjg 		bam_exit(0);
787d876c67dSjg 	}
788d876c67dSjg 
789d876c67dSjg 	/*
7907c478bd9Sstevel@tonic-gate 	 * A command option must be specfied
7917c478bd9Sstevel@tonic-gate 	 */
7927c478bd9Sstevel@tonic-gate 	if (!bam_cmd) {
7937c478bd9Sstevel@tonic-gate 		if (bam_opt && strcmp(bam_opt, "all") == 0) {
7947c478bd9Sstevel@tonic-gate 			usage();
7957c478bd9Sstevel@tonic-gate 			bam_exit(0);
7967c478bd9Sstevel@tonic-gate 		}
797*f64ca102SToomas Soome 		bam_error(_("a command option must be specified\n"));
7987c478bd9Sstevel@tonic-gate 		error = 1;
7997c478bd9Sstevel@tonic-gate 	}
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	if (error) {
8027c478bd9Sstevel@tonic-gate 		usage();
8037c478bd9Sstevel@tonic-gate 		bam_exit(1);
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	if (optind > argc) {
807*f64ca102SToomas Soome 		bam_error(_("Internal error: %s\n"), "parse_args");
8087c478bd9Sstevel@tonic-gate 		bam_exit(1);
8097c478bd9Sstevel@tonic-gate 	} else if (optind < argc) {
8107c478bd9Sstevel@tonic-gate 		bam_argv = &argv[optind];
8117c478bd9Sstevel@tonic-gate 		bam_argc = argc - optind;
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	/*
815c7c0ceafSToomas Soome 	 * mbr and pool are options for install_bootloader
816c7c0ceafSToomas Soome 	 */
817c7c0ceafSToomas Soome 	if (bam_cmd != BAM_INSTALL && (bam_mbr || bam_pool != NULL)) {
818c7c0ceafSToomas Soome 		usage();
819c7c0ceafSToomas Soome 		bam_exit(0);
820c7c0ceafSToomas Soome 	}
821c7c0ceafSToomas Soome 
822c7c0ceafSToomas Soome 	/*
8237c478bd9Sstevel@tonic-gate 	 * -n implies verbose mode
8247c478bd9Sstevel@tonic-gate 	 */
8257c478bd9Sstevel@tonic-gate 	if (bam_check)
8267c478bd9Sstevel@tonic-gate 		bam_verbose = 1;
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate 
829*f64ca102SToomas Soome error_t
8307c478bd9Sstevel@tonic-gate check_subcmd_and_options(
8317c478bd9Sstevel@tonic-gate 	char *subcmd,
8327c478bd9Sstevel@tonic-gate 	char *opt,
8337c478bd9Sstevel@tonic-gate 	subcmd_defn_t *table,
8347c478bd9Sstevel@tonic-gate 	error_t (**fp)())
8357c478bd9Sstevel@tonic-gate {
8367c478bd9Sstevel@tonic-gate 	int i;
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	if (subcmd == NULL) {
839*f64ca102SToomas Soome 		bam_error(_("this command requires a sub-command\n"));
8407c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
8417c478bd9Sstevel@tonic-gate 	}
8427c478bd9Sstevel@tonic-gate 
843eb2bd662Svikram 	if (strcmp(subcmd, "set_option") == 0) {
844eb2bd662Svikram 		if (bam_argc == 0 || bam_argv == NULL || bam_argv[0] == NULL) {
845*f64ca102SToomas Soome 			bam_error(_("missing argument for sub-command\n"));
846eb2bd662Svikram 			usage();
847eb2bd662Svikram 			return (BAM_ERROR);
848eb2bd662Svikram 		} else if (bam_argc > 1 || bam_argv[1] != NULL) {
849*f64ca102SToomas Soome 			bam_error(_("invalid trailing arguments\n"));
8501a97e40eSvikram 			usage();
8511a97e40eSvikram 			return (BAM_ERROR);
8521a97e40eSvikram 		}
85319397407SSherry Moore 	} else if (strcmp(subcmd, "update_all") == 0) {
85419397407SSherry Moore 		/*
85519397407SSherry Moore 		 * The only option we accept for the "update_all"
85619397407SSherry Moore 		 * subcmd is "fastboot".
85719397407SSherry Moore 		 */
85819397407SSherry Moore 		if (bam_argc > 1 || (bam_argc == 1 &&
85919397407SSherry Moore 		    strcmp(bam_argv[0], "fastboot") != 0)) {
860*f64ca102SToomas Soome 			bam_error(_("invalid trailing arguments\n"));
86119397407SSherry Moore 			usage();
86219397407SSherry Moore 			return (BAM_ERROR);
86319397407SSherry Moore 		}
86419397407SSherry Moore 		if (bam_argc == 1)
86519397407SSherry Moore 			sync_menu = 0;
86644da779fSWilliam Kucharski 	} else if (((strcmp(subcmd, "enable_hypervisor") != 0) &&
86744da779fSWilliam Kucharski 	    (strcmp(subcmd, "list_setting") != 0)) && (bam_argc || bam_argv)) {
86844da779fSWilliam Kucharski 		/*
86944da779fSWilliam Kucharski 		 * Of the remaining subcommands, only "enable_hypervisor" and
87044da779fSWilliam Kucharski 		 * "list_setting" take trailing arguments.
87144da779fSWilliam Kucharski 		 */
872*f64ca102SToomas Soome 		bam_error(_("invalid trailing arguments\n"));
873eb2bd662Svikram 		usage();
874eb2bd662Svikram 		return (BAM_ERROR);
8751a97e40eSvikram 	}
8761a97e40eSvikram 
8777c478bd9Sstevel@tonic-gate 	if (bam_root == NULL) {
8787c478bd9Sstevel@tonic-gate 		bam_root = rootbuf;
8797c478bd9Sstevel@tonic-gate 		bam_rootlen = 1;
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	/* verify that subcmd is valid */
8837c478bd9Sstevel@tonic-gate 	for (i = 0; table[i].subcmd != NULL; i++) {
8847c478bd9Sstevel@tonic-gate 		if (strcmp(table[i].subcmd, subcmd) == 0)
8857c478bd9Sstevel@tonic-gate 			break;
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	if (table[i].subcmd == NULL) {
889*f64ca102SToomas Soome 		bam_error(_("invalid sub-command specified: %s\n"), subcmd);
8907c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 
8931a97e40eSvikram 	if (table[i].unpriv == 0 && geteuid() != 0) {
894*f64ca102SToomas Soome 		bam_error(_("you must be root to run this command\n"));
8951a97e40eSvikram 		return (BAM_ERROR);
8961a97e40eSvikram 	}
8971a97e40eSvikram 
8981a97e40eSvikram 	/*
8991a97e40eSvikram 	 * Currently only privileged commands need a lock
9001a97e40eSvikram 	 */
9011a97e40eSvikram 	if (table[i].unpriv == 0)
9021a97e40eSvikram 		bam_lock();
9031a97e40eSvikram 
9047c478bd9Sstevel@tonic-gate 	/* subcmd verifies that opt is appropriate */
9057c478bd9Sstevel@tonic-gate 	if (table[i].option != OPT_OPTIONAL) {
9067c478bd9Sstevel@tonic-gate 		if ((table[i].option == OPT_REQ) ^ (opt != NULL)) {
9077c478bd9Sstevel@tonic-gate 			if (opt)
908*f64ca102SToomas Soome 				bam_error(_("this sub-command (%s) does not "
909*f64ca102SToomas Soome 				    "take options\n"), subcmd);
9107c478bd9Sstevel@tonic-gate 			else
911*f64ca102SToomas Soome 				bam_error(_("an option is required for this "
912*f64ca102SToomas Soome 				    "sub-command: %s\n"), subcmd);
9137c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
9147c478bd9Sstevel@tonic-gate 		}
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	*fp = table[i].handler;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
9207c478bd9Sstevel@tonic-gate }
9217c478bd9Sstevel@tonic-gate 
92240541d5dSvikram /*
92340541d5dSvikram  * NOTE: A single "/" is also considered a trailing slash and will
92440541d5dSvikram  * be deleted.
92540541d5dSvikram  */
926*f64ca102SToomas Soome void
92740541d5dSvikram elide_trailing_slash(const char *src, char *dst, size_t dstsize)
92840541d5dSvikram {
92940541d5dSvikram 	size_t dstlen;
93040541d5dSvikram 
93140541d5dSvikram 	assert(src);
93240541d5dSvikram 	assert(dst);
93340541d5dSvikram 
93440541d5dSvikram 	(void) strlcpy(dst, src, dstsize);
93540541d5dSvikram 
93640541d5dSvikram 	dstlen = strlen(dst);
93740541d5dSvikram 	if (dst[dstlen - 1] == '/') {
93840541d5dSvikram 		dst[dstlen - 1] = '\0';
93940541d5dSvikram 	}
94040541d5dSvikram }
94140541d5dSvikram 
942cedc7e57SEnrico Perla - Sun Microsystems static int
943cedc7e57SEnrico Perla - Sun Microsystems is_safe_exec(char *path)
944cedc7e57SEnrico Perla - Sun Microsystems {
945cedc7e57SEnrico Perla - Sun Microsystems 	struct stat	sb;
946cedc7e57SEnrico Perla - Sun Microsystems 
947cedc7e57SEnrico Perla - Sun Microsystems 	if (lstat(path, &sb) != 0) {
948*f64ca102SToomas Soome 		bam_error(_("stat of file failed: %s: %s\n"), path,
949*f64ca102SToomas Soome 		    strerror(errno));
950cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
951cedc7e57SEnrico Perla - Sun Microsystems 	}
952cedc7e57SEnrico Perla - Sun Microsystems 
953cedc7e57SEnrico Perla - Sun Microsystems 	if (!S_ISREG(sb.st_mode)) {
954*f64ca102SToomas Soome 		bam_error(_("%s is not a regular file, skipping\n"), path);
955cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
956cedc7e57SEnrico Perla - Sun Microsystems 	}
957cedc7e57SEnrico Perla - Sun Microsystems 
958cedc7e57SEnrico Perla - Sun Microsystems 	if (sb.st_uid != getuid()) {
959*f64ca102SToomas Soome 		bam_error(_("%s is not owned by %d, skipping\n"),
960*f64ca102SToomas Soome 		    path, getuid());
961cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
962cedc7e57SEnrico Perla - Sun Microsystems 	}
963cedc7e57SEnrico Perla - Sun Microsystems 
964cedc7e57SEnrico Perla - Sun Microsystems 	if (sb.st_mode & S_IWOTH || sb.st_mode & S_IWGRP) {
965*f64ca102SToomas Soome 		bam_error(_("%s is others or group writable, skipping\n"),
966*f64ca102SToomas Soome 		    path);
967cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
968cedc7e57SEnrico Perla - Sun Microsystems 	}
969cedc7e57SEnrico Perla - Sun Microsystems 
970cedc7e57SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
971cedc7e57SEnrico Perla - Sun Microsystems }
972cedc7e57SEnrico Perla - Sun Microsystems 
9737c478bd9Sstevel@tonic-gate static error_t
97444da779fSWilliam Kucharski list_setting(menu_t *mp, char *which, char *setting)
97544da779fSWilliam Kucharski {
97644da779fSWilliam Kucharski 	line_t	*lp;
97744da779fSWilliam Kucharski 	entry_t	*ent;
97844da779fSWilliam Kucharski 
97944da779fSWilliam Kucharski 	char	*p = which;
98044da779fSWilliam Kucharski 	int	entry;
98144da779fSWilliam Kucharski 
98244da779fSWilliam Kucharski 	int	found;
98344da779fSWilliam Kucharski 
98444da779fSWilliam Kucharski 	assert(which);
98544da779fSWilliam Kucharski 	assert(setting);
98644da779fSWilliam Kucharski 
98744da779fSWilliam Kucharski 	if (*which != NULL) {
98844da779fSWilliam Kucharski 		/*
98944da779fSWilliam Kucharski 		 * If "which" is not a number, assume it's a setting we want
99044da779fSWilliam Kucharski 		 * to look for and so set up the routine to look for "which"
99144da779fSWilliam Kucharski 		 * in the default entry.
99244da779fSWilliam Kucharski 		 */
99344da779fSWilliam Kucharski 		while (*p != NULL)
99444da779fSWilliam Kucharski 			if (!(isdigit((int)*p++))) {
99544da779fSWilliam Kucharski 				setting = which;
99644da779fSWilliam Kucharski 				which = mp->curdefault->arg;
99744da779fSWilliam Kucharski 				break;
99844da779fSWilliam Kucharski 			}
99944da779fSWilliam Kucharski 	} else {
100044da779fSWilliam Kucharski 		which = mp->curdefault->arg;
100144da779fSWilliam Kucharski 	}
100244da779fSWilliam Kucharski 
100344da779fSWilliam Kucharski 	entry = atoi(which);
100444da779fSWilliam Kucharski 
100544da779fSWilliam Kucharski 	for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != entry));
100644da779fSWilliam Kucharski 	    ent = ent->next)
100744da779fSWilliam Kucharski 		;
100844da779fSWilliam Kucharski 
100944da779fSWilliam Kucharski 	if (!ent) {
1010*f64ca102SToomas Soome 		bam_error(_("no matching entry found\n"));
101144da779fSWilliam Kucharski 		return (BAM_ERROR);
101244da779fSWilliam Kucharski 	}
101344da779fSWilliam Kucharski 
101444da779fSWilliam Kucharski 	found = (*setting == NULL);
101544da779fSWilliam Kucharski 
101644da779fSWilliam Kucharski 	for (lp = ent->start; lp != NULL; lp = lp->next) {
101744da779fSWilliam Kucharski 		if ((*setting == NULL) && (lp->flags != BAM_COMMENT))
1018*f64ca102SToomas Soome 			bam_print("%s\n", lp->line);
1019eac223ccSWilliam Kucharski 		else if (lp->cmd != NULL && strcmp(setting, lp->cmd) == 0) {
1020*f64ca102SToomas Soome 			bam_print("%s\n", lp->arg);
102144da779fSWilliam Kucharski 			found = 1;
102244da779fSWilliam Kucharski 		}
102344da779fSWilliam Kucharski 
102444da779fSWilliam Kucharski 		if (lp == ent->end)
102544da779fSWilliam Kucharski 			break;
102644da779fSWilliam Kucharski 	}
102744da779fSWilliam Kucharski 
102844da779fSWilliam Kucharski 	if (!found) {
1029*f64ca102SToomas Soome 		bam_error(_("no matching entry found\n"));
103044da779fSWilliam Kucharski 		return (BAM_ERROR);
103144da779fSWilliam Kucharski 	}
103244da779fSWilliam Kucharski 
103344da779fSWilliam Kucharski 	return (BAM_SUCCESS);
103444da779fSWilliam Kucharski }
103544da779fSWilliam Kucharski 
103644da779fSWilliam Kucharski static error_t
1037c7c0ceafSToomas Soome install_bootloader(void)
1038c7c0ceafSToomas Soome {
1039c7c0ceafSToomas Soome 	nvlist_t	*nvl;
1040c7c0ceafSToomas Soome 	uint16_t	flags = 0;
1041c7c0ceafSToomas Soome 	int		found = 0;
1042c7c0ceafSToomas Soome 	struct extmnttab mnt;
1043c7c0ceafSToomas Soome 	struct stat	statbuf = {0};
1044c7c0ceafSToomas Soome 	be_node_list_t	*be_nodes, *node;
1045c7c0ceafSToomas Soome 	FILE		*fp;
1046c7c0ceafSToomas Soome 	char		*root_ds = NULL;
1047110570ceSToomas Soome 	int		ret = BAM_ERROR;
1048c7c0ceafSToomas Soome 
1049c7c0ceafSToomas Soome 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
1050c7c0ceafSToomas Soome 		bam_error(_("out of memory\n"));
1051110570ceSToomas Soome 		return (ret);
1052c7c0ceafSToomas Soome 	}
1053c7c0ceafSToomas Soome 
1054c7c0ceafSToomas Soome 	/*
1055c7c0ceafSToomas Soome 	 * if bam_alt_root is set, the stage files are used from alt root.
1056c7c0ceafSToomas Soome 	 * if pool is set, the target devices are pool devices, stage files
1057c7c0ceafSToomas Soome 	 * are read from pool bootfs unless alt root is set.
1058c7c0ceafSToomas Soome 	 *
1059c7c0ceafSToomas Soome 	 * use arguments as targets, stage files are from alt or current root
1060c7c0ceafSToomas Soome 	 * if no arguments and no pool, install on current boot pool.
1061c7c0ceafSToomas Soome 	 */
1062c7c0ceafSToomas Soome 
1063c7c0ceafSToomas Soome 	if (bam_alt_root) {
1064c7c0ceafSToomas Soome 		if (stat(bam_root, &statbuf) != 0) {
1065*f64ca102SToomas Soome 			bam_error(_("stat of file failed: %s: %s\n"), bam_root,
1066*f64ca102SToomas Soome 			    strerror(errno));
1067c7c0ceafSToomas Soome 			goto done;
1068c7c0ceafSToomas Soome 		}
1069c7c0ceafSToomas Soome 		if ((fp = fopen(MNTTAB, "r")) == NULL) {
1070*f64ca102SToomas Soome 			bam_error(_("failed to open file: %s: %s\n"),
1071*f64ca102SToomas Soome 			    MNTTAB, strerror(errno));
1072c7c0ceafSToomas Soome 			goto done;
1073c7c0ceafSToomas Soome 		}
1074c7c0ceafSToomas Soome 		resetmnttab(fp);
1075c7c0ceafSToomas Soome 		while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
1076c7c0ceafSToomas Soome 			if (mnt.mnt_major == major(statbuf.st_dev) &&
1077c7c0ceafSToomas Soome 			    mnt.mnt_minor == minor(statbuf.st_dev)) {
1078c7c0ceafSToomas Soome 				found = 1;
1079c7c0ceafSToomas Soome 				root_ds = strdup(mnt.mnt_special);
1080c7c0ceafSToomas Soome 				break;
1081c7c0ceafSToomas Soome 			}
1082c7c0ceafSToomas Soome 		}
1083c7c0ceafSToomas Soome 		(void) fclose(fp);
1084c7c0ceafSToomas Soome 
1085c7c0ceafSToomas Soome 		if (found == 0) {
1086*f64ca102SToomas Soome 			bam_error(_("alternate root %s not in mnttab\n"),
1087*f64ca102SToomas Soome 			    bam_root);
1088c7c0ceafSToomas Soome 			goto done;
1089c7c0ceafSToomas Soome 		}
1090c7c0ceafSToomas Soome 		if (root_ds == NULL) {
1091c7c0ceafSToomas Soome 			bam_error(_("out of memory\n"));
1092c7c0ceafSToomas Soome 			goto done;
1093c7c0ceafSToomas Soome 		}
1094c7c0ceafSToomas Soome 
1095c7c0ceafSToomas Soome 		if (be_list(NULL, &be_nodes) != BE_SUCCESS) {
1096c7c0ceafSToomas Soome 			bam_error(_("No BE's found\n"));
1097c7c0ceafSToomas Soome 			goto done;
1098c7c0ceafSToomas Soome 		}
1099c7c0ceafSToomas Soome 		for (node = be_nodes; node != NULL; node = node->be_next_node)
1100c7c0ceafSToomas Soome 			if (strcmp(root_ds, node->be_root_ds) == 0)
1101c7c0ceafSToomas Soome 				break;
1102c7c0ceafSToomas Soome 
1103c7c0ceafSToomas Soome 		if (node == NULL)
1104c7c0ceafSToomas Soome 			bam_error(_("BE (%s) does not exist\n"), root_ds);
1105c7c0ceafSToomas Soome 
1106c7c0ceafSToomas Soome 		free(root_ds);
1107c7c0ceafSToomas Soome 		root_ds = NULL;
1108c7c0ceafSToomas Soome 		if (node == NULL) {
1109c7c0ceafSToomas Soome 			be_free_list(be_nodes);
1110c7c0ceafSToomas Soome 			goto done;
1111c7c0ceafSToomas Soome 		}
1112c7c0ceafSToomas Soome 		ret = nvlist_add_string(nvl, BE_ATTR_ORIG_BE_NAME,
1113c7c0ceafSToomas Soome 		    node->be_node_name);
1114c7c0ceafSToomas Soome 		ret |= nvlist_add_string(nvl, BE_ATTR_ORIG_BE_ROOT,
1115c7c0ceafSToomas Soome 		    node->be_root_ds);
1116c7c0ceafSToomas Soome 		be_free_list(be_nodes);
1117110570ceSToomas Soome 		if (ret != 0) {
1118c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1119c7c0ceafSToomas Soome 			goto done;
1120c7c0ceafSToomas Soome 		}
1121c7c0ceafSToomas Soome 	}
1122c7c0ceafSToomas Soome 
1123c7c0ceafSToomas Soome 	if (bam_force)
1124c7c0ceafSToomas Soome 		flags |= BE_INSTALLBOOT_FLAG_FORCE;
1125c7c0ceafSToomas Soome 	if (bam_mbr)
1126c7c0ceafSToomas Soome 		flags |= BE_INSTALLBOOT_FLAG_MBR;
1127c7c0ceafSToomas Soome 	if (bam_verbose)
1128c7c0ceafSToomas Soome 		flags |= BE_INSTALLBOOT_FLAG_VERBOSE;
1129c7c0ceafSToomas Soome 
1130c7c0ceafSToomas Soome 	if (nvlist_add_uint16(nvl, BE_ATTR_INSTALL_FLAGS, flags) != 0) {
1131c7c0ceafSToomas Soome 		bam_error(_("out of memory\n"));
1132110570ceSToomas Soome 		ret = BAM_ERROR;
1133c7c0ceafSToomas Soome 		goto done;
1134c7c0ceafSToomas Soome 	}
1135c7c0ceafSToomas Soome 
1136c7c0ceafSToomas Soome 	/*
1137c7c0ceafSToomas Soome 	 * if altroot was set, we got be name and be root, only need
1138c7c0ceafSToomas Soome 	 * to set pool name as target.
1139c7c0ceafSToomas Soome 	 * if no altroot, need to find be name and root from pool.
1140c7c0ceafSToomas Soome 	 */
1141c7c0ceafSToomas Soome 	if (bam_pool != NULL) {
1142c7c0ceafSToomas Soome 		ret = nvlist_add_string(nvl, BE_ATTR_ORIG_BE_POOL, bam_pool);
1143110570ceSToomas Soome 		if (ret != 0) {
1144c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1145c7c0ceafSToomas Soome 			goto done;
1146c7c0ceafSToomas Soome 		}
1147c7c0ceafSToomas Soome 		if (found) {
1148c7c0ceafSToomas Soome 			ret = be_installboot(nvl);
1149110570ceSToomas Soome 			if (ret != 0)
1150c7c0ceafSToomas Soome 				ret = BAM_ERROR;
1151c7c0ceafSToomas Soome 			goto done;
1152c7c0ceafSToomas Soome 		}
1153c7c0ceafSToomas Soome 	}
1154c7c0ceafSToomas Soome 
1155c7c0ceafSToomas Soome 	if (be_list(NULL, &be_nodes) != BE_SUCCESS) {
1156c7c0ceafSToomas Soome 		bam_error(_("No BE's found\n"));
1157c7c0ceafSToomas Soome 		ret = BAM_ERROR;
1158c7c0ceafSToomas Soome 		goto done;
1159c7c0ceafSToomas Soome 	}
1160c7c0ceafSToomas Soome 
1161c7c0ceafSToomas Soome 	if (bam_pool != NULL) {
1162c7c0ceafSToomas Soome 		/*
1163c7c0ceafSToomas Soome 		 * find active be_node in bam_pool
1164c7c0ceafSToomas Soome 		 */
1165c7c0ceafSToomas Soome 		for (node = be_nodes; node != NULL; node = node->be_next_node) {
1166c7c0ceafSToomas Soome 			if (strcmp(bam_pool, node->be_rpool) != 0)
1167c7c0ceafSToomas Soome 				continue;
1168c7c0ceafSToomas Soome 			if (node->be_active_on_boot)
1169c7c0ceafSToomas Soome 				break;
1170c7c0ceafSToomas Soome 		}
1171c7c0ceafSToomas Soome 		if (node == NULL) {
1172c7c0ceafSToomas Soome 			bam_error(_("No active BE in %s\n"), bam_pool);
1173c7c0ceafSToomas Soome 			be_free_list(be_nodes);
1174c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1175c7c0ceafSToomas Soome 			goto done;
1176c7c0ceafSToomas Soome 		}
1177c7c0ceafSToomas Soome 		ret = nvlist_add_string(nvl, BE_ATTR_ORIG_BE_NAME,
1178c7c0ceafSToomas Soome 		    node->be_node_name);
1179c7c0ceafSToomas Soome 		ret |= nvlist_add_string(nvl, BE_ATTR_ORIG_BE_ROOT,
1180c7c0ceafSToomas Soome 		    node->be_root_ds);
1181c7c0ceafSToomas Soome 		be_free_list(be_nodes);
1182110570ceSToomas Soome 		if (ret != 0) {
1183c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1184c7c0ceafSToomas Soome 			goto done;
1185c7c0ceafSToomas Soome 		}
1186c7c0ceafSToomas Soome 		ret = be_installboot(nvl);
1187110570ceSToomas Soome 		if (ret != 0)
1188c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1189c7c0ceafSToomas Soome 		goto done;
1190c7c0ceafSToomas Soome 	}
1191c7c0ceafSToomas Soome 
1192c7c0ceafSToomas Soome 	/*
1193c7c0ceafSToomas Soome 	 * get dataset for "/" and fill up the args.
1194c7c0ceafSToomas Soome 	 */
1195c7c0ceafSToomas Soome 	if ((fp = fopen(MNTTAB, "r")) == NULL) {
1196*f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"),
1197*f64ca102SToomas Soome 		    MNTTAB, strerror(errno));
1198c7c0ceafSToomas Soome 		ret = BAM_ERROR;
1199c7c0ceafSToomas Soome 		be_free_list(be_nodes);
1200c7c0ceafSToomas Soome 		goto done;
1201c7c0ceafSToomas Soome 	}
1202c7c0ceafSToomas Soome 	resetmnttab(fp);
1203c7c0ceafSToomas Soome 	found = 0;
1204c7c0ceafSToomas Soome 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
1205c7c0ceafSToomas Soome 		if (strcmp(mnt.mnt_mountp, "/") == 0) {
1206c7c0ceafSToomas Soome 			found = 1;
1207c7c0ceafSToomas Soome 			root_ds = strdup(mnt.mnt_special);
1208c7c0ceafSToomas Soome 			break;
1209c7c0ceafSToomas Soome 		}
1210c7c0ceafSToomas Soome 	}
1211c7c0ceafSToomas Soome 	(void) fclose(fp);
1212c7c0ceafSToomas Soome 
1213c7c0ceafSToomas Soome 	if (found == 0) {
1214*f64ca102SToomas Soome 		bam_error(_("alternate root %s not in mnttab\n"), "/");
1215c7c0ceafSToomas Soome 		ret = BAM_ERROR;
1216c7c0ceafSToomas Soome 		be_free_list(be_nodes);
1217c7c0ceafSToomas Soome 		goto done;
1218c7c0ceafSToomas Soome 	}
1219c7c0ceafSToomas Soome 	if (root_ds == NULL) {
1220c7c0ceafSToomas Soome 		bam_error(_("out of memory\n"));
1221c7c0ceafSToomas Soome 		ret = BAM_ERROR;
1222c7c0ceafSToomas Soome 		be_free_list(be_nodes);
1223c7c0ceafSToomas Soome 		goto done;
1224c7c0ceafSToomas Soome 	}
1225c7c0ceafSToomas Soome 
1226c7c0ceafSToomas Soome 	for (node = be_nodes; node != NULL; node = node->be_next_node) {
1227c7c0ceafSToomas Soome 		if (strcmp(root_ds, node->be_root_ds) == 0)
1228c7c0ceafSToomas Soome 			break;
1229c7c0ceafSToomas Soome 	}
1230c7c0ceafSToomas Soome 
1231c7c0ceafSToomas Soome 	if (node == NULL) {
1232c7c0ceafSToomas Soome 		bam_error(_("No such BE: %s\n"), root_ds);
1233c7c0ceafSToomas Soome 		free(root_ds);
1234c7c0ceafSToomas Soome 		be_free_list(be_nodes);
1235c7c0ceafSToomas Soome 		ret = BAM_ERROR;
1236c7c0ceafSToomas Soome 		goto done;
1237c7c0ceafSToomas Soome 	}
1238c7c0ceafSToomas Soome 	free(root_ds);
1239c7c0ceafSToomas Soome 
1240c7c0ceafSToomas Soome 	ret = nvlist_add_string(nvl, BE_ATTR_ORIG_BE_NAME, node->be_node_name);
1241c7c0ceafSToomas Soome 	ret |= nvlist_add_string(nvl, BE_ATTR_ORIG_BE_ROOT, node->be_root_ds);
1242c7c0ceafSToomas Soome 	ret |= nvlist_add_string(nvl, BE_ATTR_ORIG_BE_POOL, node->be_rpool);
1243c7c0ceafSToomas Soome 	be_free_list(be_nodes);
1244c7c0ceafSToomas Soome 
1245110570ceSToomas Soome 	if (ret != 0)
1246c7c0ceafSToomas Soome 		ret = BAM_ERROR;
1247c7c0ceafSToomas Soome 	else
1248c7c0ceafSToomas Soome 		ret = be_installboot(nvl) ? BAM_ERROR : 0;
1249c7c0ceafSToomas Soome done:
1250c7c0ceafSToomas Soome 	nvlist_free(nvl);
1251c7c0ceafSToomas Soome 
1252c7c0ceafSToomas Soome 	return (ret);
1253c7c0ceafSToomas Soome }
1254c7c0ceafSToomas Soome 
1255c7c0ceafSToomas Soome static error_t
1256c7c0ceafSToomas Soome bam_install(char *subcmd, char *opt)
1257c7c0ceafSToomas Soome {
1258c7c0ceafSToomas Soome 	error_t (*f)(void);
1259c7c0ceafSToomas Soome 
1260c7c0ceafSToomas Soome 	/*
1261c7c0ceafSToomas Soome 	 * Check arguments
1262c7c0ceafSToomas Soome 	 */
1263c7c0ceafSToomas Soome 	if (check_subcmd_and_options(subcmd, opt, inst_subcmds, &f) ==
1264c7c0ceafSToomas Soome 	    BAM_ERROR)
1265c7c0ceafSToomas Soome 		return (BAM_ERROR);
1266c7c0ceafSToomas Soome 
1267c7c0ceafSToomas Soome 	return (f());
1268c7c0ceafSToomas Soome }
1269c7c0ceafSToomas Soome 
1270c7c0ceafSToomas Soome static error_t
12717c478bd9Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[])
12727c478bd9Sstevel@tonic-gate {
12737c478bd9Sstevel@tonic-gate 	error_t			ret;
12747c478bd9Sstevel@tonic-gate 	char			menu_path[PATH_MAX];
1275eb2bd662Svikram 	char			clean_menu_root[PATH_MAX];
1276ae115bc7Smrj 	char			path[PATH_MAX];
12777c478bd9Sstevel@tonic-gate 	menu_t			*menu;
1278eb2bd662Svikram 	char			menu_root[PATH_MAX];
1279b610f78eSvikram 	struct stat		sb;
12807c478bd9Sstevel@tonic-gate 	error_t (*f)(menu_t *mp, char *menu_path, char *opt);
1281110570ceSToomas Soome 	char			*special = NULL;
1282eb2bd662Svikram 	char			*pool = NULL;
1283eb2bd662Svikram 	zfs_mnted_t		zmnted;
1284110570ceSToomas Soome 	char			*zmntpt = NULL;
1285eb2bd662Svikram 	char			*osdev;
1286eb2bd662Svikram 	char			*osroot;
1287eb2bd662Svikram 	const char		*fcn = "bam_menu()";
1288986fd29aSsetje 
1289986fd29aSsetje 	/*
1290986fd29aSsetje 	 * Menu sub-command only applies to GRUB (i.e. x86)
1291986fd29aSsetje 	 */
1292eb2bd662Svikram 	if (!is_grub(bam_alt_root ? bam_root : "/")) {
1293*f64ca102SToomas Soome 		bam_error(_("not a GRUB 0.97 based Illumos instance. "
1294*f64ca102SToomas Soome 		    "Operation not supported\n"));
1295986fd29aSsetje 		return (BAM_ERROR);
1296986fd29aSsetje 	}
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 	/*
12997c478bd9Sstevel@tonic-gate 	 * Check arguments
13007c478bd9Sstevel@tonic-gate 	 */
13017c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
13027c478bd9Sstevel@tonic-gate 	if (ret == BAM_ERROR) {
13037c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13047c478bd9Sstevel@tonic-gate 	}
13057c478bd9Sstevel@tonic-gate 
1306eb2bd662Svikram 	assert(bam_root);
1307eb2bd662Svikram 
1308eb2bd662Svikram 	(void) strlcpy(menu_root, bam_root, sizeof (menu_root));
1309eb2bd662Svikram 	osdev = osroot = NULL;
1310eb2bd662Svikram 
1311eb2bd662Svikram 	if (strcmp(subcmd, "update_entry") == 0) {
1312eb2bd662Svikram 		assert(opt);
1313eb2bd662Svikram 
1314eb2bd662Svikram 		osdev = strtok(opt, ",");
1315eb2bd662Svikram 		assert(osdev);
1316eb2bd662Svikram 		osroot = strtok(NULL, ",");
1317eb2bd662Svikram 		if (osroot) {
1318eb2bd662Svikram 			/* fixup bam_root so that it points at osroot */
1319eb2bd662Svikram 			if (realpath(osroot, rootbuf) == NULL) {
1320*f64ca102SToomas Soome 				bam_error(_("cannot resolve path %s: %s\n"),
1321*f64ca102SToomas Soome 				    osroot, strerror(errno));
1322eb2bd662Svikram 				return (BAM_ERROR);
1323eb2bd662Svikram 			}
1324eb2bd662Svikram 			bam_alt_root = 1;
1325eb2bd662Svikram 			bam_root  = rootbuf;
1326eb2bd662Svikram 			bam_rootlen = strlen(rootbuf);
1327eb2bd662Svikram 		}
1328eb2bd662Svikram 	}
132940541d5dSvikram 
133040541d5dSvikram 	/*
1331eb2bd662Svikram 	 * We support menu on PCFS (under certain conditions), but
1332eb2bd662Svikram 	 * not the OS root
133340541d5dSvikram 	 */
1334eb2bd662Svikram 	if (is_pcfs(bam_root)) {
1335*f64ca102SToomas Soome 		bam_error(_("root <%s> on PCFS is not supported\n"), bam_root);
1336eb2bd662Svikram 		return (BAM_ERROR);
1337ae115bc7Smrj 	}
1338ae115bc7Smrj 
1339eb2bd662Svikram 	if (stat(menu_root, &sb) == -1) {
1340*f64ca102SToomas Soome 		bam_error(_("cannot find GRUB menu\n"));
134140541d5dSvikram 		return (BAM_ERROR);
134240541d5dSvikram 	}
134340541d5dSvikram 
1344*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: menu root is %s\n", fcn, menu_root));
1345e7cbe64fSgw25295 
1346eb2bd662Svikram 	/*
1347eb2bd662Svikram 	 * We no longer use the GRUB slice file. If it exists, then
1348eb2bd662Svikram 	 * the user is doing something that is unsupported (such as
1349eb2bd662Svikram 	 * standard upgrading an old Live Upgrade BE). If that
1350eb2bd662Svikram 	 * happens, mimic existing behavior i.e. pretend that it is
1351eb2bd662Svikram 	 * not a BE. Emit a warning though.
1352eb2bd662Svikram 	 */
1353eb2bd662Svikram 	if (bam_alt_root) {
1354eb2bd662Svikram 		(void) snprintf(path, sizeof (path), "%s%s", bam_root,
1355eb2bd662Svikram 		    GRUB_slice);
1356eb2bd662Svikram 	} else {
1357eb2bd662Svikram 		(void) snprintf(path, sizeof (path), "%s", GRUB_slice);
1358e7cbe64fSgw25295 	}
1359e7cbe64fSgw25295 
136037eb779cSVikram Hegde 	if (bam_verbose && stat(path, &sb) == 0)
1361*f64ca102SToomas Soome 		bam_error(_("unsupported GRUB slice file (%s) exists - "
1362*f64ca102SToomas Soome 		    "ignoring.\n"), path);
1363eb2bd662Svikram 
1364eb2bd662Svikram 	if (is_zfs(menu_root)) {
1365eb2bd662Svikram 		assert(strcmp(menu_root, bam_root) == 0);
1366eb2bd662Svikram 		special = get_special(menu_root);
1367eb2bd662Svikram 		INJECT_ERROR1("Z_MENU_GET_SPECIAL", special = NULL);
1368eb2bd662Svikram 		if (special == NULL) {
1369*f64ca102SToomas Soome 			bam_error(_("cant find special file for "
1370*f64ca102SToomas Soome 			    "mount-point %s\n"), menu_root);
1371eb2bd662Svikram 			return (BAM_ERROR);
1372eb2bd662Svikram 		}
1373eb2bd662Svikram 		pool = strtok(special, "/");
1374eb2bd662Svikram 		INJECT_ERROR1("Z_MENU_GET_POOL", pool = NULL);
1375eb2bd662Svikram 		if (pool == NULL) {
1376eb2bd662Svikram 			free(special);
1377*f64ca102SToomas Soome 			bam_error(_("cant find pool for mount-point %s\n"),
1378*f64ca102SToomas Soome 			    menu_root);
1379eb2bd662Svikram 			return (BAM_ERROR);
1380eb2bd662Svikram 		}
1381*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: derived pool=%s from special\n", fcn, pool));
1382eb2bd662Svikram 
1383eb2bd662Svikram 		zmntpt = mount_top_dataset(pool, &zmnted);
1384eb2bd662Svikram 		INJECT_ERROR1("Z_MENU_MOUNT_TOP_DATASET", zmntpt = NULL);
1385eb2bd662Svikram 		if (zmntpt == NULL) {
1386*f64ca102SToomas Soome 			bam_error(_("cannot mount pool dataset for pool: %s\n"),
1387*f64ca102SToomas Soome 			    pool);
1388eb2bd662Svikram 			free(special);
1389eb2bd662Svikram 			return (BAM_ERROR);
1390eb2bd662Svikram 		}
1391*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: top dataset mountpoint=%s\n", fcn, zmntpt));
1392eb2bd662Svikram 
1393eb2bd662Svikram 		(void) strlcpy(menu_root, zmntpt, sizeof (menu_root));
1394*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: zfs menu_root=%s\n", fcn, menu_root));
1395eb2bd662Svikram 	}
1396eb2bd662Svikram 
1397eb2bd662Svikram 	elide_trailing_slash(menu_root, clean_menu_root,
1398eb2bd662Svikram 	    sizeof (clean_menu_root));
1399eb2bd662Svikram 
1400*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: cleaned menu root is <%s>\n", fcn, clean_menu_root));
1401eb2bd662Svikram 
1402eb2bd662Svikram 	(void) strlcpy(menu_path, clean_menu_root, sizeof (menu_path));
140340541d5dSvikram 	(void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path));
140440541d5dSvikram 
1405*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: menu path is: %s\n", fcn, menu_path));
1406eb2bd662Svikram 
140740541d5dSvikram 	/*
1408eb2bd662Svikram 	 * If listing the menu, display the menu location
140940541d5dSvikram 	 */
141044da779fSWilliam Kucharski 	if (strcmp(subcmd, "list_entry") == 0)
1411*f64ca102SToomas Soome 		bam_print(_("the location for the active GRUB menu is: %s\n"),
1412*f64ca102SToomas Soome 		    menu_path);
141344da779fSWilliam Kucharski 
141444da779fSWilliam Kucharski 	if ((menu = menu_read(menu_path)) == NULL) {
1415*f64ca102SToomas Soome 		bam_error(_("cannot find GRUB menu file: %s\n"), menu_path);
141644da779fSWilliam Kucharski 		free(special);
141744da779fSWilliam Kucharski 
141844da779fSWilliam Kucharski 		return (BAM_ERROR);
141940541d5dSvikram 	}
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	/*
1422eb2bd662Svikram 	 * We already checked the following case in
1423eb2bd662Svikram 	 * check_subcmd_and_suboptions() above. Complete the
1424eb2bd662Svikram 	 * final step now.
14257c478bd9Sstevel@tonic-gate 	 */
14267c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "set_option") == 0) {
1427eb2bd662Svikram 		assert(largc == 1 && largv[0] && largv[1] == NULL);
14287c478bd9Sstevel@tonic-gate 		opt = largv[0];
142944da779fSWilliam Kucharski 	} else if ((strcmp(subcmd, "enable_hypervisor") != 0) &&
143044da779fSWilliam Kucharski 	    (strcmp(subcmd, "list_setting") != 0)) {
1431eb2bd662Svikram 		assert(largc == 0 && largv == NULL);
14327c478bd9Sstevel@tonic-gate 	}
14337c478bd9Sstevel@tonic-gate 
1434eb2bd662Svikram 	ret = get_boot_cap(bam_root);
1435eb2bd662Svikram 	if (ret != BAM_SUCCESS) {
1436*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: Failed to get boot capability\n", fcn));
1437eb2bd662Svikram 		goto out;
1438eb2bd662Svikram 	}
1439ae115bc7Smrj 
14407c478bd9Sstevel@tonic-gate 	/*
14417c478bd9Sstevel@tonic-gate 	 * Once the sub-cmd handler has run
14427c478bd9Sstevel@tonic-gate 	 * only the line field is guaranteed to have valid values
14437c478bd9Sstevel@tonic-gate 	 */
144444da779fSWilliam Kucharski 	if (strcmp(subcmd, "update_entry") == 0) {
1445eb2bd662Svikram 		ret = f(menu, menu_root, osdev);
144644da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "upgrade") == 0) {
1447eb2bd662Svikram 		ret = f(menu, bam_root, menu_root);
144844da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "list_entry") == 0) {
14497c478bd9Sstevel@tonic-gate 		ret = f(menu, menu_path, opt);
145044da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "list_setting") == 0) {
145144da779fSWilliam Kucharski 		ret = f(menu, ((largc > 0) ? largv[0] : ""),
145244da779fSWilliam Kucharski 		    ((largc > 1) ? largv[1] : ""));
145344da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "disable_hypervisor") == 0) {
145444da779fSWilliam Kucharski 		if (is_sparc()) {
1455*f64ca102SToomas Soome 			bam_error(_("%s operation unsupported on SPARC "
1456*f64ca102SToomas Soome 			    "machines\n"), subcmd);
145744da779fSWilliam Kucharski 			ret = BAM_ERROR;
145844da779fSWilliam Kucharski 		} else {
145944da779fSWilliam Kucharski 			ret = f(menu, bam_root, NULL);
146044da779fSWilliam Kucharski 		}
146144da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "enable_hypervisor") == 0) {
146244da779fSWilliam Kucharski 		if (is_sparc()) {
1463*f64ca102SToomas Soome 			bam_error(_("%s operation unsupported on SPARC "
1464*f64ca102SToomas Soome 			    "machines\n"), subcmd);
146544da779fSWilliam Kucharski 			ret = BAM_ERROR;
146644da779fSWilliam Kucharski 		} else {
146744da779fSWilliam Kucharski 			char *extra_args = NULL;
146844da779fSWilliam Kucharski 
146944da779fSWilliam Kucharski 			/*
147044da779fSWilliam Kucharski 			 * Compress all arguments passed in the largv[] array
147144da779fSWilliam Kucharski 			 * into one string that can then be appended to the
147244da779fSWilliam Kucharski 			 * end of the kernel$ string the routine to enable the
147344da779fSWilliam Kucharski 			 * hypervisor will build.
147444da779fSWilliam Kucharski 			 *
147544da779fSWilliam Kucharski 			 * This allows the caller to supply arbitrary unparsed
147644da779fSWilliam Kucharski 			 * arguments, such as dom0 memory settings or APIC
147744da779fSWilliam Kucharski 			 * options.
147844da779fSWilliam Kucharski 			 *
147944da779fSWilliam Kucharski 			 * This concatenation will be done without ANY syntax
148044da779fSWilliam Kucharski 			 * checking whatsoever, so it's the responsibility of
148144da779fSWilliam Kucharski 			 * the caller to make sure the arguments are valid and
148244da779fSWilliam Kucharski 			 * do not duplicate arguments the conversion routines
148344da779fSWilliam Kucharski 			 * may create.
148444da779fSWilliam Kucharski 			 */
148544da779fSWilliam Kucharski 			if (largc > 0) {
148644da779fSWilliam Kucharski 				int extra_len, i;
148744da779fSWilliam Kucharski 
148844da779fSWilliam Kucharski 				for (extra_len = 0, i = 0; i < largc; i++)
148944da779fSWilliam Kucharski 					extra_len += strlen(largv[i]);
149044da779fSWilliam Kucharski 
149144da779fSWilliam Kucharski 				/*
149244da779fSWilliam Kucharski 				 * Allocate space for argument strings,
149344da779fSWilliam Kucharski 				 * intervening spaces and terminating NULL.
149444da779fSWilliam Kucharski 				 */
149544da779fSWilliam Kucharski 				extra_args = alloca(extra_len + largc);
149644da779fSWilliam Kucharski 
149744da779fSWilliam Kucharski 				(void) strcpy(extra_args, largv[0]);
149844da779fSWilliam Kucharski 
149944da779fSWilliam Kucharski 				for (i = 1; i < largc; i++) {
150044da779fSWilliam Kucharski 					(void) strcat(extra_args, " ");
150144da779fSWilliam Kucharski 					(void) strcat(extra_args, largv[i]);
150244da779fSWilliam Kucharski 				}
150344da779fSWilliam Kucharski 			}
150444da779fSWilliam Kucharski 
150544da779fSWilliam Kucharski 			ret = f(menu, bam_root, extra_args);
150644da779fSWilliam Kucharski 		}
150744da779fSWilliam Kucharski 	} else
1508eb2bd662Svikram 		ret = f(menu, NULL, opt);
1509eb2bd662Svikram 
15107c478bd9Sstevel@tonic-gate 	if (ret == BAM_WRITE) {
1511*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: writing menu to clean-menu-root: <%s>\n",
1512*f64ca102SToomas Soome 		    fcn, clean_menu_root));
1513eb2bd662Svikram 		ret = menu_write(clean_menu_root, menu);
15147c478bd9Sstevel@tonic-gate 	}
15157c478bd9Sstevel@tonic-gate 
1516eb2bd662Svikram out:
1517eb2bd662Svikram 	INJECT_ERROR1("POOL_SET", pool = "/pooldata");
1518eb2bd662Svikram 	assert((is_zfs(menu_root)) ^ (pool == NULL));
1519eb2bd662Svikram 	if (pool) {
1520eb2bd662Svikram 		(void) umount_top_dataset(pool, zmnted, zmntpt);
1521eb2bd662Svikram 		free(special);
1522eb2bd662Svikram 	}
15237c478bd9Sstevel@tonic-gate 	menu_free(menu);
15247c478bd9Sstevel@tonic-gate 	return (ret);
15257c478bd9Sstevel@tonic-gate }
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate static error_t
15297c478bd9Sstevel@tonic-gate bam_archive(
15307c478bd9Sstevel@tonic-gate 	char *subcmd,
15317c478bd9Sstevel@tonic-gate 	char *opt)
15327c478bd9Sstevel@tonic-gate {
15337c478bd9Sstevel@tonic-gate 	error_t			ret;
15347c478bd9Sstevel@tonic-gate 	error_t			(*f)(char *root, char *opt);
1535eb2bd662Svikram 	const char		*fcn = "bam_archive()";
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	/*
15388c1b6884Sszhou 	 * Add trailing / for archive subcommands
15398c1b6884Sszhou 	 */
15408c1b6884Sszhou 	if (rootbuf[strlen(rootbuf) - 1] != '/')
15418c1b6884Sszhou 		(void) strcat(rootbuf, "/");
15428c1b6884Sszhou 	bam_rootlen = strlen(rootbuf);
15438c1b6884Sszhou 
15448c1b6884Sszhou 	/*
15457c478bd9Sstevel@tonic-gate 	 * Check arguments
15467c478bd9Sstevel@tonic-gate 	 */
15477c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f);
15487c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS) {
15497c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15507c478bd9Sstevel@tonic-gate 	}
15517c478bd9Sstevel@tonic-gate 
1552eb2bd662Svikram 	ret = get_boot_cap(rootbuf);
1553eb2bd662Svikram 	if (ret != BAM_SUCCESS) {
1554*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: Failed to get boot capability\n", fcn));
1555ae115bc7Smrj 		return (ret);
1556eb2bd662Svikram 	}
1557ae115bc7Smrj 
15587c478bd9Sstevel@tonic-gate 	/*
15597c478bd9Sstevel@tonic-gate 	 * Check archive not supported with update_all
15607c478bd9Sstevel@tonic-gate 	 * since it is awkward to display out-of-sync
15617c478bd9Sstevel@tonic-gate 	 * information for each BE.
15627c478bd9Sstevel@tonic-gate 	 */
15637c478bd9Sstevel@tonic-gate 	if (bam_check && strcmp(subcmd, "update_all") == 0) {
1564*f64ca102SToomas Soome 		bam_error(_("the check option is not supported with "
1565*f64ca102SToomas Soome 		    "subcmd: %s\n"), subcmd);
15667c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15677c478bd9Sstevel@tonic-gate 	}
15687c478bd9Sstevel@tonic-gate 
1569b610f78eSvikram 	if (strcmp(subcmd, "update_all") == 0)
1570b610f78eSvikram 		bam_update_all = 1;
1571b610f78eSvikram 
157279c28b70SToomas Soome #if !defined(_OBP)
15732449e17fSsherrym 	ucode_install(bam_root);
15742449e17fSsherrym #endif
15752449e17fSsherrym 
1576b610f78eSvikram 	ret = f(bam_root, opt);
1577b610f78eSvikram 
1578b610f78eSvikram 	bam_update_all = 0;
1579b610f78eSvikram 
1580b610f78eSvikram 	return (ret);
15817c478bd9Sstevel@tonic-gate }
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1584ae115bc7Smrj void
15857c478bd9Sstevel@tonic-gate bam_error(char *format, ...)
15867c478bd9Sstevel@tonic-gate {
15877c478bd9Sstevel@tonic-gate 	va_list ap;
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	va_start(ap, format);
15907c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", prog);
15917c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
15927c478bd9Sstevel@tonic-gate 	va_end(ap);
15937c478bd9Sstevel@tonic-gate }
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1596eb2bd662Svikram void
1597eb2bd662Svikram bam_derror(char *format, ...)
1598eb2bd662Svikram {
1599eb2bd662Svikram 	va_list ap;
1600eb2bd662Svikram 
1601eb2bd662Svikram 	assert(bam_debug);
1602eb2bd662Svikram 
1603eb2bd662Svikram 	va_start(ap, format);
1604eb2bd662Svikram 	(void) fprintf(stderr, "DEBUG: ");
1605eb2bd662Svikram 	(void) vfprintf(stderr, format, ap);
1606eb2bd662Svikram 	va_end(ap);
1607eb2bd662Svikram }
1608eb2bd662Svikram 
1609eb2bd662Svikram /*PRINTFLIKE1*/
1610eb2bd662Svikram void
16117c478bd9Sstevel@tonic-gate bam_print(char *format, ...)
16127c478bd9Sstevel@tonic-gate {
16137c478bd9Sstevel@tonic-gate 	va_list ap;
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	va_start(ap, format);
16167c478bd9Sstevel@tonic-gate 	(void) vfprintf(stdout, format, ap);
16177c478bd9Sstevel@tonic-gate 	va_end(ap);
16187c478bd9Sstevel@tonic-gate }
16197c478bd9Sstevel@tonic-gate 
1620ae115bc7Smrj /*PRINTFLIKE1*/
1621ae115bc7Smrj void
1622ae115bc7Smrj bam_print_stderr(char *format, ...)
1623ae115bc7Smrj {
1624ae115bc7Smrj 	va_list ap;
1625ae115bc7Smrj 
1626ae115bc7Smrj 	va_start(ap, format);
1627ae115bc7Smrj 	(void) vfprintf(stderr, format, ap);
1628ae115bc7Smrj 	va_end(ap);
1629ae115bc7Smrj }
1630ae115bc7Smrj 
163166b6aef6SWilliam Kucharski void
16327c478bd9Sstevel@tonic-gate bam_exit(int excode)
16337c478bd9Sstevel@tonic-gate {
1634cedc7e57SEnrico Perla - Sun Microsystems 	restore_env();
16357c478bd9Sstevel@tonic-gate 	bam_unlock();
16367c478bd9Sstevel@tonic-gate 	exit(excode);
16377c478bd9Sstevel@tonic-gate }
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate static void
16407c478bd9Sstevel@tonic-gate bam_lock(void)
16417c478bd9Sstevel@tonic-gate {
16427c478bd9Sstevel@tonic-gate 	struct flock lock;
16437c478bd9Sstevel@tonic-gate 	pid_t pid;
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS);
16467c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
16477c478bd9Sstevel@tonic-gate 		/*
16487c478bd9Sstevel@tonic-gate 		 * We may be invoked early in boot for archive verification.
16497c478bd9Sstevel@tonic-gate 		 * In this case, root is readonly and /var/run may not exist.
16507c478bd9Sstevel@tonic-gate 		 * Proceed without the lock
16517c478bd9Sstevel@tonic-gate 		 */
16527c478bd9Sstevel@tonic-gate 		if (errno == EROFS || errno == ENOENT) {
16537c478bd9Sstevel@tonic-gate 			bam_root_readonly = 1;
16547c478bd9Sstevel@tonic-gate 			return;
16557c478bd9Sstevel@tonic-gate 		}
16567c478bd9Sstevel@tonic-gate 
1657*f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"),
1658*f64ca102SToomas Soome 		    BAM_LOCK_FILE, strerror(errno));
16597c478bd9Sstevel@tonic-gate 		bam_exit(1);
16607c478bd9Sstevel@tonic-gate 	}
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
16637c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
16647c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
16657c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) {
16687c478bd9Sstevel@tonic-gate 		if (errno != EACCES && errno != EAGAIN) {
1669*f64ca102SToomas Soome 			bam_error(_("failed to lock file: %s: %s\n"),
1670*f64ca102SToomas Soome 			    BAM_LOCK_FILE, strerror(errno));
16717c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
16727c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
16737c478bd9Sstevel@tonic-gate 			bam_exit(1);
16747c478bd9Sstevel@tonic-gate 		}
16757c478bd9Sstevel@tonic-gate 		pid = 0;
16767c478bd9Sstevel@tonic-gate 		(void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0);
1677*f64ca102SToomas Soome 		bam_print(
1678*f64ca102SToomas Soome 		    _("another instance of bootadm (pid %lu) is running\n"),
1679*f64ca102SToomas Soome 		    pid);
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 		lock.l_type = F_WRLCK;
16827c478bd9Sstevel@tonic-gate 		lock.l_whence = SEEK_SET;
16837c478bd9Sstevel@tonic-gate 		lock.l_start = 0;
16847c478bd9Sstevel@tonic-gate 		lock.l_len = 0;
16857c478bd9Sstevel@tonic-gate 		if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) {
1686*f64ca102SToomas Soome 			bam_error(_("failed to lock file: %s: %s\n"),
1687*f64ca102SToomas Soome 			    BAM_LOCK_FILE, strerror(errno));
16887c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
16897c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
16907c478bd9Sstevel@tonic-gate 			bam_exit(1);
16917c478bd9Sstevel@tonic-gate 		}
16927c478bd9Sstevel@tonic-gate 	}
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	/* We own the lock now */
16957c478bd9Sstevel@tonic-gate 	pid = getpid();
16967c478bd9Sstevel@tonic-gate 	(void) write(bam_lock_fd, &pid, sizeof (pid));
16977c478bd9Sstevel@tonic-gate }
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate static void
17007c478bd9Sstevel@tonic-gate bam_unlock(void)
17017c478bd9Sstevel@tonic-gate {
17027c478bd9Sstevel@tonic-gate 	struct flock unlock;
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 	/*
17057c478bd9Sstevel@tonic-gate 	 * NOP if we don't hold the lock
17067c478bd9Sstevel@tonic-gate 	 */
17077c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
17087c478bd9Sstevel@tonic-gate 		return;
17097c478bd9Sstevel@tonic-gate 	}
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
17127c478bd9Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
17137c478bd9Sstevel@tonic-gate 	unlock.l_start = 0;
17147c478bd9Sstevel@tonic-gate 	unlock.l_len = 0;
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) {
1717*f64ca102SToomas Soome 		bam_error(_("failed to unlock file: %s: %s\n"),
1718*f64ca102SToomas Soome 		    BAM_LOCK_FILE, strerror(errno));
17197c478bd9Sstevel@tonic-gate 	}
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 	if (close(bam_lock_fd) == -1) {
1722*f64ca102SToomas Soome 		bam_error(_("failed to close file: %s: %s\n"),
1723*f64ca102SToomas Soome 		    BAM_LOCK_FILE, strerror(errno));
17247c478bd9Sstevel@tonic-gate 	}
17257c478bd9Sstevel@tonic-gate 	bam_lock_fd = -1;
17267c478bd9Sstevel@tonic-gate }
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate static error_t
17297c478bd9Sstevel@tonic-gate list_archive(char *root, char *opt)
17307c478bd9Sstevel@tonic-gate {
17317c478bd9Sstevel@tonic-gate 	filelist_t flist;
17327c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
17337c478bd9Sstevel@tonic-gate 	line_t *lp;
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	assert(root);
17367c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
17397c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
17407c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
17417c478bd9Sstevel@tonic-gate 	}
17427c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
1745*f64ca102SToomas Soome 		bam_print(_("%s\n"), lp->line);
17467c478bd9Sstevel@tonic-gate 	}
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
17517c478bd9Sstevel@tonic-gate }
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate /*
17547c478bd9Sstevel@tonic-gate  * This routine writes a list of lines to a file.
17557c478bd9Sstevel@tonic-gate  * The list is *not* freed
17567c478bd9Sstevel@tonic-gate  */
17577c478bd9Sstevel@tonic-gate static error_t
17587c478bd9Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start)
17597c478bd9Sstevel@tonic-gate {
17607c478bd9Sstevel@tonic-gate 	char		tmpfile[PATH_MAX];
17617c478bd9Sstevel@tonic-gate 	char		path[PATH_MAX];
17627c478bd9Sstevel@tonic-gate 	FILE		*fp;
17637c478bd9Sstevel@tonic-gate 	int		ret;
17647c478bd9Sstevel@tonic-gate 	struct stat	sb;
17657c478bd9Sstevel@tonic-gate 	mode_t		mode;
17667c478bd9Sstevel@tonic-gate 	uid_t		root_uid;
17677c478bd9Sstevel@tonic-gate 	gid_t		sys_gid;
17687c478bd9Sstevel@tonic-gate 	struct passwd	*pw;
17697c478bd9Sstevel@tonic-gate 	struct group	*gp;
1770eb2bd662Svikram 	const char	*fcn = "list2file()";
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, final);
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 	if (start == NULL) {
1775eb2bd662Svikram 		/* Empty GRUB menu */
17767c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != -1) {
1777*f64ca102SToomas Soome 			bam_print(_("file is empty, deleting file: %s\n"),
1778*f64ca102SToomas Soome 			    path);
17797c478bd9Sstevel@tonic-gate 			if (unlink(path) != 0) {
1780*f64ca102SToomas Soome 				bam_error(_("failed to unlink file: %s: %s\n"),
1781*f64ca102SToomas Soome 				    path, strerror(errno));
17827c478bd9Sstevel@tonic-gate 				return (BAM_ERROR);
17837c478bd9Sstevel@tonic-gate 			} else {
17847c478bd9Sstevel@tonic-gate 				return (BAM_SUCCESS);
17857c478bd9Sstevel@tonic-gate 			}
17867c478bd9Sstevel@tonic-gate 		}
1787eb2bd662Svikram 		return (BAM_SUCCESS);
17887c478bd9Sstevel@tonic-gate 	}
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 	/*
17917c478bd9Sstevel@tonic-gate 	 * Preserve attributes of existing file if possible,
17927c478bd9Sstevel@tonic-gate 	 * otherwise ask the system for uid/gid of root/sys.
17937c478bd9Sstevel@tonic-gate 	 * If all fails, fall back on hard-coded defaults.
17947c478bd9Sstevel@tonic-gate 	 */
17957c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != -1) {
17967c478bd9Sstevel@tonic-gate 		mode = sb.st_mode;
17977c478bd9Sstevel@tonic-gate 		root_uid = sb.st_uid;
17987c478bd9Sstevel@tonic-gate 		sys_gid = sb.st_gid;
17997c478bd9Sstevel@tonic-gate 	} else {
18007c478bd9Sstevel@tonic-gate 		mode = DEFAULT_DEV_MODE;
18017c478bd9Sstevel@tonic-gate 		if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
18027c478bd9Sstevel@tonic-gate 			root_uid = pw->pw_uid;
18037c478bd9Sstevel@tonic-gate 		} else {
1804*f64ca102SToomas Soome 			bam_error(_("getpwnam: uid for %s failed, "
1805*f64ca102SToomas Soome 			    "defaulting to %d\n"),
18067c478bd9Sstevel@tonic-gate 			    DEFAULT_DEV_USER, DEFAULT_DEV_UID);
18077c478bd9Sstevel@tonic-gate 			root_uid = (uid_t)DEFAULT_DEV_UID;
18087c478bd9Sstevel@tonic-gate 		}
18097c478bd9Sstevel@tonic-gate 		if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
18107c478bd9Sstevel@tonic-gate 			sys_gid = gp->gr_gid;
18117c478bd9Sstevel@tonic-gate 		} else {
1812*f64ca102SToomas Soome 			bam_error(_("getgrnam: gid for %s failed, "
1813*f64ca102SToomas Soome 			    "defaulting to %d\n"),
18147c478bd9Sstevel@tonic-gate 			    DEFAULT_DEV_GROUP, DEFAULT_DEV_GID);
18157c478bd9Sstevel@tonic-gate 			sys_gid = (gid_t)DEFAULT_DEV_GID;
18167c478bd9Sstevel@tonic-gate 		}
18177c478bd9Sstevel@tonic-gate 	}
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	(void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp);
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate 	/* Truncate tmpfile first */
18227c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "w");
18237c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
1824*f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), tmpfile,
1825*f64ca102SToomas Soome 		    strerror(errno));
18267c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18277c478bd9Sstevel@tonic-gate 	}
1828963390b4Svikram 	ret = fclose(fp);
1829963390b4Svikram 	INJECT_ERROR1("LIST2FILE_TRUNC_FCLOSE", ret = EOF);
1830963390b4Svikram 	if (ret == EOF) {
1831*f64ca102SToomas Soome 		bam_error(_("failed to close file: %s: %s\n"),
1832*f64ca102SToomas Soome 		    tmpfile, strerror(errno));
18337c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18347c478bd9Sstevel@tonic-gate 	}
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate 	/* Now open it in append mode */
18377c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "a");
18387c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
1839*f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), tmpfile,
1840*f64ca102SToomas Soome 		    strerror(errno));
18417c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18427c478bd9Sstevel@tonic-gate 	}
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	for (; start; start = start->next) {
1845963390b4Svikram 		ret = s_fputs(start->line, fp);
1846963390b4Svikram 		INJECT_ERROR1("LIST2FILE_FPUTS", ret = EOF);
1847963390b4Svikram 		if (ret == EOF) {
1848*f64ca102SToomas Soome 			bam_error(_("write to file failed: %s: %s\n"),
1849*f64ca102SToomas Soome 			    tmpfile, strerror(errno));
18507c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
18517c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
18527c478bd9Sstevel@tonic-gate 		}
18537c478bd9Sstevel@tonic-gate 	}
18547c478bd9Sstevel@tonic-gate 
1855963390b4Svikram 	ret = fclose(fp);
1856963390b4Svikram 	INJECT_ERROR1("LIST2FILE_APPEND_FCLOSE", ret = EOF);
1857963390b4Svikram 	if (ret == EOF) {
1858*f64ca102SToomas Soome 		bam_error(_("failed to close file: %s: %s\n"),
1859*f64ca102SToomas Soome 		    tmpfile, strerror(errno));
18607c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18617c478bd9Sstevel@tonic-gate 	}
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate 	/*
1864de531be4Sjg 	 * Set up desired attributes.  Ignore failures on filesystems
1865de531be4Sjg 	 * not supporting these operations - pcfs reports unsupported
1866de531be4Sjg 	 * operations as EINVAL.
18677c478bd9Sstevel@tonic-gate 	 */
18687c478bd9Sstevel@tonic-gate 	ret = chmod(tmpfile, mode);
1869de531be4Sjg 	if (ret == -1 &&
1870de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
1871*f64ca102SToomas Soome 		bam_error(_("chmod operation on %s failed - %s\n"),
1872*f64ca102SToomas Soome 		    tmpfile, strerror(errno));
18737c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18747c478bd9Sstevel@tonic-gate 	}
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	ret = chown(tmpfile, root_uid, sys_gid);
1877de531be4Sjg 	if (ret == -1 &&
1878de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
1879*f64ca102SToomas Soome 		bam_error(_("chgrp operation on %s failed - %s\n"),
1880*f64ca102SToomas Soome 		    tmpfile, strerror(errno));
18817c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18827c478bd9Sstevel@tonic-gate 	}
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 	/*
18857c478bd9Sstevel@tonic-gate 	 * Do an atomic rename
18867c478bd9Sstevel@tonic-gate 	 */
18877c478bd9Sstevel@tonic-gate 	ret = rename(tmpfile, path);
1888963390b4Svikram 	INJECT_ERROR1("LIST2FILE_RENAME", ret = -1);
18897c478bd9Sstevel@tonic-gate 	if (ret != 0) {
1890*f64ca102SToomas Soome 		bam_error(_("rename to file failed: %s: %s\n"), path,
1891*f64ca102SToomas Soome 		    strerror(errno));
18927c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18937c478bd9Sstevel@tonic-gate 	}
18947c478bd9Sstevel@tonic-gate 
1895*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: wrote file successfully: %s\n", fcn, path));
18967c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
18977c478bd9Sstevel@tonic-gate }
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate /*
190048847494SEnrico Perla - Sun Microsystems  * Checks if the path specified (without the file name at the end) exists
190148847494SEnrico Perla - Sun Microsystems  * and creates it if not. If the path exists and is not a directory, an attempt
190248847494SEnrico Perla - Sun Microsystems  * to unlink is made.
19037c478bd9Sstevel@tonic-gate  */
190448847494SEnrico Perla - Sun Microsystems static int
190548847494SEnrico Perla - Sun Microsystems setup_path(char *path)
190648847494SEnrico Perla - Sun Microsystems {
190748847494SEnrico Perla - Sun Microsystems 	char 		*p;
190848847494SEnrico Perla - Sun Microsystems 	int		ret;
190948847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
191048847494SEnrico Perla - Sun Microsystems 
191148847494SEnrico Perla - Sun Microsystems 	p = strrchr(path, '/');
191248847494SEnrico Perla - Sun Microsystems 	if (p != NULL) {
191348847494SEnrico Perla - Sun Microsystems 		*p = '\0';
191448847494SEnrico Perla - Sun Microsystems 		if (stat(path, &sb) != 0 || !(S_ISDIR(sb.st_mode))) {
191548847494SEnrico Perla - Sun Microsystems 			/* best effort attempt, mkdirp will catch the error */
191648847494SEnrico Perla - Sun Microsystems 			(void) unlink(path);
191748847494SEnrico Perla - Sun Microsystems 			if (bam_verbose)
1918*f64ca102SToomas Soome 				bam_print(_("need to create directory "
1919*f64ca102SToomas Soome 				    "path for %s\n"), path);
192048847494SEnrico Perla - Sun Microsystems 			ret = mkdirp(path, DIR_PERMS);
192148847494SEnrico Perla - Sun Microsystems 			if (ret == -1) {
1922*f64ca102SToomas Soome 				bam_error(_("mkdir of %s failed: %s\n"),
1923*f64ca102SToomas Soome 				    path, strerror(errno));
192448847494SEnrico Perla - Sun Microsystems 				*p = '/';
192548847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
192648847494SEnrico Perla - Sun Microsystems 			}
192748847494SEnrico Perla - Sun Microsystems 		}
192848847494SEnrico Perla - Sun Microsystems 		*p = '/';
192948847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
193048847494SEnrico Perla - Sun Microsystems 	}
193148847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
193248847494SEnrico Perla - Sun Microsystems }
193348847494SEnrico Perla - Sun Microsystems 
193448847494SEnrico Perla - Sun Microsystems typedef union {
193548847494SEnrico Perla - Sun Microsystems 	gzFile	gzfile;
193648847494SEnrico Perla - Sun Microsystems 	int	fdfile;
193748847494SEnrico Perla - Sun Microsystems } outfile;
193848847494SEnrico Perla - Sun Microsystems 
193948847494SEnrico Perla - Sun Microsystems typedef struct {
194048847494SEnrico Perla - Sun Microsystems 	char		path[PATH_MAX];
194148847494SEnrico Perla - Sun Microsystems 	outfile		out;
194248847494SEnrico Perla - Sun Microsystems } cachefile;
194348847494SEnrico Perla - Sun Microsystems 
194448847494SEnrico Perla - Sun Microsystems static int
194548847494SEnrico Perla - Sun Microsystems setup_file(char *base, const char *path, cachefile *cf)
194648847494SEnrico Perla - Sun Microsystems {
194748847494SEnrico Perla - Sun Microsystems 	int	ret;
194848847494SEnrico Perla - Sun Microsystems 	char	*strip;
194948847494SEnrico Perla - Sun Microsystems 
195048847494SEnrico Perla - Sun Microsystems 	/* init gzfile or fdfile in case we fail before opening */
195148847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT)
195248847494SEnrico Perla - Sun Microsystems 		cf->out.gzfile = NULL;
195348847494SEnrico Perla - Sun Microsystems 	else
195448847494SEnrico Perla - Sun Microsystems 		cf->out.fdfile = -1;
195548847494SEnrico Perla - Sun Microsystems 
195648847494SEnrico Perla - Sun Microsystems 	/* strip the trailing altroot path */
195748847494SEnrico Perla - Sun Microsystems 	strip = (char *)path + strlen(rootbuf);
195848847494SEnrico Perla - Sun Microsystems 
195948847494SEnrico Perla - Sun Microsystems 	ret = snprintf(cf->path, sizeof (cf->path), "%s/%s", base, strip);
196048847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (cf->path)) {
1961*f64ca102SToomas Soome 		bam_error(_("unable to create path on mountpoint %s, "
1962*f64ca102SToomas Soome 		    "path too long\n"), rootbuf);
196348847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
196448847494SEnrico Perla - Sun Microsystems 	}
196548847494SEnrico Perla - Sun Microsystems 
196648847494SEnrico Perla - Sun Microsystems 	/* Check if path is present in the archive cache directory */
196748847494SEnrico Perla - Sun Microsystems 	if (setup_path(cf->path) == BAM_ERROR)
196848847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
196948847494SEnrico Perla - Sun Microsystems 
197048847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT) {
197148847494SEnrico Perla - Sun Microsystems 		if ((cf->out.gzfile = gzopen(cf->path, "wb")) == NULL) {
1972*f64ca102SToomas Soome 			bam_error(_("failed to open file: %s: %s\n"),
1973*f64ca102SToomas Soome 			    cf->path, strerror(errno));
197448847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
197548847494SEnrico Perla - Sun Microsystems 		}
197648847494SEnrico Perla - Sun Microsystems 		(void) gzsetparams(cf->out.gzfile, Z_BEST_SPEED,
197748847494SEnrico Perla - Sun Microsystems 		    Z_DEFAULT_STRATEGY);
197848847494SEnrico Perla - Sun Microsystems 	} else {
197948847494SEnrico Perla - Sun Microsystems 		if ((cf->out.fdfile = open(cf->path, O_WRONLY | O_CREAT, 0644))
198048847494SEnrico Perla - Sun Microsystems 		    == -1) {
1981*f64ca102SToomas Soome 			bam_error(_("failed to open file: %s: %s\n"),
1982*f64ca102SToomas Soome 			    cf->path, strerror(errno));
198348847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
198448847494SEnrico Perla - Sun Microsystems 		}
198548847494SEnrico Perla - Sun Microsystems 	}
198648847494SEnrico Perla - Sun Microsystems 
198748847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
198848847494SEnrico Perla - Sun Microsystems }
198948847494SEnrico Perla - Sun Microsystems 
199048847494SEnrico Perla - Sun Microsystems static int
199148847494SEnrico Perla - Sun Microsystems cache_write(cachefile cf, char *buf, int size)
199248847494SEnrico Perla - Sun Microsystems {
199348847494SEnrico Perla - Sun Microsystems 	int	err;
199448847494SEnrico Perla - Sun Microsystems 
199548847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT) {
199648847494SEnrico Perla - Sun Microsystems 		if (gzwrite(cf.out.gzfile, buf, size) < 1) {
1997*f64ca102SToomas Soome 			bam_error(_("failed to write to %s\n"),
1998*f64ca102SToomas Soome 			    gzerror(cf.out.gzfile, &err));
1999cedc7e57SEnrico Perla - Sun Microsystems 			if (err == Z_ERRNO && bam_verbose) {
2000*f64ca102SToomas Soome 				bam_error(_("write to file failed: %s: %s\n"),
2001*f64ca102SToomas Soome 				    cf.path, strerror(errno));
2002cedc7e57SEnrico Perla - Sun Microsystems 			}
200348847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
200448847494SEnrico Perla - Sun Microsystems 		}
200548847494SEnrico Perla - Sun Microsystems 	} else {
200648847494SEnrico Perla - Sun Microsystems 		if (write(cf.out.fdfile, buf, size) < 1) {
2007*f64ca102SToomas Soome 			bam_error(_("write to file failed: %s: %s\n"),
2008*f64ca102SToomas Soome 			    cf.path, strerror(errno));
200948847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
201048847494SEnrico Perla - Sun Microsystems 		}
201148847494SEnrico Perla - Sun Microsystems 	}
201248847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
201348847494SEnrico Perla - Sun Microsystems }
201448847494SEnrico Perla - Sun Microsystems 
201548847494SEnrico Perla - Sun Microsystems static int
201648847494SEnrico Perla - Sun Microsystems cache_close(cachefile cf)
201748847494SEnrico Perla - Sun Microsystems {
201848847494SEnrico Perla - Sun Microsystems 	int	ret;
201948847494SEnrico Perla - Sun Microsystems 
202048847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT) {
202148847494SEnrico Perla - Sun Microsystems 		if (cf.out.gzfile) {
202248847494SEnrico Perla - Sun Microsystems 			ret = gzclose(cf.out.gzfile);
2023cedc7e57SEnrico Perla - Sun Microsystems 			if (ret != Z_OK) {
2024*f64ca102SToomas Soome 				bam_error(_("failed to close file: %s: %s\n"),
2025*f64ca102SToomas Soome 				    cf.path, strerror(errno));
202648847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
202748847494SEnrico Perla - Sun Microsystems 			}
2028cedc7e57SEnrico Perla - Sun Microsystems 		}
202948847494SEnrico Perla - Sun Microsystems 	} else {
203048847494SEnrico Perla - Sun Microsystems 		if (cf.out.fdfile != -1) {
203148847494SEnrico Perla - Sun Microsystems 			ret = close(cf.out.fdfile);
203248847494SEnrico Perla - Sun Microsystems 			if (ret != 0) {
2033*f64ca102SToomas Soome 				bam_error(_("failed to close file: %s: %s\n"),
2034*f64ca102SToomas Soome 				    cf.path, strerror(errno));
203548847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
203648847494SEnrico Perla - Sun Microsystems 			}
203748847494SEnrico Perla - Sun Microsystems 		}
203848847494SEnrico Perla - Sun Microsystems 	}
203948847494SEnrico Perla - Sun Microsystems 
204048847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
204148847494SEnrico Perla - Sun Microsystems }
204248847494SEnrico Perla - Sun Microsystems 
204348847494SEnrico Perla - Sun Microsystems static int
204448847494SEnrico Perla - Sun Microsystems dircache_updatefile(const char *path, int what)
204548847494SEnrico Perla - Sun Microsystems {
204648847494SEnrico Perla - Sun Microsystems 	int 		ret, exitcode;
204748847494SEnrico Perla - Sun Microsystems 	char 		buf[4096 * 4];
204848847494SEnrico Perla - Sun Microsystems 	FILE 		*infile;
204948847494SEnrico Perla - Sun Microsystems 	cachefile 	outfile, outupdt;
205048847494SEnrico Perla - Sun Microsystems 
2051cedc7e57SEnrico Perla - Sun Microsystems 	if (bam_nowrite()) {
2052cedc7e57SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NEED_UPDATE);
2053cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
2054cedc7e57SEnrico Perla - Sun Microsystems 	}
2055cedc7e57SEnrico Perla - Sun Microsystems 
2056cedc7e57SEnrico Perla - Sun Microsystems 	if (!has_cachedir(what))
205748847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
205848847494SEnrico Perla - Sun Microsystems 
205948847494SEnrico Perla - Sun Microsystems 	if ((infile = fopen(path, "rb")) == NULL) {
2060*f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), path,
2061*f64ca102SToomas Soome 		    strerror(errno));
206248847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
206348847494SEnrico Perla - Sun Microsystems 	}
206448847494SEnrico Perla - Sun Microsystems 
206548847494SEnrico Perla - Sun Microsystems 	ret = setup_file(get_cachedir(what), path, &outfile);
206648847494SEnrico Perla - Sun Microsystems 	if (ret == BAM_ERROR) {
206748847494SEnrico Perla - Sun Microsystems 		exitcode = BAM_ERROR;
206848847494SEnrico Perla - Sun Microsystems 		goto out;
206948847494SEnrico Perla - Sun Microsystems 	}
207048847494SEnrico Perla - Sun Microsystems 	if (!is_dir_flag_on(what, NO_MULTI)) {
207148847494SEnrico Perla - Sun Microsystems 		ret = setup_file(get_updatedir(what), path, &outupdt);
207248847494SEnrico Perla - Sun Microsystems 		if (ret == BAM_ERROR)
207348847494SEnrico Perla - Sun Microsystems 			set_dir_flag(what, NO_MULTI);
207448847494SEnrico Perla - Sun Microsystems 	}
207548847494SEnrico Perla - Sun Microsystems 
207648847494SEnrico Perla - Sun Microsystems 	while ((ret = fread(buf, 1, sizeof (buf), infile)) > 0) {
207748847494SEnrico Perla - Sun Microsystems 		if (cache_write(outfile, buf, ret) == BAM_ERROR) {
207848847494SEnrico Perla - Sun Microsystems 			exitcode = BAM_ERROR;
207948847494SEnrico Perla - Sun Microsystems 			goto out;
208048847494SEnrico Perla - Sun Microsystems 		}
208148847494SEnrico Perla - Sun Microsystems 		if (!is_dir_flag_on(what, NO_MULTI))
208248847494SEnrico Perla - Sun Microsystems 			if (cache_write(outupdt, buf, ret) == BAM_ERROR)
208348847494SEnrico Perla - Sun Microsystems 				set_dir_flag(what, NO_MULTI);
208448847494SEnrico Perla - Sun Microsystems 	}
208548847494SEnrico Perla - Sun Microsystems 
208648847494SEnrico Perla - Sun Microsystems 	set_dir_flag(what, NEED_UPDATE);
208748847494SEnrico Perla - Sun Microsystems 	get_count(what)++;
2088cedc7e57SEnrico Perla - Sun Microsystems 	if (get_count(what) > COUNT_MAX)
2089cedc7e57SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
209048847494SEnrico Perla - Sun Microsystems 	exitcode = BAM_SUCCESS;
209148847494SEnrico Perla - Sun Microsystems out:
209248847494SEnrico Perla - Sun Microsystems 	(void) fclose(infile);
209348847494SEnrico Perla - Sun Microsystems 	if (cache_close(outfile) == BAM_ERROR)
209448847494SEnrico Perla - Sun Microsystems 		exitcode = BAM_ERROR;
209548847494SEnrico Perla - Sun Microsystems 	if (!is_dir_flag_on(what, NO_MULTI) &&
209648847494SEnrico Perla - Sun Microsystems 	    cache_close(outupdt) == BAM_ERROR)
209748847494SEnrico Perla - Sun Microsystems 		exitcode = BAM_ERROR;
209848847494SEnrico Perla - Sun Microsystems 	if (exitcode == BAM_ERROR)
209948847494SEnrico Perla - Sun Microsystems 		set_flag(UPDATE_ERROR);
210048847494SEnrico Perla - Sun Microsystems 	return (exitcode);
210148847494SEnrico Perla - Sun Microsystems }
210248847494SEnrico Perla - Sun Microsystems 
210348847494SEnrico Perla - Sun Microsystems static int
210448847494SEnrico Perla - Sun Microsystems dircache_updatedir(const char *path, int what, int updt)
210548847494SEnrico Perla - Sun Microsystems {
210648847494SEnrico Perla - Sun Microsystems 	int		ret;
210748847494SEnrico Perla - Sun Microsystems 	char		dpath[PATH_MAX];
210848847494SEnrico Perla - Sun Microsystems 	char		*strip;
210948847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
211048847494SEnrico Perla - Sun Microsystems 
211148847494SEnrico Perla - Sun Microsystems 	strip = (char *)path + strlen(rootbuf);
211248847494SEnrico Perla - Sun Microsystems 
211348847494SEnrico Perla - Sun Microsystems 	ret = snprintf(dpath, sizeof (dpath), "%s/%s", updt ?
211448847494SEnrico Perla - Sun Microsystems 	    get_updatedir(what) : get_cachedir(what), strip);
211548847494SEnrico Perla - Sun Microsystems 
211648847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (dpath)) {
2117*f64ca102SToomas Soome 		bam_error(_("unable to create path on mountpoint %s, "
2118*f64ca102SToomas Soome 		    "path too long\n"), rootbuf);
211948847494SEnrico Perla - Sun Microsystems 		set_flag(UPDATE_ERROR);
212048847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
212148847494SEnrico Perla - Sun Microsystems 	}
212248847494SEnrico Perla - Sun Microsystems 
212348847494SEnrico Perla - Sun Microsystems 	if (stat(dpath, &sb) == 0 && S_ISDIR(sb.st_mode))
212448847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
212548847494SEnrico Perla - Sun Microsystems 
212648847494SEnrico Perla - Sun Microsystems 	if (updt) {
212748847494SEnrico Perla - Sun Microsystems 		if (!is_dir_flag_on(what, NO_MULTI))
212848847494SEnrico Perla - Sun Microsystems 			if (!bam_nowrite() && mkdirp(dpath, DIR_PERMS) == -1)
212948847494SEnrico Perla - Sun Microsystems 				set_dir_flag(what, NO_MULTI);
213048847494SEnrico Perla - Sun Microsystems 	} else {
213148847494SEnrico Perla - Sun Microsystems 		if (!bam_nowrite() && mkdirp(dpath, DIR_PERMS) == -1) {
213248847494SEnrico Perla - Sun Microsystems 			set_flag(UPDATE_ERROR);
213348847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
213448847494SEnrico Perla - Sun Microsystems 		}
213548847494SEnrico Perla - Sun Microsystems 	}
213648847494SEnrico Perla - Sun Microsystems 
213748847494SEnrico Perla - Sun Microsystems 	set_dir_flag(what, NEED_UPDATE);
213848847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
213948847494SEnrico Perla - Sun Microsystems }
214048847494SEnrico Perla - Sun Microsystems 
214148847494SEnrico Perla - Sun Microsystems #define	DO_CACHE_DIR	0
214248847494SEnrico Perla - Sun Microsystems #define	DO_UPDATE_DIR	1
214348847494SEnrico Perla - Sun Microsystems 
214448847494SEnrico Perla - Sun Microsystems #if defined(_LP64) || defined(_LONGLONG_TYPE)
214548847494SEnrico Perla - Sun Microsystems typedef		Elf64_Ehdr	_elfhdr;
214648847494SEnrico Perla - Sun Microsystems #else
214748847494SEnrico Perla - Sun Microsystems typedef		Elf32_Ehdr	_elfhdr;
214848847494SEnrico Perla - Sun Microsystems #endif
214948847494SEnrico Perla - Sun Microsystems 
215048847494SEnrico Perla - Sun Microsystems /*
215148847494SEnrico Perla - Sun Microsystems  * This routine updates the contents of the cache directory
215248847494SEnrico Perla - Sun Microsystems  */
215348847494SEnrico Perla - Sun Microsystems static int
215448847494SEnrico Perla - Sun Microsystems update_dircache(const char *path, int flags)
215548847494SEnrico Perla - Sun Microsystems {
215648847494SEnrico Perla - Sun Microsystems 	int rc = BAM_SUCCESS;
215748847494SEnrico Perla - Sun Microsystems 
215848847494SEnrico Perla - Sun Microsystems 	switch (flags) {
215948847494SEnrico Perla - Sun Microsystems 	case FTW_F:
216048847494SEnrico Perla - Sun Microsystems 		{
216148847494SEnrico Perla - Sun Microsystems 		int	fd;
216248847494SEnrico Perla - Sun Microsystems 		_elfhdr	elf;
216348847494SEnrico Perla - Sun Microsystems 
216448847494SEnrico Perla - Sun Microsystems 		if ((fd = open(path, O_RDONLY)) < 0) {
2165*f64ca102SToomas Soome 			bam_error(_("failed to open file: %s: %s\n"),
2166*f64ca102SToomas Soome 			    path, strerror(errno));
216748847494SEnrico Perla - Sun Microsystems 			set_flag(UPDATE_ERROR);
216848847494SEnrico Perla - Sun Microsystems 			rc = BAM_ERROR;
216948847494SEnrico Perla - Sun Microsystems 			break;
217048847494SEnrico Perla - Sun Microsystems 		}
217148847494SEnrico Perla - Sun Microsystems 
217248847494SEnrico Perla - Sun Microsystems 		/*
217348847494SEnrico Perla - Sun Microsystems 		 * libelf and gelf would be a cleaner and easier way to handle
217448847494SEnrico Perla - Sun Microsystems 		 * this, but libelf fails compilation if _ILP32 is defined &&
217548847494SEnrico Perla - Sun Microsystems 		 * _FILE_OFFSET_BITS is != 32 ...
217648847494SEnrico Perla - Sun Microsystems 		 */
217748847494SEnrico Perla - Sun Microsystems 		if (read(fd, (void *)&elf, sizeof (_elfhdr)) < 0) {
2178*f64ca102SToomas Soome 			bam_error(_("read failed for file: %s: %s\n"),
2179*f64ca102SToomas Soome 			    path, strerror(errno));
218048847494SEnrico Perla - Sun Microsystems 			set_flag(UPDATE_ERROR);
218148847494SEnrico Perla - Sun Microsystems 			(void) close(fd);
218248847494SEnrico Perla - Sun Microsystems 			rc = BAM_ERROR;
218348847494SEnrico Perla - Sun Microsystems 			break;
218448847494SEnrico Perla - Sun Microsystems 		}
218548847494SEnrico Perla - Sun Microsystems 		(void) close(fd);
218648847494SEnrico Perla - Sun Microsystems 
218748847494SEnrico Perla - Sun Microsystems 		/*
218848847494SEnrico Perla - Sun Microsystems 		 * If the file is not an executable and is not inside an amd64
218948847494SEnrico Perla - Sun Microsystems 		 * directory, we copy it in both the cache directories,
219048847494SEnrico Perla - Sun Microsystems 		 * otherwise, we only copy it inside the 64-bit one.
219148847494SEnrico Perla - Sun Microsystems 		 */
219248847494SEnrico Perla - Sun Microsystems 		if (memcmp(elf.e_ident, ELFMAG, 4) != 0) {
219348847494SEnrico Perla - Sun Microsystems 			if (strstr(path, "/amd64")) {
219448847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE64);
219548847494SEnrico Perla - Sun Microsystems 			} else {
219648847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE32);
2197cedc7e57SEnrico Perla - Sun Microsystems 				if (rc == BAM_SUCCESS)
2198cedc7e57SEnrico Perla - Sun Microsystems 					rc = dircache_updatefile(path, FILE64);
219948847494SEnrico Perla - Sun Microsystems 			}
220048847494SEnrico Perla - Sun Microsystems 		} else {
220148847494SEnrico Perla - Sun Microsystems 			/*
220248847494SEnrico Perla - Sun Microsystems 			 * Based on the ELF class we copy the file in the 32-bit
220348847494SEnrico Perla - Sun Microsystems 			 * or the 64-bit cache directory.
220448847494SEnrico Perla - Sun Microsystems 			 */
2205cedc7e57SEnrico Perla - Sun Microsystems 			if (elf.e_ident[EI_CLASS] == ELFCLASS32) {
220648847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE32);
2207cedc7e57SEnrico Perla - Sun Microsystems 			} else if (elf.e_ident[EI_CLASS] == ELFCLASS64) {
220848847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE64);
2209cedc7e57SEnrico Perla - Sun Microsystems 			} else {
2210*f64ca102SToomas Soome 				bam_print(_("WARNING: file %s is neither a "
2211*f64ca102SToomas Soome 				    "32-bit nor a 64-bit ELF\n"), path);
221248847494SEnrico Perla - Sun Microsystems 				/* paranoid */
221348847494SEnrico Perla - Sun Microsystems 				rc  = dircache_updatefile(path, FILE32);
221448847494SEnrico Perla - Sun Microsystems 				if (rc == BAM_SUCCESS)
221548847494SEnrico Perla - Sun Microsystems 					rc = dircache_updatefile(path, FILE64);
221648847494SEnrico Perla - Sun Microsystems 			}
221748847494SEnrico Perla - Sun Microsystems 		}
221848847494SEnrico Perla - Sun Microsystems 		break;
221948847494SEnrico Perla - Sun Microsystems 		}
222048847494SEnrico Perla - Sun Microsystems 	case FTW_D:
222148847494SEnrico Perla - Sun Microsystems 		if (strstr(path, "/amd64") == NULL) {
222248847494SEnrico Perla - Sun Microsystems 			rc = dircache_updatedir(path, FILE32, DO_UPDATE_DIR);
222348847494SEnrico Perla - Sun Microsystems 			if (rc == BAM_SUCCESS)
222448847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatedir(path, FILE32,
222548847494SEnrico Perla - Sun Microsystems 				    DO_CACHE_DIR);
222648847494SEnrico Perla - Sun Microsystems 		} else {
222748847494SEnrico Perla - Sun Microsystems 			if (has_cachedir(FILE64)) {
222848847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatedir(path, FILE64,
222948847494SEnrico Perla - Sun Microsystems 				    DO_UPDATE_DIR);
223048847494SEnrico Perla - Sun Microsystems 				if (rc == BAM_SUCCESS)
223148847494SEnrico Perla - Sun Microsystems 					rc = dircache_updatedir(path, FILE64,
223248847494SEnrico Perla - Sun Microsystems 					    DO_CACHE_DIR);
223348847494SEnrico Perla - Sun Microsystems 			}
223448847494SEnrico Perla - Sun Microsystems 		}
223548847494SEnrico Perla - Sun Microsystems 		break;
223648847494SEnrico Perla - Sun Microsystems 	default:
223748847494SEnrico Perla - Sun Microsystems 		rc = BAM_ERROR;
223848847494SEnrico Perla - Sun Microsystems 		break;
223948847494SEnrico Perla - Sun Microsystems 	}
224048847494SEnrico Perla - Sun Microsystems 
224148847494SEnrico Perla - Sun Microsystems 	return (rc);
224248847494SEnrico Perla - Sun Microsystems }
224348847494SEnrico Perla - Sun Microsystems 
22447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
22457c478bd9Sstevel@tonic-gate static int
22467c478bd9Sstevel@tonic-gate cmpstat(
22477c478bd9Sstevel@tonic-gate 	const char *file,
224848847494SEnrico Perla - Sun Microsystems 	const struct stat *st,
22497c478bd9Sstevel@tonic-gate 	int flags,
22507c478bd9Sstevel@tonic-gate 	struct FTW *ftw)
22517c478bd9Sstevel@tonic-gate {
22527c478bd9Sstevel@tonic-gate 	uint_t 		sz;
22537c478bd9Sstevel@tonic-gate 	uint64_t 	*value;
22547c478bd9Sstevel@tonic-gate 	uint64_t 	filestat[2];
225589c3ee43SGangadhar Mylapuram 	int 		error, ret, status;
22567c478bd9Sstevel@tonic-gate 
225758091fd8Ssetje 	struct safefile *safefilep;
225858091fd8Ssetje 	FILE 		*fp;
225948847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
226089c3ee43SGangadhar Mylapuram 	regex_t re;
226158091fd8Ssetje 
22627c478bd9Sstevel@tonic-gate 	/*
226348847494SEnrico Perla - Sun Microsystems 	 * On SPARC we create/update links too.
22647c478bd9Sstevel@tonic-gate 	 */
226548847494SEnrico Perla - Sun Microsystems 	if (flags != FTW_F && flags != FTW_D && (flags == FTW_SL &&
226648847494SEnrico Perla - Sun Microsystems 	    !is_flag_on(IS_SPARC_TARGET)))
226748847494SEnrico Perla - Sun Microsystems 		return (0);
226848847494SEnrico Perla - Sun Microsystems 
226948847494SEnrico Perla - Sun Microsystems 	/*
227048847494SEnrico Perla - Sun Microsystems 	 * Ignore broken links
227148847494SEnrico Perla - Sun Microsystems 	 */
227248847494SEnrico Perla - Sun Microsystems 	if (flags == FTW_SL && stat(file, &sb) < 0)
22737c478bd9Sstevel@tonic-gate 		return (0);
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 	/*
22767c478bd9Sstevel@tonic-gate 	 * new_nvlp may be NULL if there were errors earlier
22777c478bd9Sstevel@tonic-gate 	 * but this is not fatal to update determination.
22787c478bd9Sstevel@tonic-gate 	 */
22797c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp) {
228048847494SEnrico Perla - Sun Microsystems 		filestat[0] = st->st_size;
228148847494SEnrico Perla - Sun Microsystems 		filestat[1] = st->st_mtime;
22827c478bd9Sstevel@tonic-gate 		error = nvlist_add_uint64_array(walk_arg.new_nvlp,
22837c478bd9Sstevel@tonic-gate 		    file + bam_rootlen, filestat, 2);
22847c478bd9Sstevel@tonic-gate 		if (error)
2285*f64ca102SToomas Soome 			bam_error(_("failed to update stat data for: %s: %s\n"),
2286*f64ca102SToomas Soome 			    file, strerror(error));
22877c478bd9Sstevel@tonic-gate 	}
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 	/*
2290d876c67dSjg 	 * If we are invoked as part of system/filesystem/boot-archive, then
229158091fd8Ssetje 	 * there are a number of things we should not worry about
22927c478bd9Sstevel@tonic-gate 	 */
229358091fd8Ssetje 	if (bam_smf_check) {
229458091fd8Ssetje 		/* ignore amd64 modules unless we are booted amd64. */
229558091fd8Ssetje 		if (!is_amd64() && strstr(file, "/amd64/") != 0)
22967c478bd9Sstevel@tonic-gate 			return (0);
22977c478bd9Sstevel@tonic-gate 
229858091fd8Ssetje 		/* read in list of safe files */
2299110570ceSToomas Soome 		if (safefiles == NULL) {
2300110570ceSToomas Soome 			fp = fopen("/boot/solaris/filelist.safe", "r");
2301110570ceSToomas Soome 			if (fp != NULL) {
230258091fd8Ssetje 				safefiles = s_calloc(1,
230358091fd8Ssetje 				    sizeof (struct safefile));
230458091fd8Ssetje 				safefilep = safefiles;
230558091fd8Ssetje 				safefilep->name = s_calloc(1, MAXPATHLEN +
230658091fd8Ssetje 				    MAXNAMELEN);
230758091fd8Ssetje 				safefilep->next = NULL;
230858091fd8Ssetje 				while (s_fgets(safefilep->name, MAXPATHLEN +
230958091fd8Ssetje 				    MAXNAMELEN, fp) != NULL) {
231058091fd8Ssetje 					safefilep->next = s_calloc(1,
231158091fd8Ssetje 					    sizeof (struct safefile));
231258091fd8Ssetje 					safefilep = safefilep->next;
231358091fd8Ssetje 					safefilep->name = s_calloc(1,
231458091fd8Ssetje 					    MAXPATHLEN + MAXNAMELEN);
231558091fd8Ssetje 					safefilep->next = NULL;
231658091fd8Ssetje 				}
231758091fd8Ssetje 				(void) fclose(fp);
231858091fd8Ssetje 			}
231958091fd8Ssetje 		}
2320110570ceSToomas Soome 	}
232158091fd8Ssetje 
23227c478bd9Sstevel@tonic-gate 	/*
232348847494SEnrico Perla - Sun Microsystems 	 * On SPARC we create a -path-list file for mkisofs
232448847494SEnrico Perla - Sun Microsystems 	 */
232548847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET) && !bam_nowrite()) {
232648847494SEnrico Perla - Sun Microsystems 		if (flags != FTW_D) {
232748847494SEnrico Perla - Sun Microsystems 			char	*strip;
232848847494SEnrico Perla - Sun Microsystems 
232948847494SEnrico Perla - Sun Microsystems 			strip = (char *)file + strlen(rootbuf);
233048847494SEnrico Perla - Sun Microsystems 			(void) fprintf(walk_arg.sparcfile, "/%s=%s\n", strip,
233148847494SEnrico Perla - Sun Microsystems 			    file);
233248847494SEnrico Perla - Sun Microsystems 		}
233348847494SEnrico Perla - Sun Microsystems 	}
233448847494SEnrico Perla - Sun Microsystems 
233548847494SEnrico Perla - Sun Microsystems 	/*
233648847494SEnrico Perla - Sun Microsystems 	 * We are transitioning from the old model to the dircache or the cache
233748847494SEnrico Perla - Sun Microsystems 	 * directory was removed: create the entry without further checkings.
233848847494SEnrico Perla - Sun Microsystems 	 */
233948847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(NEED_CACHE_DIR)) {
234048847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
2341*f64ca102SToomas Soome 			bam_print(_("    new     %s\n"), file);
234248847494SEnrico Perla - Sun Microsystems 
234348847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(IS_SPARC_TARGET)) {
234448847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
234548847494SEnrico Perla - Sun Microsystems 			return (0);
234648847494SEnrico Perla - Sun Microsystems 		}
234748847494SEnrico Perla - Sun Microsystems 
234848847494SEnrico Perla - Sun Microsystems 		ret = update_dircache(file, flags);
234948847494SEnrico Perla - Sun Microsystems 		if (ret == BAM_ERROR) {
2350*f64ca102SToomas Soome 			bam_error(_("directory cache update failed for %s\n"),
2351*f64ca102SToomas Soome 			    file);
235248847494SEnrico Perla - Sun Microsystems 			return (-1);
235348847494SEnrico Perla - Sun Microsystems 		}
235448847494SEnrico Perla - Sun Microsystems 
235548847494SEnrico Perla - Sun Microsystems 		return (0);
235648847494SEnrico Perla - Sun Microsystems 	}
235748847494SEnrico Perla - Sun Microsystems 
235848847494SEnrico Perla - Sun Microsystems 	/*
23597c478bd9Sstevel@tonic-gate 	 * We need an update if file doesn't exist in old archive
23607c478bd9Sstevel@tonic-gate 	 */
23617c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp == NULL ||
23627c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint64_array(walk_arg.old_nvlp,
23637c478bd9Sstevel@tonic-gate 	    file + bam_rootlen, &value, &sz) != 0) {
23647c478bd9Sstevel@tonic-gate 		if (bam_smf_check)	/* ignore new during smf check */
23657c478bd9Sstevel@tonic-gate 			return (0);
236648847494SEnrico Perla - Sun Microsystems 
236748847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(IS_SPARC_TARGET)) {
236848847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
236948847494SEnrico Perla - Sun Microsystems 		} else {
237048847494SEnrico Perla - Sun Microsystems 			ret = update_dircache(file, flags);
237148847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_ERROR) {
2372*f64ca102SToomas Soome 				bam_error(_("directory cache update "
2373*f64ca102SToomas Soome 				    "failed for %s\n"), file);
237448847494SEnrico Perla - Sun Microsystems 				return (-1);
237548847494SEnrico Perla - Sun Microsystems 			}
237648847494SEnrico Perla - Sun Microsystems 		}
237748847494SEnrico Perla - Sun Microsystems 
23787c478bd9Sstevel@tonic-gate 		if (bam_verbose)
2379*f64ca102SToomas Soome 			bam_print(_("    new     %s\n"), file);
23807c478bd9Sstevel@tonic-gate 		return (0);
23817c478bd9Sstevel@tonic-gate 	}
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate 	/*
238448847494SEnrico Perla - Sun Microsystems 	 * If we got there, the file is already listed as to be included in the
238548847494SEnrico Perla - Sun Microsystems 	 * iso image. We just need to know if we are going to rebuild it or not
238648847494SEnrico Perla - Sun Microsystems 	 */
238748847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET) &&
2388cedc7e57SEnrico Perla - Sun Microsystems 	    is_dir_flag_on(FILE64, NEED_UPDATE) && !bam_nowrite())
238948847494SEnrico Perla - Sun Microsystems 		return (0);
239048847494SEnrico Perla - Sun Microsystems 	/*
23917c478bd9Sstevel@tonic-gate 	 * File exists in old archive. Check if file has changed
23927c478bd9Sstevel@tonic-gate 	 */
23937c478bd9Sstevel@tonic-gate 	assert(sz == 2);
23947c478bd9Sstevel@tonic-gate 	bcopy(value, filestat, sizeof (filestat));
23957c478bd9Sstevel@tonic-gate 
239648847494SEnrico Perla - Sun Microsystems 	if (flags != FTW_D && (filestat[0] != st->st_size ||
239748847494SEnrico Perla - Sun Microsystems 	    filestat[1] != st->st_mtime)) {
23987c609327Ssetje 		if (bam_smf_check) {
23997c609327Ssetje 			safefilep = safefiles;
240089c3ee43SGangadhar Mylapuram 			while (safefilep != NULL &&
240189c3ee43SGangadhar Mylapuram 			    safefilep->name[0] != '\0') {
240289c3ee43SGangadhar Mylapuram 				if (regcomp(&re, safefilep->name,
240389c3ee43SGangadhar Mylapuram 				    REG_EXTENDED|REG_NOSUB) == 0) {
240489c3ee43SGangadhar Mylapuram 					status = regexec(&re,
240589c3ee43SGangadhar Mylapuram 					    file + bam_rootlen, 0, NULL, 0);
240689c3ee43SGangadhar Mylapuram 					regfree(&re);
240789c3ee43SGangadhar Mylapuram 					if (status == 0) {
24083b133becSGangadhar Mylapuram 						(void) creat(
24093b133becSGangadhar Mylapuram 						    NEED_UPDATE_SAFE_FILE,
241089c3ee43SGangadhar Mylapuram 						    0644);
24117c609327Ssetje 						return (0);
24127c609327Ssetje 					}
241389c3ee43SGangadhar Mylapuram 				}
24147c609327Ssetje 				safefilep = safefilep->next;
24157c609327Ssetje 			}
24167c609327Ssetje 		}
241748847494SEnrico Perla - Sun Microsystems 
241848847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(IS_SPARC_TARGET)) {
241948847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
242048847494SEnrico Perla - Sun Microsystems 		} else {
242148847494SEnrico Perla - Sun Microsystems 			ret = update_dircache(file, flags);
242248847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_ERROR) {
2423*f64ca102SToomas Soome 				bam_error(_("directory cache update failed "
2424*f64ca102SToomas Soome 				    "for %s\n"), file);
242548847494SEnrico Perla - Sun Microsystems 				return (-1);
242648847494SEnrico Perla - Sun Microsystems 			}
242748847494SEnrico Perla - Sun Microsystems 		}
242848847494SEnrico Perla - Sun Microsystems 
2429110570ceSToomas Soome 		if (bam_verbose) {
24307c478bd9Sstevel@tonic-gate 			if (bam_smf_check)
24317c478bd9Sstevel@tonic-gate 				bam_print("    %s\n", file);
24327c478bd9Sstevel@tonic-gate 			else
2433*f64ca102SToomas Soome 				bam_print(_("    changed %s\n"), file);
24347c478bd9Sstevel@tonic-gate 		}
2435110570ceSToomas Soome 	}
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 	return (0);
24387c478bd9Sstevel@tonic-gate }
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate /*
244148847494SEnrico Perla - Sun Microsystems  * Remove a directory path recursively
244248847494SEnrico Perla - Sun Microsystems  */
244348847494SEnrico Perla - Sun Microsystems static int
244448847494SEnrico Perla - Sun Microsystems rmdir_r(char *path)
244548847494SEnrico Perla - Sun Microsystems {
244648847494SEnrico Perla - Sun Microsystems 	struct dirent 	*d = NULL;
244748847494SEnrico Perla - Sun Microsystems 	DIR 		*dir = NULL;
244848847494SEnrico Perla - Sun Microsystems 	char 		tpath[PATH_MAX];
244948847494SEnrico Perla - Sun Microsystems 	struct stat 	sb;
245048847494SEnrico Perla - Sun Microsystems 
245148847494SEnrico Perla - Sun Microsystems 	if ((dir = opendir(path)) == NULL)
245248847494SEnrico Perla - Sun Microsystems 		return (-1);
245348847494SEnrico Perla - Sun Microsystems 
2454110570ceSToomas Soome 	while ((d = readdir(dir)) != NULL) {
245548847494SEnrico Perla - Sun Microsystems 		if ((strcmp(d->d_name, ".") != 0) &&
245648847494SEnrico Perla - Sun Microsystems 		    (strcmp(d->d_name, "..") != 0)) {
245748847494SEnrico Perla - Sun Microsystems 			(void) snprintf(tpath, sizeof (tpath), "%s/%s",
245848847494SEnrico Perla - Sun Microsystems 			    path, d->d_name);
245948847494SEnrico Perla - Sun Microsystems 			if (stat(tpath, &sb) == 0) {
246048847494SEnrico Perla - Sun Microsystems 				if (sb.st_mode & S_IFDIR)
246148847494SEnrico Perla - Sun Microsystems 					(void) rmdir_r(tpath);
246248847494SEnrico Perla - Sun Microsystems 				else
246348847494SEnrico Perla - Sun Microsystems 					(void) remove(tpath);
246448847494SEnrico Perla - Sun Microsystems 			}
246548847494SEnrico Perla - Sun Microsystems 		}
246648847494SEnrico Perla - Sun Microsystems 	}
246748847494SEnrico Perla - Sun Microsystems 	return (remove(path));
246848847494SEnrico Perla - Sun Microsystems }
246948847494SEnrico Perla - Sun Microsystems 
247048847494SEnrico Perla - Sun Microsystems /*
247148847494SEnrico Perla - Sun Microsystems  * Check if cache directory exists and, if not, create it and update flags
247248847494SEnrico Perla - Sun Microsystems  * accordingly. If the path exists, but it's not a directory, a best effort
247348847494SEnrico Perla - Sun Microsystems  * attempt to remove and recreate it is made.
247448847494SEnrico Perla - Sun Microsystems  * If the user requested a 'purge', always recreate the directory from scratch.
247548847494SEnrico Perla - Sun Microsystems  */
247648847494SEnrico Perla - Sun Microsystems static int
247748847494SEnrico Perla - Sun Microsystems set_cache_dir(char *root, int what)
247848847494SEnrico Perla - Sun Microsystems {
247948847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
248048847494SEnrico Perla - Sun Microsystems 	int		ret = 0;
248148847494SEnrico Perla - Sun Microsystems 
248248847494SEnrico Perla - Sun Microsystems 	ret = snprintf(get_cachedir(what), sizeof (get_cachedir(what)),
248348847494SEnrico Perla - Sun Microsystems 	    "%s%s%s%s%s", root, ARCHIVE_PREFIX, get_machine(), what == FILE64 ?
248448847494SEnrico Perla - Sun Microsystems 	    "/amd64" : "", CACHEDIR_SUFFIX);
248548847494SEnrico Perla - Sun Microsystems 
248648847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (get_cachedir(what))) {
2487*f64ca102SToomas Soome 		bam_error(_("unable to create path on mountpoint %s, "
2488*f64ca102SToomas Soome 		    "path too long\n"), rootbuf);
248948847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
249048847494SEnrico Perla - Sun Microsystems 	}
249148847494SEnrico Perla - Sun Microsystems 
24924a9df875SEnrico Perla - Sun Microsystems 	if (bam_purge || is_flag_on(INVALIDATE_CACHE))
249348847494SEnrico Perla - Sun Microsystems 		(void) rmdir_r(get_cachedir(what));
249448847494SEnrico Perla - Sun Microsystems 
249548847494SEnrico Perla - Sun Microsystems 	if (stat(get_cachedir(what), &sb) != 0 || !(S_ISDIR(sb.st_mode))) {
249648847494SEnrico Perla - Sun Microsystems 		/* best effort unlink attempt, mkdir will catch errors */
249748847494SEnrico Perla - Sun Microsystems 		(void) unlink(get_cachedir(what));
249848847494SEnrico Perla - Sun Microsystems 
249948847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
2500*f64ca102SToomas Soome 			bam_print(_("archive cache directory not found: %s\n"),
2501*f64ca102SToomas Soome 			    get_cachedir(what));
250248847494SEnrico Perla - Sun Microsystems 		ret = mkdir(get_cachedir(what), DIR_PERMS);
250348847494SEnrico Perla - Sun Microsystems 		if (ret < 0) {
2504*f64ca102SToomas Soome 			bam_error(_("mkdir of %s failed: %s\n"),
2505*f64ca102SToomas Soome 			    get_cachedir(what), strerror(errno));
250648847494SEnrico Perla - Sun Microsystems 			get_cachedir(what)[0] = '\0';
250748847494SEnrico Perla - Sun Microsystems 			return (ret);
250848847494SEnrico Perla - Sun Microsystems 		}
250948847494SEnrico Perla - Sun Microsystems 		set_flag(NEED_CACHE_DIR);
251048847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
251148847494SEnrico Perla - Sun Microsystems 	}
251248847494SEnrico Perla - Sun Microsystems 
251348847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
251448847494SEnrico Perla - Sun Microsystems }
251548847494SEnrico Perla - Sun Microsystems 
251648847494SEnrico Perla - Sun Microsystems static int
251748847494SEnrico Perla - Sun Microsystems set_update_dir(char *root, int what)
251848847494SEnrico Perla - Sun Microsystems {
251948847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
252048847494SEnrico Perla - Sun Microsystems 	int		ret;
252148847494SEnrico Perla - Sun Microsystems 
252248847494SEnrico Perla - Sun Microsystems 	if (is_dir_flag_on(what, NO_MULTI))
252348847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
252448847494SEnrico Perla - Sun Microsystems 
252548847494SEnrico Perla - Sun Microsystems 	if (!bam_extend) {
252648847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
252748847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
252848847494SEnrico Perla - Sun Microsystems 	}
252948847494SEnrico Perla - Sun Microsystems 
253048847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
253148847494SEnrico Perla - Sun Microsystems 		ret = snprintf(get_updatedir(what),
253248847494SEnrico Perla - Sun Microsystems 		    sizeof (get_updatedir(what)), "%s%s%s/amd64%s", root,
253348847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_PREFIX, get_machine(), UPDATEDIR_SUFFIX);
253448847494SEnrico Perla - Sun Microsystems 	else
253548847494SEnrico Perla - Sun Microsystems 		ret = snprintf(get_updatedir(what),
253648847494SEnrico Perla - Sun Microsystems 		    sizeof (get_updatedir(what)), "%s%s%s%s", root,
253748847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_PREFIX, get_machine(), UPDATEDIR_SUFFIX);
253848847494SEnrico Perla - Sun Microsystems 
253948847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (get_updatedir(what))) {
2540*f64ca102SToomas Soome 		bam_error(_("unable to create path on mountpoint %s, "
2541*f64ca102SToomas Soome 		    "path too long\n"), rootbuf);
254248847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
254348847494SEnrico Perla - Sun Microsystems 	}
254448847494SEnrico Perla - Sun Microsystems 
254548847494SEnrico Perla - Sun Microsystems 	if (stat(get_updatedir(what), &sb) == 0) {
254648847494SEnrico Perla - Sun Microsystems 		if (S_ISDIR(sb.st_mode))
254748847494SEnrico Perla - Sun Microsystems 			ret = rmdir_r(get_updatedir(what));
254848847494SEnrico Perla - Sun Microsystems 		else
254948847494SEnrico Perla - Sun Microsystems 			ret = unlink(get_updatedir(what));
255048847494SEnrico Perla - Sun Microsystems 
255148847494SEnrico Perla - Sun Microsystems 		if (ret != 0)
255248847494SEnrico Perla - Sun Microsystems 			set_dir_flag(what, NO_MULTI);
255348847494SEnrico Perla - Sun Microsystems 	}
255448847494SEnrico Perla - Sun Microsystems 
255548847494SEnrico Perla - Sun Microsystems 	if (mkdir(get_updatedir(what), DIR_PERMS) < 0)
255648847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
255748847494SEnrico Perla - Sun Microsystems 
255848847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
255948847494SEnrico Perla - Sun Microsystems }
256048847494SEnrico Perla - Sun Microsystems 
256148847494SEnrico Perla - Sun Microsystems static int
256248847494SEnrico Perla - Sun Microsystems is_valid_archive(char *root, int what)
256348847494SEnrico Perla - Sun Microsystems {
256448847494SEnrico Perla - Sun Microsystems 	char 		archive_path[PATH_MAX];
25654a9df875SEnrico Perla - Sun Microsystems 	char		timestamp_path[PATH_MAX];
25664a9df875SEnrico Perla - Sun Microsystems 	struct stat 	sb, timestamp;
256748847494SEnrico Perla - Sun Microsystems 	int 		ret;
256848847494SEnrico Perla - Sun Microsystems 
256948847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
257048847494SEnrico Perla - Sun Microsystems 		ret = snprintf(archive_path, sizeof (archive_path),
257148847494SEnrico Perla - Sun Microsystems 		    "%s%s%s/amd64%s", root, ARCHIVE_PREFIX, get_machine(),
257248847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_SUFFIX);
257348847494SEnrico Perla - Sun Microsystems 	else
257448847494SEnrico Perla - Sun Microsystems 		ret = snprintf(archive_path, sizeof (archive_path), "%s%s%s%s",
257548847494SEnrico Perla - Sun Microsystems 		    root, ARCHIVE_PREFIX, get_machine(), ARCHIVE_SUFFIX);
257648847494SEnrico Perla - Sun Microsystems 
257748847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (archive_path)) {
2578*f64ca102SToomas Soome 		bam_error(_("unable to create path on mountpoint %s, "
2579*f64ca102SToomas Soome 		    "path too long\n"), rootbuf);
258048847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
258148847494SEnrico Perla - Sun Microsystems 	}
258248847494SEnrico Perla - Sun Microsystems 
258348847494SEnrico Perla - Sun Microsystems 	if (stat(archive_path, &sb) != 0) {
258448847494SEnrico Perla - Sun Microsystems 		if (bam_verbose && !bam_check)
2585*f64ca102SToomas Soome 			bam_print(_("archive not found: %s\n"), archive_path);
258648847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NEED_UPDATE);
258748847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
258848847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
258948847494SEnrico Perla - Sun Microsystems 	}
259048847494SEnrico Perla - Sun Microsystems 
25914a9df875SEnrico Perla - Sun Microsystems 	/*
25924a9df875SEnrico Perla - Sun Microsystems 	 * The timestamp file is used to prevent stale files in the archive
25934a9df875SEnrico Perla - Sun Microsystems 	 * cache.
25944a9df875SEnrico Perla - Sun Microsystems 	 * Stale files can happen if the system is booted back and forth across
25954a9df875SEnrico Perla - Sun Microsystems 	 * the transition from bootadm-before-the-cache to
25964a9df875SEnrico Perla - Sun Microsystems 	 * bootadm-after-the-cache, since older versions of bootadm don't know
25974a9df875SEnrico Perla - Sun Microsystems 	 * about the existence of the archive cache.
25984a9df875SEnrico Perla - Sun Microsystems 	 *
25994a9df875SEnrico Perla - Sun Microsystems 	 * Since only bootadm-after-the-cache versions know about about this
26004a9df875SEnrico Perla - Sun Microsystems 	 * file, we require that the boot archive be older than this file.
26014a9df875SEnrico Perla - Sun Microsystems 	 */
26024a9df875SEnrico Perla - Sun Microsystems 	ret = snprintf(timestamp_path, sizeof (timestamp_path), "%s%s", root,
26034a9df875SEnrico Perla - Sun Microsystems 	    FILE_STAT_TIMESTAMP);
26044a9df875SEnrico Perla - Sun Microsystems 
26054a9df875SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (timestamp_path)) {
2606*f64ca102SToomas Soome 		bam_error(_("unable to create path on mountpoint %s, "
2607*f64ca102SToomas Soome 		    "path too long\n"), rootbuf);
26084a9df875SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
26094a9df875SEnrico Perla - Sun Microsystems 	}
26104a9df875SEnrico Perla - Sun Microsystems 
26114a9df875SEnrico Perla - Sun Microsystems 	if (stat(timestamp_path, &timestamp) != 0 ||
26124a9df875SEnrico Perla - Sun Microsystems 	    sb.st_mtime > timestamp.st_mtime) {
26134a9df875SEnrico Perla - Sun Microsystems 		if (bam_verbose && !bam_check)
2614*f64ca102SToomas Soome 			bam_print(
2615*f64ca102SToomas Soome 			    _("archive cache is out of sync. Rebuilding.\n"));
26164a9df875SEnrico Perla - Sun Microsystems 		/*
26174a9df875SEnrico Perla - Sun Microsystems 		 * Don't generate a false positive for the boot-archive service
26184a9df875SEnrico Perla - Sun Microsystems 		 * but trigger an update of the archive cache in
26194a9df875SEnrico Perla - Sun Microsystems 		 * boot-archive-update.
26204a9df875SEnrico Perla - Sun Microsystems 		 */
26214a9df875SEnrico Perla - Sun Microsystems 		if (bam_smf_check) {
26224a9df875SEnrico Perla - Sun Microsystems 			(void) creat(NEED_UPDATE_FILE, 0644);
26234a9df875SEnrico Perla - Sun Microsystems 			return (BAM_SUCCESS);
26244a9df875SEnrico Perla - Sun Microsystems 		}
26254a9df875SEnrico Perla - Sun Microsystems 
26264a9df875SEnrico Perla - Sun Microsystems 		set_flag(INVALIDATE_CACHE);
26274a9df875SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NEED_UPDATE);
26284a9df875SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
26294a9df875SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
26304a9df875SEnrico Perla - Sun Microsystems 	}
26314a9df875SEnrico Perla - Sun Microsystems 
263248847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET))
263348847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
263448847494SEnrico Perla - Sun Microsystems 
263548847494SEnrico Perla - Sun Microsystems 	if (bam_extend && sb.st_size > BA_SIZE_MAX) {
263648847494SEnrico Perla - Sun Microsystems 		if (bam_verbose && !bam_check)
2637*f64ca102SToomas Soome 			bam_print(_("archive %s is bigger than %d bytes and "
2638*f64ca102SToomas Soome 			    "will be rebuilt\n"), archive_path, BA_SIZE_MAX);
263948847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
264048847494SEnrico Perla - Sun Microsystems 	}
264148847494SEnrico Perla - Sun Microsystems 
264248847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
264348847494SEnrico Perla - Sun Microsystems }
264448847494SEnrico Perla - Sun Microsystems 
264548847494SEnrico Perla - Sun Microsystems /*
264648847494SEnrico Perla - Sun Microsystems  * Check flags and presence of required files and directories.
26477c478bd9Sstevel@tonic-gate  * The force flag and/or absence of files should
26487c478bd9Sstevel@tonic-gate  * trigger an update.
26497c478bd9Sstevel@tonic-gate  * Suppress stdout output if check (-n) option is set
26507c478bd9Sstevel@tonic-gate  * (as -n should only produce parseable output.)
26517c478bd9Sstevel@tonic-gate  */
265248847494SEnrico Perla - Sun Microsystems static int
26537c478bd9Sstevel@tonic-gate check_flags_and_files(char *root)
26547c478bd9Sstevel@tonic-gate {
265548847494SEnrico Perla - Sun Microsystems 
26567c478bd9Sstevel@tonic-gate 	struct stat 	sb;
265748847494SEnrico Perla - Sun Microsystems 	int 		ret;
265848847494SEnrico Perla - Sun Microsystems 
265948847494SEnrico Perla - Sun Microsystems 	/*
266048847494SEnrico Perla - Sun Microsystems 	 * If archive is missing, create archive
266148847494SEnrico Perla - Sun Microsystems 	 */
266248847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET)) {
266348847494SEnrico Perla - Sun Microsystems 		ret = is_valid_archive(root, FILE64);
2664cedc7e57SEnrico Perla - Sun Microsystems 		if (ret == BAM_ERROR)
2665cedc7e57SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
266648847494SEnrico Perla - Sun Microsystems 	} else {
266748847494SEnrico Perla - Sun Microsystems 		int	what = FILE32;
266848847494SEnrico Perla - Sun Microsystems 		do {
266948847494SEnrico Perla - Sun Microsystems 			ret = is_valid_archive(root, what);
267048847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_ERROR)
267148847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
267248847494SEnrico Perla - Sun Microsystems 			what++;
267348847494SEnrico Perla - Sun Microsystems 		} while (bam_direct == BAM_DIRECT_DBOOT && what < CACHEDIR_NUM);
267448847494SEnrico Perla - Sun Microsystems 	}
267548847494SEnrico Perla - Sun Microsystems 
267648847494SEnrico Perla - Sun Microsystems 	if (bam_nowrite())
267748847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
267848847494SEnrico Perla - Sun Microsystems 
267948847494SEnrico Perla - Sun Microsystems 
268048847494SEnrico Perla - Sun Microsystems 	/*
268148847494SEnrico Perla - Sun Microsystems 	 * check if cache directories exist on x86.
268248847494SEnrico Perla - Sun Microsystems 	 * check (and always open) the cache file on SPARC.
268348847494SEnrico Perla - Sun Microsystems 	 */
268448847494SEnrico Perla - Sun Microsystems 	if (is_sparc()) {
268548847494SEnrico Perla - Sun Microsystems 		ret = snprintf(get_cachedir(FILE64),
268648847494SEnrico Perla - Sun Microsystems 		    sizeof (get_cachedir(FILE64)), "%s%s%s/%s", root,
268748847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_PREFIX, get_machine(), CACHEDIR_SUFFIX);
268848847494SEnrico Perla - Sun Microsystems 
268948847494SEnrico Perla - Sun Microsystems 		if (ret >= sizeof (get_cachedir(FILE64))) {
2690*f64ca102SToomas Soome 			bam_error(_("unable to create path on mountpoint %s, "
2691*f64ca102SToomas Soome 			    "path too long\n"), rootbuf);
269248847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
269348847494SEnrico Perla - Sun Microsystems 		}
269448847494SEnrico Perla - Sun Microsystems 
269548847494SEnrico Perla - Sun Microsystems 		if (stat(get_cachedir(FILE64), &sb) != 0) {
269648847494SEnrico Perla - Sun Microsystems 			set_flag(NEED_CACHE_DIR);
269748847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
269848847494SEnrico Perla - Sun Microsystems 		}
269948847494SEnrico Perla - Sun Microsystems 
270048847494SEnrico Perla - Sun Microsystems 		walk_arg.sparcfile = fopen(get_cachedir(FILE64), "w");
270148847494SEnrico Perla - Sun Microsystems 		if (walk_arg.sparcfile == NULL) {
2702*f64ca102SToomas Soome 			bam_error(_("failed to open file: %s: %s\n"),
2703*f64ca102SToomas Soome 			    get_cachedir(FILE64), strerror(errno));
270448847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
270548847494SEnrico Perla - Sun Microsystems 		}
270648847494SEnrico Perla - Sun Microsystems 
270748847494SEnrico Perla - Sun Microsystems 		set_dir_present(FILE64);
270848847494SEnrico Perla - Sun Microsystems 	} else {
270948847494SEnrico Perla - Sun Microsystems 		int	what = FILE32;
271048847494SEnrico Perla - Sun Microsystems 
271148847494SEnrico Perla - Sun Microsystems 		do {
271248847494SEnrico Perla - Sun Microsystems 			if (set_cache_dir(root, what) != 0)
271348847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
271448847494SEnrico Perla - Sun Microsystems 
271548847494SEnrico Perla - Sun Microsystems 			set_dir_present(what);
271648847494SEnrico Perla - Sun Microsystems 
271748847494SEnrico Perla - Sun Microsystems 			if (set_update_dir(root, what) != 0)
271848847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
271948847494SEnrico Perla - Sun Microsystems 			what++;
272048847494SEnrico Perla - Sun Microsystems 		} while (bam_direct == BAM_DIRECT_DBOOT && what < CACHEDIR_NUM);
272148847494SEnrico Perla - Sun Microsystems 	}
27227c478bd9Sstevel@tonic-gate 
27237c478bd9Sstevel@tonic-gate 	/*
27247c478bd9Sstevel@tonic-gate 	 * if force, create archive unconditionally
27257c478bd9Sstevel@tonic-gate 	 */
27267c478bd9Sstevel@tonic-gate 	if (bam_force) {
272748847494SEnrico Perla - Sun Microsystems 		if (!is_sparc())
272848847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE32, NEED_UPDATE);
272948847494SEnrico Perla - Sun Microsystems 		set_dir_flag(FILE64, NEED_UPDATE);
273048847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
2731*f64ca102SToomas Soome 			bam_print(_("forced update of archive requested\n"));
273248847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
27337c478bd9Sstevel@tonic-gate 	}
27347c478bd9Sstevel@tonic-gate 
273548847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
27367c478bd9Sstevel@tonic-gate }
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate static error_t
27397c478bd9Sstevel@tonic-gate read_one_list(char *root, filelist_t  *flistp, char *filelist)
27407c478bd9Sstevel@tonic-gate {
27417c478bd9Sstevel@tonic-gate 	char 		path[PATH_MAX];
27427c478bd9Sstevel@tonic-gate 	FILE 		*fp;
27437c478bd9Sstevel@tonic-gate 	char 		buf[BAM_MAXLINE];
2744eb2bd662Svikram 	const char 	*fcn = "read_one_list()";
27457c478bd9Sstevel@tonic-gate 
27467c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, filelist);
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 	fp = fopen(path, "r");
27497c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
2750*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: failed to open archive filelist: %s: %s\n",
2751*f64ca102SToomas Soome 		    fcn, path, strerror(errno)));
27527c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27537c478bd9Sstevel@tonic-gate 	}
27547c478bd9Sstevel@tonic-gate 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
2755b610f78eSvikram 		/* skip blank lines */
2756b610f78eSvikram 		if (strspn(buf, " \t") == strlen(buf))
2757b610f78eSvikram 			continue;
27587c478bd9Sstevel@tonic-gate 		append_to_flist(flistp, buf);
27597c478bd9Sstevel@tonic-gate 	}
27607c478bd9Sstevel@tonic-gate 	if (fclose(fp) != 0) {
2761*f64ca102SToomas Soome 		bam_error(_("failed to close file: %s: %s\n"),
2762*f64ca102SToomas Soome 		    path, strerror(errno));
27637c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27647c478bd9Sstevel@tonic-gate 	}
27657c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
27667c478bd9Sstevel@tonic-gate }
27677c478bd9Sstevel@tonic-gate 
27687c478bd9Sstevel@tonic-gate static error_t
27697c478bd9Sstevel@tonic-gate read_list(char *root, filelist_t  *flistp)
27707c478bd9Sstevel@tonic-gate {
2771986fd29aSsetje 	char 		path[PATH_MAX];
2772986fd29aSsetje 	char 		cmd[PATH_MAX];
2773986fd29aSsetje 	struct stat 	sb;
2774986fd29aSsetje 	int 		n, rval;
2775eb2bd662Svikram 	const char 	*fcn = "read_list()";
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate 	/*
2780986fd29aSsetje 	 * build and check path to extract_boot_filelist.ksh
2781986fd29aSsetje 	 */
2782986fd29aSsetje 	n = snprintf(path, sizeof (path), "%s%s", root, EXTRACT_BOOT_FILELIST);
2783986fd29aSsetje 	if (n >= sizeof (path)) {
2784*f64ca102SToomas Soome 		bam_error(_("archive filelist is empty\n"));
2785986fd29aSsetje 		return (BAM_ERROR);
2786986fd29aSsetje 	}
2787986fd29aSsetje 
2788cedc7e57SEnrico Perla - Sun Microsystems 	if (is_safe_exec(path) == BAM_ERROR)
2789cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
2790cedc7e57SEnrico Perla - Sun Microsystems 
2791986fd29aSsetje 	/*
2792986fd29aSsetje 	 * If extract_boot_filelist is present, exec it, otherwise read
2793986fd29aSsetje 	 * the filelists directly, for compatibility with older images.
2794986fd29aSsetje 	 */
2795986fd29aSsetje 	if (stat(path, &sb) == 0) {
2796986fd29aSsetje 		/*
2797986fd29aSsetje 		 * build arguments to exec extract_boot_filelist.ksh
2798986fd29aSsetje 		 */
2799d876c67dSjg 		char *rootarg, *platarg;
2800d876c67dSjg 		int platarglen = 1, rootarglen = 1;
2801d876c67dSjg 		if (strlen(root) > 1)
2802d876c67dSjg 			rootarglen += strlen(root) + strlen("-R ");
2803d876c67dSjg 		if (bam_alt_platform)
2804d876c67dSjg 			platarglen += strlen(bam_platform) + strlen("-p ");
2805d876c67dSjg 		platarg = s_calloc(1, platarglen);
2806d876c67dSjg 		rootarg = s_calloc(1, rootarglen);
2807d876c67dSjg 		*platarg = 0;
2808d876c67dSjg 		*rootarg = 0;
2809d876c67dSjg 
2810986fd29aSsetje 		if (strlen(root) > 1) {
2811d876c67dSjg 			(void) snprintf(rootarg, rootarglen,
2812d876c67dSjg 			    "-R %s", root);
2813986fd29aSsetje 		}
2814d876c67dSjg 		if (bam_alt_platform) {
2815d876c67dSjg 			(void) snprintf(platarg, platarglen,
2816d876c67dSjg 			    "-p %s", bam_platform);
2817d876c67dSjg 		}
2818d876c67dSjg 		n = snprintf(cmd, sizeof (cmd), "%s %s %s /%s /%s",
2819d876c67dSjg 		    path, rootarg, platarg, BOOT_FILE_LIST, ETC_FILE_LIST);
2820d876c67dSjg 		free(platarg);
2821d876c67dSjg 		free(rootarg);
2822986fd29aSsetje 		if (n >= sizeof (cmd)) {
2823*f64ca102SToomas Soome 			bam_error(_("archive filelist is empty\n"));
2824986fd29aSsetje 			return (BAM_ERROR);
2825986fd29aSsetje 		}
2826986fd29aSsetje 		if (exec_cmd(cmd, flistp) != 0) {
2827*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: failed to open archive "
2828*f64ca102SToomas Soome 			    "filelist: %s: %s\n", fcn, path, strerror(errno)));
2829986fd29aSsetje 			return (BAM_ERROR);
2830986fd29aSsetje 		}
2831986fd29aSsetje 	} else {
2832986fd29aSsetje 		/*
28337c478bd9Sstevel@tonic-gate 		 * Read current lists of files - only the first is mandatory
28347c478bd9Sstevel@tonic-gate 		 */
28357c478bd9Sstevel@tonic-gate 		rval = read_one_list(root, flistp, BOOT_FILE_LIST);
28367c478bd9Sstevel@tonic-gate 		if (rval != BAM_SUCCESS)
28377c478bd9Sstevel@tonic-gate 			return (rval);
28387c478bd9Sstevel@tonic-gate 		(void) read_one_list(root, flistp, ETC_FILE_LIST);
2839986fd29aSsetje 	}
28407c478bd9Sstevel@tonic-gate 
28417c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL) {
2842*f64ca102SToomas Soome 		bam_error(_("archive filelist is empty\n"));
28437c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
28447c478bd9Sstevel@tonic-gate 	}
28457c478bd9Sstevel@tonic-gate 
28467c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
28477c478bd9Sstevel@tonic-gate }
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate static void
28507c478bd9Sstevel@tonic-gate getoldstat(char *root)
28517c478bd9Sstevel@tonic-gate {
28527c478bd9Sstevel@tonic-gate 	char 		path[PATH_MAX];
28537c478bd9Sstevel@tonic-gate 	int 		fd, error;
28547c478bd9Sstevel@tonic-gate 	struct stat 	sb;
28557c478bd9Sstevel@tonic-gate 	char 		*ostat;
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
28587c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDONLY);
28597c478bd9Sstevel@tonic-gate 	if (fd == -1) {
28607c478bd9Sstevel@tonic-gate 		if (bam_verbose)
2861*f64ca102SToomas Soome 			bam_print(_("failed to open file: %s: %s\n"),
2862*f64ca102SToomas Soome 			    path, strerror(errno));
286348847494SEnrico Perla - Sun Microsystems 		goto out_err;
28647c478bd9Sstevel@tonic-gate 	}
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate 	if (fstat(fd, &sb) != 0) {
2867*f64ca102SToomas Soome 		bam_error(_("stat of file failed: %s: %s\n"), path,
2868*f64ca102SToomas Soome 		    strerror(errno));
286948847494SEnrico Perla - Sun Microsystems 		goto out_err;
28707c478bd9Sstevel@tonic-gate 	}
28717c478bd9Sstevel@tonic-gate 
28727c478bd9Sstevel@tonic-gate 	ostat = s_calloc(1, sb.st_size);
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 	if (read(fd, ostat, sb.st_size) != sb.st_size) {
2875*f64ca102SToomas Soome 		bam_error(_("read failed for file: %s: %s\n"), path,
2876*f64ca102SToomas Soome 		    strerror(errno));
28777c478bd9Sstevel@tonic-gate 		free(ostat);
287848847494SEnrico Perla - Sun Microsystems 		goto out_err;
28797c478bd9Sstevel@tonic-gate 	}
28807c478bd9Sstevel@tonic-gate 
28817c478bd9Sstevel@tonic-gate 	(void) close(fd);
288248847494SEnrico Perla - Sun Microsystems 	fd = -1;
28837c478bd9Sstevel@tonic-gate 
28847c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
28857c478bd9Sstevel@tonic-gate 	error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0);
28867c478bd9Sstevel@tonic-gate 
28877c478bd9Sstevel@tonic-gate 	free(ostat);
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate 	if (error) {
2890*f64ca102SToomas Soome 		bam_error(_("failed to unpack stat data: %s: %s\n"),
2891*f64ca102SToomas Soome 		    path, strerror(error));
28927c478bd9Sstevel@tonic-gate 		walk_arg.old_nvlp = NULL;
289348847494SEnrico Perla - Sun Microsystems 		goto out_err;
289448847494SEnrico Perla - Sun Microsystems 	} else {
28957c478bd9Sstevel@tonic-gate 		return;
28967c478bd9Sstevel@tonic-gate 	}
289748847494SEnrico Perla - Sun Microsystems 
289848847494SEnrico Perla - Sun Microsystems out_err:
289948847494SEnrico Perla - Sun Microsystems 	if (fd != -1)
290048847494SEnrico Perla - Sun Microsystems 		(void) close(fd);
2901cedc7e57SEnrico Perla - Sun Microsystems 	if (!is_flag_on(IS_SPARC_TARGET))
290248847494SEnrico Perla - Sun Microsystems 		set_dir_flag(FILE32, NEED_UPDATE);
290348847494SEnrico Perla - Sun Microsystems 	set_dir_flag(FILE64, NEED_UPDATE);
290448847494SEnrico Perla - Sun Microsystems }
290548847494SEnrico Perla - Sun Microsystems 
290648847494SEnrico Perla - Sun Microsystems /* Best effort stale entry removal */
290748847494SEnrico Perla - Sun Microsystems static void
290848847494SEnrico Perla - Sun Microsystems delete_stale(char *file, int what)
290948847494SEnrico Perla - Sun Microsystems {
291048847494SEnrico Perla - Sun Microsystems 	char		path[PATH_MAX];
291148847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
291248847494SEnrico Perla - Sun Microsystems 
291348847494SEnrico Perla - Sun Microsystems 	(void) snprintf(path, sizeof (path), "%s/%s", get_cachedir(what), file);
291448847494SEnrico Perla - Sun Microsystems 	if (!bam_check && stat(path, &sb) == 0) {
291548847494SEnrico Perla - Sun Microsystems 		if (sb.st_mode & S_IFDIR)
291648847494SEnrico Perla - Sun Microsystems 			(void) rmdir_r(path);
291748847494SEnrico Perla - Sun Microsystems 		else
291848847494SEnrico Perla - Sun Microsystems 			(void) unlink(path);
291948847494SEnrico Perla - Sun Microsystems 
292048847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, (NEED_UPDATE | NO_MULTI));
292148847494SEnrico Perla - Sun Microsystems 	}
29227c478bd9Sstevel@tonic-gate }
29237c478bd9Sstevel@tonic-gate 
2924a28d77b8Svikram /*
2925a28d77b8Svikram  * Checks if a file in the current (old) archive has
2926a28d77b8Svikram  * been deleted from the root filesystem. This is needed for
2927a28d77b8Svikram  * software like Trusted Extensions (TX) that switch early
2928a28d77b8Svikram  * in boot based on presence/absence of a kernel module.
2929a28d77b8Svikram  */
2930a28d77b8Svikram static void
2931a28d77b8Svikram check4stale(char *root)
2932a28d77b8Svikram {
2933a28d77b8Svikram 	nvpair_t	*nvp;
2934a28d77b8Svikram 	nvlist_t	*nvlp;
2935a28d77b8Svikram 	char 		*file;
2936a28d77b8Svikram 	char		path[PATH_MAX];
2937a28d77b8Svikram 
2938a28d77b8Svikram 	/*
2939a28d77b8Svikram 	 * Skip stale file check during smf check
2940a28d77b8Svikram 	 */
2941a28d77b8Svikram 	if (bam_smf_check)
2942a28d77b8Svikram 		return;
2943a28d77b8Svikram 
294448847494SEnrico Perla - Sun Microsystems 	/*
294548847494SEnrico Perla - Sun Microsystems 	 * If we need to (re)create the cache, there's no need to check for
294648847494SEnrico Perla - Sun Microsystems 	 * stale files
294748847494SEnrico Perla - Sun Microsystems 	 */
294848847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(NEED_CACHE_DIR))
294948847494SEnrico Perla - Sun Microsystems 		return;
295048847494SEnrico Perla - Sun Microsystems 
2951a28d77b8Svikram 	/* Nothing to do if no old stats */
2952a28d77b8Svikram 	if ((nvlp = walk_arg.old_nvlp) == NULL)
2953a28d77b8Svikram 		return;
2954a28d77b8Svikram 
2955a28d77b8Svikram 	for (nvp = nvlist_next_nvpair(nvlp, NULL); nvp;
2956a28d77b8Svikram 	    nvp = nvlist_next_nvpair(nvlp, nvp)) {
2957a28d77b8Svikram 		file = nvpair_name(nvp);
2958a28d77b8Svikram 		if (file == NULL)
2959a28d77b8Svikram 			continue;
2960a28d77b8Svikram 		(void) snprintf(path, sizeof (path), "%s/%s",
2961a28d77b8Svikram 		    root, file);
296248847494SEnrico Perla - Sun Microsystems 		if (access(path, F_OK) < 0) {
296348847494SEnrico Perla - Sun Microsystems 			int	what;
296448847494SEnrico Perla - Sun Microsystems 
2965cedc7e57SEnrico Perla - Sun Microsystems 			if (bam_verbose)
2966*f64ca102SToomas Soome 				bam_print(_("    stale %s\n"), path);
296748847494SEnrico Perla - Sun Microsystems 
2968cedc7e57SEnrico Perla - Sun Microsystems 			if (is_flag_on(IS_SPARC_TARGET)) {
2969cedc7e57SEnrico Perla - Sun Microsystems 				set_dir_flag(FILE64, NEED_UPDATE);
2970cedc7e57SEnrico Perla - Sun Microsystems 			} else {
297148847494SEnrico Perla - Sun Microsystems 				for (what = FILE32; what < CACHEDIR_NUM; what++)
297248847494SEnrico Perla - Sun Microsystems 					if (has_cachedir(what))
297348847494SEnrico Perla - Sun Microsystems 						delete_stale(file, what);
2974a28d77b8Svikram 			}
2975a28d77b8Svikram 		}
2976a28d77b8Svikram 	}
2977cedc7e57SEnrico Perla - Sun Microsystems }
2978a28d77b8Svikram 
29797c478bd9Sstevel@tonic-gate static void
29807c478bd9Sstevel@tonic-gate create_newstat(void)
29817c478bd9Sstevel@tonic-gate {
29827c478bd9Sstevel@tonic-gate 	int error;
29837c478bd9Sstevel@tonic-gate 
29847c478bd9Sstevel@tonic-gate 	error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0);
29857c478bd9Sstevel@tonic-gate 	if (error) {
29867c478bd9Sstevel@tonic-gate 		/*
29877c478bd9Sstevel@tonic-gate 		 * Not fatal - we can still create archive
29887c478bd9Sstevel@tonic-gate 		 */
29897c478bd9Sstevel@tonic-gate 		walk_arg.new_nvlp = NULL;
2990*f64ca102SToomas Soome 		bam_error(_("failed to create stat data: %s\n"),
2991*f64ca102SToomas Soome 		    strerror(error));
29927c478bd9Sstevel@tonic-gate 	}
29937c478bd9Sstevel@tonic-gate }
29947c478bd9Sstevel@tonic-gate 
299548847494SEnrico Perla - Sun Microsystems static int
29967c478bd9Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp)
29977c478bd9Sstevel@tonic-gate {
29987c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
29997c478bd9Sstevel@tonic-gate 	line_t *lp;
30007c478bd9Sstevel@tonic-gate 
30017c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
3002986fd29aSsetje 		/*
3003986fd29aSsetje 		 * Don't follow symlinks.  A symlink must refer to
3004986fd29aSsetje 		 * a file that would appear in the archive through
3005986fd29aSsetje 		 * a direct reference.  This matches the archive
3006986fd29aSsetje 		 * construction behavior.
3007986fd29aSsetje 		 */
30087c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, lp->line);
3009986fd29aSsetje 		if (nftw(path, cmpstat, 20, FTW_PHYS) == -1) {
301048847494SEnrico Perla - Sun Microsystems 			if (is_flag_on(UPDATE_ERROR))
301148847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
30127c478bd9Sstevel@tonic-gate 			/*
30137c478bd9Sstevel@tonic-gate 			 * Some files may not exist.
30147c478bd9Sstevel@tonic-gate 			 * For example: etc/rtc_config on a x86 diskless system
30157c478bd9Sstevel@tonic-gate 			 * Emit verbose message only
30167c478bd9Sstevel@tonic-gate 			 */
30177c478bd9Sstevel@tonic-gate 			if (bam_verbose)
3018*f64ca102SToomas Soome 				bam_print(_("cannot find: %s: %s\n"),
3019*f64ca102SToomas Soome 				    path, strerror(errno));
30207c478bd9Sstevel@tonic-gate 		}
30217c478bd9Sstevel@tonic-gate 	}
302248847494SEnrico Perla - Sun Microsystems 
302348847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
30247c478bd9Sstevel@tonic-gate }
30257c478bd9Sstevel@tonic-gate 
30264a9df875SEnrico Perla - Sun Microsystems /*
30274a9df875SEnrico Perla - Sun Microsystems  * Update the timestamp file.
30284a9df875SEnrico Perla - Sun Microsystems  */
30294a9df875SEnrico Perla - Sun Microsystems static void
30304a9df875SEnrico Perla - Sun Microsystems update_timestamp(char *root)
30314a9df875SEnrico Perla - Sun Microsystems {
30324a9df875SEnrico Perla - Sun Microsystems 	char	timestamp_path[PATH_MAX];
30334a9df875SEnrico Perla - Sun Microsystems 
30344a9df875SEnrico Perla - Sun Microsystems 	/* this path length has already been checked in check_flags_and_files */
30354a9df875SEnrico Perla - Sun Microsystems 	(void) snprintf(timestamp_path, sizeof (timestamp_path), "%s%s", root,
30364a9df875SEnrico Perla - Sun Microsystems 	    FILE_STAT_TIMESTAMP);
30374a9df875SEnrico Perla - Sun Microsystems 
30384a9df875SEnrico Perla - Sun Microsystems 	/*
30394a9df875SEnrico Perla - Sun Microsystems 	 * recreate the timestamp file. Since an outdated or absent timestamp
30404a9df875SEnrico Perla - Sun Microsystems 	 * file translates in a complete rebuild of the archive cache, notify
30414a9df875SEnrico Perla - Sun Microsystems 	 * the user of the performance issue.
30424a9df875SEnrico Perla - Sun Microsystems 	 */
30434a9df875SEnrico Perla - Sun Microsystems 	if (creat(timestamp_path, FILE_STAT_MODE) < 0) {
3044*f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), timestamp_path,
3045*f64ca102SToomas Soome 		    strerror(errno));
3046*f64ca102SToomas Soome 		bam_error(_("failed to update the timestamp file, next"
3047*f64ca102SToomas Soome 		    " archive update may experience reduced performance\n"));
30484a9df875SEnrico Perla - Sun Microsystems 	}
30494a9df875SEnrico Perla - Sun Microsystems }
30504a9df875SEnrico Perla - Sun Microsystems 
30514a9df875SEnrico Perla - Sun Microsystems 
30527c478bd9Sstevel@tonic-gate static void
30537c478bd9Sstevel@tonic-gate savenew(char *root)
30547c478bd9Sstevel@tonic-gate {
30557c478bd9Sstevel@tonic-gate 	char 	path[PATH_MAX];
30567c478bd9Sstevel@tonic-gate 	char 	path2[PATH_MAX];
30577c478bd9Sstevel@tonic-gate 	size_t 	sz;
30587c478bd9Sstevel@tonic-gate 	char 	*nstat;
30597c478bd9Sstevel@tonic-gate 	int 	fd, wrote, error;
30607c478bd9Sstevel@tonic-gate 
30617c478bd9Sstevel@tonic-gate 	nstat = NULL;
30627c478bd9Sstevel@tonic-gate 	sz = 0;
30637c478bd9Sstevel@tonic-gate 	error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz,
30647c478bd9Sstevel@tonic-gate 	    NV_ENCODE_XDR, 0);
30657c478bd9Sstevel@tonic-gate 	if (error) {
3066*f64ca102SToomas Soome 		bam_error(_("failed to pack stat data: %s\n"),
3067*f64ca102SToomas Soome 		    strerror(error));
30687c478bd9Sstevel@tonic-gate 		return;
30697c478bd9Sstevel@tonic-gate 	}
30707c478bd9Sstevel@tonic-gate 
30717c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP);
30727c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE);
30737c478bd9Sstevel@tonic-gate 	if (fd == -1) {
3074*f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), path,
3075*f64ca102SToomas Soome 		    strerror(errno));
30767c478bd9Sstevel@tonic-gate 		free(nstat);
30777c478bd9Sstevel@tonic-gate 		return;
30787c478bd9Sstevel@tonic-gate 	}
30797c478bd9Sstevel@tonic-gate 	wrote = write(fd, nstat, sz);
30807c478bd9Sstevel@tonic-gate 	if (wrote != sz) {
3081*f64ca102SToomas Soome 		bam_error(_("write to file failed: %s: %s\n"), path,
3082*f64ca102SToomas Soome 		    strerror(errno));
30837c478bd9Sstevel@tonic-gate 		(void) close(fd);
30847c478bd9Sstevel@tonic-gate 		free(nstat);
30857c478bd9Sstevel@tonic-gate 		return;
30867c478bd9Sstevel@tonic-gate 	}
30877c478bd9Sstevel@tonic-gate 	(void) close(fd);
30887c478bd9Sstevel@tonic-gate 	free(nstat);
30897c478bd9Sstevel@tonic-gate 
30907c478bd9Sstevel@tonic-gate 	(void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT);
30917c478bd9Sstevel@tonic-gate 	if (rename(path, path2) != 0) {
3092*f64ca102SToomas Soome 		bam_error(_("rename to file failed: %s: %s\n"), path2,
3093*f64ca102SToomas Soome 		    strerror(errno));
30947c478bd9Sstevel@tonic-gate 	}
30957c478bd9Sstevel@tonic-gate }
30967c478bd9Sstevel@tonic-gate 
309748847494SEnrico Perla - Sun Microsystems #define	init_walk_args()	bzero(&walk_arg, sizeof (walk_arg))
309848847494SEnrico Perla - Sun Microsystems 
30997c478bd9Sstevel@tonic-gate static void
31007c478bd9Sstevel@tonic-gate clear_walk_args(void)
31017c478bd9Sstevel@tonic-gate {
31027c478bd9Sstevel@tonic-gate 	nvlist_free(walk_arg.old_nvlp);
31037c478bd9Sstevel@tonic-gate 	nvlist_free(walk_arg.new_nvlp);
310448847494SEnrico Perla - Sun Microsystems 	if (walk_arg.sparcfile)
310548847494SEnrico Perla - Sun Microsystems 		(void) fclose(walk_arg.sparcfile);
31067c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
31077c478bd9Sstevel@tonic-gate 	walk_arg.new_nvlp = NULL;
310848847494SEnrico Perla - Sun Microsystems 	walk_arg.sparcfile = NULL;
31097c478bd9Sstevel@tonic-gate }
31107c478bd9Sstevel@tonic-gate 
31117c478bd9Sstevel@tonic-gate /*
31127c478bd9Sstevel@tonic-gate  * Returns:
31137c478bd9Sstevel@tonic-gate  *	0 - no update necessary
31147c478bd9Sstevel@tonic-gate  *	1 - update required.
31157c478bd9Sstevel@tonic-gate  *	BAM_ERROR (-1) - An error occurred
31167c478bd9Sstevel@tonic-gate  *
31177c478bd9Sstevel@tonic-gate  * Special handling for check (-n):
31187c478bd9Sstevel@tonic-gate  * ================================
31197c478bd9Sstevel@tonic-gate  * The check (-n) option produces parseable output.
31207c478bd9Sstevel@tonic-gate  * To do this, we suppress all stdout messages unrelated
31217c478bd9Sstevel@tonic-gate  * to out of sync files.
31227c478bd9Sstevel@tonic-gate  * All stderr messages are still printed though.
31237c478bd9Sstevel@tonic-gate  *
31247c478bd9Sstevel@tonic-gate  */
31257c478bd9Sstevel@tonic-gate static int
31267c478bd9Sstevel@tonic-gate update_required(char *root)
31277c478bd9Sstevel@tonic-gate {
31287c478bd9Sstevel@tonic-gate 	struct stat 	sb;
31297c478bd9Sstevel@tonic-gate 	char 		path[PATH_MAX];
31307c478bd9Sstevel@tonic-gate 	filelist_t 	flist;
31317c478bd9Sstevel@tonic-gate 	filelist_t 	*flistp = &flist;
313248847494SEnrico Perla - Sun Microsystems 	int 		ret;
31337c478bd9Sstevel@tonic-gate 
31347c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
31357c478bd9Sstevel@tonic-gate 
313648847494SEnrico Perla - Sun Microsystems 	if (is_sparc())
313748847494SEnrico Perla - Sun Microsystems 		set_flag(IS_SPARC_TARGET);
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 	/*
314048847494SEnrico Perla - Sun Microsystems 	 * Check if cache directories and archives are present
31417c478bd9Sstevel@tonic-gate 	 */
314248847494SEnrico Perla - Sun Microsystems 
314348847494SEnrico Perla - Sun Microsystems 	ret = check_flags_and_files(root);
314448847494SEnrico Perla - Sun Microsystems 	if (ret < 0)
314548847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
31467c478bd9Sstevel@tonic-gate 
31477c478bd9Sstevel@tonic-gate 	/*
31487c478bd9Sstevel@tonic-gate 	 * In certain deployment scenarios, filestat may not
3149eff168e0SEnrico Perla - Sun Microsystems 	 * exist. Do not stop the boot process, but trigger an update
3150eff168e0SEnrico Perla - Sun Microsystems 	 * of the archives (which will recreate filestat.ramdisk).
31517c478bd9Sstevel@tonic-gate 	 */
31527c478bd9Sstevel@tonic-gate 	if (bam_smf_check) {
31537c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
3154eff168e0SEnrico Perla - Sun Microsystems 		if (stat(path, &sb) != 0) {
3155eff168e0SEnrico Perla - Sun Microsystems 			(void) creat(NEED_UPDATE_FILE, 0644);
31567c478bd9Sstevel@tonic-gate 			return (0);
31577c478bd9Sstevel@tonic-gate 		}
3158eff168e0SEnrico Perla - Sun Microsystems 	}
31597c478bd9Sstevel@tonic-gate 
31607c478bd9Sstevel@tonic-gate 	getoldstat(root);
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 	/*
3163a28d77b8Svikram 	 * Check if the archive contains files that are no longer
3164a28d77b8Svikram 	 * present on the root filesystem.
3165a28d77b8Svikram 	 */
3166a28d77b8Svikram 	check4stale(root);
3167a28d77b8Svikram 
3168a28d77b8Svikram 	/*
31697c478bd9Sstevel@tonic-gate 	 * read list of files
31707c478bd9Sstevel@tonic-gate 	 */
31717c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
31727c478bd9Sstevel@tonic-gate 		clear_walk_args();
31737c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
31747c478bd9Sstevel@tonic-gate 	}
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
31777c478bd9Sstevel@tonic-gate 
31787c478bd9Sstevel@tonic-gate 	/*
31797c478bd9Sstevel@tonic-gate 	 * At this point either the update is required
31807c478bd9Sstevel@tonic-gate 	 * or the decision is pending. In either case
31817c478bd9Sstevel@tonic-gate 	 * we need to create new stat nvlist
31827c478bd9Sstevel@tonic-gate 	 */
31837c478bd9Sstevel@tonic-gate 	create_newstat();
31847c478bd9Sstevel@tonic-gate 	/*
31857c478bd9Sstevel@tonic-gate 	 * This walk does 2 things:
31867c478bd9Sstevel@tonic-gate 	 *  	- gets new stat data for every file
31877c478bd9Sstevel@tonic-gate 	 *	- (optional) compare old and new stat data
31887c478bd9Sstevel@tonic-gate 	 */
318948847494SEnrico Perla - Sun Microsystems 	ret = walk_list(root, &flist);
31907c478bd9Sstevel@tonic-gate 
31917c478bd9Sstevel@tonic-gate 	/* done with the file list */
31927c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
31937c478bd9Sstevel@tonic-gate 
319448847494SEnrico Perla - Sun Microsystems 	/* something went wrong */
319548847494SEnrico Perla - Sun Microsystems 
319648847494SEnrico Perla - Sun Microsystems 	if (ret == BAM_ERROR) {
3197*f64ca102SToomas Soome 		bam_error(_("Failed to gather cache files, archives "
3198*f64ca102SToomas Soome 		    "generation aborted\n"));
319948847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
32007c478bd9Sstevel@tonic-gate 	}
32017c478bd9Sstevel@tonic-gate 
320248847494SEnrico Perla - Sun Microsystems 	if (walk_arg.new_nvlp == NULL) {
3203cedc7e57SEnrico Perla - Sun Microsystems 		if (walk_arg.sparcfile != NULL)
320448847494SEnrico Perla - Sun Microsystems 			(void) fclose(walk_arg.sparcfile);
3205*f64ca102SToomas Soome 		bam_error(_("cannot create new stat data\n"));
320648847494SEnrico Perla - Sun Microsystems 	}
32077c478bd9Sstevel@tonic-gate 
320848847494SEnrico Perla - Sun Microsystems 	/* If nothing was updated, discard newstat. */
320948847494SEnrico Perla - Sun Microsystems 
321048847494SEnrico Perla - Sun Microsystems 	if (!is_dir_flag_on(FILE32, NEED_UPDATE) &&
321148847494SEnrico Perla - Sun Microsystems 	    !is_dir_flag_on(FILE64, NEED_UPDATE)) {
32127c478bd9Sstevel@tonic-gate 		clear_walk_args();
32137c478bd9Sstevel@tonic-gate 		return (0);
32147c478bd9Sstevel@tonic-gate 	}
32157c478bd9Sstevel@tonic-gate 
3216cedc7e57SEnrico Perla - Sun Microsystems 	if (walk_arg.sparcfile != NULL)
321748847494SEnrico Perla - Sun Microsystems 		(void) fclose(walk_arg.sparcfile);
321848847494SEnrico Perla - Sun Microsystems 
32197c478bd9Sstevel@tonic-gate 	return (1);
32207c478bd9Sstevel@tonic-gate }
32217c478bd9Sstevel@tonic-gate 
322248847494SEnrico Perla - Sun Microsystems static int
322348847494SEnrico Perla - Sun Microsystems flushfs(char *root)
322448847494SEnrico Perla - Sun Microsystems {
322548847494SEnrico Perla - Sun Microsystems 	char	cmd[PATH_MAX + 30];
322648847494SEnrico Perla - Sun Microsystems 
322748847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmd, sizeof (cmd), "%s -f \"%s\" 2>/dev/null",
322848847494SEnrico Perla - Sun Microsystems 	    LOCKFS_PATH, root);
322948847494SEnrico Perla - Sun Microsystems 
323048847494SEnrico Perla - Sun Microsystems 	return (exec_cmd(cmd, NULL));
323148847494SEnrico Perla - Sun Microsystems }
323248847494SEnrico Perla - Sun Microsystems 
323348847494SEnrico Perla - Sun Microsystems static int
323448847494SEnrico Perla - Sun Microsystems do_archive_copy(char *source, char *dest)
323548847494SEnrico Perla - Sun Microsystems {
323648847494SEnrico Perla - Sun Microsystems 
3237cedc7e57SEnrico Perla - Sun Microsystems 	sync();
323848847494SEnrico Perla - Sun Microsystems 
323948847494SEnrico Perla - Sun Microsystems 	/* the equivalent of mv archive-new-$pid boot_archive */
3240cedc7e57SEnrico Perla - Sun Microsystems 	if (rename(source, dest) != 0) {
3241cedc7e57SEnrico Perla - Sun Microsystems 		(void) unlink(source);
324248847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
3243cedc7e57SEnrico Perla - Sun Microsystems 	}
324448847494SEnrico Perla - Sun Microsystems 
324548847494SEnrico Perla - Sun Microsystems 	if (flushfs(bam_root) != 0)
3246cedc7e57SEnrico Perla - Sun Microsystems 		sync();
324748847494SEnrico Perla - Sun Microsystems 
324848847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
324948847494SEnrico Perla - Sun Microsystems }
325048847494SEnrico Perla - Sun Microsystems 
325148847494SEnrico Perla - Sun Microsystems static int
325248847494SEnrico Perla - Sun Microsystems check_cmdline(filelist_t flist)
325348847494SEnrico Perla - Sun Microsystems {
325448847494SEnrico Perla - Sun Microsystems 	line_t	*lp;
325548847494SEnrico Perla - Sun Microsystems 
325648847494SEnrico Perla - Sun Microsystems 	for (lp = flist.head; lp; lp = lp->next) {
325748847494SEnrico Perla - Sun Microsystems 		if (strstr(lp->line, "Error:") != NULL ||
325848847494SEnrico Perla - Sun Microsystems 		    strstr(lp->line, "Inode number overflow") != NULL) {
3259cedc7e57SEnrico Perla - Sun Microsystems 			(void) fprintf(stderr, "%s\n", lp->line);
326048847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
326148847494SEnrico Perla - Sun Microsystems 		}
326248847494SEnrico Perla - Sun Microsystems 	}
326348847494SEnrico Perla - Sun Microsystems 
326448847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
326548847494SEnrico Perla - Sun Microsystems }
326648847494SEnrico Perla - Sun Microsystems 
3267cedc7e57SEnrico Perla - Sun Microsystems static void
3268cedc7e57SEnrico Perla - Sun Microsystems dump_errormsg(filelist_t flist)
3269cedc7e57SEnrico Perla - Sun Microsystems {
3270cedc7e57SEnrico Perla - Sun Microsystems 	line_t	*lp;
3271cedc7e57SEnrico Perla - Sun Microsystems 
3272cedc7e57SEnrico Perla - Sun Microsystems 	for (lp = flist.head; lp; lp = lp->next)
3273cedc7e57SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, "%s\n", lp->line);
3274cedc7e57SEnrico Perla - Sun Microsystems }
3275cedc7e57SEnrico Perla - Sun Microsystems 
327648847494SEnrico Perla - Sun Microsystems static int
327748847494SEnrico Perla - Sun Microsystems check_archive(char *dest)
327848847494SEnrico Perla - Sun Microsystems {
327948847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
328048847494SEnrico Perla - Sun Microsystems 
328148847494SEnrico Perla - Sun Microsystems 	if (stat(dest, &sb) != 0 || !S_ISREG(sb.st_mode) ||
328248847494SEnrico Perla - Sun Microsystems 	    sb.st_size < 10000) {
3283*f64ca102SToomas Soome 		bam_error(_("archive file %s not generated correctly\n"), dest);
328448847494SEnrico Perla - Sun Microsystems 		(void) unlink(dest);
328548847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
328648847494SEnrico Perla - Sun Microsystems 	}
328748847494SEnrico Perla - Sun Microsystems 
328848847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
328948847494SEnrico Perla - Sun Microsystems }
329048847494SEnrico Perla - Sun Microsystems 
32916debd3f5SAlexander Eremin static boolean_t
32926debd3f5SAlexander Eremin is_be(char *root)
32936debd3f5SAlexander Eremin {
32946debd3f5SAlexander Eremin 	zfs_handle_t	*zhp;
32956debd3f5SAlexander Eremin 	libzfs_handle_t	*hdl;
32966debd3f5SAlexander Eremin 	be_node_list_t	*be_nodes = NULL;
32976debd3f5SAlexander Eremin 	be_node_list_t	*cur_be;
32986debd3f5SAlexander Eremin 	boolean_t	be_exist = B_FALSE;
329940a5c998SMatthew Ahrens 	char		ds_path[ZFS_MAX_DATASET_NAME_LEN];
33006debd3f5SAlexander Eremin 
33016debd3f5SAlexander Eremin 	if (!is_zfs(root))
33026debd3f5SAlexander Eremin 		return (B_FALSE);
33036debd3f5SAlexander Eremin 	/*
33046debd3f5SAlexander Eremin 	 * Get dataset for mountpoint
33056debd3f5SAlexander Eremin 	 */
33066debd3f5SAlexander Eremin 	if ((hdl = libzfs_init()) == NULL)
33076debd3f5SAlexander Eremin 		return (B_FALSE);
33086debd3f5SAlexander Eremin 
33096debd3f5SAlexander Eremin 	if ((zhp = zfs_path_to_zhandle(hdl, root,
33106debd3f5SAlexander Eremin 	    ZFS_TYPE_FILESYSTEM)) == NULL) {
33116debd3f5SAlexander Eremin 		libzfs_fini(hdl);
33126debd3f5SAlexander Eremin 		return (B_FALSE);
33136debd3f5SAlexander Eremin 	}
33146debd3f5SAlexander Eremin 
33156debd3f5SAlexander Eremin 	(void) strlcpy(ds_path, zfs_get_name(zhp), sizeof (ds_path));
33166debd3f5SAlexander Eremin 
33176debd3f5SAlexander Eremin 	/*
33186debd3f5SAlexander Eremin 	 * Check if the current dataset is BE
33196debd3f5SAlexander Eremin 	 */
33206debd3f5SAlexander Eremin 	if (be_list(NULL, &be_nodes) == BE_SUCCESS) {
33216debd3f5SAlexander Eremin 		for (cur_be = be_nodes; cur_be != NULL;
33226debd3f5SAlexander Eremin 		    cur_be = cur_be->be_next_node) {
33236debd3f5SAlexander Eremin 
33246debd3f5SAlexander Eremin 			/*
33256debd3f5SAlexander Eremin 			 * Because we guarantee that cur_be->be_root_ds
33266debd3f5SAlexander Eremin 			 * is null-terminated by internal data structure,
33276debd3f5SAlexander Eremin 			 * we can safely use strcmp()
33286debd3f5SAlexander Eremin 			 */
33296debd3f5SAlexander Eremin 			if (strcmp(ds_path, cur_be->be_root_ds) == 0) {
33306debd3f5SAlexander Eremin 				be_exist = B_TRUE;
33316debd3f5SAlexander Eremin 				break;
33326debd3f5SAlexander Eremin 			}
33336debd3f5SAlexander Eremin 		}
33346debd3f5SAlexander Eremin 		be_free_list(be_nodes);
33356debd3f5SAlexander Eremin 	}
33366debd3f5SAlexander Eremin 	zfs_close(zhp);
33376debd3f5SAlexander Eremin 	libzfs_fini(hdl);
33386debd3f5SAlexander Eremin 
33396debd3f5SAlexander Eremin 	return (be_exist);
33406debd3f5SAlexander Eremin }
33416debd3f5SAlexander Eremin 
334248847494SEnrico Perla - Sun Microsystems /*
334348847494SEnrico Perla - Sun Microsystems  * Returns 1 if mkiso is in the expected PATH, 0 otherwise
334448847494SEnrico Perla - Sun Microsystems  */
334548847494SEnrico Perla - Sun Microsystems static int
334648847494SEnrico Perla - Sun Microsystems is_mkisofs()
334748847494SEnrico Perla - Sun Microsystems {
3348cedc7e57SEnrico Perla - Sun Microsystems 	if (access(MKISOFS_PATH, X_OK) == 0)
334948847494SEnrico Perla - Sun Microsystems 		return (1);
335048847494SEnrico Perla - Sun Microsystems 	return (0);
335148847494SEnrico Perla - Sun Microsystems }
335248847494SEnrico Perla - Sun Microsystems 
335348847494SEnrico Perla - Sun Microsystems #define	MKISO_PARAMS	" -quiet -graft-points -dlrDJN -relaxed-filenames "
335448847494SEnrico Perla - Sun Microsystems 
335548847494SEnrico Perla - Sun Microsystems static int
335648847494SEnrico Perla - Sun Microsystems create_sparc_archive(char *archive, char *tempname, char *bootblk, char *list)
335748847494SEnrico Perla - Sun Microsystems {
335848847494SEnrico Perla - Sun Microsystems 	int		ret;
335948847494SEnrico Perla - Sun Microsystems 	char		cmdline[3 * PATH_MAX + 64];
336048847494SEnrico Perla - Sun Microsystems 	filelist_t	flist = {0};
336148847494SEnrico Perla - Sun Microsystems 	const char	*func = "create_sparc_archive()";
336248847494SEnrico Perla - Sun Microsystems 
336348847494SEnrico Perla - Sun Microsystems 	if (access(bootblk, R_OK) == 1) {
3364*f64ca102SToomas Soome 		bam_error(_("unable to access bootblk file : %s\n"), bootblk);
336548847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
336648847494SEnrico Perla - Sun Microsystems 	}
336748847494SEnrico Perla - Sun Microsystems 
336848847494SEnrico Perla - Sun Microsystems 	/*
336948847494SEnrico Perla - Sun Microsystems 	 * Prepare mkisofs command line and execute it
337048847494SEnrico Perla - Sun Microsystems 	 */
337148847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s -G %s -o \"%s\" "
3372cedc7e57SEnrico Perla - Sun Microsystems 	    "-path-list \"%s\" 2>&1", MKISOFS_PATH, MKISO_PARAMS, bootblk,
337348847494SEnrico Perla - Sun Microsystems 	    tempname, list);
337448847494SEnrico Perla - Sun Microsystems 
3375*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: executing: %s\n", func, cmdline));
337648847494SEnrico Perla - Sun Microsystems 
337748847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
3378cedc7e57SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
3379cedc7e57SEnrico Perla - Sun Microsystems 		dump_errormsg(flist);
338048847494SEnrico Perla - Sun Microsystems 		goto out_err;
3381cedc7e57SEnrico Perla - Sun Microsystems 	}
338248847494SEnrico Perla - Sun Microsystems 
338348847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
338448847494SEnrico Perla - Sun Microsystems 
338548847494SEnrico Perla - Sun Microsystems 	/*
338648847494SEnrico Perla - Sun Microsystems 	 * Prepare dd command line to copy the bootblk on the new archive and
338748847494SEnrico Perla - Sun Microsystems 	 * execute it
338848847494SEnrico Perla - Sun Microsystems 	 */
338948847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s if=\"%s\" of=\"%s\""
3390cedc7e57SEnrico Perla - Sun Microsystems 	    " bs=1b oseek=1 count=15 conv=notrunc conv=sync 2>&1", DD_PATH_USR,
339148847494SEnrico Perla - Sun Microsystems 	    bootblk, tempname);
339248847494SEnrico Perla - Sun Microsystems 
3393*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: executing: %s\n", func, cmdline));
339448847494SEnrico Perla - Sun Microsystems 
339548847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
339648847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR)
339748847494SEnrico Perla - Sun Microsystems 		goto out_err;
339848847494SEnrico Perla - Sun Microsystems 
339948847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
340048847494SEnrico Perla - Sun Microsystems 
340148847494SEnrico Perla - Sun Microsystems 	/* Did we get a valid archive ? */
340248847494SEnrico Perla - Sun Microsystems 	if (check_archive(tempname) == BAM_ERROR)
340348847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
340448847494SEnrico Perla - Sun Microsystems 
340548847494SEnrico Perla - Sun Microsystems 	return (do_archive_copy(tempname, archive));
340648847494SEnrico Perla - Sun Microsystems 
340748847494SEnrico Perla - Sun Microsystems out_err:
340848847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
3409*f64ca102SToomas Soome 	bam_error(_("boot-archive creation FAILED, command: '%s'\n"), cmdline);
341048847494SEnrico Perla - Sun Microsystems 	(void) unlink(tempname);
341148847494SEnrico Perla - Sun Microsystems 	return (BAM_ERROR);
341248847494SEnrico Perla - Sun Microsystems }
341348847494SEnrico Perla - Sun Microsystems 
341448847494SEnrico Perla - Sun Microsystems static unsigned int
341548847494SEnrico Perla - Sun Microsystems from_733(unsigned char *s)
341648847494SEnrico Perla - Sun Microsystems {
341748847494SEnrico Perla - Sun Microsystems 	int		i;
341848847494SEnrico Perla - Sun Microsystems 	unsigned int	ret = 0;
341948847494SEnrico Perla - Sun Microsystems 
342048847494SEnrico Perla - Sun Microsystems 	for (i = 0; i < 4; i++)
342148847494SEnrico Perla - Sun Microsystems 		ret |= s[i] << (8 * i);
342248847494SEnrico Perla - Sun Microsystems 
342348847494SEnrico Perla - Sun Microsystems 	return (ret);
342448847494SEnrico Perla - Sun Microsystems }
342548847494SEnrico Perla - Sun Microsystems 
342648847494SEnrico Perla - Sun Microsystems static void
342748847494SEnrico Perla - Sun Microsystems to_733(unsigned char *s, unsigned int val)
342848847494SEnrico Perla - Sun Microsystems {
342948847494SEnrico Perla - Sun Microsystems 	int	i;
343048847494SEnrico Perla - Sun Microsystems 
343148847494SEnrico Perla - Sun Microsystems 	for (i = 0; i < 4; i++)
343248847494SEnrico Perla - Sun Microsystems 		s[i] = s[7-i] = (val >> (8 * i)) & 0xFF;
343348847494SEnrico Perla - Sun Microsystems }
343448847494SEnrico Perla - Sun Microsystems 
343548847494SEnrico Perla - Sun Microsystems /*
3436eeb2c267SToomas Soome  * creates sha1 hash of archive
3437eeb2c267SToomas Soome  */
3438eeb2c267SToomas Soome static int
3439eeb2c267SToomas Soome digest_archive(const char *archive)
3440eeb2c267SToomas Soome {
3441eeb2c267SToomas Soome 	char *archive_hash;
3442eeb2c267SToomas Soome 	char *hash;
3443eeb2c267SToomas Soome 	int ret;
3444eeb2c267SToomas Soome 	FILE *fp;
3445eeb2c267SToomas Soome 
3446eeb2c267SToomas Soome 	(void) asprintf(&archive_hash, "%s.hash", archive);
3447eeb2c267SToomas Soome 	if (archive_hash == NULL)
3448eeb2c267SToomas Soome 		return (BAM_ERROR);
3449eeb2c267SToomas Soome 
3450eeb2c267SToomas Soome 	if ((ret = bootadm_digest(archive, &hash)) == BAM_ERROR) {
3451eeb2c267SToomas Soome 		free(archive_hash);
3452eeb2c267SToomas Soome 		return (ret);
3453eeb2c267SToomas Soome 	}
3454eeb2c267SToomas Soome 
3455eeb2c267SToomas Soome 	fp = fopen(archive_hash, "w");
3456eeb2c267SToomas Soome 	if (fp == NULL) {
3457eeb2c267SToomas Soome 		free(archive_hash);
3458eeb2c267SToomas Soome 		free(hash);
3459eeb2c267SToomas Soome 		return (BAM_ERROR);
3460eeb2c267SToomas Soome 	}
3461eeb2c267SToomas Soome 
3462eeb2c267SToomas Soome 	(void) fprintf(fp, "%s\n", hash);
3463eeb2c267SToomas Soome 	(void) fclose(fp);
3464eeb2c267SToomas Soome 	free(hash);
3465eeb2c267SToomas Soome 	free(archive_hash);
3466eeb2c267SToomas Soome 	return (BAM_SUCCESS);
3467eeb2c267SToomas Soome }
3468eeb2c267SToomas Soome 
3469eeb2c267SToomas Soome /*
347048847494SEnrico Perla - Sun Microsystems  * Extends the current boot archive without recreating it from scratch
347148847494SEnrico Perla - Sun Microsystems  */
347248847494SEnrico Perla - Sun Microsystems static int
347348847494SEnrico Perla - Sun Microsystems extend_iso_archive(char *archive, char *tempname, char *update_dir)
347448847494SEnrico Perla - Sun Microsystems {
347548847494SEnrico Perla - Sun Microsystems 	int			fd = -1, newfd = -1, ret, i;
347648847494SEnrico Perla - Sun Microsystems 	int			next_session = 0, new_size = 0;
347748847494SEnrico Perla - Sun Microsystems 	char			cmdline[3 * PATH_MAX + 64];
347848847494SEnrico Perla - Sun Microsystems 	const char		*func = "extend_iso_archive()";
347948847494SEnrico Perla - Sun Microsystems 	filelist_t		flist = {0};
348048847494SEnrico Perla - Sun Microsystems 	struct iso_pdesc	saved_desc[MAX_IVDs];
348148847494SEnrico Perla - Sun Microsystems 
348248847494SEnrico Perla - Sun Microsystems 	fd = open(archive, O_RDWR);
348348847494SEnrico Perla - Sun Microsystems 	if (fd == -1) {
3484cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
3485*f64ca102SToomas Soome 			bam_error(_("failed to open file: %s: %s\n"),
3486*f64ca102SToomas Soome 			    archive, strerror(errno));
348748847494SEnrico Perla - Sun Microsystems 		goto out_err;
348848847494SEnrico Perla - Sun Microsystems 	}
348948847494SEnrico Perla - Sun Microsystems 
349048847494SEnrico Perla - Sun Microsystems 	/*
349148847494SEnrico Perla - Sun Microsystems 	 * A partial read is likely due to a corrupted file
349248847494SEnrico Perla - Sun Microsystems 	 */
349348847494SEnrico Perla - Sun Microsystems 	ret = pread64(fd, saved_desc, sizeof (saved_desc),
349448847494SEnrico Perla - Sun Microsystems 	    VOLDESC_OFF * CD_BLOCK);
349548847494SEnrico Perla - Sun Microsystems 	if (ret != sizeof (saved_desc)) {
3496cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
3497*f64ca102SToomas Soome 			bam_error(_("read failed for file: %s: %s\n"),
3498*f64ca102SToomas Soome 			    archive, strerror(errno));
349948847494SEnrico Perla - Sun Microsystems 		goto out_err;
350048847494SEnrico Perla - Sun Microsystems 	}
350148847494SEnrico Perla - Sun Microsystems 
350248847494SEnrico Perla - Sun Microsystems 	if (memcmp(saved_desc[0].type, "\1CD001", 6)) {
3503cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
3504*f64ca102SToomas Soome 			bam_error(_("iso descriptor signature for %s is "
3505*f64ca102SToomas Soome 			    "invalid\n"), archive);
350648847494SEnrico Perla - Sun Microsystems 		goto out_err;
350748847494SEnrico Perla - Sun Microsystems 	}
350848847494SEnrico Perla - Sun Microsystems 
350948847494SEnrico Perla - Sun Microsystems 	/*
351048847494SEnrico Perla - Sun Microsystems 	 * Read primary descriptor and locate next_session offset (it should
351148847494SEnrico Perla - Sun Microsystems 	 * point to the end of the archive)
351248847494SEnrico Perla - Sun Microsystems 	 */
351348847494SEnrico Perla - Sun Microsystems 	next_session = P2ROUNDUP(from_733(saved_desc[0].volume_space_size), 16);
351448847494SEnrico Perla - Sun Microsystems 
351548847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s -C 16,%d -M %s %s -o \""
3516cedc7e57SEnrico Perla - Sun Microsystems 	    "%s\" \"%s\" 2>&1", MKISOFS_PATH, next_session, archive,
3517cedc7e57SEnrico Perla - Sun Microsystems 	    MKISO_PARAMS, tempname, update_dir);
351848847494SEnrico Perla - Sun Microsystems 
3519*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: executing: %s\n", func, cmdline));
352048847494SEnrico Perla - Sun Microsystems 
352148847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
352248847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
3523cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose) {
3524*f64ca102SToomas Soome 			bam_error(_("Command '%s' failed while generating "
3525*f64ca102SToomas Soome 			    "multisession archive\n"), cmdline);
3526cedc7e57SEnrico Perla - Sun Microsystems 			dump_errormsg(flist);
3527cedc7e57SEnrico Perla - Sun Microsystems 		}
352848847494SEnrico Perla - Sun Microsystems 		goto out_flist_err;
352948847494SEnrico Perla - Sun Microsystems 	}
353048847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
353148847494SEnrico Perla - Sun Microsystems 
353248847494SEnrico Perla - Sun Microsystems 	newfd = open(tempname, O_RDONLY);
353348847494SEnrico Perla - Sun Microsystems 	if (newfd == -1) {
3534cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
3535*f64ca102SToomas Soome 			bam_error(_("failed to open file: %s: %s\n"),
3536*f64ca102SToomas Soome 			    archive, strerror(errno));
353748847494SEnrico Perla - Sun Microsystems 		goto out_err;
353848847494SEnrico Perla - Sun Microsystems 	}
353948847494SEnrico Perla - Sun Microsystems 
354048847494SEnrico Perla - Sun Microsystems 	ret = pread64(newfd, saved_desc, sizeof (saved_desc),
354148847494SEnrico Perla - Sun Microsystems 	    VOLDESC_OFF * CD_BLOCK);
354248847494SEnrico Perla - Sun Microsystems 	if (ret != sizeof (saved_desc)) {
3543cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
3544*f64ca102SToomas Soome 			bam_error(_("read failed for file: %s: %s\n"),
3545*f64ca102SToomas Soome 			    archive, strerror(errno));
354648847494SEnrico Perla - Sun Microsystems 		goto out_err;
354748847494SEnrico Perla - Sun Microsystems 	}
354848847494SEnrico Perla - Sun Microsystems 
354948847494SEnrico Perla - Sun Microsystems 	if (memcmp(saved_desc[0].type, "\1CD001", 6)) {
3550cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
3551*f64ca102SToomas Soome 			bam_error(_("iso descriptor signature for %s is "
3552*f64ca102SToomas Soome 			    "invalid\n"), archive);
355348847494SEnrico Perla - Sun Microsystems 		goto out_err;
355448847494SEnrico Perla - Sun Microsystems 	}
355548847494SEnrico Perla - Sun Microsystems 
355648847494SEnrico Perla - Sun Microsystems 	new_size = from_733(saved_desc[0].volume_space_size) + next_session;
355748847494SEnrico Perla - Sun Microsystems 	to_733(saved_desc[0].volume_space_size, new_size);
355848847494SEnrico Perla - Sun Microsystems 
355948847494SEnrico Perla - Sun Microsystems 	for (i = 1; i < MAX_IVDs; i++) {
356048847494SEnrico Perla - Sun Microsystems 		if (saved_desc[i].type[0] == (unsigned char)255)
356148847494SEnrico Perla - Sun Microsystems 			break;
356248847494SEnrico Perla - Sun Microsystems 		if (memcmp(saved_desc[i].id, "CD001", 5))
356348847494SEnrico Perla - Sun Microsystems 			break;
356448847494SEnrico Perla - Sun Microsystems 
356548847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
356648847494SEnrico Perla - Sun Microsystems 			bam_print("%s: Updating descriptor entry [%d]\n", func,
356748847494SEnrico Perla - Sun Microsystems 			    i);
356848847494SEnrico Perla - Sun Microsystems 
356948847494SEnrico Perla - Sun Microsystems 		to_733(saved_desc[i].volume_space_size, new_size);
357048847494SEnrico Perla - Sun Microsystems 	}
357148847494SEnrico Perla - Sun Microsystems 
357248847494SEnrico Perla - Sun Microsystems 	ret = pwrite64(fd, saved_desc, DVD_BLOCK, VOLDESC_OFF*CD_BLOCK);
357348847494SEnrico Perla - Sun Microsystems 	if (ret != DVD_BLOCK) {
3574cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
3575*f64ca102SToomas Soome 			bam_error(_("write to file failed: %s: %s\n"),
3576*f64ca102SToomas Soome 			    archive, strerror(errno));
357748847494SEnrico Perla - Sun Microsystems 		goto out_err;
357848847494SEnrico Perla - Sun Microsystems 	}
357948847494SEnrico Perla - Sun Microsystems 	(void) close(newfd);
358048847494SEnrico Perla - Sun Microsystems 	newfd = -1;
358148847494SEnrico Perla - Sun Microsystems 
3582cedc7e57SEnrico Perla - Sun Microsystems 	ret = fsync(fd);
3583cedc7e57SEnrico Perla - Sun Microsystems 	if (ret != 0)
3584cedc7e57SEnrico Perla - Sun Microsystems 		sync();
3585cedc7e57SEnrico Perla - Sun Microsystems 
358648847494SEnrico Perla - Sun Microsystems 	ret = close(fd);
358748847494SEnrico Perla - Sun Microsystems 	if (ret != 0) {
3588cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
3589*f64ca102SToomas Soome 			bam_error(_("failed to close file: %s: %s\n"),
3590*f64ca102SToomas Soome 			    archive, strerror(errno));
359148847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
359248847494SEnrico Perla - Sun Microsystems 	}
359348847494SEnrico Perla - Sun Microsystems 	fd = -1;
359448847494SEnrico Perla - Sun Microsystems 
359548847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s if=%s of=%s bs=32k "
3596cedc7e57SEnrico Perla - Sun Microsystems 	    "seek=%d conv=sync 2>&1", DD_PATH_USR, tempname, archive,
359748847494SEnrico Perla - Sun Microsystems 	    (next_session/16));
359848847494SEnrico Perla - Sun Microsystems 
3599*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: executing: %s\n", func, cmdline));
360048847494SEnrico Perla - Sun Microsystems 
360148847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
360248847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
3603cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
3604*f64ca102SToomas Soome 			bam_error(_("Command '%s' failed while generating "
3605*f64ca102SToomas Soome 			    "multisession archive\n"), cmdline);
360648847494SEnrico Perla - Sun Microsystems 		goto out_flist_err;
360748847494SEnrico Perla - Sun Microsystems 	}
360848847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
360948847494SEnrico Perla - Sun Microsystems 
361048847494SEnrico Perla - Sun Microsystems 	(void) unlink(tempname);
361148847494SEnrico Perla - Sun Microsystems 
3612eeb2c267SToomas Soome 	if (digest_archive(archive) == BAM_ERROR && bam_verbose)
3613eeb2c267SToomas Soome 		bam_print("boot archive hashing failed\n");
3614eeb2c267SToomas Soome 
3615cedc7e57SEnrico Perla - Sun Microsystems 	if (flushfs(bam_root) != 0)
3616cedc7e57SEnrico Perla - Sun Microsystems 		sync();
3617cedc7e57SEnrico Perla - Sun Microsystems 
361848847494SEnrico Perla - Sun Microsystems 	if (bam_verbose)
361948847494SEnrico Perla - Sun Microsystems 		bam_print("boot archive updated successfully\n");
362048847494SEnrico Perla - Sun Microsystems 
362148847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
362248847494SEnrico Perla - Sun Microsystems 
362348847494SEnrico Perla - Sun Microsystems out_flist_err:
362448847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
362548847494SEnrico Perla - Sun Microsystems out_err:
362648847494SEnrico Perla - Sun Microsystems 	if (fd != -1)
362748847494SEnrico Perla - Sun Microsystems 		(void) close(fd);
362848847494SEnrico Perla - Sun Microsystems 	if (newfd != -1)
362948847494SEnrico Perla - Sun Microsystems 		(void) close(newfd);
363048847494SEnrico Perla - Sun Microsystems 	return (BAM_ERROR);
363148847494SEnrico Perla - Sun Microsystems }
363248847494SEnrico Perla - Sun Microsystems 
363348847494SEnrico Perla - Sun Microsystems static int
363448847494SEnrico Perla - Sun Microsystems create_x86_archive(char *archive, char *tempname, char *update_dir)
363548847494SEnrico Perla - Sun Microsystems {
363648847494SEnrico Perla - Sun Microsystems 	int		ret;
363748847494SEnrico Perla - Sun Microsystems 	char		cmdline[3 * PATH_MAX + 64];
363848847494SEnrico Perla - Sun Microsystems 	filelist_t	flist = {0};
363948847494SEnrico Perla - Sun Microsystems 	const char	*func = "create_x86_archive()";
364048847494SEnrico Perla - Sun Microsystems 
364148847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s -o \"%s\" \"%s\" "
3642cedc7e57SEnrico Perla - Sun Microsystems 	    "2>&1", MKISOFS_PATH, MKISO_PARAMS, tempname, update_dir);
364348847494SEnrico Perla - Sun Microsystems 
3644*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: executing: %s\n", func, cmdline));
364548847494SEnrico Perla - Sun Microsystems 
364648847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
364748847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
3648*f64ca102SToomas Soome 		bam_error(_("boot-archive creation FAILED, command: '%s'\n"),
3649*f64ca102SToomas Soome 		    cmdline);
3650cedc7e57SEnrico Perla - Sun Microsystems 		dump_errormsg(flist);
365148847494SEnrico Perla - Sun Microsystems 		filelist_free(&flist);
365248847494SEnrico Perla - Sun Microsystems 		(void) unlink(tempname);
365348847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
365448847494SEnrico Perla - Sun Microsystems 	}
365548847494SEnrico Perla - Sun Microsystems 
365648847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
365748847494SEnrico Perla - Sun Microsystems 
365848847494SEnrico Perla - Sun Microsystems 	if (check_archive(tempname) == BAM_ERROR)
365948847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
366048847494SEnrico Perla - Sun Microsystems 
366148847494SEnrico Perla - Sun Microsystems 	return (do_archive_copy(tempname, archive));
366248847494SEnrico Perla - Sun Microsystems }
366348847494SEnrico Perla - Sun Microsystems 
366448847494SEnrico Perla - Sun Microsystems static int
366548847494SEnrico Perla - Sun Microsystems mkisofs_archive(char *root, int what)
366648847494SEnrico Perla - Sun Microsystems {
366748847494SEnrico Perla - Sun Microsystems 	int		ret;
366848847494SEnrico Perla - Sun Microsystems 	char		temp[PATH_MAX];
366948847494SEnrico Perla - Sun Microsystems 	char 		bootblk[PATH_MAX];
367048847494SEnrico Perla - Sun Microsystems 	char		boot_archive[PATH_MAX];
367148847494SEnrico Perla - Sun Microsystems 
367248847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
367348847494SEnrico Perla - Sun Microsystems 		ret = snprintf(temp, sizeof (temp),
367448847494SEnrico Perla - Sun Microsystems 		    "%s%s%s/amd64/archive-new-%d", root, ARCHIVE_PREFIX,
367548847494SEnrico Perla - Sun Microsystems 		    get_machine(), getpid());
367648847494SEnrico Perla - Sun Microsystems 	else
367748847494SEnrico Perla - Sun Microsystems 		ret = snprintf(temp, sizeof (temp), "%s%s%s/archive-new-%d",
367848847494SEnrico Perla - Sun Microsystems 		    root, ARCHIVE_PREFIX, get_machine(), getpid());
367948847494SEnrico Perla - Sun Microsystems 
368048847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (temp))
368148847494SEnrico Perla - Sun Microsystems 		goto out_path_err;
368248847494SEnrico Perla - Sun Microsystems 
368348847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
368448847494SEnrico Perla - Sun Microsystems 		ret = snprintf(boot_archive, sizeof (boot_archive),
368548847494SEnrico Perla - Sun Microsystems 		    "%s%s%s/amd64%s", root, ARCHIVE_PREFIX, get_machine(),
368648847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_SUFFIX);
368748847494SEnrico Perla - Sun Microsystems 	else
368848847494SEnrico Perla - Sun Microsystems 		ret = snprintf(boot_archive, sizeof (boot_archive),
368948847494SEnrico Perla - Sun Microsystems 		    "%s%s%s%s", root, ARCHIVE_PREFIX, get_machine(),
369048847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_SUFFIX);
369148847494SEnrico Perla - Sun Microsystems 
369248847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (boot_archive))
369348847494SEnrico Perla - Sun Microsystems 		goto out_path_err;
369448847494SEnrico Perla - Sun Microsystems 
369548847494SEnrico Perla - Sun Microsystems 	bam_print("updating %s\n", boot_archive);
369648847494SEnrico Perla - Sun Microsystems 
369748847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET)) {
369848847494SEnrico Perla - Sun Microsystems 		ret = snprintf(bootblk, sizeof (bootblk),
369948847494SEnrico Perla - Sun Microsystems 		    "%s/platform/%s/lib/fs/hsfs/bootblk", root, get_machine());
370048847494SEnrico Perla - Sun Microsystems 		if (ret >= sizeof (bootblk))
370148847494SEnrico Perla - Sun Microsystems 			goto out_path_err;
370248847494SEnrico Perla - Sun Microsystems 
370348847494SEnrico Perla - Sun Microsystems 		ret = create_sparc_archive(boot_archive, temp, bootblk,
370448847494SEnrico Perla - Sun Microsystems 		    get_cachedir(what));
370548847494SEnrico Perla - Sun Microsystems 	} else {
370648847494SEnrico Perla - Sun Microsystems 		if (!is_dir_flag_on(what, NO_MULTI)) {
370748847494SEnrico Perla - Sun Microsystems 			if (bam_verbose)
370848847494SEnrico Perla - Sun Microsystems 				bam_print("Attempting to extend x86 archive: "
370948847494SEnrico Perla - Sun Microsystems 				    "%s\n", boot_archive);
371048847494SEnrico Perla - Sun Microsystems 
371148847494SEnrico Perla - Sun Microsystems 			ret = extend_iso_archive(boot_archive, temp,
371248847494SEnrico Perla - Sun Microsystems 			    get_updatedir(what));
371348847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_SUCCESS) {
371448847494SEnrico Perla - Sun Microsystems 				if (bam_verbose)
371548847494SEnrico Perla - Sun Microsystems 					bam_print("Successfully extended %s\n",
371648847494SEnrico Perla - Sun Microsystems 					    boot_archive);
371748847494SEnrico Perla - Sun Microsystems 
371848847494SEnrico Perla - Sun Microsystems 				(void) rmdir_r(get_updatedir(what));
371948847494SEnrico Perla - Sun Microsystems 				return (BAM_SUCCESS);
372048847494SEnrico Perla - Sun Microsystems 			}
372148847494SEnrico Perla - Sun Microsystems 		}
372248847494SEnrico Perla - Sun Microsystems 		/*
372348847494SEnrico Perla - Sun Microsystems 		 * The boot archive will be recreated from scratch. We get here
372448847494SEnrico Perla - Sun Microsystems 		 * if at least one of these conditions is true:
372548847494SEnrico Perla - Sun Microsystems 		 * - bootadm was called without the -e switch
372648847494SEnrico Perla - Sun Microsystems 		 * - the archive (or the archive cache) doesn't exist
372748847494SEnrico Perla - Sun Microsystems 		 * - archive size is bigger than BA_SIZE_MAX
372848847494SEnrico Perla - Sun Microsystems 		 * - more than COUNT_MAX files need to be updated
372948847494SEnrico Perla - Sun Microsystems 		 * - an error occourred either populating the /updates directory
373048847494SEnrico Perla - Sun Microsystems 		 *   or extend_iso_archive() failed
373148847494SEnrico Perla - Sun Microsystems 		 */
373248847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
373348847494SEnrico Perla - Sun Microsystems 			bam_print("Unable to extend %s... rebuilding archive\n",
373448847494SEnrico Perla - Sun Microsystems 			    boot_archive);
373548847494SEnrico Perla - Sun Microsystems 
373648847494SEnrico Perla - Sun Microsystems 		if (get_updatedir(what)[0] != '\0')
373748847494SEnrico Perla - Sun Microsystems 			(void) rmdir_r(get_updatedir(what));
373848847494SEnrico Perla - Sun Microsystems 
373948847494SEnrico Perla - Sun Microsystems 
374048847494SEnrico Perla - Sun Microsystems 		ret = create_x86_archive(boot_archive, temp,
374148847494SEnrico Perla - Sun Microsystems 		    get_cachedir(what));
374248847494SEnrico Perla - Sun Microsystems 	}
374348847494SEnrico Perla - Sun Microsystems 
3744eeb2c267SToomas Soome 	if (digest_archive(boot_archive) == BAM_ERROR && bam_verbose)
3745eeb2c267SToomas Soome 		bam_print("boot archive hashing failed\n");
3746eeb2c267SToomas Soome 
374748847494SEnrico Perla - Sun Microsystems 	if (ret == BAM_SUCCESS && bam_verbose)
374848847494SEnrico Perla - Sun Microsystems 		bam_print("Successfully created %s\n", boot_archive);
374948847494SEnrico Perla - Sun Microsystems 
375048847494SEnrico Perla - Sun Microsystems 	return (ret);
375148847494SEnrico Perla - Sun Microsystems 
375248847494SEnrico Perla - Sun Microsystems out_path_err:
3753*f64ca102SToomas Soome 	bam_error(_("unable to create path on mountpoint %s, path too long\n"),
3754*f64ca102SToomas Soome 	    root);
375548847494SEnrico Perla - Sun Microsystems 	return (BAM_ERROR);
375648847494SEnrico Perla - Sun Microsystems }
375748847494SEnrico Perla - Sun Microsystems 
37587c478bd9Sstevel@tonic-gate static error_t
37597c478bd9Sstevel@tonic-gate create_ramdisk(char *root)
37607c478bd9Sstevel@tonic-gate {
37617c478bd9Sstevel@tonic-gate 	char *cmdline, path[PATH_MAX];
37627c478bd9Sstevel@tonic-gate 	size_t len;
37637c478bd9Sstevel@tonic-gate 	struct stat sb;
3764cedc7e57SEnrico Perla - Sun Microsystems 	int ret, what, status = BAM_SUCCESS;
376548847494SEnrico Perla - Sun Microsystems 
376648847494SEnrico Perla - Sun Microsystems 	/* If there is mkisofs, use it to create the required archives */
376748847494SEnrico Perla - Sun Microsystems 	if (is_mkisofs()) {
376848847494SEnrico Perla - Sun Microsystems 		for (what = FILE32; what < CACHEDIR_NUM; what++) {
3769cedc7e57SEnrico Perla - Sun Microsystems 			if (has_cachedir(what) && is_dir_flag_on(what,
3770cedc7e57SEnrico Perla - Sun Microsystems 			    NEED_UPDATE)) {
377148847494SEnrico Perla - Sun Microsystems 				ret = mkisofs_archive(root, what);
377248847494SEnrico Perla - Sun Microsystems 				if (ret != 0)
3773cedc7e57SEnrico Perla - Sun Microsystems 					status = BAM_ERROR;
377448847494SEnrico Perla - Sun Microsystems 			}
377548847494SEnrico Perla - Sun Microsystems 		}
3776cedc7e57SEnrico Perla - Sun Microsystems 		return (status);
377748847494SEnrico Perla - Sun Microsystems 	}
37787c478bd9Sstevel@tonic-gate 
37797c478bd9Sstevel@tonic-gate 	/*
378048847494SEnrico Perla - Sun Microsystems 	 * Else setup command args for create_ramdisk.ksh for the UFS archives
3781eeb2c267SToomas Soome 	 * Note: we will not create hash here, CREATE_RAMDISK should create it.
37827c478bd9Sstevel@tonic-gate 	 */
378348847494SEnrico Perla - Sun Microsystems 	if (bam_verbose)
378448847494SEnrico Perla - Sun Microsystems 		bam_print("mkisofs not found, creating UFS archive\n");
378548847494SEnrico Perla - Sun Microsystems 
3786986fd29aSsetje 	(void) snprintf(path, sizeof (path), "%s/%s", root, CREATE_RAMDISK);
37877c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
3788*f64ca102SToomas Soome 		bam_error(_("archive creation file not found: %s: %s\n"),
3789*f64ca102SToomas Soome 		    path, strerror(errno));
37907c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
37917c478bd9Sstevel@tonic-gate 	}
37927c478bd9Sstevel@tonic-gate 
3793cedc7e57SEnrico Perla - Sun Microsystems 	if (is_safe_exec(path) == BAM_ERROR)
3794cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
3795cedc7e57SEnrico Perla - Sun Microsystems 
37967c478bd9Sstevel@tonic-gate 	len = strlen(path) + strlen(root) + 10;	/* room for space + -R */
3797d876c67dSjg 	if (bam_alt_platform)
3798d876c67dSjg 		len += strlen(bam_platform) + strlen("-p ");
37997c478bd9Sstevel@tonic-gate 	cmdline = s_calloc(1, len);
38007c478bd9Sstevel@tonic-gate 
3801d876c67dSjg 	if (bam_alt_platform) {
3802d876c67dSjg 		assert(strlen(root) > 1);
3803d876c67dSjg 		(void) snprintf(cmdline, len, "%s -p %s -R %s",
3804d876c67dSjg 		    path, bam_platform, root);
3805d876c67dSjg 		/* chop off / at the end */
3806d876c67dSjg 		cmdline[strlen(cmdline) - 1] = '\0';
3807d876c67dSjg 	} else if (strlen(root) > 1) {
38087c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s -R %s", path, root);
38097c478bd9Sstevel@tonic-gate 		/* chop off / at the end */
38107c478bd9Sstevel@tonic-gate 		cmdline[strlen(cmdline) - 1] = '\0';
38117c478bd9Sstevel@tonic-gate 	} else
38127c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s", path);
38137c478bd9Sstevel@tonic-gate 
3814986fd29aSsetje 	if (exec_cmd(cmdline, NULL) != 0) {
3815*f64ca102SToomas Soome 		bam_error(_("boot-archive creation FAILED, command: '%s'\n"),
3816*f64ca102SToomas Soome 		    cmdline);
38177c478bd9Sstevel@tonic-gate 		free(cmdline);
38187c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
38197c478bd9Sstevel@tonic-gate 	}
38207c478bd9Sstevel@tonic-gate 	free(cmdline);
38217c478bd9Sstevel@tonic-gate 	/*
3822986fd29aSsetje 	 * The existence of the expected archives used to be
3823986fd29aSsetje 	 * verified here. This check is done in create_ramdisk as
3824986fd29aSsetje 	 * it needs to be in sync with the altroot operated upon.
38257c478bd9Sstevel@tonic-gate 	 */
38267c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
38277c478bd9Sstevel@tonic-gate }
38287c478bd9Sstevel@tonic-gate 
38297c478bd9Sstevel@tonic-gate /*
38307c478bd9Sstevel@tonic-gate  * Checks if target filesystem is on a ramdisk
38317c478bd9Sstevel@tonic-gate  * 1 - is miniroot
38327c478bd9Sstevel@tonic-gate  * 0 - is not
38337c478bd9Sstevel@tonic-gate  * When in doubt assume it is not a ramdisk.
38347c478bd9Sstevel@tonic-gate  */
38357c478bd9Sstevel@tonic-gate static int
38367c478bd9Sstevel@tonic-gate is_ramdisk(char *root)
38377c478bd9Sstevel@tonic-gate {
38387c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
38397c478bd9Sstevel@tonic-gate 	FILE *fp;
38407c478bd9Sstevel@tonic-gate 	int found;
3841b610f78eSvikram 	char mntpt[PATH_MAX];
3842b610f78eSvikram 	char *cp;
38437c478bd9Sstevel@tonic-gate 
38447c478bd9Sstevel@tonic-gate 	/*
38457c478bd9Sstevel@tonic-gate 	 * There are 3 situations where creating archive is
38467c478bd9Sstevel@tonic-gate 	 * of dubious value:
3847b610f78eSvikram 	 *	- create boot_archive on a lofi-mounted boot_archive
38487c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk which is the root filesystem
38497c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk mounted somewhere else
38507c478bd9Sstevel@tonic-gate 	 * The first is not easy to detect and checking for it is not
38517c478bd9Sstevel@tonic-gate 	 * worth it.
38527c478bd9Sstevel@tonic-gate 	 * The other two conditions are handled here
38537c478bd9Sstevel@tonic-gate 	 */
38547c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
38557c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
3856*f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"),
3857*f64ca102SToomas Soome 		    MNTTAB, strerror(errno));
38587c478bd9Sstevel@tonic-gate 		return (0);
38597c478bd9Sstevel@tonic-gate 	}
38607c478bd9Sstevel@tonic-gate 
38617c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
38627c478bd9Sstevel@tonic-gate 
3863b610f78eSvikram 	/*
3864b610f78eSvikram 	 * Remove any trailing / from the mount point
3865b610f78eSvikram 	 */
3866b610f78eSvikram 	(void) strlcpy(mntpt, root, sizeof (mntpt));
3867b610f78eSvikram 	if (strcmp(root, "/") != 0) {
3868b610f78eSvikram 		cp = mntpt + strlen(mntpt) - 1;
3869b610f78eSvikram 		if (*cp == '/')
3870b610f78eSvikram 			*cp = '\0';
3871b610f78eSvikram 	}
38727c478bd9Sstevel@tonic-gate 	found = 0;
38737c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
3874b610f78eSvikram 		if (strcmp(mnt.mnt_mountp, mntpt) == 0) {
38757c478bd9Sstevel@tonic-gate 			found = 1;
38767c478bd9Sstevel@tonic-gate 			break;
38777c478bd9Sstevel@tonic-gate 		}
38787c478bd9Sstevel@tonic-gate 	}
38797c478bd9Sstevel@tonic-gate 
38807c478bd9Sstevel@tonic-gate 	if (!found) {
38817c478bd9Sstevel@tonic-gate 		if (bam_verbose)
3882*f64ca102SToomas Soome 			bam_error(_("alternate root %s not in mnttab\n"),
3883*f64ca102SToomas Soome 			    mntpt);
38847c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
38857c478bd9Sstevel@tonic-gate 		return (0);
38867c478bd9Sstevel@tonic-gate 	}
38877c478bd9Sstevel@tonic-gate 
388808db0dbcSAlexander Eremin 	if (strncmp(mnt.mnt_special, RAMDISK_SPECIAL,
388908db0dbcSAlexander Eremin 	    strlen(RAMDISK_SPECIAL)) == 0) {
38907c478bd9Sstevel@tonic-gate 		if (bam_verbose)
3891*f64ca102SToomas Soome 			bam_error(_("%s is on a ramdisk device\n"), bam_root);
38927c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
38937c478bd9Sstevel@tonic-gate 		return (1);
38947c478bd9Sstevel@tonic-gate 	}
38957c478bd9Sstevel@tonic-gate 
38967c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
38977c478bd9Sstevel@tonic-gate 
38987c478bd9Sstevel@tonic-gate 	return (0);
38997c478bd9Sstevel@tonic-gate }
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate static int
3902986fd29aSsetje is_boot_archive(char *root)
39037c478bd9Sstevel@tonic-gate {
39047c478bd9Sstevel@tonic-gate 	char		path[PATH_MAX];
39057c478bd9Sstevel@tonic-gate 	struct stat	sb;
3906eb2bd662Svikram 	int		error;
3907eb2bd662Svikram 	const char	*fcn = "is_boot_archive()";
39087c478bd9Sstevel@tonic-gate 
39097c478bd9Sstevel@tonic-gate 	/*
3910986fd29aSsetje 	 * We can't create an archive without the create_ramdisk script
39117c478bd9Sstevel@tonic-gate 	 */
3912986fd29aSsetje 	(void) snprintf(path, sizeof (path), "%s/%s", root, CREATE_RAMDISK);
3913eb2bd662Svikram 	error = stat(path, &sb);
3914eb2bd662Svikram 	INJECT_ERROR1("NOT_ARCHIVE_BASED", error = -1);
3915eb2bd662Svikram 	if (error == -1) {
39167c478bd9Sstevel@tonic-gate 		if (bam_verbose)
3917*f64ca102SToomas Soome 			bam_print(_("file not found: %s\n"), path);
3918*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: not a boot archive based Solaris "
3919*f64ca102SToomas Soome 		    "instance: %s\n", fcn, root));
39207c478bd9Sstevel@tonic-gate 		return (0);
39217c478bd9Sstevel@tonic-gate 	}
39227c478bd9Sstevel@tonic-gate 
3923*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: *IS* a boot archive based Solaris instance: %s\n",
3924*f64ca102SToomas Soome 	    fcn, root));
3925986fd29aSsetje 	return (1);
3926986fd29aSsetje }
3927986fd29aSsetje 
39287c478bd9Sstevel@tonic-gate /*
3929986fd29aSsetje  * Need to call this for anything that operates on the GRUB menu
3930963390b4Svikram  * In the x86 live upgrade case the directory /boot/grub may be present
3931963390b4Svikram  * even on pre-newboot BEs. The authoritative way to check for a GRUB target
3932963390b4Svikram  * is to check for the presence of the stage2 binary which is present
3933963390b4Svikram  * only on GRUB targets (even on x86 boot partitions). Checking for the
3934963390b4Svikram  * presence of the multiboot binary is not correct as it is not present
3935963390b4Svikram  * on x86 boot partitions.
3936986fd29aSsetje  */
3937986fd29aSsetje int
3938986fd29aSsetje is_grub(const char *root)
3939986fd29aSsetje {
3940986fd29aSsetje 	char path[PATH_MAX];
3941986fd29aSsetje 	struct stat sb;
3942eb2bd662Svikram 	const char *fcn = "is_grub()";
3943986fd29aSsetje 
3944963390b4Svikram 	(void) snprintf(path, sizeof (path), "%s%s", root, GRUB_STAGE2);
39457c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
3946*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: Missing GRUB directory: %s\n", fcn, path));
39477c478bd9Sstevel@tonic-gate 		return (0);
39487c478bd9Sstevel@tonic-gate 	}
39497c478bd9Sstevel@tonic-gate 
39507c478bd9Sstevel@tonic-gate 	return (1);
39517c478bd9Sstevel@tonic-gate }
39527c478bd9Sstevel@tonic-gate 
3953*f64ca102SToomas Soome int
3954eb2bd662Svikram is_zfs(char *root)
3955eb2bd662Svikram {
3956eb2bd662Svikram 	struct statvfs		vfs;
3957eb2bd662Svikram 	int			ret;
3958eb2bd662Svikram 	const char		*fcn = "is_zfs()";
3959eb2bd662Svikram 
3960eb2bd662Svikram 	ret = statvfs(root, &vfs);
3961eb2bd662Svikram 	INJECT_ERROR1("STATVFS_ZFS", ret = 1);
3962eb2bd662Svikram 	if (ret != 0) {
3963*f64ca102SToomas Soome 		bam_error(_("statvfs failed for %s: %s\n"), root,
3964*f64ca102SToomas Soome 		    strerror(errno));
3965eb2bd662Svikram 		return (0);
3966eb2bd662Svikram 	}
3967eb2bd662Svikram 
3968eb2bd662Svikram 	if (strncmp(vfs.f_basetype, "zfs", strlen("zfs")) == 0) {
3969*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: is a ZFS filesystem: %s\n", fcn, root));
3970eb2bd662Svikram 		return (1);
3971eb2bd662Svikram 	} else {
3972*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: is *NOT* a ZFS filesystem: %s\n", fcn, root));
3973eb2bd662Svikram 		return (0);
3974eb2bd662Svikram 	}
3975eb2bd662Svikram }
3976eb2bd662Svikram 
3977*f64ca102SToomas Soome int
3978eb2bd662Svikram is_pcfs(char *root)
3979eb2bd662Svikram {
3980eb2bd662Svikram 	struct statvfs		vfs;
3981eb2bd662Svikram 	int			ret;
3982eb2bd662Svikram 	const char		*fcn = "is_pcfs()";
3983eb2bd662Svikram 
3984eb2bd662Svikram 	ret = statvfs(root, &vfs);
3985eb2bd662Svikram 	INJECT_ERROR1("STATVFS_PCFS", ret = 1);
3986eb2bd662Svikram 	if (ret != 0) {
3987*f64ca102SToomas Soome 		bam_error(_("statvfs failed for %s: %s\n"), root,
3988*f64ca102SToomas Soome 		    strerror(errno));
3989eb2bd662Svikram 		return (0);
3990eb2bd662Svikram 	}
3991eb2bd662Svikram 
3992eb2bd662Svikram 	if (strncmp(vfs.f_basetype, "pcfs", strlen("pcfs")) == 0) {
3993*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: is a PCFS filesystem: %s\n", fcn, root));
3994eb2bd662Svikram 		return (1);
3995eb2bd662Svikram 	} else {
3996*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: is *NOT* a PCFS filesystem: %s\n",
3997*f64ca102SToomas Soome 		    fcn, root));
3998eb2bd662Svikram 		return (0);
3999eb2bd662Svikram 	}
4000eb2bd662Svikram }
4001eb2bd662Svikram 
4002eb2bd662Svikram static int
40037c478bd9Sstevel@tonic-gate is_readonly(char *root)
40047c478bd9Sstevel@tonic-gate {
4005eb2bd662Svikram 	int		fd;
4006eb2bd662Svikram 	int		error;
4007eb2bd662Svikram 	char		testfile[PATH_MAX];
4008eb2bd662Svikram 	const char	*fcn = "is_readonly()";
40097c478bd9Sstevel@tonic-gate 
40107c478bd9Sstevel@tonic-gate 	/*
4011eb2bd662Svikram 	 * Using statvfs() to check for a read-only filesystem is not
4012eb2bd662Svikram 	 * reliable. The only way to reliably test is to attempt to
4013eb2bd662Svikram 	 * create a file
40147c478bd9Sstevel@tonic-gate 	 */
4015eb2bd662Svikram 	(void) snprintf(testfile, sizeof (testfile), "%s/%s.%d",
4016eb2bd662Svikram 	    root, BOOTADM_RDONLY_TEST, getpid());
40177c478bd9Sstevel@tonic-gate 
4018eb2bd662Svikram 	(void) unlink(testfile);
4019eb2bd662Svikram 
4020eb2bd662Svikram 	errno = 0;
4021eb2bd662Svikram 	fd = open(testfile, O_RDWR|O_CREAT|O_EXCL, 0644);
4022eb2bd662Svikram 	error = errno;
4023eb2bd662Svikram 	INJECT_ERROR2("RDONLY_TEST_ERROR", fd = -1, error = EACCES);
4024eb2bd662Svikram 	if (fd == -1 && error == EROFS) {
4025*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: is a READONLY filesystem: %s\n", fcn, root));
40267c478bd9Sstevel@tonic-gate 		return (1);
4027eb2bd662Svikram 	} else if (fd == -1) {
4028*f64ca102SToomas Soome 		bam_error(_("error during read-only test on %s: %s\n"),
4029*f64ca102SToomas Soome 		    root, strerror(error));
40307c478bd9Sstevel@tonic-gate 	}
40317c478bd9Sstevel@tonic-gate 
4032eb2bd662Svikram 	(void) close(fd);
4033eb2bd662Svikram 	(void) unlink(testfile);
40347c478bd9Sstevel@tonic-gate 
4035*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: is a RDWR filesystem: %s\n", fcn, root));
4036e7cbe64fSgw25295 	return (0);
4037e7cbe64fSgw25295 }
4038e7cbe64fSgw25295 
40397c478bd9Sstevel@tonic-gate static error_t
40407c478bd9Sstevel@tonic-gate update_archive(char *root, char *opt)
40417c478bd9Sstevel@tonic-gate {
40427c478bd9Sstevel@tonic-gate 	error_t ret;
40437c478bd9Sstevel@tonic-gate 
40447c478bd9Sstevel@tonic-gate 	assert(root);
40457c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
40467c478bd9Sstevel@tonic-gate 
404748847494SEnrico Perla - Sun Microsystems 	init_walk_args();
404848847494SEnrico Perla - Sun Microsystems 	(void) umask(022);
404948847494SEnrico Perla - Sun Microsystems 
40507c478bd9Sstevel@tonic-gate 	/*
40516debd3f5SAlexander Eremin 	 * Never update non-BE root in update_all
40526debd3f5SAlexander Eremin 	 */
40536debd3f5SAlexander Eremin 	if (!is_be(root) && bam_update_all)
40546debd3f5SAlexander Eremin 		return (BAM_SUCCESS);
40556debd3f5SAlexander Eremin 	/*
4056986fd29aSsetje 	 * root must belong to a boot archive based OS,
40577c478bd9Sstevel@tonic-gate 	 */
4058986fd29aSsetje 	if (!is_boot_archive(root)) {
4059b610f78eSvikram 		/*
4060b610f78eSvikram 		 * Emit message only if not in context of update_all.
4061b610f78eSvikram 		 * If in update_all, emit only if verbose flag is set.
4062b610f78eSvikram 		 */
4063b610f78eSvikram 		if (!bam_update_all || bam_verbose)
4064*f64ca102SToomas Soome 			bam_print(_("%s: not a boot archive based Solaris "
4065*f64ca102SToomas Soome 			    "instance\n"), root);
406648847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
40677c478bd9Sstevel@tonic-gate 	}
40687c478bd9Sstevel@tonic-gate 
40697c478bd9Sstevel@tonic-gate 	/*
40708c1b6884Sszhou 	 * If smf check is requested when / is writable (can happen
40718c1b6884Sszhou 	 * on first reboot following an upgrade because service
40728c1b6884Sszhou 	 * dependency is messed up), skip the check.
40738c1b6884Sszhou 	 */
407448847494SEnrico Perla - Sun Microsystems 	if (bam_smf_check && !bam_root_readonly && !is_zfs(root))
40758c1b6884Sszhou 		return (BAM_SUCCESS);
40768c1b6884Sszhou 
40778c1b6884Sszhou 	/*
40785eea6091SEnrico Perla - Sun Microsystems 	 * Don't generate archive on ramdisk.
40795eea6091SEnrico Perla - Sun Microsystems 	 */
40805eea6091SEnrico Perla - Sun Microsystems 	if (is_ramdisk(root))
40815eea6091SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
40825eea6091SEnrico Perla - Sun Microsystems 
40835eea6091SEnrico Perla - Sun Microsystems 	/*
40848c1b6884Sszhou 	 * root must be writable. This check applies to alternate
40858c1b6884Sszhou 	 * root (-R option); bam_root_readonly applies to '/' only.
408648847494SEnrico Perla - Sun Microsystems 	 * The behaviour translates into being the one of a 'check'.
40877c478bd9Sstevel@tonic-gate 	 */
408861cb17bdSsetje 	if (!bam_smf_check && !bam_check && is_readonly(root)) {
408948847494SEnrico Perla - Sun Microsystems 		set_flag(RDONLY_FSCHK);
409048847494SEnrico Perla - Sun Microsystems 		bam_check = 1;
40917c478bd9Sstevel@tonic-gate 	}
40927c478bd9Sstevel@tonic-gate 
40937c478bd9Sstevel@tonic-gate 	/*
4094cedc7e57SEnrico Perla - Sun Microsystems 	 * Now check if an update is really needed.
40957c478bd9Sstevel@tonic-gate 	 */
40967c478bd9Sstevel@tonic-gate 	ret = update_required(root);
40977c478bd9Sstevel@tonic-gate 
40987c478bd9Sstevel@tonic-gate 	/*
4099cedc7e57SEnrico Perla - Sun Microsystems 	 * The check command (-n) is *not* a dry run.
41007c478bd9Sstevel@tonic-gate 	 * It only checks if the archive is in sync.
4101cedc7e57SEnrico Perla - Sun Microsystems 	 * A readonly filesystem has to be considered an error only if an update
4102cedc7e57SEnrico Perla - Sun Microsystems 	 * is required.
41037c478bd9Sstevel@tonic-gate 	 */
4104cedc7e57SEnrico Perla - Sun Microsystems 	if (bam_nowrite()) {
410548847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(RDONLY_FSCHK)) {
41065eea6091SEnrico Perla - Sun Microsystems 			bam_check = bam_saved_check;
410748847494SEnrico Perla - Sun Microsystems 			if (ret > 0)
4108*f64ca102SToomas Soome 				bam_error(_("%s filesystem is read-only, "
4109*f64ca102SToomas Soome 				    "skipping archives update\n"), root);
411048847494SEnrico Perla - Sun Microsystems 			if (bam_update_all)
411148847494SEnrico Perla - Sun Microsystems 				return ((ret != 0) ? BAM_ERROR : BAM_SUCCESS);
411248847494SEnrico Perla - Sun Microsystems 		}
411348847494SEnrico Perla - Sun Microsystems 
41147c478bd9Sstevel@tonic-gate 		bam_exit((ret != 0) ? 1 : 0);
41157c478bd9Sstevel@tonic-gate 	}
41167c478bd9Sstevel@tonic-gate 
41177c478bd9Sstevel@tonic-gate 	if (ret == 1) {
41187c478bd9Sstevel@tonic-gate 		/* create the ramdisk */
41197c478bd9Sstevel@tonic-gate 		ret = create_ramdisk(root);
41207c478bd9Sstevel@tonic-gate 	}
4121986fd29aSsetje 
41224a9df875SEnrico Perla - Sun Microsystems 	/*
41234a9df875SEnrico Perla - Sun Microsystems 	 * if the archive is updated, save the new stat data and update the
41244a9df875SEnrico Perla - Sun Microsystems 	 * timestamp file
41254a9df875SEnrico Perla - Sun Microsystems 	 */
4126986fd29aSsetje 	if (ret == 0 && walk_arg.new_nvlp != NULL) {
4127986fd29aSsetje 		savenew(root);
41284a9df875SEnrico Perla - Sun Microsystems 		update_timestamp(root);
4129986fd29aSsetje 	}
4130986fd29aSsetje 
4131986fd29aSsetje 	clear_walk_args();
4132986fd29aSsetje 
41337c478bd9Sstevel@tonic-gate 	return (ret);
41347c478bd9Sstevel@tonic-gate }
41357c478bd9Sstevel@tonic-gate 
413640b108d7SMark J Musante static char *
413740b108d7SMark J Musante find_root_pool()
413840b108d7SMark J Musante {
413940b108d7SMark J Musante 	char *special = get_special("/");
414040b108d7SMark J Musante 	char *p;
414140b108d7SMark J Musante 
414240b108d7SMark J Musante 	if (special == NULL)
414340b108d7SMark J Musante 		return (NULL);
414440b108d7SMark J Musante 
414540b108d7SMark J Musante 	if (*special == '/') {
414640b108d7SMark J Musante 		free(special);
414740b108d7SMark J Musante 		return (NULL);
414840b108d7SMark J Musante 	}
414940b108d7SMark J Musante 
415040b108d7SMark J Musante 	if ((p = strchr(special, '/')) != NULL)
415140b108d7SMark J Musante 		*p = '\0';
415240b108d7SMark J Musante 
415340b108d7SMark J Musante 	return (special);
415440b108d7SMark J Musante }
415540b108d7SMark J Musante 
4156963390b4Svikram static error_t
4157963390b4Svikram synchronize_BE_menu(void)
4158fbac2b2bSvikram {
4159fbac2b2bSvikram 	struct stat	sb;
4160963390b4Svikram 	char		cmdline[PATH_MAX];
4161963390b4Svikram 	char		cksum_line[PATH_MAX];
4162963390b4Svikram 	filelist_t	flist = {0};
4163963390b4Svikram 	char		*old_cksum_str;
4164963390b4Svikram 	char		*old_size_str;
4165963390b4Svikram 	char		*old_file;
4166963390b4Svikram 	char		*curr_cksum_str;
4167963390b4Svikram 	char		*curr_size_str;
4168963390b4Svikram 	char		*curr_file;
41691130146cSGangadhar Mylapuram 	char		*pool = NULL;
41701130146cSGangadhar Mylapuram 	char		*mntpt = NULL;
41711130146cSGangadhar Mylapuram 	zfs_mnted_t	mnted;
4172963390b4Svikram 	FILE		*cfp;
4173963390b4Svikram 	int		found;
4174963390b4Svikram 	int		ret;
4175963390b4Svikram 	const char	*fcn = "synchronize_BE_menu()";
4176fbac2b2bSvikram 
4177*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. No args\n", fcn));
4178fbac2b2bSvikram 
4179963390b4Svikram 	/* Check if findroot enabled LU BE */
4180963390b4Svikram 	if (stat(FINDROOT_INSTALLGRUB, &sb) != 0) {
4181*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: not a Live Upgrade BE\n", fcn));
4182963390b4Svikram 		return (BAM_SUCCESS);
4183fbac2b2bSvikram 	}
4184fbac2b2bSvikram 
4185963390b4Svikram 	if (stat(LU_MENU_CKSUM, &sb) != 0) {
4186*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: checksum file absent: %s\n",
4187*f64ca102SToomas Soome 		    fcn, LU_MENU_CKSUM));
4188963390b4Svikram 		goto menu_sync;
4189fbac2b2bSvikram 	}
4190963390b4Svikram 
4191963390b4Svikram 	cfp = fopen(LU_MENU_CKSUM, "r");
4192963390b4Svikram 	INJECT_ERROR1("CKSUM_FILE_MISSING", cfp = NULL);
4193963390b4Svikram 	if (cfp == NULL) {
4194*f64ca102SToomas Soome 		bam_error(_("failed to read GRUB menu checksum file: %s\n"),
4195*f64ca102SToomas Soome 		    LU_MENU_CKSUM);
4196963390b4Svikram 		goto menu_sync;
4197963390b4Svikram 	}
4198*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: opened checksum file: %s\n", fcn, LU_MENU_CKSUM));
4199963390b4Svikram 
4200963390b4Svikram 	found = 0;
4201963390b4Svikram 	while (s_fgets(cksum_line, sizeof (cksum_line), cfp) != NULL) {
4202963390b4Svikram 		INJECT_ERROR1("MULTIPLE_CKSUM", found = 1);
4203963390b4Svikram 		if (found) {
4204*f64ca102SToomas Soome 			bam_error(_("multiple checksums for GRUB menu in "
4205*f64ca102SToomas Soome 			    "checksum file: %s\n"), LU_MENU_CKSUM);
4206963390b4Svikram 			(void) fclose(cfp);
4207963390b4Svikram 			goto menu_sync;
4208963390b4Svikram 		}
4209963390b4Svikram 		found = 1;
4210963390b4Svikram 	}
4211*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: read checksum file: %s\n", fcn, LU_MENU_CKSUM));
4212963390b4Svikram 
4213963390b4Svikram 
4214963390b4Svikram 	old_cksum_str = strtok(cksum_line, " \t");
4215963390b4Svikram 	old_size_str = strtok(NULL, " \t");
4216963390b4Svikram 	old_file = strtok(NULL, " \t");
4217963390b4Svikram 
4218963390b4Svikram 	INJECT_ERROR1("OLD_CKSUM_NULL", old_cksum_str = NULL);
4219963390b4Svikram 	INJECT_ERROR1("OLD_SIZE_NULL", old_size_str = NULL);
4220963390b4Svikram 	INJECT_ERROR1("OLD_FILE_NULL", old_file = NULL);
4221963390b4Svikram 	if (old_cksum_str == NULL || old_size_str == NULL || old_file == NULL) {
4222*f64ca102SToomas Soome 		bam_error(_("error parsing GRUB menu checksum file: %s\n"),
4223*f64ca102SToomas Soome 		    LU_MENU_CKSUM);
4224963390b4Svikram 		goto menu_sync;
4225963390b4Svikram 	}
4226*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: parsed checksum file: %s\n", fcn, LU_MENU_CKSUM));
4227963390b4Svikram 
4228963390b4Svikram 	/* Get checksum of current menu */
422940b108d7SMark J Musante 	pool = find_root_pool();
42301130146cSGangadhar Mylapuram 	if (pool) {
42311130146cSGangadhar Mylapuram 		mntpt = mount_top_dataset(pool, &mnted);
42321130146cSGangadhar Mylapuram 		if (mntpt == NULL) {
4233*f64ca102SToomas Soome 			bam_error(_("failed to mount top dataset for %s\n"),
4234*f64ca102SToomas Soome 			    pool);
423540b108d7SMark J Musante 			free(pool);
42361130146cSGangadhar Mylapuram 			return (BAM_ERROR);
42371130146cSGangadhar Mylapuram 		}
42381130146cSGangadhar Mylapuram 		(void) snprintf(cmdline, sizeof (cmdline), "%s %s%s",
42391130146cSGangadhar Mylapuram 		    CKSUM, mntpt, GRUB_MENU);
42401130146cSGangadhar Mylapuram 	} else {
42411130146cSGangadhar Mylapuram 		(void) snprintf(cmdline, sizeof (cmdline), "%s %s",
42421130146cSGangadhar Mylapuram 		    CKSUM, GRUB_MENU);
42431130146cSGangadhar Mylapuram 	}
4244963390b4Svikram 	ret = exec_cmd(cmdline, &flist);
42451130146cSGangadhar Mylapuram 	if (pool) {
42461130146cSGangadhar Mylapuram 		(void) umount_top_dataset(pool, mnted, mntpt);
42471130146cSGangadhar Mylapuram 		free(pool);
42481130146cSGangadhar Mylapuram 	}
4249963390b4Svikram 	INJECT_ERROR1("GET_CURR_CKSUM", ret = 1);
4250963390b4Svikram 	if (ret != 0) {
4251*f64ca102SToomas Soome 		bam_error(_("error generating checksum of GRUB menu\n"));
4252963390b4Svikram 		return (BAM_ERROR);
4253963390b4Svikram 	}
4254*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: successfully generated checksum\n", fcn));
4255963390b4Svikram 
4256963390b4Svikram 	INJECT_ERROR1("GET_CURR_CKSUM_OUTPUT", flist.head = NULL);
4257963390b4Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
4258*f64ca102SToomas Soome 		bam_error(_("bad checksum generated for GRUB menu\n"));
4259963390b4Svikram 		filelist_free(&flist);
4260963390b4Svikram 		return (BAM_ERROR);
4261963390b4Svikram 	}
4262*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: generated checksum output valid\n", fcn));
4263963390b4Svikram 
4264963390b4Svikram 	curr_cksum_str = strtok(flist.head->line, " \t");
4265963390b4Svikram 	curr_size_str = strtok(NULL, " \t");
4266963390b4Svikram 	curr_file = strtok(NULL, " \t");
4267963390b4Svikram 
4268963390b4Svikram 	INJECT_ERROR1("CURR_CKSUM_NULL", curr_cksum_str = NULL);
4269963390b4Svikram 	INJECT_ERROR1("CURR_SIZE_NULL", curr_size_str = NULL);
4270963390b4Svikram 	INJECT_ERROR1("CURR_FILE_NULL", curr_file = NULL);
4271963390b4Svikram 	if (curr_cksum_str == NULL || curr_size_str == NULL ||
4272963390b4Svikram 	    curr_file == NULL) {
4273*f64ca102SToomas Soome 		bam_error(_("error parsing checksum generated "
4274*f64ca102SToomas Soome 		    "for GRUB menu\n"));
4275963390b4Svikram 		filelist_free(&flist);
4276963390b4Svikram 		return (BAM_ERROR);
4277963390b4Svikram 	}
4278*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: successfully parsed generated checksum\n", fcn));
4279963390b4Svikram 
4280963390b4Svikram 	if (strcmp(old_cksum_str, curr_cksum_str) == 0 &&
4281963390b4Svikram 	    strcmp(old_size_str, curr_size_str) == 0 &&
4282963390b4Svikram 	    strcmp(old_file, curr_file) == 0) {
4283963390b4Svikram 		filelist_free(&flist);
4284*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: no change in checksum of GRUB menu\n", fcn));
4285963390b4Svikram 		return (BAM_SUCCESS);
4286963390b4Svikram 	}
4287963390b4Svikram 
4288963390b4Svikram 	filelist_free(&flist);
4289963390b4Svikram 
4290963390b4Svikram 	/* cksum doesn't match - the menu has changed */
4291*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: checksum of GRUB menu has changed\n", fcn));
4292963390b4Svikram 
4293963390b4Svikram menu_sync:
4294*f64ca102SToomas Soome 	bam_print(_("propagating updated GRUB menu\n"));
4295963390b4Svikram 
4296963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
4297a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s yes > /dev/null'",
4298963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, GRUB_MENU);
4299963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
4300963390b4Svikram 	INJECT_ERROR1("PROPAGATE_MENU", ret = 1);
4301963390b4Svikram 	if (ret != 0) {
4302*f64ca102SToomas Soome 		bam_error(_("error propagating updated GRUB menu\n"));
4303963390b4Svikram 		return (BAM_ERROR);
4304963390b4Svikram 	}
4305*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: successfully propagated GRUB menu\n", fcn));
4306963390b4Svikram 
4307a6968364Svikram 	(void) snprintf(cmdline, sizeof (cmdline), "/bin/cp %s %s > /dev/null",
4308963390b4Svikram 	    GRUB_MENU, GRUB_BACKUP_MENU);
4309963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
4310963390b4Svikram 	INJECT_ERROR1("CREATE_BACKUP", ret = 1);
4311963390b4Svikram 	if (ret != 0) {
4312*f64ca102SToomas Soome 		bam_error(_("failed to create backup for GRUB menu: %s\n"),
4313*f64ca102SToomas Soome 		    GRUB_BACKUP_MENU);
4314963390b4Svikram 		return (BAM_ERROR);
4315963390b4Svikram 	}
4316*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: successfully created backup GRUB menu: %s\n",
4317*f64ca102SToomas Soome 	    fcn, GRUB_BACKUP_MENU));
4318963390b4Svikram 
4319963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
4320a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s no > /dev/null'",
4321963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, GRUB_BACKUP_MENU);
4322963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
4323963390b4Svikram 	INJECT_ERROR1("PROPAGATE_BACKUP", ret = 1);
4324963390b4Svikram 	if (ret != 0) {
4325*f64ca102SToomas Soome 		bam_error(_("error propagating backup GRUB menu: %s\n"),
4326*f64ca102SToomas Soome 		    GRUB_BACKUP_MENU);
4327963390b4Svikram 		return (BAM_ERROR);
4328963390b4Svikram 	}
4329*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: successfully propagated backup GRUB menu: %s\n",
4330*f64ca102SToomas Soome 	    fcn, GRUB_BACKUP_MENU));
4331963390b4Svikram 
4332963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s > %s",
4333963390b4Svikram 	    CKSUM, GRUB_MENU, LU_MENU_CKSUM);
4334963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
4335963390b4Svikram 	INJECT_ERROR1("CREATE_CKSUM_FILE", ret = 1);
4336963390b4Svikram 	if (ret != 0) {
4337*f64ca102SToomas Soome 		bam_error(_("failed to write GRUB menu checksum file: %s\n"),
4338*f64ca102SToomas Soome 		    LU_MENU_CKSUM);
4339963390b4Svikram 		return (BAM_ERROR);
4340963390b4Svikram 	}
4341*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: successfully created checksum file: %s\n",
4342*f64ca102SToomas Soome 	    fcn, LU_MENU_CKSUM));
4343963390b4Svikram 
4344963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
4345a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s no > /dev/null'",
4346963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, LU_MENU_CKSUM);
4347963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
4348963390b4Svikram 	INJECT_ERROR1("PROPAGATE_MENU_CKSUM_FILE", ret = 1);
4349963390b4Svikram 	if (ret != 0) {
4350*f64ca102SToomas Soome 		bam_error(_("error propagating GRUB menu checksum file: %s\n"),
4351*f64ca102SToomas Soome 		    LU_MENU_CKSUM);
4352963390b4Svikram 		return (BAM_ERROR);
4353963390b4Svikram 	}
4354*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: successfully propagated checksum file: %s\n",
4355*f64ca102SToomas Soome 	    fcn, LU_MENU_CKSUM));
4356963390b4Svikram 
4357963390b4Svikram 	return (BAM_SUCCESS);
4358fbac2b2bSvikram }
4359fbac2b2bSvikram 
43607c478bd9Sstevel@tonic-gate static error_t
43617c478bd9Sstevel@tonic-gate update_all(char *root, char *opt)
43627c478bd9Sstevel@tonic-gate {
43637c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
43647c478bd9Sstevel@tonic-gate 	struct stat sb;
43657c478bd9Sstevel@tonic-gate 	FILE *fp;
43667c478bd9Sstevel@tonic-gate 	char multibt[PATH_MAX];
4367986fd29aSsetje 	char creatram[PATH_MAX];
43687c478bd9Sstevel@tonic-gate 	error_t ret = BAM_SUCCESS;
43697c478bd9Sstevel@tonic-gate 
437040541d5dSvikram 	assert(root);
43717c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
43727c478bd9Sstevel@tonic-gate 
437340541d5dSvikram 	if (bam_rootlen != 1 || *root != '/') {
437440541d5dSvikram 		elide_trailing_slash(root, multibt, sizeof (multibt));
4375*f64ca102SToomas Soome 		bam_error(_("an alternate root (%s) cannot be used with this "
4376*f64ca102SToomas Soome 		    "sub-command\n"), multibt);
437740541d5dSvikram 		return (BAM_ERROR);
437840541d5dSvikram 	}
437940541d5dSvikram 
43807c478bd9Sstevel@tonic-gate 	/*
43817c478bd9Sstevel@tonic-gate 	 * First update archive for current root
43827c478bd9Sstevel@tonic-gate 	 */
43837c478bd9Sstevel@tonic-gate 	if (update_archive(root, opt) != BAM_SUCCESS)
43847c478bd9Sstevel@tonic-gate 		ret = BAM_ERROR;
438598892a30Snadkarni 
438698892a30Snadkarni 	if (ret == BAM_ERROR)
438798892a30Snadkarni 		goto out;
43887c478bd9Sstevel@tonic-gate 
43897c478bd9Sstevel@tonic-gate 	/*
43907c478bd9Sstevel@tonic-gate 	 * Now walk the mount table, performing archive update
43917c478bd9Sstevel@tonic-gate 	 * for all mounted Newboot root filesystems
43927c478bd9Sstevel@tonic-gate 	 */
43937c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
43947c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
4395*f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"),
4396*f64ca102SToomas Soome 		    MNTTAB, strerror(errno));
4397b610f78eSvikram 		ret = BAM_ERROR;
4398b610f78eSvikram 		goto out;
43997c478bd9Sstevel@tonic-gate 	}
44007c478bd9Sstevel@tonic-gate 
44017c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
44027c478bd9Sstevel@tonic-gate 
44037c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
44047c478bd9Sstevel@tonic-gate 		if (mnt.mnt_special == NULL)
44057c478bd9Sstevel@tonic-gate 			continue;
4406f904d32dSJerry Gilliam 		if ((strcmp(mnt.mnt_fstype, MNTTYPE_ZFS) != 0) &&
4407f904d32dSJerry Gilliam 		    (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0))
44087c478bd9Sstevel@tonic-gate 			continue;
44097c478bd9Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, "/") == 0)
44107c478bd9Sstevel@tonic-gate 			continue;
44117c478bd9Sstevel@tonic-gate 
4412986fd29aSsetje 		(void) snprintf(creatram, sizeof (creatram), "%s/%s",
4413986fd29aSsetje 		    mnt.mnt_mountp, CREATE_RAMDISK);
44147c478bd9Sstevel@tonic-gate 
4415986fd29aSsetje 		if (stat(creatram, &sb) == -1)
44167c478bd9Sstevel@tonic-gate 			continue;
44177c478bd9Sstevel@tonic-gate 
44187c478bd9Sstevel@tonic-gate 		/*
44197c478bd9Sstevel@tonic-gate 		 * We put a trailing slash to be consistent with root = "/"
44207c478bd9Sstevel@tonic-gate 		 * case, such that we don't have to print // in some cases.
44217c478bd9Sstevel@tonic-gate 		 */
44227c478bd9Sstevel@tonic-gate 		(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
44237c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp);
44247c478bd9Sstevel@tonic-gate 		bam_rootlen = strlen(rootbuf);
4425ae115bc7Smrj 
4426ae115bc7Smrj 		/*
4427ae115bc7Smrj 		 * It's possible that other mounts may be an alternate boot
4428ae115bc7Smrj 		 * architecture, so check it again.
4429ae115bc7Smrj 		 */
4430eb2bd662Svikram 		if ((get_boot_cap(rootbuf) != BAM_SUCCESS) ||
4431ae115bc7Smrj 		    (update_archive(rootbuf, opt) != BAM_SUCCESS))
44327c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
44337c478bd9Sstevel@tonic-gate 	}
44347c478bd9Sstevel@tonic-gate 
44357c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
44367c478bd9Sstevel@tonic-gate 
4437b610f78eSvikram out:
4438fbac2b2bSvikram 	/*
4439963390b4Svikram 	 * We no longer use biosdev for Live Upgrade. Hence
4440963390b4Svikram 	 * there is no need to defer (to shutdown time) any fdisk
4441963390b4Svikram 	 * updates
4442fbac2b2bSvikram 	 */
4443963390b4Svikram 	if (stat(GRUB_fdisk, &sb) == 0 || stat(GRUB_fdisk_target, &sb) == 0) {
4444*f64ca102SToomas Soome 		bam_error(_("Deferred FDISK update file(s) found: %s, %s. "
4445*f64ca102SToomas Soome 		    "Not supported.\n"), GRUB_fdisk, GRUB_fdisk_target);
4446fbac2b2bSvikram 	}
4447fbac2b2bSvikram 
4448963390b4Svikram 	/*
4449963390b4Svikram 	 * If user has updated menu in current BE, propagate the
4450963390b4Svikram 	 * updates to all BEs.
4451963390b4Svikram 	 */
445219397407SSherry Moore 	if (sync_menu && synchronize_BE_menu() != BAM_SUCCESS)
4453963390b4Svikram 		ret = BAM_ERROR;
4454963390b4Svikram 
44557c478bd9Sstevel@tonic-gate 	return (ret);
44567c478bd9Sstevel@tonic-gate }
44577c478bd9Sstevel@tonic-gate 
44587c478bd9Sstevel@tonic-gate static void
44597c478bd9Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp)
44607c478bd9Sstevel@tonic-gate {
44617c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
44627c478bd9Sstevel@tonic-gate 		mp->start = lp;
44637c478bd9Sstevel@tonic-gate 	} else {
44647c478bd9Sstevel@tonic-gate 		mp->end->next = lp;
44658c1b6884Sszhou 		lp->prev = mp->end;
44667c478bd9Sstevel@tonic-gate 	}
44677c478bd9Sstevel@tonic-gate 	mp->end = lp;
44687c478bd9Sstevel@tonic-gate }
44697c478bd9Sstevel@tonic-gate 
4470eb2bd662Svikram void
44718c1b6884Sszhou unlink_line(menu_t *mp, line_t *lp)
44728c1b6884Sszhou {
44738c1b6884Sszhou 	/* unlink from list */
44748c1b6884Sszhou 	if (lp->prev)
44758c1b6884Sszhou 		lp->prev->next = lp->next;
44768c1b6884Sszhou 	else
44778c1b6884Sszhou 		mp->start = lp->next;
44788c1b6884Sszhou 	if (lp->next)
44798c1b6884Sszhou 		lp->next->prev = lp->prev;
44808c1b6884Sszhou 	else
44818c1b6884Sszhou 		mp->end = lp->prev;
44828c1b6884Sszhou }
44838c1b6884Sszhou 
44848c1b6884Sszhou static entry_t *
44858c1b6884Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end)
44868c1b6884Sszhou {
44878c1b6884Sszhou 	entry_t *ent, *prev;
4488eb2bd662Svikram 	const char *fcn = "boot_entry_new()";
4489eb2bd662Svikram 
4490eb2bd662Svikram 	assert(mp);
4491eb2bd662Svikram 	assert(start);
4492eb2bd662Svikram 	assert(end);
44938c1b6884Sszhou 
44948c1b6884Sszhou 	ent = s_calloc(1, sizeof (entry_t));
4495*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: new boot entry alloced\n", fcn));
44968c1b6884Sszhou 	ent->start = start;
44978c1b6884Sszhou 	ent->end = end;
44988c1b6884Sszhou 
44998c1b6884Sszhou 	if (mp->entries == NULL) {
45008c1b6884Sszhou 		mp->entries = ent;
4501*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: (first) new boot entry created\n", fcn));
45028c1b6884Sszhou 		return (ent);
45038c1b6884Sszhou 	}
45048c1b6884Sszhou 
45058c1b6884Sszhou 	prev = mp->entries;
45068c1b6884Sszhou 	while (prev->next)
45078c1b6884Sszhou 		prev = prev->next;
45088c1b6884Sszhou 	prev->next = ent;
45098c1b6884Sszhou 	ent->prev = prev;
4510*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: new boot entry linked in\n", fcn));
45118c1b6884Sszhou 	return (ent);
45128c1b6884Sszhou }
45138c1b6884Sszhou 
45148c1b6884Sszhou static void
45158c1b6884Sszhou boot_entry_addline(entry_t *ent, line_t *lp)
45168c1b6884Sszhou {
45178c1b6884Sszhou 	if (ent)
45188c1b6884Sszhou 		ent->end = lp;
45198c1b6884Sszhou }
45208c1b6884Sszhou 
45217c478bd9Sstevel@tonic-gate /*
4522843e1988Sjohnlev  * Check whether cmd matches the one indexed by which, and whether arg matches
4523843e1988Sjohnlev  * str.  which must be either KERNEL_CMD or MODULE_CMD, and a match to the
4524843e1988Sjohnlev  * respective *_DOLLAR_CMD is also acceptable.  The arg is searched using
4525843e1988Sjohnlev  * strstr(), so it can be a partial match.
4526843e1988Sjohnlev  */
4527843e1988Sjohnlev static int
4528843e1988Sjohnlev check_cmd(const char *cmd, const int which, const char *arg, const char *str)
4529843e1988Sjohnlev {
4530eb2bd662Svikram 	int			ret;
4531eb2bd662Svikram 	const char		*fcn = "check_cmd()";
4532eb2bd662Svikram 
4533*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, arg, str));
4534eb2bd662Svikram 
4535eac223ccSWilliam Kucharski 	if (cmd != NULL) {
4536843e1988Sjohnlev 		if ((strcmp(cmd, menu_cmds[which]) != 0) &&
4537843e1988Sjohnlev 		    (strcmp(cmd, menu_cmds[which + 1]) != 0)) {
4538*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: command %s does not match %s\n",
4539eb2bd662Svikram 			    fcn, cmd, menu_cmds[which]));
4540843e1988Sjohnlev 			return (0);
4541843e1988Sjohnlev 		}
4542eb2bd662Svikram 		ret = (strstr(arg, str) != NULL);
4543eac223ccSWilliam Kucharski 	} else
4544eac223ccSWilliam Kucharski 		ret = 0;
4545eb2bd662Svikram 
4546eb2bd662Svikram 	if (ret) {
4547*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
4548eb2bd662Svikram 	} else {
4549*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
4550eb2bd662Svikram 	}
4551eb2bd662Svikram 
4552eb2bd662Svikram 	return (ret);
4553eb2bd662Svikram }
4554eb2bd662Svikram 
4555eb2bd662Svikram static error_t
4556eb2bd662Svikram kernel_parser(entry_t *entry, char *cmd, char *arg, int linenum)
4557eb2bd662Svikram {
4558eb2bd662Svikram 	const char		*fcn  = "kernel_parser()";
4559eb2bd662Svikram 
4560eb2bd662Svikram 	assert(entry);
4561eb2bd662Svikram 	assert(cmd);
4562eb2bd662Svikram 	assert(arg);
4563eb2bd662Svikram 
4564eb2bd662Svikram 	if (strcmp(cmd, menu_cmds[KERNEL_CMD]) != 0 &&
4565eb2bd662Svikram 	    strcmp(cmd, menu_cmds[KERNEL_DOLLAR_CMD]) != 0) {
4566*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: not a kernel command: %s\n", fcn, cmd));
4567eb2bd662Svikram 		return (BAM_ERROR);
4568eb2bd662Svikram 	}
4569eb2bd662Svikram 
4570eb2bd662Svikram 	if (strncmp(arg, DIRECT_BOOT_32, sizeof (DIRECT_BOOT_32) - 1) == 0) {
4571*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting DBOOT|DBOOT_32 flag: %s\n",
4572*f64ca102SToomas Soome 		    fcn, arg));
4573eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_32BIT;
4574eb2bd662Svikram 	} else if (strncmp(arg, DIRECT_BOOT_KERNEL,
4575eb2bd662Svikram 	    sizeof (DIRECT_BOOT_KERNEL) - 1) == 0) {
4576*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting DBOOT flag: %s\n", fcn, arg));
4577eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT;
4578eb2bd662Svikram 	} else if (strncmp(arg, DIRECT_BOOT_64,
4579eb2bd662Svikram 	    sizeof (DIRECT_BOOT_64) - 1) == 0) {
4580*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting DBOOT|DBOOT_64 flag: %s\n",
4581*f64ca102SToomas Soome 		    fcn, arg));
4582eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_64BIT;
4583eb2bd662Svikram 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_KERNEL,
4584eb2bd662Svikram 	    sizeof (DIRECT_BOOT_FAILSAFE_KERNEL) - 1) == 0) {
4585*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting DBOOT|DBOOT_FAILSAFE flag: %s\n",
4586*f64ca102SToomas Soome 		    fcn, arg));
4587eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE;
4588bbcc54bdSEnrico Perla - Sun Microsystems 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_32,
4589bbcc54bdSEnrico Perla - Sun Microsystems 	    sizeof (DIRECT_BOOT_FAILSAFE_32) - 1) == 0) {
4590*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting DBOOT|DBOOT_FAILSAFE|DBOOT_32 "
4591*f64ca102SToomas Soome 		    "flag: %s\n", fcn, arg));
4592bbcc54bdSEnrico Perla - Sun Microsystems 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE
4593bbcc54bdSEnrico Perla - Sun Microsystems 		    | BAM_ENTRY_32BIT;
4594bbcc54bdSEnrico Perla - Sun Microsystems 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_64,
4595bbcc54bdSEnrico Perla - Sun Microsystems 	    sizeof (DIRECT_BOOT_FAILSAFE_64) - 1) == 0) {
4596*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting DBOOT|DBOOT_FAILSAFE|DBOOT_64 "
4597*f64ca102SToomas Soome 		    "flag: %s\n", fcn, arg));
4598bbcc54bdSEnrico Perla - Sun Microsystems 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE
4599bbcc54bdSEnrico Perla - Sun Microsystems 		    | BAM_ENTRY_64BIT;
4600eb2bd662Svikram 	} else if (strncmp(arg, MULTI_BOOT, sizeof (MULTI_BOOT) - 1) == 0) {
4601*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting MULTIBOOT flag: %s\n", fcn, arg));
4602eb2bd662Svikram 		entry->flags |= BAM_ENTRY_MULTIBOOT;
4603eb2bd662Svikram 	} else if (strncmp(arg, MULTI_BOOT_FAILSAFE,
4604eb2bd662Svikram 	    sizeof (MULTI_BOOT_FAILSAFE) - 1) == 0) {
4605*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting MULTIBOOT|MULTIBOOT_FAILSAFE "
4606*f64ca102SToomas Soome 		    "flag: %s\n", fcn, arg));
4607eb2bd662Svikram 		entry->flags |= BAM_ENTRY_MULTIBOOT | BAM_ENTRY_FAILSAFE;
4608eb2bd662Svikram 	} else if (strstr(arg, XEN_KERNEL_SUBSTR)) {
4609*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting XEN HV flag: %s\n", fcn, arg));
4610eb2bd662Svikram 		entry->flags |= BAM_ENTRY_HV;
4611eb2bd662Svikram 	} else if (!(entry->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU))) {
4612*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: is HAND kernel flag: %s\n", fcn, arg));
4613eb2bd662Svikram 		return (BAM_ERROR);
461437eb779cSVikram Hegde 	} else if (strncmp(arg, KERNEL_PREFIX, strlen(KERNEL_PREFIX)) == 0 &&
461537eb779cSVikram Hegde 	    strstr(arg, UNIX_SPACE)) {
461637eb779cSVikram Hegde 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_32BIT;
461737eb779cSVikram Hegde 	} else if (strncmp(arg, KERNEL_PREFIX, strlen(KERNEL_PREFIX)) == 0 &&
461837eb779cSVikram Hegde 	    strstr(arg, AMD_UNIX_SPACE)) {
461937eb779cSVikram Hegde 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_64BIT;
4620eb2bd662Svikram 	} else {
4621*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: is UNKNOWN kernel entry: %s\n", fcn, arg));
4622*f64ca102SToomas Soome 		bam_error(_("kernel command on line %d not recognized.\n"),
4623*f64ca102SToomas Soome 		    linenum);
4624eb2bd662Svikram 		return (BAM_ERROR);
4625eb2bd662Svikram 	}
4626eb2bd662Svikram 
4627eb2bd662Svikram 	return (BAM_SUCCESS);
4628eb2bd662Svikram }
4629eb2bd662Svikram 
4630eb2bd662Svikram static error_t
4631eb2bd662Svikram module_parser(entry_t *entry, char *cmd, char *arg, int linenum)
4632eb2bd662Svikram {
4633eb2bd662Svikram 	const char		*fcn = "module_parser()";
4634eb2bd662Svikram 
4635eb2bd662Svikram 	assert(entry);
4636eb2bd662Svikram 	assert(cmd);
4637eb2bd662Svikram 	assert(arg);
4638eb2bd662Svikram 
4639eb2bd662Svikram 	if (strcmp(cmd, menu_cmds[MODULE_CMD]) != 0 &&
4640eb2bd662Svikram 	    strcmp(cmd, menu_cmds[MODULE_DOLLAR_CMD]) != 0) {
4641*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: not module cmd: %s\n", fcn, cmd));
4642eb2bd662Svikram 		return (BAM_ERROR);
4643eb2bd662Svikram 	}
4644eb2bd662Svikram 
4645eb2bd662Svikram 	if (strcmp(arg, DIRECT_BOOT_ARCHIVE) == 0 ||
4646eb2bd662Svikram 	    strcmp(arg, DIRECT_BOOT_ARCHIVE_32) == 0 ||
4647eb2bd662Svikram 	    strcmp(arg, DIRECT_BOOT_ARCHIVE_64) == 0 ||
4648eb2bd662Svikram 	    strcmp(arg, MULTIBOOT_ARCHIVE) == 0 ||
4649eb2bd662Svikram 	    strcmp(arg, FAILSAFE_ARCHIVE) == 0 ||
4650bbcc54bdSEnrico Perla - Sun Microsystems 	    strcmp(arg, FAILSAFE_ARCHIVE_32) == 0 ||
4651bbcc54bdSEnrico Perla - Sun Microsystems 	    strcmp(arg, FAILSAFE_ARCHIVE_64) == 0 ||
4652eb2bd662Svikram 	    strcmp(arg, XEN_KERNEL_MODULE_LINE) == 0 ||
4653eb2bd662Svikram 	    strcmp(arg, XEN_KERNEL_MODULE_LINE_ZFS) == 0) {
4654*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: bootadm or LU module cmd: %s\n", fcn, arg));
4655eb2bd662Svikram 		return (BAM_SUCCESS);
4656eb2bd662Svikram 	} else if (!(entry->flags & BAM_ENTRY_BOOTADM) &&
4657eb2bd662Svikram 	    !(entry->flags & BAM_ENTRY_LU)) {
4658eb2bd662Svikram 		/* don't emit warning for hand entries */
4659*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: is HAND module: %s\n", fcn, arg));
4660eb2bd662Svikram 		return (BAM_ERROR);
4661eb2bd662Svikram 	} else {
4662*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: is UNKNOWN module: %s\n", fcn, arg));
4663*f64ca102SToomas Soome 		bam_error(_("module command on line %d not recognized.\n"),
4664*f64ca102SToomas Soome 		    linenum);
4665eb2bd662Svikram 		return (BAM_ERROR);
4666eb2bd662Svikram 	}
4667843e1988Sjohnlev }
4668843e1988Sjohnlev 
4669843e1988Sjohnlev /*
46707c478bd9Sstevel@tonic-gate  * A line in menu.lst looks like
46717c478bd9Sstevel@tonic-gate  * [ ]*<cmd>[ \t=]*<arg>*
46727c478bd9Sstevel@tonic-gate  */
46737c478bd9Sstevel@tonic-gate static void
46747c478bd9Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum)
46757c478bd9Sstevel@tonic-gate {
46767c478bd9Sstevel@tonic-gate 	/*
46777c478bd9Sstevel@tonic-gate 	 * save state across calls. This is so that
46787c478bd9Sstevel@tonic-gate 	 * header gets the right entry# after title has
46797c478bd9Sstevel@tonic-gate 	 * been processed
46807c478bd9Sstevel@tonic-gate 	 */
46818c1b6884Sszhou 	static line_t *prev = NULL;
46828c1b6884Sszhou 	static entry_t *curr_ent = NULL;
4683ae115bc7Smrj 	static int in_liveupgrade = 0;
4684772d6a58SWilliam Kucharski 	static int is_libbe_ent = 0;
46857c478bd9Sstevel@tonic-gate 
46867c478bd9Sstevel@tonic-gate 	line_t	*lp;
46877c478bd9Sstevel@tonic-gate 	char *cmd, *sep, *arg;
46887c478bd9Sstevel@tonic-gate 	char save, *cp, *line;
46897c478bd9Sstevel@tonic-gate 	menu_flag_t flag = BAM_INVALID;
4690eb2bd662Svikram 	const char *fcn = "line_parser()";
46917c478bd9Sstevel@tonic-gate 
4692110570ceSToomas Soome 	cmd = NULL;
46937c478bd9Sstevel@tonic-gate 	if (str == NULL) {
46947c478bd9Sstevel@tonic-gate 		return;
46957c478bd9Sstevel@tonic-gate 	}
46967c478bd9Sstevel@tonic-gate 
46977c478bd9Sstevel@tonic-gate 	/*
46987c478bd9Sstevel@tonic-gate 	 * First save a copy of the entire line.
46997c478bd9Sstevel@tonic-gate 	 * We use this later to set the line field.
47007c478bd9Sstevel@tonic-gate 	 */
47017c478bd9Sstevel@tonic-gate 	line = s_strdup(str);
47027c478bd9Sstevel@tonic-gate 
47037c478bd9Sstevel@tonic-gate 	/* Eat up leading whitespace */
47047c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
47057c478bd9Sstevel@tonic-gate 		str++;
47067c478bd9Sstevel@tonic-gate 
47077c478bd9Sstevel@tonic-gate 	if (*str == '#') {		/* comment */
47087c478bd9Sstevel@tonic-gate 		cmd = s_strdup("#");
47097c478bd9Sstevel@tonic-gate 		sep = NULL;
47107c478bd9Sstevel@tonic-gate 		arg = s_strdup(str + 1);
47117c478bd9Sstevel@tonic-gate 		flag = BAM_COMMENT;
4712ae115bc7Smrj 		if (strstr(arg, BAM_LU_HDR) != NULL) {
4713ae115bc7Smrj 			in_liveupgrade = 1;
4714ae115bc7Smrj 		} else if (strstr(arg, BAM_LU_FTR) != NULL) {
4715ae115bc7Smrj 			in_liveupgrade = 0;
4716772d6a58SWilliam Kucharski 		} else if (strstr(arg, BAM_LIBBE_FTR) != NULL) {
4717772d6a58SWilliam Kucharski 			is_libbe_ent = 1;
4718ae115bc7Smrj 		}
47197c478bd9Sstevel@tonic-gate 	} else if (*str == '\0') {	/* blank line */
47207c478bd9Sstevel@tonic-gate 		cmd = sep = arg = NULL;
47217c478bd9Sstevel@tonic-gate 		flag = BAM_EMPTY;
47227c478bd9Sstevel@tonic-gate 	} else {
47237c478bd9Sstevel@tonic-gate 		/*
47247c478bd9Sstevel@tonic-gate 		 * '=' is not a documented separator in grub syntax.
47257c478bd9Sstevel@tonic-gate 		 * However various development bits use '=' as a
47267c478bd9Sstevel@tonic-gate 		 * separator. In addition, external users also
47277c478bd9Sstevel@tonic-gate 		 * use = as a separator. So we will allow that usage.
47287c478bd9Sstevel@tonic-gate 		 */
47297c478bd9Sstevel@tonic-gate 		cp = str;
47307c478bd9Sstevel@tonic-gate 		while (*str != ' ' && *str != '\t' && *str != '=') {
47317c478bd9Sstevel@tonic-gate 			if (*str == '\0') {
47327c478bd9Sstevel@tonic-gate 				cmd = s_strdup(cp);
47337c478bd9Sstevel@tonic-gate 				sep = arg = NULL;
47347c478bd9Sstevel@tonic-gate 				break;
47357c478bd9Sstevel@tonic-gate 			}
47367c478bd9Sstevel@tonic-gate 			str++;
47377c478bd9Sstevel@tonic-gate 		}
47387c478bd9Sstevel@tonic-gate 
47397c478bd9Sstevel@tonic-gate 		if (*str != '\0') {
47407c478bd9Sstevel@tonic-gate 			save = *str;
47417c478bd9Sstevel@tonic-gate 			*str = '\0';
47427c478bd9Sstevel@tonic-gate 			cmd = s_strdup(cp);
47437c478bd9Sstevel@tonic-gate 			*str = save;
47447c478bd9Sstevel@tonic-gate 
47457c478bd9Sstevel@tonic-gate 			str++;
47467c478bd9Sstevel@tonic-gate 			save = *str;
47477c478bd9Sstevel@tonic-gate 			*str = '\0';
47487c478bd9Sstevel@tonic-gate 			sep = s_strdup(str - 1);
47497c478bd9Sstevel@tonic-gate 			*str = save;
47507c478bd9Sstevel@tonic-gate 
47517c478bd9Sstevel@tonic-gate 			while (*str == ' ' || *str == '\t')
47527c478bd9Sstevel@tonic-gate 				str++;
47537c478bd9Sstevel@tonic-gate 			if (*str == '\0')
47547c478bd9Sstevel@tonic-gate 				arg = NULL;
47557c478bd9Sstevel@tonic-gate 			else
47567c478bd9Sstevel@tonic-gate 				arg = s_strdup(str);
47577c478bd9Sstevel@tonic-gate 		}
47587c478bd9Sstevel@tonic-gate 	}
47597c478bd9Sstevel@tonic-gate 
47607c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
47617c478bd9Sstevel@tonic-gate 
47627c478bd9Sstevel@tonic-gate 	lp->cmd = cmd;
47637c478bd9Sstevel@tonic-gate 	lp->sep = sep;
47647c478bd9Sstevel@tonic-gate 	lp->arg = arg;
47657c478bd9Sstevel@tonic-gate 	lp->line = line;
47667c478bd9Sstevel@tonic-gate 	lp->lineNum = ++(*lineNum);
47677c478bd9Sstevel@tonic-gate 	if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) {
47687c478bd9Sstevel@tonic-gate 		lp->entryNum = ++(*entryNum);
47697c478bd9Sstevel@tonic-gate 		lp->flags = BAM_TITLE;
47707c478bd9Sstevel@tonic-gate 		if (prev && prev->flags == BAM_COMMENT &&
4771ae115bc7Smrj 		    prev->arg && strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
47727c478bd9Sstevel@tonic-gate 			prev->entryNum = lp->entryNum;
47738c1b6884Sszhou 			curr_ent = boot_entry_new(mp, prev, lp);
4774eb2bd662Svikram 			curr_ent->flags |= BAM_ENTRY_BOOTADM;
4775*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: is bootadm(1M) entry: %s\n",
4776*f64ca102SToomas Soome 			    fcn, arg));
47778c1b6884Sszhou 		} else {
47788c1b6884Sszhou 			curr_ent = boot_entry_new(mp, lp, lp);
4779ae115bc7Smrj 			if (in_liveupgrade) {
4780eb2bd662Svikram 				curr_ent->flags |= BAM_ENTRY_LU;
4781*f64ca102SToomas Soome 				BAM_DPRINTF(("%s: is LU entry: %s\n",
4782*f64ca102SToomas Soome 				    fcn, arg));
47838c1b6884Sszhou 			}
4784ae115bc7Smrj 		}
4785ae115bc7Smrj 		curr_ent->entryNum = *entryNum;
47867c478bd9Sstevel@tonic-gate 	} else if (flag != BAM_INVALID) {
47877c478bd9Sstevel@tonic-gate 		/*
47887c478bd9Sstevel@tonic-gate 		 * For header comments, the entry# is "fixed up"
47897c478bd9Sstevel@tonic-gate 		 * by the subsequent title
47907c478bd9Sstevel@tonic-gate 		 */
47917c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
47927c478bd9Sstevel@tonic-gate 		lp->flags = flag;
47937c478bd9Sstevel@tonic-gate 	} else {
47947c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
4795ae115bc7Smrj 
4796ae115bc7Smrj 		if (*entryNum == ENTRY_INIT) {
4797ae115bc7Smrj 			lp->flags = BAM_GLOBAL;
4798ae115bc7Smrj 		} else {
4799ae115bc7Smrj 			lp->flags = BAM_ENTRY;
4800ae115bc7Smrj 
4801ae115bc7Smrj 			if (cmd && arg) {
4802eb2bd662Svikram 				if (strcmp(cmd, menu_cmds[ROOT_CMD]) == 0) {
4803*f64ca102SToomas Soome 					BAM_DPRINTF(("%s: setting ROOT: %s\n",
4804*f64ca102SToomas Soome 					    fcn, arg));
4805ae115bc7Smrj 					curr_ent->flags |= BAM_ENTRY_ROOT;
4806eb2bd662Svikram 				} else if (strcmp(cmd, menu_cmds[FINDROOT_CMD])
4807eb2bd662Svikram 				    == 0) {
4808*f64ca102SToomas Soome 					BAM_DPRINTF(("%s: setting "
4809*f64ca102SToomas Soome 					    "FINDROOT: %s\n", fcn, arg));
4810eb2bd662Svikram 					curr_ent->flags |= BAM_ENTRY_FINDROOT;
4811eb2bd662Svikram 				} else if (strcmp(cmd,
4812eb2bd662Svikram 				    menu_cmds[CHAINLOADER_CMD]) == 0) {
4813*f64ca102SToomas Soome 					BAM_DPRINTF(("%s: setting "
4814*f64ca102SToomas Soome 					    "CHAINLOADER: %s\n", fcn, arg));
4815ae115bc7Smrj 					curr_ent->flags |=
4816ae115bc7Smrj 					    BAM_ENTRY_CHAINLOADER;
4817eb2bd662Svikram 				} else if (kernel_parser(curr_ent, cmd, arg,
4818eb2bd662Svikram 				    lp->lineNum) != BAM_SUCCESS) {
4819eb2bd662Svikram 					(void) module_parser(curr_ent, cmd,
4820eb2bd662Svikram 					    arg, lp->lineNum);
4821eb2bd662Svikram 				}
4822ae115bc7Smrj 			}
4823ae115bc7Smrj 		}
48247c478bd9Sstevel@tonic-gate 	}
48257c478bd9Sstevel@tonic-gate 
48268c1b6884Sszhou 	/* record default, old default, and entry line ranges */
4827eac223ccSWilliam Kucharski 	if (lp->flags == BAM_GLOBAL && lp->cmd != NULL &&
48288c1b6884Sszhou 	    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) {
48298c1b6884Sszhou 		mp->curdefault = lp;
48308c1b6884Sszhou 	} else if (lp->flags == BAM_COMMENT &&
48318c1b6884Sszhou 	    strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) {
48328c1b6884Sszhou 		mp->olddefault = lp;
4833ae115bc7Smrj 	} else if (lp->flags == BAM_COMMENT &&
4834ae115bc7Smrj 	    strncmp(lp->arg, BAM_OLD_RC_DEF, strlen(BAM_OLD_RC_DEF)) == 0) {
4835ae115bc7Smrj 		mp->old_rc_default = lp;
48368c1b6884Sszhou 	} else if (lp->flags == BAM_ENTRY ||
4837ae115bc7Smrj 	    (lp->flags == BAM_COMMENT &&
4838772d6a58SWilliam Kucharski 	    ((strcmp(lp->arg, BAM_BOOTADM_FTR) == 0) || is_libbe_ent))) {
4839772d6a58SWilliam Kucharski 		if (is_libbe_ent) {
4840772d6a58SWilliam Kucharski 			curr_ent->flags |= BAM_ENTRY_LIBBE;
4841772d6a58SWilliam Kucharski 			is_libbe_ent = 0;
4842772d6a58SWilliam Kucharski 		}
4843772d6a58SWilliam Kucharski 
48448c1b6884Sszhou 		boot_entry_addline(curr_ent, lp);
48458c1b6884Sszhou 	}
48467c478bd9Sstevel@tonic-gate 	append_line(mp, lp);
48477c478bd9Sstevel@tonic-gate 
48487c478bd9Sstevel@tonic-gate 	prev = lp;
48497c478bd9Sstevel@tonic-gate }
48507c478bd9Sstevel@tonic-gate 
4851eb2bd662Svikram void
485240541d5dSvikram update_numbering(menu_t *mp)
485340541d5dSvikram {
485440541d5dSvikram 	int lineNum;
485540541d5dSvikram 	int entryNum;
485640541d5dSvikram 	int old_default_value;
485740541d5dSvikram 	line_t *lp, *prev, *default_lp, *default_entry;
485840541d5dSvikram 	char buf[PATH_MAX];
485940541d5dSvikram 
486040541d5dSvikram 	if (mp->start == NULL) {
486140541d5dSvikram 		return;
486240541d5dSvikram 	}
486340541d5dSvikram 
486440541d5dSvikram 	lineNum = LINE_INIT;
486540541d5dSvikram 	entryNum = ENTRY_INIT;
486640541d5dSvikram 	old_default_value = ENTRY_INIT;
486740541d5dSvikram 	lp = default_lp = default_entry = NULL;
486840541d5dSvikram 
486940541d5dSvikram 	prev = NULL;
487040541d5dSvikram 	for (lp = mp->start; lp; prev = lp, lp = lp->next) {
487140541d5dSvikram 		lp->lineNum = ++lineNum;
487240541d5dSvikram 
487340541d5dSvikram 		/*
487440541d5dSvikram 		 * Get the value of the default command
487540541d5dSvikram 		 */
4876eac223ccSWilliam Kucharski 		if (lp->entryNum == ENTRY_INIT && lp->cmd != NULL &&
487740541d5dSvikram 		    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 &&
487840541d5dSvikram 		    lp->arg) {
487940541d5dSvikram 			old_default_value = atoi(lp->arg);
488040541d5dSvikram 			default_lp = lp;
488140541d5dSvikram 		}
488240541d5dSvikram 
488340541d5dSvikram 		/*
4884eb2bd662Svikram 		 * If not a booting entry, nothing else to fix for this
488540541d5dSvikram 		 * entry
488640541d5dSvikram 		 */
488740541d5dSvikram 		if (lp->entryNum == ENTRY_INIT)
488840541d5dSvikram 			continue;
488940541d5dSvikram 
489040541d5dSvikram 		/*
489140541d5dSvikram 		 * Record the position of the default entry.
489240541d5dSvikram 		 * The following works because global
489340541d5dSvikram 		 * commands like default and timeout should precede
489440541d5dSvikram 		 * actual boot entries, so old_default_value
489540541d5dSvikram 		 * is already known (or default cmd is missing).
489640541d5dSvikram 		 */
489740541d5dSvikram 		if (default_entry == NULL &&
489840541d5dSvikram 		    old_default_value != ENTRY_INIT &&
489940541d5dSvikram 		    lp->entryNum == old_default_value) {
490040541d5dSvikram 			default_entry = lp;
490140541d5dSvikram 		}
490240541d5dSvikram 
490340541d5dSvikram 		/*
490440541d5dSvikram 		 * Now fixup the entry number
490540541d5dSvikram 		 */
4906eac223ccSWilliam Kucharski 		if (lp->cmd != NULL &&
4907eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) {
490840541d5dSvikram 			lp->entryNum = ++entryNum;
490940541d5dSvikram 			/* fixup the bootadm header */
491040541d5dSvikram 			if (prev && prev->flags == BAM_COMMENT &&
4911ae115bc7Smrj 			    prev->arg &&
4912ae115bc7Smrj 			    strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
491340541d5dSvikram 				prev->entryNum = lp->entryNum;
491440541d5dSvikram 			}
491540541d5dSvikram 		} else {
491640541d5dSvikram 			lp->entryNum = entryNum;
491740541d5dSvikram 		}
491840541d5dSvikram 	}
491940541d5dSvikram 
492040541d5dSvikram 	/*
492140541d5dSvikram 	 * No default command in menu, simply return
492240541d5dSvikram 	 */
492340541d5dSvikram 	if (default_lp == NULL) {
492440541d5dSvikram 		return;
492540541d5dSvikram 	}
492640541d5dSvikram 
492740541d5dSvikram 	free(default_lp->arg);
492840541d5dSvikram 	free(default_lp->line);
492940541d5dSvikram 
493040541d5dSvikram 	if (default_entry == NULL) {
493140541d5dSvikram 		default_lp->arg = s_strdup("0");
493240541d5dSvikram 	} else {
493340541d5dSvikram 		(void) snprintf(buf, sizeof (buf), "%d",
493440541d5dSvikram 		    default_entry->entryNum);
493540541d5dSvikram 		default_lp->arg = s_strdup(buf);
493640541d5dSvikram 	}
493740541d5dSvikram 
493840541d5dSvikram 	/*
493940541d5dSvikram 	 * The following is required since only the line field gets
494040541d5dSvikram 	 * written back to menu.lst
494140541d5dSvikram 	 */
494240541d5dSvikram 	(void) snprintf(buf, sizeof (buf), "%s%s%s",
494340541d5dSvikram 	    menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg);
494440541d5dSvikram 	default_lp->line = s_strdup(buf);
494540541d5dSvikram }
494640541d5dSvikram 
494740541d5dSvikram 
49487c478bd9Sstevel@tonic-gate static menu_t *
49497c478bd9Sstevel@tonic-gate menu_read(char *menu_path)
49507c478bd9Sstevel@tonic-gate {
49517c478bd9Sstevel@tonic-gate 	FILE *fp;
49527c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE], *cp;
49537c478bd9Sstevel@tonic-gate 	menu_t *mp;
49547c478bd9Sstevel@tonic-gate 	int line, entry, len, n;
49557c478bd9Sstevel@tonic-gate 
49567c478bd9Sstevel@tonic-gate 	mp = s_calloc(1, sizeof (menu_t));
49577c478bd9Sstevel@tonic-gate 
49587c478bd9Sstevel@tonic-gate 	fp = fopen(menu_path, "r");
49597c478bd9Sstevel@tonic-gate 	if (fp == NULL) { /* Let the caller handle this error */
496044da779fSWilliam Kucharski 		free(mp);
496144da779fSWilliam Kucharski 		return (NULL);
49627c478bd9Sstevel@tonic-gate 	}
49637c478bd9Sstevel@tonic-gate 
49647c478bd9Sstevel@tonic-gate 	/* Note: GRUB boot entry number starts with 0 */
49657c478bd9Sstevel@tonic-gate 	line = LINE_INIT;
49667c478bd9Sstevel@tonic-gate 	entry = ENTRY_INIT;
49677c478bd9Sstevel@tonic-gate 	cp = buf;
49687c478bd9Sstevel@tonic-gate 	len = sizeof (buf);
49697c478bd9Sstevel@tonic-gate 	while (s_fgets(cp, len, fp) != NULL) {
49707c478bd9Sstevel@tonic-gate 		n = strlen(cp);
49717c478bd9Sstevel@tonic-gate 		if (cp[n - 1] == '\\') {
49727c478bd9Sstevel@tonic-gate 			len -= n - 1;
49737c478bd9Sstevel@tonic-gate 			assert(len >= 2);
49747c478bd9Sstevel@tonic-gate 			cp += n - 1;
49757c478bd9Sstevel@tonic-gate 			continue;
49767c478bd9Sstevel@tonic-gate 		}
49777c478bd9Sstevel@tonic-gate 		line_parser(mp, buf, &line, &entry);
49787c478bd9Sstevel@tonic-gate 		cp = buf;
49797c478bd9Sstevel@tonic-gate 		len = sizeof (buf);
49807c478bd9Sstevel@tonic-gate 	}
49817c478bd9Sstevel@tonic-gate 
49827c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
4983*f64ca102SToomas Soome 		bam_error(_("failed to close file: %s: %s\n"), menu_path,
4984*f64ca102SToomas Soome 		    strerror(errno));
49857c478bd9Sstevel@tonic-gate 	}
49867c478bd9Sstevel@tonic-gate 
49877c478bd9Sstevel@tonic-gate 	return (mp);
49887c478bd9Sstevel@tonic-gate }
49897c478bd9Sstevel@tonic-gate 
49907c478bd9Sstevel@tonic-gate static error_t
49917c478bd9Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title)
49927c478bd9Sstevel@tonic-gate {
49937c478bd9Sstevel@tonic-gate 	char *eq;
49947c478bd9Sstevel@tonic-gate 	char *opt_dup;
49957c478bd9Sstevel@tonic-gate 	int entryNum;
49967c478bd9Sstevel@tonic-gate 
49977c478bd9Sstevel@tonic-gate 	assert(mp);
49987c478bd9Sstevel@tonic-gate 	assert(mp->start);
49997c478bd9Sstevel@tonic-gate 	assert(opt);
50007c478bd9Sstevel@tonic-gate 
50017c478bd9Sstevel@tonic-gate 	opt_dup = s_strdup(opt);
50027c478bd9Sstevel@tonic-gate 
50037c478bd9Sstevel@tonic-gate 	if (entry)
50047c478bd9Sstevel@tonic-gate 		*entry = ENTRY_INIT;
50057c478bd9Sstevel@tonic-gate 	if (title)
50067c478bd9Sstevel@tonic-gate 		*title = NULL;
50077c478bd9Sstevel@tonic-gate 
50087c478bd9Sstevel@tonic-gate 	eq = strchr(opt_dup, '=');
50097c478bd9Sstevel@tonic-gate 	if (eq == NULL) {
5010*f64ca102SToomas Soome 		bam_error(_("invalid option: %s\n"), opt);
50117c478bd9Sstevel@tonic-gate 		free(opt_dup);
50127c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
50137c478bd9Sstevel@tonic-gate 	}
50147c478bd9Sstevel@tonic-gate 
50157c478bd9Sstevel@tonic-gate 	*eq = '\0';
50167c478bd9Sstevel@tonic-gate 	if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) {
50177c478bd9Sstevel@tonic-gate 		assert(mp->end);
50187c478bd9Sstevel@tonic-gate 		entryNum = s_strtol(eq + 1);
50197c478bd9Sstevel@tonic-gate 		if (entryNum < 0 || entryNum > mp->end->entryNum) {
5020*f64ca102SToomas Soome 			bam_error(_("invalid boot entry number: %s\n"), eq + 1);
50217c478bd9Sstevel@tonic-gate 			free(opt_dup);
50227c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
50237c478bd9Sstevel@tonic-gate 		}
50247c478bd9Sstevel@tonic-gate 		*entry = entryNum;
50257c478bd9Sstevel@tonic-gate 	} else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) {
50267c478bd9Sstevel@tonic-gate 		*title = opt + (eq - opt_dup) + 1;
50277c478bd9Sstevel@tonic-gate 	} else {
5028*f64ca102SToomas Soome 		bam_error(_("invalid option: %s\n"), opt);
50297c478bd9Sstevel@tonic-gate 		free(opt_dup);
50307c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
50317c478bd9Sstevel@tonic-gate 	}
50327c478bd9Sstevel@tonic-gate 
50337c478bd9Sstevel@tonic-gate 	free(opt_dup);
50347c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
50357c478bd9Sstevel@tonic-gate }
50367c478bd9Sstevel@tonic-gate 
50377c478bd9Sstevel@tonic-gate /*
50387c478bd9Sstevel@tonic-gate  * If invoked with no titles/entries (opt == NULL)
50397c478bd9Sstevel@tonic-gate  * only title lines in file are printed.
50407c478bd9Sstevel@tonic-gate  *
50417c478bd9Sstevel@tonic-gate  * If invoked with a title or entry #, all
50427c478bd9Sstevel@tonic-gate  * lines in *every* matching entry are listed
50437c478bd9Sstevel@tonic-gate  */
50447c478bd9Sstevel@tonic-gate static error_t
50457c478bd9Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt)
50467c478bd9Sstevel@tonic-gate {
50477c478bd9Sstevel@tonic-gate 	line_t *lp;
50487c478bd9Sstevel@tonic-gate 	int entry = ENTRY_INIT;
50497c478bd9Sstevel@tonic-gate 	int found;
50507c478bd9Sstevel@tonic-gate 	char *title = NULL;
50517c478bd9Sstevel@tonic-gate 
50527c478bd9Sstevel@tonic-gate 	assert(mp);
50537c478bd9Sstevel@tonic-gate 	assert(menu_path);
50547c478bd9Sstevel@tonic-gate 
5055eb2bd662Svikram 	/* opt is optional */
5056*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", "list_entry", menu_path,
5057eb2bd662Svikram 	    opt ? opt : "<NULL>"));
5058eb2bd662Svikram 
50597c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
5060*f64ca102SToomas Soome 		bam_error(_("menu file not found: %s\n"), menu_path);
50617c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
50627c478bd9Sstevel@tonic-gate 	}
50637c478bd9Sstevel@tonic-gate 
50647c478bd9Sstevel@tonic-gate 	if (opt != NULL) {
50657c478bd9Sstevel@tonic-gate 		if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
50667c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
50677c478bd9Sstevel@tonic-gate 		}
50687c478bd9Sstevel@tonic-gate 		assert((entry != ENTRY_INIT) ^ (title != NULL));
50697c478bd9Sstevel@tonic-gate 	} else {
50707c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0);
50717c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0);
50727c478bd9Sstevel@tonic-gate 	}
50737c478bd9Sstevel@tonic-gate 
50747c478bd9Sstevel@tonic-gate 	found = 0;
50757c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
50767c478bd9Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY)
50777c478bd9Sstevel@tonic-gate 			continue;
50787c478bd9Sstevel@tonic-gate 		if (opt == NULL && lp->flags == BAM_TITLE) {
5079*f64ca102SToomas Soome 			bam_print(_("%d %s\n"), lp->entryNum,
50807c478bd9Sstevel@tonic-gate 			    lp->arg);
50817c478bd9Sstevel@tonic-gate 			found = 1;
50827c478bd9Sstevel@tonic-gate 			continue;
50837c478bd9Sstevel@tonic-gate 		}
50847c478bd9Sstevel@tonic-gate 		if (entry != ENTRY_INIT && lp->entryNum == entry) {
5085*f64ca102SToomas Soome 			bam_print(_("%s\n"), lp->line);
50867c478bd9Sstevel@tonic-gate 			found = 1;
50877c478bd9Sstevel@tonic-gate 			continue;
50887c478bd9Sstevel@tonic-gate 		}
50897c478bd9Sstevel@tonic-gate 
50907c478bd9Sstevel@tonic-gate 		/*
50917c478bd9Sstevel@tonic-gate 		 * We set the entry value here so that all lines
50927c478bd9Sstevel@tonic-gate 		 * in entry get printed. If we subsequently match
50937c478bd9Sstevel@tonic-gate 		 * title in other entries, all lines in those
50947c478bd9Sstevel@tonic-gate 		 * entries get printed as well.
50957c478bd9Sstevel@tonic-gate 		 */
50967c478bd9Sstevel@tonic-gate 		if (title && lp->flags == BAM_TITLE && lp->arg &&
50977c478bd9Sstevel@tonic-gate 		    strncmp(title, lp->arg, strlen(title)) == 0) {
5098*f64ca102SToomas Soome 			bam_print(_("%s\n"), lp->line);
50997c478bd9Sstevel@tonic-gate 			entry = lp->entryNum;
51007c478bd9Sstevel@tonic-gate 			found = 1;
51017c478bd9Sstevel@tonic-gate 			continue;
51027c478bd9Sstevel@tonic-gate 		}
51037c478bd9Sstevel@tonic-gate 	}
51047c478bd9Sstevel@tonic-gate 
51057c478bd9Sstevel@tonic-gate 	if (!found) {
5106*f64ca102SToomas Soome 		bam_error(_("no matching entry found\n"));
51077c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
51087c478bd9Sstevel@tonic-gate 	}
51097c478bd9Sstevel@tonic-gate 
51107c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
51117c478bd9Sstevel@tonic-gate }
51127c478bd9Sstevel@tonic-gate 
5113843e1988Sjohnlev int
51147c478bd9Sstevel@tonic-gate add_boot_entry(menu_t *mp,
51157c478bd9Sstevel@tonic-gate     char *title,
5116eb2bd662Svikram     char *findroot,
51177c478bd9Sstevel@tonic-gate     char *kernel,
5118843e1988Sjohnlev     char *mod_kernel,
511944da779fSWilliam Kucharski     char *module,
512044da779fSWilliam Kucharski     char *bootfs)
51217c478bd9Sstevel@tonic-gate {
5122eb2bd662Svikram 	int		lineNum;
5123eb2bd662Svikram 	int		entryNum;
51247c478bd9Sstevel@tonic-gate 	char		linebuf[BAM_MAXLINE];
5125eb2bd662Svikram 	menu_cmd_t	k_cmd;
5126eb2bd662Svikram 	menu_cmd_t	m_cmd;
5127eb2bd662Svikram 	const char	*fcn = "add_boot_entry()";
51287c478bd9Sstevel@tonic-gate 
51297c478bd9Sstevel@tonic-gate 	assert(mp);
51307c478bd9Sstevel@tonic-gate 
5131eb2bd662Svikram 	INJECT_ERROR1("ADD_BOOT_ENTRY_FINDROOT_NULL", findroot = NULL);
5132eb2bd662Svikram 	if (findroot == NULL) {
5133*f64ca102SToomas Soome 		bam_error(_("can't find argument for findroot command\n"));
5134eb2bd662Svikram 		return (BAM_ERROR);
5135eb2bd662Svikram 	}
5136eb2bd662Svikram 
51377c478bd9Sstevel@tonic-gate 	if (title == NULL) {
5138ee3b8144Sszhou 		title = "Solaris";	/* default to Solaris */
51397c478bd9Sstevel@tonic-gate 	}
51407c478bd9Sstevel@tonic-gate 	if (kernel == NULL) {
5141*f64ca102SToomas Soome 		bam_error(_("missing suboption: %s\n"), menu_cmds[KERNEL_CMD]);
51427c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
51437c478bd9Sstevel@tonic-gate 	}
51447c478bd9Sstevel@tonic-gate 	if (module == NULL) {
5145ae115bc7Smrj 		if (bam_direct != BAM_DIRECT_DBOOT) {
5146*f64ca102SToomas Soome 			bam_error(_("missing suboption: %s\n"),
5147*f64ca102SToomas Soome 			    menu_cmds[MODULE_CMD]);
51487c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
51497c478bd9Sstevel@tonic-gate 		}
51507c478bd9Sstevel@tonic-gate 
5151ae115bc7Smrj 		/* Figure the commands out from the kernel line */
5152ae115bc7Smrj 		if (strstr(kernel, "$ISADIR") != NULL) {
5153ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE;
5154ae115bc7Smrj 		} else if (strstr(kernel, "amd64") != NULL) {
5155ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE_64;
5156ae115bc7Smrj 		} else {
5157ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE_32;
5158ae115bc7Smrj 		}
51599adc5a4eSEdward Gillett 	}
51609adc5a4eSEdward Gillett 
5161ae115bc7Smrj 	k_cmd = KERNEL_DOLLAR_CMD;
5162ae115bc7Smrj 	m_cmd = MODULE_DOLLAR_CMD;
5163ae115bc7Smrj 
51647c478bd9Sstevel@tonic-gate 	if (mp->start) {
51657c478bd9Sstevel@tonic-gate 		lineNum = mp->end->lineNum;
51667c478bd9Sstevel@tonic-gate 		entryNum = mp->end->entryNum;
51677c478bd9Sstevel@tonic-gate 	} else {
51687c478bd9Sstevel@tonic-gate 		lineNum = LINE_INIT;
51697c478bd9Sstevel@tonic-gate 		entryNum = ENTRY_INIT;
51707c478bd9Sstevel@tonic-gate 	}
51717c478bd9Sstevel@tonic-gate 
51727c478bd9Sstevel@tonic-gate 	/*
51737c478bd9Sstevel@tonic-gate 	 * No separator for comment (HDR/FTR) commands
51747c478bd9Sstevel@tonic-gate 	 * The syntax for comments is #<comment>
51757c478bd9Sstevel@tonic-gate 	 */
51767c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
5177ae115bc7Smrj 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_HDR);
51788c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
51797c478bd9Sstevel@tonic-gate 
51807c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
51817c478bd9Sstevel@tonic-gate 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
51828c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
51837c478bd9Sstevel@tonic-gate 
51847c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
5185eb2bd662Svikram 	    menu_cmds[FINDROOT_CMD], menu_cmds[SEP_CMD], findroot);
51868c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
5187*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: findroot added: line#: %d: entry#: %d\n",
5188*f64ca102SToomas Soome 	    fcn, lineNum, entryNum));
51897c478bd9Sstevel@tonic-gate 
519044da779fSWilliam Kucharski 	if (bootfs != NULL) {
519144da779fSWilliam Kucharski 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
519244da779fSWilliam Kucharski 		    menu_cmds[BOOTFS_CMD], menu_cmds[SEP_CMD], bootfs);
519344da779fSWilliam Kucharski 		line_parser(mp, linebuf, &lineNum, &entryNum);
519444da779fSWilliam Kucharski 	}
519544da779fSWilliam Kucharski 
51967c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
5197ae115bc7Smrj 	    menu_cmds[k_cmd], menu_cmds[SEP_CMD], kernel);
51988c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
51997c478bd9Sstevel@tonic-gate 
5200843e1988Sjohnlev 	if (mod_kernel != NULL) {
5201843e1988Sjohnlev 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
5202843e1988Sjohnlev 		    menu_cmds[m_cmd], menu_cmds[SEP_CMD], mod_kernel);
5203843e1988Sjohnlev 		line_parser(mp, linebuf, &lineNum, &entryNum);
5204843e1988Sjohnlev 	}
5205843e1988Sjohnlev 
52067c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
5207ae115bc7Smrj 	    menu_cmds[m_cmd], menu_cmds[SEP_CMD], module);
52088c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
52097c478bd9Sstevel@tonic-gate 
52107c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
5211ae115bc7Smrj 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_FTR);
52128c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
52137c478bd9Sstevel@tonic-gate 
52147c478bd9Sstevel@tonic-gate 	return (entryNum);
52157c478bd9Sstevel@tonic-gate }
52167c478bd9Sstevel@tonic-gate 
521744da779fSWilliam Kucharski error_t
521844da779fSWilliam Kucharski delete_boot_entry(menu_t *mp, int entryNum, int quiet)
52197c478bd9Sstevel@tonic-gate {
5220eb2bd662Svikram 	line_t		*lp;
5221eb2bd662Svikram 	line_t		*freed;
5222eb2bd662Svikram 	entry_t		*ent;
5223eb2bd662Svikram 	entry_t		*tmp;
522444da779fSWilliam Kucharski 	int		deleted = 0;
522544da779fSWilliam Kucharski 	const char	*fcn = "delete_boot_entry()";
52267c478bd9Sstevel@tonic-gate 
52277c478bd9Sstevel@tonic-gate 	assert(entryNum != ENTRY_INIT);
52287c478bd9Sstevel@tonic-gate 
5229eb2bd662Svikram 	tmp = NULL;
5230eb2bd662Svikram 
52318c1b6884Sszhou 	ent = mp->entries;
52328c1b6884Sszhou 	while (ent) {
52338c1b6884Sszhou 		lp = ent->start;
5234772d6a58SWilliam Kucharski 
5235772d6a58SWilliam Kucharski 		/*
5236772d6a58SWilliam Kucharski 		 * Check entry number and make sure it's a modifiable entry.
5237772d6a58SWilliam Kucharski 		 *
5238772d6a58SWilliam Kucharski 		 * Guidelines:
5239772d6a58SWilliam Kucharski 		 *	+ We can modify a bootadm-created entry
5240772d6a58SWilliam Kucharski 		 *	+ We can modify a libbe-created entry
5241772d6a58SWilliam Kucharski 		 */
5242772d6a58SWilliam Kucharski 		if ((lp->flags != BAM_COMMENT &&
5243772d6a58SWilliam Kucharski 		    (((ent->flags & BAM_ENTRY_LIBBE) == 0) &&
5244772d6a58SWilliam Kucharski 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0)) ||
52458c1b6884Sszhou 		    (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) {
52468c1b6884Sszhou 			ent = ent->next;
52477c478bd9Sstevel@tonic-gate 			continue;
52487c478bd9Sstevel@tonic-gate 		}
52497c478bd9Sstevel@tonic-gate 
52508c1b6884Sszhou 		/* free the entry content */
52518c1b6884Sszhou 		do {
52528c1b6884Sszhou 			freed = lp;
52538c1b6884Sszhou 			lp = lp->next;	/* prev stays the same */
5254*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: freeing line: %d\n",
5255*f64ca102SToomas Soome 			    fcn, freed->lineNum));
52568c1b6884Sszhou 			unlink_line(mp, freed);
52578c1b6884Sszhou 			line_free(freed);
52588c1b6884Sszhou 		} while (freed != ent->end);
52597c478bd9Sstevel@tonic-gate 
52608c1b6884Sszhou 		/* free the entry_t structure */
5261eb2bd662Svikram 		assert(tmp == NULL);
52628c1b6884Sszhou 		tmp = ent;
52638c1b6884Sszhou 		ent = ent->next;
52648c1b6884Sszhou 		if (tmp->prev)
52658c1b6884Sszhou 			tmp->prev->next = ent;
52667c478bd9Sstevel@tonic-gate 		else
52678c1b6884Sszhou 			mp->entries = ent;
52688c1b6884Sszhou 		if (ent)
52698c1b6884Sszhou 			ent->prev = tmp->prev;
5270*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: freeing entry: %d\n", fcn, tmp->entryNum));
5271eb2bd662Svikram 		free(tmp);
5272eb2bd662Svikram 		tmp = NULL;
52737c478bd9Sstevel@tonic-gate 		deleted = 1;
52747c478bd9Sstevel@tonic-gate 	}
52757c478bd9Sstevel@tonic-gate 
5276eb2bd662Svikram 	assert(tmp == NULL);
5277eb2bd662Svikram 
52787c478bd9Sstevel@tonic-gate 	if (!deleted && entryNum != ALL_ENTRIES) {
527944da779fSWilliam Kucharski 		if (quiet == DBE_PRINTERR)
5280*f64ca102SToomas Soome 			bam_error(_("no matching bootadm entry found\n"));
52817c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
52827c478bd9Sstevel@tonic-gate 	}
52837c478bd9Sstevel@tonic-gate 
528440541d5dSvikram 	/*
528540541d5dSvikram 	 * Now that we have deleted an entry, update
528640541d5dSvikram 	 * the entry numbering and the default cmd.
528740541d5dSvikram 	 */
528840541d5dSvikram 	update_numbering(mp);
528940541d5dSvikram 
52907c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
52917c478bd9Sstevel@tonic-gate }
52927c478bd9Sstevel@tonic-gate 
52937c478bd9Sstevel@tonic-gate static error_t
5294eb2bd662Svikram delete_all_entries(menu_t *mp, char *dummy, char *opt)
52957c478bd9Sstevel@tonic-gate {
52967c478bd9Sstevel@tonic-gate 	assert(mp);
5297eb2bd662Svikram 	assert(dummy == NULL);
52987c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
52997c478bd9Sstevel@tonic-gate 
5300*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. No args\n", "delete_all_entries"));
5301eb2bd662Svikram 
53027c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
5303*f64ca102SToomas Soome 		bam_print(_("the GRUB menu is empty\n"));
53047c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
53057c478bd9Sstevel@tonic-gate 	}
53067c478bd9Sstevel@tonic-gate 
530744da779fSWilliam Kucharski 	if (delete_boot_entry(mp, ALL_ENTRIES, DBE_PRINTERR) != BAM_SUCCESS) {
53087c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
53097c478bd9Sstevel@tonic-gate 	}
53107c478bd9Sstevel@tonic-gate 
53117c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
53127c478bd9Sstevel@tonic-gate }
53137c478bd9Sstevel@tonic-gate 
53147c478bd9Sstevel@tonic-gate static FILE *
5315eb2bd662Svikram create_diskmap(char *osroot)
53167c478bd9Sstevel@tonic-gate {
53177c478bd9Sstevel@tonic-gate 	FILE *fp;
5318cedc7e57SEnrico Perla - Sun Microsystems 	char cmd[PATH_MAX + 16];
5319cedc7e57SEnrico Perla - Sun Microsystems 	char path[PATH_MAX];
5320eb2bd662Svikram 	const char *fcn = "create_diskmap()";
53217c478bd9Sstevel@tonic-gate 
53227c478bd9Sstevel@tonic-gate 	/* make sure we have a map file */
53237c478bd9Sstevel@tonic-gate 	fp = fopen(GRUBDISK_MAP, "r");
53247c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
5325cedc7e57SEnrico Perla - Sun Microsystems 		int	ret;
5326cedc7e57SEnrico Perla - Sun Microsystems 
5327cedc7e57SEnrico Perla - Sun Microsystems 		ret = snprintf(path, sizeof (path), "%s/%s", osroot,
5328cedc7e57SEnrico Perla - Sun Microsystems 		    CREATE_DISKMAP);
5329cedc7e57SEnrico Perla - Sun Microsystems 		if (ret >= sizeof (path)) {
5330*f64ca102SToomas Soome 			bam_error(_("unable to create path on mountpoint %s, "
5331*f64ca102SToomas Soome 			    "path too long\n"), osroot);
5332cedc7e57SEnrico Perla - Sun Microsystems 			return (NULL);
5333cedc7e57SEnrico Perla - Sun Microsystems 		}
5334cedc7e57SEnrico Perla - Sun Microsystems 		if (is_safe_exec(path) == BAM_ERROR)
5335cedc7e57SEnrico Perla - Sun Microsystems 			return (NULL);
5336cedc7e57SEnrico Perla - Sun Microsystems 
53377c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd),
5338eb2bd662Svikram 		    "%s/%s > /dev/null", osroot, CREATE_DISKMAP);
5339eb2bd662Svikram 		if (exec_cmd(cmd, NULL) != 0)
5340eb2bd662Svikram 			return (NULL);
53417c478bd9Sstevel@tonic-gate 		fp = fopen(GRUBDISK_MAP, "r");
5342eb2bd662Svikram 		INJECT_ERROR1("DISKMAP_CREATE_FAIL", fp = NULL);
5343eb2bd662Svikram 		if (fp) {
5344*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: created diskmap file: %s\n",
5345*f64ca102SToomas Soome 			    fcn, GRUBDISK_MAP));
5346eb2bd662Svikram 		} else {
5347*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: FAILED to create diskmap file: %s\n",
5348*f64ca102SToomas Soome 			    fcn, GRUBDISK_MAP));
5349eb2bd662Svikram 		}
53507c478bd9Sstevel@tonic-gate 	}
53517c478bd9Sstevel@tonic-gate 	return (fp);
53527c478bd9Sstevel@tonic-gate }
53537c478bd9Sstevel@tonic-gate 
53547c478bd9Sstevel@tonic-gate #define	SECTOR_SIZE	512
53557c478bd9Sstevel@tonic-gate 
53567c478bd9Sstevel@tonic-gate static int
53577c478bd9Sstevel@tonic-gate get_partition(char *device)
53587c478bd9Sstevel@tonic-gate {
53591a902ef8SHans Rosenfeld 	int i, fd, is_pcfs, partno = PARTNO_NOTFOUND;
53607c478bd9Sstevel@tonic-gate 	struct mboot *mboot;
53617c478bd9Sstevel@tonic-gate 	char boot_sect[SECTOR_SIZE];
53627c478bd9Sstevel@tonic-gate 	char *wholedisk, *slice;
5363e998e519SSheshadri Vasudevan #ifdef i386
5364e998e519SSheshadri Vasudevan 	ext_part_t *epp;
5365e998e519SSheshadri Vasudevan 	uint32_t secnum, numsec;
53661a902ef8SHans Rosenfeld 	int rval, pno, ext_partno = PARTNO_NOTFOUND;
5367e998e519SSheshadri Vasudevan #endif
53687c478bd9Sstevel@tonic-gate 
53697c478bd9Sstevel@tonic-gate 	/* form whole disk (p0) */
53707c478bd9Sstevel@tonic-gate 	slice = device + strlen(device) - 2;
53717c478bd9Sstevel@tonic-gate 	is_pcfs = (*slice != 's');
53727c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
53737c478bd9Sstevel@tonic-gate 		*slice = '\0';
53747c478bd9Sstevel@tonic-gate 	wholedisk = s_calloc(1, strlen(device) + 3);
53757c478bd9Sstevel@tonic-gate 	(void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device);
53767c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
53777c478bd9Sstevel@tonic-gate 		*slice = 's';
53787c478bd9Sstevel@tonic-gate 
53797c478bd9Sstevel@tonic-gate 	/* read boot sector */
53807c478bd9Sstevel@tonic-gate 	fd = open(wholedisk, O_RDONLY);
53817c478bd9Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
53827c478bd9Sstevel@tonic-gate 		return (partno);
53837c478bd9Sstevel@tonic-gate 	}
53847c478bd9Sstevel@tonic-gate 	(void) close(fd);
53857c478bd9Sstevel@tonic-gate 
5386e998e519SSheshadri Vasudevan #ifdef i386
5387e998e519SSheshadri Vasudevan 	/* Read/Initialize extended partition information */
5388e998e519SSheshadri Vasudevan 	if ((rval = libfdisk_init(&epp, wholedisk, NULL, FDISK_READ_DISK))
5389e998e519SSheshadri Vasudevan 	    != FDISK_SUCCESS) {
5390e998e519SSheshadri Vasudevan 		switch (rval) {
5391e998e519SSheshadri Vasudevan 			/*
5392e998e519SSheshadri Vasudevan 			 * FDISK_EBADLOGDRIVE and FDISK_ENOLOGDRIVE can
5393e998e519SSheshadri Vasudevan 			 * be considered as soft errors and hence
5394e998e519SSheshadri Vasudevan 			 * we do not return
5395e998e519SSheshadri Vasudevan 			 */
5396e998e519SSheshadri Vasudevan 			case FDISK_EBADLOGDRIVE:
5397e998e519SSheshadri Vasudevan 				break;
5398e998e519SSheshadri Vasudevan 			case FDISK_ENOLOGDRIVE:
5399e998e519SSheshadri Vasudevan 				break;
54006cb5747bSSharath M Srinivasan 			case FDISK_EBADMAGIC:
54016cb5747bSSharath M Srinivasan 				/*FALLTHROUGH*/
5402e998e519SSheshadri Vasudevan 			default:
5403e998e519SSheshadri Vasudevan 				free(wholedisk);
54046cb5747bSSharath M Srinivasan 				libfdisk_fini(&epp);
5405e998e519SSheshadri Vasudevan 				return (partno);
5406e998e519SSheshadri Vasudevan 		}
5407e998e519SSheshadri Vasudevan 	}
5408e998e519SSheshadri Vasudevan #endif
5409e998e519SSheshadri Vasudevan 	free(wholedisk);
5410e998e519SSheshadri Vasudevan 
54117c478bd9Sstevel@tonic-gate 	/* parse fdisk table */
54127c478bd9Sstevel@tonic-gate 	mboot = (struct mboot *)((void *)boot_sect);
54137c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
54147c478bd9Sstevel@tonic-gate 		struct ipart *part =
54157c478bd9Sstevel@tonic-gate 		    (struct ipart *)(uintptr_t)mboot->parts + i;
54167c478bd9Sstevel@tonic-gate 		if (is_pcfs) {	/* looking for solaris boot part */
54177c478bd9Sstevel@tonic-gate 			if (part->systid == 0xbe) {
54187c478bd9Sstevel@tonic-gate 				partno = i;
54197c478bd9Sstevel@tonic-gate 				break;
54207c478bd9Sstevel@tonic-gate 			}
54217c478bd9Sstevel@tonic-gate 		} else {	/* look for solaris partition, old and new */
54221a902ef8SHans Rosenfeld 			if (part->systid == EFI_PMBR) {
54231a902ef8SHans Rosenfeld 				partno = PARTNO_EFI;
54241a902ef8SHans Rosenfeld 				break;
54251a902ef8SHans Rosenfeld 			}
54261a902ef8SHans Rosenfeld 
5427e998e519SSheshadri Vasudevan #ifdef i386
5428e998e519SSheshadri Vasudevan 			if ((part->systid == SUNIXOS &&
5429e998e519SSheshadri Vasudevan 			    (fdisk_is_linux_swap(epp, part->relsect,
5430e998e519SSheshadri Vasudevan 			    NULL) != 0)) || part->systid == SUNIXOS2) {
5431e998e519SSheshadri Vasudevan #else
54327c478bd9Sstevel@tonic-gate 			if (part->systid == SUNIXOS ||
54337c478bd9Sstevel@tonic-gate 			    part->systid == SUNIXOS2) {
5434e998e519SSheshadri Vasudevan #endif
54357c478bd9Sstevel@tonic-gate 				partno = i;
54367c478bd9Sstevel@tonic-gate 				break;
54377c478bd9Sstevel@tonic-gate 			}
543828074f82SSheshadri Vasudevan 
543928074f82SSheshadri Vasudevan #ifdef i386
544028074f82SSheshadri Vasudevan 			if (fdisk_is_dos_extended(part->systid))
544128074f82SSheshadri Vasudevan 				ext_partno = i;
544228074f82SSheshadri Vasudevan #endif
54437c478bd9Sstevel@tonic-gate 		}
54447c478bd9Sstevel@tonic-gate 	}
5445e998e519SSheshadri Vasudevan #ifdef i386
544628074f82SSheshadri Vasudevan 	/* If no primary solaris partition, check extended partition */
54471a902ef8SHans Rosenfeld 	if ((partno == PARTNO_NOTFOUND) && (ext_partno != PARTNO_NOTFOUND)) {
544828074f82SSheshadri Vasudevan 		rval = fdisk_get_solaris_part(epp, &pno, &secnum, &numsec);
544928074f82SSheshadri Vasudevan 		if (rval == FDISK_SUCCESS) {
545028074f82SSheshadri Vasudevan 			partno = pno - 1;
545128074f82SSheshadri Vasudevan 		}
545228074f82SSheshadri Vasudevan 	}
5453e998e519SSheshadri Vasudevan 	libfdisk_fini(&epp);
5454e998e519SSheshadri Vasudevan #endif
54557c478bd9Sstevel@tonic-gate 	return (partno);
54567c478bd9Sstevel@tonic-gate }
54577c478bd9Sstevel@tonic-gate 
5458eb2bd662Svikram char *
5459eb2bd662Svikram get_grubroot(char *osroot, char *osdev, char *menu_root)
54607c478bd9Sstevel@tonic-gate {
5461eb2bd662Svikram 	char		*grubroot;	/* (hd#,#,#) */
54627c478bd9Sstevel@tonic-gate 	char		*slice;
5463110570ceSToomas Soome 	char		*grubhd = NULL;
54647c478bd9Sstevel@tonic-gate 	int		fdiskpart;
54657c478bd9Sstevel@tonic-gate 	int		found = 0;
5466eb2bd662Svikram 	char		*devname;
5467eb2bd662Svikram 	char		*ctdname = strstr(osdev, "dsk/");
54687c478bd9Sstevel@tonic-gate 	char		linebuf[PATH_MAX];
5469eb2bd662Svikram 	FILE		*fp;
54707c478bd9Sstevel@tonic-gate 
5471eb2bd662Svikram 	INJECT_ERROR1("GRUBROOT_INVALID_OSDEV", ctdname = NULL);
5472eb2bd662Svikram 	if (ctdname == NULL) {
5473*f64ca102SToomas Soome 		bam_error(_("not a /dev/[r]dsk name: %s\n"), osdev);
54747c478bd9Sstevel@tonic-gate 		return (NULL);
5475eb2bd662Svikram 	}
5476eb2bd662Svikram 
5477eb2bd662Svikram 	if (menu_root && !menu_on_bootdisk(osroot, menu_root)) {
5478eb2bd662Svikram 		/* menu bears no resemblance to our reality */
5479*f64ca102SToomas Soome 		bam_error(_("cannot get (hd?,?,?) for menu. menu not on "
5480*f64ca102SToomas Soome 		    "bootdisk: %s\n"), osdev);
5481eb2bd662Svikram 		return (NULL);
5482eb2bd662Svikram 	}
54837c478bd9Sstevel@tonic-gate 
54847c478bd9Sstevel@tonic-gate 	ctdname += strlen("dsk/");
54857c478bd9Sstevel@tonic-gate 	slice = strrchr(ctdname, 's');
54867c478bd9Sstevel@tonic-gate 	if (slice)
54877c478bd9Sstevel@tonic-gate 		*slice = '\0';
54887c478bd9Sstevel@tonic-gate 
5489eb2bd662Svikram 	fp = create_diskmap(osroot);
5490eb2bd662Svikram 	if (fp == NULL) {
5491*f64ca102SToomas Soome 		bam_error(_("create_diskmap command failed for OS root: %s.\n"),
5492*f64ca102SToomas Soome 		    osroot);
5493eb2bd662Svikram 		return (NULL);
5494eb2bd662Svikram 	}
5495eb2bd662Svikram 
54967c478bd9Sstevel@tonic-gate 	rewind(fp);
54977c478bd9Sstevel@tonic-gate 	while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) {
54987c478bd9Sstevel@tonic-gate 		grubhd = strtok(linebuf, " \t\n");
54997c478bd9Sstevel@tonic-gate 		if (grubhd)
55007c478bd9Sstevel@tonic-gate 			devname = strtok(NULL, " \t\n");
55017c478bd9Sstevel@tonic-gate 		else
55027c478bd9Sstevel@tonic-gate 			devname = NULL;
55037c478bd9Sstevel@tonic-gate 		if (devname && strcmp(devname, ctdname) == 0) {
55047c478bd9Sstevel@tonic-gate 			found = 1;
55057c478bd9Sstevel@tonic-gate 			break;
55067c478bd9Sstevel@tonic-gate 		}
55077c478bd9Sstevel@tonic-gate 	}
55087c478bd9Sstevel@tonic-gate 
55097c478bd9Sstevel@tonic-gate 	if (slice)
55107c478bd9Sstevel@tonic-gate 		*slice = 's';
55117c478bd9Sstevel@tonic-gate 
5512eb2bd662Svikram 	(void) fclose(fp);
5513eb2bd662Svikram 	fp = NULL;
5514eb2bd662Svikram 
5515eb2bd662Svikram 	INJECT_ERROR1("GRUBROOT_BIOSDEV_FAIL", found = 0);
55167c478bd9Sstevel@tonic-gate 	if (found == 0) {
5517*f64ca102SToomas Soome 		bam_error(_("not using biosdev command for disk: %s.\n"),
5518*f64ca102SToomas Soome 		    osdev);
5519eb2bd662Svikram 		return (NULL);
55207c478bd9Sstevel@tonic-gate 	}
55217c478bd9Sstevel@tonic-gate 
5522eb2bd662Svikram 	fdiskpart = get_partition(osdev);
55231a902ef8SHans Rosenfeld 	INJECT_ERROR1("GRUBROOT_FDISK_FAIL", fdiskpart = PARTNO_NOTFOUND);
55241a902ef8SHans Rosenfeld 	if (fdiskpart == PARTNO_NOTFOUND) {
5525*f64ca102SToomas Soome 		bam_error(_("failed to determine fdisk partition: %s\n"),
5526*f64ca102SToomas Soome 		    osdev);
55277c478bd9Sstevel@tonic-gate 		return (NULL);
5528eb2bd662Svikram 	}
55297c478bd9Sstevel@tonic-gate 
5530eb2bd662Svikram 	grubroot = s_calloc(1, 10);
55311a902ef8SHans Rosenfeld 	if (fdiskpart == PARTNO_EFI) {
55321a902ef8SHans Rosenfeld 		fdiskpart = atoi(&slice[1]);
55331a902ef8SHans Rosenfeld 		slice = NULL;
55341a902ef8SHans Rosenfeld 	}
55351a902ef8SHans Rosenfeld 
55367c478bd9Sstevel@tonic-gate 	if (slice) {
5537eb2bd662Svikram 		(void) snprintf(grubroot, 10, "(hd%s,%d,%c)",
55387c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart, slice[1] + 'a' - '0');
55397c478bd9Sstevel@tonic-gate 	} else
5540eb2bd662Svikram 		(void) snprintf(grubroot, 10, "(hd%s,%d)",
55417c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart);
55427c478bd9Sstevel@tonic-gate 
5543eb2bd662Svikram 	assert(fp == NULL);
5544eb2bd662Svikram 	assert(strncmp(grubroot, "(hd", strlen("(hd")) == 0);
5545eb2bd662Svikram 	return (grubroot);
5546eb2bd662Svikram }
5547eb2bd662Svikram 
5548eb2bd662Svikram static char *
5549eb2bd662Svikram find_primary_common(char *mntpt, char *fstype)
5550eb2bd662Svikram {
5551eb2bd662Svikram 	char		signdir[PATH_MAX];
5552eb2bd662Svikram 	char		tmpsign[MAXNAMELEN + 1];
5553eb2bd662Svikram 	char		*lu;
5554eb2bd662Svikram 	char		*ufs;
5555eb2bd662Svikram 	char		*zfs;
5556eb2bd662Svikram 	DIR		*dirp = NULL;
5557eb2bd662Svikram 	struct dirent	*entp;
5558eb2bd662Svikram 	struct stat	sb;
5559eb2bd662Svikram 	const char	*fcn = "find_primary_common()";
5560eb2bd662Svikram 
5561eb2bd662Svikram 	(void) snprintf(signdir, sizeof (signdir), "%s/%s",
5562eb2bd662Svikram 	    mntpt, GRUBSIGN_DIR);
5563eb2bd662Svikram 
5564eb2bd662Svikram 	if (stat(signdir, &sb) == -1) {
5565*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: no sign dir: %s\n", fcn, signdir));
5566eb2bd662Svikram 		return (NULL);
5567eb2bd662Svikram 	}
5568eb2bd662Svikram 
5569eb2bd662Svikram 	dirp = opendir(signdir);
5570eb2bd662Svikram 	INJECT_ERROR1("SIGNDIR_OPENDIR_FAIL", dirp = NULL);
5571eb2bd662Svikram 	if (dirp == NULL) {
5572*f64ca102SToomas Soome 		bam_error(_("opendir of %s failed: %s\n"), signdir,
5573*f64ca102SToomas Soome 		    strerror(errno));
5574eb2bd662Svikram 		return (NULL);
5575eb2bd662Svikram 	}
5576eb2bd662Svikram 
5577eb2bd662Svikram 	ufs = zfs = lu = NULL;
5578eb2bd662Svikram 
5579110570ceSToomas Soome 	while ((entp = readdir(dirp)) != NULL) {
5580eb2bd662Svikram 		if (strcmp(entp->d_name, ".") == 0 ||
5581eb2bd662Svikram 		    strcmp(entp->d_name, "..") == 0)
5582eb2bd662Svikram 			continue;
5583eb2bd662Svikram 
5584eb2bd662Svikram 		(void) snprintf(tmpsign, sizeof (tmpsign), "%s", entp->d_name);
5585eb2bd662Svikram 
5586eb2bd662Svikram 		if (lu == NULL &&
5587eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_LU_PREFIX,
5588eb2bd662Svikram 		    strlen(GRUBSIGN_LU_PREFIX)) == 0) {
5589eb2bd662Svikram 			lu = s_strdup(tmpsign);
5590eb2bd662Svikram 		}
5591eb2bd662Svikram 
5592eb2bd662Svikram 		if (ufs == NULL &&
5593eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
5594eb2bd662Svikram 		    strlen(GRUBSIGN_UFS_PREFIX)) == 0) {
5595eb2bd662Svikram 			ufs = s_strdup(tmpsign);
5596eb2bd662Svikram 		}
5597eb2bd662Svikram 
5598eb2bd662Svikram 		if (zfs == NULL &&
5599eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_ZFS_PREFIX,
5600eb2bd662Svikram 		    strlen(GRUBSIGN_ZFS_PREFIX)) == 0) {
5601eb2bd662Svikram 			zfs = s_strdup(tmpsign);
5602eb2bd662Svikram 		}
5603eb2bd662Svikram 	}
5604eb2bd662Svikram 
5605*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: existing primary signs: zfs=%s ufs=%s lu=%s\n", fcn,
5606eb2bd662Svikram 	    zfs ? zfs : "NULL",
5607eb2bd662Svikram 	    ufs ? ufs : "NULL",
5608eb2bd662Svikram 	    lu ? lu : "NULL"));
5609eb2bd662Svikram 
5610eb2bd662Svikram 	if (dirp) {
5611eb2bd662Svikram 		(void) closedir(dirp);
5612eb2bd662Svikram 		dirp = NULL;
5613eb2bd662Svikram 	}
5614eb2bd662Svikram 
5615eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0 && zfs) {
5616*f64ca102SToomas Soome 		bam_error(_("found mismatched boot signature %s for "
5617*f64ca102SToomas Soome 		    "filesystem type: %s.\n"), zfs, "ufs");
5618eb2bd662Svikram 		free(zfs);
5619eb2bd662Svikram 		zfs = NULL;
5620eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0 && ufs) {
5621*f64ca102SToomas Soome 		bam_error(_("found mismatched boot signature %s for "
5622*f64ca102SToomas Soome 		    "filesystem type: %s.\n"), ufs, "zfs");
5623eb2bd662Svikram 		free(ufs);
5624eb2bd662Svikram 		ufs = NULL;
5625eb2bd662Svikram 	}
5626eb2bd662Svikram 
5627eb2bd662Svikram 	assert(dirp == NULL);
5628eb2bd662Svikram 
5629eb2bd662Svikram 	/* For now, we let Live Upgrade take care of its signature itself */
5630eb2bd662Svikram 	if (lu) {
5631*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: feeing LU sign: %s\n", fcn, lu));
5632eb2bd662Svikram 		free(lu);
5633eb2bd662Svikram 		lu = NULL;
5634eb2bd662Svikram 	}
5635eb2bd662Svikram 
5636eb2bd662Svikram 	return (zfs ? zfs : ufs);
5637eb2bd662Svikram }
5638eb2bd662Svikram 
5639eb2bd662Svikram static char *
5640eb2bd662Svikram find_backup_common(char *mntpt, char *fstype)
5641eb2bd662Svikram {
5642eb2bd662Svikram 	FILE		*bfp = NULL;
5643eb2bd662Svikram 	char		tmpsign[MAXNAMELEN + 1];
5644eb2bd662Svikram 	char		backup[PATH_MAX];
5645eb2bd662Svikram 	char		*ufs;
5646eb2bd662Svikram 	char		*zfs;
5647eb2bd662Svikram 	char		*lu;
5648eb2bd662Svikram 	int		error;
5649eb2bd662Svikram 	const char	*fcn = "find_backup_common()";
5650eb2bd662Svikram 
5651eb2bd662Svikram 	/*
5652eb2bd662Svikram 	 * We didn't find it in the primary directory.
5653eb2bd662Svikram 	 * Look at the backup
5654eb2bd662Svikram 	 */
5655eb2bd662Svikram 	(void) snprintf(backup, sizeof (backup), "%s%s",
5656eb2bd662Svikram 	    mntpt, GRUBSIGN_BACKUP);
5657eb2bd662Svikram 
5658eb2bd662Svikram 	bfp = fopen(backup, "r");
5659eb2bd662Svikram 	if (bfp == NULL) {
5660eb2bd662Svikram 		error = errno;
5661eb2bd662Svikram 		if (bam_verbose) {
5662*f64ca102SToomas Soome 			bam_error(_("failed to open file: %s: %s\n"),
5663*f64ca102SToomas Soome 			    backup, strerror(error));
5664eb2bd662Svikram 		}
5665*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: failed to open %s: %s\n",
5666*f64ca102SToomas Soome 		    fcn, backup, strerror(error)));
5667eb2bd662Svikram 		return (NULL);
5668eb2bd662Svikram 	}
5669eb2bd662Svikram 
5670eb2bd662Svikram 	ufs = zfs = lu = NULL;
5671eb2bd662Svikram 
5672eb2bd662Svikram 	while (s_fgets(tmpsign, sizeof (tmpsign), bfp) != NULL) {
5673eb2bd662Svikram 
5674eb2bd662Svikram 		if (lu == NULL &&
5675eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_LU_PREFIX,
5676eb2bd662Svikram 		    strlen(GRUBSIGN_LU_PREFIX)) == 0) {
5677eb2bd662Svikram 			lu = s_strdup(tmpsign);
5678eb2bd662Svikram 		}
5679eb2bd662Svikram 
5680eb2bd662Svikram 		if (ufs == NULL &&
5681eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
5682eb2bd662Svikram 		    strlen(GRUBSIGN_UFS_PREFIX)) == 0) {
5683eb2bd662Svikram 			ufs = s_strdup(tmpsign);
5684eb2bd662Svikram 		}
5685eb2bd662Svikram 
5686eb2bd662Svikram 		if (zfs == NULL &&
5687eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_ZFS_PREFIX,
5688eb2bd662Svikram 		    strlen(GRUBSIGN_ZFS_PREFIX)) == 0) {
5689eb2bd662Svikram 			zfs = s_strdup(tmpsign);
5690eb2bd662Svikram 		}
5691eb2bd662Svikram 	}
5692eb2bd662Svikram 
5693*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: existing backup signs: zfs=%s ufs=%s lu=%s\n", fcn,
5694eb2bd662Svikram 	    zfs ? zfs : "NULL",
5695eb2bd662Svikram 	    ufs ? ufs : "NULL",
5696eb2bd662Svikram 	    lu ? lu : "NULL"));
5697eb2bd662Svikram 
5698eb2bd662Svikram 	if (bfp) {
5699eb2bd662Svikram 		(void) fclose(bfp);
5700eb2bd662Svikram 		bfp = NULL;
5701eb2bd662Svikram 	}
5702eb2bd662Svikram 
5703eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0 && zfs) {
5704*f64ca102SToomas Soome 		bam_error(_("found mismatched boot signature %s for "
5705*f64ca102SToomas Soome 		    "filesystem type: %s.\n"), zfs, "ufs");
5706eb2bd662Svikram 		free(zfs);
5707eb2bd662Svikram 		zfs = NULL;
5708eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0 && ufs) {
5709*f64ca102SToomas Soome 		bam_error(_("found mismatched boot signature %s for "
5710*f64ca102SToomas Soome 		    "filesystem type: %s.\n"), ufs, "zfs");
5711eb2bd662Svikram 		free(ufs);
5712eb2bd662Svikram 		ufs = NULL;
5713eb2bd662Svikram 	}
5714eb2bd662Svikram 
5715eb2bd662Svikram 	assert(bfp == NULL);
5716eb2bd662Svikram 
5717eb2bd662Svikram 	/* For now, we let Live Upgrade take care of its signature itself */
5718eb2bd662Svikram 	if (lu) {
5719*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: feeing LU sign: %s\n", fcn, lu));
5720eb2bd662Svikram 		free(lu);
5721eb2bd662Svikram 		lu = NULL;
5722eb2bd662Svikram 	}
5723eb2bd662Svikram 
5724eb2bd662Svikram 	return (zfs ? zfs : ufs);
5725eb2bd662Svikram }
5726eb2bd662Svikram 
5727eb2bd662Svikram static char *
5728eb2bd662Svikram find_ufs_existing(char *osroot)
5729eb2bd662Svikram {
5730eb2bd662Svikram 	char		*sign;
5731eb2bd662Svikram 	const char	*fcn = "find_ufs_existing()";
5732eb2bd662Svikram 
5733eb2bd662Svikram 	sign = find_primary_common(osroot, "ufs");
5734eb2bd662Svikram 	if (sign == NULL) {
5735eb2bd662Svikram 		sign = find_backup_common(osroot, "ufs");
5736*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: existing backup sign: %s\n", fcn,
5737*f64ca102SToomas Soome 		    sign ? sign : "NULL"));
5738eb2bd662Svikram 	} else {
5739*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: existing primary sign: %s\n", fcn, sign));
5740eb2bd662Svikram 	}
5741eb2bd662Svikram 
5742eb2bd662Svikram 	return (sign);
5743eb2bd662Svikram }
5744eb2bd662Svikram 
5745eb2bd662Svikram char *
5746eb2bd662Svikram get_mountpoint(char *special, char *fstype)
5747eb2bd662Svikram {
5748eb2bd662Svikram 	FILE		*mntfp;
5749eb2bd662Svikram 	struct mnttab	mp = {0};
5750eb2bd662Svikram 	struct mnttab	mpref = {0};
5751eb2bd662Svikram 	int		error;
5752eb2bd662Svikram 	int		ret;
5753eb2bd662Svikram 	const char	*fcn = "get_mountpoint()";
5754eb2bd662Svikram 
5755*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, special, fstype));
5756eb2bd662Svikram 
5757eb2bd662Svikram 	mntfp = fopen(MNTTAB, "r");
5758eb2bd662Svikram 	error = errno;
5759eb2bd662Svikram 	INJECT_ERROR1("MNTTAB_ERR_GET_MNTPT", mntfp = NULL);
5760eb2bd662Svikram 	if (mntfp == NULL) {
5761*f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"),
5762*f64ca102SToomas Soome 		    MNTTAB, strerror(error));
5763eb2bd662Svikram 		return (NULL);
5764eb2bd662Svikram 	}
5765eb2bd662Svikram 
5766eb2bd662Svikram 	mpref.mnt_special = special;
5767eb2bd662Svikram 	mpref.mnt_fstype = fstype;
5768eb2bd662Svikram 
5769eb2bd662Svikram 	ret = getmntany(mntfp, &mp, &mpref);
5770eb2bd662Svikram 	INJECT_ERROR1("GET_MOUNTPOINT_MNTANY", ret = 1);
5771eb2bd662Svikram 	if (ret != 0) {
5772eb2bd662Svikram 		(void) fclose(mntfp);
5773*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: no mount-point for special=%s and "
5774*f64ca102SToomas Soome 		    "fstype=%s\n", fcn, special, fstype));
5775eb2bd662Svikram 		return (NULL);
5776eb2bd662Svikram 	}
5777eb2bd662Svikram 	(void) fclose(mntfp);
5778eb2bd662Svikram 
5779eb2bd662Svikram 	assert(mp.mnt_mountp);
5780eb2bd662Svikram 
5781*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning mount-point for special %s: %s\n",
5782*f64ca102SToomas Soome 	    fcn, special, mp.mnt_mountp));
5783eb2bd662Svikram 
5784eb2bd662Svikram 	return (s_strdup(mp.mnt_mountp));
5785eb2bd662Svikram }
5786eb2bd662Svikram 
5787eb2bd662Svikram /*
5788eb2bd662Svikram  * Mounts a "legacy" top dataset (if needed)
5789eb2bd662Svikram  * Returns:	The mountpoint of the legacy top dataset or NULL on error
5790eb2bd662Svikram  * 		mnted returns one of the above values defined for zfs_mnted_t
5791eb2bd662Svikram  */
5792eb2bd662Svikram static char *
5793eb2bd662Svikram mount_legacy_dataset(char *pool, zfs_mnted_t *mnted)
5794eb2bd662Svikram {
5795eb2bd662Svikram 	char		cmd[PATH_MAX];
5796eb2bd662Svikram 	char		tmpmnt[PATH_MAX];
5797eb2bd662Svikram 	filelist_t	flist = {0};
5798eb2bd662Svikram 	char		*is_mounted;
5799eb2bd662Svikram 	struct stat	sb;
5800eb2bd662Svikram 	int		ret;
5801eb2bd662Svikram 	const char	*fcn = "mount_legacy_dataset()";
5802eb2bd662Svikram 
5803*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. arg: %s\n", fcn, pool));
5804eb2bd662Svikram 
5805eb2bd662Svikram 	*mnted = ZFS_MNT_ERROR;
5806eb2bd662Svikram 
5807eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5808eb2bd662Svikram 	    "/sbin/zfs get -Ho value mounted %s",
5809eb2bd662Svikram 	    pool);
5810eb2bd662Svikram 
5811eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5812eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_CMD", ret = 1);
5813eb2bd662Svikram 	if (ret != 0) {
5814*f64ca102SToomas Soome 		bam_error(_("failed to determine mount status of ZFS "
5815*f64ca102SToomas Soome 		    "pool %s\n"), pool);
5816eb2bd662Svikram 		return (NULL);
5817eb2bd662Svikram 	}
5818eb2bd662Svikram 
5819eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_OUT", flist.head = NULL);
5820eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5821*f64ca102SToomas Soome 		bam_error(_("ZFS pool %s has bad mount status\n"), pool);
5822eb2bd662Svikram 		filelist_free(&flist);
5823eb2bd662Svikram 		return (NULL);
5824eb2bd662Svikram 	}
5825eb2bd662Svikram 
5826eb2bd662Svikram 	is_mounted = strtok(flist.head->line, " \t\n");
5827eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_STRTOK_YES", is_mounted = "yes");
5828eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_STRTOK_NO", is_mounted = "no");
5829eb2bd662Svikram 	if (strcmp(is_mounted, "no") != 0) {
5830eb2bd662Svikram 		filelist_free(&flist);
5831eb2bd662Svikram 		*mnted = LEGACY_ALREADY;
5832eb2bd662Svikram 		/* get_mountpoint returns a strdup'ed string */
5833*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: legacy pool %s already mounted\n",
5834*f64ca102SToomas Soome 		    fcn, pool));
5835eb2bd662Svikram 		return (get_mountpoint(pool, "zfs"));
5836eb2bd662Svikram 	}
5837eb2bd662Svikram 
5838eb2bd662Svikram 	filelist_free(&flist);
5839eb2bd662Svikram 
5840eb2bd662Svikram 	/*
5841eb2bd662Svikram 	 * legacy top dataset is not mounted. Mount it now
5842eb2bd662Svikram 	 * First create a mountpoint.
5843eb2bd662Svikram 	 */
5844eb2bd662Svikram 	(void) snprintf(tmpmnt, sizeof (tmpmnt), "%s.%d",
5845eb2bd662Svikram 	    ZFS_LEGACY_MNTPT, getpid());
5846eb2bd662Svikram 
5847eb2bd662Svikram 	ret = stat(tmpmnt, &sb);
5848eb2bd662Svikram 	if (ret == -1) {
5849*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: legacy pool %s mount-point %s absent\n",
5850*f64ca102SToomas Soome 		    fcn, pool, tmpmnt));
585148847494SEnrico Perla - Sun Microsystems 		ret = mkdirp(tmpmnt, DIR_PERMS);
5852eb2bd662Svikram 		INJECT_ERROR1("Z_MOUNT_TOP_LEG_MNTPT_MKDIRP", ret = -1);
5853eb2bd662Svikram 		if (ret == -1) {
5854*f64ca102SToomas Soome 			bam_error(_("mkdir of %s failed: %s\n"), tmpmnt,
5855*f64ca102SToomas Soome 			    strerror(errno));
5856eb2bd662Svikram 			return (NULL);
5857eb2bd662Svikram 		}
5858eb2bd662Svikram 	} else {
5859*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: legacy pool %s mount-point %s is already "
5860*f64ca102SToomas Soome 		    "present\n", fcn, pool, tmpmnt));
5861eb2bd662Svikram 	}
5862eb2bd662Svikram 
5863eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5864eb2bd662Svikram 	    "/sbin/mount -F zfs %s %s",
5865eb2bd662Svikram 	    pool, tmpmnt);
5866eb2bd662Svikram 
5867eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
5868eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_LEG_MOUNT_CMD", ret = 1);
5869eb2bd662Svikram 	if (ret != 0) {
5870*f64ca102SToomas Soome 		bam_error(_("mount of ZFS pool %s failed\n"), pool);
5871eb2bd662Svikram 		(void) rmdir(tmpmnt);
5872eb2bd662Svikram 		return (NULL);
5873eb2bd662Svikram 	}
5874eb2bd662Svikram 
5875eb2bd662Svikram 	*mnted = LEGACY_MOUNTED;
5876*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: legacy pool %s successfully mounted at %s\n",
5877*f64ca102SToomas Soome 	    fcn, pool, tmpmnt));
5878eb2bd662Svikram 	return (s_strdup(tmpmnt));
5879eb2bd662Svikram }
5880eb2bd662Svikram 
5881eb2bd662Svikram /*
5882eb2bd662Svikram  * Mounts the top dataset (if needed)
5883eb2bd662Svikram  * Returns:	The mountpoint of the top dataset or NULL on error
5884eb2bd662Svikram  * 		mnted returns one of the above values defined for zfs_mnted_t
5885eb2bd662Svikram  */
5886*f64ca102SToomas Soome char *
5887eb2bd662Svikram mount_top_dataset(char *pool, zfs_mnted_t *mnted)
5888eb2bd662Svikram {
5889eb2bd662Svikram 	char		cmd[PATH_MAX];
5890eb2bd662Svikram 	filelist_t	flist = {0};
5891eb2bd662Svikram 	char		*is_mounted;
5892eb2bd662Svikram 	char		*mntpt;
5893eb2bd662Svikram 	char		*zmntpt;
5894eb2bd662Svikram 	int		ret;
5895eb2bd662Svikram 	const char	*fcn = "mount_top_dataset()";
5896eb2bd662Svikram 
5897eb2bd662Svikram 	*mnted = ZFS_MNT_ERROR;
5898eb2bd662Svikram 
5899*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. arg: %s\n", fcn, pool));
5900eb2bd662Svikram 
5901eb2bd662Svikram 	/*
5902eb2bd662Svikram 	 * First check if the top dataset is a "legacy" dataset
5903eb2bd662Svikram 	 */
5904eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5905eb2bd662Svikram 	    "/sbin/zfs get -Ho value mountpoint %s",
5906eb2bd662Svikram 	    pool);
5907eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5908eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_GET_MNTPT", ret = 1);
5909eb2bd662Svikram 	if (ret != 0) {
5910*f64ca102SToomas Soome 		bam_error(_("failed to determine mount point of ZFS pool %s\n"),
5911*f64ca102SToomas Soome 		    pool);
5912eb2bd662Svikram 		return (NULL);
5913eb2bd662Svikram 	}
5914eb2bd662Svikram 
5915eb2bd662Svikram 	if (flist.head && (flist.head == flist.tail)) {
5916eb2bd662Svikram 		char *legacy = strtok(flist.head->line, " \t\n");
5917eb2bd662Svikram 		if (legacy && strcmp(legacy, "legacy") == 0) {
5918eb2bd662Svikram 			filelist_free(&flist);
5919*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: is legacy, pool=%s\n", fcn, pool));
5920eb2bd662Svikram 			return (mount_legacy_dataset(pool, mnted));
5921eb2bd662Svikram 		}
5922eb2bd662Svikram 	}
5923eb2bd662Svikram 
5924eb2bd662Svikram 	filelist_free(&flist);
5925eb2bd662Svikram 
5926*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: is *NOT* legacy, pool=%s\n", fcn, pool));
5927eb2bd662Svikram 
5928eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5929eb2bd662Svikram 	    "/sbin/zfs get -Ho value mounted %s",
5930eb2bd662Svikram 	    pool);
5931eb2bd662Svikram 
5932eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5933eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED", ret = 1);
5934eb2bd662Svikram 	if (ret != 0) {
5935*f64ca102SToomas Soome 		bam_error(_("failed to determine mount status of ZFS "
5936*f64ca102SToomas Soome 		    "pool %s\n"), pool);
5937eb2bd662Svikram 		return (NULL);
5938eb2bd662Svikram 	}
5939eb2bd662Svikram 
5940eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_VAL", flist.head = NULL);
5941eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5942*f64ca102SToomas Soome 		bam_error(_("ZFS pool %s has bad mount status\n"), pool);
5943eb2bd662Svikram 		filelist_free(&flist);
5944eb2bd662Svikram 		return (NULL);
5945eb2bd662Svikram 	}
5946eb2bd662Svikram 
5947eb2bd662Svikram 	is_mounted = strtok(flist.head->line, " \t\n");
5948eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_YES", is_mounted = "yes");
5949eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_NO", is_mounted = "no");
5950eb2bd662Svikram 	if (strcmp(is_mounted, "no") != 0) {
5951eb2bd662Svikram 		filelist_free(&flist);
5952eb2bd662Svikram 		*mnted = ZFS_ALREADY;
5953*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: non-legacy pool %s mounted already\n",
5954*f64ca102SToomas Soome 		    fcn, pool));
5955eb2bd662Svikram 		goto mounted;
5956eb2bd662Svikram 	}
5957eb2bd662Svikram 
5958eb2bd662Svikram 	filelist_free(&flist);
5959*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: non-legacy pool %s *NOT* already mounted\n",
5960*f64ca102SToomas Soome 	    fcn, pool));
5961eb2bd662Svikram 
5962eb2bd662Svikram 	/* top dataset is not mounted. Mount it now */
5963eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5964eb2bd662Svikram 	    "/sbin/zfs mount %s", pool);
5965eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
5966eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_MOUNT_CMD", ret = 1);
5967eb2bd662Svikram 	if (ret != 0) {
5968*f64ca102SToomas Soome 		bam_error(_("mount of ZFS pool %s failed\n"), pool);
5969eb2bd662Svikram 		return (NULL);
5970eb2bd662Svikram 	}
5971eb2bd662Svikram 	*mnted = ZFS_MOUNTED;
5972*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: non-legacy pool %s mounted now\n", fcn, pool));
5973eb2bd662Svikram 	/*FALLTHRU*/
5974eb2bd662Svikram mounted:
5975eb2bd662Svikram 	/*
5976eb2bd662Svikram 	 * Now get the mountpoint
5977eb2bd662Svikram 	 */
5978eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5979eb2bd662Svikram 	    "/sbin/zfs get -Ho value mountpoint %s",
5980eb2bd662Svikram 	    pool);
5981eb2bd662Svikram 
5982eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5983eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_CMD", ret = 1);
5984eb2bd662Svikram 	if (ret != 0) {
5985*f64ca102SToomas Soome 		bam_error(_("failed to determine mount point of ZFS pool %s\n"),
5986*f64ca102SToomas Soome 		    pool);
5987eb2bd662Svikram 		goto error;
5988eb2bd662Svikram 	}
5989eb2bd662Svikram 
5990eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_OUT", flist.head = NULL);
5991eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5992*f64ca102SToomas Soome 		bam_error(_("ZFS pool %s has no mount-point\n"), pool);
5993eb2bd662Svikram 		goto error;
5994eb2bd662Svikram 	}
5995eb2bd662Svikram 
5996eb2bd662Svikram 	mntpt = strtok(flist.head->line, " \t\n");
5997eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_STRTOK", mntpt = "foo");
5998eb2bd662Svikram 	if (*mntpt != '/') {
5999*f64ca102SToomas Soome 		bam_error(_("ZFS pool %s has bad mount-point %s\n"),
6000*f64ca102SToomas Soome 		    pool, mntpt);
6001eb2bd662Svikram 		goto error;
6002eb2bd662Svikram 	}
6003eb2bd662Svikram 	zmntpt = s_strdup(mntpt);
6004eb2bd662Svikram 
6005eb2bd662Svikram 	filelist_free(&flist);
6006eb2bd662Svikram 
6007*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: non-legacy pool %s is mounted at %s\n",
6008*f64ca102SToomas Soome 	    fcn, pool, zmntpt));
6009eb2bd662Svikram 
6010eb2bd662Svikram 	return (zmntpt);
6011eb2bd662Svikram 
6012eb2bd662Svikram error:
6013eb2bd662Svikram 	filelist_free(&flist);
6014eb2bd662Svikram 	(void) umount_top_dataset(pool, *mnted, NULL);
6015*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
6016eb2bd662Svikram 	return (NULL);
6017eb2bd662Svikram }
6018eb2bd662Svikram 
6019*f64ca102SToomas Soome int
6020eb2bd662Svikram umount_top_dataset(char *pool, zfs_mnted_t mnted, char *mntpt)
6021eb2bd662Svikram {
6022eb2bd662Svikram 	char		cmd[PATH_MAX];
6023eb2bd662Svikram 	int		ret;
6024eb2bd662Svikram 	const char	*fcn = "umount_top_dataset()";
6025eb2bd662Svikram 
6026eb2bd662Svikram 	INJECT_ERROR1("Z_UMOUNT_TOP_INVALID_STATE", mnted = ZFS_MNT_ERROR);
6027eb2bd662Svikram 	switch (mnted) {
6028eb2bd662Svikram 	case LEGACY_ALREADY:
6029eb2bd662Svikram 	case ZFS_ALREADY:
6030eb2bd662Svikram 		/* nothing to do */
6031*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: pool %s was already mounted at %s, Nothing "
6032*f64ca102SToomas Soome 		    "to umount\n", fcn, pool, mntpt ? mntpt : "NULL"));
6033eb2bd662Svikram 		free(mntpt);
6034eb2bd662Svikram 		return (BAM_SUCCESS);
6035eb2bd662Svikram 	case LEGACY_MOUNTED:
6036eb2bd662Svikram 		(void) snprintf(cmd, sizeof (cmd),
6037eb2bd662Svikram 		    "/sbin/umount %s", pool);
6038eb2bd662Svikram 		ret = exec_cmd(cmd, NULL);
6039eb2bd662Svikram 		INJECT_ERROR1("Z_UMOUNT_TOP_LEGACY_UMOUNT_FAIL", ret = 1);
6040eb2bd662Svikram 		if (ret != 0) {
6041*f64ca102SToomas Soome 			bam_error(_("umount of %s failed\n"), pool);
6042eb2bd662Svikram 			free(mntpt);
6043eb2bd662Svikram 			return (BAM_ERROR);
6044eb2bd662Svikram 		}
6045eb2bd662Svikram 		if (mntpt)
6046eb2bd662Svikram 			(void) rmdir(mntpt);
6047eb2bd662Svikram 		free(mntpt);
6048*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: legacy pool %s was mounted by us, "
6049*f64ca102SToomas Soome 		    "successfully unmounted\n", fcn, pool));
6050eb2bd662Svikram 		return (BAM_SUCCESS);
6051eb2bd662Svikram 	case ZFS_MOUNTED:
6052eb2bd662Svikram 		free(mntpt);
6053eb2bd662Svikram 		(void) snprintf(cmd, sizeof (cmd),
6054eb2bd662Svikram 		    "/sbin/zfs unmount %s", pool);
6055eb2bd662Svikram 		ret = exec_cmd(cmd, NULL);
6056eb2bd662Svikram 		INJECT_ERROR1("Z_UMOUNT_TOP_NONLEG_UMOUNT_FAIL", ret = 1);
6057eb2bd662Svikram 		if (ret != 0) {
6058*f64ca102SToomas Soome 			bam_error(_("umount of %s failed\n"), pool);
6059eb2bd662Svikram 			return (BAM_ERROR);
6060eb2bd662Svikram 		}
6061*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: nonleg pool %s was mounted by us, "
6062*f64ca102SToomas Soome 		    "successfully unmounted\n", fcn, pool));
6063eb2bd662Svikram 		return (BAM_SUCCESS);
6064eb2bd662Svikram 	default:
6065*f64ca102SToomas Soome 		bam_error(_("Internal error: bad saved mount state for "
6066*f64ca102SToomas Soome 		    "pool %s\n"), pool);
6067eb2bd662Svikram 		return (BAM_ERROR);
6068eb2bd662Svikram 	}
6069eb2bd662Svikram 	/*NOTREACHED*/
6070eb2bd662Svikram }
6071eb2bd662Svikram 
6072eb2bd662Svikram /*
6073eb2bd662Svikram  * For ZFS, osdev can be one of two forms
6074eb2bd662Svikram  * It can be a "special" file as seen in mnttab: rpool/ROOT/szboot_0402
6075eb2bd662Svikram  * It can be a /dev/[r]dsk special file. We handle both instances
6076eb2bd662Svikram  */
6077eb2bd662Svikram static char *
6078eb2bd662Svikram get_pool(char *osdev)
6079eb2bd662Svikram {
6080eb2bd662Svikram 	char		cmd[PATH_MAX];
6081eb2bd662Svikram 	char		buf[PATH_MAX];
6082eb2bd662Svikram 	filelist_t	flist = {0};
6083eb2bd662Svikram 	char		*pool;
6084eb2bd662Svikram 	char		*cp;
6085eb2bd662Svikram 	char		*slash;
6086eb2bd662Svikram 	int		ret;
6087eb2bd662Svikram 	const char	*fcn = "get_pool()";
6088eb2bd662Svikram 
6089eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_OSDEV", osdev = NULL);
6090eb2bd662Svikram 	if (osdev == NULL) {
6091*f64ca102SToomas Soome 		bam_error(_("NULL device: cannot determine pool name\n"));
6092eb2bd662Svikram 		return (NULL);
6093eb2bd662Svikram 	}
6094eb2bd662Svikram 
6095*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: osdev arg = %s\n", fcn, osdev));
6096eb2bd662Svikram 
6097eb2bd662Svikram 	if (osdev[0] != '/') {
6098eb2bd662Svikram 		(void) strlcpy(buf, osdev, sizeof (buf));
6099eb2bd662Svikram 		slash = strchr(buf, '/');
6100eb2bd662Svikram 		if (slash)
6101eb2bd662Svikram 			*slash = '\0';
6102eb2bd662Svikram 		pool = s_strdup(buf);
6103*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: got pool. pool = %s\n", fcn, pool));
6104eb2bd662Svikram 		return (pool);
6105eb2bd662Svikram 	} else if (strncmp(osdev, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
6106eb2bd662Svikram 	    strncmp(osdev, "/dev/rdsk/", strlen("/dev/rdsk/")) != 0) {
6107*f64ca102SToomas Soome 		bam_error(_("invalid device %s: cannot determine pool name\n"),
6108*f64ca102SToomas Soome 		    osdev);
6109eb2bd662Svikram 		return (NULL);
6110eb2bd662Svikram 	}
6111eb2bd662Svikram 
6112d5cb36d3SJan Setje-Eilers 	/*
6113d5cb36d3SJan Setje-Eilers 	 * Call the zfs fstyp directly since this is a zpool. This avoids
6114d5cb36d3SJan Setje-Eilers 	 * potential pcfs conflicts if the first block wasn't cleared.
6115d5cb36d3SJan Setje-Eilers 	 */
6116eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6117d5cb36d3SJan Setje-Eilers 	    "/usr/lib/fs/zfs/fstyp -a %s 2>/dev/null | /bin/grep '^name:'",
6118eb2bd662Svikram 	    osdev);
6119eb2bd662Svikram 
6120eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
6121eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_FSTYP", ret = 1);
6122eb2bd662Svikram 	if (ret != 0) {
6123*f64ca102SToomas Soome 		bam_error(_("fstyp -a on device %s failed\n"), osdev);
6124eb2bd662Svikram 		return (NULL);
6125eb2bd662Svikram 	}
6126eb2bd662Svikram 
6127eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_FSTYP_OUT", flist.head = NULL);
6128eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
6129*f64ca102SToomas Soome 		bam_error(_("NULL fstyp -a output for device %s\n"), osdev);
6130eb2bd662Svikram 		filelist_free(&flist);
6131eb2bd662Svikram 		return (NULL);
6132eb2bd662Svikram 	}
6133eb2bd662Svikram 
6134eb2bd662Svikram 	(void) strtok(flist.head->line, "'");
6135eb2bd662Svikram 	cp = strtok(NULL, "'");
6136eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_FSTYP_STRTOK", cp = NULL);
6137eb2bd662Svikram 	if (cp == NULL) {
6138*f64ca102SToomas Soome 		bam_error(_("bad fstyp -a output for device %s\n"), osdev);
6139eb2bd662Svikram 		filelist_free(&flist);
6140eb2bd662Svikram 		return (NULL);
6141eb2bd662Svikram 	}
6142eb2bd662Svikram 
6143eb2bd662Svikram 	pool = s_strdup(cp);
6144eb2bd662Svikram 
6145eb2bd662Svikram 	filelist_free(&flist);
6146eb2bd662Svikram 
6147*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: got pool. pool = %s\n", fcn, pool));
6148eb2bd662Svikram 
6149eb2bd662Svikram 	return (pool);
6150eb2bd662Svikram }
6151eb2bd662Svikram 
6152eb2bd662Svikram static char *
6153eb2bd662Svikram find_zfs_existing(char *osdev)
6154eb2bd662Svikram {
6155eb2bd662Svikram 	char		*pool;
6156eb2bd662Svikram 	zfs_mnted_t	mnted;
6157eb2bd662Svikram 	char		*mntpt;
6158eb2bd662Svikram 	char		*sign;
6159eb2bd662Svikram 	const char	*fcn = "find_zfs_existing()";
6160eb2bd662Svikram 
6161eb2bd662Svikram 	pool = get_pool(osdev);
6162eb2bd662Svikram 	INJECT_ERROR1("ZFS_FIND_EXIST_POOL", pool = NULL);
6163eb2bd662Svikram 	if (pool == NULL) {
6164*f64ca102SToomas Soome 		bam_error(_("failed to get pool for device: %s\n"), osdev);
6165eb2bd662Svikram 		return (NULL);
6166eb2bd662Svikram 	}
6167eb2bd662Svikram 
6168eb2bd662Svikram 	mntpt = mount_top_dataset(pool, &mnted);
6169eb2bd662Svikram 	INJECT_ERROR1("ZFS_FIND_EXIST_MOUNT_TOP", mntpt = NULL);
6170eb2bd662Svikram 	if (mntpt == NULL) {
6171*f64ca102SToomas Soome 		bam_error(_("failed to mount top dataset for pool: %s\n"),
6172*f64ca102SToomas Soome 		    pool);
6173eb2bd662Svikram 		free(pool);
6174eb2bd662Svikram 		return (NULL);
6175eb2bd662Svikram 	}
6176eb2bd662Svikram 
6177eb2bd662Svikram 	sign = find_primary_common(mntpt, "zfs");
6178eb2bd662Svikram 	if (sign == NULL) {
6179eb2bd662Svikram 		sign = find_backup_common(mntpt, "zfs");
6180*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: existing backup sign: %s\n", fcn,
6181*f64ca102SToomas Soome 		    sign ? sign : "NULL"));
6182eb2bd662Svikram 	} else {
6183*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: existing primary sign: %s\n", fcn, sign));
6184eb2bd662Svikram 	}
6185eb2bd662Svikram 
6186eb2bd662Svikram 	(void) umount_top_dataset(pool, mnted, mntpt);
6187eb2bd662Svikram 
6188eb2bd662Svikram 	free(pool);
6189eb2bd662Svikram 
6190eb2bd662Svikram 	return (sign);
6191eb2bd662Svikram }
6192eb2bd662Svikram 
6193eb2bd662Svikram static char *
6194eb2bd662Svikram find_existing_sign(char *osroot, char *osdev, char *fstype)
6195eb2bd662Svikram {
6196eb2bd662Svikram 	const char		*fcn = "find_existing_sign()";
6197eb2bd662Svikram 
6198eb2bd662Svikram 	INJECT_ERROR1("FIND_EXIST_NOTSUP_FS", fstype = "foofs");
6199eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0) {
6200*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: checking for existing UFS sign\n", fcn));
6201eb2bd662Svikram 		return (find_ufs_existing(osroot));
6202eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0) {
6203*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: checking for existing ZFS sign\n", fcn));
6204eb2bd662Svikram 		return (find_zfs_existing(osdev));
6205eb2bd662Svikram 	} else {
6206*f64ca102SToomas Soome 		bam_error(_("boot signature not supported for fstype: %s\n"),
6207*f64ca102SToomas Soome 		    fstype);
6208eb2bd662Svikram 		return (NULL);
6209eb2bd662Svikram 	}
6210eb2bd662Svikram }
6211eb2bd662Svikram 
6212eb2bd662Svikram #define	MH_HASH_SZ	16
6213eb2bd662Svikram 
6214eb2bd662Svikram typedef enum {
6215eb2bd662Svikram 	MH_ERROR = -1,
6216eb2bd662Svikram 	MH_NOMATCH,
6217eb2bd662Svikram 	MH_MATCH
6218eb2bd662Svikram } mh_search_t;
6219eb2bd662Svikram 
6220eb2bd662Svikram typedef struct mcache {
6221eb2bd662Svikram 	char	*mc_special;
6222eb2bd662Svikram 	char	*mc_mntpt;
6223eb2bd662Svikram 	char	*mc_fstype;
6224eb2bd662Svikram 	struct mcache *mc_next;
6225eb2bd662Svikram } mcache_t;
6226eb2bd662Svikram 
6227eb2bd662Svikram typedef struct mhash {
6228eb2bd662Svikram 	mcache_t *mh_hash[MH_HASH_SZ];
6229eb2bd662Svikram } mhash_t;
6230eb2bd662Svikram 
6231eb2bd662Svikram static int
6232eb2bd662Svikram mhash_fcn(char *key)
6233eb2bd662Svikram {
6234eb2bd662Svikram 	int		i;
6235eb2bd662Svikram 	uint64_t	sum = 0;
6236eb2bd662Svikram 
6237eb2bd662Svikram 	for (i = 0; key[i] != '\0'; i++) {
6238eb2bd662Svikram 		sum += (uchar_t)key[i];
6239eb2bd662Svikram 	}
6240eb2bd662Svikram 
6241eb2bd662Svikram 	sum %= MH_HASH_SZ;
6242eb2bd662Svikram 
6243eb2bd662Svikram 	assert(sum < MH_HASH_SZ);
6244eb2bd662Svikram 
6245eb2bd662Svikram 	return (sum);
6246eb2bd662Svikram }
6247eb2bd662Svikram 
6248eb2bd662Svikram static mhash_t *
6249eb2bd662Svikram cache_mnttab(void)
6250eb2bd662Svikram {
6251eb2bd662Svikram 	FILE		*mfp;
6252eb2bd662Svikram 	struct extmnttab mnt;
6253eb2bd662Svikram 	mcache_t	*mcp;
6254eb2bd662Svikram 	mhash_t		*mhp;
6255eb2bd662Svikram 	char		*ctds;
6256eb2bd662Svikram 	int		idx;
6257eb2bd662Svikram 	int		error;
6258eb2bd662Svikram 	char		*special_dup;
6259eb2bd662Svikram 	const char	*fcn = "cache_mnttab()";
6260eb2bd662Svikram 
6261eb2bd662Svikram 	mfp = fopen(MNTTAB, "r");
6262eb2bd662Svikram 	error = errno;
6263eb2bd662Svikram 	INJECT_ERROR1("CACHE_MNTTAB_MNTTAB_ERR", mfp = NULL);
6264eb2bd662Svikram 	if (mfp == NULL) {
6265*f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), MNTTAB,
6266*f64ca102SToomas Soome 		    strerror(error));
6267eb2bd662Svikram 		return (NULL);
6268eb2bd662Svikram 	}
6269eb2bd662Svikram 
6270eb2bd662Svikram 	mhp = s_calloc(1, sizeof (mhash_t));
6271eb2bd662Svikram 
6272eb2bd662Svikram 	resetmnttab(mfp);
6273eb2bd662Svikram 
6274eb2bd662Svikram 	while (getextmntent(mfp, &mnt, sizeof (mnt)) == 0) {
6275eb2bd662Svikram 		/* only cache ufs */
6276eb2bd662Svikram 		if (strcmp(mnt.mnt_fstype, "ufs") != 0)
6277eb2bd662Svikram 			continue;
6278eb2bd662Svikram 
6279eb2bd662Svikram 		/* basename() modifies its arg, so dup it */
6280eb2bd662Svikram 		special_dup = s_strdup(mnt.mnt_special);
6281eb2bd662Svikram 		ctds = basename(special_dup);
6282eb2bd662Svikram 
6283eb2bd662Svikram 		mcp = s_calloc(1, sizeof (mcache_t));
6284eb2bd662Svikram 		mcp->mc_special = s_strdup(ctds);
6285eb2bd662Svikram 		mcp->mc_mntpt = s_strdup(mnt.mnt_mountp);
6286eb2bd662Svikram 		mcp->mc_fstype = s_strdup(mnt.mnt_fstype);
6287*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: caching mount: special=%s, mntpt=%s, "
6288*f64ca102SToomas Soome 		    "fstype=%s\n", fcn, ctds, mnt.mnt_mountp, mnt.mnt_fstype));
6289eb2bd662Svikram 		idx = mhash_fcn(ctds);
6290eb2bd662Svikram 		mcp->mc_next = mhp->mh_hash[idx];
6291eb2bd662Svikram 		mhp->mh_hash[idx] = mcp;
6292eb2bd662Svikram 		free(special_dup);
6293eb2bd662Svikram 	}
6294eb2bd662Svikram 
6295eb2bd662Svikram 	(void) fclose(mfp);
6296eb2bd662Svikram 
6297eb2bd662Svikram 	return (mhp);
6298eb2bd662Svikram }
6299eb2bd662Svikram 
6300eb2bd662Svikram static void
6301eb2bd662Svikram free_mnttab(mhash_t *mhp)
6302eb2bd662Svikram {
6303eb2bd662Svikram 	mcache_t	*mcp;
6304eb2bd662Svikram 	int		i;
6305eb2bd662Svikram 
6306eb2bd662Svikram 	for (i = 0; i < MH_HASH_SZ; i++) {
6307110570ceSToomas Soome 		while ((mcp = mhp->mh_hash[i]) != NULL) {
6308eb2bd662Svikram 			mhp->mh_hash[i] = mcp->mc_next;
6309eb2bd662Svikram 			free(mcp->mc_special);
6310eb2bd662Svikram 			free(mcp->mc_mntpt);
6311eb2bd662Svikram 			free(mcp->mc_fstype);
6312eb2bd662Svikram 			free(mcp);
6313eb2bd662Svikram 		}
6314eb2bd662Svikram 	}
6315eb2bd662Svikram 
6316eb2bd662Svikram 	for (i = 0; i < MH_HASH_SZ; i++) {
6317eb2bd662Svikram 		assert(mhp->mh_hash[i] == NULL);
6318eb2bd662Svikram 	}
6319eb2bd662Svikram 	free(mhp);
6320eb2bd662Svikram }
6321eb2bd662Svikram 
6322eb2bd662Svikram static mh_search_t
6323eb2bd662Svikram search_hash(mhash_t *mhp, char *special, char **mntpt)
6324eb2bd662Svikram {
6325eb2bd662Svikram 	int		idx;
6326eb2bd662Svikram 	mcache_t	*mcp;
6327eb2bd662Svikram 	const char 	*fcn = "search_hash()";
6328eb2bd662Svikram 
6329eb2bd662Svikram 	assert(mntpt);
6330eb2bd662Svikram 
6331eb2bd662Svikram 	*mntpt = NULL;
6332eb2bd662Svikram 
6333eb2bd662Svikram 	INJECT_ERROR1("SEARCH_HASH_FULL_PATH", special = "/foo");
6334eb2bd662Svikram 	if (strchr(special, '/')) {
6335*f64ca102SToomas Soome 		bam_error(_("invalid key for mnttab hash: %s\n"), special);
6336eb2bd662Svikram 		return (MH_ERROR);
6337eb2bd662Svikram 	}
6338eb2bd662Svikram 
6339eb2bd662Svikram 	idx = mhash_fcn(special);
6340eb2bd662Svikram 
6341eb2bd662Svikram 	for (mcp = mhp->mh_hash[idx]; mcp; mcp = mcp->mc_next) {
6342eb2bd662Svikram 		if (strcmp(mcp->mc_special, special) == 0)
6343eb2bd662Svikram 			break;
6344eb2bd662Svikram 	}
6345eb2bd662Svikram 
6346eb2bd662Svikram 	if (mcp == NULL) {
6347*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: no match in cache for: %s\n", fcn, special));
6348eb2bd662Svikram 		return (MH_NOMATCH);
6349eb2bd662Svikram 	}
6350eb2bd662Svikram 
6351eb2bd662Svikram 	assert(strcmp(mcp->mc_fstype, "ufs") == 0);
6352eb2bd662Svikram 	*mntpt = mcp->mc_mntpt;
6353*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: *MATCH* in cache for: %s\n", fcn, special));
6354eb2bd662Svikram 	return (MH_MATCH);
6355eb2bd662Svikram }
6356eb2bd662Svikram 
6357eb2bd662Svikram static int
6358eb2bd662Svikram check_add_ufs_sign_to_list(FILE *tfp, char *mntpt)
6359eb2bd662Svikram {
6360eb2bd662Svikram 	char		*sign;
6361eb2bd662Svikram 	char		*signline;
6362eb2bd662Svikram 	char		signbuf[MAXNAMELEN];
6363eb2bd662Svikram 	int		len;
6364eb2bd662Svikram 	int		error;
6365eb2bd662Svikram 	const char	*fcn = "check_add_ufs_sign_to_list()";
6366eb2bd662Svikram 
6367eb2bd662Svikram 	/* safe to specify NULL as "osdev" arg for UFS */
6368eb2bd662Svikram 	sign = find_existing_sign(mntpt, NULL, "ufs");
6369eb2bd662Svikram 	if (sign == NULL) {
6370eb2bd662Svikram 		/* No existing signature, nothing to add to list */
6371*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: no sign on %s to add to signlist\n",
6372*f64ca102SToomas Soome 		    fcn, mntpt));
6373eb2bd662Svikram 		return (0);
6374eb2bd662Svikram 	}
6375eb2bd662Svikram 
6376eb2bd662Svikram 	(void) snprintf(signbuf, sizeof (signbuf), "%s\n", sign);
6377eb2bd662Svikram 	signline = signbuf;
6378eb2bd662Svikram 
6379eb2bd662Svikram 	INJECT_ERROR1("UFS_MNTPT_SIGN_NOTUFS", signline = "pool_rpool10\n");
6380eb2bd662Svikram 	if (strncmp(signline, GRUBSIGN_UFS_PREFIX,
6381eb2bd662Svikram 	    strlen(GRUBSIGN_UFS_PREFIX))) {
6382*f64ca102SToomas Soome 		bam_error(_("invalid UFS boot signature %s\n"), sign);
6383eb2bd662Svikram 		free(sign);
6384eb2bd662Svikram 		/* ignore invalid signatures */
6385eb2bd662Svikram 		return (0);
6386eb2bd662Svikram 	}
6387eb2bd662Svikram 
6388eb2bd662Svikram 	len = fputs(signline, tfp);
6389eb2bd662Svikram 	error = errno;
6390eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_PUTS_ERROR", len = 0);
6391eb2bd662Svikram 	if (len != strlen(signline)) {
6392*f64ca102SToomas Soome 		bam_error(_("failed to write signature %s to signature "
6393*f64ca102SToomas Soome 		    "list: %s\n"), sign, strerror(error));
6394eb2bd662Svikram 		free(sign);
6395eb2bd662Svikram 		return (-1);
6396eb2bd662Svikram 	}
6397eb2bd662Svikram 
6398eb2bd662Svikram 	free(sign);
6399eb2bd662Svikram 
6400*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: successfully added sign on %s to signlist\n",
6401*f64ca102SToomas Soome 	    fcn, mntpt));
6402eb2bd662Svikram 	return (0);
6403eb2bd662Svikram }
6404eb2bd662Svikram 
6405eb2bd662Svikram /*
6406eb2bd662Svikram  * slice is a basename not a full pathname
6407eb2bd662Svikram  */
6408eb2bd662Svikram static int
6409eb2bd662Svikram process_slice_common(char *slice, FILE *tfp, mhash_t *mhp, char *tmpmnt)
6410eb2bd662Svikram {
6411eb2bd662Svikram 	int		ret;
6412eb2bd662Svikram 	char		cmd[PATH_MAX];
6413eb2bd662Svikram 	char		path[PATH_MAX];
6414eb2bd662Svikram 	struct stat	sbuf;
6415eb2bd662Svikram 	char		*mntpt;
6416eb2bd662Svikram 	filelist_t	flist = {0};
6417eb2bd662Svikram 	char		*fstype;
6418eb2bd662Svikram 	char		blkslice[PATH_MAX];
6419eb2bd662Svikram 	const char	*fcn = "process_slice_common()";
6420eb2bd662Svikram 
6421eb2bd662Svikram 
6422eb2bd662Svikram 	ret = search_hash(mhp, slice, &mntpt);
6423eb2bd662Svikram 	switch (ret) {
6424eb2bd662Svikram 		case MH_MATCH:
6425eb2bd662Svikram 			if (check_add_ufs_sign_to_list(tfp, mntpt) == -1)
6426eb2bd662Svikram 				return (-1);
6427eb2bd662Svikram 			else
6428eb2bd662Svikram 				return (0);
6429eb2bd662Svikram 		case MH_NOMATCH:
6430eb2bd662Svikram 			break;
6431eb2bd662Svikram 		case MH_ERROR:
6432eb2bd662Svikram 		default:
6433eb2bd662Svikram 			return (-1);
6434eb2bd662Svikram 	}
6435eb2bd662Svikram 
6436eb2bd662Svikram 	(void) snprintf(path, sizeof (path), "/dev/rdsk/%s", slice);
6437eb2bd662Svikram 	if (stat(path, &sbuf) == -1) {
6438*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: slice does not exist: %s\n", fcn, path));
6439eb2bd662Svikram 		return (0);
6440eb2bd662Svikram 	}
6441eb2bd662Svikram 
6442d5cb36d3SJan Setje-Eilers 	/* Check if ufs. Call ufs fstyp directly to avoid pcfs conflicts. */
6443eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6444d5cb36d3SJan Setje-Eilers 	    "/usr/lib/fs/ufs/fstyp /dev/rdsk/%s 2>/dev/null",
6445eb2bd662Svikram 	    slice);
6446eb2bd662Svikram 
6447eb2bd662Svikram 	if (exec_cmd(cmd, &flist) != 0) {
6448eb2bd662Svikram 		if (bam_verbose)
6449*f64ca102SToomas Soome 			bam_print(_("fstyp failed for slice: %s\n"), slice);
6450eb2bd662Svikram 		return (0);
6451eb2bd662Svikram 	}
6452eb2bd662Svikram 
6453eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
6454eb2bd662Svikram 		if (bam_verbose)
6455*f64ca102SToomas Soome 			bam_print(_("bad output from fstyp for slice: %s\n"),
6456*f64ca102SToomas Soome 			    slice);
6457eb2bd662Svikram 		filelist_free(&flist);
6458eb2bd662Svikram 		return (0);
6459eb2bd662Svikram 	}
6460eb2bd662Svikram 
6461eb2bd662Svikram 	fstype = strtok(flist.head->line, " \t\n");
6462eb2bd662Svikram 	if (fstype == NULL || strcmp(fstype, "ufs") != 0) {
6463eb2bd662Svikram 		if (bam_verbose)
6464*f64ca102SToomas Soome 			bam_print(_("%s is not a ufs slice: %s\n"),
6465*f64ca102SToomas Soome 			    slice, fstype);
6466eb2bd662Svikram 		filelist_free(&flist);
6467eb2bd662Svikram 		return (0);
6468eb2bd662Svikram 	}
6469eb2bd662Svikram 
6470eb2bd662Svikram 	filelist_free(&flist);
6471eb2bd662Svikram 
6472eb2bd662Svikram 	/*
6473eb2bd662Svikram 	 * Since we are mounting the filesystem read-only, the
6474eb2bd662Svikram 	 * the last mount field of the superblock is unchanged
6475eb2bd662Svikram 	 * and does not need to be fixed up post-mount;
6476eb2bd662Svikram 	 */
6477eb2bd662Svikram 
6478eb2bd662Svikram 	(void) snprintf(blkslice, sizeof (blkslice), "/dev/dsk/%s",
6479eb2bd662Svikram 	    slice);
6480eb2bd662Svikram 
6481eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6482eb2bd662Svikram 	    "/usr/sbin/mount -F ufs -o ro %s %s "
6483eb2bd662Svikram 	    "> /dev/null 2>&1", blkslice, tmpmnt);
6484eb2bd662Svikram 
6485eb2bd662Svikram 	if (exec_cmd(cmd, NULL) != 0) {
6486eb2bd662Svikram 		if (bam_verbose)
6487*f64ca102SToomas Soome 			bam_print(_("mount of %s (fstype %s) failed\n"),
6488*f64ca102SToomas Soome 			    blkslice, "ufs");
6489eb2bd662Svikram 		return (0);
6490eb2bd662Svikram 	}
6491eb2bd662Svikram 
6492eb2bd662Svikram 	ret = check_add_ufs_sign_to_list(tfp, tmpmnt);
6493eb2bd662Svikram 
6494eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6495eb2bd662Svikram 	    "/usr/sbin/umount -f %s > /dev/null 2>&1",
6496eb2bd662Svikram 	    tmpmnt);
6497eb2bd662Svikram 
6498eb2bd662Svikram 	if (exec_cmd(cmd, NULL) != 0) {
6499*f64ca102SToomas Soome 		bam_print(_("umount of %s failed\n"), slice);
6500eb2bd662Svikram 		return (0);
6501eb2bd662Svikram 	}
6502eb2bd662Svikram 
6503eb2bd662Svikram 	return (ret);
6504eb2bd662Svikram }
6505eb2bd662Svikram 
6506eb2bd662Svikram static int
6507eb2bd662Svikram process_vtoc_slices(
6508eb2bd662Svikram 	char *s0,
6509eb2bd662Svikram 	struct vtoc *vtoc,
6510eb2bd662Svikram 	FILE *tfp,
6511eb2bd662Svikram 	mhash_t *mhp,
6512eb2bd662Svikram 	char *tmpmnt)
6513eb2bd662Svikram {
6514eb2bd662Svikram 	int		idx;
6515eb2bd662Svikram 	char		slice[PATH_MAX];
6516eb2bd662Svikram 	size_t		len;
6517eb2bd662Svikram 	char		*cp;
6518eb2bd662Svikram 	const char	*fcn = "process_vtoc_slices()";
6519eb2bd662Svikram 
6520eb2bd662Svikram 	len = strlen(s0);
6521eb2bd662Svikram 
6522eb2bd662Svikram 	assert(s0[len - 2] == 's' && s0[len - 1] == '0');
6523eb2bd662Svikram 
6524eb2bd662Svikram 	s0[len - 1] = '\0';
6525eb2bd662Svikram 
6526eb2bd662Svikram 	(void) strlcpy(slice, s0, sizeof (slice));
6527eb2bd662Svikram 
6528eb2bd662Svikram 	s0[len - 1] = '0';
6529eb2bd662Svikram 
6530eb2bd662Svikram 	cp = slice + len - 1;
6531eb2bd662Svikram 
6532eb2bd662Svikram 	for (idx = 0; idx < vtoc->v_nparts; idx++) {
6533eb2bd662Svikram 
6534eb2bd662Svikram 		(void) snprintf(cp, sizeof (slice) - (len - 1), "%u", idx);
6535eb2bd662Svikram 
6536eb2bd662Svikram 		if (vtoc->v_part[idx].p_size == 0) {
6537*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: skipping 0-length slice: %s\n",
6538*f64ca102SToomas Soome 			    fcn, slice));
6539eb2bd662Svikram 			continue;
6540eb2bd662Svikram 		}
6541eb2bd662Svikram 
6542eb2bd662Svikram 		/* Skip "SWAP", "USR", "BACKUP", "VAR", "HOME", "ALTSCTR" */
6543eb2bd662Svikram 		switch (vtoc->v_part[idx].p_tag) {
6544eb2bd662Svikram 		case V_SWAP:
6545eb2bd662Svikram 		case V_USR:
6546eb2bd662Svikram 		case V_BACKUP:
6547eb2bd662Svikram 		case V_VAR:
6548eb2bd662Svikram 		case V_HOME:
6549eb2bd662Svikram 		case V_ALTSCTR:
6550*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: unsupported tag, "
6551*f64ca102SToomas Soome 			    "skipping: %s\n", fcn, slice));
6552eb2bd662Svikram 			continue;
6553eb2bd662Svikram 		default:
6554*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: supported tag, checking: %s\n",
6555*f64ca102SToomas Soome 			    fcn, slice));
6556eb2bd662Svikram 			break;
6557eb2bd662Svikram 		}
6558eb2bd662Svikram 
6559eb2bd662Svikram 		/* skip unmountable and readonly slices */
6560eb2bd662Svikram 		switch (vtoc->v_part[idx].p_flag) {
6561eb2bd662Svikram 		case V_UNMNT:
6562eb2bd662Svikram 		case V_RONLY:
6563*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: non-RDWR flag, skipping: %s\n",
6564*f64ca102SToomas Soome 			    fcn, slice));
6565eb2bd662Svikram 			continue;
6566eb2bd662Svikram 		default:
6567*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: RDWR flag, checking: %s\n",
6568*f64ca102SToomas Soome 			    fcn, slice));
6569eb2bd662Svikram 			break;
6570eb2bd662Svikram 		}
6571eb2bd662Svikram 
6572eb2bd662Svikram 		if (process_slice_common(slice, tfp, mhp, tmpmnt) == -1) {
6573eb2bd662Svikram 			return (-1);
6574eb2bd662Svikram 		}
6575eb2bd662Svikram 	}
6576eb2bd662Svikram 
6577eb2bd662Svikram 	return (0);
6578eb2bd662Svikram }
6579eb2bd662Svikram 
6580eb2bd662Svikram static int
6581eb2bd662Svikram process_efi_slices(
6582eb2bd662Svikram 	char *s0,
6583eb2bd662Svikram 	struct dk_gpt *efi,
6584eb2bd662Svikram 	FILE *tfp,
6585eb2bd662Svikram 	mhash_t *mhp,
6586eb2bd662Svikram 	char *tmpmnt)
6587eb2bd662Svikram {
6588eb2bd662Svikram 	int		idx;
6589eb2bd662Svikram 	char		slice[PATH_MAX];
6590eb2bd662Svikram 	size_t		len;
6591eb2bd662Svikram 	char		*cp;
6592eb2bd662Svikram 	const char	*fcn = "process_efi_slices()";
6593eb2bd662Svikram 
6594eb2bd662Svikram 	len = strlen(s0);
6595eb2bd662Svikram 
6596eb2bd662Svikram 	assert(s0[len - 2] == 's' && s0[len - 1] == '0');
6597eb2bd662Svikram 
6598eb2bd662Svikram 	s0[len - 1] = '\0';
6599eb2bd662Svikram 
6600eb2bd662Svikram 	(void) strlcpy(slice, s0, sizeof (slice));
6601eb2bd662Svikram 
6602eb2bd662Svikram 	s0[len - 1] = '0';
6603eb2bd662Svikram 
6604eb2bd662Svikram 	cp = slice + len - 1;
6605eb2bd662Svikram 
6606eb2bd662Svikram 	for (idx = 0; idx < efi->efi_nparts; idx++) {
6607eb2bd662Svikram 
6608eb2bd662Svikram 		(void) snprintf(cp, sizeof (slice) - (len - 1), "%u", idx);
6609eb2bd662Svikram 
6610eb2bd662Svikram 		if (efi->efi_parts[idx].p_size == 0) {
6611*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: skipping 0-length slice: %s\n",
6612*f64ca102SToomas Soome 			    fcn, slice));
6613eb2bd662Svikram 			continue;
6614eb2bd662Svikram 		}
6615eb2bd662Svikram 
6616eb2bd662Svikram 		/* Skip "SWAP", "USR", "BACKUP", "VAR", "HOME", "ALTSCTR" */
6617eb2bd662Svikram 		switch (efi->efi_parts[idx].p_tag) {
6618eb2bd662Svikram 		case V_SWAP:
6619eb2bd662Svikram 		case V_USR:
6620eb2bd662Svikram 		case V_BACKUP:
6621eb2bd662Svikram 		case V_VAR:
6622eb2bd662Svikram 		case V_HOME:
6623eb2bd662Svikram 		case V_ALTSCTR:
6624*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: unsupported tag, skipping: %s\n",
6625*f64ca102SToomas Soome 			    fcn, slice));
6626eb2bd662Svikram 			continue;
6627eb2bd662Svikram 		default:
6628*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: supported tag, checking: %s\n",
6629*f64ca102SToomas Soome 			    fcn, slice));
6630eb2bd662Svikram 			break;
6631eb2bd662Svikram 		}
6632eb2bd662Svikram 
6633eb2bd662Svikram 		/* skip unmountable and readonly slices */
6634eb2bd662Svikram 		switch (efi->efi_parts[idx].p_flag) {
6635eb2bd662Svikram 		case V_UNMNT:
6636eb2bd662Svikram 		case V_RONLY:
6637*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: non-RDWR flag, skipping: %s\n",
6638*f64ca102SToomas Soome 			    fcn, slice));
6639eb2bd662Svikram 			continue;
6640eb2bd662Svikram 		default:
6641*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: RDWR flag, checking: %s\n",
6642*f64ca102SToomas Soome 			    fcn, slice));
6643eb2bd662Svikram 			break;
6644eb2bd662Svikram 		}
6645eb2bd662Svikram 
6646eb2bd662Svikram 		if (process_slice_common(slice, tfp, mhp, tmpmnt) == -1) {
6647eb2bd662Svikram 			return (-1);
6648eb2bd662Svikram 		}
6649eb2bd662Svikram 	}
6650eb2bd662Svikram 
6651eb2bd662Svikram 	return (0);
6652eb2bd662Svikram }
6653eb2bd662Svikram 
6654eb2bd662Svikram /*
6655eb2bd662Svikram  * s0 is a basename not a full path
6656eb2bd662Svikram  */
6657eb2bd662Svikram static int
6658eb2bd662Svikram process_slice0(char *s0, FILE *tfp, mhash_t *mhp, char *tmpmnt)
6659eb2bd662Svikram {
6660eb2bd662Svikram 	struct vtoc		vtoc;
6661eb2bd662Svikram 	struct dk_gpt		*efi;
6662eb2bd662Svikram 	char			s0path[PATH_MAX];
6663eb2bd662Svikram 	struct stat		sbuf;
6664eb2bd662Svikram 	int			e_flag;
6665eb2bd662Svikram 	int			v_flag;
6666eb2bd662Svikram 	int			retval;
6667eb2bd662Svikram 	int			err;
6668eb2bd662Svikram 	int			fd;
6669eb2bd662Svikram 	const char		*fcn = "process_slice0()";
6670eb2bd662Svikram 
6671eb2bd662Svikram 	(void) snprintf(s0path, sizeof (s0path), "/dev/rdsk/%s", s0);
6672eb2bd662Svikram 
6673eb2bd662Svikram 	if (stat(s0path, &sbuf) == -1) {
6674*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: slice 0 does not exist: %s\n", fcn, s0path));
6675eb2bd662Svikram 		return (0);
6676eb2bd662Svikram 	}
6677eb2bd662Svikram 
6678eb2bd662Svikram 	fd = open(s0path, O_NONBLOCK|O_RDONLY);
6679eb2bd662Svikram 	if (fd == -1) {
6680*f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), s0path,
6681*f64ca102SToomas Soome 		    strerror(errno));
6682eb2bd662Svikram 		return (0);
6683eb2bd662Svikram 	}
6684eb2bd662Svikram 
6685eb2bd662Svikram 	e_flag = v_flag = 0;
6686eb2bd662Svikram 	retval = ((err = read_vtoc(fd, &vtoc)) >= 0) ? 0 : err;
6687eb2bd662Svikram 	switch (retval) {
6688eb2bd662Svikram 		case VT_EIO:
6689*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: failed to read: %s\n",
6690*f64ca102SToomas Soome 			    fcn, s0path));
6691eb2bd662Svikram 			break;
6692eb2bd662Svikram 		case VT_EINVAL:
6693*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: is INVALID: %s\n",
6694*f64ca102SToomas Soome 			    fcn, s0path));
6695eb2bd662Svikram 			break;
6696eb2bd662Svikram 		case VT_ERROR:
6697*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: unknown error while "
6698*f64ca102SToomas Soome 			    "reading: %s\n", fcn, s0path));
6699eb2bd662Svikram 			break;
6700eb2bd662Svikram 		case VT_ENOTSUP:
6701eb2bd662Svikram 			e_flag = 1;
6702*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: not supported: %s\n",
6703*f64ca102SToomas Soome 			    fcn, s0path));
6704eb2bd662Svikram 			break;
6705eb2bd662Svikram 		case 0:
6706eb2bd662Svikram 			v_flag = 1;
6707*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: SUCCESS reading: %s\n",
6708*f64ca102SToomas Soome 			    fcn, s0path));
6709eb2bd662Svikram 			break;
6710eb2bd662Svikram 		default:
6711*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: READ: unknown return "
6712*f64ca102SToomas Soome 			    "code: %s\n", fcn, s0path));
6713eb2bd662Svikram 			break;
6714eb2bd662Svikram 	}
6715eb2bd662Svikram 
6716eb2bd662Svikram 
6717eb2bd662Svikram 	if (e_flag) {
6718eb2bd662Svikram 		e_flag = 0;
6719eb2bd662Svikram 		retval = ((err = efi_alloc_and_read(fd, &efi)) >= 0) ? 0 : err;
6720eb2bd662Svikram 		switch (retval) {
6721eb2bd662Svikram 		case VT_EIO:
6722*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: failed to read: %s\n",
6723*f64ca102SToomas Soome 			    fcn, s0path));
6724eb2bd662Svikram 			break;
6725eb2bd662Svikram 		case VT_EINVAL:
6726*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: is INVALID: %s\n", fcn, s0path));
6727eb2bd662Svikram 			break;
6728eb2bd662Svikram 		case VT_ERROR:
6729*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: unknown error while "
6730*f64ca102SToomas Soome 			    "reading: %s\n", fcn, s0path));
6731eb2bd662Svikram 			break;
6732eb2bd662Svikram 		case VT_ENOTSUP:
6733*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: not supported: %s\n",
6734*f64ca102SToomas Soome 			    fcn, s0path));
6735eb2bd662Svikram 			break;
6736eb2bd662Svikram 		case 0:
6737eb2bd662Svikram 			e_flag = 1;
6738*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: SUCCESS reading: %s\n",
6739*f64ca102SToomas Soome 			    fcn, s0path));
6740eb2bd662Svikram 			break;
6741eb2bd662Svikram 		default:
6742*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: READ: unknown return code: %s\n",
6743*f64ca102SToomas Soome 			    fcn, s0path));
6744eb2bd662Svikram 			break;
6745eb2bd662Svikram 		}
6746eb2bd662Svikram 	}
6747eb2bd662Svikram 
6748eb2bd662Svikram 	(void) close(fd);
6749eb2bd662Svikram 
6750eb2bd662Svikram 	if (v_flag) {
6751eb2bd662Svikram 		retval = process_vtoc_slices(s0,
6752eb2bd662Svikram 		    &vtoc, tfp, mhp, tmpmnt);
6753eb2bd662Svikram 	} else if (e_flag) {
6754eb2bd662Svikram 		retval = process_efi_slices(s0,
6755eb2bd662Svikram 		    efi, tfp, mhp, tmpmnt);
6756eb2bd662Svikram 	} else {
6757*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: disk has neither VTOC nor EFI: %s\n",
6758*f64ca102SToomas Soome 		    fcn, s0path));
6759eb2bd662Svikram 		return (0);
6760eb2bd662Svikram 	}
6761eb2bd662Svikram 
6762eb2bd662Svikram 	return (retval);
6763eb2bd662Svikram }
6764eb2bd662Svikram 
6765eb2bd662Svikram /*
6766eb2bd662Svikram  * Find and create a list of all existing UFS boot signatures
6767eb2bd662Svikram  */
6768eb2bd662Svikram static int
6769eb2bd662Svikram FindAllUfsSignatures(void)
6770eb2bd662Svikram {
6771eb2bd662Svikram 	mhash_t		*mnttab_hash;
6772eb2bd662Svikram 	DIR		*dirp = NULL;
6773eb2bd662Svikram 	struct dirent	*dp;
6774eb2bd662Svikram 	char		tmpmnt[PATH_MAX];
6775eb2bd662Svikram 	char		cmd[PATH_MAX];
6776eb2bd662Svikram 	struct stat	sb;
6777eb2bd662Svikram 	int		fd;
6778eb2bd662Svikram 	FILE		*tfp;
6779eb2bd662Svikram 	size_t		len;
6780eb2bd662Svikram 	int		ret;
6781eb2bd662Svikram 	int		error;
6782eb2bd662Svikram 	const char	*fcn = "FindAllUfsSignatures()";
6783eb2bd662Svikram 
6784eb2bd662Svikram 	if (stat(UFS_SIGNATURE_LIST, &sb) != -1)  {
6785*f64ca102SToomas Soome 		bam_print(_("       - signature list %s exists\n"),
6786*f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST);
6787eb2bd662Svikram 		return (0);
6788eb2bd662Svikram 	}
6789eb2bd662Svikram 
6790eb2bd662Svikram 	fd = open(UFS_SIGNATURE_LIST".tmp",
6791eb2bd662Svikram 	    O_RDWR|O_CREAT|O_TRUNC, 0644);
6792eb2bd662Svikram 	error = errno;
6793eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_TMP_TRUNC", fd = -1);
6794eb2bd662Svikram 	if (fd == -1) {
6795*f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"),
6796*f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST".tmp", strerror(error));
6797eb2bd662Svikram 		return (-1);
6798eb2bd662Svikram 	}
6799eb2bd662Svikram 
6800eb2bd662Svikram 	ret = close(fd);
6801eb2bd662Svikram 	error = errno;
6802eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_TMP_CLOSE", ret = -1);
6803eb2bd662Svikram 	if (ret == -1) {
6804*f64ca102SToomas Soome 		bam_error(_("failed to close file: %s: %s\n"),
6805*f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST".tmp", strerror(error));
6806eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6807eb2bd662Svikram 		return (-1);
6808eb2bd662Svikram 	}
6809eb2bd662Svikram 
6810eb2bd662Svikram 	tfp = fopen(UFS_SIGNATURE_LIST".tmp", "a");
6811eb2bd662Svikram 	error = errno;
6812eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_APPEND_FOPEN", tfp = NULL);
6813eb2bd662Svikram 	if (tfp == NULL) {
6814*f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"),
6815*f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST".tmp", strerror(error));
6816eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6817eb2bd662Svikram 		return (-1);
6818eb2bd662Svikram 	}
6819eb2bd662Svikram 
6820eb2bd662Svikram 	mnttab_hash = cache_mnttab();
6821eb2bd662Svikram 	INJECT_ERROR1("CACHE_MNTTAB_ERROR", mnttab_hash = NULL);
6822eb2bd662Svikram 	if (mnttab_hash == NULL) {
6823eb2bd662Svikram 		(void) fclose(tfp);
6824eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6825*f64ca102SToomas Soome 		bam_error(_("%s: failed to cache /etc/mnttab\n"), fcn);
6826eb2bd662Svikram 		return (-1);
6827eb2bd662Svikram 	}
6828eb2bd662Svikram 
6829eb2bd662Svikram 	(void) snprintf(tmpmnt, sizeof (tmpmnt),
6830eb2bd662Svikram 	    "/tmp/bootadm_ufs_sign_mnt.%d", getpid());
6831eb2bd662Svikram 	(void) unlink(tmpmnt);
6832eb2bd662Svikram 
683348847494SEnrico Perla - Sun Microsystems 	ret = mkdirp(tmpmnt, DIR_PERMS);
6834eb2bd662Svikram 	error = errno;
6835eb2bd662Svikram 	INJECT_ERROR1("MKDIRP_SIGN_MNT", ret = -1);
6836eb2bd662Svikram 	if (ret == -1) {
6837*f64ca102SToomas Soome 		bam_error(_("mkdir of %s failed: %s\n"), tmpmnt,
6838*f64ca102SToomas Soome 		    strerror(error));
6839eb2bd662Svikram 		free_mnttab(mnttab_hash);
6840eb2bd662Svikram 		(void) fclose(tfp);
6841eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6842eb2bd662Svikram 		return (-1);
6843eb2bd662Svikram 	}
6844eb2bd662Svikram 
6845eb2bd662Svikram 	dirp = opendir("/dev/rdsk");
6846eb2bd662Svikram 	error = errno;
6847eb2bd662Svikram 	INJECT_ERROR1("OPENDIR_DEV_RDSK", dirp = NULL);
6848eb2bd662Svikram 	if (dirp == NULL) {
6849*f64ca102SToomas Soome 		bam_error(_("opendir of %s failed: %s\n"), "/dev/rdsk",
6850*f64ca102SToomas Soome 		    strerror(error));
6851eb2bd662Svikram 		goto fail;
6852eb2bd662Svikram 	}
6853eb2bd662Svikram 
6854110570ceSToomas Soome 	while ((dp = readdir(dirp)) != NULL) {
6855eb2bd662Svikram 		if (strcmp(dp->d_name, ".") == 0 ||
6856eb2bd662Svikram 		    strcmp(dp->d_name, "..") == 0)
6857eb2bd662Svikram 			continue;
6858eb2bd662Svikram 
6859eb2bd662Svikram 		/*
6860eb2bd662Svikram 		 * we only look for the s0 slice. This is guranteed to
6861eb2bd662Svikram 		 * have 's' at len - 2.
6862eb2bd662Svikram 		 */
6863eb2bd662Svikram 		len = strlen(dp->d_name);
6864eb2bd662Svikram 		if (dp->d_name[len - 2 ] != 's' || dp->d_name[len - 1] != '0') {
6865*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: skipping non-s0 slice: %s\n",
6866*f64ca102SToomas Soome 			    fcn, dp->d_name));
6867eb2bd662Svikram 			continue;
6868eb2bd662Svikram 		}
6869eb2bd662Svikram 
6870eb2bd662Svikram 		ret = process_slice0(dp->d_name, tfp, mnttab_hash, tmpmnt);
6871eb2bd662Svikram 		INJECT_ERROR1("PROCESS_S0_FAIL", ret = -1);
6872eb2bd662Svikram 		if (ret == -1)
6873eb2bd662Svikram 			goto fail;
6874eb2bd662Svikram 	}
6875eb2bd662Svikram 
6876eb2bd662Svikram 	(void) closedir(dirp);
6877eb2bd662Svikram 	free_mnttab(mnttab_hash);
6878eb2bd662Svikram 	(void) rmdir(tmpmnt);
6879eb2bd662Svikram 
6880eb2bd662Svikram 	ret = fclose(tfp);
6881eb2bd662Svikram 	error = errno;
6882eb2bd662Svikram 	INJECT_ERROR1("FCLOSE_SIGNLIST_TMP", ret = EOF);
6883eb2bd662Svikram 	if (ret == EOF) {
6884*f64ca102SToomas Soome 		bam_error(_("failed to close file: %s: %s\n"),
6885*f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST".tmp", strerror(error));
6886eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6887eb2bd662Svikram 		return (-1);
6888eb2bd662Svikram 	}
6889eb2bd662Svikram 
6890eb2bd662Svikram 	/* We have a list of existing GRUB signatures. Sort it first */
6891eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6892eb2bd662Svikram 	    "/usr/bin/sort -u %s.tmp > %s.sorted",
6893eb2bd662Svikram 	    UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST);
6894eb2bd662Svikram 
6895eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
6896eb2bd662Svikram 	INJECT_ERROR1("SORT_SIGN_LIST", ret = 1);
6897eb2bd662Svikram 	if (ret != 0) {
6898*f64ca102SToomas Soome 		bam_error(_("error sorting GRUB UFS boot signatures\n"));
6899eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
6900eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6901eb2bd662Svikram 		return (-1);
6902eb2bd662Svikram 	}
6903eb2bd662Svikram 
6904eb2bd662Svikram 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
6905eb2bd662Svikram 
6906eb2bd662Svikram 	ret = rename(UFS_SIGNATURE_LIST".sorted", UFS_SIGNATURE_LIST);
6907eb2bd662Svikram 	error = errno;
6908eb2bd662Svikram 	INJECT_ERROR1("RENAME_TMP_SIGNLIST", ret = -1);
6909eb2bd662Svikram 	if (ret == -1) {
6910*f64ca102SToomas Soome 		bam_error(_("rename to file failed: %s: %s\n"),
6911*f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST, strerror(error));
6912eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
6913eb2bd662Svikram 		return (-1);
6914eb2bd662Svikram 	}
6915eb2bd662Svikram 
6916eb2bd662Svikram 	if (stat(UFS_SIGNATURE_LIST, &sb) == 0 && sb.st_size == 0) {
6917*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: generated zero length signlist: %s.\n",
6918*f64ca102SToomas Soome 		    fcn, UFS_SIGNATURE_LIST));
6919eb2bd662Svikram 	}
6920eb2bd662Svikram 
6921*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
6922eb2bd662Svikram 	return (0);
6923eb2bd662Svikram 
6924eb2bd662Svikram fail:
6925eb2bd662Svikram 	if (dirp)
6926eb2bd662Svikram 		(void) closedir(dirp);
6927eb2bd662Svikram 	free_mnttab(mnttab_hash);
6928eb2bd662Svikram 	(void) rmdir(tmpmnt);
6929eb2bd662Svikram 	(void) fclose(tfp);
6930eb2bd662Svikram 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
6931*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
6932eb2bd662Svikram 	return (-1);
6933eb2bd662Svikram }
6934eb2bd662Svikram 
6935eb2bd662Svikram static char *
6936eb2bd662Svikram create_ufs_sign(void)
6937eb2bd662Svikram {
6938eb2bd662Svikram 	struct stat	sb;
6939eb2bd662Svikram 	int		signnum = -1;
6940eb2bd662Svikram 	char		tmpsign[MAXNAMELEN + 1];
6941eb2bd662Svikram 	char		*numstr;
6942eb2bd662Svikram 	int		i;
6943eb2bd662Svikram 	FILE		*tfp;
6944eb2bd662Svikram 	int		ret;
6945eb2bd662Svikram 	int		error;
6946eb2bd662Svikram 	const char	*fcn = "create_ufs_sign()";
6947eb2bd662Svikram 
6948*f64ca102SToomas Soome 	bam_print(_("  - searching for UFS boot signatures\n"));
6949eb2bd662Svikram 
6950eb2bd662Svikram 	ret = FindAllUfsSignatures();
6951eb2bd662Svikram 	INJECT_ERROR1("FIND_ALL_UFS", ret = -1);
6952eb2bd662Svikram 	if (ret == -1) {
6953*f64ca102SToomas Soome 		bam_error(_("search for UFS boot signatures failed\n"));
6954eb2bd662Svikram 		return (NULL);
6955eb2bd662Svikram 	}
6956eb2bd662Svikram 
6957eb2bd662Svikram 	/* Make sure the list exists and is owned by root */
6958eb2bd662Svikram 	INJECT_ERROR1("SIGNLIST_NOT_CREATED",
6959eb2bd662Svikram 	    (void) unlink(UFS_SIGNATURE_LIST));
6960eb2bd662Svikram 	if (stat(UFS_SIGNATURE_LIST, &sb) == -1 || sb.st_uid != 0) {
6961eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6962*f64ca102SToomas Soome 		bam_error(_("missing UFS signature list file: %s\n"),
6963*f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST);
6964eb2bd662Svikram 		return (NULL);
6965eb2bd662Svikram 	}
6966eb2bd662Svikram 
6967eb2bd662Svikram 	if (sb.st_size == 0) {
6968*f64ca102SToomas Soome 		bam_print(_("   - no existing UFS boot signatures\n"));
6969eb2bd662Svikram 		i = 0;
6970eb2bd662Svikram 		goto found;
6971eb2bd662Svikram 	}
6972eb2bd662Svikram 
6973eb2bd662Svikram 	/* The signature list was sorted when it was created */
6974eb2bd662Svikram 	tfp = fopen(UFS_SIGNATURE_LIST, "r");
6975eb2bd662Svikram 	error = errno;
6976eb2bd662Svikram 	INJECT_ERROR1("FOPEN_SIGN_LIST", tfp = NULL);
6977eb2bd662Svikram 	if (tfp == NULL) {
6978*f64ca102SToomas Soome 		bam_error(_("error opening UFS boot signature list "
6979*f64ca102SToomas Soome 		    "file %s: %s\n"), UFS_SIGNATURE_LIST, strerror(error));
6980eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6981eb2bd662Svikram 		return (NULL);
6982eb2bd662Svikram 	}
6983eb2bd662Svikram 
6984eb2bd662Svikram 	for (i = 0; s_fgets(tmpsign, sizeof (tmpsign), tfp); i++) {
6985eb2bd662Svikram 
6986eb2bd662Svikram 		if (strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
6987eb2bd662Svikram 		    strlen(GRUBSIGN_UFS_PREFIX)) != 0) {
6988eb2bd662Svikram 			(void) fclose(tfp);
6989eb2bd662Svikram 			(void) unlink(UFS_SIGNATURE_LIST);
6990*f64ca102SToomas Soome 			bam_error(_("bad UFS boot signature: %s\n"), tmpsign);
6991eb2bd662Svikram 			return (NULL);
6992eb2bd662Svikram 		}
6993eb2bd662Svikram 		numstr = tmpsign + strlen(GRUBSIGN_UFS_PREFIX);
6994eb2bd662Svikram 
6995eb2bd662Svikram 		if (numstr[0] == '\0' || !isdigit(numstr[0])) {
6996eb2bd662Svikram 			(void) fclose(tfp);
6997eb2bd662Svikram 			(void) unlink(UFS_SIGNATURE_LIST);
6998*f64ca102SToomas Soome 			bam_error(_("bad UFS boot signature: %s\n"), tmpsign);
6999eb2bd662Svikram 			return (NULL);
7000eb2bd662Svikram 		}
7001eb2bd662Svikram 
7002eb2bd662Svikram 		signnum = atoi(numstr);
7003eb2bd662Svikram 		INJECT_ERROR1("NEGATIVE_SIGN", signnum = -1);
7004eb2bd662Svikram 		if (signnum < 0) {
7005eb2bd662Svikram 			(void) fclose(tfp);
7006eb2bd662Svikram 			(void) unlink(UFS_SIGNATURE_LIST);
7007*f64ca102SToomas Soome 			bam_error(_("bad UFS boot signature: %s\n"), tmpsign);
7008eb2bd662Svikram 			return (NULL);
7009eb2bd662Svikram 		}
7010eb2bd662Svikram 
7011eb2bd662Svikram 		if (i != signnum) {
7012*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: found hole %d in sign list.\n",
7013*f64ca102SToomas Soome 			    fcn, i));
7014eb2bd662Svikram 			break;
7015eb2bd662Svikram 		}
7016eb2bd662Svikram 	}
7017eb2bd662Svikram 
7018eb2bd662Svikram 	(void) fclose(tfp);
7019eb2bd662Svikram 
7020eb2bd662Svikram found:
7021eb2bd662Svikram 	(void) snprintf(tmpsign, sizeof (tmpsign), "rootfs%d", i);
7022eb2bd662Svikram 
7023eb2bd662Svikram 	/* add the ufs signature to the /var/run list of signatures */
7024eb2bd662Svikram 	ret = ufs_add_to_sign_list(tmpsign);
7025eb2bd662Svikram 	INJECT_ERROR1("UFS_ADD_TO_SIGN_LIST", ret = -1);
7026eb2bd662Svikram 	if (ret == -1) {
7027eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
7028*f64ca102SToomas Soome 		bam_error(_("failed to add sign %s to signlist.\n"), tmpsign);
7029eb2bd662Svikram 		return (NULL);
7030eb2bd662Svikram 	}
7031eb2bd662Svikram 
7032*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7033eb2bd662Svikram 
7034eb2bd662Svikram 	return (s_strdup(tmpsign));
7035eb2bd662Svikram }
7036eb2bd662Svikram 
7037eb2bd662Svikram static char *
7038eb2bd662Svikram get_fstype(char *osroot)
7039eb2bd662Svikram {
7040eb2bd662Svikram 	FILE		*mntfp;
7041eb2bd662Svikram 	struct mnttab	mp = {0};
7042eb2bd662Svikram 	struct mnttab	mpref = {0};
7043eb2bd662Svikram 	int		error;
7044eb2bd662Svikram 	int		ret;
7045eb2bd662Svikram 	const char	*fcn = "get_fstype()";
7046eb2bd662Svikram 
7047eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_OSROOT", osroot = NULL);
7048eb2bd662Svikram 	if (osroot == NULL) {
7049*f64ca102SToomas Soome 		bam_error(_("no OS mountpoint. Cannot determine fstype\n"));
7050eb2bd662Svikram 		return (NULL);
7051eb2bd662Svikram 	}
7052eb2bd662Svikram 
7053eb2bd662Svikram 	mntfp = fopen(MNTTAB, "r");
7054eb2bd662Svikram 	error = errno;
7055eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_FOPEN", mntfp = NULL);
7056eb2bd662Svikram 	if (mntfp == NULL) {
7057*f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), MNTTAB,
7058*f64ca102SToomas Soome 		    strerror(error));
7059eb2bd662Svikram 		return (NULL);
7060eb2bd662Svikram 	}
7061eb2bd662Svikram 
7062eb2bd662Svikram 	if (*osroot == '\0')
7063eb2bd662Svikram 		mpref.mnt_mountp = "/";
7064eb2bd662Svikram 	else
7065eb2bd662Svikram 		mpref.mnt_mountp = osroot;
7066eb2bd662Svikram 
7067eb2bd662Svikram 	ret = getmntany(mntfp, &mp, &mpref);
7068eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_GETMNTANY", ret = 1);
7069eb2bd662Svikram 	if (ret != 0) {
7070*f64ca102SToomas Soome 		bam_error(_("failed to find OS mountpoint %s in %s\n"),
7071*f64ca102SToomas Soome 		    osroot, MNTTAB);
7072eb2bd662Svikram 		(void) fclose(mntfp);
7073eb2bd662Svikram 		return (NULL);
7074eb2bd662Svikram 	}
7075eb2bd662Svikram 	(void) fclose(mntfp);
7076eb2bd662Svikram 
7077eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_NULL", mp.mnt_fstype = NULL);
7078eb2bd662Svikram 	if (mp.mnt_fstype == NULL) {
7079*f64ca102SToomas Soome 		bam_error(_("NULL fstype found for OS root %s\n"), osroot);
7080eb2bd662Svikram 		return (NULL);
7081eb2bd662Svikram 	}
7082eb2bd662Svikram 
7083*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7084eb2bd662Svikram 
7085eb2bd662Svikram 	return (s_strdup(mp.mnt_fstype));
7086eb2bd662Svikram }
7087eb2bd662Svikram 
7088eb2bd662Svikram static char *
7089eb2bd662Svikram create_zfs_sign(char *osdev)
7090eb2bd662Svikram {
7091eb2bd662Svikram 	char		tmpsign[PATH_MAX];
7092eb2bd662Svikram 	char		*pool;
7093eb2bd662Svikram 	const char	*fcn = "create_zfs_sign()";
7094eb2bd662Svikram 
7095*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. arg: %s\n", fcn, osdev));
7096eb2bd662Svikram 
7097eb2bd662Svikram 	/*
7098eb2bd662Svikram 	 * First find the pool name
7099eb2bd662Svikram 	 */
7100eb2bd662Svikram 	pool = get_pool(osdev);
7101eb2bd662Svikram 	INJECT_ERROR1("CREATE_ZFS_SIGN_GET_POOL", pool = NULL);
7102eb2bd662Svikram 	if (pool == NULL) {
7103*f64ca102SToomas Soome 		bam_error(_("failed to get pool name from %s\n"), osdev);
7104eb2bd662Svikram 		return (NULL);
7105eb2bd662Svikram 	}
7106eb2bd662Svikram 
7107eb2bd662Svikram 	(void) snprintf(tmpsign, sizeof (tmpsign), "pool_%s", pool);
7108eb2bd662Svikram 
7109*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: created ZFS sign: %s\n", fcn, tmpsign));
7110eb2bd662Svikram 
7111eb2bd662Svikram 	free(pool);
7112eb2bd662Svikram 
7113*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7114eb2bd662Svikram 
7115eb2bd662Svikram 	return (s_strdup(tmpsign));
7116eb2bd662Svikram }
7117eb2bd662Svikram 
7118eb2bd662Svikram static char *
7119eb2bd662Svikram create_new_sign(char *osdev, char *fstype)
7120eb2bd662Svikram {
7121eb2bd662Svikram 	char		*sign;
7122eb2bd662Svikram 	const char	*fcn = "create_new_sign()";
7123eb2bd662Svikram 
7124eb2bd662Svikram 	INJECT_ERROR1("NEW_SIGN_FSTYPE", fstype = "foofs");
7125eb2bd662Svikram 
7126eb2bd662Svikram 	if (strcmp(fstype, "zfs") == 0) {
7127*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: created new ZFS sign\n", fcn));
7128eb2bd662Svikram 		sign = create_zfs_sign(osdev);
7129eb2bd662Svikram 	} else if (strcmp(fstype, "ufs") == 0) {
7130*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: created new UFS sign\n", fcn));
7131eb2bd662Svikram 		sign = create_ufs_sign();
7132eb2bd662Svikram 	} else {
7133*f64ca102SToomas Soome 		bam_error(_("boot signature not supported for fstype: %s\n"),
7134*f64ca102SToomas Soome 		    fstype);
7135eb2bd662Svikram 		sign = NULL;
7136eb2bd662Svikram 	}
7137eb2bd662Svikram 
7138*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: created new sign: %s\n", fcn,
7139*f64ca102SToomas Soome 	    sign ? sign : "<NULL>"));
7140eb2bd662Svikram 	return (sign);
7141eb2bd662Svikram }
7142eb2bd662Svikram 
7143eb2bd662Svikram static int
7144eb2bd662Svikram set_backup_common(char *mntpt, char *sign)
7145eb2bd662Svikram {
7146eb2bd662Svikram 	FILE		*bfp;
7147eb2bd662Svikram 	char		backup[PATH_MAX];
7148eb2bd662Svikram 	char		tmpsign[PATH_MAX];
7149eb2bd662Svikram 	int		error;
7150eb2bd662Svikram 	char		*bdir;
7151eb2bd662Svikram 	char		*backup_dup;
7152eb2bd662Svikram 	struct stat	sb;
7153eb2bd662Svikram 	int		ret;
7154eb2bd662Svikram 	const char	*fcn = "set_backup_common()";
7155eb2bd662Svikram 
7156eb2bd662Svikram 	(void) snprintf(backup, sizeof (backup), "%s%s",
7157eb2bd662Svikram 	    mntpt, GRUBSIGN_BACKUP);
7158eb2bd662Svikram 
7159eb2bd662Svikram 	/* First read the backup */
7160eb2bd662Svikram 	bfp = fopen(backup, "r");
7161eb2bd662Svikram 	if (bfp != NULL) {
7162eb2bd662Svikram 		while (s_fgets(tmpsign, sizeof (tmpsign), bfp)) {
7163eb2bd662Svikram 			if (strcmp(tmpsign, sign) == 0) {
7164*f64ca102SToomas Soome 				BAM_DPRINTF(("%s: found sign (%s) in backup.\n",
7165*f64ca102SToomas Soome 				    fcn, sign));
7166eb2bd662Svikram 				(void) fclose(bfp);
7167eb2bd662Svikram 				return (0);
7168eb2bd662Svikram 			}
7169eb2bd662Svikram 		}
7170eb2bd662Svikram 		(void) fclose(bfp);
7171*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: backup exists but sign %s not found\n",
7172*f64ca102SToomas Soome 		    fcn, sign));
7173eb2bd662Svikram 	} else {
7174*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: no backup file (%s) found.\n", fcn, backup));
7175eb2bd662Svikram 	}
7176eb2bd662Svikram 
7177eb2bd662Svikram 	/*
7178eb2bd662Svikram 	 * Didn't find the correct signature. First create
7179eb2bd662Svikram 	 * the directory if necessary.
7180eb2bd662Svikram 	 */
7181eb2bd662Svikram 
7182eb2bd662Svikram 	/* dirname() modifies its argument so dup it */
7183eb2bd662Svikram 	backup_dup = s_strdup(backup);
7184eb2bd662Svikram 	bdir = dirname(backup_dup);
7185eb2bd662Svikram 	assert(bdir);
7186eb2bd662Svikram 
7187eb2bd662Svikram 	ret = stat(bdir, &sb);
7188eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_STAT", ret = -1);
7189eb2bd662Svikram 	if (ret == -1) {
7190*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: backup dir (%s) does not exist.\n",
7191*f64ca102SToomas Soome 		    fcn, bdir));
719248847494SEnrico Perla - Sun Microsystems 		ret = mkdirp(bdir, DIR_PERMS);
7193eb2bd662Svikram 		error = errno;
7194eb2bd662Svikram 		INJECT_ERROR1("SET_BACKUP_MKDIRP", ret = -1);
7195eb2bd662Svikram 		if (ret == -1) {
7196*f64ca102SToomas Soome 			bam_error(_("mkdirp() of backup dir failed: %s: %s\n"),
7197eb2bd662Svikram 			    GRUBSIGN_BACKUP, strerror(error));
7198eb2bd662Svikram 			free(backup_dup);
7199eb2bd662Svikram 			return (-1);
7200eb2bd662Svikram 		}
7201eb2bd662Svikram 	}
7202eb2bd662Svikram 	free(backup_dup);
7203eb2bd662Svikram 
7204eb2bd662Svikram 	/*
7205eb2bd662Svikram 	 * Open the backup in append mode to add the correct
7206eb2bd662Svikram 	 * signature;
7207eb2bd662Svikram 	 */
7208eb2bd662Svikram 	bfp = fopen(backup, "a");
7209eb2bd662Svikram 	error = errno;
7210eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_FOPEN_A", bfp = NULL);
7211eb2bd662Svikram 	if (bfp == NULL) {
7212*f64ca102SToomas Soome 		bam_error(_("error opening boot signature backup "
7213*f64ca102SToomas Soome 		    "file %s: %s\n"), GRUBSIGN_BACKUP, strerror(error));
7214eb2bd662Svikram 		return (-1);
7215eb2bd662Svikram 	}
7216eb2bd662Svikram 
7217eb2bd662Svikram 	(void) snprintf(tmpsign, sizeof (tmpsign), "%s\n", sign);
7218eb2bd662Svikram 
7219eb2bd662Svikram 	ret = fputs(tmpsign, bfp);
7220eb2bd662Svikram 	error = errno;
7221eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_FPUTS", ret = 0);
7222eb2bd662Svikram 	if (ret != strlen(tmpsign)) {
7223*f64ca102SToomas Soome 		bam_error(_("error writing boot signature backup "
7224*f64ca102SToomas Soome 		    "file %s: %s\n"), GRUBSIGN_BACKUP, strerror(error));
7225eb2bd662Svikram 		(void) fclose(bfp);
7226eb2bd662Svikram 		return (-1);
7227eb2bd662Svikram 	}
7228eb2bd662Svikram 
7229eb2bd662Svikram 	(void) fclose(bfp);
7230eb2bd662Svikram 
7231eb2bd662Svikram 	if (bam_verbose)
7232*f64ca102SToomas Soome 		bam_print(_("updated boot signature backup file %s\n"),
7233*f64ca102SToomas Soome 		    GRUBSIGN_BACKUP);
7234eb2bd662Svikram 
7235*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7236eb2bd662Svikram 
7237eb2bd662Svikram 	return (0);
7238eb2bd662Svikram }
7239eb2bd662Svikram 
7240eb2bd662Svikram static int
7241eb2bd662Svikram set_backup_ufs(char *osroot, char *sign)
7242eb2bd662Svikram {
7243eb2bd662Svikram 	const char	*fcn = "set_backup_ufs()";
7244eb2bd662Svikram 
7245*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osroot, sign));
7246eb2bd662Svikram 	return (set_backup_common(osroot, sign));
7247eb2bd662Svikram }
7248eb2bd662Svikram 
7249eb2bd662Svikram static int
7250eb2bd662Svikram set_backup_zfs(char *osdev, char *sign)
7251eb2bd662Svikram {
7252eb2bd662Svikram 	char		*pool;
7253eb2bd662Svikram 	char		*mntpt;
7254eb2bd662Svikram 	zfs_mnted_t	mnted;
7255eb2bd662Svikram 	int		ret;
7256eb2bd662Svikram 	const char	*fcn = "set_backup_zfs()";
7257eb2bd662Svikram 
7258*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osdev, sign));
7259eb2bd662Svikram 
7260eb2bd662Svikram 	pool = get_pool(osdev);
7261eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_GET_POOL", pool = NULL);
7262eb2bd662Svikram 	if (pool == NULL) {
7263*f64ca102SToomas Soome 		bam_error(_("failed to get pool name from %s\n"), osdev);
7264eb2bd662Svikram 		return (-1);
7265eb2bd662Svikram 	}
7266eb2bd662Svikram 
7267eb2bd662Svikram 	mntpt = mount_top_dataset(pool, &mnted);
7268eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_MOUNT_DATASET", mntpt = NULL);
7269eb2bd662Svikram 	if (mntpt == NULL) {
7270*f64ca102SToomas Soome 		bam_error(_("failed to mount top dataset for %s\n"), pool);
7271eb2bd662Svikram 		free(pool);
7272eb2bd662Svikram 		return (-1);
7273eb2bd662Svikram 	}
7274eb2bd662Svikram 
7275eb2bd662Svikram 	ret = set_backup_common(mntpt, sign);
7276eb2bd662Svikram 
7277eb2bd662Svikram 	(void) umount_top_dataset(pool, mnted, mntpt);
7278eb2bd662Svikram 
7279eb2bd662Svikram 	free(pool);
7280eb2bd662Svikram 
7281eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_ZFS_FAIL", ret = 1);
7282eb2bd662Svikram 	if (ret == 0) {
7283*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7284eb2bd662Svikram 	} else {
7285*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
7286eb2bd662Svikram 	}
7287eb2bd662Svikram 
7288eb2bd662Svikram 	return (ret);
7289eb2bd662Svikram }
7290eb2bd662Svikram 
7291eb2bd662Svikram static int
7292eb2bd662Svikram set_backup(char *osroot, char *osdev, char *sign, char *fstype)
7293eb2bd662Svikram {
7294eb2bd662Svikram 	const char	*fcn = "set_backup()";
7295eb2bd662Svikram 	int		ret;
7296eb2bd662Svikram 
7297eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_FSTYPE", fstype = "foofs");
7298eb2bd662Svikram 
7299eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0) {
7300*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting UFS backup sign\n", fcn));
7301eb2bd662Svikram 		ret = set_backup_ufs(osroot, sign);
7302eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0) {
7303*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting ZFS backup sign\n", fcn));
7304eb2bd662Svikram 		ret = set_backup_zfs(osdev, sign);
7305eb2bd662Svikram 	} else {
7306*f64ca102SToomas Soome 		bam_error(_("boot signature not supported for fstype: %s\n"),
7307*f64ca102SToomas Soome 		    fstype);
7308eb2bd662Svikram 		ret = -1;
7309eb2bd662Svikram 	}
7310eb2bd662Svikram 
7311eb2bd662Svikram 	if (ret == 0) {
7312*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7313eb2bd662Svikram 	} else {
7314*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
7315eb2bd662Svikram 	}
7316eb2bd662Svikram 
7317eb2bd662Svikram 	return (ret);
7318eb2bd662Svikram }
7319eb2bd662Svikram 
7320eb2bd662Svikram static int
7321eb2bd662Svikram set_primary_common(char *mntpt, char *sign)
7322eb2bd662Svikram {
7323eb2bd662Svikram 	char		signfile[PATH_MAX];
7324eb2bd662Svikram 	char		signdir[PATH_MAX];
7325eb2bd662Svikram 	struct stat	sb;
7326eb2bd662Svikram 	int		fd;
7327eb2bd662Svikram 	int		error;
7328eb2bd662Svikram 	int		ret;
7329eb2bd662Svikram 	const char	*fcn = "set_primary_common()";
7330eb2bd662Svikram 
7331eb2bd662Svikram 	(void) snprintf(signfile, sizeof (signfile), "%s/%s/%s",
7332eb2bd662Svikram 	    mntpt, GRUBSIGN_DIR, sign);
7333eb2bd662Svikram 
7334eb2bd662Svikram 	if (stat(signfile, &sb) != -1) {
7335eb2bd662Svikram 		if (bam_verbose)
7336*f64ca102SToomas Soome 			bam_print(_("primary sign %s exists\n"), sign);
7337eb2bd662Svikram 		return (0);
7338eb2bd662Svikram 	} else {
7339*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: primary sign (%s) does not exist\n",
7340*f64ca102SToomas Soome 		    fcn, signfile));
7341eb2bd662Svikram 	}
7342eb2bd662Svikram 
7343eb2bd662Svikram 	(void) snprintf(signdir, sizeof (signdir), "%s/%s",
7344eb2bd662Svikram 	    mntpt, GRUBSIGN_DIR);
7345eb2bd662Svikram 
7346eb2bd662Svikram 	if (stat(signdir, &sb) == -1) {
7347*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: primary signdir (%s) does not exist\n",
7348*f64ca102SToomas Soome 		    fcn, signdir));
734948847494SEnrico Perla - Sun Microsystems 		ret = mkdirp(signdir, DIR_PERMS);
7350eb2bd662Svikram 		error = errno;
7351eb2bd662Svikram 		INJECT_ERROR1("SET_PRIMARY_MKDIRP", ret = -1);
7352eb2bd662Svikram 		if (ret == -1) {
7353*f64ca102SToomas Soome 			bam_error(_("error creating boot signature "
7354*f64ca102SToomas Soome 			    "directory %s: %s\n"), signdir, strerror(errno));
7355eb2bd662Svikram 			return (-1);
7356eb2bd662Svikram 		}
7357eb2bd662Svikram 	}
7358eb2bd662Svikram 
7359eb2bd662Svikram 	fd = open(signfile, O_RDWR|O_CREAT|O_TRUNC, 0444);
7360eb2bd662Svikram 	error = errno;
7361eb2bd662Svikram 	INJECT_ERROR1("PRIMARY_SIGN_CREAT", fd = -1);
7362eb2bd662Svikram 	if (fd == -1) {
7363*f64ca102SToomas Soome 		bam_error(_("error creating primary boot signature %s: %s\n"),
7364*f64ca102SToomas Soome 		    signfile, strerror(error));
7365eb2bd662Svikram 		return (-1);
7366eb2bd662Svikram 	}
7367eb2bd662Svikram 
7368eb2bd662Svikram 	ret = fsync(fd);
7369eb2bd662Svikram 	error = errno;
7370eb2bd662Svikram 	INJECT_ERROR1("PRIMARY_FSYNC", ret = -1);
7371eb2bd662Svikram 	if (ret != 0) {
7372*f64ca102SToomas Soome 		bam_error(_("error syncing primary boot signature %s: %s\n"),
7373*f64ca102SToomas Soome 		    signfile, strerror(error));
7374eb2bd662Svikram 	}
7375eb2bd662Svikram 
7376eb2bd662Svikram 	(void) close(fd);
7377eb2bd662Svikram 
7378eb2bd662Svikram 	if (bam_verbose)
7379*f64ca102SToomas Soome 		bam_print(_("created primary GRUB boot signature: %s\n"),
7380*f64ca102SToomas Soome 		    signfile);
7381eb2bd662Svikram 
7382*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7383eb2bd662Svikram 
7384eb2bd662Svikram 	return (0);
7385eb2bd662Svikram }
7386eb2bd662Svikram 
7387eb2bd662Svikram static int
7388eb2bd662Svikram set_primary_ufs(char *osroot, char *sign)
7389eb2bd662Svikram {
7390eb2bd662Svikram 	const char	*fcn = "set_primary_ufs()";
7391eb2bd662Svikram 
7392*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osroot, sign));
7393eb2bd662Svikram 	return (set_primary_common(osroot, sign));
7394eb2bd662Svikram }
7395eb2bd662Svikram 
7396eb2bd662Svikram static int
7397eb2bd662Svikram set_primary_zfs(char *osdev, char *sign)
7398eb2bd662Svikram {
7399eb2bd662Svikram 	char		*pool;
7400eb2bd662Svikram 	char		*mntpt;
7401eb2bd662Svikram 	zfs_mnted_t	mnted;
7402eb2bd662Svikram 	int		ret;
7403eb2bd662Svikram 	const char	*fcn = "set_primary_zfs()";
7404eb2bd662Svikram 
7405*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osdev, sign));
7406eb2bd662Svikram 
7407eb2bd662Svikram 	pool = get_pool(osdev);
7408eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_GET_POOL", pool = NULL);
7409eb2bd662Svikram 	if (pool == NULL) {
7410*f64ca102SToomas Soome 		bam_error(_("failed to get pool name from %s\n"), osdev);
7411eb2bd662Svikram 		return (-1);
7412eb2bd662Svikram 	}
7413eb2bd662Svikram 
7414eb2bd662Svikram 	/* Pool name must exist in the sign */
7415eb2bd662Svikram 	ret = (strstr(sign, pool) != NULL);
7416eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_POOL_SIGN_INCOMPAT", ret = 0);
7417eb2bd662Svikram 	if (ret == 0) {
7418*f64ca102SToomas Soome 		bam_error(_("pool name %s not present in signature %s\n"),
7419*f64ca102SToomas Soome 		    pool, sign);
7420eb2bd662Svikram 		free(pool);
7421eb2bd662Svikram 		return (-1);
7422eb2bd662Svikram 	}
7423eb2bd662Svikram 
7424eb2bd662Svikram 	mntpt = mount_top_dataset(pool, &mnted);
7425eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_MOUNT_DATASET", mntpt = NULL);
7426eb2bd662Svikram 	if (mntpt == NULL) {
7427*f64ca102SToomas Soome 		bam_error(_("failed to mount top dataset for %s\n"), pool);
7428eb2bd662Svikram 		free(pool);
7429eb2bd662Svikram 		return (-1);
7430eb2bd662Svikram 	}
7431eb2bd662Svikram 
7432eb2bd662Svikram 	ret = set_primary_common(mntpt, sign);
7433eb2bd662Svikram 
7434eb2bd662Svikram 	(void) umount_top_dataset(pool, mnted, mntpt);
7435eb2bd662Svikram 
7436eb2bd662Svikram 	free(pool);
7437eb2bd662Svikram 
7438eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_FAIL", ret = 1);
7439eb2bd662Svikram 	if (ret == 0) {
7440*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7441eb2bd662Svikram 	} else {
7442*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
7443eb2bd662Svikram 	}
7444eb2bd662Svikram 
7445eb2bd662Svikram 	return (ret);
7446eb2bd662Svikram }
7447eb2bd662Svikram 
7448eb2bd662Svikram static int
7449eb2bd662Svikram set_primary(char *osroot, char *osdev, char *sign, char *fstype)
7450eb2bd662Svikram {
7451eb2bd662Svikram 	const char	*fcn = "set_primary()";
7452eb2bd662Svikram 	int		ret;
7453eb2bd662Svikram 
7454eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_FSTYPE", fstype = "foofs");
7455eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0) {
7456*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting UFS primary sign\n", fcn));
7457eb2bd662Svikram 		ret = set_primary_ufs(osroot, sign);
7458eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0) {
7459*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting ZFS primary sign\n", fcn));
7460eb2bd662Svikram 		ret = set_primary_zfs(osdev, sign);
7461eb2bd662Svikram 	} else {
7462*f64ca102SToomas Soome 		bam_error(_("boot signature not supported for fstype: %s\n"),
7463*f64ca102SToomas Soome 		    fstype);
7464eb2bd662Svikram 		ret = -1;
7465eb2bd662Svikram 	}
7466eb2bd662Svikram 
7467eb2bd662Svikram 	if (ret == 0) {
7468*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7469eb2bd662Svikram 	} else {
7470*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
7471eb2bd662Svikram 	}
7472eb2bd662Svikram 
7473eb2bd662Svikram 	return (ret);
7474eb2bd662Svikram }
7475eb2bd662Svikram 
7476eb2bd662Svikram static int
7477eb2bd662Svikram ufs_add_to_sign_list(char *sign)
7478eb2bd662Svikram {
7479eb2bd662Svikram 	FILE		*tfp;
7480eb2bd662Svikram 	char		signline[MAXNAMELEN];
7481eb2bd662Svikram 	char		cmd[PATH_MAX];
7482eb2bd662Svikram 	int		ret;
7483eb2bd662Svikram 	int		error;
7484eb2bd662Svikram 	const char	*fcn = "ufs_add_to_sign_list()";
7485eb2bd662Svikram 
7486eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_NOT_UFS", sign = "pool_rpool5");
7487eb2bd662Svikram 	if (strncmp(sign, GRUBSIGN_UFS_PREFIX,
7488eb2bd662Svikram 	    strlen(GRUBSIGN_UFS_PREFIX)) != 0) {
7489*f64ca102SToomas Soome 		bam_error(_("invalid UFS boot signature %s\n"), sign);
7490eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
7491eb2bd662Svikram 		return (-1);
7492eb2bd662Svikram 	}
7493eb2bd662Svikram 
7494eb2bd662Svikram 	/*
7495eb2bd662Svikram 	 * most failures in this routine are not a fatal error
7496eb2bd662Svikram 	 * We simply unlink the /var/run file and continue
7497eb2bd662Svikram 	 */
7498eb2bd662Svikram 
7499eb2bd662Svikram 	ret = rename(UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST".tmp");
7500eb2bd662Svikram 	error = errno;
7501eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_RENAME", ret = -1);
7502eb2bd662Svikram 	if (ret == -1) {
7503*f64ca102SToomas Soome 		bam_error(_("rename to file failed: %s: %s\n"),
7504*f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST".tmp", strerror(error));
7505eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
7506eb2bd662Svikram 		return (0);
7507eb2bd662Svikram 	}
7508eb2bd662Svikram 
7509eb2bd662Svikram 	tfp = fopen(UFS_SIGNATURE_LIST".tmp", "a");
7510eb2bd662Svikram 	error = errno;
7511eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FOPEN", tfp = NULL);
7512eb2bd662Svikram 	if (tfp == NULL) {
7513*f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"),
7514*f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST".tmp", strerror(error));
7515eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
7516eb2bd662Svikram 		return (0);
7517eb2bd662Svikram 	}
7518eb2bd662Svikram 
7519eb2bd662Svikram 	(void) snprintf(signline, sizeof (signline), "%s\n", sign);
7520eb2bd662Svikram 
7521eb2bd662Svikram 	ret = fputs(signline, tfp);
7522eb2bd662Svikram 	error = errno;
7523eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FPUTS", ret = 0);
7524eb2bd662Svikram 	if (ret != strlen(signline)) {
7525*f64ca102SToomas Soome 		bam_error(_("failed to write signature %s to signature "
7526*f64ca102SToomas Soome 		    "list: %s\n"), sign, strerror(error));
7527eb2bd662Svikram 		(void) fclose(tfp);
7528eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
7529eb2bd662Svikram 		return (0);
7530eb2bd662Svikram 	}
7531eb2bd662Svikram 
7532eb2bd662Svikram 	ret = fclose(tfp);
7533eb2bd662Svikram 	error = errno;
7534eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FCLOSE", ret = EOF);
7535eb2bd662Svikram 	if (ret == EOF) {
7536*f64ca102SToomas Soome 		bam_error(_("failed to close file: %s: %s\n"),
7537*f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST".tmp", strerror(error));
7538eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
7539eb2bd662Svikram 		return (0);
7540eb2bd662Svikram 	}
7541eb2bd662Svikram 
7542eb2bd662Svikram 	/* Sort the list again */
7543eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
7544eb2bd662Svikram 	    "/usr/bin/sort -u %s.tmp > %s.sorted",
7545eb2bd662Svikram 	    UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST);
7546eb2bd662Svikram 
7547eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
7548eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_SORT", ret = 1);
7549eb2bd662Svikram 	if (ret != 0) {
7550*f64ca102SToomas Soome 		bam_error(_("error sorting GRUB UFS boot signatures\n"));
7551eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
7552eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
7553eb2bd662Svikram 		return (0);
7554eb2bd662Svikram 	}
7555eb2bd662Svikram 
7556eb2bd662Svikram 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
7557eb2bd662Svikram 
7558eb2bd662Svikram 	ret = rename(UFS_SIGNATURE_LIST".sorted", UFS_SIGNATURE_LIST);
7559eb2bd662Svikram 	error = errno;
7560eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_RENAME2", ret = -1);
7561eb2bd662Svikram 	if (ret == -1) {
7562*f64ca102SToomas Soome 		bam_error(_("rename to file failed: %s: %s\n"),
7563*f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST, strerror(error));
7564eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
7565eb2bd662Svikram 		return (0);
7566eb2bd662Svikram 	}
7567eb2bd662Svikram 
7568*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7569eb2bd662Svikram 
7570eb2bd662Svikram 	return (0);
7571eb2bd662Svikram }
7572eb2bd662Svikram 
7573eb2bd662Svikram static int
7574eb2bd662Svikram set_signature(char *osroot, char *osdev, char *sign, char *fstype)
7575eb2bd662Svikram {
7576eb2bd662Svikram 	int		ret;
7577eb2bd662Svikram 	const char	*fcn = "set_signature()";
7578eb2bd662Svikram 
7579*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s %s %s\n", fcn,
7580*f64ca102SToomas Soome 	    osroot, osdev, sign, fstype));
7581eb2bd662Svikram 
7582eb2bd662Svikram 	ret = set_backup(osroot, osdev, sign, fstype);
7583eb2bd662Svikram 	INJECT_ERROR1("SET_SIGNATURE_BACKUP", ret = -1);
7584eb2bd662Svikram 	if (ret == -1) {
7585*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
7586*f64ca102SToomas Soome 		bam_error(_("failed to set backup sign (%s) for %s: %s\n"),
7587*f64ca102SToomas Soome 		    sign, osroot, osdev);
7588eb2bd662Svikram 		return (-1);
7589eb2bd662Svikram 	}
7590eb2bd662Svikram 
7591eb2bd662Svikram 	ret = set_primary(osroot, osdev, sign, fstype);
7592eb2bd662Svikram 	INJECT_ERROR1("SET_SIGNATURE_PRIMARY", ret = -1);
7593eb2bd662Svikram 
7594eb2bd662Svikram 	if (ret == 0) {
7595*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7596eb2bd662Svikram 	} else {
7597*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
7598*f64ca102SToomas Soome 		bam_error(_("failed to set primary sign (%s) for %s: %s\n"),
7599*f64ca102SToomas Soome 		    sign, osroot, osdev);
7600eb2bd662Svikram 
7601eb2bd662Svikram 	}
7602eb2bd662Svikram 	return (ret);
7603eb2bd662Svikram }
7604eb2bd662Svikram 
7605eb2bd662Svikram char *
7606eb2bd662Svikram get_grubsign(char *osroot, char *osdev)
7607eb2bd662Svikram {
7608eb2bd662Svikram 	char		*grubsign;	/* (<sign>,#,#) */
7609eb2bd662Svikram 	char		*slice;
7610eb2bd662Svikram 	int		fdiskpart;
7611eb2bd662Svikram 	char		*sign;
7612eb2bd662Svikram 	char		*fstype;
7613eb2bd662Svikram 	int		ret;
7614eb2bd662Svikram 	const char	*fcn = "get_grubsign()";
7615eb2bd662Svikram 
7616*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osroot, osdev));
7617eb2bd662Svikram 	fstype = get_fstype(osroot);
7618eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBSIGN_FSTYPE", fstype = NULL);
7619eb2bd662Svikram 	if (fstype == NULL) {
7620*f64ca102SToomas Soome 		bam_error(_("failed to get fstype for %s\n"), osroot);
7621eb2bd662Svikram 		return (NULL);
7622eb2bd662Svikram 	}
7623eb2bd662Svikram 
7624eb2bd662Svikram 	sign = find_existing_sign(osroot, osdev, fstype);
7625eb2bd662Svikram 	INJECT_ERROR1("FIND_EXISTING_SIGN", sign = NULL);
7626eb2bd662Svikram 	if (sign == NULL) {
7627*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: no existing grubsign for %s: %s\n",
7628*f64ca102SToomas Soome 		    fcn, osroot, osdev));
7629eb2bd662Svikram 		sign = create_new_sign(osdev, fstype);
7630eb2bd662Svikram 		INJECT_ERROR1("CREATE_NEW_SIGN", sign = NULL);
7631eb2bd662Svikram 		if (sign == NULL) {
7632*f64ca102SToomas Soome 			bam_error(_("failed to create GRUB boot signature for "
7633*f64ca102SToomas Soome 			    "device: %s\n"), osdev);
7634eb2bd662Svikram 			free(fstype);
7635eb2bd662Svikram 			return (NULL);
7636eb2bd662Svikram 		}
7637eb2bd662Svikram 	}
7638eb2bd662Svikram 
7639eb2bd662Svikram 	ret = set_signature(osroot, osdev, sign, fstype);
7640eb2bd662Svikram 	INJECT_ERROR1("SET_SIGNATURE_FAIL", ret = -1);
7641eb2bd662Svikram 	if (ret == -1) {
7642*f64ca102SToomas Soome 		bam_error(_("failed to write GRUB boot signature for "
7643*f64ca102SToomas Soome 		    "device: %s\n"), osdev);
7644eb2bd662Svikram 		free(sign);
7645eb2bd662Svikram 		free(fstype);
7646eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
7647eb2bd662Svikram 		return (NULL);
7648eb2bd662Svikram 	}
7649eb2bd662Svikram 
7650eb2bd662Svikram 	free(fstype);
7651eb2bd662Svikram 
7652eb2bd662Svikram 	if (bam_verbose)
7653*f64ca102SToomas Soome 		bam_print(_("found or created GRUB signature %s for %s\n"),
7654*f64ca102SToomas Soome 		    sign, osdev);
7655eb2bd662Svikram 
7656eb2bd662Svikram 	fdiskpart = get_partition(osdev);
76571a902ef8SHans Rosenfeld 	INJECT_ERROR1("GET_GRUBSIGN_FDISK", fdiskpart = PARTNO_NOTFOUND);
76581a902ef8SHans Rosenfeld 	if (fdiskpart == PARTNO_NOTFOUND) {
7659*f64ca102SToomas Soome 		bam_error(_("failed to determine fdisk partition: %s\n"),
7660*f64ca102SToomas Soome 		    osdev);
7661eb2bd662Svikram 		free(sign);
7662eb2bd662Svikram 		return (NULL);
7663eb2bd662Svikram 	}
7664eb2bd662Svikram 
7665eb2bd662Svikram 	slice = strrchr(osdev, 's');
7666eb2bd662Svikram 
76671a902ef8SHans Rosenfeld 	if (fdiskpart == PARTNO_EFI) {
76681a902ef8SHans Rosenfeld 		fdiskpart = atoi(&slice[1]);
76691a902ef8SHans Rosenfeld 		slice = NULL;
76701a902ef8SHans Rosenfeld 	}
76711a902ef8SHans Rosenfeld 
7672eb2bd662Svikram 	grubsign = s_calloc(1, MAXNAMELEN + 10);
7673eb2bd662Svikram 	if (slice) {
7674eb2bd662Svikram 		(void) snprintf(grubsign, MAXNAMELEN + 10, "(%s,%d,%c)",
7675eb2bd662Svikram 		    sign, fdiskpart, slice[1] + 'a' - '0');
7676eb2bd662Svikram 	} else
7677eb2bd662Svikram 		(void) snprintf(grubsign, MAXNAMELEN + 10, "(%s,%d)",
7678eb2bd662Svikram 		    sign, fdiskpart);
7679eb2bd662Svikram 
7680eb2bd662Svikram 	free(sign);
7681eb2bd662Svikram 
7682*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: successfully created grubsign %s\n", fcn, grubsign));
7683eb2bd662Svikram 
7684eb2bd662Svikram 	return (grubsign);
76857c478bd9Sstevel@tonic-gate }
76867c478bd9Sstevel@tonic-gate 
7687ee3b8144Sszhou static char *
7688ee3b8144Sszhou get_title(char *rootdir)
76897c478bd9Sstevel@tonic-gate {
7690eb2bd662Svikram 	static char	title[80];
7691eb2bd662Svikram 	char		*cp = NULL;
7692eb2bd662Svikram 	char		release[PATH_MAX];
76937c478bd9Sstevel@tonic-gate 	FILE		*fp;
7694eb2bd662Svikram 	const char	*fcn = "get_title()";
76957c478bd9Sstevel@tonic-gate 
76967c478bd9Sstevel@tonic-gate 	/* open the /etc/release file */
76977c478bd9Sstevel@tonic-gate 	(void) snprintf(release, sizeof (release), "%s/etc/release", rootdir);
76987c478bd9Sstevel@tonic-gate 
76997c478bd9Sstevel@tonic-gate 	fp = fopen(release, "r");
7700eb2bd662Svikram 	if (fp == NULL) {
7701*f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), release,
7702*f64ca102SToomas Soome 		    strerror(errno));
7703eb2bd662Svikram 		cp = NULL;
7704eb2bd662Svikram 		goto out;
7705eb2bd662Svikram 	}
77067c478bd9Sstevel@tonic-gate 
7707655ac28eSGeorge Asaad 	/* grab first line of /etc/release */
7708655ac28eSGeorge Asaad 	cp = s_fgets(title, sizeof (title), fp);
7709655ac28eSGeorge Asaad 	if (cp) {
7710655ac28eSGeorge Asaad 		while (isspace(*cp))    /* remove leading spaces */
7711655ac28eSGeorge Asaad 			cp++;
77127c478bd9Sstevel@tonic-gate 	}
7713655ac28eSGeorge Asaad 
77147c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
7715eb2bd662Svikram 
7716eb2bd662Svikram out:
7717655ac28eSGeorge Asaad 	cp = cp ? cp : "Oracle Solaris";
7718eb2bd662Svikram 
7719*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: got title: %s\n", fcn, cp));
7720eb2bd662Svikram 
7721eb2bd662Svikram 	return (cp);
77227c478bd9Sstevel@tonic-gate }
77237c478bd9Sstevel@tonic-gate 
7724ae115bc7Smrj char *
77257c478bd9Sstevel@tonic-gate get_special(char *mountp)
77267c478bd9Sstevel@tonic-gate {
77277c478bd9Sstevel@tonic-gate 	FILE		*mntfp;
7728eb2bd662Svikram 	struct mnttab	mp = {0};
7729eb2bd662Svikram 	struct mnttab	mpref = {0};
7730eb2bd662Svikram 	int		error;
7731eb2bd662Svikram 	int		ret;
7732eb2bd662Svikram 	const char 	*fcn = "get_special()";
7733eb2bd662Svikram 
7734eb2bd662Svikram 	INJECT_ERROR1("GET_SPECIAL_MNTPT", mountp = NULL);
7735eb2bd662Svikram 	if (mountp == NULL) {
7736*f64ca102SToomas Soome 		bam_error(_("cannot get special file: NULL mount-point\n"));
7737eb2bd662Svikram 		return (NULL);
7738eb2bd662Svikram 	}
77397c478bd9Sstevel@tonic-gate 
77407c478bd9Sstevel@tonic-gate 	mntfp = fopen(MNTTAB, "r");
7741eb2bd662Svikram 	error = errno;
7742eb2bd662Svikram 	INJECT_ERROR1("GET_SPECIAL_MNTTAB_OPEN", mntfp = NULL);
77437c478bd9Sstevel@tonic-gate 	if (mntfp == NULL) {
7744*f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), MNTTAB,
7745*f64ca102SToomas Soome 		    strerror(error));
7746eb2bd662Svikram 		return (NULL);
77477c478bd9Sstevel@tonic-gate 	}
77487c478bd9Sstevel@tonic-gate 
77497c478bd9Sstevel@tonic-gate 	if (*mountp == '\0')
77507c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = "/";
77517c478bd9Sstevel@tonic-gate 	else
77527c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = mountp;
7753eb2bd662Svikram 
7754eb2bd662Svikram 	ret = getmntany(mntfp, &mp, &mpref);
7755eb2bd662Svikram 	INJECT_ERROR1("GET_SPECIAL_MNTTAB_SEARCH", ret = 1);
7756eb2bd662Svikram 	if (ret != 0) {
77577c478bd9Sstevel@tonic-gate 		(void) fclose(mntfp);
7758*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: Cannot get special file:  mount-point %s "
7759*f64ca102SToomas Soome 		    "not in mnttab\n", fcn, mountp));
77607c478bd9Sstevel@tonic-gate 		return (NULL);
77617c478bd9Sstevel@tonic-gate 	}
77627c478bd9Sstevel@tonic-gate 	(void) fclose(mntfp);
77637c478bd9Sstevel@tonic-gate 
7764*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning special: %s\n", fcn, mp.mnt_special));
7765eb2bd662Svikram 
77667c478bd9Sstevel@tonic-gate 	return (s_strdup(mp.mnt_special));
77677c478bd9Sstevel@tonic-gate }
77687c478bd9Sstevel@tonic-gate 
7769eb2bd662Svikram static void
7770eb2bd662Svikram free_physarray(char **physarray, int n)
77717c478bd9Sstevel@tonic-gate {
7772eb2bd662Svikram 	int			i;
7773eb2bd662Svikram 	const char		*fcn = "free_physarray()";
77747c478bd9Sstevel@tonic-gate 
7775eb2bd662Svikram 	assert(physarray);
7776eb2bd662Svikram 	assert(n);
7777eb2bd662Svikram 
7778*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entering args: %d\n", fcn, n));
7779eb2bd662Svikram 
7780eb2bd662Svikram 	for (i = 0; i < n; i++) {
7781eb2bd662Svikram 		free(physarray[i]);
77827c478bd9Sstevel@tonic-gate 	}
7783eb2bd662Svikram 	free(physarray);
7784eb2bd662Svikram 
7785*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7786eb2bd662Svikram }
7787eb2bd662Svikram 
7788eb2bd662Svikram static int
7789eb2bd662Svikram zfs_get_physical(char *special, char ***physarray, int *n)
7790eb2bd662Svikram {
7791eb2bd662Svikram 	char			sdup[PATH_MAX];
7792eb2bd662Svikram 	char			cmd[PATH_MAX];
7793eb2bd662Svikram 	char			dsk[PATH_MAX];
7794eb2bd662Svikram 	char			*pool;
7795eb2bd662Svikram 	filelist_t		flist = {0};
7796eb2bd662Svikram 	line_t			*lp;
7797eb2bd662Svikram 	line_t			*startlp;
7798eb2bd662Svikram 	char			*comp1;
7799eb2bd662Svikram 	int			i;
7800eb2bd662Svikram 	int			ret;
7801eb2bd662Svikram 	const char		*fcn = "zfs_get_physical()";
7802eb2bd662Svikram 
7803eb2bd662Svikram 	assert(special);
7804eb2bd662Svikram 
7805*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. arg: %s\n", fcn, special));
7806eb2bd662Svikram 
7807eb2bd662Svikram 	INJECT_ERROR1("INVALID_ZFS_SPECIAL", special = "/foo");
7808eb2bd662Svikram 	if (special[0] == '/') {
7809*f64ca102SToomas Soome 		bam_error(_("invalid device for ZFS filesystem: %s\n"),
7810*f64ca102SToomas Soome 		    special);
7811eb2bd662Svikram 		return (-1);
7812eb2bd662Svikram 	}
7813eb2bd662Svikram 
7814eb2bd662Svikram 	(void) strlcpy(sdup, special, sizeof (sdup));
7815eb2bd662Svikram 
7816eb2bd662Svikram 	pool = strtok(sdup, "/");
7817eb2bd662Svikram 	INJECT_ERROR1("ZFS_GET_PHYS_POOL", pool = NULL);
7818eb2bd662Svikram 	if (pool == NULL) {
7819*f64ca102SToomas Soome 		bam_error(_("cannot derive ZFS pool from special: %s\n"),
7820*f64ca102SToomas Soome 		    special);
7821eb2bd662Svikram 		return (-1);
7822eb2bd662Svikram 	}
7823eb2bd662Svikram 
7824eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/zpool status %s", pool);
7825eb2bd662Svikram 
7826eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
7827eb2bd662Svikram 	INJECT_ERROR1("ZFS_GET_PHYS_STATUS", ret = 1);
7828eb2bd662Svikram 	if (ret != 0) {
7829*f64ca102SToomas Soome 		bam_error(_("cannot get zpool status for pool: %s\n"), pool);
7830eb2bd662Svikram 		return (-1);
7831eb2bd662Svikram 	}
7832eb2bd662Svikram 
7833eb2bd662Svikram 	INJECT_ERROR1("ZFS_GET_PHYS_STATUS_OUT", flist.head = NULL);
7834eb2bd662Svikram 	if (flist.head == NULL) {
7835*f64ca102SToomas Soome 		bam_error(_("bad zpool status for pool=%s\n"), pool);
7836eb2bd662Svikram 		filelist_free(&flist);
7837eb2bd662Svikram 		return (-1);
7838eb2bd662Svikram 	}
7839eb2bd662Svikram 
7840eb2bd662Svikram 	for (lp = flist.head; lp; lp = lp->next) {
7841*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: strtok() zpool status line=%s\n",
7842*f64ca102SToomas Soome 		    fcn, lp->line));
7843eb2bd662Svikram 		comp1 = strtok(lp->line, " \t");
7844eb2bd662Svikram 		if (comp1 == NULL) {
7845eb2bd662Svikram 			free(lp->line);
7846eb2bd662Svikram 			lp->line = NULL;
7847eb2bd662Svikram 		} else {
7848eb2bd662Svikram 			comp1 = s_strdup(comp1);
7849eb2bd662Svikram 			free(lp->line);
7850eb2bd662Svikram 			lp->line = comp1;
7851eb2bd662Svikram 		}
7852eb2bd662Svikram 	}
7853eb2bd662Svikram 
7854eb2bd662Svikram 	for (lp = flist.head; lp; lp = lp->next) {
7855eb2bd662Svikram 		if (lp->line == NULL)
7856eb2bd662Svikram 			continue;
7857eb2bd662Svikram 		if (strcmp(lp->line, pool) == 0) {
7858*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: found pool name: %s in zpool "
7859*f64ca102SToomas Soome 			    "status\n", fcn, pool));
7860eb2bd662Svikram 			break;
7861eb2bd662Svikram 		}
7862eb2bd662Svikram 	}
7863eb2bd662Svikram 
7864eb2bd662Svikram 	if (lp == NULL) {
7865*f64ca102SToomas Soome 		bam_error(_("no pool name %s in zpool status\n"), pool);
7866eb2bd662Svikram 		filelist_free(&flist);
7867eb2bd662Svikram 		return (-1);
7868eb2bd662Svikram 	}
7869eb2bd662Svikram 
7870eb2bd662Svikram 	startlp = lp->next;
7871eb2bd662Svikram 	for (i = 0, lp = startlp; lp; lp = lp->next) {
7872eb2bd662Svikram 		if (lp->line == NULL)
7873eb2bd662Svikram 			continue;
7874eb2bd662Svikram 		if (strcmp(lp->line, "mirror") == 0)
7875eb2bd662Svikram 			continue;
7876eb2bd662Svikram 		if (lp->line[0] == '\0' || strcmp(lp->line, "errors:") == 0)
7877eb2bd662Svikram 			break;
7878eb2bd662Svikram 		i++;
7879*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: counting phys slices in zpool status: %d\n",
7880*f64ca102SToomas Soome 		    fcn, i));
7881eb2bd662Svikram 	}
7882eb2bd662Svikram 
7883eb2bd662Svikram 	if (i == 0) {
7884*f64ca102SToomas Soome 		bam_error(_("no physical device in zpool status for pool=%s\n"),
7885*f64ca102SToomas Soome 		    pool);
7886eb2bd662Svikram 		filelist_free(&flist);
7887eb2bd662Svikram 		return (-1);
7888eb2bd662Svikram 	}
7889eb2bd662Svikram 
7890eb2bd662Svikram 	*n = i;
7891eb2bd662Svikram 	*physarray = s_calloc(*n, sizeof (char *));
7892eb2bd662Svikram 	for (i = 0, lp = startlp; lp; lp = lp->next) {
7893eb2bd662Svikram 		if (lp->line == NULL)
7894eb2bd662Svikram 			continue;
7895eb2bd662Svikram 		if (strcmp(lp->line, "mirror") == 0)
7896eb2bd662Svikram 			continue;
7897eb2bd662Svikram 		if (strcmp(lp->line, "errors:") == 0)
7898eb2bd662Svikram 			break;
7899eb2bd662Svikram 		if (strncmp(lp->line, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
7900eb2bd662Svikram 		    strncmp(lp->line, "/dev/rdsk/",
7901eb2bd662Svikram 		    strlen("/dev/rdsk/")) != 0)  {
7902e998e519SSheshadri Vasudevan 			(void) snprintf(dsk, sizeof (dsk), "/dev/rdsk/%s",
7903eb2bd662Svikram 			    lp->line);
7904eb2bd662Svikram 		} else {
7905eb2bd662Svikram 			(void) strlcpy(dsk, lp->line, sizeof (dsk));
7906eb2bd662Svikram 		}
7907*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: adding phys slice=%s from pool %s status\n",
7908*f64ca102SToomas Soome 		    fcn, dsk, pool));
7909eb2bd662Svikram 		(*physarray)[i++] = s_strdup(dsk);
7910eb2bd662Svikram 	}
7911eb2bd662Svikram 
7912eb2bd662Svikram 	assert(i == *n);
7913eb2bd662Svikram 
7914eb2bd662Svikram 	filelist_free(&flist);
7915eb2bd662Svikram 
7916*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7917eb2bd662Svikram 	return (0);
7918eb2bd662Svikram }
7919eb2bd662Svikram 
7920eb2bd662Svikram static int
7921eb2bd662Svikram get_physical(char *menu_root, char ***physarray, int *n)
7922eb2bd662Svikram {
7923eb2bd662Svikram 	char			*special;
7924eb2bd662Svikram 	int			ret;
7925eb2bd662Svikram 	const char		*fcn = "get_physical()";
7926eb2bd662Svikram 
7927eb2bd662Svikram 	assert(menu_root);
7928eb2bd662Svikram 	assert(physarray);
7929eb2bd662Svikram 	assert(n);
7930eb2bd662Svikram 
7931eb2bd662Svikram 	*physarray = NULL;
7932eb2bd662Svikram 	*n = 0;
7933eb2bd662Svikram 
7934*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. arg: %s\n", fcn, menu_root));
7935eb2bd662Svikram 
7936eb2bd662Svikram 	/* First get the device special file from /etc/mnttab */
7937eb2bd662Svikram 	special = get_special(menu_root);
7938eb2bd662Svikram 	INJECT_ERROR1("GET_PHYSICAL_SPECIAL", special = NULL);
7939eb2bd662Svikram 	if (special == NULL) {
7940*f64ca102SToomas Soome 		bam_error(_("cannot get special file for mount-point: %s\n"),
7941*f64ca102SToomas Soome 		    menu_root);
7942eb2bd662Svikram 		return (-1);
7943eb2bd662Svikram 	}
7944eb2bd662Svikram 
7945eb2bd662Svikram 	/* If already a physical device nothing to do */
7946eb2bd662Svikram 	if (strncmp(special, "/dev/dsk/", strlen("/dev/dsk/")) == 0 ||
7947eb2bd662Svikram 	    strncmp(special, "/dev/rdsk/", strlen("/dev/rdsk/")) == 0) {
7948*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: got physical device already directly for "
7949*f64ca102SToomas Soome 		    "menu_root=%s special=%s\n", fcn, menu_root, special));
7950*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7951eb2bd662Svikram 		*physarray = s_calloc(1, sizeof (char *));
7952eb2bd662Svikram 		(*physarray)[0] = special;
7953eb2bd662Svikram 		*n = 1;
7954eb2bd662Svikram 		return (0);
7955eb2bd662Svikram 	}
7956eb2bd662Svikram 
7957eb2bd662Svikram 	if (is_zfs(menu_root)) {
7958eb2bd662Svikram 		ret = zfs_get_physical(special, physarray, n);
7959eb2bd662Svikram 	} else {
7960*f64ca102SToomas Soome 		bam_error(_("cannot derive physical device for %s (%s), "
7961*f64ca102SToomas Soome 		    "unsupported filesystem\n"), menu_root, special);
7962eb2bd662Svikram 		ret = -1;
7963eb2bd662Svikram 	}
7964eb2bd662Svikram 
7965eb2bd662Svikram 	free(special);
7966eb2bd662Svikram 
7967eb2bd662Svikram 	INJECT_ERROR1("GET_PHYSICAL_RET", ret = -1);
7968eb2bd662Svikram 	if (ret == -1) {
7969*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
7970eb2bd662Svikram 	} else {
7971eb2bd662Svikram 		int	i;
7972eb2bd662Svikram 		assert (*n > 0);
7973eb2bd662Svikram 		for (i = 0; i < *n; i++) {
7974*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: returning physical=%s\n",
7975*f64ca102SToomas Soome 			    fcn, (*physarray)[i]));
7976eb2bd662Svikram 		}
7977eb2bd662Svikram 	}
7978eb2bd662Svikram 
7979eb2bd662Svikram 	return (ret);
7980eb2bd662Svikram }
7981eb2bd662Svikram 
7982eb2bd662Svikram static int
7983eb2bd662Svikram is_bootdisk(char *osroot, char *physical)
7984eb2bd662Svikram {
7985eb2bd662Svikram 	int			ret;
7986eb2bd662Svikram 	char			*grubroot;
7987eb2bd662Svikram 	char			*bootp;
7988eb2bd662Svikram 	const char		*fcn = "is_bootdisk()";
7989eb2bd662Svikram 
7990eb2bd662Svikram 	assert(osroot);
7991eb2bd662Svikram 	assert(physical);
7992eb2bd662Svikram 
7993*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osroot, physical));
7994eb2bd662Svikram 
7995eb2bd662Svikram 	bootp = strstr(physical, "p0:boot");
7996eb2bd662Svikram 	if (bootp)
7997eb2bd662Svikram 		*bootp = '\0';
7998eb2bd662Svikram 	/*
7999eb2bd662Svikram 	 * We just want the BIOS mapping for menu disk.
8000eb2bd662Svikram 	 * Don't pass menu_root to get_grubroot() as the
8001eb2bd662Svikram 	 * check that it is used for is not relevant here.
8002eb2bd662Svikram 	 * The osroot is immaterial as well - it is only used to
8003eb2bd662Svikram 	 * to find create_diskmap script. Everything hinges on
8004eb2bd662Svikram 	 * "physical"
8005eb2bd662Svikram 	 */
8006eb2bd662Svikram 	grubroot = get_grubroot(osroot, physical, NULL);
8007eb2bd662Svikram 
8008eb2bd662Svikram 	INJECT_ERROR1("IS_BOOTDISK_GRUBROOT", grubroot = NULL);
8009eb2bd662Svikram 	if (grubroot == NULL) {
801037eb779cSVikram Hegde 		if (bam_verbose)
8011*f64ca102SToomas Soome 			bam_error(_("cannot determine BIOS disk ID 'hd?' for "
8012*f64ca102SToomas Soome 			    "disk: %s\n"), physical);
8013eb2bd662Svikram 		return (0);
8014eb2bd662Svikram 	}
8015eb2bd662Svikram 	ret = grubroot[3] == '0';
8016eb2bd662Svikram 	free(grubroot);
8017eb2bd662Svikram 
8018*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning ret = %d\n", fcn, ret));
8019eb2bd662Svikram 
8020eb2bd662Svikram 	return (ret);
8021eb2bd662Svikram }
8022eb2bd662Svikram 
8023eb2bd662Svikram /*
8024eb2bd662Svikram  * Check if menu is on the boot device
80257c478bd9Sstevel@tonic-gate  * Return 0 (false) on error
80267c478bd9Sstevel@tonic-gate  */
80277c478bd9Sstevel@tonic-gate static int
8028eb2bd662Svikram menu_on_bootdisk(char *osroot, char *menu_root)
80297c478bd9Sstevel@tonic-gate {
8030eb2bd662Svikram 	char		**physarray;
80317c478bd9Sstevel@tonic-gate 	int		ret;
8032eb2bd662Svikram 	int		n;
8033eb2bd662Svikram 	int		i;
8034eb2bd662Svikram 	int		on_bootdisk;
8035eb2bd662Svikram 	const char	*fcn = "menu_on_bootdisk()";
80367c478bd9Sstevel@tonic-gate 
8037*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osroot, menu_root));
80387c478bd9Sstevel@tonic-gate 
8039eb2bd662Svikram 	ret = get_physical(menu_root, &physarray, &n);
8040eb2bd662Svikram 	INJECT_ERROR1("MENU_ON_BOOTDISK_PHYSICAL", ret = -1);
8041eb2bd662Svikram 	if (ret != 0) {
8042*f64ca102SToomas Soome 		bam_error(_("cannot get physical device special file for menu "
8043*f64ca102SToomas Soome 		    "root: %s\n"), menu_root);
80447c478bd9Sstevel@tonic-gate 		return (0);
8045eb2bd662Svikram 	}
8046eb2bd662Svikram 
8047eb2bd662Svikram 	assert(physarray);
8048eb2bd662Svikram 	assert(n > 0);
8049eb2bd662Svikram 
8050eb2bd662Svikram 	on_bootdisk = 0;
8051eb2bd662Svikram 	for (i = 0; i < n; i++) {
8052eb2bd662Svikram 		assert(strncmp(physarray[i], "/dev/dsk/",
8053eb2bd662Svikram 		    strlen("/dev/dsk/")) == 0 ||
8054eb2bd662Svikram 		    strncmp(physarray[i], "/dev/rdsk/",
8055eb2bd662Svikram 		    strlen("/dev/rdsk/")) == 0);
8056eb2bd662Svikram 
8057*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: checking if phys-device=%s is on bootdisk\n",
8058*f64ca102SToomas Soome 		    fcn, physarray[i]));
8059eb2bd662Svikram 		if (is_bootdisk(osroot, physarray[i])) {
8060eb2bd662Svikram 			on_bootdisk = 1;
8061*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: phys-device=%s *IS* on bootdisk\n",
8062*f64ca102SToomas Soome 			    fcn, physarray[i]));
8063eb2bd662Svikram 		}
8064eb2bd662Svikram 	}
8065eb2bd662Svikram 
8066eb2bd662Svikram 	free_physarray(physarray, n);
8067eb2bd662Svikram 
8068eb2bd662Svikram 	INJECT_ERROR1("ON_BOOTDISK_YES", on_bootdisk = 1);
8069eb2bd662Svikram 	INJECT_ERROR1("ON_BOOTDISK_NO", on_bootdisk = 0);
8070eb2bd662Svikram 	if (on_bootdisk) {
8071*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
8072eb2bd662Svikram 	} else {
8073*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
8074eb2bd662Svikram 	}
8075eb2bd662Svikram 
8076eb2bd662Svikram 	return (on_bootdisk);
8077eb2bd662Svikram }
8078eb2bd662Svikram 
8079eb2bd662Svikram void
8080eb2bd662Svikram bam_add_line(menu_t *mp, entry_t *entry, line_t *prev, line_t *lp)
8081eb2bd662Svikram {
8082eb2bd662Svikram 	const char	*fcn = "bam_add_line()";
8083eb2bd662Svikram 
8084eb2bd662Svikram 	assert(mp);
8085eb2bd662Svikram 	assert(entry);
8086eb2bd662Svikram 	assert(prev);
8087eb2bd662Svikram 	assert(lp);
8088eb2bd662Svikram 
8089eb2bd662Svikram 	lp->next = prev->next;
8090eb2bd662Svikram 	if (prev->next) {
8091*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: previous next exists\n", fcn));
8092eb2bd662Svikram 		prev->next->prev = lp;
8093eb2bd662Svikram 	} else {
8094*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: previous next does not exist\n", fcn));
8095eb2bd662Svikram 	}
8096eb2bd662Svikram 	prev->next = lp;
8097eb2bd662Svikram 	lp->prev = prev;
8098eb2bd662Svikram 
8099eb2bd662Svikram 	if (entry->end == prev) {
8100*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: last line in entry\n", fcn));
8101eb2bd662Svikram 		entry->end = lp;
8102eb2bd662Svikram 	}
8103eb2bd662Svikram 	if (mp->end == prev) {
8104eb2bd662Svikram 		assert(lp->next == NULL);
8105eb2bd662Svikram 		mp->end = lp;
8106*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: last line in menu\n", fcn));
8107eb2bd662Svikram 	}
81087c478bd9Sstevel@tonic-gate }
81097c478bd9Sstevel@tonic-gate 
81108c1b6884Sszhou /*
81118c1b6884Sszhou  * look for matching bootadm entry with specified parameters
81128c1b6884Sszhou  * Here are the rules (based on existing usage):
81138c1b6884Sszhou  * - If title is specified, match on title only
8114eb2bd662Svikram  * - Else, match on root/findroot, kernel, and module.
8115eb2bd662Svikram  *   Note that, if root_opt is non-zero, the absence of
8116eb2bd662Svikram  *   root line is considered a match.
81178c1b6884Sszhou  */
81188c1b6884Sszhou static entry_t *
8119eb2bd662Svikram find_boot_entry(
8120eb2bd662Svikram 	menu_t *mp,
8121eb2bd662Svikram 	char *title,
8122eb2bd662Svikram 	char *kernel,
8123eb2bd662Svikram 	char *findroot,
8124eb2bd662Svikram 	char *root,
8125eb2bd662Svikram 	char *module,
8126eb2bd662Svikram 	int root_opt,
8127eb2bd662Svikram 	int *entry_num)
81288c1b6884Sszhou {
81298c1b6884Sszhou 	int		i;
81308c1b6884Sszhou 	line_t		*lp;
81318c1b6884Sszhou 	entry_t		*ent;
8132eb2bd662Svikram 	const char	*fcn = "find_boot_entry()";
8133eb2bd662Svikram 
8134eb2bd662Svikram 	if (entry_num)
8135eb2bd662Svikram 		*entry_num = BAM_ERROR;
81368c1b6884Sszhou 
81378c1b6884Sszhou 	/* find matching entry */
81388c1b6884Sszhou 	for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) {
81398c1b6884Sszhou 		lp = ent->start;
81408c1b6884Sszhou 
81418c1b6884Sszhou 		/* first line of entry must be bootadm comment */
81428c1b6884Sszhou 		lp = ent->start;
8143ae115bc7Smrj 		if (lp->flags != BAM_COMMENT ||
8144ae115bc7Smrj 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0) {
81458c1b6884Sszhou 			continue;
81468c1b6884Sszhou 		}
81478c1b6884Sszhou 
81488c1b6884Sszhou 		/* advance to title line */
81498c1b6884Sszhou 		lp = lp->next;
81508c1b6884Sszhou 		if (title) {
81518c1b6884Sszhou 			if (lp->flags == BAM_TITLE && lp->arg &&
8152eb2bd662Svikram 			    strcmp(lp->arg, title) == 0) {
8153*f64ca102SToomas Soome 				BAM_DPRINTF(("%s: matched title: %s\n",
8154*f64ca102SToomas Soome 				    fcn, title));
81558c1b6884Sszhou 				break;
8156eb2bd662Svikram 			}
8157*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: no match title: %s, %s\n",
8158*f64ca102SToomas Soome 			    fcn, title, lp->arg));
81598c1b6884Sszhou 			continue;	/* check title only */
81608c1b6884Sszhou 		}
81618c1b6884Sszhou 
81628c1b6884Sszhou 		lp = lp->next;	/* advance to root line */
8163843e1988Sjohnlev 		if (lp == NULL) {
8164843e1988Sjohnlev 			continue;
8165eac223ccSWilliam Kucharski 		} else if (lp->cmd != NULL &&
8166eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[FINDROOT_CMD]) == 0) {
8167eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_NULL_FINDROOT",
8168eb2bd662Svikram 			    findroot = NULL);
8169eb2bd662Svikram 			if (findroot == NULL) {
8170*f64ca102SToomas Soome 				BAM_DPRINTF(("%s: no match line has findroot, "
8171*f64ca102SToomas Soome 				    "we don't: %s\n", fcn, lp->arg));
81728c1b6884Sszhou 				continue;
81738c1b6884Sszhou 			}
8174eb2bd662Svikram 			/* findroot command found, try match  */
8175eb2bd662Svikram 			if (strcmp(lp->arg, findroot) != 0) {
8176*f64ca102SToomas Soome 				BAM_DPRINTF(("%s: no match findroot: %s, %s\n",
8177eb2bd662Svikram 				    fcn, findroot, lp->arg));
8178eb2bd662Svikram 				continue;
8179eb2bd662Svikram 			}
8180*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: matched findroot: %s\n",
8181*f64ca102SToomas Soome 			    fcn, findroot));
8182eb2bd662Svikram 			lp = lp->next;	/* advance to kernel line */
8183eac223ccSWilliam Kucharski 		} else if (lp->cmd != NULL &&
8184eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
8185eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_NULL_ROOT", root = NULL);
8186eb2bd662Svikram 			if (root == NULL) {
8187*f64ca102SToomas Soome 				BAM_DPRINTF(("%s: no match, line has root, we "
8188*f64ca102SToomas Soome 				    "don't: %s\n", fcn, lp->arg));
8189eb2bd662Svikram 				continue;
8190eb2bd662Svikram 			}
8191eb2bd662Svikram 			/* root cmd found, try match */
8192eb2bd662Svikram 			if (strcmp(lp->arg, root) != 0) {
8193*f64ca102SToomas Soome 				BAM_DPRINTF(("%s: no match root: %s, %s\n",
8194eb2bd662Svikram 				    fcn, root, lp->arg));
8195eb2bd662Svikram 				continue;
8196eb2bd662Svikram 			}
8197*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: matched root: %s\n", fcn, root));
81988c1b6884Sszhou 			lp = lp->next;	/* advance to kernel line */
81998c1b6884Sszhou 		} else {
8200eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_ROOT_OPT_NO",
8201eb2bd662Svikram 			    root_opt = 0);
8202eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_ROOT_OPT_YES",
8203eb2bd662Svikram 			    root_opt = 1);
82048c1b6884Sszhou 			/* no root command, see if root is optional */
82058c1b6884Sszhou 			if (root_opt == 0) {
8206*f64ca102SToomas Soome 				BAM_DPRINTF(("%s: root NOT optional\n", fcn));
82078c1b6884Sszhou 				continue;
82088c1b6884Sszhou 			}
8209*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: root IS optional\n", fcn));
82108c1b6884Sszhou 		}
82118c1b6884Sszhou 
82128c1b6884Sszhou 		if (lp == NULL || lp->next == NULL) {
82138c1b6884Sszhou 			continue;
82148c1b6884Sszhou 		}
82158c1b6884Sszhou 
8216843e1988Sjohnlev 		if (kernel &&
8217843e1988Sjohnlev 		    (!check_cmd(lp->cmd, KERNEL_CMD, lp->arg, kernel))) {
8218bbcc54bdSEnrico Perla - Sun Microsystems 			if (!(ent->flags & BAM_ENTRY_FAILSAFE) ||
8219bbcc54bdSEnrico Perla - Sun Microsystems 			    !(ent->flags & BAM_ENTRY_DBOOT) ||
8220bbcc54bdSEnrico Perla - Sun Microsystems 			    strcmp(kernel, DIRECT_BOOT_FAILSAFE_LINE) != 0)
82218c1b6884Sszhou 				continue;
8222bbcc54bdSEnrico Perla - Sun Microsystems 
8223bbcc54bdSEnrico Perla - Sun Microsystems 			ent->flags |= BAM_ENTRY_UPGFSKERNEL;
8224bbcc54bdSEnrico Perla - Sun Microsystems 
82258c1b6884Sszhou 		}
8226*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: kernel match: %s, %s\n", fcn,
8227*f64ca102SToomas Soome 		    kernel, lp->arg));
8228843e1988Sjohnlev 
8229843e1988Sjohnlev 		/*
8230843e1988Sjohnlev 		 * Check for matching module entry (failsafe or normal).
8231843e1988Sjohnlev 		 * If it fails to match, we go around the loop again.
8232843e1988Sjohnlev 		 * For xpv entries, there are two module lines, so we
8233843e1988Sjohnlev 		 * do the check twice.
8234843e1988Sjohnlev 		 */
8235843e1988Sjohnlev 		lp = lp->next;	/* advance to module line */
8236843e1988Sjohnlev 		if (check_cmd(lp->cmd, MODULE_CMD, lp->arg, module) ||
8237843e1988Sjohnlev 		    (((lp = lp->next) != NULL) &&
8238843e1988Sjohnlev 		    check_cmd(lp->cmd, MODULE_CMD, lp->arg, module))) {
8239843e1988Sjohnlev 			/* match found */
8240*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: module match: %s, %s\n", fcn,
8241*f64ca102SToomas Soome 			    module, lp->arg));
8242843e1988Sjohnlev 			break;
8243843e1988Sjohnlev 		}
8244bbcc54bdSEnrico Perla - Sun Microsystems 
8245bbcc54bdSEnrico Perla - Sun Microsystems 		if (strcmp(module, FAILSAFE_ARCHIVE) == 0 &&
8246bbcc54bdSEnrico Perla - Sun Microsystems 		    (strcmp(lp->prev->arg, FAILSAFE_ARCHIVE_32) == 0 ||
8247bbcc54bdSEnrico Perla - Sun Microsystems 		    strcmp(lp->prev->arg, FAILSAFE_ARCHIVE_64) == 0)) {
8248bbcc54bdSEnrico Perla - Sun Microsystems 			ent->flags |= BAM_ENTRY_UPGFSMODULE;
8249bbcc54bdSEnrico Perla - Sun Microsystems 			break;
8250bbcc54bdSEnrico Perla - Sun Microsystems 		}
8251bbcc54bdSEnrico Perla - Sun Microsystems 
82528c1b6884Sszhou 	}
82538c1b6884Sszhou 
8254eb2bd662Svikram 	if (ent && entry_num) {
82558c1b6884Sszhou 		*entry_num = i;
8256843e1988Sjohnlev 	}
8257eb2bd662Svikram 
8258eb2bd662Svikram 	if (ent) {
8259*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning ret = %d\n", fcn, i));
8260eb2bd662Svikram 	} else {
8261*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning ret = %d\n", fcn, BAM_ERROR));
8262eb2bd662Svikram 	}
82638c1b6884Sszhou 	return (ent);
82648c1b6884Sszhou }
82658c1b6884Sszhou 
82668c1b6884Sszhou static int
8267eb2bd662Svikram update_boot_entry(menu_t *mp, char *title, char *findroot, char *root,
8268eb2bd662Svikram     char *kernel, char *mod_kernel, char *module, int root_opt)
82698c1b6884Sszhou {
8270eb2bd662Svikram 	int		i;
8271eb2bd662Svikram 	int		change_kernel = 0;
82728c1b6884Sszhou 	entry_t		*ent;
82738c1b6884Sszhou 	line_t		*lp;
8274eb2bd662Svikram 	line_t		*tlp;
82758c1b6884Sszhou 	char		linebuf[BAM_MAXLINE];
8276eb2bd662Svikram 	const char	*fcn = "update_boot_entry()";
82778c1b6884Sszhou 
82788c1b6884Sszhou 	/* note: don't match on title, it's updated on upgrade */
8279eb2bd662Svikram 	ent = find_boot_entry(mp, NULL, kernel, findroot, root, module,
8280eb2bd662Svikram 	    root_opt, &i);
8281ae115bc7Smrj 	if ((ent == NULL) && (bam_direct == BAM_DIRECT_DBOOT)) {
8282ae115bc7Smrj 		/*
8283ae115bc7Smrj 		 * We may be upgrading a kernel from multiboot to
8284eb2bd662Svikram 		 * directboot.  Look for a multiboot entry. A multiboot
8285eb2bd662Svikram 		 * entry will not have a findroot line.
8286ae115bc7Smrj 		 */
8287eb2bd662Svikram 		ent = find_boot_entry(mp, NULL, "multiboot", NULL, root,
8288eb2bd662Svikram 		    MULTIBOOT_ARCHIVE, root_opt, &i);
8289ae115bc7Smrj 		if (ent != NULL) {
8290*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: upgrading entry from dboot to "
8291*f64ca102SToomas Soome 			    "multiboot: root = %s\n", fcn, root));
8292ae115bc7Smrj 			change_kernel = 1;
8293ae115bc7Smrj 		}
8294eb2bd662Svikram 	} else if (ent) {
8295*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: found entry with matching findroot: %s\n",
8296*f64ca102SToomas Soome 		    fcn, findroot));
8297ae115bc7Smrj 	}
82988c1b6884Sszhou 
8299eb2bd662Svikram 	if (ent == NULL) {
8300*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: boot entry not found in menu. Creating "
8301*f64ca102SToomas Soome 		    "new entry, findroot = %s\n", fcn, findroot));
8302eb2bd662Svikram 		return (add_boot_entry(mp, title, findroot,
830344da779fSWilliam Kucharski 		    kernel, mod_kernel, module, NULL));
8304eb2bd662Svikram 	}
8305eb2bd662Svikram 
8306eb2bd662Svikram 	/* replace title of existing entry and update findroot line */
83078c1b6884Sszhou 	lp = ent->start;
83088c1b6884Sszhou 	lp = lp->next;	/* title line */
83098c1b6884Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
83108c1b6884Sszhou 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
83118c1b6884Sszhou 	free(lp->arg);
83128c1b6884Sszhou 	free(lp->line);
83138c1b6884Sszhou 	lp->arg = s_strdup(title);
83148c1b6884Sszhou 	lp->line = s_strdup(linebuf);
8315*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: changing title to: %s\n", fcn, title));
83168c1b6884Sszhou 
8317eb2bd662Svikram 	tlp = lp;	/* title line */
83188c1b6884Sszhou 	lp = lp->next;	/* root line */
8319eb2bd662Svikram 
8320eb2bd662Svikram 	/* if no root or findroot command, create a new line_t */
8321eac223ccSWilliam Kucharski 	if ((lp->cmd != NULL) && (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) != 0 &&
8322eac223ccSWilliam Kucharski 	    strcmp(lp->cmd, menu_cmds[FINDROOT_CMD]) != 0)) {
8323eb2bd662Svikram 		lp = s_calloc(1, sizeof (line_t));
8324eb2bd662Svikram 		bam_add_line(mp, ent, tlp, lp);
8325eb2bd662Svikram 	} else {
8326eac223ccSWilliam Kucharski 		if (lp->cmd != NULL)
8327eb2bd662Svikram 			free(lp->cmd);
8328eac223ccSWilliam Kucharski 
8329eb2bd662Svikram 		free(lp->sep);
8330eb2bd662Svikram 		free(lp->arg);
8331eb2bd662Svikram 		free(lp->line);
83328c1b6884Sszhou 	}
8333ae115bc7Smrj 
8334eb2bd662Svikram 	lp->cmd = s_strdup(menu_cmds[FINDROOT_CMD]);
8335eb2bd662Svikram 	lp->sep = s_strdup(menu_cmds[SEP_CMD]);
8336eb2bd662Svikram 	lp->arg = s_strdup(findroot);
8337eb2bd662Svikram 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
8338eb2bd662Svikram 	    menu_cmds[FINDROOT_CMD], menu_cmds[SEP_CMD], findroot);
8339eb2bd662Svikram 	lp->line = s_strdup(linebuf);
8340*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: adding findroot line: %s\n", fcn, findroot));
8341eb2bd662Svikram 
8342eb2bd662Svikram 	/* kernel line */
8343eb2bd662Svikram 	lp = lp->next;
8344eb2bd662Svikram 
8345bbcc54bdSEnrico Perla - Sun Microsystems 	if (ent->flags & BAM_ENTRY_UPGFSKERNEL) {
8346bbcc54bdSEnrico Perla - Sun Microsystems 		char		*params = NULL;
8347bbcc54bdSEnrico Perla - Sun Microsystems 
8348bbcc54bdSEnrico Perla - Sun Microsystems 		params = strstr(lp->line, "-s");
8349bbcc54bdSEnrico Perla - Sun Microsystems 		if (params != NULL)
8350bbcc54bdSEnrico Perla - Sun Microsystems 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s%s",
8351bbcc54bdSEnrico Perla - Sun Microsystems 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
8352bbcc54bdSEnrico Perla - Sun Microsystems 			    kernel, params+2);
8353bbcc54bdSEnrico Perla - Sun Microsystems 		else
8354bbcc54bdSEnrico Perla - Sun Microsystems 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
8355bbcc54bdSEnrico Perla - Sun Microsystems 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
8356bbcc54bdSEnrico Perla - Sun Microsystems 			    kernel);
8357bbcc54bdSEnrico Perla - Sun Microsystems 
8358eac223ccSWilliam Kucharski 		if (lp->cmd != NULL)
8359bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->cmd);
8360eac223ccSWilliam Kucharski 
8361bbcc54bdSEnrico Perla - Sun Microsystems 		free(lp->arg);
8362bbcc54bdSEnrico Perla - Sun Microsystems 		free(lp->line);
8363bbcc54bdSEnrico Perla - Sun Microsystems 		lp->cmd = s_strdup(menu_cmds[KERNEL_DOLLAR_CMD]);
8364bbcc54bdSEnrico Perla - Sun Microsystems 		lp->arg = s_strdup(strstr(linebuf, "/"));
8365bbcc54bdSEnrico Perla - Sun Microsystems 		lp->line = s_strdup(linebuf);
8366bbcc54bdSEnrico Perla - Sun Microsystems 		ent->flags &= ~BAM_ENTRY_UPGFSKERNEL;
8367*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: adding new kernel$ line: %s\n",
8368*f64ca102SToomas Soome 		    fcn, lp->prev->cmd));
8369bbcc54bdSEnrico Perla - Sun Microsystems 	}
8370bbcc54bdSEnrico Perla - Sun Microsystems 
8371ae115bc7Smrj 	if (change_kernel) {
8372ae115bc7Smrj 		/*
8373ae115bc7Smrj 		 * We're upgrading from multiboot to directboot.
8374ae115bc7Smrj 		 */
8375eac223ccSWilliam Kucharski 		if (lp->cmd != NULL &&
8376eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[KERNEL_CMD]) == 0) {
8377ae115bc7Smrj 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
8378ae115bc7Smrj 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
8379ae115bc7Smrj 			    kernel);
8380eb2bd662Svikram 			free(lp->cmd);
8381ae115bc7Smrj 			free(lp->arg);
8382ae115bc7Smrj 			free(lp->line);
8383eb2bd662Svikram 			lp->cmd = s_strdup(menu_cmds[KERNEL_DOLLAR_CMD]);
8384ae115bc7Smrj 			lp->arg = s_strdup(kernel);
8385ae115bc7Smrj 			lp->line = s_strdup(linebuf);
8386ae115bc7Smrj 			lp = lp->next;
8387*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: adding new kernel$ line: %s\n",
8388*f64ca102SToomas Soome 			    fcn, kernel));
8389ae115bc7Smrj 		}
8390eac223ccSWilliam Kucharski 		if (lp->cmd != NULL &&
8391eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) {
8392ae115bc7Smrj 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
8393ae115bc7Smrj 			    menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD],
8394ae115bc7Smrj 			    module);
8395eb2bd662Svikram 			free(lp->cmd);
8396ae115bc7Smrj 			free(lp->arg);
8397ae115bc7Smrj 			free(lp->line);
8398eb2bd662Svikram 			lp->cmd = s_strdup(menu_cmds[MODULE_DOLLAR_CMD]);
8399ae115bc7Smrj 			lp->arg = s_strdup(module);
8400ae115bc7Smrj 			lp->line = s_strdup(linebuf);
8401ae115bc7Smrj 			lp = lp->next;
8402*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: adding new module$ line: %s\n",
8403*f64ca102SToomas Soome 			    fcn, module));
8404ae115bc7Smrj 		}
8405ae115bc7Smrj 	}
8406bbcc54bdSEnrico Perla - Sun Microsystems 
8407bbcc54bdSEnrico Perla - Sun Microsystems 	/* module line */
8408bbcc54bdSEnrico Perla - Sun Microsystems 	lp = lp->next;
8409bbcc54bdSEnrico Perla - Sun Microsystems 
8410bbcc54bdSEnrico Perla - Sun Microsystems 	if (ent->flags & BAM_ENTRY_UPGFSMODULE) {
8411eac223ccSWilliam Kucharski 		if (lp->cmd != NULL &&
8412eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) {
8413bbcc54bdSEnrico Perla - Sun Microsystems 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
8414bbcc54bdSEnrico Perla - Sun Microsystems 			    menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD],
8415bbcc54bdSEnrico Perla - Sun Microsystems 			    module);
8416bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->cmd);
8417bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->arg);
8418bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->line);
8419bbcc54bdSEnrico Perla - Sun Microsystems 			lp->cmd = s_strdup(menu_cmds[MODULE_DOLLAR_CMD]);
8420bbcc54bdSEnrico Perla - Sun Microsystems 			lp->arg = s_strdup(module);
8421bbcc54bdSEnrico Perla - Sun Microsystems 			lp->line = s_strdup(linebuf);
8422bbcc54bdSEnrico Perla - Sun Microsystems 			lp = lp->next;
8423bbcc54bdSEnrico Perla - Sun Microsystems 			ent->flags &= ~BAM_ENTRY_UPGFSMODULE;
8424*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: adding new module$ line: %s\n",
8425*f64ca102SToomas Soome 			    fcn, module));
8426bbcc54bdSEnrico Perla - Sun Microsystems 		}
8427bbcc54bdSEnrico Perla - Sun Microsystems 	}
8428bbcc54bdSEnrico Perla - Sun Microsystems 
8429*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning ret = %d\n", fcn, i));
84308c1b6884Sszhou 	return (i);
84318c1b6884Sszhou }
84328c1b6884Sszhou 
8433eb2bd662Svikram int
8434eb2bd662Svikram root_optional(char *osroot, char *menu_root)
8435eb2bd662Svikram {
8436eb2bd662Svikram 	char			*ospecial;
8437eb2bd662Svikram 	char			*mspecial;
8438eb2bd662Svikram 	char			*slash;
8439eb2bd662Svikram 	int			root_opt;
8440eb2bd662Svikram 	int			ret1;
8441eb2bd662Svikram 	int			ret2;
8442eb2bd662Svikram 	const char		*fcn = "root_optional()";
8443eb2bd662Svikram 
8444*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osroot, menu_root));
8445eb2bd662Svikram 
8446eb2bd662Svikram 	/*
8447eb2bd662Svikram 	 * For all filesystems except ZFS, a straight compare of osroot
8448eb2bd662Svikram 	 * and menu_root will tell us if root is optional.
8449eb2bd662Svikram 	 * For ZFS, the situation is complicated by the fact that
8450eb2bd662Svikram 	 * menu_root and osroot are always different
8451eb2bd662Svikram 	 */
8452eb2bd662Svikram 	ret1 = is_zfs(osroot);
8453eb2bd662Svikram 	ret2 = is_zfs(menu_root);
8454eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPT_NOT_ZFS", ret1 = 0);
8455eb2bd662Svikram 	if (!ret1 || !ret2) {
8456*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: one or more non-ZFS filesystems (%s, %s)\n",
8457*f64ca102SToomas Soome 		    fcn, osroot, menu_root));
8458eb2bd662Svikram 		root_opt = (strcmp(osroot, menu_root) == 0);
8459eb2bd662Svikram 		goto out;
8460eb2bd662Svikram 	}
8461eb2bd662Svikram 
8462eb2bd662Svikram 	ospecial = get_special(osroot);
8463eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_OSPECIAL", ospecial = NULL);
8464eb2bd662Svikram 	if (ospecial == NULL) {
8465*f64ca102SToomas Soome 		bam_error(_("failed to get special file for osroot: %s\n"),
8466*f64ca102SToomas Soome 		    osroot);
8467eb2bd662Svikram 		return (0);
8468eb2bd662Svikram 	}
8469*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: ospecial=%s for osroot=%s\n", fcn, ospecial, osroot));
8470eb2bd662Svikram 
8471eb2bd662Svikram 	mspecial = get_special(menu_root);
8472eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_MSPECIAL", mspecial = NULL);
8473eb2bd662Svikram 	if (mspecial == NULL) {
8474*f64ca102SToomas Soome 		bam_error(_("failed to get special file for menu_root: %s\n"),
8475*f64ca102SToomas Soome 		    menu_root);
8476eb2bd662Svikram 		free(ospecial);
8477eb2bd662Svikram 		return (0);
8478eb2bd662Svikram 	}
8479*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: mspecial=%s for menu_root=%s\n",
8480*f64ca102SToomas Soome 	    fcn, mspecial, menu_root));
8481eb2bd662Svikram 
8482eb2bd662Svikram 	slash = strchr(ospecial, '/');
8483eb2bd662Svikram 	if (slash)
8484eb2bd662Svikram 		*slash = '\0';
8485*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: FIXED ospecial=%s for osroot=%s\n",
8486*f64ca102SToomas Soome 	    fcn, ospecial, osroot));
8487eb2bd662Svikram 
8488eb2bd662Svikram 	root_opt = (strcmp(ospecial, mspecial) == 0);
8489eb2bd662Svikram 
8490eb2bd662Svikram 	free(ospecial);
8491eb2bd662Svikram 	free(mspecial);
8492eb2bd662Svikram 
8493eb2bd662Svikram out:
8494eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_NO", root_opt = 0);
8495eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_YES", root_opt = 1);
8496eb2bd662Svikram 	if (root_opt) {
8497*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
8498eb2bd662Svikram 	} else {
8499*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
8500eb2bd662Svikram 	}
8501eb2bd662Svikram 
8502eb2bd662Svikram 	return (root_opt);
8503eb2bd662Svikram }
8504eb2bd662Svikram 
85057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
85067c478bd9Sstevel@tonic-gate static error_t
8507eb2bd662Svikram update_entry(menu_t *mp, char *menu_root, char *osdev)
85087c478bd9Sstevel@tonic-gate {
85097c478bd9Sstevel@tonic-gate 	int		entry;
8510eb2bd662Svikram 	char		*grubsign;
8511eb2bd662Svikram 	char		*grubroot;
8512eb2bd662Svikram 	char		*title;
8513eb2bd662Svikram 	char		osroot[PATH_MAX];
8514eb2bd662Svikram 	char		*failsafe_kernel = NULL;
85158c1b6884Sszhou 	struct stat	sbuf;
85168c1b6884Sszhou 	char		failsafe[256];
8517bbcc54bdSEnrico Perla - Sun Microsystems 	char		failsafe_64[256];
8518eb2bd662Svikram 	int		ret;
8519eb2bd662Svikram 	const char	*fcn = "update_entry()";
85207c478bd9Sstevel@tonic-gate 
85217c478bd9Sstevel@tonic-gate 	assert(mp);
8522eb2bd662Svikram 	assert(menu_root);
8523eb2bd662Svikram 	assert(osdev);
8524eb2bd662Svikram 	assert(bam_root);
85257c478bd9Sstevel@tonic-gate 
8526*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s %s\n", fcn, menu_root, osdev,
8527*f64ca102SToomas Soome 	    bam_root));
8528eb2bd662Svikram 
8529eb2bd662Svikram 	(void) strlcpy(osroot, bam_root, sizeof (osroot));
8530eb2bd662Svikram 
85317c478bd9Sstevel@tonic-gate 	title = get_title(osroot);
8532eb2bd662Svikram 	assert(title);
85337c478bd9Sstevel@tonic-gate 
8534eb2bd662Svikram 	grubsign = get_grubsign(osroot, osdev);
8535eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBSIGN_FAIL", grubsign = NULL);
8536eb2bd662Svikram 	if (grubsign == NULL) {
8537*f64ca102SToomas Soome 		bam_error(_("failed to get grubsign for root: %s, device %s\n"),
8538*f64ca102SToomas Soome 		    osroot, osdev);
85397c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
85407c478bd9Sstevel@tonic-gate 	}
8541eb2bd662Svikram 
8542eb2bd662Svikram 	/*
8543eb2bd662Svikram 	 * It is not a fatal error if get_grubroot() fails
8544eb2bd662Svikram 	 * We no longer rely on biosdev to populate the
8545eb2bd662Svikram 	 * menu
8546eb2bd662Svikram 	 */
8547eb2bd662Svikram 	grubroot = get_grubroot(osroot, osdev, menu_root);
8548eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBROOT_FAIL", grubroot = NULL);
8549eb2bd662Svikram 	if (grubroot) {
8550*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: get_grubroot success. osroot=%s, osdev=%s, "
8551*f64ca102SToomas Soome 		    "menu_root=%s\n", fcn, osroot, osdev, menu_root));
8552eb2bd662Svikram 	} else {
8553*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: get_grubroot failed. osroot=%s, osdev=%s, "
8554*f64ca102SToomas Soome 		    "menu_root=%s\n", fcn, osroot, osdev, menu_root));
85557c478bd9Sstevel@tonic-gate 	}
85567c478bd9Sstevel@tonic-gate 
85577c478bd9Sstevel@tonic-gate 	/* add the entry for normal Solaris */
8558eb2bd662Svikram 	INJECT_ERROR1("UPDATE_ENTRY_MULTIBOOT",
8559eb2bd662Svikram 	    bam_direct = BAM_DIRECT_MULTIBOOT);
8560ae115bc7Smrj 	if (bam_direct == BAM_DIRECT_DBOOT) {
8561eb2bd662Svikram 		entry = update_boot_entry(mp, title, grubsign, grubroot,
8562e7cbe64fSgw25295 		    (bam_zfs ? DIRECT_BOOT_KERNEL_ZFS : DIRECT_BOOT_KERNEL),
8563eb2bd662Svikram 		    NULL, DIRECT_BOOT_ARCHIVE,
8564eb2bd662Svikram 		    root_optional(osroot, menu_root));
8565*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: updated boot entry bam_zfs=%d, "
8566*f64ca102SToomas Soome 		    "grubsign = %s\n", fcn, bam_zfs, grubsign));
8567843e1988Sjohnlev 		if ((entry != BAM_ERROR) && (bam_is_hv == BAM_HV_PRESENT)) {
8568eb2bd662Svikram 			(void) update_boot_entry(mp, NEW_HV_ENTRY, grubsign,
8569eb2bd662Svikram 			    grubroot, XEN_MENU, bam_zfs ?
8570eb2bd662Svikram 			    XEN_KERNEL_MODULE_LINE_ZFS : XEN_KERNEL_MODULE_LINE,
8571eb2bd662Svikram 			    DIRECT_BOOT_ARCHIVE,
8572eb2bd662Svikram 			    root_optional(osroot, menu_root));
8573*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: updated HV entry bam_zfs=%d, "
8574*f64ca102SToomas Soome 			    "grubsign = %s\n", fcn, bam_zfs, grubsign));
8575ae115bc7Smrj 		}
8576843e1988Sjohnlev 	} else {
8577eb2bd662Svikram 		entry = update_boot_entry(mp, title, grubsign, grubroot,
8578eb2bd662Svikram 		    MULTI_BOOT, NULL, MULTIBOOT_ARCHIVE,
8579eb2bd662Svikram 		    root_optional(osroot, menu_root));
8580eb2bd662Svikram 
8581*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: updated MULTIBOOT entry grubsign = %s\n",
8582*f64ca102SToomas Soome 		    fcn, grubsign));
8583843e1988Sjohnlev 	}
85847c478bd9Sstevel@tonic-gate 
8585843e1988Sjohnlev 	/*
8586843e1988Sjohnlev 	 * Add the entry for failsafe archive.  On a bfu'd system, the
8587843e1988Sjohnlev 	 * failsafe may be different than the installed kernel.
8588843e1988Sjohnlev 	 */
8589eb2bd662Svikram 	(void) snprintf(failsafe, sizeof (failsafe), "%s%s",
8590bbcc54bdSEnrico Perla - Sun Microsystems 	    osroot, FAILSAFE_ARCHIVE_32);
8591bbcc54bdSEnrico Perla - Sun Microsystems 	(void) snprintf(failsafe_64, sizeof (failsafe_64), "%s%s",
8592bbcc54bdSEnrico Perla - Sun Microsystems 	    osroot, FAILSAFE_ARCHIVE_64);
8593bbcc54bdSEnrico Perla - Sun Microsystems 
8594bbcc54bdSEnrico Perla - Sun Microsystems 	/*
8595bbcc54bdSEnrico Perla - Sun Microsystems 	 * Check if at least one of the two archives exists
8596bbcc54bdSEnrico Perla - Sun Microsystems 	 * Using $ISADIR as the default line, we have an entry which works
8597bbcc54bdSEnrico Perla - Sun Microsystems 	 * for both the cases.
8598bbcc54bdSEnrico Perla - Sun Microsystems 	 */
8599bbcc54bdSEnrico Perla - Sun Microsystems 
8600bbcc54bdSEnrico Perla - Sun Microsystems 	if (stat(failsafe, &sbuf) == 0 || stat(failsafe_64, &sbuf) == 0) {
860160d0a590Srscott 
860260d0a590Srscott 		/* Figure out where the kernel line should point */
860360d0a590Srscott 		(void) snprintf(failsafe, sizeof (failsafe), "%s%s", osroot,
8604bbcc54bdSEnrico Perla - Sun Microsystems 		    DIRECT_BOOT_FAILSAFE_32);
8605bbcc54bdSEnrico Perla - Sun Microsystems 		(void) snprintf(failsafe_64, sizeof (failsafe_64), "%s%s",
8606bbcc54bdSEnrico Perla - Sun Microsystems 		    osroot, DIRECT_BOOT_FAILSAFE_64);
8607bbcc54bdSEnrico Perla - Sun Microsystems 		if (stat(failsafe, &sbuf) == 0 ||
8608bbcc54bdSEnrico Perla - Sun Microsystems 		    stat(failsafe_64, &sbuf) == 0) {
8609963390b4Svikram 			failsafe_kernel = DIRECT_BOOT_FAILSAFE_LINE;
861060d0a590Srscott 		} else {
861160d0a590Srscott 			(void) snprintf(failsafe, sizeof (failsafe), "%s%s",
861260d0a590Srscott 			    osroot, MULTI_BOOT_FAILSAFE);
861360d0a590Srscott 			if (stat(failsafe, &sbuf) == 0) {
861460d0a590Srscott 				failsafe_kernel = MULTI_BOOT_FAILSAFE_LINE;
861560d0a590Srscott 			}
861660d0a590Srscott 		}
861760d0a590Srscott 		if (failsafe_kernel != NULL) {
8618eb2bd662Svikram 			(void) update_boot_entry(mp, FAILSAFE_TITLE, grubsign,
8619eb2bd662Svikram 			    grubroot, failsafe_kernel, NULL, FAILSAFE_ARCHIVE,
8620eb2bd662Svikram 			    root_optional(osroot, menu_root));
8621*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: updated FAILSAFE entry "
8622*f64ca102SToomas Soome 			    "failsafe_kernel = %s\n", fcn, failsafe_kernel));
862360d0a590Srscott 		}
8624ae115bc7Smrj 	}
8625eb2bd662Svikram 	free(grubroot);
86267c478bd9Sstevel@tonic-gate 
8627eb2bd662Svikram 	INJECT_ERROR1("UPDATE_ENTRY_ERROR", entry = BAM_ERROR);
86287c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
8629*f64ca102SToomas Soome 		bam_error(_("failed to add boot entry with title=%s, grub "
8630*f64ca102SToomas Soome 		    "signature=%s\n"), title, grubsign);
8631eb2bd662Svikram 		free(grubsign);
86327c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
86337c478bd9Sstevel@tonic-gate 	}
8634eb2bd662Svikram 	free(grubsign);
8635eb2bd662Svikram 
8636eb2bd662Svikram 	update_numbering(mp);
8637eb2bd662Svikram 	ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
8638eb2bd662Svikram 	INJECT_ERROR1("SET_DEFAULT_ERROR", ret = BAM_ERROR);
8639eb2bd662Svikram 	if (ret == BAM_ERROR) {
8640*f64ca102SToomas Soome 		bam_error(_("failed to set GRUB menu default to %d\n"), entry);
8641eb2bd662Svikram 	}
8642*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
86437c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
86447c478bd9Sstevel@tonic-gate }
86457c478bd9Sstevel@tonic-gate 
86468c1b6884Sszhou static void
8647ae115bc7Smrj save_default_entry(menu_t *mp, const char *which)
86488c1b6884Sszhou {
8649eb2bd662Svikram 	int		lineNum;
8650eb2bd662Svikram 	int		entryNum;
86518c1b6884Sszhou 	int		entry = 0;	/* default is 0 */
86528c1b6884Sszhou 	char		linebuf[BAM_MAXLINE];
86538c1b6884Sszhou 	line_t		*lp = mp->curdefault;
8654eb2bd662Svikram 	const char	*fcn = "save_default_entry()";
86558c1b6884Sszhou 
8656bff269d0Svikram 	if (mp->start) {
8657bff269d0Svikram 		lineNum = mp->end->lineNum;
8658bff269d0Svikram 		entryNum = mp->end->entryNum;
8659bff269d0Svikram 	} else {
8660bff269d0Svikram 		lineNum = LINE_INIT;
8661bff269d0Svikram 		entryNum = ENTRY_INIT;
8662bff269d0Svikram 	}
8663bff269d0Svikram 
86648c1b6884Sszhou 	if (lp)
86658c1b6884Sszhou 		entry = s_strtol(lp->arg);
86668c1b6884Sszhou 
8667ae115bc7Smrj 	(void) snprintf(linebuf, sizeof (linebuf), "#%s%d", which, entry);
8668*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: saving default to: %s\n", fcn, linebuf));
86698c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
8670*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: saved default to lineNum=%d, entryNum=%d\n", fcn,
8671*f64ca102SToomas Soome 	    lineNum, entryNum));
86728c1b6884Sszhou }
86738c1b6884Sszhou 
86748c1b6884Sszhou static void
8675ae115bc7Smrj restore_default_entry(menu_t *mp, const char *which, line_t *lp)
86768c1b6884Sszhou {
86778c1b6884Sszhou 	int		entry;
86788c1b6884Sszhou 	char		*str;
8679eb2bd662Svikram 	const char	*fcn = "restore_default_entry()";
86808c1b6884Sszhou 
8681eb2bd662Svikram 	if (lp == NULL) {
8682*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: NULL saved default\n", fcn));
86838c1b6884Sszhou 		return;		/* nothing to restore */
8684eb2bd662Svikram 	}
8685eb2bd662Svikram 
8686*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: saved default string: %s\n", fcn, which));
86878c1b6884Sszhou 
8688ae115bc7Smrj 	str = lp->arg + strlen(which);
86898c1b6884Sszhou 	entry = s_strtol(str);
86908c1b6884Sszhou 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
86918c1b6884Sszhou 
8692*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: restored default to entryNum: %d\n", fcn, entry));
8693eb2bd662Svikram 
86948c1b6884Sszhou 	/* delete saved old default line */
86958c1b6884Sszhou 	unlink_line(mp, lp);
86968c1b6884Sszhou 	line_free(lp);
86978c1b6884Sszhou }
86988c1b6884Sszhou 
86997c478bd9Sstevel@tonic-gate /*
87007c478bd9Sstevel@tonic-gate  * This function is for supporting reboot with args.
87017c478bd9Sstevel@tonic-gate  * The opt value can be:
87027c478bd9Sstevel@tonic-gate  * NULL		delete temp entry, if present
8703eb2bd662Svikram  * entry=<n>	switches default entry to <n>
87047c478bd9Sstevel@tonic-gate  * else		treated as boot-args and setup a temperary menu entry
87057c478bd9Sstevel@tonic-gate  *		and make it the default
8706eb2bd662Svikram  * Note that we are always rebooting the current OS instance
8707eb2bd662Svikram  * so osroot == / always.
87087c478bd9Sstevel@tonic-gate  */
87097c478bd9Sstevel@tonic-gate #define	REBOOT_TITLE	"Solaris_reboot_transient"
87107c478bd9Sstevel@tonic-gate 
87118c1b6884Sszhou /*ARGSUSED*/
87127c478bd9Sstevel@tonic-gate static error_t
8713eb2bd662Svikram update_temp(menu_t *mp, char *dummy, char *opt)
87147c478bd9Sstevel@tonic-gate {
87157c478bd9Sstevel@tonic-gate 	int		entry;
8716eb2bd662Svikram 	char		*osdev;
8717eb2bd662Svikram 	char		*fstype;
8718eb2bd662Svikram 	char		*sign;
8719eb2bd662Svikram 	char		*opt_ptr;
8720eb2bd662Svikram 	char		*path;
8721ae115bc7Smrj 	char		kernbuf[BUFSIZ];
8722ae115bc7Smrj 	char		args_buf[BUFSIZ];
8723eb2bd662Svikram 	char		signbuf[PATH_MAX];
8724eb2bd662Svikram 	int		ret;
8725eb2bd662Svikram 	const char	*fcn = "update_temp()";
87267c478bd9Sstevel@tonic-gate 
87277c478bd9Sstevel@tonic-gate 	assert(mp);
8728eb2bd662Svikram 	assert(dummy == NULL);
8729eb2bd662Svikram 
8730eb2bd662Svikram 	/* opt can be NULL */
8731*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. arg: %s\n", fcn, opt ? opt : "<NULL>"));
8732*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: bam_alt_root: %d, bam_root: %s\n", fcn,
8733*f64ca102SToomas Soome 	    bam_alt_root, bam_root));
8734eb2bd662Svikram 
8735eb2bd662Svikram 	if (bam_alt_root || bam_rootlen != 1 ||
8736eb2bd662Svikram 	    strcmp(bam_root, "/") != 0 ||
8737eb2bd662Svikram 	    strcmp(rootbuf, "/") != 0) {
8738*f64ca102SToomas Soome 		bam_error(_("an alternate root (%s) cannot be used with this "
8739*f64ca102SToomas Soome 		    "sub-command\n"), bam_root);
8740eb2bd662Svikram 		return (BAM_ERROR);
8741eb2bd662Svikram 	}
87427c478bd9Sstevel@tonic-gate 
87438c1b6884Sszhou 	/* If no option, delete exiting reboot menu entry */
87448c1b6884Sszhou 	if (opt == NULL) {
8745eb2bd662Svikram 		entry_t		*ent;
8746*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: opt is NULL\n", fcn));
8747eb2bd662Svikram 		ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL,
8748eb2bd662Svikram 		    NULL, NULL, 0, &entry);
8749eb2bd662Svikram 		if (ent == NULL) {	/* not found is ok */
8750*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: transient entry not found\n", fcn));
87518c1b6884Sszhou 			return (BAM_SUCCESS);
8752eb2bd662Svikram 		}
875344da779fSWilliam Kucharski 		(void) delete_boot_entry(mp, entry, DBE_PRINTERR);
8754ae115bc7Smrj 		restore_default_entry(mp, BAM_OLDDEF, mp->olddefault);
8755ae115bc7Smrj 		mp->olddefault = NULL;
8756*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: restored old default\n", fcn));
8757*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
87588c1b6884Sszhou 		return (BAM_WRITE);
87598c1b6884Sszhou 	}
87608c1b6884Sszhou 
87618c1b6884Sszhou 	/* if entry= is specified, set the default entry */
8762eb2bd662Svikram 	if (strncmp(opt, "entry=", strlen("entry=")) == 0) {
8763eb2bd662Svikram 		int entryNum = s_strtol(opt + strlen("entry="));
8764*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: opt has entry=: %s\n", fcn, opt));
8765eb2bd662Svikram 		if (selector(mp, opt, &entry, NULL) == BAM_SUCCESS) {
87667c478bd9Sstevel@tonic-gate 			/* this is entry=# option */
8767eb2bd662Svikram 			ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
8768*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: default set to %d, "
8769*f64ca102SToomas Soome 			    "set_default ret=%d\n", fcn, entry, ret));
8770eb2bd662Svikram 			return (ret);
8771eb2bd662Svikram 		} else {
8772*f64ca102SToomas Soome 			bam_error(_("failed to set GRUB menu default to %d\n"),
8773*f64ca102SToomas Soome 			    entryNum);
8774eb2bd662Svikram 			return (BAM_ERROR);
8775eb2bd662Svikram 		}
87767c478bd9Sstevel@tonic-gate 	}
87777c478bd9Sstevel@tonic-gate 
87787c478bd9Sstevel@tonic-gate 	/*
8779eb2bd662Svikram 	 * add a new menu entry based on opt and make it the default
8780b610f78eSvikram 	 */
8781eb2bd662Svikram 
8782eb2bd662Svikram 	fstype = get_fstype("/");
8783eb2bd662Svikram 	INJECT_ERROR1("REBOOT_FSTYPE_NULL", fstype = NULL);
8784eb2bd662Svikram 	if (fstype == NULL) {
8785*f64ca102SToomas Soome 		bam_error(_("failed to determine filesystem type for \"/\". "
8786*f64ca102SToomas Soome 		    "Reboot with \narguments failed.\n"));
8787eb2bd662Svikram 		return (BAM_ERROR);
87887c478bd9Sstevel@tonic-gate 	}
8789eb2bd662Svikram 
8790eb2bd662Svikram 	osdev = get_special("/");
8791eb2bd662Svikram 	INJECT_ERROR1("REBOOT_SPECIAL_NULL", osdev = NULL);
8792eb2bd662Svikram 	if (osdev == NULL) {
8793eb2bd662Svikram 		free(fstype);
8794*f64ca102SToomas Soome 		bam_error(_("failed to find device special file for \"/\". "
8795*f64ca102SToomas Soome 		    "Reboot with \narguments failed.\n"));
8796eb2bd662Svikram 		return (BAM_ERROR);
8797b610f78eSvikram 	}
8798eb2bd662Svikram 
8799eb2bd662Svikram 	sign = find_existing_sign("/", osdev, fstype);
8800eb2bd662Svikram 	INJECT_ERROR1("REBOOT_SIGN_NULL", sign = NULL);
8801eb2bd662Svikram 	if (sign == NULL) {
8802eb2bd662Svikram 		free(fstype);
8803eb2bd662Svikram 		free(osdev);
8804*f64ca102SToomas Soome 		bam_error(_("failed to find boot signature. Reboot with "
8805*f64ca102SToomas Soome 		    "arguments failed.\n"));
8806eb2bd662Svikram 		return (BAM_ERROR);
8807eb2bd662Svikram 	}
8808eb2bd662Svikram 
8809eb2bd662Svikram 	free(osdev);
8810eb2bd662Svikram 	(void) strlcpy(signbuf, sign, sizeof (signbuf));
8811eb2bd662Svikram 	free(sign);
8812eb2bd662Svikram 
8813eb2bd662Svikram 	assert(strchr(signbuf, '(') == NULL && strchr(signbuf, ',') == NULL &&
8814eb2bd662Svikram 	    strchr(signbuf, ')') == NULL);
8815eb2bd662Svikram 
8816eb2bd662Svikram 	/*
8817eb2bd662Svikram 	 * There is no alternate root while doing reboot with args
8818eb2bd662Svikram 	 * This version of bootadm is only delivered with a DBOOT
8819eb2bd662Svikram 	 * version of Solaris.
8820eb2bd662Svikram 	 */
8821eb2bd662Svikram 	INJECT_ERROR1("REBOOT_NOT_DBOOT", bam_direct = BAM_DIRECT_MULTIBOOT);
8822eb2bd662Svikram 	if (bam_direct != BAM_DIRECT_DBOOT) {
88239bd6d269SLin Ling 		free(fstype);
8824*f64ca102SToomas Soome 		bam_error(_("the root filesystem is not a dboot Solaris "
8825*f64ca102SToomas Soome 		    "instance. \nThis version of bootadm is not supported "
8826*f64ca102SToomas Soome 		    "on this version of Solaris.\n"));
88277c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
88287c478bd9Sstevel@tonic-gate 	}
88297c478bd9Sstevel@tonic-gate 
88307c478bd9Sstevel@tonic-gate 	/* add an entry for Solaris reboot */
8831ae115bc7Smrj 	if (opt[0] == '-') {
8832ae115bc7Smrj 		/* It's an option - first see if boot-file is set */
8833eb2bd662Svikram 		ret = get_kernel(mp, KERNEL_CMD, kernbuf, sizeof (kernbuf));
8834eb2bd662Svikram 		INJECT_ERROR1("REBOOT_GET_KERNEL", ret = BAM_ERROR);
8835eb2bd662Svikram 		if (ret != BAM_SUCCESS) {
88369bd6d269SLin Ling 			free(fstype);
8837*f64ca102SToomas Soome 			bam_error(_("reboot with arguments: error querying "
8838*f64ca102SToomas Soome 			    "current boot-file settings\n"));
8839ae115bc7Smrj 			return (BAM_ERROR);
8840eb2bd662Svikram 		}
8841ae115bc7Smrj 		if (kernbuf[0] == '\0')
8842eb2bd662Svikram 			(void) strlcpy(kernbuf, DIRECT_BOOT_KERNEL,
8843eb2bd662Svikram 			    sizeof (kernbuf));
88449bd6d269SLin Ling 		/*
88459bd6d269SLin Ling 		 * If this is a zfs file system and kernbuf does not
88469bd6d269SLin Ling 		 * have "-B $ZFS-BOOTFS" string yet, add it.
88479bd6d269SLin Ling 		 */
88489bd6d269SLin Ling 		if (strcmp(fstype, "zfs") == 0 && !strstr(kernbuf, ZFS_BOOT)) {
88499bd6d269SLin Ling 			(void) strlcat(kernbuf, " ", sizeof (kernbuf));
88509bd6d269SLin Ling 			(void) strlcat(kernbuf, ZFS_BOOT, sizeof (kernbuf));
88519bd6d269SLin Ling 		}
8852eb2bd662Svikram 		(void) strlcat(kernbuf, " ", sizeof (kernbuf));
8853eb2bd662Svikram 		(void) strlcat(kernbuf, opt, sizeof (kernbuf));
8854*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: reboot with args, option specified: "
8855*f64ca102SToomas Soome 		    "kern=%s\n", fcn, kernbuf));
8856ae115bc7Smrj 	} else if (opt[0] == '/') {
8857455710d3Srscott 		/* It's a full path, so write it out. */
8858eb2bd662Svikram 		(void) strlcpy(kernbuf, opt, sizeof (kernbuf));
8859455710d3Srscott 
8860455710d3Srscott 		/*
8861455710d3Srscott 		 * If someone runs:
8862455710d3Srscott 		 *
8863455710d3Srscott 		 *	# eeprom boot-args='-kd'
8864455710d3Srscott 		 *	# reboot /platform/i86pc/kernel/unix
8865455710d3Srscott 		 *
8866455710d3Srscott 		 * we want to use the boot-args as part of the boot
8867455710d3Srscott 		 * line.  On the other hand, if someone runs:
8868455710d3Srscott 		 *
8869455710d3Srscott 		 *	# reboot "/platform/i86pc/kernel/unix -kd"
8870455710d3Srscott 		 *
8871455710d3Srscott 		 * we don't need to mess with boot-args.  If there's
8872455710d3Srscott 		 * no space in the options string, assume we're in the
8873455710d3Srscott 		 * first case.
8874455710d3Srscott 		 */
8875455710d3Srscott 		if (strchr(opt, ' ') == NULL) {
8876eb2bd662Svikram 			ret = get_kernel(mp, ARGS_CMD, args_buf,
8877eb2bd662Svikram 			    sizeof (args_buf));
8878eb2bd662Svikram 			INJECT_ERROR1("REBOOT_GET_ARGS", ret = BAM_ERROR);
8879eb2bd662Svikram 			if (ret != BAM_SUCCESS) {
88809bd6d269SLin Ling 				free(fstype);
8881*f64ca102SToomas Soome 				bam_error(_("reboot with arguments: error "
8882*f64ca102SToomas Soome 				    "querying current boot-args settings\n"));
8883455710d3Srscott 				return (BAM_ERROR);
8884eb2bd662Svikram 			}
8885455710d3Srscott 
8886455710d3Srscott 			if (args_buf[0] != '\0') {
8887eb2bd662Svikram 				(void) strlcat(kernbuf, " ", sizeof (kernbuf));
8888455710d3Srscott 				(void) strlcat(kernbuf, args_buf,
8889eb2bd662Svikram 				    sizeof (kernbuf));
8890455710d3Srscott 			}
8891455710d3Srscott 		}
8892*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: reboot with args, abspath specified: "
8893*f64ca102SToomas Soome 		    "kern=%s\n", fcn, kernbuf));
8894ae115bc7Smrj 	} else {
8895455710d3Srscott 		/*
8896455710d3Srscott 		 * It may be a partial path, or it may be a partial
8897455710d3Srscott 		 * path followed by options.  Assume that only options
8898455710d3Srscott 		 * follow a space.  If someone sends us a kernel path
8899455710d3Srscott 		 * that includes a space, they deserve to be broken.
8900455710d3Srscott 		 */
8901455710d3Srscott 		opt_ptr = strchr(opt, ' ');
8902455710d3Srscott 		if (opt_ptr != NULL) {
8903455710d3Srscott 			*opt_ptr = '\0';
8904455710d3Srscott 		}
8905455710d3Srscott 
8906ae115bc7Smrj 		path = expand_path(opt);
8907ae115bc7Smrj 		if (path != NULL) {
8908eb2bd662Svikram 			(void) strlcpy(kernbuf, path, sizeof (kernbuf));
8909ae115bc7Smrj 			free(path);
8910455710d3Srscott 
8911455710d3Srscott 			/*
8912455710d3Srscott 			 * If there were options given, use those.
8913455710d3Srscott 			 * Otherwise, copy over the default options.
8914455710d3Srscott 			 */
8915455710d3Srscott 			if (opt_ptr != NULL) {
8916455710d3Srscott 				/* Restore the space in opt string */
8917455710d3Srscott 				*opt_ptr = ' ';
8918455710d3Srscott 				(void) strlcat(kernbuf, opt_ptr,
8919eb2bd662Svikram 				    sizeof (kernbuf));
8920455710d3Srscott 			} else {
8921eb2bd662Svikram 				ret = get_kernel(mp, ARGS_CMD, args_buf,
8922eb2bd662Svikram 				    sizeof (args_buf));
8923eb2bd662Svikram 				INJECT_ERROR1("UPDATE_TEMP_PARTIAL_ARGS",
8924eb2bd662Svikram 				    ret = BAM_ERROR);
8925eb2bd662Svikram 				if (ret != BAM_SUCCESS) {
89269bd6d269SLin Ling 					free(fstype);
8927*f64ca102SToomas Soome 					bam_error(_("reboot with arguments: "
8928*f64ca102SToomas Soome 					    "error querying current boot-args "
8929*f64ca102SToomas Soome 					    "settings\n"));
8930ae115bc7Smrj 					return (BAM_ERROR);
8931eb2bd662Svikram 				}
8932ae115bc7Smrj 
8933ae115bc7Smrj 				if (args_buf[0] != '\0') {
8934ae115bc7Smrj 					(void) strlcat(kernbuf, " ",
8935eb2bd662Svikram 					    sizeof (kernbuf));
8936ae115bc7Smrj 					(void) strlcat(kernbuf,
8937eb2bd662Svikram 					    args_buf, sizeof (kernbuf));
8938ae115bc7Smrj 				}
8939ae115bc7Smrj 			}
8940*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: resolved partial path: %s\n",
8941*f64ca102SToomas Soome 			    fcn, kernbuf));
8942455710d3Srscott 		} else {
89439bd6d269SLin Ling 			free(fstype);
8944*f64ca102SToomas Soome 			bam_error(_("unable to expand %s to a full file"
8945*f64ca102SToomas Soome 			    " path.\n"), opt);
8946*f64ca102SToomas Soome 			bam_print_stderr(_("Rebooting with default kernel "
8947*f64ca102SToomas Soome 			    "and options.\n"));
8948455710d3Srscott 			return (BAM_ERROR);
8949ae115bc7Smrj 		}
8950ae115bc7Smrj 	}
89519bd6d269SLin Ling 	free(fstype);
8952eb2bd662Svikram 	entry = add_boot_entry(mp, REBOOT_TITLE, signbuf, kernbuf,
895344da779fSWilliam Kucharski 	    NULL, NULL, NULL);
8954eb2bd662Svikram 	INJECT_ERROR1("REBOOT_ADD_BOOT_ENTRY", entry = BAM_ERROR);
89557c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
8956*f64ca102SToomas Soome 		bam_error(_("Cannot update menu. Cannot reboot with "
8957*f64ca102SToomas Soome 		    "requested arguments\n"));
89587c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
89597c478bd9Sstevel@tonic-gate 	}
89608c1b6884Sszhou 
8961ae115bc7Smrj 	save_default_entry(mp, BAM_OLDDEF);
8962eb2bd662Svikram 	ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
8963eb2bd662Svikram 	INJECT_ERROR1("REBOOT_SET_GLOBAL", ret = BAM_ERROR);
8964eb2bd662Svikram 	if (ret == BAM_ERROR) {
8965*f64ca102SToomas Soome 		bam_error(_("reboot with arguments: setting GRUB menu default "
8966*f64ca102SToomas Soome 		    "to %d failed\n"), entry);
8967eb2bd662Svikram 	}
8968*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
89697c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
89707c478bd9Sstevel@tonic-gate }
89717c478bd9Sstevel@tonic-gate 
897244da779fSWilliam Kucharski error_t
89737c478bd9Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val)
89747c478bd9Sstevel@tonic-gate {
8975eb2bd662Svikram 	line_t		*lp;
8976eb2bd662Svikram 	line_t		*found;
8977eb2bd662Svikram 	line_t		*last;
8978eb2bd662Svikram 	char		*cp;
8979eb2bd662Svikram 	char		*str;
89807c478bd9Sstevel@tonic-gate 	char		prefix[BAM_MAXLINE];
89817c478bd9Sstevel@tonic-gate 	size_t		len;
8982eb2bd662Svikram 	const char	*fcn = "set_global()";
89837c478bd9Sstevel@tonic-gate 
89847c478bd9Sstevel@tonic-gate 	assert(mp);
89857c478bd9Sstevel@tonic-gate 	assert(globalcmd);
89867c478bd9Sstevel@tonic-gate 
8987b610f78eSvikram 	if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) {
8988eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_VAL_NEG", val = -1);
8989eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_MENU_EMPTY", mp->end = NULL);
8990eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_VAL_TOO_BIG", val = 100);
8991b610f78eSvikram 		if (val < 0 || mp->end == NULL || val > mp->end->entryNum) {
8992b610f78eSvikram 			(void) snprintf(prefix, sizeof (prefix), "%d", val);
8993*f64ca102SToomas Soome 			bam_error(_("invalid boot entry number: %s\n"), prefix);
8994b610f78eSvikram 			return (BAM_ERROR);
8995b610f78eSvikram 		}
8996b610f78eSvikram 	}
8997b610f78eSvikram 
89987c478bd9Sstevel@tonic-gate 	found = last = NULL;
89997c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
90007c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
90017c478bd9Sstevel@tonic-gate 			continue;
90027c478bd9Sstevel@tonic-gate 
90037c478bd9Sstevel@tonic-gate 		last = lp; /* track the last global found */
90047c478bd9Sstevel@tonic-gate 
9005eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_NULL_CMD", lp->cmd = NULL);
90067c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
9007*f64ca102SToomas Soome 			bam_error(_("no command at line %d\n"), lp->lineNum);
90087c478bd9Sstevel@tonic-gate 			continue;
90097c478bd9Sstevel@tonic-gate 		}
90107c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
90117c478bd9Sstevel@tonic-gate 			continue;
90127c478bd9Sstevel@tonic-gate 
9013*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: found matching global command: %s\n",
9014*f64ca102SToomas Soome 		    fcn, globalcmd));
9015eb2bd662Svikram 
90167c478bd9Sstevel@tonic-gate 		if (found) {
9017*f64ca102SToomas Soome 			bam_error(_("duplicate command %s at line %d of "
9018*f64ca102SToomas Soome 			    "%sboot/grub/menu.lst\n"), globalcmd,
9019*f64ca102SToomas Soome 			    lp->lineNum, bam_root);
90207c478bd9Sstevel@tonic-gate 		}
90217c478bd9Sstevel@tonic-gate 		found = lp;
90227c478bd9Sstevel@tonic-gate 	}
90237c478bd9Sstevel@tonic-gate 
90247c478bd9Sstevel@tonic-gate 	if (found == NULL) {
90257c478bd9Sstevel@tonic-gate 		lp = s_calloc(1, sizeof (line_t));
90267c478bd9Sstevel@tonic-gate 		if (last == NULL) {
90277c478bd9Sstevel@tonic-gate 			lp->next = mp->start;
90287c478bd9Sstevel@tonic-gate 			mp->start = lp;
90297c478bd9Sstevel@tonic-gate 			mp->end = (mp->end) ? mp->end : lp;
90307c478bd9Sstevel@tonic-gate 		} else {
90317c478bd9Sstevel@tonic-gate 			lp->next = last->next;
90327c478bd9Sstevel@tonic-gate 			last->next = lp;
90337c478bd9Sstevel@tonic-gate 			if (lp->next == NULL)
90347c478bd9Sstevel@tonic-gate 				mp->end = lp;
90357c478bd9Sstevel@tonic-gate 		}
90367c478bd9Sstevel@tonic-gate 		lp->flags = BAM_GLOBAL; /* other fields not needed for writes */
90377c478bd9Sstevel@tonic-gate 		len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]);
90387c478bd9Sstevel@tonic-gate 		len += 10;	/* val < 10 digits */
90397c478bd9Sstevel@tonic-gate 		lp->line = s_calloc(1, len);
90407c478bd9Sstevel@tonic-gate 		(void) snprintf(lp->line, len, "%s%s%d",
90417c478bd9Sstevel@tonic-gate 		    globalcmd, menu_cmds[SEP_CMD], val);
9042*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: wrote new global line: %s\n", fcn, lp->line));
9043*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
90447c478bd9Sstevel@tonic-gate 		return (BAM_WRITE);
90457c478bd9Sstevel@tonic-gate 	}
90467c478bd9Sstevel@tonic-gate 
90477c478bd9Sstevel@tonic-gate 	/*
90487c478bd9Sstevel@tonic-gate 	 * We are changing an existing entry. Retain any prefix whitespace,
90497c478bd9Sstevel@tonic-gate 	 * but overwrite everything else. This preserves tabs added for
90507c478bd9Sstevel@tonic-gate 	 * readability.
90517c478bd9Sstevel@tonic-gate 	 */
90527c478bd9Sstevel@tonic-gate 	str = found->line;
90537c478bd9Sstevel@tonic-gate 	cp = prefix;
90547c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
90557c478bd9Sstevel@tonic-gate 		*(cp++) = *(str++);
90567c478bd9Sstevel@tonic-gate 	*cp = '\0'; /* Terminate prefix */
90577c478bd9Sstevel@tonic-gate 	len = strlen(prefix) + strlen(globalcmd);
90587c478bd9Sstevel@tonic-gate 	len += strlen(menu_cmds[SEP_CMD]) + 10;
90597c478bd9Sstevel@tonic-gate 
90607c478bd9Sstevel@tonic-gate 	free(found->line);
90617c478bd9Sstevel@tonic-gate 	found->line = s_calloc(1, len);
90627c478bd9Sstevel@tonic-gate 	(void) snprintf(found->line, len,
90637c478bd9Sstevel@tonic-gate 	    "%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val);
90647c478bd9Sstevel@tonic-gate 
9065*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: replaced global line with: %s\n", fcn, found->line));
9066*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
90677c478bd9Sstevel@tonic-gate 	return (BAM_WRITE); /* need a write to menu */
90687c478bd9Sstevel@tonic-gate }
90697c478bd9Sstevel@tonic-gate 
9070ae115bc7Smrj /*
9071ae115bc7Smrj  * partial_path may be anything like "kernel/unix" or "kmdb".  Try to
9072455710d3Srscott  * expand it to a full unix path.  The calling function is expected to
9073455710d3Srscott  * output a message if an error occurs and NULL is returned.
9074ae115bc7Smrj  */
9075ae115bc7Smrj static char *
9076ae115bc7Smrj expand_path(const char *partial_path)
9077ae115bc7Smrj {
9078ae115bc7Smrj 	int		new_path_len;
9079eb2bd662Svikram 	char		*new_path;
9080eb2bd662Svikram 	char		new_path2[PATH_MAX];
9081ae115bc7Smrj 	struct stat	sb;
9082eb2bd662Svikram 	const char	*fcn = "expand_path()";
9083ae115bc7Smrj 
9084ae115bc7Smrj 	new_path_len = strlen(partial_path) + 64;
9085ae115bc7Smrj 	new_path = s_calloc(1, new_path_len);
9086ae115bc7Smrj 
9087ae115bc7Smrj 	/* First, try the simplest case - something like "kernel/unix" */
9088ae115bc7Smrj 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s",
9089ae115bc7Smrj 	    partial_path);
9090ae115bc7Smrj 	if (stat(new_path, &sb) == 0) {
9091*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: expanded path: %s\n", fcn, new_path));
9092ae115bc7Smrj 		return (new_path);
9093ae115bc7Smrj 	}
9094ae115bc7Smrj 
9095ae115bc7Smrj 	if (strcmp(partial_path, "kmdb") == 0) {
9096ae115bc7Smrj 		(void) snprintf(new_path, new_path_len, "%s -k",
9097ae115bc7Smrj 		    DIRECT_BOOT_KERNEL);
9098*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: expanded path: %s\n", fcn, new_path));
9099ae115bc7Smrj 		return (new_path);
9100ae115bc7Smrj 	}
9101ae115bc7Smrj 
9102ae115bc7Smrj 	/*
9103ae115bc7Smrj 	 * We've quickly reached unsupported usage.  Try once more to
9104ae115bc7Smrj 	 * see if we were just given a glom name.
9105ae115bc7Smrj 	 */
9106ae115bc7Smrj 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s/unix",
9107ae115bc7Smrj 	    partial_path);
9108ae115bc7Smrj 	(void) snprintf(new_path2, PATH_MAX, "/platform/i86pc/%s/amd64/unix",
9109ae115bc7Smrj 	    partial_path);
9110ae115bc7Smrj 	if (stat(new_path, &sb) == 0) {
9111ae115bc7Smrj 		if (stat(new_path2, &sb) == 0) {
9112ae115bc7Smrj 			/*
9113ae115bc7Smrj 			 * We matched both, so we actually
9114ae115bc7Smrj 			 * want to write the $ISADIR version.
9115ae115bc7Smrj 			 */
9116ae115bc7Smrj 			(void) snprintf(new_path, new_path_len,
9117ae115bc7Smrj 			    "/platform/i86pc/kernel/%s/$ISADIR/unix",
9118ae115bc7Smrj 			    partial_path);
9119ae115bc7Smrj 		}
9120*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: expanded path: %s\n", fcn, new_path));
9121ae115bc7Smrj 		return (new_path);
9122ae115bc7Smrj 	}
9123ae115bc7Smrj 
9124ae115bc7Smrj 	free(new_path);
9125*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
9126ae115bc7Smrj 	return (NULL);
9127ae115bc7Smrj }
9128ae115bc7Smrj 
9129ae115bc7Smrj /*
9130ae115bc7Smrj  * The kernel cmd and arg have been changed, so
9131ae115bc7Smrj  * check whether the archive line needs to change.
9132ae115bc7Smrj  */
9133ae115bc7Smrj static void
9134ae115bc7Smrj set_archive_line(entry_t *entryp, line_t *kernelp)
9135ae115bc7Smrj {
9136ae115bc7Smrj 	line_t		*lp = entryp->start;
9137ae115bc7Smrj 	char		*new_archive;
9138ae115bc7Smrj 	menu_cmd_t	m_cmd;
9139eb2bd662Svikram 	const char	*fcn = "set_archive_line()";
9140ae115bc7Smrj 
9141ae115bc7Smrj 	for (; lp != NULL; lp = lp->next) {
9142eac223ccSWilliam Kucharski 		if (lp->cmd != NULL && strncmp(lp->cmd, menu_cmds[MODULE_CMD],
9143ae115bc7Smrj 		    sizeof (menu_cmds[MODULE_CMD]) - 1) == 0) {
9144ae115bc7Smrj 			break;
9145ae115bc7Smrj 		}
9146eb2bd662Svikram 
9147eb2bd662Svikram 		INJECT_ERROR1("SET_ARCHIVE_LINE_END_ENTRY", lp = entryp->end);
9148eb2bd662Svikram 		if (lp == entryp->end) {
9149*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: no module/archive line for entry: "
9150*f64ca102SToomas Soome 			    "%d\n", fcn, entryp->entryNum));
9151ae115bc7Smrj 			return;
9152ae115bc7Smrj 		}
9153eb2bd662Svikram 	}
9154eb2bd662Svikram 	INJECT_ERROR1("SET_ARCHIVE_LINE_END_MENU", lp = NULL);
9155eb2bd662Svikram 	if (lp == NULL) {
9156*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: no module/archive line for entry: %d\n",
9157*f64ca102SToomas Soome 		    fcn, entryp->entryNum));
9158ae115bc7Smrj 		return;
9159eb2bd662Svikram 	}
9160ae115bc7Smrj 
9161ae115bc7Smrj 	if (strstr(kernelp->arg, "$ISADIR") != NULL) {
9162ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE;
9163ae115bc7Smrj 		m_cmd = MODULE_DOLLAR_CMD;
9164ae115bc7Smrj 	} else if (strstr(kernelp->arg, "amd64") != NULL) {
9165ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE_64;
9166ae115bc7Smrj 		m_cmd = MODULE_CMD;
9167ae115bc7Smrj 	} else {
9168ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE_32;
9169ae115bc7Smrj 		m_cmd = MODULE_CMD;
9170ae115bc7Smrj 	}
9171ae115bc7Smrj 
9172eb2bd662Svikram 	if (strcmp(lp->arg, new_archive) == 0) {
9173*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: no change for line: %s\n", fcn, lp->arg));
9174ae115bc7Smrj 		return;
9175eb2bd662Svikram 	}
9176ae115bc7Smrj 
9177eac223ccSWilliam Kucharski 	if (lp->cmd != NULL && strcmp(lp->cmd, menu_cmds[m_cmd]) != 0) {
9178ae115bc7Smrj 		free(lp->cmd);
9179ae115bc7Smrj 		lp->cmd = s_strdup(menu_cmds[m_cmd]);
9180ae115bc7Smrj 	}
9181ae115bc7Smrj 
9182ae115bc7Smrj 	free(lp->arg);
9183ae115bc7Smrj 	lp->arg = s_strdup(new_archive);
9184ae115bc7Smrj 	update_line(lp);
9185*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: replaced for line: %s\n", fcn, lp->line));
9186ae115bc7Smrj }
9187ae115bc7Smrj 
9188ae115bc7Smrj /*
9189ae115bc7Smrj  * Title for an entry to set properties that once went in bootenv.rc.
9190ae115bc7Smrj  */
9191ae115bc7Smrj #define	BOOTENV_RC_TITLE	"Solaris bootenv rc"
9192ae115bc7Smrj 
9193ae115bc7Smrj /*
9194ae115bc7Smrj  * If path is NULL, return the kernel (optnum == KERNEL_CMD) or arguments
9195ae115bc7Smrj  * (optnum == ARGS_CMD) in the argument buf.  If path is a zero-length
9196ae115bc7Smrj  * string, reset the value to the default.  If path is a non-zero-length
9197ae115bc7Smrj  * string, set the kernel or arguments.
9198ae115bc7Smrj  */
9199ae115bc7Smrj static error_t
9200eb2bd662Svikram get_set_kernel(
9201eb2bd662Svikram 	menu_t *mp,
9202eb2bd662Svikram 	menu_cmd_t optnum,
9203eb2bd662Svikram 	char *path,
9204eb2bd662Svikram 	char *buf,
9205eb2bd662Svikram 	size_t bufsize)
9206ae115bc7Smrj {
9207eb2bd662Svikram 	int		entryNum;
9208eb2bd662Svikram 	int		rv = BAM_SUCCESS;
9209eb2bd662Svikram 	int		free_new_path = 0;
9210ae115bc7Smrj 	entry_t		*entryp;
9211eb2bd662Svikram 	line_t		*ptr;
9212eb2bd662Svikram 	line_t		*kernelp;
9213eb2bd662Svikram 	char		*new_arg;
9214eb2bd662Svikram 	char		*old_args;
9215eb2bd662Svikram 	char		*space;
9216eb2bd662Svikram 	char		*new_path;
9217ae115bc7Smrj 	char		old_space;
9218110570ceSToomas Soome 	size_t		old_kernel_len = 0;
9219eb2bd662Svikram 	size_t		new_str_len;
9220eb2bd662Svikram 	char		*fstype;
9221eb2bd662Svikram 	char		*osdev;
9222eb2bd662Svikram 	char		*sign;
9223eb2bd662Svikram 	char		signbuf[PATH_MAX];
9224eb2bd662Svikram 	int		ret;
9225eb2bd662Svikram 	const char	*fcn = "get_set_kernel()";
9226ae115bc7Smrj 
9227ae115bc7Smrj 	assert(bufsize > 0);
9228ae115bc7Smrj 
9229ae115bc7Smrj 	ptr = kernelp = NULL;
9230ae115bc7Smrj 	new_arg = old_args = space = NULL;
9231eb2bd662Svikram 	new_path = NULL;
9232ae115bc7Smrj 	buf[0] = '\0';
9233ae115bc7Smrj 
9234eb2bd662Svikram 	INJECT_ERROR1("GET_SET_KERNEL_NOT_DBOOT",
9235eb2bd662Svikram 	    bam_direct = BAM_DIRECT_MULTIBOOT);
9236ae115bc7Smrj 	if (bam_direct != BAM_DIRECT_DBOOT) {
9237*f64ca102SToomas Soome 		bam_error(_("bootadm set-menu %s may only be run on "
9238*f64ca102SToomas Soome 		    "directboot kernels.\n"),
9239*f64ca102SToomas Soome 		    optnum == KERNEL_CMD ? "kernel" : "args");
9240ae115bc7Smrj 		return (BAM_ERROR);
9241ae115bc7Smrj 	}
9242ae115bc7Smrj 
9243ae115bc7Smrj 	/*
9244ae115bc7Smrj 	 * If a user changed the default entry to a non-bootadm controlled
9245ae115bc7Smrj 	 * one, we don't want to mess with it.  Just print an error and
9246ae115bc7Smrj 	 * return.
9247ae115bc7Smrj 	 */
9248ae115bc7Smrj 	if (mp->curdefault) {
9249ae115bc7Smrj 		entryNum = s_strtol(mp->curdefault->arg);
9250ae115bc7Smrj 		for (entryp = mp->entries; entryp; entryp = entryp->next) {
9251ae115bc7Smrj 			if (entryp->entryNum == entryNum)
9252ae115bc7Smrj 				break;
9253ae115bc7Smrj 		}
9254ae115bc7Smrj 		if ((entryp != NULL) &&
9255ae115bc7Smrj 		    ((entryp->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU)) == 0)) {
9256*f64ca102SToomas Soome 			bam_error(_("Default /boot/grub/menu.lst entry is not "
9257*f64ca102SToomas Soome 			    "controlled by bootadm.  Exiting\n"));
9258ae115bc7Smrj 			return (BAM_ERROR);
9259ae115bc7Smrj 		}
9260ae115bc7Smrj 	}
9261ae115bc7Smrj 
9262eb2bd662Svikram 	entryp = find_boot_entry(mp, BOOTENV_RC_TITLE, NULL, NULL, NULL, NULL,
9263eb2bd662Svikram 	    0, &entryNum);
9264ae115bc7Smrj 
9265ae115bc7Smrj 	if (entryp != NULL) {
9266ae115bc7Smrj 		for (ptr = entryp->start; ptr && ptr != entryp->end;
9267ae115bc7Smrj 		    ptr = ptr->next) {
9268ae115bc7Smrj 			if (strncmp(ptr->cmd, menu_cmds[KERNEL_CMD],
9269ae115bc7Smrj 			    sizeof (menu_cmds[KERNEL_CMD]) - 1) == 0) {
9270ae115bc7Smrj 				kernelp = ptr;
9271ae115bc7Smrj 				break;
9272ae115bc7Smrj 			}
9273ae115bc7Smrj 		}
9274ae115bc7Smrj 		if (kernelp == NULL) {
9275*f64ca102SToomas Soome 			bam_error(_("no kernel line found in entry %d\n"),
9276*f64ca102SToomas Soome 			    entryNum);
9277ae115bc7Smrj 			return (BAM_ERROR);
9278ae115bc7Smrj 		}
9279ae115bc7Smrj 
9280ae115bc7Smrj 		old_kernel_len = strcspn(kernelp->arg, " \t");
9281ae115bc7Smrj 		space = old_args = kernelp->arg + old_kernel_len;
9282ae115bc7Smrj 		while ((*old_args == ' ') || (*old_args == '\t'))
9283ae115bc7Smrj 			old_args++;
9284ae115bc7Smrj 	}
9285ae115bc7Smrj 
9286ae115bc7Smrj 	if (path == NULL) {
9287eb2bd662Svikram 		if (entryp == NULL) {
9288*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: no RC entry, nothing to report\n",
9289*f64ca102SToomas Soome 			    fcn));
9290*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
9291ae115bc7Smrj 			return (BAM_SUCCESS);
9292eb2bd662Svikram 		}
9293eb2bd662Svikram 		assert(kernelp);
9294ae115bc7Smrj 		if (optnum == ARGS_CMD) {
9295eb2bd662Svikram 			if (old_args[0] != '\0') {
9296ae115bc7Smrj 				(void) strlcpy(buf, old_args, bufsize);
9297*f64ca102SToomas Soome 				BAM_DPRINTF(("%s: read menu boot-args: %s\n",
9298*f64ca102SToomas Soome 				    fcn, buf));
9299eb2bd662Svikram 			}
9300ae115bc7Smrj 		} else {
9301ae115bc7Smrj 			/*
9302ae115bc7Smrj 			 * We need to print the kernel, so we just turn the
9303ae115bc7Smrj 			 * first space into a '\0' and print the beginning.
9304ae115bc7Smrj 			 * We don't print anything if it's the default kernel.
9305ae115bc7Smrj 			 */
9306ae115bc7Smrj 			old_space = *space;
9307ae115bc7Smrj 			*space = '\0';
9308eb2bd662Svikram 			if (strcmp(kernelp->arg, DIRECT_BOOT_KERNEL) != 0) {
9309ae115bc7Smrj 				(void) strlcpy(buf, kernelp->arg, bufsize);
9310*f64ca102SToomas Soome 				BAM_DPRINTF(("%s: read menu boot-file: %s\n",
9311*f64ca102SToomas Soome 				    fcn, buf));
9312eb2bd662Svikram 			}
9313ae115bc7Smrj 			*space = old_space;
9314ae115bc7Smrj 		}
9315*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
9316ae115bc7Smrj 		return (BAM_SUCCESS);
9317ae115bc7Smrj 	}
9318ae115bc7Smrj 
9319ae115bc7Smrj 	/*
9320ae115bc7Smrj 	 * First, check if we're resetting an entry to the default.
9321ae115bc7Smrj 	 */
9322ae115bc7Smrj 	if ((path[0] == '\0') ||
9323ae115bc7Smrj 	    ((optnum == KERNEL_CMD) &&
9324ae115bc7Smrj 	    (strcmp(path, DIRECT_BOOT_KERNEL) == 0))) {
9325ae115bc7Smrj 		if ((entryp == NULL) || (kernelp == NULL)) {
9326ae115bc7Smrj 			/* No previous entry, it's already the default */
9327*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: no reset, already has default\n",
9328*f64ca102SToomas Soome 			    fcn));
9329ae115bc7Smrj 			return (BAM_SUCCESS);
9330ae115bc7Smrj 		}
9331ae115bc7Smrj 
9332ae115bc7Smrj 		/*
9333ae115bc7Smrj 		 * Check if we can delete the entry.  If we're resetting the
9334ae115bc7Smrj 		 * kernel command, and the args is already empty, or if we're
9335ae115bc7Smrj 		 * resetting the args command, and the kernel is already the
9336ae115bc7Smrj 		 * default, we can restore the old default and delete the entry.
9337ae115bc7Smrj 		 */
9338ae115bc7Smrj 		if (((optnum == KERNEL_CMD) &&
9339ae115bc7Smrj 		    ((old_args == NULL) || (old_args[0] == '\0'))) ||
9340ae115bc7Smrj 		    ((optnum == ARGS_CMD) &&
9341ae115bc7Smrj 		    (strncmp(kernelp->arg, DIRECT_BOOT_KERNEL,
9342ae115bc7Smrj 		    sizeof (DIRECT_BOOT_KERNEL) - 1) == 0))) {
9343ae115bc7Smrj 			kernelp = NULL;
934444da779fSWilliam Kucharski 			(void) delete_boot_entry(mp, entryNum, DBE_PRINTERR);
9345ae115bc7Smrj 			restore_default_entry(mp, BAM_OLD_RC_DEF,
9346ae115bc7Smrj 			    mp->old_rc_default);
9347ae115bc7Smrj 			mp->old_rc_default = NULL;
9348ae115bc7Smrj 			rv = BAM_WRITE;
9349*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: resetting to default\n", fcn));
9350ae115bc7Smrj 			goto done;
9351ae115bc7Smrj 		}
9352ae115bc7Smrj 
9353ae115bc7Smrj 		if (optnum == KERNEL_CMD) {
9354ae115bc7Smrj 			/*
9355ae115bc7Smrj 			 * At this point, we've already checked that old_args
9356ae115bc7Smrj 			 * and entryp are valid pointers.  The "+ 2" is for
9357ae115bc7Smrj 			 * a space a the string termination character.
9358ae115bc7Smrj 			 */
9359ae115bc7Smrj 			new_str_len = (sizeof (DIRECT_BOOT_KERNEL) - 1) +
9360ae115bc7Smrj 			    strlen(old_args) + 2;
9361ae115bc7Smrj 			new_arg = s_calloc(1, new_str_len);
9362ae115bc7Smrj 			(void) snprintf(new_arg, new_str_len, "%s %s",
9363ae115bc7Smrj 			    DIRECT_BOOT_KERNEL, old_args);
9364ae115bc7Smrj 			free(kernelp->arg);
9365ae115bc7Smrj 			kernelp->arg = new_arg;
9366ae115bc7Smrj 
9367ae115bc7Smrj 			/*
9368ae115bc7Smrj 			 * We have changed the kernel line, so we may need
9369ae115bc7Smrj 			 * to update the archive line as well.
9370ae115bc7Smrj 			 */
9371ae115bc7Smrj 			set_archive_line(entryp, kernelp);
9372*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: reset kernel to default, but "
9373*f64ca102SToomas Soome 			    "retained old args: %s\n", fcn, kernelp->arg));
9374ae115bc7Smrj 		} else {
9375ae115bc7Smrj 			/*
9376ae115bc7Smrj 			 * We're resetting the boot args to nothing, so
9377ae115bc7Smrj 			 * we only need to copy the kernel.  We've already
9378ae115bc7Smrj 			 * checked that the kernel is not the default.
9379ae115bc7Smrj 			 */
9380ae115bc7Smrj 			new_arg = s_calloc(1, old_kernel_len + 1);
9381ae115bc7Smrj 			(void) snprintf(new_arg, old_kernel_len + 1, "%s",
9382ae115bc7Smrj 			    kernelp->arg);
9383ae115bc7Smrj 			free(kernelp->arg);
9384ae115bc7Smrj 			kernelp->arg = new_arg;
9385*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: reset args to default, but retained "
9386*f64ca102SToomas Soome 			    "old kernel: %s\n", fcn, kernelp->arg));
9387ae115bc7Smrj 		}
9388ae115bc7Smrj 		rv = BAM_WRITE;
9389ae115bc7Smrj 		goto done;
9390ae115bc7Smrj 	}
9391ae115bc7Smrj 
9392ae115bc7Smrj 	/*
9393ae115bc7Smrj 	 * Expand the kernel file to a full path, if necessary
9394ae115bc7Smrj 	 */
9395ae115bc7Smrj 	if ((optnum == KERNEL_CMD) && (path[0] != '/')) {
9396ae115bc7Smrj 		new_path = expand_path(path);
9397ae115bc7Smrj 		if (new_path == NULL) {
9398*f64ca102SToomas Soome 			bam_error(_("unable to expand %s to a full file "
9399*f64ca102SToomas Soome 			    "path.\n"), path);
9400*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
9401ae115bc7Smrj 			return (BAM_ERROR);
9402ae115bc7Smrj 		}
9403ae115bc7Smrj 		free_new_path = 1;
9404ae115bc7Smrj 	} else {
9405ae115bc7Smrj 		new_path = path;
9406ae115bc7Smrj 		free_new_path = 0;
9407ae115bc7Smrj 	}
9408ae115bc7Smrj 
9409ae115bc7Smrj 	/*
9410ae115bc7Smrj 	 * At this point, we know we're setting a new value.  First, take care
9411ae115bc7Smrj 	 * of the case where there was no previous entry.
9412ae115bc7Smrj 	 */
9413ae115bc7Smrj 	if (entryp == NULL) {
9414eb2bd662Svikram 
9415ae115bc7Smrj 		/* Similar to code in update_temp */
9416eb2bd662Svikram 		fstype = get_fstype("/");
9417eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_FSTYPE", fstype = NULL);
9418eb2bd662Svikram 		if (fstype == NULL) {
9419*f64ca102SToomas Soome 			bam_error(_("cannot determine filesystem type for "
9420*f64ca102SToomas Soome 			    "\"/\".\nCannot generate GRUB menu entry with "
9421*f64ca102SToomas Soome 			    "EEPROM arguments.\n"));
9422ae115bc7Smrj 			rv = BAM_ERROR;
9423ae115bc7Smrj 			goto done;
9424ae115bc7Smrj 		}
9425eb2bd662Svikram 
9426eb2bd662Svikram 		osdev = get_special("/");
9427eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_SPECIAL", osdev = NULL);
9428eb2bd662Svikram 		if (osdev == NULL) {
9429eb2bd662Svikram 			free(fstype);
9430*f64ca102SToomas Soome 			bam_error(_("cannot determine device special file for "
9431*f64ca102SToomas Soome 			    "\"/\".\nCannot generate GRUB menu entry with "
9432*f64ca102SToomas Soome 			    "EEPROM arguments.\n"));
9433eb2bd662Svikram 			rv = BAM_ERROR;
9434eb2bd662Svikram 			goto done;
9435eb2bd662Svikram 		}
9436eb2bd662Svikram 
9437eb2bd662Svikram 		sign = find_existing_sign("/", osdev, fstype);
9438eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_SIGN", sign = NULL);
9439eb2bd662Svikram 		if (sign == NULL) {
9440eb2bd662Svikram 			free(fstype);
9441eb2bd662Svikram 			free(osdev);
9442*f64ca102SToomas Soome 			bam_error(_("cannot determine boot signature for "
9443*f64ca102SToomas Soome 			    "\"/\".\nCannot generate GRUB menu entry with "
9444*f64ca102SToomas Soome 			    "EEPROM arguments.\n"));
9445eb2bd662Svikram 			rv = BAM_ERROR;
9446eb2bd662Svikram 			goto done;
9447eb2bd662Svikram 		}
9448eb2bd662Svikram 
9449eb2bd662Svikram 		free(osdev);
9450eb2bd662Svikram 		(void) strlcpy(signbuf, sign, sizeof (signbuf));
9451eb2bd662Svikram 		free(sign);
9452eb2bd662Svikram 		assert(strchr(signbuf, '(') == NULL &&
9453eb2bd662Svikram 		    strchr(signbuf, ',') == NULL &&
9454eb2bd662Svikram 		    strchr(signbuf, ')') == NULL);
9455eb2bd662Svikram 
9456ae115bc7Smrj 		if (optnum == KERNEL_CMD) {
94574e483a70SGangadhar Mylapuram 			if (strcmp(fstype, "zfs") == 0) {
94584e483a70SGangadhar Mylapuram 				new_str_len = strlen(new_path) +
94594e483a70SGangadhar Mylapuram 				    strlen(ZFS_BOOT) + 8;
94604e483a70SGangadhar Mylapuram 				new_arg = s_calloc(1, new_str_len);
94614e483a70SGangadhar Mylapuram 				(void) snprintf(new_arg, new_str_len, "%s %s",
94624e483a70SGangadhar Mylapuram 				    new_path, ZFS_BOOT);
9463*f64ca102SToomas Soome 				BAM_DPRINTF(("%s: new kernel=%s\n", fcn,
94644e483a70SGangadhar Mylapuram 				    new_arg));
94654e483a70SGangadhar Mylapuram 				entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
94664e483a70SGangadhar Mylapuram 				    signbuf, new_arg, NULL, NULL, NULL);
94674e483a70SGangadhar Mylapuram 				free(new_arg);
94684e483a70SGangadhar Mylapuram 			} else {
9469*f64ca102SToomas Soome 				BAM_DPRINTF(("%s: new kernel=%s\n", fcn,
94704e483a70SGangadhar Mylapuram 				    new_path));
9471ae115bc7Smrj 				entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
947244da779fSWilliam Kucharski 				    signbuf, new_path, NULL, NULL, NULL);
94734e483a70SGangadhar Mylapuram 			}
9474ae115bc7Smrj 		} else {
94754487ce1dSGangadhar Mylapuram 			new_str_len = strlen(path) + 8;
94764487ce1dSGangadhar Mylapuram 			if (strcmp(fstype, "zfs") == 0) {
94774487ce1dSGangadhar Mylapuram 				new_str_len += strlen(DIRECT_BOOT_KERNEL_ZFS);
9478ae115bc7Smrj 				new_arg = s_calloc(1, new_str_len);
94794487ce1dSGangadhar Mylapuram 				(void) snprintf(new_arg, new_str_len, "%s %s",
94804487ce1dSGangadhar Mylapuram 				    DIRECT_BOOT_KERNEL_ZFS, path);
94814487ce1dSGangadhar Mylapuram 			} else {
94824487ce1dSGangadhar Mylapuram 				new_str_len += strlen(DIRECT_BOOT_KERNEL);
94834487ce1dSGangadhar Mylapuram 				new_arg = s_calloc(1, new_str_len);
9484ae115bc7Smrj 				(void) snprintf(new_arg, new_str_len, "%s %s",
9485ae115bc7Smrj 				    DIRECT_BOOT_KERNEL, path);
94864487ce1dSGangadhar Mylapuram 			}
94874487ce1dSGangadhar Mylapuram 
9488*f64ca102SToomas Soome 			BAM_DPRINTF(("%s: new args=%s\n", fcn, new_arg));
9489ae115bc7Smrj 			entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
949044da779fSWilliam Kucharski 			    signbuf, new_arg, NULL, DIRECT_BOOT_ARCHIVE, NULL);
9491eb2bd662Svikram 			free(new_arg);
9492eb2bd662Svikram 		}
94934487ce1dSGangadhar Mylapuram 		free(fstype);
9494eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_ADD_BOOT_ENTRY",
9495eb2bd662Svikram 		    entryNum = BAM_ERROR);
9496eb2bd662Svikram 		if (entryNum == BAM_ERROR) {
9497*f64ca102SToomas Soome 			bam_error(_("failed to add boot entry: %s\n"),
9498eb2bd662Svikram 			    BOOTENV_RC_TITLE);
9499eb2bd662Svikram 			rv = BAM_ERROR;
9500eb2bd662Svikram 			goto done;
9501ae115bc7Smrj 		}
9502ae115bc7Smrj 		save_default_entry(mp, BAM_OLD_RC_DEF);
9503eb2bd662Svikram 		ret = set_global(mp, menu_cmds[DEFAULT_CMD], entryNum);
9504eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_SET_GLOBAL", ret = BAM_ERROR);
9505eb2bd662Svikram 		if (ret == BAM_ERROR) {
9506*f64ca102SToomas Soome 			bam_error(_("failed to set default to: %d\n"),
9507*f64ca102SToomas Soome 			    entryNum);
9508eb2bd662Svikram 		}
9509ae115bc7Smrj 		rv = BAM_WRITE;
9510ae115bc7Smrj 		goto done;
9511ae115bc7Smrj 	}
9512ae115bc7Smrj 
9513ae115bc7Smrj 	/*
9514ae115bc7Smrj 	 * There was already an bootenv entry which we need to edit.
9515ae115bc7Smrj 	 */
9516ae115bc7Smrj 	if (optnum == KERNEL_CMD) {
9517ae115bc7Smrj 		new_str_len = strlen(new_path) + strlen(old_args) + 2;
9518ae115bc7Smrj 		new_arg = s_calloc(1, new_str_len);
9519ae115bc7Smrj 		(void) snprintf(new_arg, new_str_len, "%s %s", new_path,
9520ae115bc7Smrj 		    old_args);
9521ae115bc7Smrj 		free(kernelp->arg);
9522ae115bc7Smrj 		kernelp->arg = new_arg;
9523ae115bc7Smrj 
9524ae115bc7Smrj 		/*
9525ae115bc7Smrj 		 * If we have changed the kernel line, we may need to update
9526ae115bc7Smrj 		 * the archive line as well.
9527ae115bc7Smrj 		 */
9528ae115bc7Smrj 		set_archive_line(entryp, kernelp);
9529*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: rc line exists, replaced kernel, same "
9530*f64ca102SToomas Soome 		    "args: %s\n", fcn, kernelp->arg));
9531ae115bc7Smrj 	} else {
9532ae115bc7Smrj 		new_str_len = old_kernel_len + strlen(path) + 8;
9533ae115bc7Smrj 		new_arg = s_calloc(1, new_str_len);
9534ae115bc7Smrj 		(void) strncpy(new_arg, kernelp->arg, old_kernel_len);
9535ae115bc7Smrj 		(void) strlcat(new_arg, " ", new_str_len);
9536ae115bc7Smrj 		(void) strlcat(new_arg, path, new_str_len);
9537ae115bc7Smrj 		free(kernelp->arg);
9538ae115bc7Smrj 		kernelp->arg = new_arg;
9539*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: rc line exists, same kernel, but new "
9540*f64ca102SToomas Soome 		    "args: %s\n", fcn, kernelp->arg));
9541ae115bc7Smrj 	}
9542ae115bc7Smrj 	rv = BAM_WRITE;
9543ae115bc7Smrj 
9544ae115bc7Smrj done:
9545ae115bc7Smrj 	if ((rv == BAM_WRITE) && kernelp)
9546ae115bc7Smrj 		update_line(kernelp);
9547ae115bc7Smrj 	if (free_new_path)
9548ae115bc7Smrj 		free(new_path);
9549eb2bd662Svikram 	if (rv == BAM_WRITE) {
9550*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
9551eb2bd662Svikram 	} else {
9552*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
9553eb2bd662Svikram 	}
9554ae115bc7Smrj 	return (rv);
9555ae115bc7Smrj }
9556ae115bc7Smrj 
9557eb2bd662Svikram static error_t
9558eb2bd662Svikram get_kernel(menu_t *mp, menu_cmd_t optnum, char *buf, size_t bufsize)
9559eb2bd662Svikram {
9560eb2bd662Svikram 	const char	*fcn = "get_kernel()";
9561*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. arg: %s\n", fcn, menu_cmds[optnum]));
9562eb2bd662Svikram 	return (get_set_kernel(mp, optnum, NULL, buf, bufsize));
9563eb2bd662Svikram }
9564eb2bd662Svikram 
9565eb2bd662Svikram static error_t
9566eb2bd662Svikram set_kernel(menu_t *mp, menu_cmd_t optnum, char *path, char *buf, size_t bufsize)
9567eb2bd662Svikram {
9568eb2bd662Svikram 	const char	*fcn = "set_kernel()";
9569eb2bd662Svikram 	assert(path != NULL);
9570*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn,
9571*f64ca102SToomas Soome 	    menu_cmds[optnum], path));
9572eb2bd662Svikram 	return (get_set_kernel(mp, optnum, path, buf, bufsize));
9573eb2bd662Svikram }
9574eb2bd662Svikram 
95757c478bd9Sstevel@tonic-gate /*ARGSUSED*/
95767c478bd9Sstevel@tonic-gate static error_t
9577eb2bd662Svikram set_option(menu_t *mp, char *dummy, char *opt)
95787c478bd9Sstevel@tonic-gate {
9579eb2bd662Svikram 	int		optnum;
9580eb2bd662Svikram 	int		optval;
95817c478bd9Sstevel@tonic-gate 	char		*val;
9582ae115bc7Smrj 	char		buf[BUFSIZ] = "";
9583ae115bc7Smrj 	error_t		rv;
9584eb2bd662Svikram 	const char	*fcn = "set_option()";
95857c478bd9Sstevel@tonic-gate 
95867c478bd9Sstevel@tonic-gate 	assert(mp);
95877c478bd9Sstevel@tonic-gate 	assert(opt);
9588eb2bd662Svikram 	assert(dummy == NULL);
9589eb2bd662Svikram 
9590eb2bd662Svikram 	/* opt is set from bam_argv[0] and is always non-NULL */
9591*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. arg: %s\n", fcn, opt));
95927c478bd9Sstevel@tonic-gate 
95937c478bd9Sstevel@tonic-gate 	val = strchr(opt, '=');
9594ae115bc7Smrj 	if (val != NULL) {
9595ae115bc7Smrj 		*val = '\0';
95967c478bd9Sstevel@tonic-gate 	}
95977c478bd9Sstevel@tonic-gate 
95987c478bd9Sstevel@tonic-gate 	if (strcmp(opt, "default") == 0) {
95997c478bd9Sstevel@tonic-gate 		optnum = DEFAULT_CMD;
96007c478bd9Sstevel@tonic-gate 	} else if (strcmp(opt, "timeout") == 0) {
96017c478bd9Sstevel@tonic-gate 		optnum = TIMEOUT_CMD;
9602ae115bc7Smrj 	} else if (strcmp(opt, menu_cmds[KERNEL_CMD]) == 0) {
9603ae115bc7Smrj 		optnum = KERNEL_CMD;
9604ae115bc7Smrj 	} else if (strcmp(opt, menu_cmds[ARGS_CMD]) == 0) {
9605ae115bc7Smrj 		optnum = ARGS_CMD;
96067c478bd9Sstevel@tonic-gate 	} else {
9607*f64ca102SToomas Soome 		bam_error(_("invalid option: %s\n"), opt);
96087c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
96097c478bd9Sstevel@tonic-gate 	}
96107c478bd9Sstevel@tonic-gate 
9611ae115bc7Smrj 	/*
9612ae115bc7Smrj 	 * kernel and args are allowed without "=new_value" strings.  All
9613ae115bc7Smrj 	 * others cause errors
9614ae115bc7Smrj 	 */
9615ae115bc7Smrj 	if ((val == NULL) && (optnum != KERNEL_CMD) && (optnum != ARGS_CMD)) {
9616*f64ca102SToomas Soome 		bam_error(_("option has no argument: %s\n"), opt);
9617ae115bc7Smrj 		return (BAM_ERROR);
9618ae115bc7Smrj 	} else if (val != NULL) {
96197c478bd9Sstevel@tonic-gate 		*val = '=';
9620ae115bc7Smrj 	}
9621ae115bc7Smrj 
9622ae115bc7Smrj 	if ((optnum == KERNEL_CMD) || (optnum == ARGS_CMD)) {
9623*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting %s option to %s\n",
9624*f64ca102SToomas Soome 		    fcn, menu_cmds[optnum], val ? val + 1 : "NULL"));
9625eb2bd662Svikram 
9626eb2bd662Svikram 		if (val)
9627eb2bd662Svikram 			rv = set_kernel(mp, optnum, val + 1, buf, sizeof (buf));
9628eb2bd662Svikram 		else
9629eb2bd662Svikram 			rv = get_kernel(mp, optnum, buf, sizeof (buf));
9630ae115bc7Smrj 		if ((rv == BAM_SUCCESS) && (buf[0] != '\0'))
9631ae115bc7Smrj 			(void) printf("%s\n", buf);
9632ae115bc7Smrj 	} else {
9633ae115bc7Smrj 		optval = s_strtol(val + 1);
9634*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting %s option to %s\n", fcn,
9635*f64ca102SToomas Soome 		    menu_cmds[optnum], val + 1));
9636eb2bd662Svikram 		rv = set_global(mp, menu_cmds[optnum], optval);
96377c478bd9Sstevel@tonic-gate 	}
9638eb2bd662Svikram 
9639eb2bd662Svikram 	if (rv == BAM_WRITE || rv == BAM_SUCCESS) {
9640*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
9641eb2bd662Svikram 	} else {
9642*f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
9643eb2bd662Svikram 	}
9644eb2bd662Svikram 
9645eb2bd662Svikram 	return (rv);
9646ae115bc7Smrj }
96477c478bd9Sstevel@tonic-gate 
96487c478bd9Sstevel@tonic-gate /*
96497c478bd9Sstevel@tonic-gate  * The quiet argument suppresses messages. This is used
96507c478bd9Sstevel@tonic-gate  * when invoked in the context of other commands (e.g. list_entry)
96517c478bd9Sstevel@tonic-gate  */
96527c478bd9Sstevel@tonic-gate static error_t
96537c478bd9Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet)
96547c478bd9Sstevel@tonic-gate {
96557c478bd9Sstevel@tonic-gate 	line_t *lp;
96567c478bd9Sstevel@tonic-gate 	char *arg;
96577c478bd9Sstevel@tonic-gate 	int done, ret = BAM_SUCCESS;
96587c478bd9Sstevel@tonic-gate 
96597c478bd9Sstevel@tonic-gate 	assert(mp);
96607c478bd9Sstevel@tonic-gate 	assert(menu_path);
96617c478bd9Sstevel@tonic-gate 	assert(globalcmd);
96627c478bd9Sstevel@tonic-gate 
96637c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
96647c478bd9Sstevel@tonic-gate 		if (!quiet)
9665*f64ca102SToomas Soome 			bam_error(_("menu file not found: %s\n"), menu_path);
96667c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
96677c478bd9Sstevel@tonic-gate 	}
96687c478bd9Sstevel@tonic-gate 
96697c478bd9Sstevel@tonic-gate 	done = 0;
96707c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
96717c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
96727c478bd9Sstevel@tonic-gate 			continue;
96737c478bd9Sstevel@tonic-gate 
96747c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
96757c478bd9Sstevel@tonic-gate 			if (!quiet)
9676*f64ca102SToomas Soome 				bam_error(_("no command at line %d\n"),
9677*f64ca102SToomas Soome 				    lp->lineNum);
96787c478bd9Sstevel@tonic-gate 			continue;
96797c478bd9Sstevel@tonic-gate 		}
96807c478bd9Sstevel@tonic-gate 
96817c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
96827c478bd9Sstevel@tonic-gate 			continue;
96837c478bd9Sstevel@tonic-gate 
96847c478bd9Sstevel@tonic-gate 		/* Found global. Check for duplicates */
96857c478bd9Sstevel@tonic-gate 		if (done && !quiet) {
9686*f64ca102SToomas Soome 			bam_error(_("duplicate command %s at line %d of "
9687*f64ca102SToomas Soome 			    "%sboot/grub/menu.lst\n"), globalcmd,
9688*f64ca102SToomas Soome 			    lp->lineNum, bam_root);
96897c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
96907c478bd9Sstevel@tonic-gate 		}
96917c478bd9Sstevel@tonic-gate 
96927c478bd9Sstevel@tonic-gate 		arg = lp->arg ? lp->arg : "";
9693*f64ca102SToomas Soome 		bam_print(_("%s %s\n"), globalcmd, arg);
96947c478bd9Sstevel@tonic-gate 		done = 1;
96957c478bd9Sstevel@tonic-gate 	}
96967c478bd9Sstevel@tonic-gate 
96977c478bd9Sstevel@tonic-gate 	if (!done && bam_verbose)
9698*f64ca102SToomas Soome 		bam_print(_("no %s entry found\n"), globalcmd);
96997c478bd9Sstevel@tonic-gate 
97007c478bd9Sstevel@tonic-gate 	return (ret);
97017c478bd9Sstevel@tonic-gate }
97027c478bd9Sstevel@tonic-gate 
97037c478bd9Sstevel@tonic-gate static error_t
97047c478bd9Sstevel@tonic-gate menu_write(char *root, menu_t *mp)
97057c478bd9Sstevel@tonic-gate {
9706eb2bd662Svikram 	const char *fcn = "menu_write()";
9707eb2bd662Svikram 
9708*f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered menu_write() for root: <%s>\n", fcn, root));
97097c478bd9Sstevel@tonic-gate 	return (list2file(root, MENU_TMP, GRUB_MENU, mp->start));
97107c478bd9Sstevel@tonic-gate }
97117c478bd9Sstevel@tonic-gate 
9712eb2bd662Svikram void
97137c478bd9Sstevel@tonic-gate line_free(line_t *lp)
97147c478bd9Sstevel@tonic-gate {
97157c478bd9Sstevel@tonic-gate 	if (lp == NULL)
97167c478bd9Sstevel@tonic-gate 		return;
97177c478bd9Sstevel@tonic-gate 
9718eac223ccSWilliam Kucharski 	if (lp->cmd != NULL)
97197c478bd9Sstevel@tonic-gate 		free(lp->cmd);
97207c478bd9Sstevel@tonic-gate 	if (lp->sep)
97217c478bd9Sstevel@tonic-gate 		free(lp->sep);
97227c478bd9Sstevel@tonic-gate 	if (lp->arg)
97237c478bd9Sstevel@tonic-gate 		free(lp->arg);
97247c478bd9Sstevel@tonic-gate 	if (lp->line)
97257c478bd9Sstevel@tonic-gate 		free(lp->line);
97267c478bd9Sstevel@tonic-gate 	free(lp);
97277c478bd9Sstevel@tonic-gate }
97287c478bd9Sstevel@tonic-gate 
97297c478bd9Sstevel@tonic-gate static void
97307c478bd9Sstevel@tonic-gate linelist_free(line_t *start)
97317c478bd9Sstevel@tonic-gate {
97327c478bd9Sstevel@tonic-gate 	line_t *lp;
97337c478bd9Sstevel@tonic-gate 
97347c478bd9Sstevel@tonic-gate 	while (start) {
97357c478bd9Sstevel@tonic-gate 		lp = start;
97367c478bd9Sstevel@tonic-gate 		start = start->next;
97377c478bd9Sstevel@tonic-gate 		line_free(lp);
97387c478bd9Sstevel@tonic-gate 	}
97397c478bd9Sstevel@tonic-gate }
97407c478bd9Sstevel@tonic-gate 
97417c478bd9Sstevel@tonic-gate static void
97427c478bd9Sstevel@tonic-gate filelist_free(filelist_t *flistp)
97437c478bd9Sstevel@tonic-gate {
97447c478bd9Sstevel@tonic-gate 	linelist_free(flistp->head);
97457c478bd9Sstevel@tonic-gate 	flistp->head = NULL;
97467c478bd9Sstevel@tonic-gate 	flistp->tail = NULL;
97477c478bd9Sstevel@tonic-gate }
97487c478bd9Sstevel@tonic-gate 
97497c478bd9Sstevel@tonic-gate static void
97507c478bd9Sstevel@tonic-gate menu_free(menu_t *mp)
97517c478bd9Sstevel@tonic-gate {
97528c1b6884Sszhou 	entry_t *ent, *tmp;
97537c478bd9Sstevel@tonic-gate 	assert(mp);
97547c478bd9Sstevel@tonic-gate 
97557c478bd9Sstevel@tonic-gate 	if (mp->start)
97567c478bd9Sstevel@tonic-gate 		linelist_free(mp->start);
97578c1b6884Sszhou 	ent = mp->entries;
97588c1b6884Sszhou 	while (ent) {
97598c1b6884Sszhou 		tmp = ent;
97608c1b6884Sszhou 		ent = tmp->next;
97618c1b6884Sszhou 		free(tmp);
97628c1b6884Sszhou 	}
97637c478bd9Sstevel@tonic-gate 
97648c1b6884Sszhou 	free(mp);
97657c478bd9Sstevel@tonic-gate }
97667c478bd9Sstevel@tonic-gate 
97677c478bd9Sstevel@tonic-gate /*
97687c478bd9Sstevel@tonic-gate  * Utility routines
97697c478bd9Sstevel@tonic-gate  */
97707c478bd9Sstevel@tonic-gate 
97717c478bd9Sstevel@tonic-gate 
97727c478bd9Sstevel@tonic-gate /*
97737c478bd9Sstevel@tonic-gate  * Returns 0 on success
97747c478bd9Sstevel@tonic-gate  * Any other value indicates an error
97757c478bd9Sstevel@tonic-gate  */
97767c478bd9Sstevel@tonic-gate static int
9777986fd29aSsetje exec_cmd(char *cmdline, filelist_t *flistp)
97787c478bd9Sstevel@tonic-gate {
97797c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
97807c478bd9Sstevel@tonic-gate 	int ret;
97817c478bd9Sstevel@tonic-gate 	FILE *ptr;
97827c478bd9Sstevel@tonic-gate 	sigset_t set;
97837c478bd9Sstevel@tonic-gate 	void (*disp)(int);
97847c478bd9Sstevel@tonic-gate 
97857c478bd9Sstevel@tonic-gate 	/*
97867c478bd9Sstevel@tonic-gate 	 * For security
97877c478bd9Sstevel@tonic-gate 	 * - only absolute paths are allowed
97887c478bd9Sstevel@tonic-gate 	 * - set IFS to space and tab
97897c478bd9Sstevel@tonic-gate 	 */
97907c478bd9Sstevel@tonic-gate 	if (*cmdline != '/') {
9791*f64ca102SToomas Soome 		bam_error(_("path is not absolute: %s\n"), cmdline);
97927c478bd9Sstevel@tonic-gate 		return (-1);
97937c478bd9Sstevel@tonic-gate 	}
97947c478bd9Sstevel@tonic-gate 	(void) putenv("IFS= \t");
97957c478bd9Sstevel@tonic-gate 
97967c478bd9Sstevel@tonic-gate 	/*
97977c478bd9Sstevel@tonic-gate 	 * We may have been exec'ed with SIGCHLD blocked
97987c478bd9Sstevel@tonic-gate 	 * unblock it here
97997c478bd9Sstevel@tonic-gate 	 */
98007c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
98017c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
98027c478bd9Sstevel@tonic-gate 	if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
9803*f64ca102SToomas Soome 		bam_error(_("cannot unblock SIGCHLD: %s\n"), strerror(errno));
98047c478bd9Sstevel@tonic-gate 		return (-1);
98057c478bd9Sstevel@tonic-gate 	}
98067c478bd9Sstevel@tonic-gate 
98077c478bd9Sstevel@tonic-gate 	/*
98087c478bd9Sstevel@tonic-gate 	 * Set SIGCHLD disposition to SIG_DFL for popen/pclose
98097c478bd9Sstevel@tonic-gate 	 */
98107c478bd9Sstevel@tonic-gate 	disp = sigset(SIGCHLD, SIG_DFL);
98117c478bd9Sstevel@tonic-gate 	if (disp == SIG_ERR) {
9812*f64ca102SToomas Soome 		bam_error(_("cannot set SIGCHLD disposition: %s\n"),
9813*f64ca102SToomas Soome 		    strerror(errno));
98147c478bd9Sstevel@tonic-gate 		return (-1);
98157c478bd9Sstevel@tonic-gate 	}
98167c478bd9Sstevel@tonic-gate 	if (disp == SIG_HOLD) {
9817*f64ca102SToomas Soome 		bam_error(_("SIGCHLD signal blocked. Cannot exec: %s\n"),
9818*f64ca102SToomas Soome 		    cmdline);
98197c478bd9Sstevel@tonic-gate 		return (-1);
98207c478bd9Sstevel@tonic-gate 	}
98217c478bd9Sstevel@tonic-gate 
98227c478bd9Sstevel@tonic-gate 	ptr = popen(cmdline, "r");
98237c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
9824*f64ca102SToomas Soome 		bam_error(_("popen failed: %s: %s\n"), cmdline,
9825*f64ca102SToomas Soome 		    strerror(errno));
98267c478bd9Sstevel@tonic-gate 		return (-1);
98277c478bd9Sstevel@tonic-gate 	}
98287c478bd9Sstevel@tonic-gate 
98297c478bd9Sstevel@tonic-gate 	/*
98307c478bd9Sstevel@tonic-gate 	 * If we simply do a pclose() following a popen(), pclose()
98317c478bd9Sstevel@tonic-gate 	 * will close the reader end of the pipe immediately even
98327c478bd9Sstevel@tonic-gate 	 * if the child process has not started/exited. pclose()
98337c478bd9Sstevel@tonic-gate 	 * does wait for cmd to terminate before returning though.
98347c478bd9Sstevel@tonic-gate 	 * When the executed command writes its output to the pipe
98357c478bd9Sstevel@tonic-gate 	 * there is no reader process and the command dies with
98367c478bd9Sstevel@tonic-gate 	 * SIGPIPE. To avoid this we read repeatedly until read
98377c478bd9Sstevel@tonic-gate 	 * terminates with EOF. This indicates that the command
98387c478bd9Sstevel@tonic-gate 	 * (writer) has closed the pipe and we can safely do a
98397c478bd9Sstevel@tonic-gate 	 * pclose().
98407c478bd9Sstevel@tonic-gate 	 *
98417c478bd9Sstevel@tonic-gate 	 * Since pclose() does wait for the command to exit,
98427c478bd9Sstevel@tonic-gate 	 * we can safely reap the exit status of the command
98437c478bd9Sstevel@tonic-gate 	 * from the value returned by pclose()
98447c478bd9Sstevel@tonic-gate 	 */
9845986fd29aSsetje 	while (s_fgets(buf, sizeof (buf), ptr) != NULL) {
9846986fd29aSsetje 		if (flistp == NULL) {
9847986fd29aSsetje 			/* s_fgets strips newlines, so insert them at the end */
9848*f64ca102SToomas Soome 			bam_print(_("%s\n"), buf);
9849986fd29aSsetje 		} else {
9850986fd29aSsetje 			append_to_flist(flistp, buf);
98517c478bd9Sstevel@tonic-gate 		}
98527c478bd9Sstevel@tonic-gate 	}
98537c478bd9Sstevel@tonic-gate 
98547c478bd9Sstevel@tonic-gate 	ret = pclose(ptr);
98557c478bd9Sstevel@tonic-gate 	if (ret == -1) {
9856*f64ca102SToomas Soome 		bam_error(_("pclose failed: %s: %s\n"), cmdline,
9857*f64ca102SToomas Soome 		    strerror(errno));
98587c478bd9Sstevel@tonic-gate 		return (-1);
98597c478bd9Sstevel@tonic-gate 	}
98607c478bd9Sstevel@tonic-gate 
98617c478bd9Sstevel@tonic-gate 	if (WIFEXITED(ret)) {
98627c478bd9Sstevel@tonic-gate 		return (WEXITSTATUS(ret));
98637c478bd9Sstevel@tonic-gate 	} else {
9864*f64ca102SToomas Soome 		bam_error(_("command terminated abnormally: %s: %d\n"),
9865*f64ca102SToomas Soome 		    cmdline, ret);
98667c478bd9Sstevel@tonic-gate 		return (-1);
98677c478bd9Sstevel@tonic-gate 	}
98687c478bd9Sstevel@tonic-gate }
98697c478bd9Sstevel@tonic-gate 
98707c478bd9Sstevel@tonic-gate /*
98717c478bd9Sstevel@tonic-gate  * Since this function returns -1 on error
98727c478bd9Sstevel@tonic-gate  * it cannot be used to convert -1. However,
98737c478bd9Sstevel@tonic-gate  * that is sufficient for what we need.
98747c478bd9Sstevel@tonic-gate  */
98757c478bd9Sstevel@tonic-gate static long
98767c478bd9Sstevel@tonic-gate s_strtol(char *str)
98777c478bd9Sstevel@tonic-gate {
98787c478bd9Sstevel@tonic-gate 	long l;
98797c478bd9Sstevel@tonic-gate 	char *res = NULL;
98807c478bd9Sstevel@tonic-gate 
98817c478bd9Sstevel@tonic-gate 	if (str == NULL) {
98827c478bd9Sstevel@tonic-gate 		return (-1);
98837c478bd9Sstevel@tonic-gate 	}
98847c478bd9Sstevel@tonic-gate 
98857c478bd9Sstevel@tonic-gate 	errno = 0;
98867c478bd9Sstevel@tonic-gate 	l = strtol(str, &res, 10);
98877c478bd9Sstevel@tonic-gate 	if (errno || *res != '\0') {
98887c478bd9Sstevel@tonic-gate 		return (-1);
98897c478bd9Sstevel@tonic-gate 	}
98907c478bd9Sstevel@tonic-gate 
98917c478bd9Sstevel@tonic-gate 	return (l);
98927c478bd9Sstevel@tonic-gate }
98937c478bd9Sstevel@tonic-gate 
98947c478bd9Sstevel@tonic-gate /*
98957c478bd9Sstevel@tonic-gate  * Wrapper around fputs, that adds a newline (since fputs doesn't)
98967c478bd9Sstevel@tonic-gate  */
98977c478bd9Sstevel@tonic-gate static int
98987c478bd9Sstevel@tonic-gate s_fputs(char *str, FILE *fp)
98997c478bd9Sstevel@tonic-gate {
99007c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
99017c478bd9Sstevel@tonic-gate 
99027c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s\n", str);
99037c478bd9Sstevel@tonic-gate 	return (fputs(linebuf, fp));
99047c478bd9Sstevel@tonic-gate }
99057c478bd9Sstevel@tonic-gate 
99067c478bd9Sstevel@tonic-gate /*
99077c478bd9Sstevel@tonic-gate  * Wrapper around fgets, that strips newlines returned by fgets
99087c478bd9Sstevel@tonic-gate  */
9909ae115bc7Smrj char *
99107c478bd9Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp)
99117c478bd9Sstevel@tonic-gate {
99127c478bd9Sstevel@tonic-gate 	int n;
99137c478bd9Sstevel@tonic-gate 
99147c478bd9Sstevel@tonic-gate 	buf = fgets(buf, buflen, fp);
99157c478bd9Sstevel@tonic-gate 	if (buf) {
99167c478bd9Sstevel@tonic-gate 		n = strlen(buf);
99177c478bd9Sstevel@tonic-gate 		if (n == buflen - 1 && buf[n-1] != '\n')
9918*f64ca102SToomas Soome 			bam_error(_("the following line is too long "
9919*f64ca102SToomas Soome 			    "(> %d chars)\n\t%s\n"), buflen - 1, buf);
99207c478bd9Sstevel@tonic-gate 		buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1];
99217c478bd9Sstevel@tonic-gate 	}
99227c478bd9Sstevel@tonic-gate 
99237c478bd9Sstevel@tonic-gate 	return (buf);
99247c478bd9Sstevel@tonic-gate }
99257c478bd9Sstevel@tonic-gate 
9926ae115bc7Smrj void *
99277c478bd9Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz)
99287c478bd9Sstevel@tonic-gate {
99297c478bd9Sstevel@tonic-gate 	void *ptr;
99307c478bd9Sstevel@tonic-gate 
99317c478bd9Sstevel@tonic-gate 	ptr = calloc(nelem, sz);
99327c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
9933*f64ca102SToomas Soome 		bam_error(_("could not allocate memory: size = %u\n"),
9934*f64ca102SToomas Soome 		    nelem*sz);
99357c478bd9Sstevel@tonic-gate 		bam_exit(1);
99367c478bd9Sstevel@tonic-gate 	}
99377c478bd9Sstevel@tonic-gate 	return (ptr);
99387c478bd9Sstevel@tonic-gate }
99397c478bd9Sstevel@tonic-gate 
9940ae115bc7Smrj void *
9941ae115bc7Smrj s_realloc(void *ptr, size_t sz)
9942ae115bc7Smrj {
9943ae115bc7Smrj 	ptr = realloc(ptr, sz);
9944ae115bc7Smrj 	if (ptr == NULL) {
9945*f64ca102SToomas Soome 		bam_error(_("could not allocate memory: size = %u\n"), sz);
9946ae115bc7Smrj 		bam_exit(1);
9947ae115bc7Smrj 	}
9948ae115bc7Smrj 	return (ptr);
9949ae115bc7Smrj }
9950ae115bc7Smrj 
9951eb2bd662Svikram char *
99527c478bd9Sstevel@tonic-gate s_strdup(char *str)
99537c478bd9Sstevel@tonic-gate {
99547c478bd9Sstevel@tonic-gate 	char *ptr;
99557c478bd9Sstevel@tonic-gate 
99567c478bd9Sstevel@tonic-gate 	if (str == NULL)
99577c478bd9Sstevel@tonic-gate 		return (NULL);
99587c478bd9Sstevel@tonic-gate 
99597c478bd9Sstevel@tonic-gate 	ptr = strdup(str);
99607c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
9961*f64ca102SToomas Soome 		bam_error(_("could not allocate memory: size = %u\n"),
9962*f64ca102SToomas Soome 		    strlen(str) + 1);
99637c478bd9Sstevel@tonic-gate 		bam_exit(1);
99647c478bd9Sstevel@tonic-gate 	}
99657c478bd9Sstevel@tonic-gate 	return (ptr);
99667c478bd9Sstevel@tonic-gate }
99677c478bd9Sstevel@tonic-gate 
99687c478bd9Sstevel@tonic-gate /*
99697c478bd9Sstevel@tonic-gate  * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients)
99707c478bd9Sstevel@tonic-gate  * Returns 0 otherwise
99717c478bd9Sstevel@tonic-gate  */
99727c478bd9Sstevel@tonic-gate static int
99737c478bd9Sstevel@tonic-gate is_amd64(void)
99747c478bd9Sstevel@tonic-gate {
99757c478bd9Sstevel@tonic-gate 	static int amd64 = -1;
99767c478bd9Sstevel@tonic-gate 	char isabuf[257];	/* from sysinfo(2) manpage */
99777c478bd9Sstevel@tonic-gate 
99787c478bd9Sstevel@tonic-gate 	if (amd64 != -1)
99797c478bd9Sstevel@tonic-gate 		return (amd64);
99807c478bd9Sstevel@tonic-gate 
9981d876c67dSjg 	if (bam_alt_platform) {
9982d876c67dSjg 		if (strcmp(bam_platform, "i86pc") == 0) {
99837c478bd9Sstevel@tonic-gate 			amd64 = 1;		/* diskless server */
9984d876c67dSjg 		}
9985d876c67dSjg 	} else {
9986d876c67dSjg 		if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 &&
9987d876c67dSjg 		    strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0) {
9988d876c67dSjg 			amd64 = 1;
9989d876c67dSjg 		} else if (strstr(isabuf, "i386") == NULL) {
9990d876c67dSjg 			amd64 = 1;		/* diskless server */
9991d876c67dSjg 		}
9992d876c67dSjg 	}
9993d876c67dSjg 	if (amd64 == -1)
99947c478bd9Sstevel@tonic-gate 		amd64 = 0;
99957c478bd9Sstevel@tonic-gate 
99967c478bd9Sstevel@tonic-gate 	return (amd64);
99977c478bd9Sstevel@tonic-gate }
99987c478bd9Sstevel@tonic-gate 
999979755401Ssetje static char *
1000079755401Ssetje get_machine(void)
10001986fd29aSsetje {
1000279755401Ssetje 	static int cached = -1;
1000379755401Ssetje 	static char mbuf[257];	/* from sysinfo(2) manpage */
10004986fd29aSsetje 
1000579755401Ssetje 	if (cached == 0)
1000679755401Ssetje 		return (mbuf);
10007d876c67dSjg 
10008d876c67dSjg 	if (bam_alt_platform) {
1000979755401Ssetje 		return (bam_platform);
10010d876c67dSjg 	} else {
1001179755401Ssetje 		if (sysinfo(SI_MACHINE, mbuf, sizeof (mbuf)) > 0) {
1001279755401Ssetje 			cached = 1;
10013d876c67dSjg 		}
10014d876c67dSjg 	}
1001579755401Ssetje 	if (cached == -1) {
1001679755401Ssetje 		mbuf[0] = '\0';
1001779755401Ssetje 		cached = 0;
10018986fd29aSsetje 	}
10019986fd29aSsetje 
1002079755401Ssetje 	return (mbuf);
10021986fd29aSsetje }
10022986fd29aSsetje 
10023eb2bd662Svikram int
10024eb2bd662Svikram is_sparc(void)
10025eb2bd662Svikram {
1002679755401Ssetje 	static int issparc = -1;
1002779755401Ssetje 	char mbuf[257];	/* from sysinfo(2) manpage */
1002879755401Ssetje 
1002979755401Ssetje 	if (issparc != -1)
1003079755401Ssetje 		return (issparc);
1003179755401Ssetje 
1003279755401Ssetje 	if (bam_alt_platform) {
1003379755401Ssetje 		if (strncmp(bam_platform, "sun4", 4) == 0) {
1003479755401Ssetje 			issparc = 1;
1003579755401Ssetje 		}
1003679755401Ssetje 	} else {
1003779755401Ssetje 		if (sysinfo(SI_ARCHITECTURE, mbuf, sizeof (mbuf)) > 0 &&
10038d7d0f93bSjg 		    strcmp(mbuf, "sparc") == 0) {
1003979755401Ssetje 			issparc = 1;
1004079755401Ssetje 		}
10041d7d0f93bSjg 	}
10042d7d0f93bSjg 	if (issparc == -1)
10043d7d0f93bSjg 		issparc = 0;
1004479755401Ssetje 
1004579755401Ssetje 	return (issparc);
10046eb2bd662Svikram }
10047986fd29aSsetje 
100487c478bd9Sstevel@tonic-gate static void
100497c478bd9Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s)
100507c478bd9Sstevel@tonic-gate {
100517c478bd9Sstevel@tonic-gate 	line_t *lp;
100527c478bd9Sstevel@tonic-gate 
100537c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
100547c478bd9Sstevel@tonic-gate 	lp->line = s_strdup(s);
100557c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL)
100567c478bd9Sstevel@tonic-gate 		flistp->head = lp;
100577c478bd9Sstevel@tonic-gate 	else
100587c478bd9Sstevel@tonic-gate 		flistp->tail->next = lp;
100597c478bd9Sstevel@tonic-gate 	flistp->tail = lp;
100607c478bd9Sstevel@tonic-gate }
100612449e17fSsherrym 
1006279c28b70SToomas Soome #if !defined(_OBP)
100632449e17fSsherrym 
100642449e17fSsherrym UCODE_VENDORS;
100652449e17fSsherrym 
100662449e17fSsherrym /*ARGSUSED*/
100672449e17fSsherrym static void
100682449e17fSsherrym ucode_install(char *root)
100692449e17fSsherrym {
100702449e17fSsherrym 	int i;
100712449e17fSsherrym 
100722449e17fSsherrym 	for (i = 0; ucode_vendors[i].filestr != NULL; i++) {
100732449e17fSsherrym 		int cmd_len = PATH_MAX + 256;
100742449e17fSsherrym 		char cmd[PATH_MAX + 256];
100752449e17fSsherrym 		char file[PATH_MAX];
100762449e17fSsherrym 		char timestamp[PATH_MAX];
100772449e17fSsherrym 		struct stat fstatus, tstatus;
100782449e17fSsherrym 		struct utimbuf u_times;
100792449e17fSsherrym 
10080adc586deSMark Johnson 		(void) snprintf(file, PATH_MAX, "%s/%s/%s-ucode.%s",
10081adc586deSMark Johnson 		    bam_root, UCODE_INSTALL_PATH, ucode_vendors[i].filestr,
10082adc586deSMark Johnson 		    ucode_vendors[i].extstr);
100832449e17fSsherrym 
100842449e17fSsherrym 		if (stat(file, &fstatus) != 0 || !(S_ISREG(fstatus.st_mode)))
100852449e17fSsherrym 			continue;
100862449e17fSsherrym 
100872449e17fSsherrym 		(void) snprintf(timestamp, PATH_MAX, "%s.ts", file);
100882449e17fSsherrym 
100892449e17fSsherrym 		if (stat(timestamp, &tstatus) == 0 &&
100902449e17fSsherrym 		    fstatus.st_mtime <= tstatus.st_mtime)
100912449e17fSsherrym 			continue;
100922449e17fSsherrym 
100932449e17fSsherrym 		(void) snprintf(cmd, cmd_len, "/usr/sbin/ucodeadm -i -R "
100942449e17fSsherrym 		    "%s/%s/%s %s > /dev/null 2>&1", bam_root,
100952449e17fSsherrym 		    UCODE_INSTALL_PATH, ucode_vendors[i].vendorstr, file);
100962449e17fSsherrym 		if (system(cmd) != 0)
100972449e17fSsherrym 			return;
100982449e17fSsherrym 
100992449e17fSsherrym 		if (creat(timestamp, S_IRUSR | S_IWUSR) == -1)
101002449e17fSsherrym 			return;
101012449e17fSsherrym 
101022449e17fSsherrym 		u_times.actime = fstatus.st_atime;
101032449e17fSsherrym 		u_times.modtime = fstatus.st_mtime;
101042449e17fSsherrym 		(void) utime(timestamp, &u_times);
101052449e17fSsherrym 	}
101062449e17fSsherrym }
101072449e17fSsherrym #endif
10108