xref: /titanic_52/usr/src/cmd/boot/bootadm/bootadm.c (revision 71668a2f05bc8eb5e0c1f7eaf23e10cc7d40c90b)
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>
71d0911063SToomas Soome #include <deflt.h>
72e998e519SSheshadri Vasudevan #ifdef i386
73e998e519SSheshadri Vasudevan #include <libfdisk.h>
74e998e519SSheshadri Vasudevan #endif
75986fd29aSsetje 
7679c28b70SToomas Soome #if !defined(_OBP)
772449e17fSsherrym #include <sys/ucode.h>
782449e17fSsherrym #endif
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #include <pwd.h>
817c478bd9Sstevel@tonic-gate #include <grp.h>
827c478bd9Sstevel@tonic-gate #include <device_info.h>
83eb2bd662Svikram #include <sys/vtoc.h>
84eb2bd662Svikram #include <sys/efi_partition.h>
8589c3ee43SGangadhar Mylapuram #include <regex.h>
867c478bd9Sstevel@tonic-gate #include <locale.h>
87c7c0ceafSToomas Soome #include <sys/mkdev.h>
887c478bd9Sstevel@tonic-gate 
89ae115bc7Smrj #include "bootadm.h"
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
927c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
937c478bd9Sstevel@tonic-gate #endif	/* TEXT_DOMAIN */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /* Type definitions */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /* Primary subcmds */
987c478bd9Sstevel@tonic-gate typedef enum {
997c478bd9Sstevel@tonic-gate 	BAM_MENU = 3,
100c7c0ceafSToomas Soome 	BAM_ARCHIVE,
101c7c0ceafSToomas Soome 	BAM_INSTALL
1027c478bd9Sstevel@tonic-gate } subcmd_t;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #define	LINE_INIT	0	/* lineNum initial value */
1057c478bd9Sstevel@tonic-gate #define	ENTRY_INIT	-1	/* entryNum initial value */
1067c478bd9Sstevel@tonic-gate #define	ALL_ENTRIES	-2	/* selects all boot entries */
1077c478bd9Sstevel@tonic-gate 
1081a902ef8SHans Rosenfeld #define	PARTNO_NOTFOUND -1	/* Solaris partition not found */
1091a902ef8SHans Rosenfeld #define	PARTNO_EFI	-2	/* EFI partition table found */
1101a902ef8SHans Rosenfeld 
1117c478bd9Sstevel@tonic-gate #define	GRUB_DIR		"/boot/grub"
112963390b4Svikram #define	GRUB_STAGE2		GRUB_DIR "/stage2"
1137c478bd9Sstevel@tonic-gate #define	GRUB_MENU		"/boot/grub/menu.lst"
1147c478bd9Sstevel@tonic-gate #define	MENU_TMP		"/boot/grub/menu.lst.tmp"
115963390b4Svikram #define	GRUB_BACKUP_MENU	"/etc/lu/GRUB_backup_menu"
1163e58d0cbSToomas Soome #define	RAMDISK_SPECIAL		"/devices/ramdisk"
11740541d5dSvikram #define	STUBBOOT		"/stubboot"
118eb2bd662Svikram #define	MULTIBOOT		"/platform/i86pc/multiboot"
119eb2bd662Svikram #define	GRUBSIGN_DIR		"/boot/grub/bootsign"
120eb2bd662Svikram #define	GRUBSIGN_BACKUP		"/etc/bootsign"
121eb2bd662Svikram #define	GRUBSIGN_UFS_PREFIX	"rootfs"
122eb2bd662Svikram #define	GRUBSIGN_ZFS_PREFIX	"pool_"
123eb2bd662Svikram #define	GRUBSIGN_LU_PREFIX	"BE_"
124eb2bd662Svikram #define	UFS_SIGNATURE_LIST	"/var/run/grub_ufs_signatures"
125eb2bd662Svikram #define	ZFS_LEGACY_MNTPT	"/tmp/bootadm_mnt_zfs_legacy"
126eb2bd662Svikram 
127d0911063SToomas Soome /* BE defaults */
128d0911063SToomas Soome #define	BE_DEFAULTS		"/etc/default/be"
129d0911063SToomas Soome #define	BE_DFLT_BE_HAS_GRUB	"BE_HAS_GRUB="
130d0911063SToomas Soome 
131eb2bd662Svikram #define	BOOTADM_RDONLY_TEST	"BOOTADM_RDONLY_TEST"
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /* lock related */
1347c478bd9Sstevel@tonic-gate #define	BAM_LOCK_FILE		"/var/run/bootadm.lock"
1357c478bd9Sstevel@tonic-gate #define	LOCK_FILE_PERMS		(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
1367c478bd9Sstevel@tonic-gate 
137986fd29aSsetje #define	CREATE_RAMDISK		"boot/solaris/bin/create_ramdisk"
138986fd29aSsetje #define	CREATE_DISKMAP		"boot/solaris/bin/create_diskmap"
139986fd29aSsetje #define	EXTRACT_BOOT_FILELIST	"boot/solaris/bin/extract_boot_filelist"
1407c478bd9Sstevel@tonic-gate #define	GRUBDISK_MAP		"/var/run/solaris_grubdisk.map"
1417c478bd9Sstevel@tonic-gate 
142b610f78eSvikram #define	GRUB_slice		"/etc/lu/GRUB_slice"
143b610f78eSvikram #define	GRUB_root		"/etc/lu/GRUB_root"
144fbac2b2bSvikram #define	GRUB_fdisk		"/etc/lu/GRUB_fdisk"
145fbac2b2bSvikram #define	GRUB_fdisk_target	"/etc/lu/GRUB_fdisk_target"
146963390b4Svikram #define	FINDROOT_INSTALLGRUB	"/etc/lu/installgrub.findroot"
147963390b4Svikram #define	LULIB			"/usr/lib/lu/lulib"
148963390b4Svikram #define	LULIB_PROPAGATE_FILE	"lulib_propagate_file"
149963390b4Svikram #define	CKSUM			"/usr/bin/cksum"
150963390b4Svikram #define	LU_MENU_CKSUM		"/etc/lu/menu.cksum"
151963390b4Svikram #define	BOOTADM			"/sbin/bootadm"
152b610f78eSvikram 
153b610f78eSvikram #define	INSTALLGRUB		"/sbin/installgrub"
154b610f78eSvikram #define	STAGE1			"/boot/grub/stage1"
155b610f78eSvikram #define	STAGE2			"/boot/grub/stage2"
156b610f78eSvikram 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * Default file attributes
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_MODE	0644	/* default permissions */
1617c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_UID		0	/* user root */
1627c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_GID		3	/* group sys */
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate  * Menu related
1667c478bd9Sstevel@tonic-gate  * menu_cmd_t and menu_cmds must be kept in sync
1677c478bd9Sstevel@tonic-gate  */
168ae115bc7Smrj char *menu_cmds[] = {
1697c478bd9Sstevel@tonic-gate 	"default",	/* DEFAULT_CMD */
1707c478bd9Sstevel@tonic-gate 	"timeout",	/* TIMEOUT_CMD */
1717c478bd9Sstevel@tonic-gate 	"title",	/* TITLE_CMD */
1727c478bd9Sstevel@tonic-gate 	"root",		/* ROOT_CMD */
1737c478bd9Sstevel@tonic-gate 	"kernel",	/* KERNEL_CMD */
174ae115bc7Smrj 	"kernel$",	/* KERNEL_DOLLAR_CMD */
1757c478bd9Sstevel@tonic-gate 	"module",	/* MODULE_CMD */
176ae115bc7Smrj 	"module$",	/* MODULE_DOLLAR_CMD */
1777c478bd9Sstevel@tonic-gate 	" ",		/* SEP_CMD */
1787c478bd9Sstevel@tonic-gate 	"#",		/* COMMENT_CMD */
179ae115bc7Smrj 	"chainloader",	/* CHAINLOADER_CMD */
180ae115bc7Smrj 	"args",		/* ARGS_CMD */
181eb2bd662Svikram 	"findroot",	/* FINDROOT_CMD */
18244da779fSWilliam Kucharski 	"bootfs",	/* BOOTFS_CMD */
1837c478bd9Sstevel@tonic-gate 	NULL
1847c478bd9Sstevel@tonic-gate };
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate #define	OPT_ENTRY_NUM	"entry"
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
189eb2bd662Svikram  * exec_cmd related
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate typedef struct {
1927c478bd9Sstevel@tonic-gate 	line_t *head;
1937c478bd9Sstevel@tonic-gate 	line_t *tail;
1947c478bd9Sstevel@tonic-gate } filelist_t;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate #define	BOOT_FILE_LIST	"boot/solaris/filelist.ramdisk"
1977c478bd9Sstevel@tonic-gate #define	ETC_FILE_LIST	"etc/boot/solaris/filelist.ramdisk"
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate #define	FILE_STAT	"boot/solaris/filestat.ramdisk"
2007c478bd9Sstevel@tonic-gate #define	FILE_STAT_TMP	"boot/solaris/filestat.ramdisk.tmp"
2017c478bd9Sstevel@tonic-gate #define	DIR_PERMS	(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
2027c478bd9Sstevel@tonic-gate #define	FILE_STAT_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
2037c478bd9Sstevel@tonic-gate 
2044a9df875SEnrico Perla - Sun Microsystems #define	FILE_STAT_TIMESTAMP	"boot/solaris/timestamp.cache"
2054a9df875SEnrico Perla - Sun Microsystems 
2067c478bd9Sstevel@tonic-gate /* Globals */
207ae115bc7Smrj int bam_verbose;
208ae115bc7Smrj int bam_force;
209eb2bd662Svikram int bam_debug;
2107c478bd9Sstevel@tonic-gate static char *prog;
2117c478bd9Sstevel@tonic-gate static subcmd_t bam_cmd;
212f64ca102SToomas Soome char *bam_root;
213f64ca102SToomas Soome int bam_rootlen;
2147c478bd9Sstevel@tonic-gate static int bam_root_readonly;
215f64ca102SToomas Soome int bam_alt_root;
21648847494SEnrico Perla - Sun Microsystems static int bam_extend = 0;
21748847494SEnrico Perla - Sun Microsystems static int bam_purge = 0;
2187c478bd9Sstevel@tonic-gate static char *bam_subcmd;
2197c478bd9Sstevel@tonic-gate static char *bam_opt;
2207c478bd9Sstevel@tonic-gate static char **bam_argv;
221c7c0ceafSToomas Soome static char *bam_pool;
2227c478bd9Sstevel@tonic-gate static int bam_argc;
2237c478bd9Sstevel@tonic-gate static int bam_check;
2245eea6091SEnrico Perla - Sun Microsystems static int bam_saved_check;
2257c478bd9Sstevel@tonic-gate static int bam_smf_check;
2267c478bd9Sstevel@tonic-gate static int bam_lock_fd = -1;
227e7cbe64fSgw25295 static int bam_zfs;
228c7c0ceafSToomas Soome static int bam_mbr;
229f64ca102SToomas Soome char rootbuf[PATH_MAX] = "/";
230b610f78eSvikram static int bam_update_all;
231d876c67dSjg static int bam_alt_platform;
232d876c67dSjg static char *bam_platform;
233cedc7e57SEnrico Perla - Sun Microsystems static char *bam_home_env = NULL;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate /* function prototypes */
236986fd29aSsetje static void parse_args_internal(int, char *[]);
237986fd29aSsetje static void parse_args(int, char *argv[]);
238986fd29aSsetje static error_t bam_menu(char *, char *, int, char *[]);
239c7c0ceafSToomas Soome static error_t bam_install(char *, char *);
240986fd29aSsetje static error_t bam_archive(char *, char *);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate static void bam_lock(void);
2437c478bd9Sstevel@tonic-gate static void bam_unlock(void);
2447c478bd9Sstevel@tonic-gate 
245986fd29aSsetje static int exec_cmd(char *, filelist_t *);
246986fd29aSsetje static error_t read_globals(menu_t *, char *, char *, int);
247eb2bd662Svikram static int menu_on_bootdisk(char *os_root, char *menu_root);
248986fd29aSsetje static menu_t *menu_read(char *);
249986fd29aSsetje static error_t menu_write(char *, menu_t *);
250986fd29aSsetje static void linelist_free(line_t *);
251986fd29aSsetje static void menu_free(menu_t *);
252986fd29aSsetje static void filelist_free(filelist_t *);
253986fd29aSsetje static error_t list2file(char *, char *, char *, line_t *);
254986fd29aSsetje static error_t list_entry(menu_t *, char *, char *);
25544da779fSWilliam Kucharski static error_t list_setting(menu_t *, char *, char *);
256986fd29aSsetje static error_t delete_all_entries(menu_t *, char *, char *);
257eb2bd662Svikram static error_t update_entry(menu_t *mp, char *menu_root, char *opt);
258eb2bd662Svikram static error_t update_temp(menu_t *mp, char *dummy, char *opt);
2597c478bd9Sstevel@tonic-gate 
260c7c0ceafSToomas Soome static error_t install_bootloader(void);
261986fd29aSsetje static error_t update_archive(char *, char *);
262986fd29aSsetje static error_t list_archive(char *, char *);
263986fd29aSsetje static error_t update_all(char *, char *);
264986fd29aSsetje static error_t read_list(char *, filelist_t *);
265986fd29aSsetje static error_t set_option(menu_t *, char *, char *);
266986fd29aSsetje static error_t set_kernel(menu_t *, menu_cmd_t, char *, char *, size_t);
267eb2bd662Svikram static error_t get_kernel(menu_t *, menu_cmd_t, char *, size_t);
268986fd29aSsetje static char *expand_path(const char *);
2697c478bd9Sstevel@tonic-gate 
270986fd29aSsetje static long s_strtol(char *);
271986fd29aSsetje static int s_fputs(char *, FILE *);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate static int is_amd64(void);
27479755401Ssetje static char *get_machine(void);
2757c478bd9Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *);
276eb2bd662Svikram static int ufs_add_to_sign_list(char *sign);
277963390b4Svikram static error_t synchronize_BE_menu(void);
2787c478bd9Sstevel@tonic-gate 
27979c28b70SToomas Soome #if !defined(_OBP)
2802449e17fSsherrym static void ucode_install();
2812449e17fSsherrym #endif
2822449e17fSsherrym 
2837c478bd9Sstevel@tonic-gate /* Menu related sub commands */
2847c478bd9Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = {
285eb2bd662Svikram 	"set_option",		OPT_ABSENT,	set_option, 0,	/* PUB */
2861a97e40eSvikram 	"list_entry",		OPT_OPTIONAL,	list_entry, 1,	/* PUB */
2871a97e40eSvikram 	"delete_all_entries",	OPT_ABSENT,	delete_all_entries, 0, /* PVT */
2881a97e40eSvikram 	"update_entry",		OPT_REQ,	update_entry, 0, /* menu */
2891a97e40eSvikram 	"update_temp",		OPT_OPTIONAL,	update_temp, 0,	/* reboot */
290ae115bc7Smrj 	"upgrade",		OPT_ABSENT,	upgrade_menu, 0, /* menu */
29144da779fSWilliam Kucharski 	"list_setting",		OPT_OPTIONAL,	list_setting, 1, /* menu */
29244da779fSWilliam Kucharski 	"disable_hypervisor",	OPT_ABSENT,	cvt_to_metal, 0, /* menu */
29344da779fSWilliam Kucharski 	"enable_hypervisor",	OPT_ABSENT,	cvt_to_hyper, 0, /* menu */
2941a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
2957c478bd9Sstevel@tonic-gate };
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate /* Archive related sub commands */
2987c478bd9Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = {
2991a97e40eSvikram 	"update",		OPT_ABSENT,	update_archive, 0, /* PUB */
3001a97e40eSvikram 	"update_all",		OPT_ABSENT,	update_all, 0,	/* PVT */
3011a97e40eSvikram 	"list",			OPT_OPTIONAL,	list_archive, 1, /* PUB */
3021a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
3037c478bd9Sstevel@tonic-gate };
3047c478bd9Sstevel@tonic-gate 
305c7c0ceafSToomas Soome /* Install related sub commands */
306c7c0ceafSToomas Soome static subcmd_defn_t inst_subcmds[] = {
307c7c0ceafSToomas Soome 	"install_bootloader",	OPT_ABSENT,	install_bootloader, 0, /* PUB */
308c7c0ceafSToomas Soome 	NULL,			0,		NULL, 0	/* must be last */
309c7c0ceafSToomas Soome };
310c7c0ceafSToomas Soome 
31148847494SEnrico Perla - Sun Microsystems enum dircache_copy_opt {
31248847494SEnrico Perla - Sun Microsystems 	FILE32 = 0,
31348847494SEnrico Perla - Sun Microsystems 	FILE64,
31448847494SEnrico Perla - Sun Microsystems 	CACHEDIR_NUM
31548847494SEnrico Perla - Sun Microsystems };
31648847494SEnrico Perla - Sun Microsystems 
31748847494SEnrico Perla - Sun Microsystems /*
31848847494SEnrico Perla - Sun Microsystems  * Directory specific flags:
31948847494SEnrico Perla - Sun Microsystems  * NEED_UPDATE : the specified archive needs to be updated
32048847494SEnrico Perla - Sun Microsystems  * NO_MULTI : don't extend the specified archive, but recreate it
32148847494SEnrico Perla - Sun Microsystems  */
32248847494SEnrico Perla - Sun Microsystems #define	NEED_UPDATE		0x00000001
32348847494SEnrico Perla - Sun Microsystems #define	NO_MULTI		0x00000002
32448847494SEnrico Perla - Sun Microsystems 
32548847494SEnrico Perla - Sun Microsystems #define	set_dir_flag(id, f)	(walk_arg.dirinfo[id].flags |= f)
32648847494SEnrico Perla - Sun Microsystems #define	unset_dir_flag(id, f)	(walk_arg.dirinfo[id].flags &= ~f)
32748847494SEnrico Perla - Sun Microsystems #define	is_dir_flag_on(id, f)	(walk_arg.dirinfo[id].flags & f ? 1 : 0)
32848847494SEnrico Perla - Sun Microsystems 
32948847494SEnrico Perla - Sun Microsystems #define	get_cachedir(id)	(walk_arg.dirinfo[id].cdir_path)
33048847494SEnrico Perla - Sun Microsystems #define	get_updatedir(id)	(walk_arg.dirinfo[id].update_path)
33148847494SEnrico Perla - Sun Microsystems #define	get_count(id)		(walk_arg.dirinfo[id].count)
33248847494SEnrico Perla - Sun Microsystems #define	has_cachedir(id)	(walk_arg.dirinfo[id].has_dir)
33348847494SEnrico Perla - Sun Microsystems #define	set_dir_present(id)	(walk_arg.dirinfo[id].has_dir = 1)
33448847494SEnrico Perla - Sun Microsystems 
33548847494SEnrico Perla - Sun Microsystems /*
33648847494SEnrico Perla - Sun Microsystems  * dirinfo_t (specific cache directory information):
33748847494SEnrico Perla - Sun Microsystems  * cdir_path:   path to the archive cache directory
33848847494SEnrico Perla - Sun Microsystems  * update_path: path to the update directory (contains the files that will be
33948847494SEnrico Perla - Sun Microsystems  *              used to extend the archive)
34048847494SEnrico Perla - Sun Microsystems  * has_dir:	the specified cache directory is active
34148847494SEnrico Perla - Sun Microsystems  * count:	the number of files to update
34248847494SEnrico Perla - Sun Microsystems  * flags:	directory specific flags
34348847494SEnrico Perla - Sun Microsystems  */
34448847494SEnrico Perla - Sun Microsystems typedef struct _dirinfo {
34548847494SEnrico Perla - Sun Microsystems 	char	cdir_path[PATH_MAX];
34648847494SEnrico Perla - Sun Microsystems 	char	update_path[PATH_MAX];
34748847494SEnrico Perla - Sun Microsystems 	int	has_dir;
34848847494SEnrico Perla - Sun Microsystems 	int	count;
34948847494SEnrico Perla - Sun Microsystems 	int	flags;
35048847494SEnrico Perla - Sun Microsystems } dirinfo_t;
35148847494SEnrico Perla - Sun Microsystems 
35248847494SEnrico Perla - Sun Microsystems /*
35348847494SEnrico Perla - Sun Microsystems  * Update flags:
35448847494SEnrico Perla - Sun Microsystems  * NEED_CACHE_DIR : cache directory is missing and needs to be created
35548847494SEnrico Perla - Sun Microsystems  * IS_SPARC_TARGET : the target mountpoint is a SPARC environment
35648847494SEnrico Perla - Sun Microsystems  * UPDATE_ERROR : an error occourred while traversing the list of files
35748847494SEnrico Perla - Sun Microsystems  * RDONLY_FSCHK : the target filesystem is read-only
35848847494SEnrico Perla - Sun Microsystems  * RAMDSK_FSCHK : the target filesystem is on a ramdisk
35948847494SEnrico Perla - Sun Microsystems  */
36048847494SEnrico Perla - Sun Microsystems #define	NEED_CACHE_DIR		0x00000001
36148847494SEnrico Perla - Sun Microsystems #define	IS_SPARC_TARGET		0x00000002
36248847494SEnrico Perla - Sun Microsystems #define	UPDATE_ERROR		0x00000004
36348847494SEnrico Perla - Sun Microsystems #define	RDONLY_FSCHK		0x00000008
3644a9df875SEnrico Perla - Sun Microsystems #define	INVALIDATE_CACHE	0x00000010
36548847494SEnrico Perla - Sun Microsystems 
36648847494SEnrico Perla - Sun Microsystems #define	is_flag_on(flag)	(walk_arg.update_flags & flag ? 1 : 0)
36748847494SEnrico Perla - Sun Microsystems #define	set_flag(flag)		(walk_arg.update_flags |= flag)
36848847494SEnrico Perla - Sun Microsystems #define	unset_flag(flag)	(walk_arg.update_flags &= ~flag)
36948847494SEnrico Perla - Sun Microsystems 
37048847494SEnrico Perla - Sun Microsystems /*
37148847494SEnrico Perla - Sun Microsystems  * struct walk_arg :
37248847494SEnrico Perla - Sun Microsystems  * update_flags: flags related to the current updating process
37348847494SEnrico Perla - Sun Microsystems  * new_nvlp/old_nvlp: new and old list of archive-files / attributes pairs
37448847494SEnrico Perla - Sun Microsystems  * sparcfile: list of file paths for mkisofs -path-list (SPARC only)
37548847494SEnrico Perla - Sun Microsystems  */
3767c478bd9Sstevel@tonic-gate static struct {
37748847494SEnrico Perla - Sun Microsystems 	int 		update_flags;
3787c478bd9Sstevel@tonic-gate 	nvlist_t 	*new_nvlp;
3797c478bd9Sstevel@tonic-gate 	nvlist_t 	*old_nvlp;
38048847494SEnrico Perla - Sun Microsystems 	FILE 		*sparcfile;
38148847494SEnrico Perla - Sun Microsystems 	dirinfo_t	dirinfo[CACHEDIR_NUM];
3827c478bd9Sstevel@tonic-gate } walk_arg;
3837c478bd9Sstevel@tonic-gate 
3849632cfadSsetje struct safefile {
38558091fd8Ssetje 	char *name;
38658091fd8Ssetje 	struct safefile *next;
38758091fd8Ssetje };
38858091fd8Ssetje 
389ae115bc7Smrj static struct safefile *safefiles = NULL;
3903b133becSGangadhar Mylapuram 
3913b133becSGangadhar Mylapuram /*
3923b133becSGangadhar Mylapuram  * svc:/system/filesystem/usr:default service checks for this file and
3933b133becSGangadhar Mylapuram  * does a boot archive update and then reboot the system.
3943b133becSGangadhar Mylapuram  */
39558091fd8Ssetje #define	NEED_UPDATE_FILE "/etc/svc/volatile/boot_archive_needs_update"
39658091fd8Ssetje 
3973b133becSGangadhar Mylapuram /*
3983b133becSGangadhar Mylapuram  * svc:/system/boot-archive-update:default checks for this file and
3993b133becSGangadhar Mylapuram  * updates the boot archive.
4003b133becSGangadhar Mylapuram  */
4013b133becSGangadhar Mylapuram #define	NEED_UPDATE_SAFE_FILE "/etc/svc/volatile/boot_archive_safefile_update"
4023b133becSGangadhar Mylapuram 
40348847494SEnrico Perla - Sun Microsystems /* Thanks growisofs */
40448847494SEnrico Perla - Sun Microsystems #define	CD_BLOCK	((off64_t)2048)
40548847494SEnrico Perla - Sun Microsystems #define	VOLDESC_OFF	16
40648847494SEnrico Perla - Sun Microsystems #define	DVD_BLOCK	(32*1024)
40748847494SEnrico Perla - Sun Microsystems #define	MAX_IVDs	16
40848847494SEnrico Perla - Sun Microsystems 
40948847494SEnrico Perla - Sun Microsystems struct iso_pdesc {
41048847494SEnrico Perla - Sun Microsystems     unsigned char type	[1];
41148847494SEnrico Perla - Sun Microsystems     unsigned char id	[5];
41248847494SEnrico Perla - Sun Microsystems     unsigned char void1	[80-5-1];
41348847494SEnrico Perla - Sun Microsystems     unsigned char volume_space_size [8];
41448847494SEnrico Perla - Sun Microsystems     unsigned char void2	[2048-80-8];
41548847494SEnrico Perla - Sun Microsystems };
41648847494SEnrico Perla - Sun Microsystems 
41748847494SEnrico Perla - Sun Microsystems /*
41848847494SEnrico Perla - Sun Microsystems  * COUNT_MAX:	maximum number of changed files to justify a multisession update
41948847494SEnrico Perla - Sun Microsystems  * BA_SIZE_MAX:	maximum size of the boot_archive to justify a multisession
42048847494SEnrico Perla - Sun Microsystems  * 		update
42148847494SEnrico Perla - Sun Microsystems  */
42248847494SEnrico Perla - Sun Microsystems #define	COUNT_MAX		50
42348847494SEnrico Perla - Sun Microsystems #define	BA_SIZE_MAX		(50 * 1024 * 1024)
42448847494SEnrico Perla - Sun Microsystems 
42548847494SEnrico Perla - Sun Microsystems #define	bam_nowrite()		(bam_check || bam_smf_check)
42648847494SEnrico Perla - Sun Microsystems 
42719397407SSherry Moore static int sync_menu = 1;	/* whether we need to sync the BE menus */
42819397407SSherry Moore 
4297c478bd9Sstevel@tonic-gate static void
4307c478bd9Sstevel@tonic-gate usage(void)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "USAGE:\n");
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	/* archive usage */
435d876c67dSjg 	(void) fprintf(stderr,
436c365cc98SToomas Soome 	    "\t%s update-archive [-vn] [-R altroot [-p platform]]\n", prog);
437d876c67dSjg 	(void) fprintf(stderr,
438c365cc98SToomas Soome 	    "\t%s list-archive [-R altroot [-p platform]]\n", prog);
43979c28b70SToomas Soome #if defined(_OBP)
440c7c0ceafSToomas Soome 	(void) fprintf(stderr,
441c7c0ceafSToomas Soome 	    "\t%s install-bootloader [-fv] [-R altroot] [-P pool]\n", prog);
442c7c0ceafSToomas Soome #else
443c7c0ceafSToomas Soome 	(void) fprintf(stderr,
444c7c0ceafSToomas Soome 	    "\t%s install-bootloader [-Mfv] [-R altroot] [-P pool]\n", prog);
445c7c0ceafSToomas Soome #endif
44679c28b70SToomas Soome #if !defined(_OBP)
4477c478bd9Sstevel@tonic-gate 	/* x86 only */
4487c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog);
4497c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog);
4507c478bd9Sstevel@tonic-gate #endif
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
453cedc7e57SEnrico Perla - Sun Microsystems /*
454cedc7e57SEnrico Perla - Sun Microsystems  * Best effort attempt to restore the $HOME value.
455cedc7e57SEnrico Perla - Sun Microsystems  */
456cedc7e57SEnrico Perla - Sun Microsystems static void
457cedc7e57SEnrico Perla - Sun Microsystems restore_env()
458cedc7e57SEnrico Perla - Sun Microsystems {
459cedc7e57SEnrico Perla - Sun Microsystems 	char	home_env[PATH_MAX];
460cedc7e57SEnrico Perla - Sun Microsystems 
461cedc7e57SEnrico Perla - Sun Microsystems 	if (bam_home_env) {
462cedc7e57SEnrico Perla - Sun Microsystems 		(void) snprintf(home_env, sizeof (home_env), "HOME=%s",
463cedc7e57SEnrico Perla - Sun Microsystems 		    bam_home_env);
464cedc7e57SEnrico Perla - Sun Microsystems 		(void) putenv(home_env);
465cedc7e57SEnrico Perla - Sun Microsystems 	}
466cedc7e57SEnrico Perla - Sun Microsystems }
467cedc7e57SEnrico Perla - Sun Microsystems 
468cedc7e57SEnrico Perla - Sun Microsystems 
469cedc7e57SEnrico Perla - Sun Microsystems #define		SLEEP_TIME	5
470cedc7e57SEnrico Perla - Sun Microsystems #define		MAX_TRIES	4
471cedc7e57SEnrico Perla - Sun Microsystems 
472cedc7e57SEnrico Perla - Sun Microsystems /*
473cedc7e57SEnrico Perla - Sun Microsystems  * Sanitize the environment in which bootadm will execute its sub-processes
474cedc7e57SEnrico Perla - Sun Microsystems  * (ex. mkisofs). This is done to prevent those processes from attempting
475cedc7e57SEnrico Perla - Sun Microsystems  * to access files (ex. .mkisofsrc) or stat paths that might be on NFS
476cedc7e57SEnrico Perla - Sun Microsystems  * or, potentially, insecure.
477cedc7e57SEnrico Perla - Sun Microsystems  */
478cedc7e57SEnrico Perla - Sun Microsystems static void
479cedc7e57SEnrico Perla - Sun Microsystems sanitize_env()
480cedc7e57SEnrico Perla - Sun Microsystems {
481cedc7e57SEnrico Perla - Sun Microsystems 	int	stry = 0;
482cedc7e57SEnrico Perla - Sun Microsystems 
483cedc7e57SEnrico Perla - Sun Microsystems 	/* don't depend on caller umask */
484cedc7e57SEnrico Perla - Sun Microsystems 	(void) umask(0022);
485cedc7e57SEnrico Perla - Sun Microsystems 
486cedc7e57SEnrico Perla - Sun Microsystems 	/* move away from a potential unsafe current working directory */
487cedc7e57SEnrico Perla - Sun Microsystems 	while (chdir("/") == -1) {
488cedc7e57SEnrico Perla - Sun Microsystems 		if (errno != EINTR) {
489cedc7e57SEnrico Perla - Sun Microsystems 			bam_print("WARNING: unable to chdir to /");
490cedc7e57SEnrico Perla - Sun Microsystems 			break;
491cedc7e57SEnrico Perla - Sun Microsystems 		}
492cedc7e57SEnrico Perla - Sun Microsystems 	}
493cedc7e57SEnrico Perla - Sun Microsystems 
494cedc7e57SEnrico Perla - Sun Microsystems 	bam_home_env = getenv("HOME");
495cedc7e57SEnrico Perla - Sun Microsystems 	while (bam_home_env != NULL && putenv("HOME=/") == -1) {
496cedc7e57SEnrico Perla - Sun Microsystems 		if (errno == ENOMEM) {
497cedc7e57SEnrico Perla - Sun Microsystems 			/* retry no more than MAX_TRIES times */
498cedc7e57SEnrico Perla - Sun Microsystems 			if (++stry > MAX_TRIES) {
499cedc7e57SEnrico Perla - Sun Microsystems 				bam_print("WARNING: unable to recover from "
500cedc7e57SEnrico Perla - Sun Microsystems 				    "system memory pressure... aborting \n");
501cedc7e57SEnrico Perla - Sun Microsystems 				bam_exit(EXIT_FAILURE);
502cedc7e57SEnrico Perla - Sun Microsystems 			}
503cedc7e57SEnrico Perla - Sun Microsystems 			/* memory is tight, try to sleep */
504cedc7e57SEnrico Perla - Sun Microsystems 			bam_print("Attempting to recover from memory pressure: "
505cedc7e57SEnrico Perla - Sun Microsystems 			    "sleeping for %d seconds\n", SLEEP_TIME * stry);
506cedc7e57SEnrico Perla - Sun Microsystems 			(void) sleep(SLEEP_TIME * stry);
507cedc7e57SEnrico Perla - Sun Microsystems 		} else {
508cedc7e57SEnrico Perla - Sun Microsystems 			bam_print("WARNING: unable to sanitize HOME\n");
509cedc7e57SEnrico Perla - Sun Microsystems 		}
510cedc7e57SEnrico Perla - Sun Microsystems 	}
511cedc7e57SEnrico Perla - Sun Microsystems }
512cedc7e57SEnrico Perla - Sun Microsystems 
5137c478bd9Sstevel@tonic-gate int
5147c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
5157c478bd9Sstevel@tonic-gate {
516110570ceSToomas Soome 	error_t ret = BAM_SUCCESS;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
5197c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
5227c478bd9Sstevel@tonic-gate 		prog = argv[0];
5237c478bd9Sstevel@tonic-gate 	} else {
5247c478bd9Sstevel@tonic-gate 		prog++;
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 
527eb2bd662Svikram 	INJECT_ERROR1("ASSERT_ON", assert(0))
5287c478bd9Sstevel@tonic-gate 
529cedc7e57SEnrico Perla - Sun Microsystems 	sanitize_env();
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	switch (bam_cmd) {
5347c478bd9Sstevel@tonic-gate 		case BAM_MENU:
535f64ca102SToomas Soome 			if (is_grub(bam_alt_root ? bam_root : "/")) {
536f64ca102SToomas Soome 				ret = bam_menu(bam_subcmd, bam_opt,
537f64ca102SToomas Soome 				    bam_argc, bam_argv);
538f64ca102SToomas Soome 			} else {
539f64ca102SToomas Soome 				ret = bam_loader_menu(bam_subcmd, bam_opt,
540f64ca102SToomas Soome 				    bam_argc, bam_argv);
541f64ca102SToomas Soome 			}
5427c478bd9Sstevel@tonic-gate 			break;
5437c478bd9Sstevel@tonic-gate 		case BAM_ARCHIVE:
5447c478bd9Sstevel@tonic-gate 			ret = bam_archive(bam_subcmd, bam_opt);
5457c478bd9Sstevel@tonic-gate 			break;
546c7c0ceafSToomas Soome 		case BAM_INSTALL:
547c7c0ceafSToomas Soome 			ret = bam_install(bam_subcmd, bam_opt);
548c7c0ceafSToomas Soome 			break;
5497c478bd9Sstevel@tonic-gate 		default:
5507c478bd9Sstevel@tonic-gate 			usage();
5517c478bd9Sstevel@tonic-gate 			bam_exit(1);
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS)
55544da779fSWilliam Kucharski 		bam_exit((ret == BAM_NOCHANGE) ? 2 : 1);
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	bam_unlock();
5587c478bd9Sstevel@tonic-gate 	return (0);
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate /*
5627c478bd9Sstevel@tonic-gate  * Equivalence of public and internal commands:
5637c478bd9Sstevel@tonic-gate  *	update-archive  -- -a update
5647c478bd9Sstevel@tonic-gate  *	list-archive	-- -a list
5657c478bd9Sstevel@tonic-gate  *	set-menu	-- -m set_option
5667c478bd9Sstevel@tonic-gate  *	list-menu	-- -m list_entry
5677c478bd9Sstevel@tonic-gate  *	update-menu	-- -m update_entry
568c7c0ceafSToomas Soome  *	install-bootloader	-- -i install_bootloader
5697c478bd9Sstevel@tonic-gate  */
5707c478bd9Sstevel@tonic-gate static struct cmd_map {
5717c478bd9Sstevel@tonic-gate 	char *bam_cmdname;
5727c478bd9Sstevel@tonic-gate 	int bam_cmd;
5737c478bd9Sstevel@tonic-gate 	char *bam_subcmd;
5747c478bd9Sstevel@tonic-gate } cmd_map[] = {
5757c478bd9Sstevel@tonic-gate 	{ "update-archive",	BAM_ARCHIVE,	"update"},
5767c478bd9Sstevel@tonic-gate 	{ "list-archive",	BAM_ARCHIVE,	"list"},
5777c478bd9Sstevel@tonic-gate 	{ "set-menu",		BAM_MENU,	"set_option"},
5787c478bd9Sstevel@tonic-gate 	{ "list-menu",		BAM_MENU,	"list_entry"},
5797c478bd9Sstevel@tonic-gate 	{ "update-menu",	BAM_MENU,	"update_entry"},
580c7c0ceafSToomas Soome 	{ "install-bootloader",	BAM_INSTALL,	"install_bootloader"},
5817c478bd9Sstevel@tonic-gate 	{ NULL,			0,		NULL}
5827c478bd9Sstevel@tonic-gate };
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate /*
5857c478bd9Sstevel@tonic-gate  * Commands syntax published in bootadm(1M) are parsed here
5867c478bd9Sstevel@tonic-gate  */
5877c478bd9Sstevel@tonic-gate static void
5887c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[])
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate 	struct cmd_map *cmp = cmd_map;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	/* command conforming to the final spec */
5937c478bd9Sstevel@tonic-gate 	if (argc > 1 && argv[1][0] != '-') {
5947c478bd9Sstevel@tonic-gate 		/*
5957c478bd9Sstevel@tonic-gate 		 * Map commands to internal table.
5967c478bd9Sstevel@tonic-gate 		 */
5977c478bd9Sstevel@tonic-gate 		while (cmp->bam_cmdname) {
5987c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], cmp->bam_cmdname) == 0) {
5997c478bd9Sstevel@tonic-gate 				bam_cmd = cmp->bam_cmd;
6007c478bd9Sstevel@tonic-gate 				bam_subcmd = cmp->bam_subcmd;
6017c478bd9Sstevel@tonic-gate 				break;
6027c478bd9Sstevel@tonic-gate 			}
6037c478bd9Sstevel@tonic-gate 			cmp++;
6047c478bd9Sstevel@tonic-gate 		}
6057c478bd9Sstevel@tonic-gate 		if (cmp->bam_cmdname == NULL) {
6067c478bd9Sstevel@tonic-gate 			usage();
6077c478bd9Sstevel@tonic-gate 			bam_exit(1);
6087c478bd9Sstevel@tonic-gate 		}
6097c478bd9Sstevel@tonic-gate 		argc--;
6107c478bd9Sstevel@tonic-gate 		argv++;
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	parse_args_internal(argc, argv);
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate /*
6177c478bd9Sstevel@tonic-gate  * A combination of public and private commands are parsed here.
6187c478bd9Sstevel@tonic-gate  * The internal syntax and the corresponding functionality are:
6197c478bd9Sstevel@tonic-gate  *	-a update			-- update-archive
6207c478bd9Sstevel@tonic-gate  *	-a list				-- list-archive
62144da779fSWilliam Kucharski  *	-a update-all			-- (reboot to sync all mnted OS archive)
622c7c0ceafSToomas Soome  *	-i install_bootloader		-- install-bootloader
6237c478bd9Sstevel@tonic-gate  *	-m update_entry			-- update-menu
6247c478bd9Sstevel@tonic-gate  *	-m list_entry			-- list-menu
6257c478bd9Sstevel@tonic-gate  *	-m update_temp			-- (reboot -- [boot-args])
6267c478bd9Sstevel@tonic-gate  *	-m delete_all_entries		-- (called from install)
62744da779fSWilliam Kucharski  *	-m enable_hypervisor [args]	-- cvt_to_hyper
62844da779fSWilliam Kucharski  *	-m disable_hypervisor		-- cvt_to_metal
62944da779fSWilliam Kucharski  *	-m list_setting [entry] [value]	-- list_setting
63044da779fSWilliam Kucharski  *
63148847494SEnrico Perla - Sun Microsystems  * A set of private flags is there too:
63248847494SEnrico Perla - Sun Microsystems  *	-F		-- purge the cache directories and rebuild them
63348847494SEnrico Perla - Sun Microsystems  *	-e		-- use the (faster) archive update approach (used by
63448847494SEnrico Perla - Sun Microsystems  *			   reboot)
6357c478bd9Sstevel@tonic-gate  */
6367c478bd9Sstevel@tonic-gate static void
6377c478bd9Sstevel@tonic-gate parse_args_internal(int argc, char *argv[])
6387c478bd9Sstevel@tonic-gate {
6397c478bd9Sstevel@tonic-gate 	int c, error;
6407c478bd9Sstevel@tonic-gate 	extern char *optarg;
6417c478bd9Sstevel@tonic-gate 	extern int optind, opterr;
64279c28b70SToomas Soome #if defined(_OBP)
643c7c0ceafSToomas Soome 	const char *optstring = "a:d:fi:m:no:veFCR:p:P:XZ";
644c7c0ceafSToomas Soome #else
645c7c0ceafSToomas Soome 	const char *optstring = "a:d:fi:m:no:veFCMR:p:P:XZ";
646c7c0ceafSToomas Soome #endif
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	/* Suppress error message from getopt */
6497c478bd9Sstevel@tonic-gate 	opterr = 0;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	error = 0;
652c7c0ceafSToomas Soome 	while ((c = getopt(argc, argv, optstring)) != -1) {
6537c478bd9Sstevel@tonic-gate 		switch (c) {
6547c478bd9Sstevel@tonic-gate 		case 'a':
6557c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
6567c478bd9Sstevel@tonic-gate 				error = 1;
657f64ca102SToomas Soome 				bam_error(
658f64ca102SToomas Soome 				    _("multiple commands specified: -%c\n"), c);
6597c478bd9Sstevel@tonic-gate 			}
6607c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_ARCHIVE;
6617c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
6627c478bd9Sstevel@tonic-gate 			break;
6637c478bd9Sstevel@tonic-gate 		case 'd':
6647c478bd9Sstevel@tonic-gate 			if (bam_debug) {
6657c478bd9Sstevel@tonic-gate 				error = 1;
666f64ca102SToomas Soome 				bam_error(
667f64ca102SToomas Soome 				    _("duplicate options specified: -%c\n"), c);
6687c478bd9Sstevel@tonic-gate 			}
6697c478bd9Sstevel@tonic-gate 			bam_debug = s_strtol(optarg);
6707c478bd9Sstevel@tonic-gate 			break;
6717c478bd9Sstevel@tonic-gate 		case 'f':
6727c478bd9Sstevel@tonic-gate 			bam_force = 1;
6737c478bd9Sstevel@tonic-gate 			break;
67448847494SEnrico Perla - Sun Microsystems 		case 'F':
67548847494SEnrico Perla - Sun Microsystems 			bam_purge = 1;
67648847494SEnrico Perla - Sun Microsystems 			break;
677c7c0ceafSToomas Soome 		case 'i':
678c7c0ceafSToomas Soome 			if (bam_cmd) {
679c7c0ceafSToomas Soome 				error = 1;
680f64ca102SToomas Soome 				bam_error(
681f64ca102SToomas Soome 				    _("multiple commands specified: -%c\n"), c);
682c7c0ceafSToomas Soome 			}
683c7c0ceafSToomas Soome 			bam_cmd = BAM_INSTALL;
684c7c0ceafSToomas Soome 			bam_subcmd = optarg;
685c7c0ceafSToomas Soome 			break;
6867c478bd9Sstevel@tonic-gate 		case 'm':
6877c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
6887c478bd9Sstevel@tonic-gate 				error = 1;
689f64ca102SToomas Soome 				bam_error(
690f64ca102SToomas Soome 				    _("multiple commands specified: -%c\n"), c);
6917c478bd9Sstevel@tonic-gate 			}
6927c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_MENU;
6937c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
6947c478bd9Sstevel@tonic-gate 			break;
69579c28b70SToomas Soome #if !defined(_OBP)
696c7c0ceafSToomas Soome 		case 'M':
697c7c0ceafSToomas Soome 			bam_mbr = 1;
698c7c0ceafSToomas Soome 			break;
699c7c0ceafSToomas Soome #endif
7007c478bd9Sstevel@tonic-gate 		case 'n':
7017c478bd9Sstevel@tonic-gate 			bam_check = 1;
7025eea6091SEnrico Perla - Sun Microsystems 			/*
7035eea6091SEnrico Perla - Sun Microsystems 			 * We save the original value of bam_check. The new
7045eea6091SEnrico Perla - Sun Microsystems 			 * approach in case of a read-only filesystem is to
7055eea6091SEnrico Perla - Sun Microsystems 			 * behave as a check, so we need a way to restore the
7065eea6091SEnrico Perla - Sun Microsystems 			 * original value after the evaluation of the read-only
7075eea6091SEnrico Perla - Sun Microsystems 			 * filesystem has been done.
7085eea6091SEnrico Perla - Sun Microsystems 			 * Even if we don't allow at the moment a check with
7095eea6091SEnrico Perla - Sun Microsystems 			 * update_all, this approach is more robust than
7105eea6091SEnrico Perla - Sun Microsystems 			 * simply resetting bam_check to zero.
7115eea6091SEnrico Perla - Sun Microsystems 			 */
7125eea6091SEnrico Perla - Sun Microsystems 			bam_saved_check = 1;
7137c478bd9Sstevel@tonic-gate 			break;
7147c478bd9Sstevel@tonic-gate 		case 'o':
7157c478bd9Sstevel@tonic-gate 			if (bam_opt) {
7167c478bd9Sstevel@tonic-gate 				error = 1;
717f64ca102SToomas Soome 				bam_error(
718f64ca102SToomas Soome 				    _("duplicate options specified: -%c\n"), c);
7197c478bd9Sstevel@tonic-gate 			}
7207c478bd9Sstevel@tonic-gate 			bam_opt = optarg;
7217c478bd9Sstevel@tonic-gate 			break;
7227c478bd9Sstevel@tonic-gate 		case 'v':
7237c478bd9Sstevel@tonic-gate 			bam_verbose = 1;
7247c478bd9Sstevel@tonic-gate 			break;
7258c1b6884Sszhou 		case 'C':
7268c1b6884Sszhou 			bam_smf_check = 1;
7278c1b6884Sszhou 			break;
728c7c0ceafSToomas Soome 		case 'P':
729c7c0ceafSToomas Soome 			if (bam_pool != NULL) {
730c7c0ceafSToomas Soome 				error = 1;
731f64ca102SToomas Soome 				bam_error(
732f64ca102SToomas Soome 				    _("duplicate options specified: -%c\n"), c);
733c7c0ceafSToomas Soome 			}
734c7c0ceafSToomas Soome 			bam_pool = optarg;
735c7c0ceafSToomas Soome 			break;
7367c478bd9Sstevel@tonic-gate 		case 'R':
7377c478bd9Sstevel@tonic-gate 			if (bam_root) {
7387c478bd9Sstevel@tonic-gate 				error = 1;
739f64ca102SToomas Soome 				bam_error(
740f64ca102SToomas Soome 				    _("duplicate options specified: -%c\n"), c);
7417c478bd9Sstevel@tonic-gate 				break;
7427c478bd9Sstevel@tonic-gate 			} else if (realpath(optarg, rootbuf) == NULL) {
7437c478bd9Sstevel@tonic-gate 				error = 1;
744f64ca102SToomas Soome 				bam_error(_("cannot resolve path %s: %s\n"),
745f64ca102SToomas Soome 				    optarg, strerror(errno));
7467c478bd9Sstevel@tonic-gate 				break;
7477c478bd9Sstevel@tonic-gate 			}
74840541d5dSvikram 			bam_alt_root = 1;
7497c478bd9Sstevel@tonic-gate 			bam_root = rootbuf;
7507c478bd9Sstevel@tonic-gate 			bam_rootlen = strlen(rootbuf);
7517c478bd9Sstevel@tonic-gate 			break;
752d876c67dSjg 		case 'p':
753d876c67dSjg 			bam_alt_platform = 1;
754d876c67dSjg 			bam_platform = optarg;
755d876c67dSjg 			if ((strcmp(bam_platform, "i86pc") != 0) &&
756d876c67dSjg 			    (strcmp(bam_platform, "sun4u") != 0) &&
757d876c67dSjg 			    (strcmp(bam_platform, "sun4v") != 0)) {
758d876c67dSjg 				error = 1;
759f64ca102SToomas Soome 				bam_error(_("invalid platform %s - must be "
760f64ca102SToomas Soome 				    "one of sun4u, sun4v or i86pc\n"),
761f64ca102SToomas Soome 				    bam_platform);
762d876c67dSjg 			}
763d876c67dSjg 			break;
7643028dfd6SFrank Van Der Linden 		case 'X':
7653028dfd6SFrank Van Der Linden 			bam_is_hv = BAM_HV_PRESENT;
7663028dfd6SFrank Van Der Linden 			break;
767e7cbe64fSgw25295 		case 'Z':
768e7cbe64fSgw25295 			bam_zfs = 1;
769e7cbe64fSgw25295 			break;
77048847494SEnrico Perla - Sun Microsystems 		case 'e':
77148847494SEnrico Perla - Sun Microsystems 			bam_extend = 1;
77248847494SEnrico Perla - Sun Microsystems 			break;
7737c478bd9Sstevel@tonic-gate 		case '?':
7747c478bd9Sstevel@tonic-gate 			error = 1;
775f64ca102SToomas Soome 			bam_error(_("invalid option or missing option "
776f64ca102SToomas Soome 			    "argument: -%c\n"), optopt);
7777c478bd9Sstevel@tonic-gate 			break;
7787c478bd9Sstevel@tonic-gate 		default :
7797c478bd9Sstevel@tonic-gate 			error = 1;
780f64ca102SToomas Soome 			bam_error(_("invalid option or missing option "
781f64ca102SToomas Soome 			    "argument: -%c\n"), c);
7827c478bd9Sstevel@tonic-gate 			break;
7837c478bd9Sstevel@tonic-gate 		}
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	/*
787d876c67dSjg 	 * An alternate platform requires an alternate root
788d876c67dSjg 	 */
789d876c67dSjg 	if (bam_alt_platform && bam_alt_root == 0) {
790d876c67dSjg 		usage();
791d876c67dSjg 		bam_exit(0);
792d876c67dSjg 	}
793d876c67dSjg 
794d876c67dSjg 	/*
7957c478bd9Sstevel@tonic-gate 	 * A command option must be specfied
7967c478bd9Sstevel@tonic-gate 	 */
7977c478bd9Sstevel@tonic-gate 	if (!bam_cmd) {
7987c478bd9Sstevel@tonic-gate 		if (bam_opt && strcmp(bam_opt, "all") == 0) {
7997c478bd9Sstevel@tonic-gate 			usage();
8007c478bd9Sstevel@tonic-gate 			bam_exit(0);
8017c478bd9Sstevel@tonic-gate 		}
802f64ca102SToomas Soome 		bam_error(_("a command option must be specified\n"));
8037c478bd9Sstevel@tonic-gate 		error = 1;
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	if (error) {
8077c478bd9Sstevel@tonic-gate 		usage();
8087c478bd9Sstevel@tonic-gate 		bam_exit(1);
8097c478bd9Sstevel@tonic-gate 	}
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	if (optind > argc) {
812f64ca102SToomas Soome 		bam_error(_("Internal error: %s\n"), "parse_args");
8137c478bd9Sstevel@tonic-gate 		bam_exit(1);
8147c478bd9Sstevel@tonic-gate 	} else if (optind < argc) {
8157c478bd9Sstevel@tonic-gate 		bam_argv = &argv[optind];
8167c478bd9Sstevel@tonic-gate 		bam_argc = argc - optind;
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	/*
820c7c0ceafSToomas Soome 	 * mbr and pool are options for install_bootloader
821c7c0ceafSToomas Soome 	 */
822c7c0ceafSToomas Soome 	if (bam_cmd != BAM_INSTALL && (bam_mbr || bam_pool != NULL)) {
823c7c0ceafSToomas Soome 		usage();
824c7c0ceafSToomas Soome 		bam_exit(0);
825c7c0ceafSToomas Soome 	}
826c7c0ceafSToomas Soome 
827c7c0ceafSToomas Soome 	/*
8287c478bd9Sstevel@tonic-gate 	 * -n implies verbose mode
8297c478bd9Sstevel@tonic-gate 	 */
8307c478bd9Sstevel@tonic-gate 	if (bam_check)
8317c478bd9Sstevel@tonic-gate 		bam_verbose = 1;
8327c478bd9Sstevel@tonic-gate }
8337c478bd9Sstevel@tonic-gate 
834f64ca102SToomas Soome error_t
8357c478bd9Sstevel@tonic-gate check_subcmd_and_options(
8367c478bd9Sstevel@tonic-gate 	char *subcmd,
8377c478bd9Sstevel@tonic-gate 	char *opt,
8387c478bd9Sstevel@tonic-gate 	subcmd_defn_t *table,
8397c478bd9Sstevel@tonic-gate 	error_t (**fp)())
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	int i;
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	if (subcmd == NULL) {
844f64ca102SToomas Soome 		bam_error(_("this command requires a sub-command\n"));
8457c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate 
848eb2bd662Svikram 	if (strcmp(subcmd, "set_option") == 0) {
849eb2bd662Svikram 		if (bam_argc == 0 || bam_argv == NULL || bam_argv[0] == NULL) {
850f64ca102SToomas Soome 			bam_error(_("missing argument for sub-command\n"));
851eb2bd662Svikram 			usage();
852eb2bd662Svikram 			return (BAM_ERROR);
853eb2bd662Svikram 		} else if (bam_argc > 1 || bam_argv[1] != NULL) {
854f64ca102SToomas Soome 			bam_error(_("invalid trailing arguments\n"));
8551a97e40eSvikram 			usage();
8561a97e40eSvikram 			return (BAM_ERROR);
8571a97e40eSvikram 		}
85819397407SSherry Moore 	} else if (strcmp(subcmd, "update_all") == 0) {
85919397407SSherry Moore 		/*
86019397407SSherry Moore 		 * The only option we accept for the "update_all"
86119397407SSherry Moore 		 * subcmd is "fastboot".
86219397407SSherry Moore 		 */
86319397407SSherry Moore 		if (bam_argc > 1 || (bam_argc == 1 &&
86419397407SSherry Moore 		    strcmp(bam_argv[0], "fastboot") != 0)) {
865f64ca102SToomas Soome 			bam_error(_("invalid trailing arguments\n"));
86619397407SSherry Moore 			usage();
86719397407SSherry Moore 			return (BAM_ERROR);
86819397407SSherry Moore 		}
86919397407SSherry Moore 		if (bam_argc == 1)
87019397407SSherry Moore 			sync_menu = 0;
87144da779fSWilliam Kucharski 	} else if (((strcmp(subcmd, "enable_hypervisor") != 0) &&
87244da779fSWilliam Kucharski 	    (strcmp(subcmd, "list_setting") != 0)) && (bam_argc || bam_argv)) {
87344da779fSWilliam Kucharski 		/*
87444da779fSWilliam Kucharski 		 * Of the remaining subcommands, only "enable_hypervisor" and
87544da779fSWilliam Kucharski 		 * "list_setting" take trailing arguments.
87644da779fSWilliam Kucharski 		 */
877f64ca102SToomas Soome 		bam_error(_("invalid trailing arguments\n"));
878eb2bd662Svikram 		usage();
879eb2bd662Svikram 		return (BAM_ERROR);
8801a97e40eSvikram 	}
8811a97e40eSvikram 
8827c478bd9Sstevel@tonic-gate 	if (bam_root == NULL) {
8837c478bd9Sstevel@tonic-gate 		bam_root = rootbuf;
8847c478bd9Sstevel@tonic-gate 		bam_rootlen = 1;
8857c478bd9Sstevel@tonic-gate 	}
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	/* verify that subcmd is valid */
8887c478bd9Sstevel@tonic-gate 	for (i = 0; table[i].subcmd != NULL; i++) {
8897c478bd9Sstevel@tonic-gate 		if (strcmp(table[i].subcmd, subcmd) == 0)
8907c478bd9Sstevel@tonic-gate 			break;
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	if (table[i].subcmd == NULL) {
894f64ca102SToomas Soome 		bam_error(_("invalid sub-command specified: %s\n"), subcmd);
8957c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
8967c478bd9Sstevel@tonic-gate 	}
8977c478bd9Sstevel@tonic-gate 
8981a97e40eSvikram 	if (table[i].unpriv == 0 && geteuid() != 0) {
899f64ca102SToomas Soome 		bam_error(_("you must be root to run this command\n"));
9001a97e40eSvikram 		return (BAM_ERROR);
9011a97e40eSvikram 	}
9021a97e40eSvikram 
9031a97e40eSvikram 	/*
9041a97e40eSvikram 	 * Currently only privileged commands need a lock
9051a97e40eSvikram 	 */
9061a97e40eSvikram 	if (table[i].unpriv == 0)
9071a97e40eSvikram 		bam_lock();
9081a97e40eSvikram 
9097c478bd9Sstevel@tonic-gate 	/* subcmd verifies that opt is appropriate */
9107c478bd9Sstevel@tonic-gate 	if (table[i].option != OPT_OPTIONAL) {
9117c478bd9Sstevel@tonic-gate 		if ((table[i].option == OPT_REQ) ^ (opt != NULL)) {
9127c478bd9Sstevel@tonic-gate 			if (opt)
913f64ca102SToomas Soome 				bam_error(_("this sub-command (%s) does not "
914f64ca102SToomas Soome 				    "take options\n"), subcmd);
9157c478bd9Sstevel@tonic-gate 			else
916f64ca102SToomas Soome 				bam_error(_("an option is required for this "
917f64ca102SToomas Soome 				    "sub-command: %s\n"), subcmd);
9187c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
9197c478bd9Sstevel@tonic-gate 		}
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	*fp = table[i].handler;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate 
92740541d5dSvikram /*
92840541d5dSvikram  * NOTE: A single "/" is also considered a trailing slash and will
92940541d5dSvikram  * be deleted.
93040541d5dSvikram  */
931f64ca102SToomas Soome void
93240541d5dSvikram elide_trailing_slash(const char *src, char *dst, size_t dstsize)
93340541d5dSvikram {
93440541d5dSvikram 	size_t dstlen;
93540541d5dSvikram 
93640541d5dSvikram 	assert(src);
93740541d5dSvikram 	assert(dst);
93840541d5dSvikram 
93940541d5dSvikram 	(void) strlcpy(dst, src, dstsize);
94040541d5dSvikram 
94140541d5dSvikram 	dstlen = strlen(dst);
94240541d5dSvikram 	if (dst[dstlen - 1] == '/') {
94340541d5dSvikram 		dst[dstlen - 1] = '\0';
94440541d5dSvikram 	}
94540541d5dSvikram }
94640541d5dSvikram 
947cedc7e57SEnrico Perla - Sun Microsystems static int
948cedc7e57SEnrico Perla - Sun Microsystems is_safe_exec(char *path)
949cedc7e57SEnrico Perla - Sun Microsystems {
950cedc7e57SEnrico Perla - Sun Microsystems 	struct stat	sb;
951cedc7e57SEnrico Perla - Sun Microsystems 
952cedc7e57SEnrico Perla - Sun Microsystems 	if (lstat(path, &sb) != 0) {
953f64ca102SToomas Soome 		bam_error(_("stat of file failed: %s: %s\n"), path,
954f64ca102SToomas Soome 		    strerror(errno));
955cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
956cedc7e57SEnrico Perla - Sun Microsystems 	}
957cedc7e57SEnrico Perla - Sun Microsystems 
958cedc7e57SEnrico Perla - Sun Microsystems 	if (!S_ISREG(sb.st_mode)) {
959f64ca102SToomas Soome 		bam_error(_("%s is not a regular file, skipping\n"), path);
960cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
961cedc7e57SEnrico Perla - Sun Microsystems 	}
962cedc7e57SEnrico Perla - Sun Microsystems 
963cedc7e57SEnrico Perla - Sun Microsystems 	if (sb.st_uid != getuid()) {
964f64ca102SToomas Soome 		bam_error(_("%s is not owned by %d, skipping\n"),
965f64ca102SToomas Soome 		    path, getuid());
966cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
967cedc7e57SEnrico Perla - Sun Microsystems 	}
968cedc7e57SEnrico Perla - Sun Microsystems 
969cedc7e57SEnrico Perla - Sun Microsystems 	if (sb.st_mode & S_IWOTH || sb.st_mode & S_IWGRP) {
970f64ca102SToomas Soome 		bam_error(_("%s is others or group writable, skipping\n"),
971f64ca102SToomas Soome 		    path);
972cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
973cedc7e57SEnrico Perla - Sun Microsystems 	}
974cedc7e57SEnrico Perla - Sun Microsystems 
975cedc7e57SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
976cedc7e57SEnrico Perla - Sun Microsystems }
977cedc7e57SEnrico Perla - Sun Microsystems 
9787c478bd9Sstevel@tonic-gate static error_t
97944da779fSWilliam Kucharski list_setting(menu_t *mp, char *which, char *setting)
98044da779fSWilliam Kucharski {
98144da779fSWilliam Kucharski 	line_t	*lp;
98244da779fSWilliam Kucharski 	entry_t	*ent;
98344da779fSWilliam Kucharski 
98444da779fSWilliam Kucharski 	char	*p = which;
98544da779fSWilliam Kucharski 	int	entry;
98644da779fSWilliam Kucharski 
98744da779fSWilliam Kucharski 	int	found;
98844da779fSWilliam Kucharski 
98944da779fSWilliam Kucharski 	assert(which);
99044da779fSWilliam Kucharski 	assert(setting);
99144da779fSWilliam Kucharski 
99244da779fSWilliam Kucharski 	if (*which != NULL) {
99344da779fSWilliam Kucharski 		/*
99444da779fSWilliam Kucharski 		 * If "which" is not a number, assume it's a setting we want
99544da779fSWilliam Kucharski 		 * to look for and so set up the routine to look for "which"
99644da779fSWilliam Kucharski 		 * in the default entry.
99744da779fSWilliam Kucharski 		 */
99844da779fSWilliam Kucharski 		while (*p != NULL)
99944da779fSWilliam Kucharski 			if (!(isdigit((int)*p++))) {
100044da779fSWilliam Kucharski 				setting = which;
100144da779fSWilliam Kucharski 				which = mp->curdefault->arg;
100244da779fSWilliam Kucharski 				break;
100344da779fSWilliam Kucharski 			}
100444da779fSWilliam Kucharski 	} else {
100544da779fSWilliam Kucharski 		which = mp->curdefault->arg;
100644da779fSWilliam Kucharski 	}
100744da779fSWilliam Kucharski 
100844da779fSWilliam Kucharski 	entry = atoi(which);
100944da779fSWilliam Kucharski 
101044da779fSWilliam Kucharski 	for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != entry));
101144da779fSWilliam Kucharski 	    ent = ent->next)
101244da779fSWilliam Kucharski 		;
101344da779fSWilliam Kucharski 
101444da779fSWilliam Kucharski 	if (!ent) {
1015f64ca102SToomas Soome 		bam_error(_("no matching entry found\n"));
101644da779fSWilliam Kucharski 		return (BAM_ERROR);
101744da779fSWilliam Kucharski 	}
101844da779fSWilliam Kucharski 
101944da779fSWilliam Kucharski 	found = (*setting == NULL);
102044da779fSWilliam Kucharski 
102144da779fSWilliam Kucharski 	for (lp = ent->start; lp != NULL; lp = lp->next) {
102244da779fSWilliam Kucharski 		if ((*setting == NULL) && (lp->flags != BAM_COMMENT))
1023f64ca102SToomas Soome 			bam_print("%s\n", lp->line);
1024eac223ccSWilliam Kucharski 		else if (lp->cmd != NULL && strcmp(setting, lp->cmd) == 0) {
1025f64ca102SToomas Soome 			bam_print("%s\n", lp->arg);
102644da779fSWilliam Kucharski 			found = 1;
102744da779fSWilliam Kucharski 		}
102844da779fSWilliam Kucharski 
102944da779fSWilliam Kucharski 		if (lp == ent->end)
103044da779fSWilliam Kucharski 			break;
103144da779fSWilliam Kucharski 	}
103244da779fSWilliam Kucharski 
103344da779fSWilliam Kucharski 	if (!found) {
1034f64ca102SToomas Soome 		bam_error(_("no matching entry found\n"));
103544da779fSWilliam Kucharski 		return (BAM_ERROR);
103644da779fSWilliam Kucharski 	}
103744da779fSWilliam Kucharski 
103844da779fSWilliam Kucharski 	return (BAM_SUCCESS);
103944da779fSWilliam Kucharski }
104044da779fSWilliam Kucharski 
104144da779fSWilliam Kucharski static error_t
1042c7c0ceafSToomas Soome install_bootloader(void)
1043c7c0ceafSToomas Soome {
1044c7c0ceafSToomas Soome 	nvlist_t	*nvl;
1045c7c0ceafSToomas Soome 	uint16_t	flags = 0;
1046c7c0ceafSToomas Soome 	int		found = 0;
1047c7c0ceafSToomas Soome 	struct extmnttab mnt;
1048c7c0ceafSToomas Soome 	struct stat	statbuf = {0};
1049c7c0ceafSToomas Soome 	be_node_list_t	*be_nodes, *node;
1050c7c0ceafSToomas Soome 	FILE		*fp;
1051c7c0ceafSToomas Soome 	char		*root_ds = NULL;
1052110570ceSToomas Soome 	int		ret = BAM_ERROR;
1053c7c0ceafSToomas Soome 
1054c7c0ceafSToomas Soome 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
1055c7c0ceafSToomas Soome 		bam_error(_("out of memory\n"));
1056110570ceSToomas Soome 		return (ret);
1057c7c0ceafSToomas Soome 	}
1058c7c0ceafSToomas Soome 
1059c7c0ceafSToomas Soome 	/*
1060c7c0ceafSToomas Soome 	 * if bam_alt_root is set, the stage files are used from alt root.
1061c7c0ceafSToomas Soome 	 * if pool is set, the target devices are pool devices, stage files
1062c7c0ceafSToomas Soome 	 * are read from pool bootfs unless alt root is set.
1063c7c0ceafSToomas Soome 	 *
1064c7c0ceafSToomas Soome 	 * use arguments as targets, stage files are from alt or current root
1065c7c0ceafSToomas Soome 	 * if no arguments and no pool, install on current boot pool.
1066c7c0ceafSToomas Soome 	 */
1067c7c0ceafSToomas Soome 
1068c7c0ceafSToomas Soome 	if (bam_alt_root) {
1069c7c0ceafSToomas Soome 		if (stat(bam_root, &statbuf) != 0) {
1070f64ca102SToomas Soome 			bam_error(_("stat of file failed: %s: %s\n"), bam_root,
1071f64ca102SToomas Soome 			    strerror(errno));
1072c7c0ceafSToomas Soome 			goto done;
1073c7c0ceafSToomas Soome 		}
1074c7c0ceafSToomas Soome 		if ((fp = fopen(MNTTAB, "r")) == NULL) {
1075f64ca102SToomas Soome 			bam_error(_("failed to open file: %s: %s\n"),
1076f64ca102SToomas Soome 			    MNTTAB, strerror(errno));
1077c7c0ceafSToomas Soome 			goto done;
1078c7c0ceafSToomas Soome 		}
1079c7c0ceafSToomas Soome 		resetmnttab(fp);
1080c7c0ceafSToomas Soome 		while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
1081c7c0ceafSToomas Soome 			if (mnt.mnt_major == major(statbuf.st_dev) &&
1082c7c0ceafSToomas Soome 			    mnt.mnt_minor == minor(statbuf.st_dev)) {
1083c7c0ceafSToomas Soome 				found = 1;
1084c7c0ceafSToomas Soome 				root_ds = strdup(mnt.mnt_special);
1085c7c0ceafSToomas Soome 				break;
1086c7c0ceafSToomas Soome 			}
1087c7c0ceafSToomas Soome 		}
1088c7c0ceafSToomas Soome 		(void) fclose(fp);
1089c7c0ceafSToomas Soome 
1090c7c0ceafSToomas Soome 		if (found == 0) {
1091f64ca102SToomas Soome 			bam_error(_("alternate root %s not in mnttab\n"),
1092f64ca102SToomas Soome 			    bam_root);
1093c7c0ceafSToomas Soome 			goto done;
1094c7c0ceafSToomas Soome 		}
1095c7c0ceafSToomas Soome 		if (root_ds == NULL) {
1096c7c0ceafSToomas Soome 			bam_error(_("out of memory\n"));
1097c7c0ceafSToomas Soome 			goto done;
1098c7c0ceafSToomas Soome 		}
1099c7c0ceafSToomas Soome 
1100*71668a2fSAndy Fiddaman 		if (be_list(NULL, &be_nodes, BE_LIST_DEFAULT) != BE_SUCCESS) {
1101c7c0ceafSToomas Soome 			bam_error(_("No BE's found\n"));
1102c7c0ceafSToomas Soome 			goto done;
1103c7c0ceafSToomas Soome 		}
1104c7c0ceafSToomas Soome 		for (node = be_nodes; node != NULL; node = node->be_next_node)
1105c7c0ceafSToomas Soome 			if (strcmp(root_ds, node->be_root_ds) == 0)
1106c7c0ceafSToomas Soome 				break;
1107c7c0ceafSToomas Soome 
1108c7c0ceafSToomas Soome 		if (node == NULL)
1109c7c0ceafSToomas Soome 			bam_error(_("BE (%s) does not exist\n"), root_ds);
1110c7c0ceafSToomas Soome 
1111c7c0ceafSToomas Soome 		free(root_ds);
1112c7c0ceafSToomas Soome 		root_ds = NULL;
1113c7c0ceafSToomas Soome 		if (node == NULL) {
1114c7c0ceafSToomas Soome 			be_free_list(be_nodes);
1115c7c0ceafSToomas Soome 			goto done;
1116c7c0ceafSToomas Soome 		}
1117c7c0ceafSToomas Soome 		ret = nvlist_add_string(nvl, BE_ATTR_ORIG_BE_NAME,
1118c7c0ceafSToomas Soome 		    node->be_node_name);
1119c7c0ceafSToomas Soome 		ret |= nvlist_add_string(nvl, BE_ATTR_ORIG_BE_ROOT,
1120c7c0ceafSToomas Soome 		    node->be_root_ds);
1121c7c0ceafSToomas Soome 		be_free_list(be_nodes);
1122110570ceSToomas Soome 		if (ret != 0) {
1123c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1124c7c0ceafSToomas Soome 			goto done;
1125c7c0ceafSToomas Soome 		}
1126c7c0ceafSToomas Soome 	}
1127c7c0ceafSToomas Soome 
1128c7c0ceafSToomas Soome 	if (bam_force)
1129c7c0ceafSToomas Soome 		flags |= BE_INSTALLBOOT_FLAG_FORCE;
1130c7c0ceafSToomas Soome 	if (bam_mbr)
1131c7c0ceafSToomas Soome 		flags |= BE_INSTALLBOOT_FLAG_MBR;
1132c7c0ceafSToomas Soome 	if (bam_verbose)
1133c7c0ceafSToomas Soome 		flags |= BE_INSTALLBOOT_FLAG_VERBOSE;
1134c7c0ceafSToomas Soome 
1135c7c0ceafSToomas Soome 	if (nvlist_add_uint16(nvl, BE_ATTR_INSTALL_FLAGS, flags) != 0) {
1136c7c0ceafSToomas Soome 		bam_error(_("out of memory\n"));
1137110570ceSToomas Soome 		ret = BAM_ERROR;
1138c7c0ceafSToomas Soome 		goto done;
1139c7c0ceafSToomas Soome 	}
1140c7c0ceafSToomas Soome 
1141c7c0ceafSToomas Soome 	/*
1142c7c0ceafSToomas Soome 	 * if altroot was set, we got be name and be root, only need
1143c7c0ceafSToomas Soome 	 * to set pool name as target.
1144c7c0ceafSToomas Soome 	 * if no altroot, need to find be name and root from pool.
1145c7c0ceafSToomas Soome 	 */
1146c7c0ceafSToomas Soome 	if (bam_pool != NULL) {
1147c7c0ceafSToomas Soome 		ret = nvlist_add_string(nvl, BE_ATTR_ORIG_BE_POOL, bam_pool);
1148110570ceSToomas Soome 		if (ret != 0) {
1149c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1150c7c0ceafSToomas Soome 			goto done;
1151c7c0ceafSToomas Soome 		}
1152c7c0ceafSToomas Soome 		if (found) {
1153c7c0ceafSToomas Soome 			ret = be_installboot(nvl);
1154110570ceSToomas Soome 			if (ret != 0)
1155c7c0ceafSToomas Soome 				ret = BAM_ERROR;
1156c7c0ceafSToomas Soome 			goto done;
1157c7c0ceafSToomas Soome 		}
1158c7c0ceafSToomas Soome 	}
1159c7c0ceafSToomas Soome 
1160*71668a2fSAndy Fiddaman 	if (be_list(NULL, &be_nodes, BE_LIST_DEFAULT) != BE_SUCCESS) {
1161c7c0ceafSToomas Soome 		bam_error(_("No BE's found\n"));
1162c7c0ceafSToomas Soome 		ret = BAM_ERROR;
1163c7c0ceafSToomas Soome 		goto done;
1164c7c0ceafSToomas Soome 	}
1165c7c0ceafSToomas Soome 
1166c7c0ceafSToomas Soome 	if (bam_pool != NULL) {
1167c7c0ceafSToomas Soome 		/*
1168c7c0ceafSToomas Soome 		 * find active be_node in bam_pool
1169c7c0ceafSToomas Soome 		 */
1170c7c0ceafSToomas Soome 		for (node = be_nodes; node != NULL; node = node->be_next_node) {
1171c7c0ceafSToomas Soome 			if (strcmp(bam_pool, node->be_rpool) != 0)
1172c7c0ceafSToomas Soome 				continue;
1173c7c0ceafSToomas Soome 			if (node->be_active_on_boot)
1174c7c0ceafSToomas Soome 				break;
1175c7c0ceafSToomas Soome 		}
1176c7c0ceafSToomas Soome 		if (node == NULL) {
1177c7c0ceafSToomas Soome 			bam_error(_("No active BE in %s\n"), bam_pool);
1178c7c0ceafSToomas Soome 			be_free_list(be_nodes);
1179c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1180c7c0ceafSToomas Soome 			goto done;
1181c7c0ceafSToomas Soome 		}
1182c7c0ceafSToomas Soome 		ret = nvlist_add_string(nvl, BE_ATTR_ORIG_BE_NAME,
1183c7c0ceafSToomas Soome 		    node->be_node_name);
1184c7c0ceafSToomas Soome 		ret |= nvlist_add_string(nvl, BE_ATTR_ORIG_BE_ROOT,
1185c7c0ceafSToomas Soome 		    node->be_root_ds);
1186c7c0ceafSToomas Soome 		be_free_list(be_nodes);
1187110570ceSToomas Soome 		if (ret != 0) {
1188c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1189c7c0ceafSToomas Soome 			goto done;
1190c7c0ceafSToomas Soome 		}
1191c7c0ceafSToomas Soome 		ret = be_installboot(nvl);
1192110570ceSToomas Soome 		if (ret != 0)
1193c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1194c7c0ceafSToomas Soome 		goto done;
1195c7c0ceafSToomas Soome 	}
1196c7c0ceafSToomas Soome 
1197c7c0ceafSToomas Soome 	/*
1198c7c0ceafSToomas Soome 	 * get dataset for "/" and fill up the args.
1199c7c0ceafSToomas Soome 	 */
1200c7c0ceafSToomas Soome 	if ((fp = fopen(MNTTAB, "r")) == NULL) {
1201f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"),
1202f64ca102SToomas Soome 		    MNTTAB, strerror(errno));
1203c7c0ceafSToomas Soome 		ret = BAM_ERROR;
1204c7c0ceafSToomas Soome 		be_free_list(be_nodes);
1205c7c0ceafSToomas Soome 		goto done;
1206c7c0ceafSToomas Soome 	}
1207c7c0ceafSToomas Soome 	resetmnttab(fp);
1208c7c0ceafSToomas Soome 	found = 0;
1209c7c0ceafSToomas Soome 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
1210c7c0ceafSToomas Soome 		if (strcmp(mnt.mnt_mountp, "/") == 0) {
1211c7c0ceafSToomas Soome 			found = 1;
1212c7c0ceafSToomas Soome 			root_ds = strdup(mnt.mnt_special);
1213c7c0ceafSToomas Soome 			break;
1214c7c0ceafSToomas Soome 		}
1215c7c0ceafSToomas Soome 	}
1216c7c0ceafSToomas Soome 	(void) fclose(fp);
1217c7c0ceafSToomas Soome 
1218c7c0ceafSToomas Soome 	if (found == 0) {
1219f64ca102SToomas Soome 		bam_error(_("alternate root %s not in mnttab\n"), "/");
1220c7c0ceafSToomas Soome 		ret = BAM_ERROR;
1221c7c0ceafSToomas Soome 		be_free_list(be_nodes);
1222c7c0ceafSToomas Soome 		goto done;
1223c7c0ceafSToomas Soome 	}
1224c7c0ceafSToomas Soome 	if (root_ds == NULL) {
1225c7c0ceafSToomas Soome 		bam_error(_("out of memory\n"));
1226c7c0ceafSToomas Soome 		ret = BAM_ERROR;
1227c7c0ceafSToomas Soome 		be_free_list(be_nodes);
1228c7c0ceafSToomas Soome 		goto done;
1229c7c0ceafSToomas Soome 	}
1230c7c0ceafSToomas Soome 
1231c7c0ceafSToomas Soome 	for (node = be_nodes; node != NULL; node = node->be_next_node) {
1232c7c0ceafSToomas Soome 		if (strcmp(root_ds, node->be_root_ds) == 0)
1233c7c0ceafSToomas Soome 			break;
1234c7c0ceafSToomas Soome 	}
1235c7c0ceafSToomas Soome 
1236c7c0ceafSToomas Soome 	if (node == NULL) {
1237c7c0ceafSToomas Soome 		bam_error(_("No such BE: %s\n"), root_ds);
1238c7c0ceafSToomas Soome 		free(root_ds);
1239c7c0ceafSToomas Soome 		be_free_list(be_nodes);
1240c7c0ceafSToomas Soome 		ret = BAM_ERROR;
1241c7c0ceafSToomas Soome 		goto done;
1242c7c0ceafSToomas Soome 	}
1243c7c0ceafSToomas Soome 	free(root_ds);
1244c7c0ceafSToomas Soome 
1245c7c0ceafSToomas Soome 	ret = nvlist_add_string(nvl, BE_ATTR_ORIG_BE_NAME, node->be_node_name);
1246c7c0ceafSToomas Soome 	ret |= nvlist_add_string(nvl, BE_ATTR_ORIG_BE_ROOT, node->be_root_ds);
1247c7c0ceafSToomas Soome 	ret |= nvlist_add_string(nvl, BE_ATTR_ORIG_BE_POOL, node->be_rpool);
1248c7c0ceafSToomas Soome 	be_free_list(be_nodes);
1249c7c0ceafSToomas Soome 
1250110570ceSToomas Soome 	if (ret != 0)
1251c7c0ceafSToomas Soome 		ret = BAM_ERROR;
1252c7c0ceafSToomas Soome 	else
1253c7c0ceafSToomas Soome 		ret = be_installboot(nvl) ? BAM_ERROR : 0;
1254c7c0ceafSToomas Soome done:
1255c7c0ceafSToomas Soome 	nvlist_free(nvl);
1256c7c0ceafSToomas Soome 
1257c7c0ceafSToomas Soome 	return (ret);
1258c7c0ceafSToomas Soome }
1259c7c0ceafSToomas Soome 
1260c7c0ceafSToomas Soome static error_t
1261c7c0ceafSToomas Soome bam_install(char *subcmd, char *opt)
1262c7c0ceafSToomas Soome {
1263c7c0ceafSToomas Soome 	error_t (*f)(void);
1264c7c0ceafSToomas Soome 
1265c7c0ceafSToomas Soome 	/*
1266c7c0ceafSToomas Soome 	 * Check arguments
1267c7c0ceafSToomas Soome 	 */
1268c7c0ceafSToomas Soome 	if (check_subcmd_and_options(subcmd, opt, inst_subcmds, &f) ==
1269c7c0ceafSToomas Soome 	    BAM_ERROR)
1270c7c0ceafSToomas Soome 		return (BAM_ERROR);
1271c7c0ceafSToomas Soome 
1272c7c0ceafSToomas Soome 	return (f());
1273c7c0ceafSToomas Soome }
1274c7c0ceafSToomas Soome 
1275c7c0ceafSToomas Soome static error_t
12767c478bd9Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[])
12777c478bd9Sstevel@tonic-gate {
12787c478bd9Sstevel@tonic-gate 	error_t			ret;
12797c478bd9Sstevel@tonic-gate 	char			menu_path[PATH_MAX];
1280eb2bd662Svikram 	char			clean_menu_root[PATH_MAX];
1281ae115bc7Smrj 	char			path[PATH_MAX];
12827c478bd9Sstevel@tonic-gate 	menu_t			*menu;
1283eb2bd662Svikram 	char			menu_root[PATH_MAX];
1284b610f78eSvikram 	struct stat		sb;
12857c478bd9Sstevel@tonic-gate 	error_t (*f)(menu_t *mp, char *menu_path, char *opt);
1286110570ceSToomas Soome 	char			*special = NULL;
1287eb2bd662Svikram 	char			*pool = NULL;
1288eb2bd662Svikram 	zfs_mnted_t		zmnted;
1289110570ceSToomas Soome 	char			*zmntpt = NULL;
1290eb2bd662Svikram 	char			*osdev;
1291eb2bd662Svikram 	char			*osroot;
1292eb2bd662Svikram 	const char		*fcn = "bam_menu()";
1293986fd29aSsetje 
1294986fd29aSsetje 	/*
1295986fd29aSsetje 	 * Menu sub-command only applies to GRUB (i.e. x86)
1296986fd29aSsetje 	 */
1297eb2bd662Svikram 	if (!is_grub(bam_alt_root ? bam_root : "/")) {
1298f64ca102SToomas Soome 		bam_error(_("not a GRUB 0.97 based Illumos instance. "
1299f64ca102SToomas Soome 		    "Operation not supported\n"));
1300986fd29aSsetje 		return (BAM_ERROR);
1301986fd29aSsetje 	}
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	/*
13047c478bd9Sstevel@tonic-gate 	 * Check arguments
13057c478bd9Sstevel@tonic-gate 	 */
13067c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
13077c478bd9Sstevel@tonic-gate 	if (ret == BAM_ERROR) {
13087c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13097c478bd9Sstevel@tonic-gate 	}
13107c478bd9Sstevel@tonic-gate 
1311eb2bd662Svikram 	assert(bam_root);
1312eb2bd662Svikram 
1313eb2bd662Svikram 	(void) strlcpy(menu_root, bam_root, sizeof (menu_root));
1314eb2bd662Svikram 	osdev = osroot = NULL;
1315eb2bd662Svikram 
1316eb2bd662Svikram 	if (strcmp(subcmd, "update_entry") == 0) {
1317eb2bd662Svikram 		assert(opt);
1318eb2bd662Svikram 
1319eb2bd662Svikram 		osdev = strtok(opt, ",");
1320eb2bd662Svikram 		assert(osdev);
1321eb2bd662Svikram 		osroot = strtok(NULL, ",");
1322eb2bd662Svikram 		if (osroot) {
1323eb2bd662Svikram 			/* fixup bam_root so that it points at osroot */
1324eb2bd662Svikram 			if (realpath(osroot, rootbuf) == NULL) {
1325f64ca102SToomas Soome 				bam_error(_("cannot resolve path %s: %s\n"),
1326f64ca102SToomas Soome 				    osroot, strerror(errno));
1327eb2bd662Svikram 				return (BAM_ERROR);
1328eb2bd662Svikram 			}
1329eb2bd662Svikram 			bam_alt_root = 1;
1330eb2bd662Svikram 			bam_root  = rootbuf;
1331eb2bd662Svikram 			bam_rootlen = strlen(rootbuf);
1332eb2bd662Svikram 		}
1333eb2bd662Svikram 	}
133440541d5dSvikram 
133540541d5dSvikram 	/*
1336eb2bd662Svikram 	 * We support menu on PCFS (under certain conditions), but
1337eb2bd662Svikram 	 * not the OS root
133840541d5dSvikram 	 */
1339eb2bd662Svikram 	if (is_pcfs(bam_root)) {
1340f64ca102SToomas Soome 		bam_error(_("root <%s> on PCFS is not supported\n"), bam_root);
1341eb2bd662Svikram 		return (BAM_ERROR);
1342ae115bc7Smrj 	}
1343ae115bc7Smrj 
1344eb2bd662Svikram 	if (stat(menu_root, &sb) == -1) {
1345f64ca102SToomas Soome 		bam_error(_("cannot find GRUB menu\n"));
134640541d5dSvikram 		return (BAM_ERROR);
134740541d5dSvikram 	}
134840541d5dSvikram 
1349f64ca102SToomas Soome 	BAM_DPRINTF(("%s: menu root is %s\n", fcn, menu_root));
1350e7cbe64fSgw25295 
1351eb2bd662Svikram 	/*
1352eb2bd662Svikram 	 * We no longer use the GRUB slice file. If it exists, then
1353eb2bd662Svikram 	 * the user is doing something that is unsupported (such as
1354eb2bd662Svikram 	 * standard upgrading an old Live Upgrade BE). If that
1355eb2bd662Svikram 	 * happens, mimic existing behavior i.e. pretend that it is
1356eb2bd662Svikram 	 * not a BE. Emit a warning though.
1357eb2bd662Svikram 	 */
1358eb2bd662Svikram 	if (bam_alt_root) {
1359eb2bd662Svikram 		(void) snprintf(path, sizeof (path), "%s%s", bam_root,
1360eb2bd662Svikram 		    GRUB_slice);
1361eb2bd662Svikram 	} else {
1362eb2bd662Svikram 		(void) snprintf(path, sizeof (path), "%s", GRUB_slice);
1363e7cbe64fSgw25295 	}
1364e7cbe64fSgw25295 
136537eb779cSVikram Hegde 	if (bam_verbose && stat(path, &sb) == 0)
1366f64ca102SToomas Soome 		bam_error(_("unsupported GRUB slice file (%s) exists - "
1367f64ca102SToomas Soome 		    "ignoring.\n"), path);
1368eb2bd662Svikram 
1369eb2bd662Svikram 	if (is_zfs(menu_root)) {
1370eb2bd662Svikram 		assert(strcmp(menu_root, bam_root) == 0);
1371eb2bd662Svikram 		special = get_special(menu_root);
1372eb2bd662Svikram 		INJECT_ERROR1("Z_MENU_GET_SPECIAL", special = NULL);
1373eb2bd662Svikram 		if (special == NULL) {
1374f64ca102SToomas Soome 			bam_error(_("cant find special file for "
1375f64ca102SToomas Soome 			    "mount-point %s\n"), menu_root);
1376eb2bd662Svikram 			return (BAM_ERROR);
1377eb2bd662Svikram 		}
1378eb2bd662Svikram 		pool = strtok(special, "/");
1379eb2bd662Svikram 		INJECT_ERROR1("Z_MENU_GET_POOL", pool = NULL);
1380eb2bd662Svikram 		if (pool == NULL) {
1381eb2bd662Svikram 			free(special);
1382f64ca102SToomas Soome 			bam_error(_("cant find pool for mount-point %s\n"),
1383f64ca102SToomas Soome 			    menu_root);
1384eb2bd662Svikram 			return (BAM_ERROR);
1385eb2bd662Svikram 		}
1386f64ca102SToomas Soome 		BAM_DPRINTF(("%s: derived pool=%s from special\n", fcn, pool));
1387eb2bd662Svikram 
1388eb2bd662Svikram 		zmntpt = mount_top_dataset(pool, &zmnted);
1389eb2bd662Svikram 		INJECT_ERROR1("Z_MENU_MOUNT_TOP_DATASET", zmntpt = NULL);
1390eb2bd662Svikram 		if (zmntpt == NULL) {
1391f64ca102SToomas Soome 			bam_error(_("cannot mount pool dataset for pool: %s\n"),
1392f64ca102SToomas Soome 			    pool);
1393eb2bd662Svikram 			free(special);
1394eb2bd662Svikram 			return (BAM_ERROR);
1395eb2bd662Svikram 		}
1396f64ca102SToomas Soome 		BAM_DPRINTF(("%s: top dataset mountpoint=%s\n", fcn, zmntpt));
1397eb2bd662Svikram 
1398eb2bd662Svikram 		(void) strlcpy(menu_root, zmntpt, sizeof (menu_root));
1399f64ca102SToomas Soome 		BAM_DPRINTF(("%s: zfs menu_root=%s\n", fcn, menu_root));
1400eb2bd662Svikram 	}
1401eb2bd662Svikram 
1402eb2bd662Svikram 	elide_trailing_slash(menu_root, clean_menu_root,
1403eb2bd662Svikram 	    sizeof (clean_menu_root));
1404eb2bd662Svikram 
1405f64ca102SToomas Soome 	BAM_DPRINTF(("%s: cleaned menu root is <%s>\n", fcn, clean_menu_root));
1406eb2bd662Svikram 
1407eb2bd662Svikram 	(void) strlcpy(menu_path, clean_menu_root, sizeof (menu_path));
140840541d5dSvikram 	(void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path));
140940541d5dSvikram 
1410f64ca102SToomas Soome 	BAM_DPRINTF(("%s: menu path is: %s\n", fcn, menu_path));
1411eb2bd662Svikram 
141240541d5dSvikram 	/*
1413eb2bd662Svikram 	 * If listing the menu, display the menu location
141440541d5dSvikram 	 */
141544da779fSWilliam Kucharski 	if (strcmp(subcmd, "list_entry") == 0)
1416f64ca102SToomas Soome 		bam_print(_("the location for the active GRUB menu is: %s\n"),
1417f64ca102SToomas Soome 		    menu_path);
141844da779fSWilliam Kucharski 
141944da779fSWilliam Kucharski 	if ((menu = menu_read(menu_path)) == NULL) {
1420f64ca102SToomas Soome 		bam_error(_("cannot find GRUB menu file: %s\n"), menu_path);
142144da779fSWilliam Kucharski 		free(special);
142244da779fSWilliam Kucharski 
142344da779fSWilliam Kucharski 		return (BAM_ERROR);
142440541d5dSvikram 	}
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	/*
1427eb2bd662Svikram 	 * We already checked the following case in
1428eb2bd662Svikram 	 * check_subcmd_and_suboptions() above. Complete the
1429eb2bd662Svikram 	 * final step now.
14307c478bd9Sstevel@tonic-gate 	 */
14317c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "set_option") == 0) {
1432eb2bd662Svikram 		assert(largc == 1 && largv[0] && largv[1] == NULL);
14337c478bd9Sstevel@tonic-gate 		opt = largv[0];
143444da779fSWilliam Kucharski 	} else if ((strcmp(subcmd, "enable_hypervisor") != 0) &&
143544da779fSWilliam Kucharski 	    (strcmp(subcmd, "list_setting") != 0)) {
1436eb2bd662Svikram 		assert(largc == 0 && largv == NULL);
14377c478bd9Sstevel@tonic-gate 	}
14387c478bd9Sstevel@tonic-gate 
1439eb2bd662Svikram 	ret = get_boot_cap(bam_root);
1440eb2bd662Svikram 	if (ret != BAM_SUCCESS) {
1441f64ca102SToomas Soome 		BAM_DPRINTF(("%s: Failed to get boot capability\n", fcn));
1442eb2bd662Svikram 		goto out;
1443eb2bd662Svikram 	}
1444ae115bc7Smrj 
14457c478bd9Sstevel@tonic-gate 	/*
14467c478bd9Sstevel@tonic-gate 	 * Once the sub-cmd handler has run
14477c478bd9Sstevel@tonic-gate 	 * only the line field is guaranteed to have valid values
14487c478bd9Sstevel@tonic-gate 	 */
144944da779fSWilliam Kucharski 	if (strcmp(subcmd, "update_entry") == 0) {
1450eb2bd662Svikram 		ret = f(menu, menu_root, osdev);
145144da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "upgrade") == 0) {
1452eb2bd662Svikram 		ret = f(menu, bam_root, menu_root);
145344da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "list_entry") == 0) {
14547c478bd9Sstevel@tonic-gate 		ret = f(menu, menu_path, opt);
145544da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "list_setting") == 0) {
145644da779fSWilliam Kucharski 		ret = f(menu, ((largc > 0) ? largv[0] : ""),
145744da779fSWilliam Kucharski 		    ((largc > 1) ? largv[1] : ""));
145844da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "disable_hypervisor") == 0) {
145944da779fSWilliam Kucharski 		if (is_sparc()) {
1460f64ca102SToomas Soome 			bam_error(_("%s operation unsupported on SPARC "
1461f64ca102SToomas Soome 			    "machines\n"), subcmd);
146244da779fSWilliam Kucharski 			ret = BAM_ERROR;
146344da779fSWilliam Kucharski 		} else {
146444da779fSWilliam Kucharski 			ret = f(menu, bam_root, NULL);
146544da779fSWilliam Kucharski 		}
146644da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "enable_hypervisor") == 0) {
146744da779fSWilliam Kucharski 		if (is_sparc()) {
1468f64ca102SToomas Soome 			bam_error(_("%s operation unsupported on SPARC "
1469f64ca102SToomas Soome 			    "machines\n"), subcmd);
147044da779fSWilliam Kucharski 			ret = BAM_ERROR;
147144da779fSWilliam Kucharski 		} else {
147244da779fSWilliam Kucharski 			char *extra_args = NULL;
147344da779fSWilliam Kucharski 
147444da779fSWilliam Kucharski 			/*
147544da779fSWilliam Kucharski 			 * Compress all arguments passed in the largv[] array
147644da779fSWilliam Kucharski 			 * into one string that can then be appended to the
147744da779fSWilliam Kucharski 			 * end of the kernel$ string the routine to enable the
147844da779fSWilliam Kucharski 			 * hypervisor will build.
147944da779fSWilliam Kucharski 			 *
148044da779fSWilliam Kucharski 			 * This allows the caller to supply arbitrary unparsed
148144da779fSWilliam Kucharski 			 * arguments, such as dom0 memory settings or APIC
148244da779fSWilliam Kucharski 			 * options.
148344da779fSWilliam Kucharski 			 *
148444da779fSWilliam Kucharski 			 * This concatenation will be done without ANY syntax
148544da779fSWilliam Kucharski 			 * checking whatsoever, so it's the responsibility of
148644da779fSWilliam Kucharski 			 * the caller to make sure the arguments are valid and
148744da779fSWilliam Kucharski 			 * do not duplicate arguments the conversion routines
148844da779fSWilliam Kucharski 			 * may create.
148944da779fSWilliam Kucharski 			 */
149044da779fSWilliam Kucharski 			if (largc > 0) {
149144da779fSWilliam Kucharski 				int extra_len, i;
149244da779fSWilliam Kucharski 
149344da779fSWilliam Kucharski 				for (extra_len = 0, i = 0; i < largc; i++)
149444da779fSWilliam Kucharski 					extra_len += strlen(largv[i]);
149544da779fSWilliam Kucharski 
149644da779fSWilliam Kucharski 				/*
149744da779fSWilliam Kucharski 				 * Allocate space for argument strings,
149844da779fSWilliam Kucharski 				 * intervening spaces and terminating NULL.
149944da779fSWilliam Kucharski 				 */
150044da779fSWilliam Kucharski 				extra_args = alloca(extra_len + largc);
150144da779fSWilliam Kucharski 
150244da779fSWilliam Kucharski 				(void) strcpy(extra_args, largv[0]);
150344da779fSWilliam Kucharski 
150444da779fSWilliam Kucharski 				for (i = 1; i < largc; i++) {
150544da779fSWilliam Kucharski 					(void) strcat(extra_args, " ");
150644da779fSWilliam Kucharski 					(void) strcat(extra_args, largv[i]);
150744da779fSWilliam Kucharski 				}
150844da779fSWilliam Kucharski 			}
150944da779fSWilliam Kucharski 
151044da779fSWilliam Kucharski 			ret = f(menu, bam_root, extra_args);
151144da779fSWilliam Kucharski 		}
151244da779fSWilliam Kucharski 	} else
1513eb2bd662Svikram 		ret = f(menu, NULL, opt);
1514eb2bd662Svikram 
15157c478bd9Sstevel@tonic-gate 	if (ret == BAM_WRITE) {
1516f64ca102SToomas Soome 		BAM_DPRINTF(("%s: writing menu to clean-menu-root: <%s>\n",
1517f64ca102SToomas Soome 		    fcn, clean_menu_root));
1518eb2bd662Svikram 		ret = menu_write(clean_menu_root, menu);
15197c478bd9Sstevel@tonic-gate 	}
15207c478bd9Sstevel@tonic-gate 
1521eb2bd662Svikram out:
1522eb2bd662Svikram 	INJECT_ERROR1("POOL_SET", pool = "/pooldata");
1523eb2bd662Svikram 	assert((is_zfs(menu_root)) ^ (pool == NULL));
1524eb2bd662Svikram 	if (pool) {
1525eb2bd662Svikram 		(void) umount_top_dataset(pool, zmnted, zmntpt);
1526eb2bd662Svikram 		free(special);
1527eb2bd662Svikram 	}
15287c478bd9Sstevel@tonic-gate 	menu_free(menu);
15297c478bd9Sstevel@tonic-gate 	return (ret);
15307c478bd9Sstevel@tonic-gate }
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate static error_t
15347c478bd9Sstevel@tonic-gate bam_archive(
15357c478bd9Sstevel@tonic-gate 	char *subcmd,
15367c478bd9Sstevel@tonic-gate 	char *opt)
15377c478bd9Sstevel@tonic-gate {
15387c478bd9Sstevel@tonic-gate 	error_t			ret;
15397c478bd9Sstevel@tonic-gate 	error_t			(*f)(char *root, char *opt);
1540eb2bd662Svikram 	const char		*fcn = "bam_archive()";
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 	/*
15438c1b6884Sszhou 	 * Add trailing / for archive subcommands
15448c1b6884Sszhou 	 */
15458c1b6884Sszhou 	if (rootbuf[strlen(rootbuf) - 1] != '/')
15468c1b6884Sszhou 		(void) strcat(rootbuf, "/");
15478c1b6884Sszhou 	bam_rootlen = strlen(rootbuf);
15488c1b6884Sszhou 
15498c1b6884Sszhou 	/*
15507c478bd9Sstevel@tonic-gate 	 * Check arguments
15517c478bd9Sstevel@tonic-gate 	 */
15527c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f);
15537c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS) {
15547c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15557c478bd9Sstevel@tonic-gate 	}
15567c478bd9Sstevel@tonic-gate 
1557eb2bd662Svikram 	ret = get_boot_cap(rootbuf);
1558eb2bd662Svikram 	if (ret != BAM_SUCCESS) {
1559f64ca102SToomas Soome 		BAM_DPRINTF(("%s: Failed to get boot capability\n", fcn));
1560ae115bc7Smrj 		return (ret);
1561eb2bd662Svikram 	}
1562ae115bc7Smrj 
15637c478bd9Sstevel@tonic-gate 	/*
15647c478bd9Sstevel@tonic-gate 	 * Check archive not supported with update_all
15657c478bd9Sstevel@tonic-gate 	 * since it is awkward to display out-of-sync
15667c478bd9Sstevel@tonic-gate 	 * information for each BE.
15677c478bd9Sstevel@tonic-gate 	 */
15687c478bd9Sstevel@tonic-gate 	if (bam_check && strcmp(subcmd, "update_all") == 0) {
1569f64ca102SToomas Soome 		bam_error(_("the check option is not supported with "
1570f64ca102SToomas Soome 		    "subcmd: %s\n"), subcmd);
15717c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15727c478bd9Sstevel@tonic-gate 	}
15737c478bd9Sstevel@tonic-gate 
1574b610f78eSvikram 	if (strcmp(subcmd, "update_all") == 0)
1575b610f78eSvikram 		bam_update_all = 1;
1576b610f78eSvikram 
157779c28b70SToomas Soome #if !defined(_OBP)
15782449e17fSsherrym 	ucode_install(bam_root);
15792449e17fSsherrym #endif
15802449e17fSsherrym 
1581b610f78eSvikram 	ret = f(bam_root, opt);
1582b610f78eSvikram 
1583b610f78eSvikram 	bam_update_all = 0;
1584b610f78eSvikram 
1585b610f78eSvikram 	return (ret);
15867c478bd9Sstevel@tonic-gate }
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1589ae115bc7Smrj void
15907c478bd9Sstevel@tonic-gate bam_error(char *format, ...)
15917c478bd9Sstevel@tonic-gate {
15927c478bd9Sstevel@tonic-gate 	va_list ap;
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	va_start(ap, format);
15957c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", prog);
15967c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
15977c478bd9Sstevel@tonic-gate 	va_end(ap);
15987c478bd9Sstevel@tonic-gate }
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1601eb2bd662Svikram void
1602eb2bd662Svikram bam_derror(char *format, ...)
1603eb2bd662Svikram {
1604eb2bd662Svikram 	va_list ap;
1605eb2bd662Svikram 
1606eb2bd662Svikram 	assert(bam_debug);
1607eb2bd662Svikram 
1608eb2bd662Svikram 	va_start(ap, format);
1609eb2bd662Svikram 	(void) fprintf(stderr, "DEBUG: ");
1610eb2bd662Svikram 	(void) vfprintf(stderr, format, ap);
1611eb2bd662Svikram 	va_end(ap);
1612eb2bd662Svikram }
1613eb2bd662Svikram 
1614eb2bd662Svikram /*PRINTFLIKE1*/
1615eb2bd662Svikram void
16167c478bd9Sstevel@tonic-gate bam_print(char *format, ...)
16177c478bd9Sstevel@tonic-gate {
16187c478bd9Sstevel@tonic-gate 	va_list ap;
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	va_start(ap, format);
16217c478bd9Sstevel@tonic-gate 	(void) vfprintf(stdout, format, ap);
16227c478bd9Sstevel@tonic-gate 	va_end(ap);
16237c478bd9Sstevel@tonic-gate }
16247c478bd9Sstevel@tonic-gate 
1625ae115bc7Smrj /*PRINTFLIKE1*/
1626ae115bc7Smrj void
1627ae115bc7Smrj bam_print_stderr(char *format, ...)
1628ae115bc7Smrj {
1629ae115bc7Smrj 	va_list ap;
1630ae115bc7Smrj 
1631ae115bc7Smrj 	va_start(ap, format);
1632ae115bc7Smrj 	(void) vfprintf(stderr, format, ap);
1633ae115bc7Smrj 	va_end(ap);
1634ae115bc7Smrj }
1635ae115bc7Smrj 
163666b6aef6SWilliam Kucharski void
16377c478bd9Sstevel@tonic-gate bam_exit(int excode)
16387c478bd9Sstevel@tonic-gate {
1639cedc7e57SEnrico Perla - Sun Microsystems 	restore_env();
16407c478bd9Sstevel@tonic-gate 	bam_unlock();
16417c478bd9Sstevel@tonic-gate 	exit(excode);
16427c478bd9Sstevel@tonic-gate }
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate static void
16457c478bd9Sstevel@tonic-gate bam_lock(void)
16467c478bd9Sstevel@tonic-gate {
16477c478bd9Sstevel@tonic-gate 	struct flock lock;
16487c478bd9Sstevel@tonic-gate 	pid_t pid;
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS);
16517c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
16527c478bd9Sstevel@tonic-gate 		/*
16537c478bd9Sstevel@tonic-gate 		 * We may be invoked early in boot for archive verification.
16547c478bd9Sstevel@tonic-gate 		 * In this case, root is readonly and /var/run may not exist.
16557c478bd9Sstevel@tonic-gate 		 * Proceed without the lock
16567c478bd9Sstevel@tonic-gate 		 */
16577c478bd9Sstevel@tonic-gate 		if (errno == EROFS || errno == ENOENT) {
16587c478bd9Sstevel@tonic-gate 			bam_root_readonly = 1;
16597c478bd9Sstevel@tonic-gate 			return;
16607c478bd9Sstevel@tonic-gate 		}
16617c478bd9Sstevel@tonic-gate 
1662f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"),
1663f64ca102SToomas Soome 		    BAM_LOCK_FILE, strerror(errno));
16647c478bd9Sstevel@tonic-gate 		bam_exit(1);
16657c478bd9Sstevel@tonic-gate 	}
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
16687c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
16697c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
16707c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) {
16737c478bd9Sstevel@tonic-gate 		if (errno != EACCES && errno != EAGAIN) {
1674f64ca102SToomas Soome 			bam_error(_("failed to lock file: %s: %s\n"),
1675f64ca102SToomas Soome 			    BAM_LOCK_FILE, strerror(errno));
16767c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
16777c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
16787c478bd9Sstevel@tonic-gate 			bam_exit(1);
16797c478bd9Sstevel@tonic-gate 		}
16807c478bd9Sstevel@tonic-gate 		pid = 0;
16817c478bd9Sstevel@tonic-gate 		(void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0);
1682f64ca102SToomas Soome 		bam_print(
1683f64ca102SToomas Soome 		    _("another instance of bootadm (pid %lu) is running\n"),
1684f64ca102SToomas Soome 		    pid);
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 		lock.l_type = F_WRLCK;
16877c478bd9Sstevel@tonic-gate 		lock.l_whence = SEEK_SET;
16887c478bd9Sstevel@tonic-gate 		lock.l_start = 0;
16897c478bd9Sstevel@tonic-gate 		lock.l_len = 0;
16907c478bd9Sstevel@tonic-gate 		if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) {
1691f64ca102SToomas Soome 			bam_error(_("failed to lock file: %s: %s\n"),
1692f64ca102SToomas Soome 			    BAM_LOCK_FILE, strerror(errno));
16937c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
16947c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
16957c478bd9Sstevel@tonic-gate 			bam_exit(1);
16967c478bd9Sstevel@tonic-gate 		}
16977c478bd9Sstevel@tonic-gate 	}
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 	/* We own the lock now */
17007c478bd9Sstevel@tonic-gate 	pid = getpid();
17017c478bd9Sstevel@tonic-gate 	(void) write(bam_lock_fd, &pid, sizeof (pid));
17027c478bd9Sstevel@tonic-gate }
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate static void
17057c478bd9Sstevel@tonic-gate bam_unlock(void)
17067c478bd9Sstevel@tonic-gate {
17077c478bd9Sstevel@tonic-gate 	struct flock unlock;
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	/*
17107c478bd9Sstevel@tonic-gate 	 * NOP if we don't hold the lock
17117c478bd9Sstevel@tonic-gate 	 */
17127c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
17137c478bd9Sstevel@tonic-gate 		return;
17147c478bd9Sstevel@tonic-gate 	}
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
17177c478bd9Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
17187c478bd9Sstevel@tonic-gate 	unlock.l_start = 0;
17197c478bd9Sstevel@tonic-gate 	unlock.l_len = 0;
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) {
1722f64ca102SToomas Soome 		bam_error(_("failed to unlock file: %s: %s\n"),
1723f64ca102SToomas Soome 		    BAM_LOCK_FILE, strerror(errno));
17247c478bd9Sstevel@tonic-gate 	}
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 	if (close(bam_lock_fd) == -1) {
1727f64ca102SToomas Soome 		bam_error(_("failed to close file: %s: %s\n"),
1728f64ca102SToomas Soome 		    BAM_LOCK_FILE, strerror(errno));
17297c478bd9Sstevel@tonic-gate 	}
17307c478bd9Sstevel@tonic-gate 	bam_lock_fd = -1;
17317c478bd9Sstevel@tonic-gate }
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate static error_t
17347c478bd9Sstevel@tonic-gate list_archive(char *root, char *opt)
17357c478bd9Sstevel@tonic-gate {
17367c478bd9Sstevel@tonic-gate 	filelist_t flist;
17377c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
17387c478bd9Sstevel@tonic-gate 	line_t *lp;
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 	assert(root);
17417c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
17447c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
17457c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
17467c478bd9Sstevel@tonic-gate 	}
17477c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
1750f64ca102SToomas Soome 		bam_print(_("%s\n"), lp->line);
17517c478bd9Sstevel@tonic-gate 	}
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
17567c478bd9Sstevel@tonic-gate }
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate /*
17597c478bd9Sstevel@tonic-gate  * This routine writes a list of lines to a file.
17607c478bd9Sstevel@tonic-gate  * The list is *not* freed
17617c478bd9Sstevel@tonic-gate  */
17627c478bd9Sstevel@tonic-gate static error_t
17637c478bd9Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start)
17647c478bd9Sstevel@tonic-gate {
17657c478bd9Sstevel@tonic-gate 	char		tmpfile[PATH_MAX];
17667c478bd9Sstevel@tonic-gate 	char		path[PATH_MAX];
17677c478bd9Sstevel@tonic-gate 	FILE		*fp;
17687c478bd9Sstevel@tonic-gate 	int		ret;
17697c478bd9Sstevel@tonic-gate 	struct stat	sb;
17707c478bd9Sstevel@tonic-gate 	mode_t		mode;
17717c478bd9Sstevel@tonic-gate 	uid_t		root_uid;
17727c478bd9Sstevel@tonic-gate 	gid_t		sys_gid;
17737c478bd9Sstevel@tonic-gate 	struct passwd	*pw;
17747c478bd9Sstevel@tonic-gate 	struct group	*gp;
1775eb2bd662Svikram 	const char	*fcn = "list2file()";
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, final);
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	if (start == NULL) {
1780eb2bd662Svikram 		/* Empty GRUB menu */
17817c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != -1) {
1782f64ca102SToomas Soome 			bam_print(_("file is empty, deleting file: %s\n"),
1783f64ca102SToomas Soome 			    path);
17847c478bd9Sstevel@tonic-gate 			if (unlink(path) != 0) {
1785f64ca102SToomas Soome 				bam_error(_("failed to unlink file: %s: %s\n"),
1786f64ca102SToomas Soome 				    path, strerror(errno));
17877c478bd9Sstevel@tonic-gate 				return (BAM_ERROR);
17887c478bd9Sstevel@tonic-gate 			} else {
17897c478bd9Sstevel@tonic-gate 				return (BAM_SUCCESS);
17907c478bd9Sstevel@tonic-gate 			}
17917c478bd9Sstevel@tonic-gate 		}
1792eb2bd662Svikram 		return (BAM_SUCCESS);
17937c478bd9Sstevel@tonic-gate 	}
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	/*
17967c478bd9Sstevel@tonic-gate 	 * Preserve attributes of existing file if possible,
17977c478bd9Sstevel@tonic-gate 	 * otherwise ask the system for uid/gid of root/sys.
17987c478bd9Sstevel@tonic-gate 	 * If all fails, fall back on hard-coded defaults.
17997c478bd9Sstevel@tonic-gate 	 */
18007c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != -1) {
18017c478bd9Sstevel@tonic-gate 		mode = sb.st_mode;
18027c478bd9Sstevel@tonic-gate 		root_uid = sb.st_uid;
18037c478bd9Sstevel@tonic-gate 		sys_gid = sb.st_gid;
18047c478bd9Sstevel@tonic-gate 	} else {
18057c478bd9Sstevel@tonic-gate 		mode = DEFAULT_DEV_MODE;
18067c478bd9Sstevel@tonic-gate 		if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
18077c478bd9Sstevel@tonic-gate 			root_uid = pw->pw_uid;
18087c478bd9Sstevel@tonic-gate 		} else {
1809f64ca102SToomas Soome 			bam_error(_("getpwnam: uid for %s failed, "
1810f64ca102SToomas Soome 			    "defaulting to %d\n"),
18117c478bd9Sstevel@tonic-gate 			    DEFAULT_DEV_USER, DEFAULT_DEV_UID);
18127c478bd9Sstevel@tonic-gate 			root_uid = (uid_t)DEFAULT_DEV_UID;
18137c478bd9Sstevel@tonic-gate 		}
18147c478bd9Sstevel@tonic-gate 		if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
18157c478bd9Sstevel@tonic-gate 			sys_gid = gp->gr_gid;
18167c478bd9Sstevel@tonic-gate 		} else {
1817f64ca102SToomas Soome 			bam_error(_("getgrnam: gid for %s failed, "
1818f64ca102SToomas Soome 			    "defaulting to %d\n"),
18197c478bd9Sstevel@tonic-gate 			    DEFAULT_DEV_GROUP, DEFAULT_DEV_GID);
18207c478bd9Sstevel@tonic-gate 			sys_gid = (gid_t)DEFAULT_DEV_GID;
18217c478bd9Sstevel@tonic-gate 		}
18227c478bd9Sstevel@tonic-gate 	}
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	(void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp);
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	/* Truncate tmpfile first */
18277c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "w");
18287c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
1829f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), tmpfile,
1830f64ca102SToomas Soome 		    strerror(errno));
18317c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18327c478bd9Sstevel@tonic-gate 	}
1833963390b4Svikram 	ret = fclose(fp);
1834963390b4Svikram 	INJECT_ERROR1("LIST2FILE_TRUNC_FCLOSE", ret = EOF);
1835963390b4Svikram 	if (ret == EOF) {
1836f64ca102SToomas Soome 		bam_error(_("failed to close file: %s: %s\n"),
1837f64ca102SToomas Soome 		    tmpfile, strerror(errno));
18387c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18397c478bd9Sstevel@tonic-gate 	}
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	/* Now open it in append mode */
18427c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "a");
18437c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
1844f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), tmpfile,
1845f64ca102SToomas Soome 		    strerror(errno));
18467c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18477c478bd9Sstevel@tonic-gate 	}
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	for (; start; start = start->next) {
1850963390b4Svikram 		ret = s_fputs(start->line, fp);
1851963390b4Svikram 		INJECT_ERROR1("LIST2FILE_FPUTS", ret = EOF);
1852963390b4Svikram 		if (ret == EOF) {
1853f64ca102SToomas Soome 			bam_error(_("write to file failed: %s: %s\n"),
1854f64ca102SToomas Soome 			    tmpfile, strerror(errno));
18557c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
18567c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
18577c478bd9Sstevel@tonic-gate 		}
18587c478bd9Sstevel@tonic-gate 	}
18597c478bd9Sstevel@tonic-gate 
1860963390b4Svikram 	ret = fclose(fp);
1861963390b4Svikram 	INJECT_ERROR1("LIST2FILE_APPEND_FCLOSE", ret = EOF);
1862963390b4Svikram 	if (ret == EOF) {
1863f64ca102SToomas Soome 		bam_error(_("failed to close file: %s: %s\n"),
1864f64ca102SToomas Soome 		    tmpfile, strerror(errno));
18657c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18667c478bd9Sstevel@tonic-gate 	}
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	/*
1869de531be4Sjg 	 * Set up desired attributes.  Ignore failures on filesystems
1870de531be4Sjg 	 * not supporting these operations - pcfs reports unsupported
1871de531be4Sjg 	 * operations as EINVAL.
18727c478bd9Sstevel@tonic-gate 	 */
18737c478bd9Sstevel@tonic-gate 	ret = chmod(tmpfile, mode);
1874de531be4Sjg 	if (ret == -1 &&
1875de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
1876f64ca102SToomas Soome 		bam_error(_("chmod operation on %s failed - %s\n"),
1877f64ca102SToomas Soome 		    tmpfile, strerror(errno));
18787c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18797c478bd9Sstevel@tonic-gate 	}
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 	ret = chown(tmpfile, root_uid, sys_gid);
1882de531be4Sjg 	if (ret == -1 &&
1883de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
1884f64ca102SToomas Soome 		bam_error(_("chgrp operation on %s failed - %s\n"),
1885f64ca102SToomas Soome 		    tmpfile, strerror(errno));
18867c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18877c478bd9Sstevel@tonic-gate 	}
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 	/*
18907c478bd9Sstevel@tonic-gate 	 * Do an atomic rename
18917c478bd9Sstevel@tonic-gate 	 */
18927c478bd9Sstevel@tonic-gate 	ret = rename(tmpfile, path);
1893963390b4Svikram 	INJECT_ERROR1("LIST2FILE_RENAME", ret = -1);
18947c478bd9Sstevel@tonic-gate 	if (ret != 0) {
1895f64ca102SToomas Soome 		bam_error(_("rename to file failed: %s: %s\n"), path,
1896f64ca102SToomas Soome 		    strerror(errno));
18977c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18987c478bd9Sstevel@tonic-gate 	}
18997c478bd9Sstevel@tonic-gate 
1900f64ca102SToomas Soome 	BAM_DPRINTF(("%s: wrote file successfully: %s\n", fcn, path));
19017c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
19027c478bd9Sstevel@tonic-gate }
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate /*
190548847494SEnrico Perla - Sun Microsystems  * Checks if the path specified (without the file name at the end) exists
190648847494SEnrico Perla - Sun Microsystems  * and creates it if not. If the path exists and is not a directory, an attempt
190748847494SEnrico Perla - Sun Microsystems  * to unlink is made.
19087c478bd9Sstevel@tonic-gate  */
190948847494SEnrico Perla - Sun Microsystems static int
191048847494SEnrico Perla - Sun Microsystems setup_path(char *path)
191148847494SEnrico Perla - Sun Microsystems {
191248847494SEnrico Perla - Sun Microsystems 	char 		*p;
191348847494SEnrico Perla - Sun Microsystems 	int		ret;
191448847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
191548847494SEnrico Perla - Sun Microsystems 
191648847494SEnrico Perla - Sun Microsystems 	p = strrchr(path, '/');
191748847494SEnrico Perla - Sun Microsystems 	if (p != NULL) {
191848847494SEnrico Perla - Sun Microsystems 		*p = '\0';
191948847494SEnrico Perla - Sun Microsystems 		if (stat(path, &sb) != 0 || !(S_ISDIR(sb.st_mode))) {
192048847494SEnrico Perla - Sun Microsystems 			/* best effort attempt, mkdirp will catch the error */
192148847494SEnrico Perla - Sun Microsystems 			(void) unlink(path);
192248847494SEnrico Perla - Sun Microsystems 			if (bam_verbose)
1923f64ca102SToomas Soome 				bam_print(_("need to create directory "
1924f64ca102SToomas Soome 				    "path for %s\n"), path);
192548847494SEnrico Perla - Sun Microsystems 			ret = mkdirp(path, DIR_PERMS);
192648847494SEnrico Perla - Sun Microsystems 			if (ret == -1) {
1927f64ca102SToomas Soome 				bam_error(_("mkdir of %s failed: %s\n"),
1928f64ca102SToomas Soome 				    path, strerror(errno));
192948847494SEnrico Perla - Sun Microsystems 				*p = '/';
193048847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
193148847494SEnrico Perla - Sun Microsystems 			}
193248847494SEnrico Perla - Sun Microsystems 		}
193348847494SEnrico Perla - Sun Microsystems 		*p = '/';
193448847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
193548847494SEnrico Perla - Sun Microsystems 	}
193648847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
193748847494SEnrico Perla - Sun Microsystems }
193848847494SEnrico Perla - Sun Microsystems 
193948847494SEnrico Perla - Sun Microsystems typedef union {
194048847494SEnrico Perla - Sun Microsystems 	gzFile	gzfile;
194148847494SEnrico Perla - Sun Microsystems 	int	fdfile;
194248847494SEnrico Perla - Sun Microsystems } outfile;
194348847494SEnrico Perla - Sun Microsystems 
194448847494SEnrico Perla - Sun Microsystems typedef struct {
194548847494SEnrico Perla - Sun Microsystems 	char		path[PATH_MAX];
194648847494SEnrico Perla - Sun Microsystems 	outfile		out;
194748847494SEnrico Perla - Sun Microsystems } cachefile;
194848847494SEnrico Perla - Sun Microsystems 
194948847494SEnrico Perla - Sun Microsystems static int
195048847494SEnrico Perla - Sun Microsystems setup_file(char *base, const char *path, cachefile *cf)
195148847494SEnrico Perla - Sun Microsystems {
195248847494SEnrico Perla - Sun Microsystems 	int	ret;
195348847494SEnrico Perla - Sun Microsystems 	char	*strip;
195448847494SEnrico Perla - Sun Microsystems 
195548847494SEnrico Perla - Sun Microsystems 	/* init gzfile or fdfile in case we fail before opening */
195648847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT)
195748847494SEnrico Perla - Sun Microsystems 		cf->out.gzfile = NULL;
195848847494SEnrico Perla - Sun Microsystems 	else
195948847494SEnrico Perla - Sun Microsystems 		cf->out.fdfile = -1;
196048847494SEnrico Perla - Sun Microsystems 
196148847494SEnrico Perla - Sun Microsystems 	/* strip the trailing altroot path */
196248847494SEnrico Perla - Sun Microsystems 	strip = (char *)path + strlen(rootbuf);
196348847494SEnrico Perla - Sun Microsystems 
196448847494SEnrico Perla - Sun Microsystems 	ret = snprintf(cf->path, sizeof (cf->path), "%s/%s", base, strip);
196548847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (cf->path)) {
1966f64ca102SToomas Soome 		bam_error(_("unable to create path on mountpoint %s, "
1967f64ca102SToomas Soome 		    "path too long\n"), rootbuf);
196848847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
196948847494SEnrico Perla - Sun Microsystems 	}
197048847494SEnrico Perla - Sun Microsystems 
197148847494SEnrico Perla - Sun Microsystems 	/* Check if path is present in the archive cache directory */
197248847494SEnrico Perla - Sun Microsystems 	if (setup_path(cf->path) == BAM_ERROR)
197348847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
197448847494SEnrico Perla - Sun Microsystems 
197548847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT) {
197648847494SEnrico Perla - Sun Microsystems 		if ((cf->out.gzfile = gzopen(cf->path, "wb")) == NULL) {
1977f64ca102SToomas Soome 			bam_error(_("failed to open file: %s: %s\n"),
1978f64ca102SToomas Soome 			    cf->path, strerror(errno));
197948847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
198048847494SEnrico Perla - Sun Microsystems 		}
198148847494SEnrico Perla - Sun Microsystems 		(void) gzsetparams(cf->out.gzfile, Z_BEST_SPEED,
198248847494SEnrico Perla - Sun Microsystems 		    Z_DEFAULT_STRATEGY);
198348847494SEnrico Perla - Sun Microsystems 	} else {
198448847494SEnrico Perla - Sun Microsystems 		if ((cf->out.fdfile = open(cf->path, O_WRONLY | O_CREAT, 0644))
198548847494SEnrico Perla - Sun Microsystems 		    == -1) {
1986f64ca102SToomas Soome 			bam_error(_("failed to open file: %s: %s\n"),
1987f64ca102SToomas Soome 			    cf->path, strerror(errno));
198848847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
198948847494SEnrico Perla - Sun Microsystems 		}
199048847494SEnrico Perla - Sun Microsystems 	}
199148847494SEnrico Perla - Sun Microsystems 
199248847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
199348847494SEnrico Perla - Sun Microsystems }
199448847494SEnrico Perla - Sun Microsystems 
199548847494SEnrico Perla - Sun Microsystems static int
199648847494SEnrico Perla - Sun Microsystems cache_write(cachefile cf, char *buf, int size)
199748847494SEnrico Perla - Sun Microsystems {
199848847494SEnrico Perla - Sun Microsystems 	int	err;
199948847494SEnrico Perla - Sun Microsystems 
200048847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT) {
200148847494SEnrico Perla - Sun Microsystems 		if (gzwrite(cf.out.gzfile, buf, size) < 1) {
2002f64ca102SToomas Soome 			bam_error(_("failed to write to %s\n"),
2003f64ca102SToomas Soome 			    gzerror(cf.out.gzfile, &err));
2004cedc7e57SEnrico Perla - Sun Microsystems 			if (err == Z_ERRNO && bam_verbose) {
2005f64ca102SToomas Soome 				bam_error(_("write to file failed: %s: %s\n"),
2006f64ca102SToomas Soome 				    cf.path, strerror(errno));
2007cedc7e57SEnrico Perla - Sun Microsystems 			}
200848847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
200948847494SEnrico Perla - Sun Microsystems 		}
201048847494SEnrico Perla - Sun Microsystems 	} else {
201148847494SEnrico Perla - Sun Microsystems 		if (write(cf.out.fdfile, buf, size) < 1) {
2012f64ca102SToomas Soome 			bam_error(_("write to file failed: %s: %s\n"),
2013f64ca102SToomas Soome 			    cf.path, strerror(errno));
201448847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
201548847494SEnrico Perla - Sun Microsystems 		}
201648847494SEnrico Perla - Sun Microsystems 	}
201748847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
201848847494SEnrico Perla - Sun Microsystems }
201948847494SEnrico Perla - Sun Microsystems 
202048847494SEnrico Perla - Sun Microsystems static int
202148847494SEnrico Perla - Sun Microsystems cache_close(cachefile cf)
202248847494SEnrico Perla - Sun Microsystems {
202348847494SEnrico Perla - Sun Microsystems 	int	ret;
202448847494SEnrico Perla - Sun Microsystems 
202548847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT) {
202648847494SEnrico Perla - Sun Microsystems 		if (cf.out.gzfile) {
202748847494SEnrico Perla - Sun Microsystems 			ret = gzclose(cf.out.gzfile);
2028cedc7e57SEnrico Perla - Sun Microsystems 			if (ret != Z_OK) {
2029f64ca102SToomas Soome 				bam_error(_("failed to close file: %s: %s\n"),
2030f64ca102SToomas Soome 				    cf.path, strerror(errno));
203148847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
203248847494SEnrico Perla - Sun Microsystems 			}
2033cedc7e57SEnrico Perla - Sun Microsystems 		}
203448847494SEnrico Perla - Sun Microsystems 	} else {
203548847494SEnrico Perla - Sun Microsystems 		if (cf.out.fdfile != -1) {
203648847494SEnrico Perla - Sun Microsystems 			ret = close(cf.out.fdfile);
203748847494SEnrico Perla - Sun Microsystems 			if (ret != 0) {
2038f64ca102SToomas Soome 				bam_error(_("failed to close file: %s: %s\n"),
2039f64ca102SToomas Soome 				    cf.path, strerror(errno));
204048847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
204148847494SEnrico Perla - Sun Microsystems 			}
204248847494SEnrico Perla - Sun Microsystems 		}
204348847494SEnrico Perla - Sun Microsystems 	}
204448847494SEnrico Perla - Sun Microsystems 
204548847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
204648847494SEnrico Perla - Sun Microsystems }
204748847494SEnrico Perla - Sun Microsystems 
204848847494SEnrico Perla - Sun Microsystems static int
204948847494SEnrico Perla - Sun Microsystems dircache_updatefile(const char *path, int what)
205048847494SEnrico Perla - Sun Microsystems {
205148847494SEnrico Perla - Sun Microsystems 	int 		ret, exitcode;
205248847494SEnrico Perla - Sun Microsystems 	char 		buf[4096 * 4];
205348847494SEnrico Perla - Sun Microsystems 	FILE 		*infile;
205448847494SEnrico Perla - Sun Microsystems 	cachefile 	outfile, outupdt;
205548847494SEnrico Perla - Sun Microsystems 
2056cedc7e57SEnrico Perla - Sun Microsystems 	if (bam_nowrite()) {
2057cedc7e57SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NEED_UPDATE);
2058cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
2059cedc7e57SEnrico Perla - Sun Microsystems 	}
2060cedc7e57SEnrico Perla - Sun Microsystems 
2061cedc7e57SEnrico Perla - Sun Microsystems 	if (!has_cachedir(what))
206248847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
206348847494SEnrico Perla - Sun Microsystems 
206448847494SEnrico Perla - Sun Microsystems 	if ((infile = fopen(path, "rb")) == NULL) {
2065f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), path,
2066f64ca102SToomas Soome 		    strerror(errno));
206748847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
206848847494SEnrico Perla - Sun Microsystems 	}
206948847494SEnrico Perla - Sun Microsystems 
207048847494SEnrico Perla - Sun Microsystems 	ret = setup_file(get_cachedir(what), path, &outfile);
207148847494SEnrico Perla - Sun Microsystems 	if (ret == BAM_ERROR) {
207248847494SEnrico Perla - Sun Microsystems 		exitcode = BAM_ERROR;
207348847494SEnrico Perla - Sun Microsystems 		goto out;
207448847494SEnrico Perla - Sun Microsystems 	}
207548847494SEnrico Perla - Sun Microsystems 	if (!is_dir_flag_on(what, NO_MULTI)) {
207648847494SEnrico Perla - Sun Microsystems 		ret = setup_file(get_updatedir(what), path, &outupdt);
207748847494SEnrico Perla - Sun Microsystems 		if (ret == BAM_ERROR)
207848847494SEnrico Perla - Sun Microsystems 			set_dir_flag(what, NO_MULTI);
207948847494SEnrico Perla - Sun Microsystems 	}
208048847494SEnrico Perla - Sun Microsystems 
208148847494SEnrico Perla - Sun Microsystems 	while ((ret = fread(buf, 1, sizeof (buf), infile)) > 0) {
208248847494SEnrico Perla - Sun Microsystems 		if (cache_write(outfile, buf, ret) == BAM_ERROR) {
208348847494SEnrico Perla - Sun Microsystems 			exitcode = BAM_ERROR;
208448847494SEnrico Perla - Sun Microsystems 			goto out;
208548847494SEnrico Perla - Sun Microsystems 		}
208648847494SEnrico Perla - Sun Microsystems 		if (!is_dir_flag_on(what, NO_MULTI))
208748847494SEnrico Perla - Sun Microsystems 			if (cache_write(outupdt, buf, ret) == BAM_ERROR)
208848847494SEnrico Perla - Sun Microsystems 				set_dir_flag(what, NO_MULTI);
208948847494SEnrico Perla - Sun Microsystems 	}
209048847494SEnrico Perla - Sun Microsystems 
209148847494SEnrico Perla - Sun Microsystems 	set_dir_flag(what, NEED_UPDATE);
209248847494SEnrico Perla - Sun Microsystems 	get_count(what)++;
2093cedc7e57SEnrico Perla - Sun Microsystems 	if (get_count(what) > COUNT_MAX)
2094cedc7e57SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
209548847494SEnrico Perla - Sun Microsystems 	exitcode = BAM_SUCCESS;
209648847494SEnrico Perla - Sun Microsystems out:
209748847494SEnrico Perla - Sun Microsystems 	(void) fclose(infile);
209848847494SEnrico Perla - Sun Microsystems 	if (cache_close(outfile) == BAM_ERROR)
209948847494SEnrico Perla - Sun Microsystems 		exitcode = BAM_ERROR;
210048847494SEnrico Perla - Sun Microsystems 	if (!is_dir_flag_on(what, NO_MULTI) &&
210148847494SEnrico Perla - Sun Microsystems 	    cache_close(outupdt) == BAM_ERROR)
210248847494SEnrico Perla - Sun Microsystems 		exitcode = BAM_ERROR;
210348847494SEnrico Perla - Sun Microsystems 	if (exitcode == BAM_ERROR)
210448847494SEnrico Perla - Sun Microsystems 		set_flag(UPDATE_ERROR);
210548847494SEnrico Perla - Sun Microsystems 	return (exitcode);
210648847494SEnrico Perla - Sun Microsystems }
210748847494SEnrico Perla - Sun Microsystems 
210848847494SEnrico Perla - Sun Microsystems static int
210948847494SEnrico Perla - Sun Microsystems dircache_updatedir(const char *path, int what, int updt)
211048847494SEnrico Perla - Sun Microsystems {
211148847494SEnrico Perla - Sun Microsystems 	int		ret;
211248847494SEnrico Perla - Sun Microsystems 	char		dpath[PATH_MAX];
211348847494SEnrico Perla - Sun Microsystems 	char		*strip;
211448847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
211548847494SEnrico Perla - Sun Microsystems 
211648847494SEnrico Perla - Sun Microsystems 	strip = (char *)path + strlen(rootbuf);
211748847494SEnrico Perla - Sun Microsystems 
211848847494SEnrico Perla - Sun Microsystems 	ret = snprintf(dpath, sizeof (dpath), "%s/%s", updt ?
211948847494SEnrico Perla - Sun Microsystems 	    get_updatedir(what) : get_cachedir(what), strip);
212048847494SEnrico Perla - Sun Microsystems 
212148847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (dpath)) {
2122f64ca102SToomas Soome 		bam_error(_("unable to create path on mountpoint %s, "
2123f64ca102SToomas Soome 		    "path too long\n"), rootbuf);
212448847494SEnrico Perla - Sun Microsystems 		set_flag(UPDATE_ERROR);
212548847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
212648847494SEnrico Perla - Sun Microsystems 	}
212748847494SEnrico Perla - Sun Microsystems 
212848847494SEnrico Perla - Sun Microsystems 	if (stat(dpath, &sb) == 0 && S_ISDIR(sb.st_mode))
212948847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
213048847494SEnrico Perla - Sun Microsystems 
213148847494SEnrico Perla - Sun Microsystems 	if (updt) {
213248847494SEnrico Perla - Sun Microsystems 		if (!is_dir_flag_on(what, NO_MULTI))
213348847494SEnrico Perla - Sun Microsystems 			if (!bam_nowrite() && mkdirp(dpath, DIR_PERMS) == -1)
213448847494SEnrico Perla - Sun Microsystems 				set_dir_flag(what, NO_MULTI);
213548847494SEnrico Perla - Sun Microsystems 	} else {
213648847494SEnrico Perla - Sun Microsystems 		if (!bam_nowrite() && mkdirp(dpath, DIR_PERMS) == -1) {
213748847494SEnrico Perla - Sun Microsystems 			set_flag(UPDATE_ERROR);
213848847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
213948847494SEnrico Perla - Sun Microsystems 		}
214048847494SEnrico Perla - Sun Microsystems 	}
214148847494SEnrico Perla - Sun Microsystems 
214248847494SEnrico Perla - Sun Microsystems 	set_dir_flag(what, NEED_UPDATE);
214348847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
214448847494SEnrico Perla - Sun Microsystems }
214548847494SEnrico Perla - Sun Microsystems 
214648847494SEnrico Perla - Sun Microsystems #define	DO_CACHE_DIR	0
214748847494SEnrico Perla - Sun Microsystems #define	DO_UPDATE_DIR	1
214848847494SEnrico Perla - Sun Microsystems 
214948847494SEnrico Perla - Sun Microsystems #if defined(_LP64) || defined(_LONGLONG_TYPE)
215048847494SEnrico Perla - Sun Microsystems typedef		Elf64_Ehdr	_elfhdr;
215148847494SEnrico Perla - Sun Microsystems #else
215248847494SEnrico Perla - Sun Microsystems typedef		Elf32_Ehdr	_elfhdr;
215348847494SEnrico Perla - Sun Microsystems #endif
215448847494SEnrico Perla - Sun Microsystems 
215548847494SEnrico Perla - Sun Microsystems /*
215648847494SEnrico Perla - Sun Microsystems  * This routine updates the contents of the cache directory
215748847494SEnrico Perla - Sun Microsystems  */
215848847494SEnrico Perla - Sun Microsystems static int
215948847494SEnrico Perla - Sun Microsystems update_dircache(const char *path, int flags)
216048847494SEnrico Perla - Sun Microsystems {
216148847494SEnrico Perla - Sun Microsystems 	int rc = BAM_SUCCESS;
216248847494SEnrico Perla - Sun Microsystems 
216348847494SEnrico Perla - Sun Microsystems 	switch (flags) {
216448847494SEnrico Perla - Sun Microsystems 	case FTW_F:
216548847494SEnrico Perla - Sun Microsystems 		{
216648847494SEnrico Perla - Sun Microsystems 		int	fd;
216748847494SEnrico Perla - Sun Microsystems 		_elfhdr	elf;
216848847494SEnrico Perla - Sun Microsystems 
216948847494SEnrico Perla - Sun Microsystems 		if ((fd = open(path, O_RDONLY)) < 0) {
2170f64ca102SToomas Soome 			bam_error(_("failed to open file: %s: %s\n"),
2171f64ca102SToomas Soome 			    path, strerror(errno));
217248847494SEnrico Perla - Sun Microsystems 			set_flag(UPDATE_ERROR);
217348847494SEnrico Perla - Sun Microsystems 			rc = BAM_ERROR;
217448847494SEnrico Perla - Sun Microsystems 			break;
217548847494SEnrico Perla - Sun Microsystems 		}
217648847494SEnrico Perla - Sun Microsystems 
217748847494SEnrico Perla - Sun Microsystems 		/*
217848847494SEnrico Perla - Sun Microsystems 		 * libelf and gelf would be a cleaner and easier way to handle
217948847494SEnrico Perla - Sun Microsystems 		 * this, but libelf fails compilation if _ILP32 is defined &&
218048847494SEnrico Perla - Sun Microsystems 		 * _FILE_OFFSET_BITS is != 32 ...
218148847494SEnrico Perla - Sun Microsystems 		 */
218248847494SEnrico Perla - Sun Microsystems 		if (read(fd, (void *)&elf, sizeof (_elfhdr)) < 0) {
2183f64ca102SToomas Soome 			bam_error(_("read failed for file: %s: %s\n"),
2184f64ca102SToomas Soome 			    path, strerror(errno));
218548847494SEnrico Perla - Sun Microsystems 			set_flag(UPDATE_ERROR);
218648847494SEnrico Perla - Sun Microsystems 			(void) close(fd);
218748847494SEnrico Perla - Sun Microsystems 			rc = BAM_ERROR;
218848847494SEnrico Perla - Sun Microsystems 			break;
218948847494SEnrico Perla - Sun Microsystems 		}
219048847494SEnrico Perla - Sun Microsystems 		(void) close(fd);
219148847494SEnrico Perla - Sun Microsystems 
219248847494SEnrico Perla - Sun Microsystems 		/*
219348847494SEnrico Perla - Sun Microsystems 		 * If the file is not an executable and is not inside an amd64
219448847494SEnrico Perla - Sun Microsystems 		 * directory, we copy it in both the cache directories,
219548847494SEnrico Perla - Sun Microsystems 		 * otherwise, we only copy it inside the 64-bit one.
219648847494SEnrico Perla - Sun Microsystems 		 */
219748847494SEnrico Perla - Sun Microsystems 		if (memcmp(elf.e_ident, ELFMAG, 4) != 0) {
219848847494SEnrico Perla - Sun Microsystems 			if (strstr(path, "/amd64")) {
219948847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE64);
220048847494SEnrico Perla - Sun Microsystems 			} else {
220148847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE32);
2202cedc7e57SEnrico Perla - Sun Microsystems 				if (rc == BAM_SUCCESS)
2203cedc7e57SEnrico Perla - Sun Microsystems 					rc = dircache_updatefile(path, FILE64);
220448847494SEnrico Perla - Sun Microsystems 			}
220548847494SEnrico Perla - Sun Microsystems 		} else {
220648847494SEnrico Perla - Sun Microsystems 			/*
220748847494SEnrico Perla - Sun Microsystems 			 * Based on the ELF class we copy the file in the 32-bit
220848847494SEnrico Perla - Sun Microsystems 			 * or the 64-bit cache directory.
220948847494SEnrico Perla - Sun Microsystems 			 */
2210cedc7e57SEnrico Perla - Sun Microsystems 			if (elf.e_ident[EI_CLASS] == ELFCLASS32) {
221148847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE32);
2212cedc7e57SEnrico Perla - Sun Microsystems 			} else if (elf.e_ident[EI_CLASS] == ELFCLASS64) {
221348847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE64);
2214cedc7e57SEnrico Perla - Sun Microsystems 			} else {
2215f64ca102SToomas Soome 				bam_print(_("WARNING: file %s is neither a "
2216f64ca102SToomas Soome 				    "32-bit nor a 64-bit ELF\n"), path);
221748847494SEnrico Perla - Sun Microsystems 				/* paranoid */
221848847494SEnrico Perla - Sun Microsystems 				rc  = dircache_updatefile(path, FILE32);
221948847494SEnrico Perla - Sun Microsystems 				if (rc == BAM_SUCCESS)
222048847494SEnrico Perla - Sun Microsystems 					rc = dircache_updatefile(path, FILE64);
222148847494SEnrico Perla - Sun Microsystems 			}
222248847494SEnrico Perla - Sun Microsystems 		}
222348847494SEnrico Perla - Sun Microsystems 		break;
222448847494SEnrico Perla - Sun Microsystems 		}
222548847494SEnrico Perla - Sun Microsystems 	case FTW_D:
222648847494SEnrico Perla - Sun Microsystems 		if (strstr(path, "/amd64") == NULL) {
222748847494SEnrico Perla - Sun Microsystems 			rc = dircache_updatedir(path, FILE32, DO_UPDATE_DIR);
222848847494SEnrico Perla - Sun Microsystems 			if (rc == BAM_SUCCESS)
222948847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatedir(path, FILE32,
223048847494SEnrico Perla - Sun Microsystems 				    DO_CACHE_DIR);
223148847494SEnrico Perla - Sun Microsystems 		} else {
223248847494SEnrico Perla - Sun Microsystems 			if (has_cachedir(FILE64)) {
223348847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatedir(path, FILE64,
223448847494SEnrico Perla - Sun Microsystems 				    DO_UPDATE_DIR);
223548847494SEnrico Perla - Sun Microsystems 				if (rc == BAM_SUCCESS)
223648847494SEnrico Perla - Sun Microsystems 					rc = dircache_updatedir(path, FILE64,
223748847494SEnrico Perla - Sun Microsystems 					    DO_CACHE_DIR);
223848847494SEnrico Perla - Sun Microsystems 			}
223948847494SEnrico Perla - Sun Microsystems 		}
224048847494SEnrico Perla - Sun Microsystems 		break;
224148847494SEnrico Perla - Sun Microsystems 	default:
224248847494SEnrico Perla - Sun Microsystems 		rc = BAM_ERROR;
224348847494SEnrico Perla - Sun Microsystems 		break;
224448847494SEnrico Perla - Sun Microsystems 	}
224548847494SEnrico Perla - Sun Microsystems 
224648847494SEnrico Perla - Sun Microsystems 	return (rc);
224748847494SEnrico Perla - Sun Microsystems }
224848847494SEnrico Perla - Sun Microsystems 
22497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
22507c478bd9Sstevel@tonic-gate static int
22517c478bd9Sstevel@tonic-gate cmpstat(
22527c478bd9Sstevel@tonic-gate 	const char *file,
225348847494SEnrico Perla - Sun Microsystems 	const struct stat *st,
22547c478bd9Sstevel@tonic-gate 	int flags,
22557c478bd9Sstevel@tonic-gate 	struct FTW *ftw)
22567c478bd9Sstevel@tonic-gate {
22577c478bd9Sstevel@tonic-gate 	uint_t 		sz;
22587c478bd9Sstevel@tonic-gate 	uint64_t 	*value;
22597c478bd9Sstevel@tonic-gate 	uint64_t 	filestat[2];
226089c3ee43SGangadhar Mylapuram 	int 		error, ret, status;
22617c478bd9Sstevel@tonic-gate 
226258091fd8Ssetje 	struct safefile *safefilep;
226358091fd8Ssetje 	FILE 		*fp;
226448847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
226589c3ee43SGangadhar Mylapuram 	regex_t re;
226658091fd8Ssetje 
22677c478bd9Sstevel@tonic-gate 	/*
226848847494SEnrico Perla - Sun Microsystems 	 * On SPARC we create/update links too.
22697c478bd9Sstevel@tonic-gate 	 */
227048847494SEnrico Perla - Sun Microsystems 	if (flags != FTW_F && flags != FTW_D && (flags == FTW_SL &&
227148847494SEnrico Perla - Sun Microsystems 	    !is_flag_on(IS_SPARC_TARGET)))
227248847494SEnrico Perla - Sun Microsystems 		return (0);
227348847494SEnrico Perla - Sun Microsystems 
227448847494SEnrico Perla - Sun Microsystems 	/*
227548847494SEnrico Perla - Sun Microsystems 	 * Ignore broken links
227648847494SEnrico Perla - Sun Microsystems 	 */
227748847494SEnrico Perla - Sun Microsystems 	if (flags == FTW_SL && stat(file, &sb) < 0)
22787c478bd9Sstevel@tonic-gate 		return (0);
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	/*
22817c478bd9Sstevel@tonic-gate 	 * new_nvlp may be NULL if there were errors earlier
22827c478bd9Sstevel@tonic-gate 	 * but this is not fatal to update determination.
22837c478bd9Sstevel@tonic-gate 	 */
22847c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp) {
228548847494SEnrico Perla - Sun Microsystems 		filestat[0] = st->st_size;
228648847494SEnrico Perla - Sun Microsystems 		filestat[1] = st->st_mtime;
22877c478bd9Sstevel@tonic-gate 		error = nvlist_add_uint64_array(walk_arg.new_nvlp,
22887c478bd9Sstevel@tonic-gate 		    file + bam_rootlen, filestat, 2);
22897c478bd9Sstevel@tonic-gate 		if (error)
2290f64ca102SToomas Soome 			bam_error(_("failed to update stat data for: %s: %s\n"),
2291f64ca102SToomas Soome 			    file, strerror(error));
22927c478bd9Sstevel@tonic-gate 	}
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 	/*
2295d876c67dSjg 	 * If we are invoked as part of system/filesystem/boot-archive, then
229658091fd8Ssetje 	 * there are a number of things we should not worry about
22977c478bd9Sstevel@tonic-gate 	 */
229858091fd8Ssetje 	if (bam_smf_check) {
229958091fd8Ssetje 		/* ignore amd64 modules unless we are booted amd64. */
230058091fd8Ssetje 		if (!is_amd64() && strstr(file, "/amd64/") != 0)
23017c478bd9Sstevel@tonic-gate 			return (0);
23027c478bd9Sstevel@tonic-gate 
230358091fd8Ssetje 		/* read in list of safe files */
2304110570ceSToomas Soome 		if (safefiles == NULL) {
2305110570ceSToomas Soome 			fp = fopen("/boot/solaris/filelist.safe", "r");
2306110570ceSToomas Soome 			if (fp != NULL) {
230758091fd8Ssetje 				safefiles = s_calloc(1,
230858091fd8Ssetje 				    sizeof (struct safefile));
230958091fd8Ssetje 				safefilep = safefiles;
231058091fd8Ssetje 				safefilep->name = s_calloc(1, MAXPATHLEN +
231158091fd8Ssetje 				    MAXNAMELEN);
231258091fd8Ssetje 				safefilep->next = NULL;
231358091fd8Ssetje 				while (s_fgets(safefilep->name, MAXPATHLEN +
231458091fd8Ssetje 				    MAXNAMELEN, fp) != NULL) {
231558091fd8Ssetje 					safefilep->next = s_calloc(1,
231658091fd8Ssetje 					    sizeof (struct safefile));
231758091fd8Ssetje 					safefilep = safefilep->next;
231858091fd8Ssetje 					safefilep->name = s_calloc(1,
231958091fd8Ssetje 					    MAXPATHLEN + MAXNAMELEN);
232058091fd8Ssetje 					safefilep->next = NULL;
232158091fd8Ssetje 				}
232258091fd8Ssetje 				(void) fclose(fp);
232358091fd8Ssetje 			}
232458091fd8Ssetje 		}
2325110570ceSToomas Soome 	}
232658091fd8Ssetje 
23277c478bd9Sstevel@tonic-gate 	/*
232848847494SEnrico Perla - Sun Microsystems 	 * On SPARC we create a -path-list file for mkisofs
232948847494SEnrico Perla - Sun Microsystems 	 */
233048847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET) && !bam_nowrite()) {
233148847494SEnrico Perla - Sun Microsystems 		if (flags != FTW_D) {
233248847494SEnrico Perla - Sun Microsystems 			char	*strip;
233348847494SEnrico Perla - Sun Microsystems 
233448847494SEnrico Perla - Sun Microsystems 			strip = (char *)file + strlen(rootbuf);
233548847494SEnrico Perla - Sun Microsystems 			(void) fprintf(walk_arg.sparcfile, "/%s=%s\n", strip,
233648847494SEnrico Perla - Sun Microsystems 			    file);
233748847494SEnrico Perla - Sun Microsystems 		}
233848847494SEnrico Perla - Sun Microsystems 	}
233948847494SEnrico Perla - Sun Microsystems 
234048847494SEnrico Perla - Sun Microsystems 	/*
234148847494SEnrico Perla - Sun Microsystems 	 * We are transitioning from the old model to the dircache or the cache
234248847494SEnrico Perla - Sun Microsystems 	 * directory was removed: create the entry without further checkings.
234348847494SEnrico Perla - Sun Microsystems 	 */
234448847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(NEED_CACHE_DIR)) {
234548847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
2346f64ca102SToomas Soome 			bam_print(_("    new     %s\n"), file);
234748847494SEnrico Perla - Sun Microsystems 
234848847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(IS_SPARC_TARGET)) {
234948847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
235048847494SEnrico Perla - Sun Microsystems 			return (0);
235148847494SEnrico Perla - Sun Microsystems 		}
235248847494SEnrico Perla - Sun Microsystems 
235348847494SEnrico Perla - Sun Microsystems 		ret = update_dircache(file, flags);
235448847494SEnrico Perla - Sun Microsystems 		if (ret == BAM_ERROR) {
2355f64ca102SToomas Soome 			bam_error(_("directory cache update failed for %s\n"),
2356f64ca102SToomas Soome 			    file);
235748847494SEnrico Perla - Sun Microsystems 			return (-1);
235848847494SEnrico Perla - Sun Microsystems 		}
235948847494SEnrico Perla - Sun Microsystems 
236048847494SEnrico Perla - Sun Microsystems 		return (0);
236148847494SEnrico Perla - Sun Microsystems 	}
236248847494SEnrico Perla - Sun Microsystems 
236348847494SEnrico Perla - Sun Microsystems 	/*
23647c478bd9Sstevel@tonic-gate 	 * We need an update if file doesn't exist in old archive
23657c478bd9Sstevel@tonic-gate 	 */
23667c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp == NULL ||
23677c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint64_array(walk_arg.old_nvlp,
23687c478bd9Sstevel@tonic-gate 	    file + bam_rootlen, &value, &sz) != 0) {
23697c478bd9Sstevel@tonic-gate 		if (bam_smf_check)	/* ignore new during smf check */
23707c478bd9Sstevel@tonic-gate 			return (0);
237148847494SEnrico Perla - Sun Microsystems 
237248847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(IS_SPARC_TARGET)) {
237348847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
237448847494SEnrico Perla - Sun Microsystems 		} else {
237548847494SEnrico Perla - Sun Microsystems 			ret = update_dircache(file, flags);
237648847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_ERROR) {
2377f64ca102SToomas Soome 				bam_error(_("directory cache update "
2378f64ca102SToomas Soome 				    "failed for %s\n"), file);
237948847494SEnrico Perla - Sun Microsystems 				return (-1);
238048847494SEnrico Perla - Sun Microsystems 			}
238148847494SEnrico Perla - Sun Microsystems 		}
238248847494SEnrico Perla - Sun Microsystems 
23837c478bd9Sstevel@tonic-gate 		if (bam_verbose)
2384f64ca102SToomas Soome 			bam_print(_("    new     %s\n"), file);
23857c478bd9Sstevel@tonic-gate 		return (0);
23867c478bd9Sstevel@tonic-gate 	}
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate 	/*
238948847494SEnrico Perla - Sun Microsystems 	 * If we got there, the file is already listed as to be included in the
239048847494SEnrico Perla - Sun Microsystems 	 * iso image. We just need to know if we are going to rebuild it or not
239148847494SEnrico Perla - Sun Microsystems 	 */
239248847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET) &&
2393cedc7e57SEnrico Perla - Sun Microsystems 	    is_dir_flag_on(FILE64, NEED_UPDATE) && !bam_nowrite())
239448847494SEnrico Perla - Sun Microsystems 		return (0);
239548847494SEnrico Perla - Sun Microsystems 	/*
23967c478bd9Sstevel@tonic-gate 	 * File exists in old archive. Check if file has changed
23977c478bd9Sstevel@tonic-gate 	 */
23987c478bd9Sstevel@tonic-gate 	assert(sz == 2);
23997c478bd9Sstevel@tonic-gate 	bcopy(value, filestat, sizeof (filestat));
24007c478bd9Sstevel@tonic-gate 
240148847494SEnrico Perla - Sun Microsystems 	if (flags != FTW_D && (filestat[0] != st->st_size ||
240248847494SEnrico Perla - Sun Microsystems 	    filestat[1] != st->st_mtime)) {
24037c609327Ssetje 		if (bam_smf_check) {
24047c609327Ssetje 			safefilep = safefiles;
240589c3ee43SGangadhar Mylapuram 			while (safefilep != NULL &&
240689c3ee43SGangadhar Mylapuram 			    safefilep->name[0] != '\0') {
240789c3ee43SGangadhar Mylapuram 				if (regcomp(&re, safefilep->name,
240889c3ee43SGangadhar Mylapuram 				    REG_EXTENDED|REG_NOSUB) == 0) {
240989c3ee43SGangadhar Mylapuram 					status = regexec(&re,
241089c3ee43SGangadhar Mylapuram 					    file + bam_rootlen, 0, NULL, 0);
241189c3ee43SGangadhar Mylapuram 					regfree(&re);
241289c3ee43SGangadhar Mylapuram 					if (status == 0) {
24133b133becSGangadhar Mylapuram 						(void) creat(
24143b133becSGangadhar Mylapuram 						    NEED_UPDATE_SAFE_FILE,
241589c3ee43SGangadhar Mylapuram 						    0644);
24167c609327Ssetje 						return (0);
24177c609327Ssetje 					}
241889c3ee43SGangadhar Mylapuram 				}
24197c609327Ssetje 				safefilep = safefilep->next;
24207c609327Ssetje 			}
24217c609327Ssetje 		}
242248847494SEnrico Perla - Sun Microsystems 
242348847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(IS_SPARC_TARGET)) {
242448847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
242548847494SEnrico Perla - Sun Microsystems 		} else {
242648847494SEnrico Perla - Sun Microsystems 			ret = update_dircache(file, flags);
242748847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_ERROR) {
2428f64ca102SToomas Soome 				bam_error(_("directory cache update failed "
2429f64ca102SToomas Soome 				    "for %s\n"), file);
243048847494SEnrico Perla - Sun Microsystems 				return (-1);
243148847494SEnrico Perla - Sun Microsystems 			}
243248847494SEnrico Perla - Sun Microsystems 		}
243348847494SEnrico Perla - Sun Microsystems 
2434110570ceSToomas Soome 		if (bam_verbose) {
24357c478bd9Sstevel@tonic-gate 			if (bam_smf_check)
24367c478bd9Sstevel@tonic-gate 				bam_print("    %s\n", file);
24377c478bd9Sstevel@tonic-gate 			else
2438f64ca102SToomas Soome 				bam_print(_("    changed %s\n"), file);
24397c478bd9Sstevel@tonic-gate 		}
2440110570ceSToomas Soome 	}
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate 	return (0);
24437c478bd9Sstevel@tonic-gate }
24447c478bd9Sstevel@tonic-gate 
24457c478bd9Sstevel@tonic-gate /*
244648847494SEnrico Perla - Sun Microsystems  * Remove a directory path recursively
244748847494SEnrico Perla - Sun Microsystems  */
244848847494SEnrico Perla - Sun Microsystems static int
244948847494SEnrico Perla - Sun Microsystems rmdir_r(char *path)
245048847494SEnrico Perla - Sun Microsystems {
245148847494SEnrico Perla - Sun Microsystems 	struct dirent 	*d = NULL;
245248847494SEnrico Perla - Sun Microsystems 	DIR 		*dir = NULL;
245348847494SEnrico Perla - Sun Microsystems 	char 		tpath[PATH_MAX];
245448847494SEnrico Perla - Sun Microsystems 	struct stat 	sb;
245548847494SEnrico Perla - Sun Microsystems 
245648847494SEnrico Perla - Sun Microsystems 	if ((dir = opendir(path)) == NULL)
245748847494SEnrico Perla - Sun Microsystems 		return (-1);
245848847494SEnrico Perla - Sun Microsystems 
2459110570ceSToomas Soome 	while ((d = readdir(dir)) != NULL) {
246048847494SEnrico Perla - Sun Microsystems 		if ((strcmp(d->d_name, ".") != 0) &&
246148847494SEnrico Perla - Sun Microsystems 		    (strcmp(d->d_name, "..") != 0)) {
246248847494SEnrico Perla - Sun Microsystems 			(void) snprintf(tpath, sizeof (tpath), "%s/%s",
246348847494SEnrico Perla - Sun Microsystems 			    path, d->d_name);
246448847494SEnrico Perla - Sun Microsystems 			if (stat(tpath, &sb) == 0) {
246548847494SEnrico Perla - Sun Microsystems 				if (sb.st_mode & S_IFDIR)
246648847494SEnrico Perla - Sun Microsystems 					(void) rmdir_r(tpath);
246748847494SEnrico Perla - Sun Microsystems 				else
246848847494SEnrico Perla - Sun Microsystems 					(void) remove(tpath);
246948847494SEnrico Perla - Sun Microsystems 			}
247048847494SEnrico Perla - Sun Microsystems 		}
247148847494SEnrico Perla - Sun Microsystems 	}
247248847494SEnrico Perla - Sun Microsystems 	return (remove(path));
247348847494SEnrico Perla - Sun Microsystems }
247448847494SEnrico Perla - Sun Microsystems 
247548847494SEnrico Perla - Sun Microsystems /*
247648847494SEnrico Perla - Sun Microsystems  * Check if cache directory exists and, if not, create it and update flags
247748847494SEnrico Perla - Sun Microsystems  * accordingly. If the path exists, but it's not a directory, a best effort
247848847494SEnrico Perla - Sun Microsystems  * attempt to remove and recreate it is made.
247948847494SEnrico Perla - Sun Microsystems  * If the user requested a 'purge', always recreate the directory from scratch.
248048847494SEnrico Perla - Sun Microsystems  */
248148847494SEnrico Perla - Sun Microsystems static int
248248847494SEnrico Perla - Sun Microsystems set_cache_dir(char *root, int what)
248348847494SEnrico Perla - Sun Microsystems {
248448847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
248548847494SEnrico Perla - Sun Microsystems 	int		ret = 0;
248648847494SEnrico Perla - Sun Microsystems 
248748847494SEnrico Perla - Sun Microsystems 	ret = snprintf(get_cachedir(what), sizeof (get_cachedir(what)),
248848847494SEnrico Perla - Sun Microsystems 	    "%s%s%s%s%s", root, ARCHIVE_PREFIX, get_machine(), what == FILE64 ?
248948847494SEnrico Perla - Sun Microsystems 	    "/amd64" : "", CACHEDIR_SUFFIX);
249048847494SEnrico Perla - Sun Microsystems 
249148847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (get_cachedir(what))) {
2492f64ca102SToomas Soome 		bam_error(_("unable to create path on mountpoint %s, "
2493f64ca102SToomas Soome 		    "path too long\n"), rootbuf);
249448847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
249548847494SEnrico Perla - Sun Microsystems 	}
249648847494SEnrico Perla - Sun Microsystems 
24974a9df875SEnrico Perla - Sun Microsystems 	if (bam_purge || is_flag_on(INVALIDATE_CACHE))
249848847494SEnrico Perla - Sun Microsystems 		(void) rmdir_r(get_cachedir(what));
249948847494SEnrico Perla - Sun Microsystems 
250048847494SEnrico Perla - Sun Microsystems 	if (stat(get_cachedir(what), &sb) != 0 || !(S_ISDIR(sb.st_mode))) {
250148847494SEnrico Perla - Sun Microsystems 		/* best effort unlink attempt, mkdir will catch errors */
250248847494SEnrico Perla - Sun Microsystems 		(void) unlink(get_cachedir(what));
250348847494SEnrico Perla - Sun Microsystems 
250448847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
2505f64ca102SToomas Soome 			bam_print(_("archive cache directory not found: %s\n"),
2506f64ca102SToomas Soome 			    get_cachedir(what));
250748847494SEnrico Perla - Sun Microsystems 		ret = mkdir(get_cachedir(what), DIR_PERMS);
250848847494SEnrico Perla - Sun Microsystems 		if (ret < 0) {
2509f64ca102SToomas Soome 			bam_error(_("mkdir of %s failed: %s\n"),
2510f64ca102SToomas Soome 			    get_cachedir(what), strerror(errno));
251148847494SEnrico Perla - Sun Microsystems 			get_cachedir(what)[0] = '\0';
251248847494SEnrico Perla - Sun Microsystems 			return (ret);
251348847494SEnrico Perla - Sun Microsystems 		}
251448847494SEnrico Perla - Sun Microsystems 		set_flag(NEED_CACHE_DIR);
251548847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
251648847494SEnrico Perla - Sun Microsystems 	}
251748847494SEnrico Perla - Sun Microsystems 
251848847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
251948847494SEnrico Perla - Sun Microsystems }
252048847494SEnrico Perla - Sun Microsystems 
252148847494SEnrico Perla - Sun Microsystems static int
252248847494SEnrico Perla - Sun Microsystems set_update_dir(char *root, int what)
252348847494SEnrico Perla - Sun Microsystems {
252448847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
252548847494SEnrico Perla - Sun Microsystems 	int		ret;
252648847494SEnrico Perla - Sun Microsystems 
252748847494SEnrico Perla - Sun Microsystems 	if (is_dir_flag_on(what, NO_MULTI))
252848847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
252948847494SEnrico Perla - Sun Microsystems 
253048847494SEnrico Perla - Sun Microsystems 	if (!bam_extend) {
253148847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
253248847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
253348847494SEnrico Perla - Sun Microsystems 	}
253448847494SEnrico Perla - Sun Microsystems 
253548847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
253648847494SEnrico Perla - Sun Microsystems 		ret = snprintf(get_updatedir(what),
253748847494SEnrico Perla - Sun Microsystems 		    sizeof (get_updatedir(what)), "%s%s%s/amd64%s", root,
253848847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_PREFIX, get_machine(), UPDATEDIR_SUFFIX);
253948847494SEnrico Perla - Sun Microsystems 	else
254048847494SEnrico Perla - Sun Microsystems 		ret = snprintf(get_updatedir(what),
254148847494SEnrico Perla - Sun Microsystems 		    sizeof (get_updatedir(what)), "%s%s%s%s", root,
254248847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_PREFIX, get_machine(), UPDATEDIR_SUFFIX);
254348847494SEnrico Perla - Sun Microsystems 
254448847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (get_updatedir(what))) {
2545f64ca102SToomas Soome 		bam_error(_("unable to create path on mountpoint %s, "
2546f64ca102SToomas Soome 		    "path too long\n"), rootbuf);
254748847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
254848847494SEnrico Perla - Sun Microsystems 	}
254948847494SEnrico Perla - Sun Microsystems 
255048847494SEnrico Perla - Sun Microsystems 	if (stat(get_updatedir(what), &sb) == 0) {
255148847494SEnrico Perla - Sun Microsystems 		if (S_ISDIR(sb.st_mode))
255248847494SEnrico Perla - Sun Microsystems 			ret = rmdir_r(get_updatedir(what));
255348847494SEnrico Perla - Sun Microsystems 		else
255448847494SEnrico Perla - Sun Microsystems 			ret = unlink(get_updatedir(what));
255548847494SEnrico Perla - Sun Microsystems 
255648847494SEnrico Perla - Sun Microsystems 		if (ret != 0)
255748847494SEnrico Perla - Sun Microsystems 			set_dir_flag(what, NO_MULTI);
255848847494SEnrico Perla - Sun Microsystems 	}
255948847494SEnrico Perla - Sun Microsystems 
256048847494SEnrico Perla - Sun Microsystems 	if (mkdir(get_updatedir(what), DIR_PERMS) < 0)
256148847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
256248847494SEnrico Perla - Sun Microsystems 
256348847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
256448847494SEnrico Perla - Sun Microsystems }
256548847494SEnrico Perla - Sun Microsystems 
256648847494SEnrico Perla - Sun Microsystems static int
256748847494SEnrico Perla - Sun Microsystems is_valid_archive(char *root, int what)
256848847494SEnrico Perla - Sun Microsystems {
256948847494SEnrico Perla - Sun Microsystems 	char 		archive_path[PATH_MAX];
25704a9df875SEnrico Perla - Sun Microsystems 	char		timestamp_path[PATH_MAX];
25714a9df875SEnrico Perla - Sun Microsystems 	struct stat 	sb, timestamp;
257248847494SEnrico Perla - Sun Microsystems 	int 		ret;
257348847494SEnrico Perla - Sun Microsystems 
257448847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
257548847494SEnrico Perla - Sun Microsystems 		ret = snprintf(archive_path, sizeof (archive_path),
257648847494SEnrico Perla - Sun Microsystems 		    "%s%s%s/amd64%s", root, ARCHIVE_PREFIX, get_machine(),
257748847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_SUFFIX);
257848847494SEnrico Perla - Sun Microsystems 	else
257948847494SEnrico Perla - Sun Microsystems 		ret = snprintf(archive_path, sizeof (archive_path), "%s%s%s%s",
258048847494SEnrico Perla - Sun Microsystems 		    root, ARCHIVE_PREFIX, get_machine(), ARCHIVE_SUFFIX);
258148847494SEnrico Perla - Sun Microsystems 
258248847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (archive_path)) {
2583f64ca102SToomas Soome 		bam_error(_("unable to create path on mountpoint %s, "
2584f64ca102SToomas Soome 		    "path too long\n"), rootbuf);
258548847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
258648847494SEnrico Perla - Sun Microsystems 	}
258748847494SEnrico Perla - Sun Microsystems 
258848847494SEnrico Perla - Sun Microsystems 	if (stat(archive_path, &sb) != 0) {
258948847494SEnrico Perla - Sun Microsystems 		if (bam_verbose && !bam_check)
2590f64ca102SToomas Soome 			bam_print(_("archive not found: %s\n"), archive_path);
259148847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NEED_UPDATE);
259248847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
259348847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
259448847494SEnrico Perla - Sun Microsystems 	}
259548847494SEnrico Perla - Sun Microsystems 
25964a9df875SEnrico Perla - Sun Microsystems 	/*
25974a9df875SEnrico Perla - Sun Microsystems 	 * The timestamp file is used to prevent stale files in the archive
25984a9df875SEnrico Perla - Sun Microsystems 	 * cache.
25994a9df875SEnrico Perla - Sun Microsystems 	 * Stale files can happen if the system is booted back and forth across
26004a9df875SEnrico Perla - Sun Microsystems 	 * the transition from bootadm-before-the-cache to
26014a9df875SEnrico Perla - Sun Microsystems 	 * bootadm-after-the-cache, since older versions of bootadm don't know
26024a9df875SEnrico Perla - Sun Microsystems 	 * about the existence of the archive cache.
26034a9df875SEnrico Perla - Sun Microsystems 	 *
26044a9df875SEnrico Perla - Sun Microsystems 	 * Since only bootadm-after-the-cache versions know about about this
26054a9df875SEnrico Perla - Sun Microsystems 	 * file, we require that the boot archive be older than this file.
26064a9df875SEnrico Perla - Sun Microsystems 	 */
26074a9df875SEnrico Perla - Sun Microsystems 	ret = snprintf(timestamp_path, sizeof (timestamp_path), "%s%s", root,
26084a9df875SEnrico Perla - Sun Microsystems 	    FILE_STAT_TIMESTAMP);
26094a9df875SEnrico Perla - Sun Microsystems 
26104a9df875SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (timestamp_path)) {
2611f64ca102SToomas Soome 		bam_error(_("unable to create path on mountpoint %s, "
2612f64ca102SToomas Soome 		    "path too long\n"), rootbuf);
26134a9df875SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
26144a9df875SEnrico Perla - Sun Microsystems 	}
26154a9df875SEnrico Perla - Sun Microsystems 
26164a9df875SEnrico Perla - Sun Microsystems 	if (stat(timestamp_path, &timestamp) != 0 ||
26174a9df875SEnrico Perla - Sun Microsystems 	    sb.st_mtime > timestamp.st_mtime) {
26184a9df875SEnrico Perla - Sun Microsystems 		if (bam_verbose && !bam_check)
2619f64ca102SToomas Soome 			bam_print(
2620f64ca102SToomas Soome 			    _("archive cache is out of sync. Rebuilding.\n"));
26214a9df875SEnrico Perla - Sun Microsystems 		/*
26224a9df875SEnrico Perla - Sun Microsystems 		 * Don't generate a false positive for the boot-archive service
26234a9df875SEnrico Perla - Sun Microsystems 		 * but trigger an update of the archive cache in
26244a9df875SEnrico Perla - Sun Microsystems 		 * boot-archive-update.
26254a9df875SEnrico Perla - Sun Microsystems 		 */
26264a9df875SEnrico Perla - Sun Microsystems 		if (bam_smf_check) {
26274a9df875SEnrico Perla - Sun Microsystems 			(void) creat(NEED_UPDATE_FILE, 0644);
26284a9df875SEnrico Perla - Sun Microsystems 			return (BAM_SUCCESS);
26294a9df875SEnrico Perla - Sun Microsystems 		}
26304a9df875SEnrico Perla - Sun Microsystems 
26314a9df875SEnrico Perla - Sun Microsystems 		set_flag(INVALIDATE_CACHE);
26324a9df875SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NEED_UPDATE);
26334a9df875SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
26344a9df875SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
26354a9df875SEnrico Perla - Sun Microsystems 	}
26364a9df875SEnrico Perla - Sun Microsystems 
263748847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET))
263848847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
263948847494SEnrico Perla - Sun Microsystems 
264048847494SEnrico Perla - Sun Microsystems 	if (bam_extend && sb.st_size > BA_SIZE_MAX) {
264148847494SEnrico Perla - Sun Microsystems 		if (bam_verbose && !bam_check)
2642f64ca102SToomas Soome 			bam_print(_("archive %s is bigger than %d bytes and "
2643f64ca102SToomas Soome 			    "will be rebuilt\n"), archive_path, BA_SIZE_MAX);
264448847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
264548847494SEnrico Perla - Sun Microsystems 	}
264648847494SEnrico Perla - Sun Microsystems 
264748847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
264848847494SEnrico Perla - Sun Microsystems }
264948847494SEnrico Perla - Sun Microsystems 
265048847494SEnrico Perla - Sun Microsystems /*
265148847494SEnrico Perla - Sun Microsystems  * Check flags and presence of required files and directories.
26527c478bd9Sstevel@tonic-gate  * The force flag and/or absence of files should
26537c478bd9Sstevel@tonic-gate  * trigger an update.
26547c478bd9Sstevel@tonic-gate  * Suppress stdout output if check (-n) option is set
26557c478bd9Sstevel@tonic-gate  * (as -n should only produce parseable output.)
26567c478bd9Sstevel@tonic-gate  */
265748847494SEnrico Perla - Sun Microsystems static int
26587c478bd9Sstevel@tonic-gate check_flags_and_files(char *root)
26597c478bd9Sstevel@tonic-gate {
266048847494SEnrico Perla - Sun Microsystems 
26617c478bd9Sstevel@tonic-gate 	struct stat 	sb;
266248847494SEnrico Perla - Sun Microsystems 	int 		ret;
266348847494SEnrico Perla - Sun Microsystems 
266448847494SEnrico Perla - Sun Microsystems 	/*
266548847494SEnrico Perla - Sun Microsystems 	 * If archive is missing, create archive
266648847494SEnrico Perla - Sun Microsystems 	 */
266748847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET)) {
266848847494SEnrico Perla - Sun Microsystems 		ret = is_valid_archive(root, FILE64);
2669cedc7e57SEnrico Perla - Sun Microsystems 		if (ret == BAM_ERROR)
2670cedc7e57SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
267148847494SEnrico Perla - Sun Microsystems 	} else {
267248847494SEnrico Perla - Sun Microsystems 		int	what = FILE32;
267348847494SEnrico Perla - Sun Microsystems 		do {
267448847494SEnrico Perla - Sun Microsystems 			ret = is_valid_archive(root, what);
267548847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_ERROR)
267648847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
267748847494SEnrico Perla - Sun Microsystems 			what++;
267848847494SEnrico Perla - Sun Microsystems 		} while (bam_direct == BAM_DIRECT_DBOOT && what < CACHEDIR_NUM);
267948847494SEnrico Perla - Sun Microsystems 	}
268048847494SEnrico Perla - Sun Microsystems 
268148847494SEnrico Perla - Sun Microsystems 	if (bam_nowrite())
268248847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
268348847494SEnrico Perla - Sun Microsystems 
268448847494SEnrico Perla - Sun Microsystems 
268548847494SEnrico Perla - Sun Microsystems 	/*
268648847494SEnrico Perla - Sun Microsystems 	 * check if cache directories exist on x86.
268748847494SEnrico Perla - Sun Microsystems 	 * check (and always open) the cache file on SPARC.
268848847494SEnrico Perla - Sun Microsystems 	 */
268948847494SEnrico Perla - Sun Microsystems 	if (is_sparc()) {
269048847494SEnrico Perla - Sun Microsystems 		ret = snprintf(get_cachedir(FILE64),
269148847494SEnrico Perla - Sun Microsystems 		    sizeof (get_cachedir(FILE64)), "%s%s%s/%s", root,
269248847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_PREFIX, get_machine(), CACHEDIR_SUFFIX);
269348847494SEnrico Perla - Sun Microsystems 
269448847494SEnrico Perla - Sun Microsystems 		if (ret >= sizeof (get_cachedir(FILE64))) {
2695f64ca102SToomas Soome 			bam_error(_("unable to create path on mountpoint %s, "
2696f64ca102SToomas Soome 			    "path too long\n"), rootbuf);
269748847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
269848847494SEnrico Perla - Sun Microsystems 		}
269948847494SEnrico Perla - Sun Microsystems 
270048847494SEnrico Perla - Sun Microsystems 		if (stat(get_cachedir(FILE64), &sb) != 0) {
270148847494SEnrico Perla - Sun Microsystems 			set_flag(NEED_CACHE_DIR);
270248847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
270348847494SEnrico Perla - Sun Microsystems 		}
270448847494SEnrico Perla - Sun Microsystems 
270548847494SEnrico Perla - Sun Microsystems 		walk_arg.sparcfile = fopen(get_cachedir(FILE64), "w");
270648847494SEnrico Perla - Sun Microsystems 		if (walk_arg.sparcfile == NULL) {
2707f64ca102SToomas Soome 			bam_error(_("failed to open file: %s: %s\n"),
2708f64ca102SToomas Soome 			    get_cachedir(FILE64), strerror(errno));
270948847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
271048847494SEnrico Perla - Sun Microsystems 		}
271148847494SEnrico Perla - Sun Microsystems 
271248847494SEnrico Perla - Sun Microsystems 		set_dir_present(FILE64);
271348847494SEnrico Perla - Sun Microsystems 	} else {
271448847494SEnrico Perla - Sun Microsystems 		int	what = FILE32;
271548847494SEnrico Perla - Sun Microsystems 
271648847494SEnrico Perla - Sun Microsystems 		do {
271748847494SEnrico Perla - Sun Microsystems 			if (set_cache_dir(root, what) != 0)
271848847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
271948847494SEnrico Perla - Sun Microsystems 
272048847494SEnrico Perla - Sun Microsystems 			set_dir_present(what);
272148847494SEnrico Perla - Sun Microsystems 
272248847494SEnrico Perla - Sun Microsystems 			if (set_update_dir(root, what) != 0)
272348847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
272448847494SEnrico Perla - Sun Microsystems 			what++;
272548847494SEnrico Perla - Sun Microsystems 		} while (bam_direct == BAM_DIRECT_DBOOT && what < CACHEDIR_NUM);
272648847494SEnrico Perla - Sun Microsystems 	}
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate 	/*
27297c478bd9Sstevel@tonic-gate 	 * if force, create archive unconditionally
27307c478bd9Sstevel@tonic-gate 	 */
27317c478bd9Sstevel@tonic-gate 	if (bam_force) {
273248847494SEnrico Perla - Sun Microsystems 		if (!is_sparc())
273348847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE32, NEED_UPDATE);
273448847494SEnrico Perla - Sun Microsystems 		set_dir_flag(FILE64, NEED_UPDATE);
273548847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
2736f64ca102SToomas Soome 			bam_print(_("forced update of archive requested\n"));
273748847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
27387c478bd9Sstevel@tonic-gate 	}
27397c478bd9Sstevel@tonic-gate 
274048847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
27417c478bd9Sstevel@tonic-gate }
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate static error_t
27447c478bd9Sstevel@tonic-gate read_one_list(char *root, filelist_t  *flistp, char *filelist)
27457c478bd9Sstevel@tonic-gate {
27467c478bd9Sstevel@tonic-gate 	char 		path[PATH_MAX];
27477c478bd9Sstevel@tonic-gate 	FILE 		*fp;
27487c478bd9Sstevel@tonic-gate 	char 		buf[BAM_MAXLINE];
2749eb2bd662Svikram 	const char 	*fcn = "read_one_list()";
27507c478bd9Sstevel@tonic-gate 
27517c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, filelist);
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate 	fp = fopen(path, "r");
27547c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
2755f64ca102SToomas Soome 		BAM_DPRINTF(("%s: failed to open archive filelist: %s: %s\n",
2756f64ca102SToomas Soome 		    fcn, path, strerror(errno)));
27577c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27587c478bd9Sstevel@tonic-gate 	}
27597c478bd9Sstevel@tonic-gate 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
2760b610f78eSvikram 		/* skip blank lines */
2761b610f78eSvikram 		if (strspn(buf, " \t") == strlen(buf))
2762b610f78eSvikram 			continue;
27637c478bd9Sstevel@tonic-gate 		append_to_flist(flistp, buf);
27647c478bd9Sstevel@tonic-gate 	}
27657c478bd9Sstevel@tonic-gate 	if (fclose(fp) != 0) {
2766f64ca102SToomas Soome 		bam_error(_("failed to close file: %s: %s\n"),
2767f64ca102SToomas Soome 		    path, strerror(errno));
27687c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27697c478bd9Sstevel@tonic-gate 	}
27707c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
27717c478bd9Sstevel@tonic-gate }
27727c478bd9Sstevel@tonic-gate 
27737c478bd9Sstevel@tonic-gate static error_t
27747c478bd9Sstevel@tonic-gate read_list(char *root, filelist_t  *flistp)
27757c478bd9Sstevel@tonic-gate {
2776986fd29aSsetje 	char 		path[PATH_MAX];
2777986fd29aSsetje 	char 		cmd[PATH_MAX];
2778986fd29aSsetje 	struct stat 	sb;
2779986fd29aSsetje 	int 		n, rval;
2780eb2bd662Svikram 	const char 	*fcn = "read_list()";
27817c478bd9Sstevel@tonic-gate 
27827c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
27837c478bd9Sstevel@tonic-gate 
27847c478bd9Sstevel@tonic-gate 	/*
2785986fd29aSsetje 	 * build and check path to extract_boot_filelist.ksh
2786986fd29aSsetje 	 */
2787986fd29aSsetje 	n = snprintf(path, sizeof (path), "%s%s", root, EXTRACT_BOOT_FILELIST);
2788986fd29aSsetje 	if (n >= sizeof (path)) {
2789f64ca102SToomas Soome 		bam_error(_("archive filelist is empty\n"));
2790986fd29aSsetje 		return (BAM_ERROR);
2791986fd29aSsetje 	}
2792986fd29aSsetje 
2793cedc7e57SEnrico Perla - Sun Microsystems 	if (is_safe_exec(path) == BAM_ERROR)
2794cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
2795cedc7e57SEnrico Perla - Sun Microsystems 
2796986fd29aSsetje 	/*
2797986fd29aSsetje 	 * If extract_boot_filelist is present, exec it, otherwise read
2798986fd29aSsetje 	 * the filelists directly, for compatibility with older images.
2799986fd29aSsetje 	 */
2800986fd29aSsetje 	if (stat(path, &sb) == 0) {
2801986fd29aSsetje 		/*
2802986fd29aSsetje 		 * build arguments to exec extract_boot_filelist.ksh
2803986fd29aSsetje 		 */
2804d876c67dSjg 		char *rootarg, *platarg;
2805d876c67dSjg 		int platarglen = 1, rootarglen = 1;
2806d876c67dSjg 		if (strlen(root) > 1)
2807d876c67dSjg 			rootarglen += strlen(root) + strlen("-R ");
2808d876c67dSjg 		if (bam_alt_platform)
2809d876c67dSjg 			platarglen += strlen(bam_platform) + strlen("-p ");
2810d876c67dSjg 		platarg = s_calloc(1, platarglen);
2811d876c67dSjg 		rootarg = s_calloc(1, rootarglen);
2812d876c67dSjg 		*platarg = 0;
2813d876c67dSjg 		*rootarg = 0;
2814d876c67dSjg 
2815986fd29aSsetje 		if (strlen(root) > 1) {
2816d876c67dSjg 			(void) snprintf(rootarg, rootarglen,
2817d876c67dSjg 			    "-R %s", root);
2818986fd29aSsetje 		}
2819d876c67dSjg 		if (bam_alt_platform) {
2820d876c67dSjg 			(void) snprintf(platarg, platarglen,
2821d876c67dSjg 			    "-p %s", bam_platform);
2822d876c67dSjg 		}
2823d876c67dSjg 		n = snprintf(cmd, sizeof (cmd), "%s %s %s /%s /%s",
2824d876c67dSjg 		    path, rootarg, platarg, BOOT_FILE_LIST, ETC_FILE_LIST);
2825d876c67dSjg 		free(platarg);
2826d876c67dSjg 		free(rootarg);
2827986fd29aSsetje 		if (n >= sizeof (cmd)) {
2828f64ca102SToomas Soome 			bam_error(_("archive filelist is empty\n"));
2829986fd29aSsetje 			return (BAM_ERROR);
2830986fd29aSsetje 		}
2831986fd29aSsetje 		if (exec_cmd(cmd, flistp) != 0) {
2832f64ca102SToomas Soome 			BAM_DPRINTF(("%s: failed to open archive "
2833f64ca102SToomas Soome 			    "filelist: %s: %s\n", fcn, path, strerror(errno)));
2834986fd29aSsetje 			return (BAM_ERROR);
2835986fd29aSsetje 		}
2836986fd29aSsetje 	} else {
2837986fd29aSsetje 		/*
28387c478bd9Sstevel@tonic-gate 		 * Read current lists of files - only the first is mandatory
28397c478bd9Sstevel@tonic-gate 		 */
28407c478bd9Sstevel@tonic-gate 		rval = read_one_list(root, flistp, BOOT_FILE_LIST);
28417c478bd9Sstevel@tonic-gate 		if (rval != BAM_SUCCESS)
28427c478bd9Sstevel@tonic-gate 			return (rval);
28437c478bd9Sstevel@tonic-gate 		(void) read_one_list(root, flistp, ETC_FILE_LIST);
2844986fd29aSsetje 	}
28457c478bd9Sstevel@tonic-gate 
28467c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL) {
2847f64ca102SToomas Soome 		bam_error(_("archive filelist is empty\n"));
28487c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
28497c478bd9Sstevel@tonic-gate 	}
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
28527c478bd9Sstevel@tonic-gate }
28537c478bd9Sstevel@tonic-gate 
28547c478bd9Sstevel@tonic-gate static void
28557c478bd9Sstevel@tonic-gate getoldstat(char *root)
28567c478bd9Sstevel@tonic-gate {
28577c478bd9Sstevel@tonic-gate 	char 		path[PATH_MAX];
28587c478bd9Sstevel@tonic-gate 	int 		fd, error;
28597c478bd9Sstevel@tonic-gate 	struct stat 	sb;
28607c478bd9Sstevel@tonic-gate 	char 		*ostat;
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
28637c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDONLY);
28647c478bd9Sstevel@tonic-gate 	if (fd == -1) {
28657c478bd9Sstevel@tonic-gate 		if (bam_verbose)
2866f64ca102SToomas Soome 			bam_print(_("failed to open file: %s: %s\n"),
2867f64ca102SToomas Soome 			    path, strerror(errno));
286848847494SEnrico Perla - Sun Microsystems 		goto out_err;
28697c478bd9Sstevel@tonic-gate 	}
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate 	if (fstat(fd, &sb) != 0) {
2872f64ca102SToomas Soome 		bam_error(_("stat of file failed: %s: %s\n"), path,
2873f64ca102SToomas Soome 		    strerror(errno));
287448847494SEnrico Perla - Sun Microsystems 		goto out_err;
28757c478bd9Sstevel@tonic-gate 	}
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate 	ostat = s_calloc(1, sb.st_size);
28787c478bd9Sstevel@tonic-gate 
28797c478bd9Sstevel@tonic-gate 	if (read(fd, ostat, sb.st_size) != sb.st_size) {
2880f64ca102SToomas Soome 		bam_error(_("read failed for file: %s: %s\n"), path,
2881f64ca102SToomas Soome 		    strerror(errno));
28827c478bd9Sstevel@tonic-gate 		free(ostat);
288348847494SEnrico Perla - Sun Microsystems 		goto out_err;
28847c478bd9Sstevel@tonic-gate 	}
28857c478bd9Sstevel@tonic-gate 
28867c478bd9Sstevel@tonic-gate 	(void) close(fd);
288748847494SEnrico Perla - Sun Microsystems 	fd = -1;
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
28907c478bd9Sstevel@tonic-gate 	error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0);
28917c478bd9Sstevel@tonic-gate 
28927c478bd9Sstevel@tonic-gate 	free(ostat);
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 	if (error) {
2895f64ca102SToomas Soome 		bam_error(_("failed to unpack stat data: %s: %s\n"),
2896f64ca102SToomas Soome 		    path, strerror(error));
28977c478bd9Sstevel@tonic-gate 		walk_arg.old_nvlp = NULL;
289848847494SEnrico Perla - Sun Microsystems 		goto out_err;
289948847494SEnrico Perla - Sun Microsystems 	} else {
29007c478bd9Sstevel@tonic-gate 		return;
29017c478bd9Sstevel@tonic-gate 	}
290248847494SEnrico Perla - Sun Microsystems 
290348847494SEnrico Perla - Sun Microsystems out_err:
290448847494SEnrico Perla - Sun Microsystems 	if (fd != -1)
290548847494SEnrico Perla - Sun Microsystems 		(void) close(fd);
2906cedc7e57SEnrico Perla - Sun Microsystems 	if (!is_flag_on(IS_SPARC_TARGET))
290748847494SEnrico Perla - Sun Microsystems 		set_dir_flag(FILE32, NEED_UPDATE);
290848847494SEnrico Perla - Sun Microsystems 	set_dir_flag(FILE64, NEED_UPDATE);
290948847494SEnrico Perla - Sun Microsystems }
291048847494SEnrico Perla - Sun Microsystems 
291148847494SEnrico Perla - Sun Microsystems /* Best effort stale entry removal */
291248847494SEnrico Perla - Sun Microsystems static void
291348847494SEnrico Perla - Sun Microsystems delete_stale(char *file, int what)
291448847494SEnrico Perla - Sun Microsystems {
291548847494SEnrico Perla - Sun Microsystems 	char		path[PATH_MAX];
291648847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
291748847494SEnrico Perla - Sun Microsystems 
291848847494SEnrico Perla - Sun Microsystems 	(void) snprintf(path, sizeof (path), "%s/%s", get_cachedir(what), file);
291948847494SEnrico Perla - Sun Microsystems 	if (!bam_check && stat(path, &sb) == 0) {
292048847494SEnrico Perla - Sun Microsystems 		if (sb.st_mode & S_IFDIR)
292148847494SEnrico Perla - Sun Microsystems 			(void) rmdir_r(path);
292248847494SEnrico Perla - Sun Microsystems 		else
292348847494SEnrico Perla - Sun Microsystems 			(void) unlink(path);
292448847494SEnrico Perla - Sun Microsystems 
292548847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, (NEED_UPDATE | NO_MULTI));
292648847494SEnrico Perla - Sun Microsystems 	}
29277c478bd9Sstevel@tonic-gate }
29287c478bd9Sstevel@tonic-gate 
2929a28d77b8Svikram /*
2930a28d77b8Svikram  * Checks if a file in the current (old) archive has
2931a28d77b8Svikram  * been deleted from the root filesystem. This is needed for
2932a28d77b8Svikram  * software like Trusted Extensions (TX) that switch early
2933a28d77b8Svikram  * in boot based on presence/absence of a kernel module.
2934a28d77b8Svikram  */
2935a28d77b8Svikram static void
2936a28d77b8Svikram check4stale(char *root)
2937a28d77b8Svikram {
2938a28d77b8Svikram 	nvpair_t	*nvp;
2939a28d77b8Svikram 	nvlist_t	*nvlp;
2940a28d77b8Svikram 	char 		*file;
2941a28d77b8Svikram 	char		path[PATH_MAX];
2942a28d77b8Svikram 
2943a28d77b8Svikram 	/*
2944a28d77b8Svikram 	 * Skip stale file check during smf check
2945a28d77b8Svikram 	 */
2946a28d77b8Svikram 	if (bam_smf_check)
2947a28d77b8Svikram 		return;
2948a28d77b8Svikram 
294948847494SEnrico Perla - Sun Microsystems 	/*
295048847494SEnrico Perla - Sun Microsystems 	 * If we need to (re)create the cache, there's no need to check for
295148847494SEnrico Perla - Sun Microsystems 	 * stale files
295248847494SEnrico Perla - Sun Microsystems 	 */
295348847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(NEED_CACHE_DIR))
295448847494SEnrico Perla - Sun Microsystems 		return;
295548847494SEnrico Perla - Sun Microsystems 
2956a28d77b8Svikram 	/* Nothing to do if no old stats */
2957a28d77b8Svikram 	if ((nvlp = walk_arg.old_nvlp) == NULL)
2958a28d77b8Svikram 		return;
2959a28d77b8Svikram 
2960a28d77b8Svikram 	for (nvp = nvlist_next_nvpair(nvlp, NULL); nvp;
2961a28d77b8Svikram 	    nvp = nvlist_next_nvpair(nvlp, nvp)) {
2962a28d77b8Svikram 		file = nvpair_name(nvp);
2963a28d77b8Svikram 		if (file == NULL)
2964a28d77b8Svikram 			continue;
2965a28d77b8Svikram 		(void) snprintf(path, sizeof (path), "%s/%s",
2966a28d77b8Svikram 		    root, file);
296748847494SEnrico Perla - Sun Microsystems 		if (access(path, F_OK) < 0) {
296848847494SEnrico Perla - Sun Microsystems 			int	what;
296948847494SEnrico Perla - Sun Microsystems 
2970cedc7e57SEnrico Perla - Sun Microsystems 			if (bam_verbose)
2971f64ca102SToomas Soome 				bam_print(_("    stale %s\n"), path);
297248847494SEnrico Perla - Sun Microsystems 
2973cedc7e57SEnrico Perla - Sun Microsystems 			if (is_flag_on(IS_SPARC_TARGET)) {
2974cedc7e57SEnrico Perla - Sun Microsystems 				set_dir_flag(FILE64, NEED_UPDATE);
2975cedc7e57SEnrico Perla - Sun Microsystems 			} else {
297648847494SEnrico Perla - Sun Microsystems 				for (what = FILE32; what < CACHEDIR_NUM; what++)
297748847494SEnrico Perla - Sun Microsystems 					if (has_cachedir(what))
297848847494SEnrico Perla - Sun Microsystems 						delete_stale(file, what);
2979a28d77b8Svikram 			}
2980a28d77b8Svikram 		}
2981a28d77b8Svikram 	}
2982cedc7e57SEnrico Perla - Sun Microsystems }
2983a28d77b8Svikram 
29847c478bd9Sstevel@tonic-gate static void
29857c478bd9Sstevel@tonic-gate create_newstat(void)
29867c478bd9Sstevel@tonic-gate {
29877c478bd9Sstevel@tonic-gate 	int error;
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate 	error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0);
29907c478bd9Sstevel@tonic-gate 	if (error) {
29917c478bd9Sstevel@tonic-gate 		/*
29927c478bd9Sstevel@tonic-gate 		 * Not fatal - we can still create archive
29937c478bd9Sstevel@tonic-gate 		 */
29947c478bd9Sstevel@tonic-gate 		walk_arg.new_nvlp = NULL;
2995f64ca102SToomas Soome 		bam_error(_("failed to create stat data: %s\n"),
2996f64ca102SToomas Soome 		    strerror(error));
29977c478bd9Sstevel@tonic-gate 	}
29987c478bd9Sstevel@tonic-gate }
29997c478bd9Sstevel@tonic-gate 
300048847494SEnrico Perla - Sun Microsystems static int
30017c478bd9Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp)
30027c478bd9Sstevel@tonic-gate {
30037c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
30047c478bd9Sstevel@tonic-gate 	line_t *lp;
30057c478bd9Sstevel@tonic-gate 
30067c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
3007986fd29aSsetje 		/*
3008986fd29aSsetje 		 * Don't follow symlinks.  A symlink must refer to
3009986fd29aSsetje 		 * a file that would appear in the archive through
3010986fd29aSsetje 		 * a direct reference.  This matches the archive
3011986fd29aSsetje 		 * construction behavior.
3012986fd29aSsetje 		 */
30137c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, lp->line);
3014986fd29aSsetje 		if (nftw(path, cmpstat, 20, FTW_PHYS) == -1) {
301548847494SEnrico Perla - Sun Microsystems 			if (is_flag_on(UPDATE_ERROR))
301648847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
30177c478bd9Sstevel@tonic-gate 			/*
30187c478bd9Sstevel@tonic-gate 			 * Some files may not exist.
30197c478bd9Sstevel@tonic-gate 			 * For example: etc/rtc_config on a x86 diskless system
30207c478bd9Sstevel@tonic-gate 			 * Emit verbose message only
30217c478bd9Sstevel@tonic-gate 			 */
30227c478bd9Sstevel@tonic-gate 			if (bam_verbose)
3023f64ca102SToomas Soome 				bam_print(_("cannot find: %s: %s\n"),
3024f64ca102SToomas Soome 				    path, strerror(errno));
30257c478bd9Sstevel@tonic-gate 		}
30267c478bd9Sstevel@tonic-gate 	}
302748847494SEnrico Perla - Sun Microsystems 
302848847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
30297c478bd9Sstevel@tonic-gate }
30307c478bd9Sstevel@tonic-gate 
30314a9df875SEnrico Perla - Sun Microsystems /*
30324a9df875SEnrico Perla - Sun Microsystems  * Update the timestamp file.
30334a9df875SEnrico Perla - Sun Microsystems  */
30344a9df875SEnrico Perla - Sun Microsystems static void
30354a9df875SEnrico Perla - Sun Microsystems update_timestamp(char *root)
30364a9df875SEnrico Perla - Sun Microsystems {
30374a9df875SEnrico Perla - Sun Microsystems 	char	timestamp_path[PATH_MAX];
30384a9df875SEnrico Perla - Sun Microsystems 
30394a9df875SEnrico Perla - Sun Microsystems 	/* this path length has already been checked in check_flags_and_files */
30404a9df875SEnrico Perla - Sun Microsystems 	(void) snprintf(timestamp_path, sizeof (timestamp_path), "%s%s", root,
30414a9df875SEnrico Perla - Sun Microsystems 	    FILE_STAT_TIMESTAMP);
30424a9df875SEnrico Perla - Sun Microsystems 
30434a9df875SEnrico Perla - Sun Microsystems 	/*
30444a9df875SEnrico Perla - Sun Microsystems 	 * recreate the timestamp file. Since an outdated or absent timestamp
30454a9df875SEnrico Perla - Sun Microsystems 	 * file translates in a complete rebuild of the archive cache, notify
30464a9df875SEnrico Perla - Sun Microsystems 	 * the user of the performance issue.
30474a9df875SEnrico Perla - Sun Microsystems 	 */
30484a9df875SEnrico Perla - Sun Microsystems 	if (creat(timestamp_path, FILE_STAT_MODE) < 0) {
3049f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), timestamp_path,
3050f64ca102SToomas Soome 		    strerror(errno));
3051f64ca102SToomas Soome 		bam_error(_("failed to update the timestamp file, next"
3052f64ca102SToomas Soome 		    " archive update may experience reduced performance\n"));
30534a9df875SEnrico Perla - Sun Microsystems 	}
30544a9df875SEnrico Perla - Sun Microsystems }
30554a9df875SEnrico Perla - Sun Microsystems 
30564a9df875SEnrico Perla - Sun Microsystems 
30577c478bd9Sstevel@tonic-gate static void
30587c478bd9Sstevel@tonic-gate savenew(char *root)
30597c478bd9Sstevel@tonic-gate {
30607c478bd9Sstevel@tonic-gate 	char 	path[PATH_MAX];
30617c478bd9Sstevel@tonic-gate 	char 	path2[PATH_MAX];
30627c478bd9Sstevel@tonic-gate 	size_t 	sz;
30637c478bd9Sstevel@tonic-gate 	char 	*nstat;
30647c478bd9Sstevel@tonic-gate 	int 	fd, wrote, error;
30657c478bd9Sstevel@tonic-gate 
30667c478bd9Sstevel@tonic-gate 	nstat = NULL;
30677c478bd9Sstevel@tonic-gate 	sz = 0;
30687c478bd9Sstevel@tonic-gate 	error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz,
30697c478bd9Sstevel@tonic-gate 	    NV_ENCODE_XDR, 0);
30707c478bd9Sstevel@tonic-gate 	if (error) {
3071f64ca102SToomas Soome 		bam_error(_("failed to pack stat data: %s\n"),
3072f64ca102SToomas Soome 		    strerror(error));
30737c478bd9Sstevel@tonic-gate 		return;
30747c478bd9Sstevel@tonic-gate 	}
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP);
30777c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE);
30787c478bd9Sstevel@tonic-gate 	if (fd == -1) {
3079f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), path,
3080f64ca102SToomas Soome 		    strerror(errno));
30817c478bd9Sstevel@tonic-gate 		free(nstat);
30827c478bd9Sstevel@tonic-gate 		return;
30837c478bd9Sstevel@tonic-gate 	}
30847c478bd9Sstevel@tonic-gate 	wrote = write(fd, nstat, sz);
30857c478bd9Sstevel@tonic-gate 	if (wrote != sz) {
3086f64ca102SToomas Soome 		bam_error(_("write to file failed: %s: %s\n"), path,
3087f64ca102SToomas Soome 		    strerror(errno));
30887c478bd9Sstevel@tonic-gate 		(void) close(fd);
30897c478bd9Sstevel@tonic-gate 		free(nstat);
30907c478bd9Sstevel@tonic-gate 		return;
30917c478bd9Sstevel@tonic-gate 	}
30927c478bd9Sstevel@tonic-gate 	(void) close(fd);
30937c478bd9Sstevel@tonic-gate 	free(nstat);
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate 	(void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT);
30967c478bd9Sstevel@tonic-gate 	if (rename(path, path2) != 0) {
3097f64ca102SToomas Soome 		bam_error(_("rename to file failed: %s: %s\n"), path2,
3098f64ca102SToomas Soome 		    strerror(errno));
30997c478bd9Sstevel@tonic-gate 	}
31007c478bd9Sstevel@tonic-gate }
31017c478bd9Sstevel@tonic-gate 
310248847494SEnrico Perla - Sun Microsystems #define	init_walk_args()	bzero(&walk_arg, sizeof (walk_arg))
310348847494SEnrico Perla - Sun Microsystems 
31047c478bd9Sstevel@tonic-gate static void
31057c478bd9Sstevel@tonic-gate clear_walk_args(void)
31067c478bd9Sstevel@tonic-gate {
31077c478bd9Sstevel@tonic-gate 	nvlist_free(walk_arg.old_nvlp);
31087c478bd9Sstevel@tonic-gate 	nvlist_free(walk_arg.new_nvlp);
310948847494SEnrico Perla - Sun Microsystems 	if (walk_arg.sparcfile)
311048847494SEnrico Perla - Sun Microsystems 		(void) fclose(walk_arg.sparcfile);
31117c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
31127c478bd9Sstevel@tonic-gate 	walk_arg.new_nvlp = NULL;
311348847494SEnrico Perla - Sun Microsystems 	walk_arg.sparcfile = NULL;
31147c478bd9Sstevel@tonic-gate }
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate /*
31177c478bd9Sstevel@tonic-gate  * Returns:
31187c478bd9Sstevel@tonic-gate  *	0 - no update necessary
31197c478bd9Sstevel@tonic-gate  *	1 - update required.
31207c478bd9Sstevel@tonic-gate  *	BAM_ERROR (-1) - An error occurred
31217c478bd9Sstevel@tonic-gate  *
31227c478bd9Sstevel@tonic-gate  * Special handling for check (-n):
31237c478bd9Sstevel@tonic-gate  * ================================
31247c478bd9Sstevel@tonic-gate  * The check (-n) option produces parseable output.
31257c478bd9Sstevel@tonic-gate  * To do this, we suppress all stdout messages unrelated
31267c478bd9Sstevel@tonic-gate  * to out of sync files.
31277c478bd9Sstevel@tonic-gate  * All stderr messages are still printed though.
31287c478bd9Sstevel@tonic-gate  *
31297c478bd9Sstevel@tonic-gate  */
31307c478bd9Sstevel@tonic-gate static int
31317c478bd9Sstevel@tonic-gate update_required(char *root)
31327c478bd9Sstevel@tonic-gate {
31337c478bd9Sstevel@tonic-gate 	struct stat 	sb;
31347c478bd9Sstevel@tonic-gate 	char 		path[PATH_MAX];
31357c478bd9Sstevel@tonic-gate 	filelist_t 	flist;
31367c478bd9Sstevel@tonic-gate 	filelist_t 	*flistp = &flist;
313748847494SEnrico Perla - Sun Microsystems 	int 		ret;
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
31407c478bd9Sstevel@tonic-gate 
314148847494SEnrico Perla - Sun Microsystems 	if (is_sparc())
314248847494SEnrico Perla - Sun Microsystems 		set_flag(IS_SPARC_TARGET);
31437c478bd9Sstevel@tonic-gate 
31447c478bd9Sstevel@tonic-gate 	/*
314548847494SEnrico Perla - Sun Microsystems 	 * Check if cache directories and archives are present
31467c478bd9Sstevel@tonic-gate 	 */
314748847494SEnrico Perla - Sun Microsystems 
314848847494SEnrico Perla - Sun Microsystems 	ret = check_flags_and_files(root);
314948847494SEnrico Perla - Sun Microsystems 	if (ret < 0)
315048847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
31517c478bd9Sstevel@tonic-gate 
31527c478bd9Sstevel@tonic-gate 	/*
31537c478bd9Sstevel@tonic-gate 	 * In certain deployment scenarios, filestat may not
3154eff168e0SEnrico Perla - Sun Microsystems 	 * exist. Do not stop the boot process, but trigger an update
3155eff168e0SEnrico Perla - Sun Microsystems 	 * of the archives (which will recreate filestat.ramdisk).
31567c478bd9Sstevel@tonic-gate 	 */
31577c478bd9Sstevel@tonic-gate 	if (bam_smf_check) {
31587c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
3159eff168e0SEnrico Perla - Sun Microsystems 		if (stat(path, &sb) != 0) {
3160eff168e0SEnrico Perla - Sun Microsystems 			(void) creat(NEED_UPDATE_FILE, 0644);
31617c478bd9Sstevel@tonic-gate 			return (0);
31627c478bd9Sstevel@tonic-gate 		}
3163eff168e0SEnrico Perla - Sun Microsystems 	}
31647c478bd9Sstevel@tonic-gate 
31657c478bd9Sstevel@tonic-gate 	getoldstat(root);
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate 	/*
3168a28d77b8Svikram 	 * Check if the archive contains files that are no longer
3169a28d77b8Svikram 	 * present on the root filesystem.
3170a28d77b8Svikram 	 */
3171a28d77b8Svikram 	check4stale(root);
3172a28d77b8Svikram 
3173a28d77b8Svikram 	/*
31747c478bd9Sstevel@tonic-gate 	 * read list of files
31757c478bd9Sstevel@tonic-gate 	 */
31767c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
31777c478bd9Sstevel@tonic-gate 		clear_walk_args();
31787c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
31797c478bd9Sstevel@tonic-gate 	}
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate 	/*
31847c478bd9Sstevel@tonic-gate 	 * At this point either the update is required
31857c478bd9Sstevel@tonic-gate 	 * or the decision is pending. In either case
31867c478bd9Sstevel@tonic-gate 	 * we need to create new stat nvlist
31877c478bd9Sstevel@tonic-gate 	 */
31887c478bd9Sstevel@tonic-gate 	create_newstat();
31897c478bd9Sstevel@tonic-gate 	/*
31907c478bd9Sstevel@tonic-gate 	 * This walk does 2 things:
31917c478bd9Sstevel@tonic-gate 	 *  	- gets new stat data for every file
31927c478bd9Sstevel@tonic-gate 	 *	- (optional) compare old and new stat data
31937c478bd9Sstevel@tonic-gate 	 */
319448847494SEnrico Perla - Sun Microsystems 	ret = walk_list(root, &flist);
31957c478bd9Sstevel@tonic-gate 
31967c478bd9Sstevel@tonic-gate 	/* done with the file list */
31977c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
31987c478bd9Sstevel@tonic-gate 
319948847494SEnrico Perla - Sun Microsystems 	/* something went wrong */
320048847494SEnrico Perla - Sun Microsystems 
320148847494SEnrico Perla - Sun Microsystems 	if (ret == BAM_ERROR) {
3202f64ca102SToomas Soome 		bam_error(_("Failed to gather cache files, archives "
3203f64ca102SToomas Soome 		    "generation aborted\n"));
320448847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
32057c478bd9Sstevel@tonic-gate 	}
32067c478bd9Sstevel@tonic-gate 
320748847494SEnrico Perla - Sun Microsystems 	if (walk_arg.new_nvlp == NULL) {
3208cedc7e57SEnrico Perla - Sun Microsystems 		if (walk_arg.sparcfile != NULL)
320948847494SEnrico Perla - Sun Microsystems 			(void) fclose(walk_arg.sparcfile);
3210f64ca102SToomas Soome 		bam_error(_("cannot create new stat data\n"));
321148847494SEnrico Perla - Sun Microsystems 	}
32127c478bd9Sstevel@tonic-gate 
321348847494SEnrico Perla - Sun Microsystems 	/* If nothing was updated, discard newstat. */
321448847494SEnrico Perla - Sun Microsystems 
321548847494SEnrico Perla - Sun Microsystems 	if (!is_dir_flag_on(FILE32, NEED_UPDATE) &&
321648847494SEnrico Perla - Sun Microsystems 	    !is_dir_flag_on(FILE64, NEED_UPDATE)) {
32177c478bd9Sstevel@tonic-gate 		clear_walk_args();
32187c478bd9Sstevel@tonic-gate 		return (0);
32197c478bd9Sstevel@tonic-gate 	}
32207c478bd9Sstevel@tonic-gate 
3221cedc7e57SEnrico Perla - Sun Microsystems 	if (walk_arg.sparcfile != NULL)
322248847494SEnrico Perla - Sun Microsystems 		(void) fclose(walk_arg.sparcfile);
322348847494SEnrico Perla - Sun Microsystems 
32247c478bd9Sstevel@tonic-gate 	return (1);
32257c478bd9Sstevel@tonic-gate }
32267c478bd9Sstevel@tonic-gate 
322748847494SEnrico Perla - Sun Microsystems static int
322848847494SEnrico Perla - Sun Microsystems flushfs(char *root)
322948847494SEnrico Perla - Sun Microsystems {
323048847494SEnrico Perla - Sun Microsystems 	char	cmd[PATH_MAX + 30];
323148847494SEnrico Perla - Sun Microsystems 
323248847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmd, sizeof (cmd), "%s -f \"%s\" 2>/dev/null",
323348847494SEnrico Perla - Sun Microsystems 	    LOCKFS_PATH, root);
323448847494SEnrico Perla - Sun Microsystems 
323548847494SEnrico Perla - Sun Microsystems 	return (exec_cmd(cmd, NULL));
323648847494SEnrico Perla - Sun Microsystems }
323748847494SEnrico Perla - Sun Microsystems 
323848847494SEnrico Perla - Sun Microsystems static int
323948847494SEnrico Perla - Sun Microsystems do_archive_copy(char *source, char *dest)
324048847494SEnrico Perla - Sun Microsystems {
324148847494SEnrico Perla - Sun Microsystems 
3242cedc7e57SEnrico Perla - Sun Microsystems 	sync();
324348847494SEnrico Perla - Sun Microsystems 
324448847494SEnrico Perla - Sun Microsystems 	/* the equivalent of mv archive-new-$pid boot_archive */
3245cedc7e57SEnrico Perla - Sun Microsystems 	if (rename(source, dest) != 0) {
3246cedc7e57SEnrico Perla - Sun Microsystems 		(void) unlink(source);
324748847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
3248cedc7e57SEnrico Perla - Sun Microsystems 	}
324948847494SEnrico Perla - Sun Microsystems 
325048847494SEnrico Perla - Sun Microsystems 	if (flushfs(bam_root) != 0)
3251cedc7e57SEnrico Perla - Sun Microsystems 		sync();
325248847494SEnrico Perla - Sun Microsystems 
325348847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
325448847494SEnrico Perla - Sun Microsystems }
325548847494SEnrico Perla - Sun Microsystems 
325648847494SEnrico Perla - Sun Microsystems static int
325748847494SEnrico Perla - Sun Microsystems check_cmdline(filelist_t flist)
325848847494SEnrico Perla - Sun Microsystems {
325948847494SEnrico Perla - Sun Microsystems 	line_t	*lp;
326048847494SEnrico Perla - Sun Microsystems 
326148847494SEnrico Perla - Sun Microsystems 	for (lp = flist.head; lp; lp = lp->next) {
326248847494SEnrico Perla - Sun Microsystems 		if (strstr(lp->line, "Error:") != NULL ||
326348847494SEnrico Perla - Sun Microsystems 		    strstr(lp->line, "Inode number overflow") != NULL) {
3264cedc7e57SEnrico Perla - Sun Microsystems 			(void) fprintf(stderr, "%s\n", lp->line);
326548847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
326648847494SEnrico Perla - Sun Microsystems 		}
326748847494SEnrico Perla - Sun Microsystems 	}
326848847494SEnrico Perla - Sun Microsystems 
326948847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
327048847494SEnrico Perla - Sun Microsystems }
327148847494SEnrico Perla - Sun Microsystems 
3272cedc7e57SEnrico Perla - Sun Microsystems static void
3273cedc7e57SEnrico Perla - Sun Microsystems dump_errormsg(filelist_t flist)
3274cedc7e57SEnrico Perla - Sun Microsystems {
3275cedc7e57SEnrico Perla - Sun Microsystems 	line_t	*lp;
3276cedc7e57SEnrico Perla - Sun Microsystems 
3277cedc7e57SEnrico Perla - Sun Microsystems 	for (lp = flist.head; lp; lp = lp->next)
3278cedc7e57SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, "%s\n", lp->line);
3279cedc7e57SEnrico Perla - Sun Microsystems }
3280cedc7e57SEnrico Perla - Sun Microsystems 
328148847494SEnrico Perla - Sun Microsystems static int
328248847494SEnrico Perla - Sun Microsystems check_archive(char *dest)
328348847494SEnrico Perla - Sun Microsystems {
328448847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
328548847494SEnrico Perla - Sun Microsystems 
328648847494SEnrico Perla - Sun Microsystems 	if (stat(dest, &sb) != 0 || !S_ISREG(sb.st_mode) ||
328748847494SEnrico Perla - Sun Microsystems 	    sb.st_size < 10000) {
3288f64ca102SToomas Soome 		bam_error(_("archive file %s not generated correctly\n"), dest);
328948847494SEnrico Perla - Sun Microsystems 		(void) unlink(dest);
329048847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
329148847494SEnrico Perla - Sun Microsystems 	}
329248847494SEnrico Perla - Sun Microsystems 
329348847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
329448847494SEnrico Perla - Sun Microsystems }
329548847494SEnrico Perla - Sun Microsystems 
32966debd3f5SAlexander Eremin static boolean_t
32976debd3f5SAlexander Eremin is_be(char *root)
32986debd3f5SAlexander Eremin {
32996debd3f5SAlexander Eremin 	zfs_handle_t	*zhp;
33006debd3f5SAlexander Eremin 	libzfs_handle_t	*hdl;
33016debd3f5SAlexander Eremin 	be_node_list_t	*be_nodes = NULL;
33026debd3f5SAlexander Eremin 	be_node_list_t	*cur_be;
33036debd3f5SAlexander Eremin 	boolean_t	be_exist = B_FALSE;
330440a5c998SMatthew Ahrens 	char		ds_path[ZFS_MAX_DATASET_NAME_LEN];
33056debd3f5SAlexander Eremin 
33066debd3f5SAlexander Eremin 	if (!is_zfs(root))
33076debd3f5SAlexander Eremin 		return (B_FALSE);
33086debd3f5SAlexander Eremin 	/*
33096debd3f5SAlexander Eremin 	 * Get dataset for mountpoint
33106debd3f5SAlexander Eremin 	 */
33116debd3f5SAlexander Eremin 	if ((hdl = libzfs_init()) == NULL)
33126debd3f5SAlexander Eremin 		return (B_FALSE);
33136debd3f5SAlexander Eremin 
33146debd3f5SAlexander Eremin 	if ((zhp = zfs_path_to_zhandle(hdl, root,
33156debd3f5SAlexander Eremin 	    ZFS_TYPE_FILESYSTEM)) == NULL) {
33166debd3f5SAlexander Eremin 		libzfs_fini(hdl);
33176debd3f5SAlexander Eremin 		return (B_FALSE);
33186debd3f5SAlexander Eremin 	}
33196debd3f5SAlexander Eremin 
33206debd3f5SAlexander Eremin 	(void) strlcpy(ds_path, zfs_get_name(zhp), sizeof (ds_path));
33216debd3f5SAlexander Eremin 
33226debd3f5SAlexander Eremin 	/*
33236debd3f5SAlexander Eremin 	 * Check if the current dataset is BE
33246debd3f5SAlexander Eremin 	 */
3325*71668a2fSAndy Fiddaman 	if (be_list(NULL, &be_nodes, BE_LIST_DEFAULT) == BE_SUCCESS) {
33266debd3f5SAlexander Eremin 		for (cur_be = be_nodes; cur_be != NULL;
33276debd3f5SAlexander Eremin 		    cur_be = cur_be->be_next_node) {
33286debd3f5SAlexander Eremin 
33296debd3f5SAlexander Eremin 			/*
33306debd3f5SAlexander Eremin 			 * Because we guarantee that cur_be->be_root_ds
33316debd3f5SAlexander Eremin 			 * is null-terminated by internal data structure,
33326debd3f5SAlexander Eremin 			 * we can safely use strcmp()
33336debd3f5SAlexander Eremin 			 */
33346debd3f5SAlexander Eremin 			if (strcmp(ds_path, cur_be->be_root_ds) == 0) {
33356debd3f5SAlexander Eremin 				be_exist = B_TRUE;
33366debd3f5SAlexander Eremin 				break;
33376debd3f5SAlexander Eremin 			}
33386debd3f5SAlexander Eremin 		}
33396debd3f5SAlexander Eremin 		be_free_list(be_nodes);
33406debd3f5SAlexander Eremin 	}
33416debd3f5SAlexander Eremin 	zfs_close(zhp);
33426debd3f5SAlexander Eremin 	libzfs_fini(hdl);
33436debd3f5SAlexander Eremin 
33446debd3f5SAlexander Eremin 	return (be_exist);
33456debd3f5SAlexander Eremin }
33466debd3f5SAlexander Eremin 
334748847494SEnrico Perla - Sun Microsystems /*
334848847494SEnrico Perla - Sun Microsystems  * Returns 1 if mkiso is in the expected PATH, 0 otherwise
334948847494SEnrico Perla - Sun Microsystems  */
335048847494SEnrico Perla - Sun Microsystems static int
335148847494SEnrico Perla - Sun Microsystems is_mkisofs()
335248847494SEnrico Perla - Sun Microsystems {
3353cedc7e57SEnrico Perla - Sun Microsystems 	if (access(MKISOFS_PATH, X_OK) == 0)
335448847494SEnrico Perla - Sun Microsystems 		return (1);
335548847494SEnrico Perla - Sun Microsystems 	return (0);
335648847494SEnrico Perla - Sun Microsystems }
335748847494SEnrico Perla - Sun Microsystems 
335848847494SEnrico Perla - Sun Microsystems #define	MKISO_PARAMS	" -quiet -graft-points -dlrDJN -relaxed-filenames "
335948847494SEnrico Perla - Sun Microsystems 
336048847494SEnrico Perla - Sun Microsystems static int
336148847494SEnrico Perla - Sun Microsystems create_sparc_archive(char *archive, char *tempname, char *bootblk, char *list)
336248847494SEnrico Perla - Sun Microsystems {
336348847494SEnrico Perla - Sun Microsystems 	int		ret;
336448847494SEnrico Perla - Sun Microsystems 	char		cmdline[3 * PATH_MAX + 64];
336548847494SEnrico Perla - Sun Microsystems 	filelist_t	flist = {0};
336648847494SEnrico Perla - Sun Microsystems 	const char	*func = "create_sparc_archive()";
336748847494SEnrico Perla - Sun Microsystems 
336848847494SEnrico Perla - Sun Microsystems 	if (access(bootblk, R_OK) == 1) {
3369f64ca102SToomas Soome 		bam_error(_("unable to access bootblk file : %s\n"), bootblk);
337048847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
337148847494SEnrico Perla - Sun Microsystems 	}
337248847494SEnrico Perla - Sun Microsystems 
337348847494SEnrico Perla - Sun Microsystems 	/*
337448847494SEnrico Perla - Sun Microsystems 	 * Prepare mkisofs command line and execute it
337548847494SEnrico Perla - Sun Microsystems 	 */
337648847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s -G %s -o \"%s\" "
3377cedc7e57SEnrico Perla - Sun Microsystems 	    "-path-list \"%s\" 2>&1", MKISOFS_PATH, MKISO_PARAMS, bootblk,
337848847494SEnrico Perla - Sun Microsystems 	    tempname, list);
337948847494SEnrico Perla - Sun Microsystems 
3380f64ca102SToomas Soome 	BAM_DPRINTF(("%s: executing: %s\n", func, cmdline));
338148847494SEnrico Perla - Sun Microsystems 
338248847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
3383cedc7e57SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
3384cedc7e57SEnrico Perla - Sun Microsystems 		dump_errormsg(flist);
338548847494SEnrico Perla - Sun Microsystems 		goto out_err;
3386cedc7e57SEnrico Perla - Sun Microsystems 	}
338748847494SEnrico Perla - Sun Microsystems 
338848847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
338948847494SEnrico Perla - Sun Microsystems 
339048847494SEnrico Perla - Sun Microsystems 	/*
339148847494SEnrico Perla - Sun Microsystems 	 * Prepare dd command line to copy the bootblk on the new archive and
339248847494SEnrico Perla - Sun Microsystems 	 * execute it
339348847494SEnrico Perla - Sun Microsystems 	 */
339448847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s if=\"%s\" of=\"%s\""
3395cedc7e57SEnrico Perla - Sun Microsystems 	    " bs=1b oseek=1 count=15 conv=notrunc conv=sync 2>&1", DD_PATH_USR,
339648847494SEnrico Perla - Sun Microsystems 	    bootblk, tempname);
339748847494SEnrico Perla - Sun Microsystems 
3398f64ca102SToomas Soome 	BAM_DPRINTF(("%s: executing: %s\n", func, cmdline));
339948847494SEnrico Perla - Sun Microsystems 
340048847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
340148847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR)
340248847494SEnrico Perla - Sun Microsystems 		goto out_err;
340348847494SEnrico Perla - Sun Microsystems 
340448847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
340548847494SEnrico Perla - Sun Microsystems 
340648847494SEnrico Perla - Sun Microsystems 	/* Did we get a valid archive ? */
340748847494SEnrico Perla - Sun Microsystems 	if (check_archive(tempname) == BAM_ERROR)
340848847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
340948847494SEnrico Perla - Sun Microsystems 
341048847494SEnrico Perla - Sun Microsystems 	return (do_archive_copy(tempname, archive));
341148847494SEnrico Perla - Sun Microsystems 
341248847494SEnrico Perla - Sun Microsystems out_err:
341348847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
3414f64ca102SToomas Soome 	bam_error(_("boot-archive creation FAILED, command: '%s'\n"), cmdline);
341548847494SEnrico Perla - Sun Microsystems 	(void) unlink(tempname);
341648847494SEnrico Perla - Sun Microsystems 	return (BAM_ERROR);
341748847494SEnrico Perla - Sun Microsystems }
341848847494SEnrico Perla - Sun Microsystems 
341948847494SEnrico Perla - Sun Microsystems static unsigned int
342048847494SEnrico Perla - Sun Microsystems from_733(unsigned char *s)
342148847494SEnrico Perla - Sun Microsystems {
342248847494SEnrico Perla - Sun Microsystems 	int		i;
342348847494SEnrico Perla - Sun Microsystems 	unsigned int	ret = 0;
342448847494SEnrico Perla - Sun Microsystems 
342548847494SEnrico Perla - Sun Microsystems 	for (i = 0; i < 4; i++)
342648847494SEnrico Perla - Sun Microsystems 		ret |= s[i] << (8 * i);
342748847494SEnrico Perla - Sun Microsystems 
342848847494SEnrico Perla - Sun Microsystems 	return (ret);
342948847494SEnrico Perla - Sun Microsystems }
343048847494SEnrico Perla - Sun Microsystems 
343148847494SEnrico Perla - Sun Microsystems static void
343248847494SEnrico Perla - Sun Microsystems to_733(unsigned char *s, unsigned int val)
343348847494SEnrico Perla - Sun Microsystems {
343448847494SEnrico Perla - Sun Microsystems 	int	i;
343548847494SEnrico Perla - Sun Microsystems 
343648847494SEnrico Perla - Sun Microsystems 	for (i = 0; i < 4; i++)
343748847494SEnrico Perla - Sun Microsystems 		s[i] = s[7-i] = (val >> (8 * i)) & 0xFF;
343848847494SEnrico Perla - Sun Microsystems }
343948847494SEnrico Perla - Sun Microsystems 
344048847494SEnrico Perla - Sun Microsystems /*
3441eeb2c267SToomas Soome  * creates sha1 hash of archive
3442eeb2c267SToomas Soome  */
3443eeb2c267SToomas Soome static int
3444eeb2c267SToomas Soome digest_archive(const char *archive)
3445eeb2c267SToomas Soome {
3446eeb2c267SToomas Soome 	char *archive_hash;
3447eeb2c267SToomas Soome 	char *hash;
3448eeb2c267SToomas Soome 	int ret;
3449eeb2c267SToomas Soome 	FILE *fp;
3450eeb2c267SToomas Soome 
3451eeb2c267SToomas Soome 	(void) asprintf(&archive_hash, "%s.hash", archive);
3452eeb2c267SToomas Soome 	if (archive_hash == NULL)
3453eeb2c267SToomas Soome 		return (BAM_ERROR);
3454eeb2c267SToomas Soome 
3455eeb2c267SToomas Soome 	if ((ret = bootadm_digest(archive, &hash)) == BAM_ERROR) {
3456eeb2c267SToomas Soome 		free(archive_hash);
3457eeb2c267SToomas Soome 		return (ret);
3458eeb2c267SToomas Soome 	}
3459eeb2c267SToomas Soome 
3460eeb2c267SToomas Soome 	fp = fopen(archive_hash, "w");
3461eeb2c267SToomas Soome 	if (fp == NULL) {
3462eeb2c267SToomas Soome 		free(archive_hash);
3463eeb2c267SToomas Soome 		free(hash);
3464eeb2c267SToomas Soome 		return (BAM_ERROR);
3465eeb2c267SToomas Soome 	}
3466eeb2c267SToomas Soome 
3467eeb2c267SToomas Soome 	(void) fprintf(fp, "%s\n", hash);
3468eeb2c267SToomas Soome 	(void) fclose(fp);
3469eeb2c267SToomas Soome 	free(hash);
3470eeb2c267SToomas Soome 	free(archive_hash);
3471eeb2c267SToomas Soome 	return (BAM_SUCCESS);
3472eeb2c267SToomas Soome }
3473eeb2c267SToomas Soome 
3474eeb2c267SToomas Soome /*
347548847494SEnrico Perla - Sun Microsystems  * Extends the current boot archive without recreating it from scratch
347648847494SEnrico Perla - Sun Microsystems  */
347748847494SEnrico Perla - Sun Microsystems static int
347848847494SEnrico Perla - Sun Microsystems extend_iso_archive(char *archive, char *tempname, char *update_dir)
347948847494SEnrico Perla - Sun Microsystems {
348048847494SEnrico Perla - Sun Microsystems 	int			fd = -1, newfd = -1, ret, i;
348148847494SEnrico Perla - Sun Microsystems 	int			next_session = 0, new_size = 0;
348248847494SEnrico Perla - Sun Microsystems 	char			cmdline[3 * PATH_MAX + 64];
348348847494SEnrico Perla - Sun Microsystems 	const char		*func = "extend_iso_archive()";
348448847494SEnrico Perla - Sun Microsystems 	filelist_t		flist = {0};
348548847494SEnrico Perla - Sun Microsystems 	struct iso_pdesc	saved_desc[MAX_IVDs];
348648847494SEnrico Perla - Sun Microsystems 
348748847494SEnrico Perla - Sun Microsystems 	fd = open(archive, O_RDWR);
348848847494SEnrico Perla - Sun Microsystems 	if (fd == -1) {
3489cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
3490f64ca102SToomas Soome 			bam_error(_("failed to open file: %s: %s\n"),
3491f64ca102SToomas Soome 			    archive, strerror(errno));
349248847494SEnrico Perla - Sun Microsystems 		goto out_err;
349348847494SEnrico Perla - Sun Microsystems 	}
349448847494SEnrico Perla - Sun Microsystems 
349548847494SEnrico Perla - Sun Microsystems 	/*
349648847494SEnrico Perla - Sun Microsystems 	 * A partial read is likely due to a corrupted file
349748847494SEnrico Perla - Sun Microsystems 	 */
349848847494SEnrico Perla - Sun Microsystems 	ret = pread64(fd, saved_desc, sizeof (saved_desc),
349948847494SEnrico Perla - Sun Microsystems 	    VOLDESC_OFF * CD_BLOCK);
350048847494SEnrico Perla - Sun Microsystems 	if (ret != sizeof (saved_desc)) {
3501cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
3502f64ca102SToomas Soome 			bam_error(_("read failed for file: %s: %s\n"),
3503f64ca102SToomas Soome 			    archive, strerror(errno));
350448847494SEnrico Perla - Sun Microsystems 		goto out_err;
350548847494SEnrico Perla - Sun Microsystems 	}
350648847494SEnrico Perla - Sun Microsystems 
350748847494SEnrico Perla - Sun Microsystems 	if (memcmp(saved_desc[0].type, "\1CD001", 6)) {
3508cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
3509f64ca102SToomas Soome 			bam_error(_("iso descriptor signature for %s is "
3510f64ca102SToomas Soome 			    "invalid\n"), archive);
351148847494SEnrico Perla - Sun Microsystems 		goto out_err;
351248847494SEnrico Perla - Sun Microsystems 	}
351348847494SEnrico Perla - Sun Microsystems 
351448847494SEnrico Perla - Sun Microsystems 	/*
351548847494SEnrico Perla - Sun Microsystems 	 * Read primary descriptor and locate next_session offset (it should
351648847494SEnrico Perla - Sun Microsystems 	 * point to the end of the archive)
351748847494SEnrico Perla - Sun Microsystems 	 */
351848847494SEnrico Perla - Sun Microsystems 	next_session = P2ROUNDUP(from_733(saved_desc[0].volume_space_size), 16);
351948847494SEnrico Perla - Sun Microsystems 
352048847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s -C 16,%d -M %s %s -o \""
3521cedc7e57SEnrico Perla - Sun Microsystems 	    "%s\" \"%s\" 2>&1", MKISOFS_PATH, next_session, archive,
3522cedc7e57SEnrico Perla - Sun Microsystems 	    MKISO_PARAMS, tempname, update_dir);
352348847494SEnrico Perla - Sun Microsystems 
3524f64ca102SToomas Soome 	BAM_DPRINTF(("%s: executing: %s\n", func, cmdline));
352548847494SEnrico Perla - Sun Microsystems 
352648847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
352748847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
3528cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose) {
3529f64ca102SToomas Soome 			bam_error(_("Command '%s' failed while generating "
3530f64ca102SToomas Soome 			    "multisession archive\n"), cmdline);
3531cedc7e57SEnrico Perla - Sun Microsystems 			dump_errormsg(flist);
3532cedc7e57SEnrico Perla - Sun Microsystems 		}
353348847494SEnrico Perla - Sun Microsystems 		goto out_flist_err;
353448847494SEnrico Perla - Sun Microsystems 	}
353548847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
353648847494SEnrico Perla - Sun Microsystems 
353748847494SEnrico Perla - Sun Microsystems 	newfd = open(tempname, O_RDONLY);
353848847494SEnrico Perla - Sun Microsystems 	if (newfd == -1) {
3539cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
3540f64ca102SToomas Soome 			bam_error(_("failed to open file: %s: %s\n"),
3541f64ca102SToomas Soome 			    archive, strerror(errno));
354248847494SEnrico Perla - Sun Microsystems 		goto out_err;
354348847494SEnrico Perla - Sun Microsystems 	}
354448847494SEnrico Perla - Sun Microsystems 
354548847494SEnrico Perla - Sun Microsystems 	ret = pread64(newfd, saved_desc, sizeof (saved_desc),
354648847494SEnrico Perla - Sun Microsystems 	    VOLDESC_OFF * CD_BLOCK);
354748847494SEnrico Perla - Sun Microsystems 	if (ret != sizeof (saved_desc)) {
3548cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
3549f64ca102SToomas Soome 			bam_error(_("read failed for file: %s: %s\n"),
3550f64ca102SToomas Soome 			    archive, strerror(errno));
355148847494SEnrico Perla - Sun Microsystems 		goto out_err;
355248847494SEnrico Perla - Sun Microsystems 	}
355348847494SEnrico Perla - Sun Microsystems 
355448847494SEnrico Perla - Sun Microsystems 	if (memcmp(saved_desc[0].type, "\1CD001", 6)) {
3555cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
3556f64ca102SToomas Soome 			bam_error(_("iso descriptor signature for %s is "
3557f64ca102SToomas Soome 			    "invalid\n"), archive);
355848847494SEnrico Perla - Sun Microsystems 		goto out_err;
355948847494SEnrico Perla - Sun Microsystems 	}
356048847494SEnrico Perla - Sun Microsystems 
356148847494SEnrico Perla - Sun Microsystems 	new_size = from_733(saved_desc[0].volume_space_size) + next_session;
356248847494SEnrico Perla - Sun Microsystems 	to_733(saved_desc[0].volume_space_size, new_size);
356348847494SEnrico Perla - Sun Microsystems 
356448847494SEnrico Perla - Sun Microsystems 	for (i = 1; i < MAX_IVDs; i++) {
356548847494SEnrico Perla - Sun Microsystems 		if (saved_desc[i].type[0] == (unsigned char)255)
356648847494SEnrico Perla - Sun Microsystems 			break;
356748847494SEnrico Perla - Sun Microsystems 		if (memcmp(saved_desc[i].id, "CD001", 5))
356848847494SEnrico Perla - Sun Microsystems 			break;
356948847494SEnrico Perla - Sun Microsystems 
357048847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
357148847494SEnrico Perla - Sun Microsystems 			bam_print("%s: Updating descriptor entry [%d]\n", func,
357248847494SEnrico Perla - Sun Microsystems 			    i);
357348847494SEnrico Perla - Sun Microsystems 
357448847494SEnrico Perla - Sun Microsystems 		to_733(saved_desc[i].volume_space_size, new_size);
357548847494SEnrico Perla - Sun Microsystems 	}
357648847494SEnrico Perla - Sun Microsystems 
357748847494SEnrico Perla - Sun Microsystems 	ret = pwrite64(fd, saved_desc, DVD_BLOCK, VOLDESC_OFF*CD_BLOCK);
357848847494SEnrico Perla - Sun Microsystems 	if (ret != DVD_BLOCK) {
3579cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
3580f64ca102SToomas Soome 			bam_error(_("write to file failed: %s: %s\n"),
3581f64ca102SToomas Soome 			    archive, strerror(errno));
358248847494SEnrico Perla - Sun Microsystems 		goto out_err;
358348847494SEnrico Perla - Sun Microsystems 	}
358448847494SEnrico Perla - Sun Microsystems 	(void) close(newfd);
358548847494SEnrico Perla - Sun Microsystems 	newfd = -1;
358648847494SEnrico Perla - Sun Microsystems 
3587cedc7e57SEnrico Perla - Sun Microsystems 	ret = fsync(fd);
3588cedc7e57SEnrico Perla - Sun Microsystems 	if (ret != 0)
3589cedc7e57SEnrico Perla - Sun Microsystems 		sync();
3590cedc7e57SEnrico Perla - Sun Microsystems 
359148847494SEnrico Perla - Sun Microsystems 	ret = close(fd);
359248847494SEnrico Perla - Sun Microsystems 	if (ret != 0) {
3593cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
3594f64ca102SToomas Soome 			bam_error(_("failed to close file: %s: %s\n"),
3595f64ca102SToomas Soome 			    archive, strerror(errno));
359648847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
359748847494SEnrico Perla - Sun Microsystems 	}
359848847494SEnrico Perla - Sun Microsystems 	fd = -1;
359948847494SEnrico Perla - Sun Microsystems 
360048847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s if=%s of=%s bs=32k "
3601cedc7e57SEnrico Perla - Sun Microsystems 	    "seek=%d conv=sync 2>&1", DD_PATH_USR, tempname, archive,
360248847494SEnrico Perla - Sun Microsystems 	    (next_session/16));
360348847494SEnrico Perla - Sun Microsystems 
3604f64ca102SToomas Soome 	BAM_DPRINTF(("%s: executing: %s\n", func, cmdline));
360548847494SEnrico Perla - Sun Microsystems 
360648847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
360748847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
3608cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
3609f64ca102SToomas Soome 			bam_error(_("Command '%s' failed while generating "
3610f64ca102SToomas Soome 			    "multisession archive\n"), cmdline);
361148847494SEnrico Perla - Sun Microsystems 		goto out_flist_err;
361248847494SEnrico Perla - Sun Microsystems 	}
361348847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
361448847494SEnrico Perla - Sun Microsystems 
361548847494SEnrico Perla - Sun Microsystems 	(void) unlink(tempname);
361648847494SEnrico Perla - Sun Microsystems 
3617eeb2c267SToomas Soome 	if (digest_archive(archive) == BAM_ERROR && bam_verbose)
3618eeb2c267SToomas Soome 		bam_print("boot archive hashing failed\n");
3619eeb2c267SToomas Soome 
3620cedc7e57SEnrico Perla - Sun Microsystems 	if (flushfs(bam_root) != 0)
3621cedc7e57SEnrico Perla - Sun Microsystems 		sync();
3622cedc7e57SEnrico Perla - Sun Microsystems 
362348847494SEnrico Perla - Sun Microsystems 	if (bam_verbose)
362448847494SEnrico Perla - Sun Microsystems 		bam_print("boot archive updated successfully\n");
362548847494SEnrico Perla - Sun Microsystems 
362648847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
362748847494SEnrico Perla - Sun Microsystems 
362848847494SEnrico Perla - Sun Microsystems out_flist_err:
362948847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
363048847494SEnrico Perla - Sun Microsystems out_err:
363148847494SEnrico Perla - Sun Microsystems 	if (fd != -1)
363248847494SEnrico Perla - Sun Microsystems 		(void) close(fd);
363348847494SEnrico Perla - Sun Microsystems 	if (newfd != -1)
363448847494SEnrico Perla - Sun Microsystems 		(void) close(newfd);
363548847494SEnrico Perla - Sun Microsystems 	return (BAM_ERROR);
363648847494SEnrico Perla - Sun Microsystems }
363748847494SEnrico Perla - Sun Microsystems 
363848847494SEnrico Perla - Sun Microsystems static int
363948847494SEnrico Perla - Sun Microsystems create_x86_archive(char *archive, char *tempname, char *update_dir)
364048847494SEnrico Perla - Sun Microsystems {
364148847494SEnrico Perla - Sun Microsystems 	int		ret;
364248847494SEnrico Perla - Sun Microsystems 	char		cmdline[3 * PATH_MAX + 64];
364348847494SEnrico Perla - Sun Microsystems 	filelist_t	flist = {0};
364448847494SEnrico Perla - Sun Microsystems 	const char	*func = "create_x86_archive()";
364548847494SEnrico Perla - Sun Microsystems 
364648847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s -o \"%s\" \"%s\" "
3647cedc7e57SEnrico Perla - Sun Microsystems 	    "2>&1", MKISOFS_PATH, MKISO_PARAMS, tempname, update_dir);
364848847494SEnrico Perla - Sun Microsystems 
3649f64ca102SToomas Soome 	BAM_DPRINTF(("%s: executing: %s\n", func, cmdline));
365048847494SEnrico Perla - Sun Microsystems 
365148847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
365248847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
3653f64ca102SToomas Soome 		bam_error(_("boot-archive creation FAILED, command: '%s'\n"),
3654f64ca102SToomas Soome 		    cmdline);
3655cedc7e57SEnrico Perla - Sun Microsystems 		dump_errormsg(flist);
365648847494SEnrico Perla - Sun Microsystems 		filelist_free(&flist);
365748847494SEnrico Perla - Sun Microsystems 		(void) unlink(tempname);
365848847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
365948847494SEnrico Perla - Sun Microsystems 	}
366048847494SEnrico Perla - Sun Microsystems 
366148847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
366248847494SEnrico Perla - Sun Microsystems 
366348847494SEnrico Perla - Sun Microsystems 	if (check_archive(tempname) == BAM_ERROR)
366448847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
366548847494SEnrico Perla - Sun Microsystems 
366648847494SEnrico Perla - Sun Microsystems 	return (do_archive_copy(tempname, archive));
366748847494SEnrico Perla - Sun Microsystems }
366848847494SEnrico Perla - Sun Microsystems 
366948847494SEnrico Perla - Sun Microsystems static int
367048847494SEnrico Perla - Sun Microsystems mkisofs_archive(char *root, int what)
367148847494SEnrico Perla - Sun Microsystems {
367248847494SEnrico Perla - Sun Microsystems 	int		ret;
367348847494SEnrico Perla - Sun Microsystems 	char		temp[PATH_MAX];
367448847494SEnrico Perla - Sun Microsystems 	char 		bootblk[PATH_MAX];
367548847494SEnrico Perla - Sun Microsystems 	char		boot_archive[PATH_MAX];
367648847494SEnrico Perla - Sun Microsystems 
367748847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
367848847494SEnrico Perla - Sun Microsystems 		ret = snprintf(temp, sizeof (temp),
367948847494SEnrico Perla - Sun Microsystems 		    "%s%s%s/amd64/archive-new-%d", root, ARCHIVE_PREFIX,
368048847494SEnrico Perla - Sun Microsystems 		    get_machine(), getpid());
368148847494SEnrico Perla - Sun Microsystems 	else
368248847494SEnrico Perla - Sun Microsystems 		ret = snprintf(temp, sizeof (temp), "%s%s%s/archive-new-%d",
368348847494SEnrico Perla - Sun Microsystems 		    root, ARCHIVE_PREFIX, get_machine(), getpid());
368448847494SEnrico Perla - Sun Microsystems 
368548847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (temp))
368648847494SEnrico Perla - Sun Microsystems 		goto out_path_err;
368748847494SEnrico Perla - Sun Microsystems 
368848847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
368948847494SEnrico Perla - Sun Microsystems 		ret = snprintf(boot_archive, sizeof (boot_archive),
369048847494SEnrico Perla - Sun Microsystems 		    "%s%s%s/amd64%s", root, ARCHIVE_PREFIX, get_machine(),
369148847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_SUFFIX);
369248847494SEnrico Perla - Sun Microsystems 	else
369348847494SEnrico Perla - Sun Microsystems 		ret = snprintf(boot_archive, sizeof (boot_archive),
369448847494SEnrico Perla - Sun Microsystems 		    "%s%s%s%s", root, ARCHIVE_PREFIX, get_machine(),
369548847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_SUFFIX);
369648847494SEnrico Perla - Sun Microsystems 
369748847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (boot_archive))
369848847494SEnrico Perla - Sun Microsystems 		goto out_path_err;
369948847494SEnrico Perla - Sun Microsystems 
370048847494SEnrico Perla - Sun Microsystems 	bam_print("updating %s\n", boot_archive);
370148847494SEnrico Perla - Sun Microsystems 
370248847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET)) {
370348847494SEnrico Perla - Sun Microsystems 		ret = snprintf(bootblk, sizeof (bootblk),
370448847494SEnrico Perla - Sun Microsystems 		    "%s/platform/%s/lib/fs/hsfs/bootblk", root, get_machine());
370548847494SEnrico Perla - Sun Microsystems 		if (ret >= sizeof (bootblk))
370648847494SEnrico Perla - Sun Microsystems 			goto out_path_err;
370748847494SEnrico Perla - Sun Microsystems 
370848847494SEnrico Perla - Sun Microsystems 		ret = create_sparc_archive(boot_archive, temp, bootblk,
370948847494SEnrico Perla - Sun Microsystems 		    get_cachedir(what));
371048847494SEnrico Perla - Sun Microsystems 	} else {
371148847494SEnrico Perla - Sun Microsystems 		if (!is_dir_flag_on(what, NO_MULTI)) {
371248847494SEnrico Perla - Sun Microsystems 			if (bam_verbose)
371348847494SEnrico Perla - Sun Microsystems 				bam_print("Attempting to extend x86 archive: "
371448847494SEnrico Perla - Sun Microsystems 				    "%s\n", boot_archive);
371548847494SEnrico Perla - Sun Microsystems 
371648847494SEnrico Perla - Sun Microsystems 			ret = extend_iso_archive(boot_archive, temp,
371748847494SEnrico Perla - Sun Microsystems 			    get_updatedir(what));
371848847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_SUCCESS) {
371948847494SEnrico Perla - Sun Microsystems 				if (bam_verbose)
372048847494SEnrico Perla - Sun Microsystems 					bam_print("Successfully extended %s\n",
372148847494SEnrico Perla - Sun Microsystems 					    boot_archive);
372248847494SEnrico Perla - Sun Microsystems 
372348847494SEnrico Perla - Sun Microsystems 				(void) rmdir_r(get_updatedir(what));
372448847494SEnrico Perla - Sun Microsystems 				return (BAM_SUCCESS);
372548847494SEnrico Perla - Sun Microsystems 			}
372648847494SEnrico Perla - Sun Microsystems 		}
372748847494SEnrico Perla - Sun Microsystems 		/*
372848847494SEnrico Perla - Sun Microsystems 		 * The boot archive will be recreated from scratch. We get here
372948847494SEnrico Perla - Sun Microsystems 		 * if at least one of these conditions is true:
373048847494SEnrico Perla - Sun Microsystems 		 * - bootadm was called without the -e switch
373148847494SEnrico Perla - Sun Microsystems 		 * - the archive (or the archive cache) doesn't exist
373248847494SEnrico Perla - Sun Microsystems 		 * - archive size is bigger than BA_SIZE_MAX
373348847494SEnrico Perla - Sun Microsystems 		 * - more than COUNT_MAX files need to be updated
373448847494SEnrico Perla - Sun Microsystems 		 * - an error occourred either populating the /updates directory
373548847494SEnrico Perla - Sun Microsystems 		 *   or extend_iso_archive() failed
373648847494SEnrico Perla - Sun Microsystems 		 */
373748847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
373848847494SEnrico Perla - Sun Microsystems 			bam_print("Unable to extend %s... rebuilding archive\n",
373948847494SEnrico Perla - Sun Microsystems 			    boot_archive);
374048847494SEnrico Perla - Sun Microsystems 
374148847494SEnrico Perla - Sun Microsystems 		if (get_updatedir(what)[0] != '\0')
374248847494SEnrico Perla - Sun Microsystems 			(void) rmdir_r(get_updatedir(what));
374348847494SEnrico Perla - Sun Microsystems 
374448847494SEnrico Perla - Sun Microsystems 
374548847494SEnrico Perla - Sun Microsystems 		ret = create_x86_archive(boot_archive, temp,
374648847494SEnrico Perla - Sun Microsystems 		    get_cachedir(what));
374748847494SEnrico Perla - Sun Microsystems 	}
374848847494SEnrico Perla - Sun Microsystems 
3749eeb2c267SToomas Soome 	if (digest_archive(boot_archive) == BAM_ERROR && bam_verbose)
3750eeb2c267SToomas Soome 		bam_print("boot archive hashing failed\n");
3751eeb2c267SToomas Soome 
375248847494SEnrico Perla - Sun Microsystems 	if (ret == BAM_SUCCESS && bam_verbose)
375348847494SEnrico Perla - Sun Microsystems 		bam_print("Successfully created %s\n", boot_archive);
375448847494SEnrico Perla - Sun Microsystems 
375548847494SEnrico Perla - Sun Microsystems 	return (ret);
375648847494SEnrico Perla - Sun Microsystems 
375748847494SEnrico Perla - Sun Microsystems out_path_err:
3758f64ca102SToomas Soome 	bam_error(_("unable to create path on mountpoint %s, path too long\n"),
3759f64ca102SToomas Soome 	    root);
376048847494SEnrico Perla - Sun Microsystems 	return (BAM_ERROR);
376148847494SEnrico Perla - Sun Microsystems }
376248847494SEnrico Perla - Sun Microsystems 
37637c478bd9Sstevel@tonic-gate static error_t
37647c478bd9Sstevel@tonic-gate create_ramdisk(char *root)
37657c478bd9Sstevel@tonic-gate {
37667c478bd9Sstevel@tonic-gate 	char *cmdline, path[PATH_MAX];
37677c478bd9Sstevel@tonic-gate 	size_t len;
37687c478bd9Sstevel@tonic-gate 	struct stat sb;
3769cedc7e57SEnrico Perla - Sun Microsystems 	int ret, what, status = BAM_SUCCESS;
377048847494SEnrico Perla - Sun Microsystems 
377148847494SEnrico Perla - Sun Microsystems 	/* If there is mkisofs, use it to create the required archives */
377248847494SEnrico Perla - Sun Microsystems 	if (is_mkisofs()) {
377348847494SEnrico Perla - Sun Microsystems 		for (what = FILE32; what < CACHEDIR_NUM; what++) {
3774cedc7e57SEnrico Perla - Sun Microsystems 			if (has_cachedir(what) && is_dir_flag_on(what,
3775cedc7e57SEnrico Perla - Sun Microsystems 			    NEED_UPDATE)) {
377648847494SEnrico Perla - Sun Microsystems 				ret = mkisofs_archive(root, what);
377748847494SEnrico Perla - Sun Microsystems 				if (ret != 0)
3778cedc7e57SEnrico Perla - Sun Microsystems 					status = BAM_ERROR;
377948847494SEnrico Perla - Sun Microsystems 			}
378048847494SEnrico Perla - Sun Microsystems 		}
3781cedc7e57SEnrico Perla - Sun Microsystems 		return (status);
378248847494SEnrico Perla - Sun Microsystems 	}
37837c478bd9Sstevel@tonic-gate 
37847c478bd9Sstevel@tonic-gate 	/*
378548847494SEnrico Perla - Sun Microsystems 	 * Else setup command args for create_ramdisk.ksh for the UFS archives
3786eeb2c267SToomas Soome 	 * Note: we will not create hash here, CREATE_RAMDISK should create it.
37877c478bd9Sstevel@tonic-gate 	 */
378848847494SEnrico Perla - Sun Microsystems 	if (bam_verbose)
378948847494SEnrico Perla - Sun Microsystems 		bam_print("mkisofs not found, creating UFS archive\n");
379048847494SEnrico Perla - Sun Microsystems 
3791986fd29aSsetje 	(void) snprintf(path, sizeof (path), "%s/%s", root, CREATE_RAMDISK);
37927c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
3793f64ca102SToomas Soome 		bam_error(_("archive creation file not found: %s: %s\n"),
3794f64ca102SToomas Soome 		    path, strerror(errno));
37957c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
37967c478bd9Sstevel@tonic-gate 	}
37977c478bd9Sstevel@tonic-gate 
3798cedc7e57SEnrico Perla - Sun Microsystems 	if (is_safe_exec(path) == BAM_ERROR)
3799cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
3800cedc7e57SEnrico Perla - Sun Microsystems 
38017c478bd9Sstevel@tonic-gate 	len = strlen(path) + strlen(root) + 10;	/* room for space + -R */
3802d876c67dSjg 	if (bam_alt_platform)
3803d876c67dSjg 		len += strlen(bam_platform) + strlen("-p ");
38047c478bd9Sstevel@tonic-gate 	cmdline = s_calloc(1, len);
38057c478bd9Sstevel@tonic-gate 
3806d876c67dSjg 	if (bam_alt_platform) {
3807d876c67dSjg 		assert(strlen(root) > 1);
3808d876c67dSjg 		(void) snprintf(cmdline, len, "%s -p %s -R %s",
3809d876c67dSjg 		    path, bam_platform, root);
3810d876c67dSjg 		/* chop off / at the end */
3811d876c67dSjg 		cmdline[strlen(cmdline) - 1] = '\0';
3812d876c67dSjg 	} else if (strlen(root) > 1) {
38137c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s -R %s", path, root);
38147c478bd9Sstevel@tonic-gate 		/* chop off / at the end */
38157c478bd9Sstevel@tonic-gate 		cmdline[strlen(cmdline) - 1] = '\0';
38167c478bd9Sstevel@tonic-gate 	} else
38177c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s", path);
38187c478bd9Sstevel@tonic-gate 
3819986fd29aSsetje 	if (exec_cmd(cmdline, NULL) != 0) {
3820f64ca102SToomas Soome 		bam_error(_("boot-archive creation FAILED, command: '%s'\n"),
3821f64ca102SToomas Soome 		    cmdline);
38227c478bd9Sstevel@tonic-gate 		free(cmdline);
38237c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
38247c478bd9Sstevel@tonic-gate 	}
38257c478bd9Sstevel@tonic-gate 	free(cmdline);
38267c478bd9Sstevel@tonic-gate 	/*
3827986fd29aSsetje 	 * The existence of the expected archives used to be
3828986fd29aSsetje 	 * verified here. This check is done in create_ramdisk as
3829986fd29aSsetje 	 * it needs to be in sync with the altroot operated upon.
38307c478bd9Sstevel@tonic-gate 	 */
38317c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
38327c478bd9Sstevel@tonic-gate }
38337c478bd9Sstevel@tonic-gate 
38347c478bd9Sstevel@tonic-gate /*
38357c478bd9Sstevel@tonic-gate  * Checks if target filesystem is on a ramdisk
38367c478bd9Sstevel@tonic-gate  * 1 - is miniroot
38377c478bd9Sstevel@tonic-gate  * 0 - is not
38387c478bd9Sstevel@tonic-gate  * When in doubt assume it is not a ramdisk.
38397c478bd9Sstevel@tonic-gate  */
38407c478bd9Sstevel@tonic-gate static int
38417c478bd9Sstevel@tonic-gate is_ramdisk(char *root)
38427c478bd9Sstevel@tonic-gate {
38437c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
38447c478bd9Sstevel@tonic-gate 	FILE *fp;
38457c478bd9Sstevel@tonic-gate 	int found;
3846b610f78eSvikram 	char mntpt[PATH_MAX];
3847b610f78eSvikram 	char *cp;
38487c478bd9Sstevel@tonic-gate 
38497c478bd9Sstevel@tonic-gate 	/*
38507c478bd9Sstevel@tonic-gate 	 * There are 3 situations where creating archive is
38517c478bd9Sstevel@tonic-gate 	 * of dubious value:
3852b610f78eSvikram 	 *	- create boot_archive on a lofi-mounted boot_archive
38537c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk which is the root filesystem
38547c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk mounted somewhere else
38557c478bd9Sstevel@tonic-gate 	 * The first is not easy to detect and checking for it is not
38567c478bd9Sstevel@tonic-gate 	 * worth it.
38577c478bd9Sstevel@tonic-gate 	 * The other two conditions are handled here
38587c478bd9Sstevel@tonic-gate 	 */
38597c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
38607c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
3861f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"),
3862f64ca102SToomas Soome 		    MNTTAB, strerror(errno));
38637c478bd9Sstevel@tonic-gate 		return (0);
38647c478bd9Sstevel@tonic-gate 	}
38657c478bd9Sstevel@tonic-gate 
38667c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
38677c478bd9Sstevel@tonic-gate 
3868b610f78eSvikram 	/*
3869b610f78eSvikram 	 * Remove any trailing / from the mount point
3870b610f78eSvikram 	 */
3871b610f78eSvikram 	(void) strlcpy(mntpt, root, sizeof (mntpt));
3872b610f78eSvikram 	if (strcmp(root, "/") != 0) {
3873b610f78eSvikram 		cp = mntpt + strlen(mntpt) - 1;
3874b610f78eSvikram 		if (*cp == '/')
3875b610f78eSvikram 			*cp = '\0';
3876b610f78eSvikram 	}
38777c478bd9Sstevel@tonic-gate 	found = 0;
38787c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
3879b610f78eSvikram 		if (strcmp(mnt.mnt_mountp, mntpt) == 0) {
38807c478bd9Sstevel@tonic-gate 			found = 1;
38817c478bd9Sstevel@tonic-gate 			break;
38827c478bd9Sstevel@tonic-gate 		}
38837c478bd9Sstevel@tonic-gate 	}
38847c478bd9Sstevel@tonic-gate 
38857c478bd9Sstevel@tonic-gate 	if (!found) {
38867c478bd9Sstevel@tonic-gate 		if (bam_verbose)
3887f64ca102SToomas Soome 			bam_error(_("alternate root %s not in mnttab\n"),
3888f64ca102SToomas Soome 			    mntpt);
38897c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
38907c478bd9Sstevel@tonic-gate 		return (0);
38917c478bd9Sstevel@tonic-gate 	}
38927c478bd9Sstevel@tonic-gate 
389308db0dbcSAlexander Eremin 	if (strncmp(mnt.mnt_special, RAMDISK_SPECIAL,
389408db0dbcSAlexander Eremin 	    strlen(RAMDISK_SPECIAL)) == 0) {
38957c478bd9Sstevel@tonic-gate 		if (bam_verbose)
3896f64ca102SToomas Soome 			bam_error(_("%s is on a ramdisk device\n"), bam_root);
38977c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
38987c478bd9Sstevel@tonic-gate 		return (1);
38997c478bd9Sstevel@tonic-gate 	}
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
39027c478bd9Sstevel@tonic-gate 
39037c478bd9Sstevel@tonic-gate 	return (0);
39047c478bd9Sstevel@tonic-gate }
39057c478bd9Sstevel@tonic-gate 
39067c478bd9Sstevel@tonic-gate static int
3907986fd29aSsetje is_boot_archive(char *root)
39087c478bd9Sstevel@tonic-gate {
39097c478bd9Sstevel@tonic-gate 	char		path[PATH_MAX];
39107c478bd9Sstevel@tonic-gate 	struct stat	sb;
3911eb2bd662Svikram 	int		error;
3912eb2bd662Svikram 	const char	*fcn = "is_boot_archive()";
39137c478bd9Sstevel@tonic-gate 
39147c478bd9Sstevel@tonic-gate 	/*
3915986fd29aSsetje 	 * We can't create an archive without the create_ramdisk script
39167c478bd9Sstevel@tonic-gate 	 */
3917986fd29aSsetje 	(void) snprintf(path, sizeof (path), "%s/%s", root, CREATE_RAMDISK);
3918eb2bd662Svikram 	error = stat(path, &sb);
3919eb2bd662Svikram 	INJECT_ERROR1("NOT_ARCHIVE_BASED", error = -1);
3920eb2bd662Svikram 	if (error == -1) {
39217c478bd9Sstevel@tonic-gate 		if (bam_verbose)
3922f64ca102SToomas Soome 			bam_print(_("file not found: %s\n"), path);
3923f64ca102SToomas Soome 		BAM_DPRINTF(("%s: not a boot archive based Solaris "
3924f64ca102SToomas Soome 		    "instance: %s\n", fcn, root));
39257c478bd9Sstevel@tonic-gate 		return (0);
39267c478bd9Sstevel@tonic-gate 	}
39277c478bd9Sstevel@tonic-gate 
3928f64ca102SToomas Soome 	BAM_DPRINTF(("%s: *IS* a boot archive based Solaris instance: %s\n",
3929f64ca102SToomas Soome 	    fcn, root));
3930986fd29aSsetje 	return (1);
3931986fd29aSsetje }
3932986fd29aSsetje 
39337c478bd9Sstevel@tonic-gate /*
3934986fd29aSsetje  * Need to call this for anything that operates on the GRUB menu
3935963390b4Svikram  * In the x86 live upgrade case the directory /boot/grub may be present
3936963390b4Svikram  * even on pre-newboot BEs. The authoritative way to check for a GRUB target
3937963390b4Svikram  * is to check for the presence of the stage2 binary which is present
3938963390b4Svikram  * only on GRUB targets (even on x86 boot partitions). Checking for the
3939963390b4Svikram  * presence of the multiboot binary is not correct as it is not present
3940963390b4Svikram  * on x86 boot partitions.
3941986fd29aSsetje  */
3942986fd29aSsetje int
3943986fd29aSsetje is_grub(const char *root)
3944986fd29aSsetje {
3945986fd29aSsetje 	char path[PATH_MAX];
3946986fd29aSsetje 	struct stat sb;
3947d0911063SToomas Soome 	void *defp;
3948d0911063SToomas Soome 	boolean_t grub = B_FALSE;
3949d0911063SToomas Soome 	const char *res = NULL;
3950eb2bd662Svikram 	const char *fcn = "is_grub()";
3951986fd29aSsetje 
3952d0911063SToomas Soome 	/* grub is disabled by default */
3953d0911063SToomas Soome 	if ((defp = defopen_r(BE_DEFAULTS)) == NULL) {
39547c478bd9Sstevel@tonic-gate 		return (0);
3955d0911063SToomas Soome 	} else {
3956d0911063SToomas Soome 		res = defread_r(BE_DFLT_BE_HAS_GRUB, defp);
3957d0911063SToomas Soome 		if (res != NULL && res[0] != '\0') {
3958d0911063SToomas Soome 			if (strcasecmp(res, "true") == 0)
3959d0911063SToomas Soome 				grub = B_TRUE;
3960d0911063SToomas Soome 		}
3961d0911063SToomas Soome 		defclose_r(defp);
39627c478bd9Sstevel@tonic-gate 	}
39637c478bd9Sstevel@tonic-gate 
3964d0911063SToomas Soome 	if (grub == B_TRUE) {
3965d0911063SToomas Soome 		(void) snprintf(path, sizeof (path), "%s%s", root, GRUB_STAGE2);
3966d0911063SToomas Soome 		if (stat(path, &sb) == -1) {
3967d0911063SToomas Soome 			BAM_DPRINTF(("%s: Missing GRUB directory: %s\n",
3968d0911063SToomas Soome 			    fcn, path));
3969d0911063SToomas Soome 			return (0);
3970d0911063SToomas Soome 		} else
39717c478bd9Sstevel@tonic-gate 			return (1);
39727c478bd9Sstevel@tonic-gate 	}
39737c478bd9Sstevel@tonic-gate 
3974d0911063SToomas Soome 	return (0);
3975d0911063SToomas Soome }
3976d0911063SToomas Soome 
3977f64ca102SToomas Soome int
3978eb2bd662Svikram is_zfs(char *root)
3979eb2bd662Svikram {
3980eb2bd662Svikram 	struct statvfs		vfs;
3981eb2bd662Svikram 	int			ret;
3982eb2bd662Svikram 	const char		*fcn = "is_zfs()";
3983eb2bd662Svikram 
3984eb2bd662Svikram 	ret = statvfs(root, &vfs);
3985eb2bd662Svikram 	INJECT_ERROR1("STATVFS_ZFS", ret = 1);
3986eb2bd662Svikram 	if (ret != 0) {
3987f64ca102SToomas Soome 		bam_error(_("statvfs failed for %s: %s\n"), root,
3988f64ca102SToomas Soome 		    strerror(errno));
3989eb2bd662Svikram 		return (0);
3990eb2bd662Svikram 	}
3991eb2bd662Svikram 
3992eb2bd662Svikram 	if (strncmp(vfs.f_basetype, "zfs", strlen("zfs")) == 0) {
3993f64ca102SToomas Soome 		BAM_DPRINTF(("%s: is a ZFS filesystem: %s\n", fcn, root));
3994eb2bd662Svikram 		return (1);
3995eb2bd662Svikram 	} else {
3996f64ca102SToomas Soome 		BAM_DPRINTF(("%s: is *NOT* a ZFS filesystem: %s\n", fcn, root));
3997eb2bd662Svikram 		return (0);
3998eb2bd662Svikram 	}
3999eb2bd662Svikram }
4000eb2bd662Svikram 
4001f64ca102SToomas Soome int
4002eb2bd662Svikram is_pcfs(char *root)
4003eb2bd662Svikram {
4004eb2bd662Svikram 	struct statvfs		vfs;
4005eb2bd662Svikram 	int			ret;
4006eb2bd662Svikram 	const char		*fcn = "is_pcfs()";
4007eb2bd662Svikram 
4008eb2bd662Svikram 	ret = statvfs(root, &vfs);
4009eb2bd662Svikram 	INJECT_ERROR1("STATVFS_PCFS", ret = 1);
4010eb2bd662Svikram 	if (ret != 0) {
4011f64ca102SToomas Soome 		bam_error(_("statvfs failed for %s: %s\n"), root,
4012f64ca102SToomas Soome 		    strerror(errno));
4013eb2bd662Svikram 		return (0);
4014eb2bd662Svikram 	}
4015eb2bd662Svikram 
4016eb2bd662Svikram 	if (strncmp(vfs.f_basetype, "pcfs", strlen("pcfs")) == 0) {
4017f64ca102SToomas Soome 		BAM_DPRINTF(("%s: is a PCFS filesystem: %s\n", fcn, root));
4018eb2bd662Svikram 		return (1);
4019eb2bd662Svikram 	} else {
4020f64ca102SToomas Soome 		BAM_DPRINTF(("%s: is *NOT* a PCFS filesystem: %s\n",
4021f64ca102SToomas Soome 		    fcn, root));
4022eb2bd662Svikram 		return (0);
4023eb2bd662Svikram 	}
4024eb2bd662Svikram }
4025eb2bd662Svikram 
4026eb2bd662Svikram static int
40277c478bd9Sstevel@tonic-gate is_readonly(char *root)
40287c478bd9Sstevel@tonic-gate {
4029eb2bd662Svikram 	int		fd;
4030eb2bd662Svikram 	int		error;
4031eb2bd662Svikram 	char		testfile[PATH_MAX];
4032eb2bd662Svikram 	const char	*fcn = "is_readonly()";
40337c478bd9Sstevel@tonic-gate 
40347c478bd9Sstevel@tonic-gate 	/*
4035eb2bd662Svikram 	 * Using statvfs() to check for a read-only filesystem is not
4036eb2bd662Svikram 	 * reliable. The only way to reliably test is to attempt to
4037eb2bd662Svikram 	 * create a file
40387c478bd9Sstevel@tonic-gate 	 */
4039eb2bd662Svikram 	(void) snprintf(testfile, sizeof (testfile), "%s/%s.%d",
4040eb2bd662Svikram 	    root, BOOTADM_RDONLY_TEST, getpid());
40417c478bd9Sstevel@tonic-gate 
4042eb2bd662Svikram 	(void) unlink(testfile);
4043eb2bd662Svikram 
4044eb2bd662Svikram 	errno = 0;
4045eb2bd662Svikram 	fd = open(testfile, O_RDWR|O_CREAT|O_EXCL, 0644);
4046eb2bd662Svikram 	error = errno;
4047eb2bd662Svikram 	INJECT_ERROR2("RDONLY_TEST_ERROR", fd = -1, error = EACCES);
4048eb2bd662Svikram 	if (fd == -1 && error == EROFS) {
4049f64ca102SToomas Soome 		BAM_DPRINTF(("%s: is a READONLY filesystem: %s\n", fcn, root));
40507c478bd9Sstevel@tonic-gate 		return (1);
4051eb2bd662Svikram 	} else if (fd == -1) {
4052f64ca102SToomas Soome 		bam_error(_("error during read-only test on %s: %s\n"),
4053f64ca102SToomas Soome 		    root, strerror(error));
40547c478bd9Sstevel@tonic-gate 	}
40557c478bd9Sstevel@tonic-gate 
4056eb2bd662Svikram 	(void) close(fd);
4057eb2bd662Svikram 	(void) unlink(testfile);
40587c478bd9Sstevel@tonic-gate 
4059f64ca102SToomas Soome 	BAM_DPRINTF(("%s: is a RDWR filesystem: %s\n", fcn, root));
4060e7cbe64fSgw25295 	return (0);
4061e7cbe64fSgw25295 }
4062e7cbe64fSgw25295 
40637c478bd9Sstevel@tonic-gate static error_t
40647c478bd9Sstevel@tonic-gate update_archive(char *root, char *opt)
40657c478bd9Sstevel@tonic-gate {
40667c478bd9Sstevel@tonic-gate 	error_t ret;
40677c478bd9Sstevel@tonic-gate 
40687c478bd9Sstevel@tonic-gate 	assert(root);
40697c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
40707c478bd9Sstevel@tonic-gate 
407148847494SEnrico Perla - Sun Microsystems 	init_walk_args();
407248847494SEnrico Perla - Sun Microsystems 	(void) umask(022);
407348847494SEnrico Perla - Sun Microsystems 
40747c478bd9Sstevel@tonic-gate 	/*
40756debd3f5SAlexander Eremin 	 * Never update non-BE root in update_all
40766debd3f5SAlexander Eremin 	 */
40776debd3f5SAlexander Eremin 	if (!is_be(root) && bam_update_all)
40786debd3f5SAlexander Eremin 		return (BAM_SUCCESS);
40796debd3f5SAlexander Eremin 	/*
4080986fd29aSsetje 	 * root must belong to a boot archive based OS,
40817c478bd9Sstevel@tonic-gate 	 */
4082986fd29aSsetje 	if (!is_boot_archive(root)) {
4083b610f78eSvikram 		/*
4084b610f78eSvikram 		 * Emit message only if not in context of update_all.
4085b610f78eSvikram 		 * If in update_all, emit only if verbose flag is set.
4086b610f78eSvikram 		 */
4087b610f78eSvikram 		if (!bam_update_all || bam_verbose)
4088f64ca102SToomas Soome 			bam_print(_("%s: not a boot archive based Solaris "
4089f64ca102SToomas Soome 			    "instance\n"), root);
409048847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
40917c478bd9Sstevel@tonic-gate 	}
40927c478bd9Sstevel@tonic-gate 
40937c478bd9Sstevel@tonic-gate 	/*
40948c1b6884Sszhou 	 * If smf check is requested when / is writable (can happen
40958c1b6884Sszhou 	 * on first reboot following an upgrade because service
40968c1b6884Sszhou 	 * dependency is messed up), skip the check.
40978c1b6884Sszhou 	 */
409848847494SEnrico Perla - Sun Microsystems 	if (bam_smf_check && !bam_root_readonly && !is_zfs(root))
40998c1b6884Sszhou 		return (BAM_SUCCESS);
41008c1b6884Sszhou 
41018c1b6884Sszhou 	/*
41025eea6091SEnrico Perla - Sun Microsystems 	 * Don't generate archive on ramdisk.
41035eea6091SEnrico Perla - Sun Microsystems 	 */
41045eea6091SEnrico Perla - Sun Microsystems 	if (is_ramdisk(root))
41055eea6091SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
41065eea6091SEnrico Perla - Sun Microsystems 
41075eea6091SEnrico Perla - Sun Microsystems 	/*
41088c1b6884Sszhou 	 * root must be writable. This check applies to alternate
41098c1b6884Sszhou 	 * root (-R option); bam_root_readonly applies to '/' only.
411048847494SEnrico Perla - Sun Microsystems 	 * The behaviour translates into being the one of a 'check'.
41117c478bd9Sstevel@tonic-gate 	 */
411261cb17bdSsetje 	if (!bam_smf_check && !bam_check && is_readonly(root)) {
411348847494SEnrico Perla - Sun Microsystems 		set_flag(RDONLY_FSCHK);
411448847494SEnrico Perla - Sun Microsystems 		bam_check = 1;
41157c478bd9Sstevel@tonic-gate 	}
41167c478bd9Sstevel@tonic-gate 
41177c478bd9Sstevel@tonic-gate 	/*
4118cedc7e57SEnrico Perla - Sun Microsystems 	 * Now check if an update is really needed.
41197c478bd9Sstevel@tonic-gate 	 */
41207c478bd9Sstevel@tonic-gate 	ret = update_required(root);
41217c478bd9Sstevel@tonic-gate 
41227c478bd9Sstevel@tonic-gate 	/*
4123cedc7e57SEnrico Perla - Sun Microsystems 	 * The check command (-n) is *not* a dry run.
41247c478bd9Sstevel@tonic-gate 	 * It only checks if the archive is in sync.
4125cedc7e57SEnrico Perla - Sun Microsystems 	 * A readonly filesystem has to be considered an error only if an update
4126cedc7e57SEnrico Perla - Sun Microsystems 	 * is required.
41277c478bd9Sstevel@tonic-gate 	 */
4128cedc7e57SEnrico Perla - Sun Microsystems 	if (bam_nowrite()) {
412948847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(RDONLY_FSCHK)) {
41305eea6091SEnrico Perla - Sun Microsystems 			bam_check = bam_saved_check;
413148847494SEnrico Perla - Sun Microsystems 			if (ret > 0)
4132f64ca102SToomas Soome 				bam_error(_("%s filesystem is read-only, "
4133f64ca102SToomas Soome 				    "skipping archives update\n"), root);
413448847494SEnrico Perla - Sun Microsystems 			if (bam_update_all)
413548847494SEnrico Perla - Sun Microsystems 				return ((ret != 0) ? BAM_ERROR : BAM_SUCCESS);
413648847494SEnrico Perla - Sun Microsystems 		}
413748847494SEnrico Perla - Sun Microsystems 
41387c478bd9Sstevel@tonic-gate 		bam_exit((ret != 0) ? 1 : 0);
41397c478bd9Sstevel@tonic-gate 	}
41407c478bd9Sstevel@tonic-gate 
41417c478bd9Sstevel@tonic-gate 	if (ret == 1) {
41427c478bd9Sstevel@tonic-gate 		/* create the ramdisk */
41437c478bd9Sstevel@tonic-gate 		ret = create_ramdisk(root);
41447c478bd9Sstevel@tonic-gate 	}
4145986fd29aSsetje 
41464a9df875SEnrico Perla - Sun Microsystems 	/*
41474a9df875SEnrico Perla - Sun Microsystems 	 * if the archive is updated, save the new stat data and update the
41484a9df875SEnrico Perla - Sun Microsystems 	 * timestamp file
41494a9df875SEnrico Perla - Sun Microsystems 	 */
4150986fd29aSsetje 	if (ret == 0 && walk_arg.new_nvlp != NULL) {
4151986fd29aSsetje 		savenew(root);
41524a9df875SEnrico Perla - Sun Microsystems 		update_timestamp(root);
4153986fd29aSsetje 	}
4154986fd29aSsetje 
4155986fd29aSsetje 	clear_walk_args();
4156986fd29aSsetje 
41577c478bd9Sstevel@tonic-gate 	return (ret);
41587c478bd9Sstevel@tonic-gate }
41597c478bd9Sstevel@tonic-gate 
416040b108d7SMark J Musante static char *
416140b108d7SMark J Musante find_root_pool()
416240b108d7SMark J Musante {
416340b108d7SMark J Musante 	char *special = get_special("/");
416440b108d7SMark J Musante 	char *p;
416540b108d7SMark J Musante 
416640b108d7SMark J Musante 	if (special == NULL)
416740b108d7SMark J Musante 		return (NULL);
416840b108d7SMark J Musante 
416940b108d7SMark J Musante 	if (*special == '/') {
417040b108d7SMark J Musante 		free(special);
417140b108d7SMark J Musante 		return (NULL);
417240b108d7SMark J Musante 	}
417340b108d7SMark J Musante 
417440b108d7SMark J Musante 	if ((p = strchr(special, '/')) != NULL)
417540b108d7SMark J Musante 		*p = '\0';
417640b108d7SMark J Musante 
417740b108d7SMark J Musante 	return (special);
417840b108d7SMark J Musante }
417940b108d7SMark J Musante 
4180963390b4Svikram static error_t
4181963390b4Svikram synchronize_BE_menu(void)
4182fbac2b2bSvikram {
4183fbac2b2bSvikram 	struct stat	sb;
4184963390b4Svikram 	char		cmdline[PATH_MAX];
4185963390b4Svikram 	char		cksum_line[PATH_MAX];
4186963390b4Svikram 	filelist_t	flist = {0};
4187963390b4Svikram 	char		*old_cksum_str;
4188963390b4Svikram 	char		*old_size_str;
4189963390b4Svikram 	char		*old_file;
4190963390b4Svikram 	char		*curr_cksum_str;
4191963390b4Svikram 	char		*curr_size_str;
4192963390b4Svikram 	char		*curr_file;
41931130146cSGangadhar Mylapuram 	char		*pool = NULL;
41941130146cSGangadhar Mylapuram 	char		*mntpt = NULL;
41951130146cSGangadhar Mylapuram 	zfs_mnted_t	mnted;
4196963390b4Svikram 	FILE		*cfp;
4197963390b4Svikram 	int		found;
4198963390b4Svikram 	int		ret;
4199963390b4Svikram 	const char	*fcn = "synchronize_BE_menu()";
4200fbac2b2bSvikram 
4201f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. No args\n", fcn));
4202fbac2b2bSvikram 
4203963390b4Svikram 	/* Check if findroot enabled LU BE */
4204963390b4Svikram 	if (stat(FINDROOT_INSTALLGRUB, &sb) != 0) {
4205f64ca102SToomas Soome 		BAM_DPRINTF(("%s: not a Live Upgrade BE\n", fcn));
4206963390b4Svikram 		return (BAM_SUCCESS);
4207fbac2b2bSvikram 	}
4208fbac2b2bSvikram 
4209963390b4Svikram 	if (stat(LU_MENU_CKSUM, &sb) != 0) {
4210f64ca102SToomas Soome 		BAM_DPRINTF(("%s: checksum file absent: %s\n",
4211f64ca102SToomas Soome 		    fcn, LU_MENU_CKSUM));
4212963390b4Svikram 		goto menu_sync;
4213fbac2b2bSvikram 	}
4214963390b4Svikram 
4215963390b4Svikram 	cfp = fopen(LU_MENU_CKSUM, "r");
4216963390b4Svikram 	INJECT_ERROR1("CKSUM_FILE_MISSING", cfp = NULL);
4217963390b4Svikram 	if (cfp == NULL) {
4218f64ca102SToomas Soome 		bam_error(_("failed to read GRUB menu checksum file: %s\n"),
4219f64ca102SToomas Soome 		    LU_MENU_CKSUM);
4220963390b4Svikram 		goto menu_sync;
4221963390b4Svikram 	}
4222f64ca102SToomas Soome 	BAM_DPRINTF(("%s: opened checksum file: %s\n", fcn, LU_MENU_CKSUM));
4223963390b4Svikram 
4224963390b4Svikram 	found = 0;
4225963390b4Svikram 	while (s_fgets(cksum_line, sizeof (cksum_line), cfp) != NULL) {
4226963390b4Svikram 		INJECT_ERROR1("MULTIPLE_CKSUM", found = 1);
4227963390b4Svikram 		if (found) {
4228f64ca102SToomas Soome 			bam_error(_("multiple checksums for GRUB menu in "
4229f64ca102SToomas Soome 			    "checksum file: %s\n"), LU_MENU_CKSUM);
4230963390b4Svikram 			(void) fclose(cfp);
4231963390b4Svikram 			goto menu_sync;
4232963390b4Svikram 		}
4233963390b4Svikram 		found = 1;
4234963390b4Svikram 	}
4235f64ca102SToomas Soome 	BAM_DPRINTF(("%s: read checksum file: %s\n", fcn, LU_MENU_CKSUM));
4236963390b4Svikram 
4237963390b4Svikram 
4238963390b4Svikram 	old_cksum_str = strtok(cksum_line, " \t");
4239963390b4Svikram 	old_size_str = strtok(NULL, " \t");
4240963390b4Svikram 	old_file = strtok(NULL, " \t");
4241963390b4Svikram 
4242963390b4Svikram 	INJECT_ERROR1("OLD_CKSUM_NULL", old_cksum_str = NULL);
4243963390b4Svikram 	INJECT_ERROR1("OLD_SIZE_NULL", old_size_str = NULL);
4244963390b4Svikram 	INJECT_ERROR1("OLD_FILE_NULL", old_file = NULL);
4245963390b4Svikram 	if (old_cksum_str == NULL || old_size_str == NULL || old_file == NULL) {
4246f64ca102SToomas Soome 		bam_error(_("error parsing GRUB menu checksum file: %s\n"),
4247f64ca102SToomas Soome 		    LU_MENU_CKSUM);
4248963390b4Svikram 		goto menu_sync;
4249963390b4Svikram 	}
4250f64ca102SToomas Soome 	BAM_DPRINTF(("%s: parsed checksum file: %s\n", fcn, LU_MENU_CKSUM));
4251963390b4Svikram 
4252963390b4Svikram 	/* Get checksum of current menu */
425340b108d7SMark J Musante 	pool = find_root_pool();
42541130146cSGangadhar Mylapuram 	if (pool) {
42551130146cSGangadhar Mylapuram 		mntpt = mount_top_dataset(pool, &mnted);
42561130146cSGangadhar Mylapuram 		if (mntpt == NULL) {
4257f64ca102SToomas Soome 			bam_error(_("failed to mount top dataset for %s\n"),
4258f64ca102SToomas Soome 			    pool);
425940b108d7SMark J Musante 			free(pool);
42601130146cSGangadhar Mylapuram 			return (BAM_ERROR);
42611130146cSGangadhar Mylapuram 		}
42621130146cSGangadhar Mylapuram 		(void) snprintf(cmdline, sizeof (cmdline), "%s %s%s",
42631130146cSGangadhar Mylapuram 		    CKSUM, mntpt, GRUB_MENU);
42641130146cSGangadhar Mylapuram 	} else {
42651130146cSGangadhar Mylapuram 		(void) snprintf(cmdline, sizeof (cmdline), "%s %s",
42661130146cSGangadhar Mylapuram 		    CKSUM, GRUB_MENU);
42671130146cSGangadhar Mylapuram 	}
4268963390b4Svikram 	ret = exec_cmd(cmdline, &flist);
42691130146cSGangadhar Mylapuram 	if (pool) {
42701130146cSGangadhar Mylapuram 		(void) umount_top_dataset(pool, mnted, mntpt);
42711130146cSGangadhar Mylapuram 		free(pool);
42721130146cSGangadhar Mylapuram 	}
4273963390b4Svikram 	INJECT_ERROR1("GET_CURR_CKSUM", ret = 1);
4274963390b4Svikram 	if (ret != 0) {
4275f64ca102SToomas Soome 		bam_error(_("error generating checksum of GRUB menu\n"));
4276963390b4Svikram 		return (BAM_ERROR);
4277963390b4Svikram 	}
4278f64ca102SToomas Soome 	BAM_DPRINTF(("%s: successfully generated checksum\n", fcn));
4279963390b4Svikram 
4280963390b4Svikram 	INJECT_ERROR1("GET_CURR_CKSUM_OUTPUT", flist.head = NULL);
4281963390b4Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
4282f64ca102SToomas Soome 		bam_error(_("bad checksum generated for GRUB menu\n"));
4283963390b4Svikram 		filelist_free(&flist);
4284963390b4Svikram 		return (BAM_ERROR);
4285963390b4Svikram 	}
4286f64ca102SToomas Soome 	BAM_DPRINTF(("%s: generated checksum output valid\n", fcn));
4287963390b4Svikram 
4288963390b4Svikram 	curr_cksum_str = strtok(flist.head->line, " \t");
4289963390b4Svikram 	curr_size_str = strtok(NULL, " \t");
4290963390b4Svikram 	curr_file = strtok(NULL, " \t");
4291963390b4Svikram 
4292963390b4Svikram 	INJECT_ERROR1("CURR_CKSUM_NULL", curr_cksum_str = NULL);
4293963390b4Svikram 	INJECT_ERROR1("CURR_SIZE_NULL", curr_size_str = NULL);
4294963390b4Svikram 	INJECT_ERROR1("CURR_FILE_NULL", curr_file = NULL);
4295963390b4Svikram 	if (curr_cksum_str == NULL || curr_size_str == NULL ||
4296963390b4Svikram 	    curr_file == NULL) {
4297f64ca102SToomas Soome 		bam_error(_("error parsing checksum generated "
4298f64ca102SToomas Soome 		    "for GRUB menu\n"));
4299963390b4Svikram 		filelist_free(&flist);
4300963390b4Svikram 		return (BAM_ERROR);
4301963390b4Svikram 	}
4302f64ca102SToomas Soome 	BAM_DPRINTF(("%s: successfully parsed generated checksum\n", fcn));
4303963390b4Svikram 
4304963390b4Svikram 	if (strcmp(old_cksum_str, curr_cksum_str) == 0 &&
4305963390b4Svikram 	    strcmp(old_size_str, curr_size_str) == 0 &&
4306963390b4Svikram 	    strcmp(old_file, curr_file) == 0) {
4307963390b4Svikram 		filelist_free(&flist);
4308f64ca102SToomas Soome 		BAM_DPRINTF(("%s: no change in checksum of GRUB menu\n", fcn));
4309963390b4Svikram 		return (BAM_SUCCESS);
4310963390b4Svikram 	}
4311963390b4Svikram 
4312963390b4Svikram 	filelist_free(&flist);
4313963390b4Svikram 
4314963390b4Svikram 	/* cksum doesn't match - the menu has changed */
4315f64ca102SToomas Soome 	BAM_DPRINTF(("%s: checksum of GRUB menu has changed\n", fcn));
4316963390b4Svikram 
4317963390b4Svikram menu_sync:
4318f64ca102SToomas Soome 	bam_print(_("propagating updated GRUB menu\n"));
4319963390b4Svikram 
4320963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
4321a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s yes > /dev/null'",
4322963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, GRUB_MENU);
4323963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
4324963390b4Svikram 	INJECT_ERROR1("PROPAGATE_MENU", ret = 1);
4325963390b4Svikram 	if (ret != 0) {
4326f64ca102SToomas Soome 		bam_error(_("error propagating updated GRUB menu\n"));
4327963390b4Svikram 		return (BAM_ERROR);
4328963390b4Svikram 	}
4329f64ca102SToomas Soome 	BAM_DPRINTF(("%s: successfully propagated GRUB menu\n", fcn));
4330963390b4Svikram 
4331a6968364Svikram 	(void) snprintf(cmdline, sizeof (cmdline), "/bin/cp %s %s > /dev/null",
4332963390b4Svikram 	    GRUB_MENU, GRUB_BACKUP_MENU);
4333963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
4334963390b4Svikram 	INJECT_ERROR1("CREATE_BACKUP", ret = 1);
4335963390b4Svikram 	if (ret != 0) {
4336f64ca102SToomas Soome 		bam_error(_("failed to create backup for GRUB menu: %s\n"),
4337f64ca102SToomas Soome 		    GRUB_BACKUP_MENU);
4338963390b4Svikram 		return (BAM_ERROR);
4339963390b4Svikram 	}
4340f64ca102SToomas Soome 	BAM_DPRINTF(("%s: successfully created backup GRUB menu: %s\n",
4341f64ca102SToomas Soome 	    fcn, GRUB_BACKUP_MENU));
4342963390b4Svikram 
4343963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
4344a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s no > /dev/null'",
4345963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, GRUB_BACKUP_MENU);
4346963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
4347963390b4Svikram 	INJECT_ERROR1("PROPAGATE_BACKUP", ret = 1);
4348963390b4Svikram 	if (ret != 0) {
4349f64ca102SToomas Soome 		bam_error(_("error propagating backup GRUB menu: %s\n"),
4350f64ca102SToomas Soome 		    GRUB_BACKUP_MENU);
4351963390b4Svikram 		return (BAM_ERROR);
4352963390b4Svikram 	}
4353f64ca102SToomas Soome 	BAM_DPRINTF(("%s: successfully propagated backup GRUB menu: %s\n",
4354f64ca102SToomas Soome 	    fcn, GRUB_BACKUP_MENU));
4355963390b4Svikram 
4356963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s > %s",
4357963390b4Svikram 	    CKSUM, GRUB_MENU, LU_MENU_CKSUM);
4358963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
4359963390b4Svikram 	INJECT_ERROR1("CREATE_CKSUM_FILE", ret = 1);
4360963390b4Svikram 	if (ret != 0) {
4361f64ca102SToomas Soome 		bam_error(_("failed to write GRUB menu checksum file: %s\n"),
4362f64ca102SToomas Soome 		    LU_MENU_CKSUM);
4363963390b4Svikram 		return (BAM_ERROR);
4364963390b4Svikram 	}
4365f64ca102SToomas Soome 	BAM_DPRINTF(("%s: successfully created checksum file: %s\n",
4366f64ca102SToomas Soome 	    fcn, LU_MENU_CKSUM));
4367963390b4Svikram 
4368963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
4369a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s no > /dev/null'",
4370963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, LU_MENU_CKSUM);
4371963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
4372963390b4Svikram 	INJECT_ERROR1("PROPAGATE_MENU_CKSUM_FILE", ret = 1);
4373963390b4Svikram 	if (ret != 0) {
4374f64ca102SToomas Soome 		bam_error(_("error propagating GRUB menu checksum file: %s\n"),
4375f64ca102SToomas Soome 		    LU_MENU_CKSUM);
4376963390b4Svikram 		return (BAM_ERROR);
4377963390b4Svikram 	}
4378f64ca102SToomas Soome 	BAM_DPRINTF(("%s: successfully propagated checksum file: %s\n",
4379f64ca102SToomas Soome 	    fcn, LU_MENU_CKSUM));
4380963390b4Svikram 
4381963390b4Svikram 	return (BAM_SUCCESS);
4382fbac2b2bSvikram }
4383fbac2b2bSvikram 
43847c478bd9Sstevel@tonic-gate static error_t
43857c478bd9Sstevel@tonic-gate update_all(char *root, char *opt)
43867c478bd9Sstevel@tonic-gate {
43877c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
43887c478bd9Sstevel@tonic-gate 	struct stat sb;
43897c478bd9Sstevel@tonic-gate 	FILE *fp;
43907c478bd9Sstevel@tonic-gate 	char multibt[PATH_MAX];
4391986fd29aSsetje 	char creatram[PATH_MAX];
43927c478bd9Sstevel@tonic-gate 	error_t ret = BAM_SUCCESS;
43937c478bd9Sstevel@tonic-gate 
439440541d5dSvikram 	assert(root);
43957c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
43967c478bd9Sstevel@tonic-gate 
439740541d5dSvikram 	if (bam_rootlen != 1 || *root != '/') {
439840541d5dSvikram 		elide_trailing_slash(root, multibt, sizeof (multibt));
4399f64ca102SToomas Soome 		bam_error(_("an alternate root (%s) cannot be used with this "
4400f64ca102SToomas Soome 		    "sub-command\n"), multibt);
440140541d5dSvikram 		return (BAM_ERROR);
440240541d5dSvikram 	}
440340541d5dSvikram 
44047c478bd9Sstevel@tonic-gate 	/*
44057c478bd9Sstevel@tonic-gate 	 * First update archive for current root
44067c478bd9Sstevel@tonic-gate 	 */
44077c478bd9Sstevel@tonic-gate 	if (update_archive(root, opt) != BAM_SUCCESS)
44087c478bd9Sstevel@tonic-gate 		ret = BAM_ERROR;
440998892a30Snadkarni 
441098892a30Snadkarni 	if (ret == BAM_ERROR)
441198892a30Snadkarni 		goto out;
44127c478bd9Sstevel@tonic-gate 
44137c478bd9Sstevel@tonic-gate 	/*
44147c478bd9Sstevel@tonic-gate 	 * Now walk the mount table, performing archive update
44157c478bd9Sstevel@tonic-gate 	 * for all mounted Newboot root filesystems
44167c478bd9Sstevel@tonic-gate 	 */
44177c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
44187c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
4419f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"),
4420f64ca102SToomas Soome 		    MNTTAB, strerror(errno));
4421b610f78eSvikram 		ret = BAM_ERROR;
4422b610f78eSvikram 		goto out;
44237c478bd9Sstevel@tonic-gate 	}
44247c478bd9Sstevel@tonic-gate 
44257c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
44267c478bd9Sstevel@tonic-gate 
44277c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
44287c478bd9Sstevel@tonic-gate 		if (mnt.mnt_special == NULL)
44297c478bd9Sstevel@tonic-gate 			continue;
4430f904d32dSJerry Gilliam 		if ((strcmp(mnt.mnt_fstype, MNTTYPE_ZFS) != 0) &&
4431f904d32dSJerry Gilliam 		    (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0))
44327c478bd9Sstevel@tonic-gate 			continue;
44337c478bd9Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, "/") == 0)
44347c478bd9Sstevel@tonic-gate 			continue;
44357c478bd9Sstevel@tonic-gate 
4436986fd29aSsetje 		(void) snprintf(creatram, sizeof (creatram), "%s/%s",
4437986fd29aSsetje 		    mnt.mnt_mountp, CREATE_RAMDISK);
44387c478bd9Sstevel@tonic-gate 
4439986fd29aSsetje 		if (stat(creatram, &sb) == -1)
44407c478bd9Sstevel@tonic-gate 			continue;
44417c478bd9Sstevel@tonic-gate 
44427c478bd9Sstevel@tonic-gate 		/*
44437c478bd9Sstevel@tonic-gate 		 * We put a trailing slash to be consistent with root = "/"
44447c478bd9Sstevel@tonic-gate 		 * case, such that we don't have to print // in some cases.
44457c478bd9Sstevel@tonic-gate 		 */
44467c478bd9Sstevel@tonic-gate 		(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
44477c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp);
44487c478bd9Sstevel@tonic-gate 		bam_rootlen = strlen(rootbuf);
4449ae115bc7Smrj 
4450ae115bc7Smrj 		/*
4451ae115bc7Smrj 		 * It's possible that other mounts may be an alternate boot
4452ae115bc7Smrj 		 * architecture, so check it again.
4453ae115bc7Smrj 		 */
4454eb2bd662Svikram 		if ((get_boot_cap(rootbuf) != BAM_SUCCESS) ||
4455ae115bc7Smrj 		    (update_archive(rootbuf, opt) != BAM_SUCCESS))
44567c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
44577c478bd9Sstevel@tonic-gate 	}
44587c478bd9Sstevel@tonic-gate 
44597c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
44607c478bd9Sstevel@tonic-gate 
4461b610f78eSvikram out:
4462fbac2b2bSvikram 	/*
4463963390b4Svikram 	 * We no longer use biosdev for Live Upgrade. Hence
4464963390b4Svikram 	 * there is no need to defer (to shutdown time) any fdisk
4465963390b4Svikram 	 * updates
4466fbac2b2bSvikram 	 */
4467963390b4Svikram 	if (stat(GRUB_fdisk, &sb) == 0 || stat(GRUB_fdisk_target, &sb) == 0) {
4468f64ca102SToomas Soome 		bam_error(_("Deferred FDISK update file(s) found: %s, %s. "
4469f64ca102SToomas Soome 		    "Not supported.\n"), GRUB_fdisk, GRUB_fdisk_target);
4470fbac2b2bSvikram 	}
4471fbac2b2bSvikram 
4472963390b4Svikram 	/*
4473963390b4Svikram 	 * If user has updated menu in current BE, propagate the
4474963390b4Svikram 	 * updates to all BEs.
4475963390b4Svikram 	 */
447619397407SSherry Moore 	if (sync_menu && synchronize_BE_menu() != BAM_SUCCESS)
4477963390b4Svikram 		ret = BAM_ERROR;
4478963390b4Svikram 
44797c478bd9Sstevel@tonic-gate 	return (ret);
44807c478bd9Sstevel@tonic-gate }
44817c478bd9Sstevel@tonic-gate 
44827c478bd9Sstevel@tonic-gate static void
44837c478bd9Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp)
44847c478bd9Sstevel@tonic-gate {
44857c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
44867c478bd9Sstevel@tonic-gate 		mp->start = lp;
44877c478bd9Sstevel@tonic-gate 	} else {
44887c478bd9Sstevel@tonic-gate 		mp->end->next = lp;
44898c1b6884Sszhou 		lp->prev = mp->end;
44907c478bd9Sstevel@tonic-gate 	}
44917c478bd9Sstevel@tonic-gate 	mp->end = lp;
44927c478bd9Sstevel@tonic-gate }
44937c478bd9Sstevel@tonic-gate 
4494eb2bd662Svikram void
44958c1b6884Sszhou unlink_line(menu_t *mp, line_t *lp)
44968c1b6884Sszhou {
44978c1b6884Sszhou 	/* unlink from list */
44988c1b6884Sszhou 	if (lp->prev)
44998c1b6884Sszhou 		lp->prev->next = lp->next;
45008c1b6884Sszhou 	else
45018c1b6884Sszhou 		mp->start = lp->next;
45028c1b6884Sszhou 	if (lp->next)
45038c1b6884Sszhou 		lp->next->prev = lp->prev;
45048c1b6884Sszhou 	else
45058c1b6884Sszhou 		mp->end = lp->prev;
45068c1b6884Sszhou }
45078c1b6884Sszhou 
45088c1b6884Sszhou static entry_t *
45098c1b6884Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end)
45108c1b6884Sszhou {
45118c1b6884Sszhou 	entry_t *ent, *prev;
4512eb2bd662Svikram 	const char *fcn = "boot_entry_new()";
4513eb2bd662Svikram 
4514eb2bd662Svikram 	assert(mp);
4515eb2bd662Svikram 	assert(start);
4516eb2bd662Svikram 	assert(end);
45178c1b6884Sszhou 
45188c1b6884Sszhou 	ent = s_calloc(1, sizeof (entry_t));
4519f64ca102SToomas Soome 	BAM_DPRINTF(("%s: new boot entry alloced\n", fcn));
45208c1b6884Sszhou 	ent->start = start;
45218c1b6884Sszhou 	ent->end = end;
45228c1b6884Sszhou 
45238c1b6884Sszhou 	if (mp->entries == NULL) {
45248c1b6884Sszhou 		mp->entries = ent;
4525f64ca102SToomas Soome 		BAM_DPRINTF(("%s: (first) new boot entry created\n", fcn));
45268c1b6884Sszhou 		return (ent);
45278c1b6884Sszhou 	}
45288c1b6884Sszhou 
45298c1b6884Sszhou 	prev = mp->entries;
45308c1b6884Sszhou 	while (prev->next)
45318c1b6884Sszhou 		prev = prev->next;
45328c1b6884Sszhou 	prev->next = ent;
45338c1b6884Sszhou 	ent->prev = prev;
4534f64ca102SToomas Soome 	BAM_DPRINTF(("%s: new boot entry linked in\n", fcn));
45358c1b6884Sszhou 	return (ent);
45368c1b6884Sszhou }
45378c1b6884Sszhou 
45388c1b6884Sszhou static void
45398c1b6884Sszhou boot_entry_addline(entry_t *ent, line_t *lp)
45408c1b6884Sszhou {
45418c1b6884Sszhou 	if (ent)
45428c1b6884Sszhou 		ent->end = lp;
45438c1b6884Sszhou }
45448c1b6884Sszhou 
45457c478bd9Sstevel@tonic-gate /*
4546843e1988Sjohnlev  * Check whether cmd matches the one indexed by which, and whether arg matches
4547843e1988Sjohnlev  * str.  which must be either KERNEL_CMD or MODULE_CMD, and a match to the
4548843e1988Sjohnlev  * respective *_DOLLAR_CMD is also acceptable.  The arg is searched using
4549843e1988Sjohnlev  * strstr(), so it can be a partial match.
4550843e1988Sjohnlev  */
4551843e1988Sjohnlev static int
4552843e1988Sjohnlev check_cmd(const char *cmd, const int which, const char *arg, const char *str)
4553843e1988Sjohnlev {
4554eb2bd662Svikram 	int			ret;
4555eb2bd662Svikram 	const char		*fcn = "check_cmd()";
4556eb2bd662Svikram 
4557f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, arg, str));
4558eb2bd662Svikram 
4559eac223ccSWilliam Kucharski 	if (cmd != NULL) {
4560843e1988Sjohnlev 		if ((strcmp(cmd, menu_cmds[which]) != 0) &&
4561843e1988Sjohnlev 		    (strcmp(cmd, menu_cmds[which + 1]) != 0)) {
4562f64ca102SToomas Soome 			BAM_DPRINTF(("%s: command %s does not match %s\n",
4563eb2bd662Svikram 			    fcn, cmd, menu_cmds[which]));
4564843e1988Sjohnlev 			return (0);
4565843e1988Sjohnlev 		}
4566eb2bd662Svikram 		ret = (strstr(arg, str) != NULL);
4567eac223ccSWilliam Kucharski 	} else
4568eac223ccSWilliam Kucharski 		ret = 0;
4569eb2bd662Svikram 
4570eb2bd662Svikram 	if (ret) {
4571f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
4572eb2bd662Svikram 	} else {
4573f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
4574eb2bd662Svikram 	}
4575eb2bd662Svikram 
4576eb2bd662Svikram 	return (ret);
4577eb2bd662Svikram }
4578eb2bd662Svikram 
4579eb2bd662Svikram static error_t
4580eb2bd662Svikram kernel_parser(entry_t *entry, char *cmd, char *arg, int linenum)
4581eb2bd662Svikram {
4582eb2bd662Svikram 	const char		*fcn  = "kernel_parser()";
4583eb2bd662Svikram 
4584eb2bd662Svikram 	assert(entry);
4585eb2bd662Svikram 	assert(cmd);
4586eb2bd662Svikram 	assert(arg);
4587eb2bd662Svikram 
4588eb2bd662Svikram 	if (strcmp(cmd, menu_cmds[KERNEL_CMD]) != 0 &&
4589eb2bd662Svikram 	    strcmp(cmd, menu_cmds[KERNEL_DOLLAR_CMD]) != 0) {
4590f64ca102SToomas Soome 		BAM_DPRINTF(("%s: not a kernel command: %s\n", fcn, cmd));
4591eb2bd662Svikram 		return (BAM_ERROR);
4592eb2bd662Svikram 	}
4593eb2bd662Svikram 
4594eb2bd662Svikram 	if (strncmp(arg, DIRECT_BOOT_32, sizeof (DIRECT_BOOT_32) - 1) == 0) {
4595f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting DBOOT|DBOOT_32 flag: %s\n",
4596f64ca102SToomas Soome 		    fcn, arg));
4597eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_32BIT;
4598eb2bd662Svikram 	} else if (strncmp(arg, DIRECT_BOOT_KERNEL,
4599eb2bd662Svikram 	    sizeof (DIRECT_BOOT_KERNEL) - 1) == 0) {
4600f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting DBOOT flag: %s\n", fcn, arg));
4601eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT;
4602eb2bd662Svikram 	} else if (strncmp(arg, DIRECT_BOOT_64,
4603eb2bd662Svikram 	    sizeof (DIRECT_BOOT_64) - 1) == 0) {
4604f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting DBOOT|DBOOT_64 flag: %s\n",
4605f64ca102SToomas Soome 		    fcn, arg));
4606eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_64BIT;
4607eb2bd662Svikram 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_KERNEL,
4608eb2bd662Svikram 	    sizeof (DIRECT_BOOT_FAILSAFE_KERNEL) - 1) == 0) {
4609f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting DBOOT|DBOOT_FAILSAFE flag: %s\n",
4610f64ca102SToomas Soome 		    fcn, arg));
4611eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE;
4612bbcc54bdSEnrico Perla - Sun Microsystems 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_32,
4613bbcc54bdSEnrico Perla - Sun Microsystems 	    sizeof (DIRECT_BOOT_FAILSAFE_32) - 1) == 0) {
4614f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting DBOOT|DBOOT_FAILSAFE|DBOOT_32 "
4615f64ca102SToomas Soome 		    "flag: %s\n", fcn, arg));
4616bbcc54bdSEnrico Perla - Sun Microsystems 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE
4617bbcc54bdSEnrico Perla - Sun Microsystems 		    | BAM_ENTRY_32BIT;
4618bbcc54bdSEnrico Perla - Sun Microsystems 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_64,
4619bbcc54bdSEnrico Perla - Sun Microsystems 	    sizeof (DIRECT_BOOT_FAILSAFE_64) - 1) == 0) {
4620f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting DBOOT|DBOOT_FAILSAFE|DBOOT_64 "
4621f64ca102SToomas Soome 		    "flag: %s\n", fcn, arg));
4622bbcc54bdSEnrico Perla - Sun Microsystems 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE
4623bbcc54bdSEnrico Perla - Sun Microsystems 		    | BAM_ENTRY_64BIT;
4624eb2bd662Svikram 	} else if (strncmp(arg, MULTI_BOOT, sizeof (MULTI_BOOT) - 1) == 0) {
4625f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting MULTIBOOT flag: %s\n", fcn, arg));
4626eb2bd662Svikram 		entry->flags |= BAM_ENTRY_MULTIBOOT;
4627eb2bd662Svikram 	} else if (strncmp(arg, MULTI_BOOT_FAILSAFE,
4628eb2bd662Svikram 	    sizeof (MULTI_BOOT_FAILSAFE) - 1) == 0) {
4629f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting MULTIBOOT|MULTIBOOT_FAILSAFE "
4630f64ca102SToomas Soome 		    "flag: %s\n", fcn, arg));
4631eb2bd662Svikram 		entry->flags |= BAM_ENTRY_MULTIBOOT | BAM_ENTRY_FAILSAFE;
4632eb2bd662Svikram 	} else if (strstr(arg, XEN_KERNEL_SUBSTR)) {
4633f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting XEN HV flag: %s\n", fcn, arg));
4634eb2bd662Svikram 		entry->flags |= BAM_ENTRY_HV;
4635eb2bd662Svikram 	} else if (!(entry->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU))) {
4636f64ca102SToomas Soome 		BAM_DPRINTF(("%s: is HAND kernel flag: %s\n", fcn, arg));
4637eb2bd662Svikram 		return (BAM_ERROR);
463837eb779cSVikram Hegde 	} else if (strncmp(arg, KERNEL_PREFIX, strlen(KERNEL_PREFIX)) == 0 &&
463937eb779cSVikram Hegde 	    strstr(arg, UNIX_SPACE)) {
464037eb779cSVikram Hegde 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_32BIT;
464137eb779cSVikram Hegde 	} else if (strncmp(arg, KERNEL_PREFIX, strlen(KERNEL_PREFIX)) == 0 &&
464237eb779cSVikram Hegde 	    strstr(arg, AMD_UNIX_SPACE)) {
464337eb779cSVikram Hegde 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_64BIT;
4644eb2bd662Svikram 	} else {
4645f64ca102SToomas Soome 		BAM_DPRINTF(("%s: is UNKNOWN kernel entry: %s\n", fcn, arg));
4646f64ca102SToomas Soome 		bam_error(_("kernel command on line %d not recognized.\n"),
4647f64ca102SToomas Soome 		    linenum);
4648eb2bd662Svikram 		return (BAM_ERROR);
4649eb2bd662Svikram 	}
4650eb2bd662Svikram 
4651eb2bd662Svikram 	return (BAM_SUCCESS);
4652eb2bd662Svikram }
4653eb2bd662Svikram 
4654eb2bd662Svikram static error_t
4655eb2bd662Svikram module_parser(entry_t *entry, char *cmd, char *arg, int linenum)
4656eb2bd662Svikram {
4657eb2bd662Svikram 	const char		*fcn = "module_parser()";
4658eb2bd662Svikram 
4659eb2bd662Svikram 	assert(entry);
4660eb2bd662Svikram 	assert(cmd);
4661eb2bd662Svikram 	assert(arg);
4662eb2bd662Svikram 
4663eb2bd662Svikram 	if (strcmp(cmd, menu_cmds[MODULE_CMD]) != 0 &&
4664eb2bd662Svikram 	    strcmp(cmd, menu_cmds[MODULE_DOLLAR_CMD]) != 0) {
4665f64ca102SToomas Soome 		BAM_DPRINTF(("%s: not module cmd: %s\n", fcn, cmd));
4666eb2bd662Svikram 		return (BAM_ERROR);
4667eb2bd662Svikram 	}
4668eb2bd662Svikram 
4669eb2bd662Svikram 	if (strcmp(arg, DIRECT_BOOT_ARCHIVE) == 0 ||
4670eb2bd662Svikram 	    strcmp(arg, DIRECT_BOOT_ARCHIVE_32) == 0 ||
4671eb2bd662Svikram 	    strcmp(arg, DIRECT_BOOT_ARCHIVE_64) == 0 ||
4672eb2bd662Svikram 	    strcmp(arg, MULTIBOOT_ARCHIVE) == 0 ||
4673eb2bd662Svikram 	    strcmp(arg, FAILSAFE_ARCHIVE) == 0 ||
4674bbcc54bdSEnrico Perla - Sun Microsystems 	    strcmp(arg, FAILSAFE_ARCHIVE_32) == 0 ||
4675bbcc54bdSEnrico Perla - Sun Microsystems 	    strcmp(arg, FAILSAFE_ARCHIVE_64) == 0 ||
4676eb2bd662Svikram 	    strcmp(arg, XEN_KERNEL_MODULE_LINE) == 0 ||
4677eb2bd662Svikram 	    strcmp(arg, XEN_KERNEL_MODULE_LINE_ZFS) == 0) {
4678f64ca102SToomas Soome 		BAM_DPRINTF(("%s: bootadm or LU module cmd: %s\n", fcn, arg));
4679eb2bd662Svikram 		return (BAM_SUCCESS);
4680eb2bd662Svikram 	} else if (!(entry->flags & BAM_ENTRY_BOOTADM) &&
4681eb2bd662Svikram 	    !(entry->flags & BAM_ENTRY_LU)) {
4682eb2bd662Svikram 		/* don't emit warning for hand entries */
4683f64ca102SToomas Soome 		BAM_DPRINTF(("%s: is HAND module: %s\n", fcn, arg));
4684eb2bd662Svikram 		return (BAM_ERROR);
4685eb2bd662Svikram 	} else {
4686f64ca102SToomas Soome 		BAM_DPRINTF(("%s: is UNKNOWN module: %s\n", fcn, arg));
4687f64ca102SToomas Soome 		bam_error(_("module command on line %d not recognized.\n"),
4688f64ca102SToomas Soome 		    linenum);
4689eb2bd662Svikram 		return (BAM_ERROR);
4690eb2bd662Svikram 	}
4691843e1988Sjohnlev }
4692843e1988Sjohnlev 
4693843e1988Sjohnlev /*
46947c478bd9Sstevel@tonic-gate  * A line in menu.lst looks like
46957c478bd9Sstevel@tonic-gate  * [ ]*<cmd>[ \t=]*<arg>*
46967c478bd9Sstevel@tonic-gate  */
46977c478bd9Sstevel@tonic-gate static void
46987c478bd9Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum)
46997c478bd9Sstevel@tonic-gate {
47007c478bd9Sstevel@tonic-gate 	/*
47017c478bd9Sstevel@tonic-gate 	 * save state across calls. This is so that
47027c478bd9Sstevel@tonic-gate 	 * header gets the right entry# after title has
47037c478bd9Sstevel@tonic-gate 	 * been processed
47047c478bd9Sstevel@tonic-gate 	 */
47058c1b6884Sszhou 	static line_t *prev = NULL;
47068c1b6884Sszhou 	static entry_t *curr_ent = NULL;
4707ae115bc7Smrj 	static int in_liveupgrade = 0;
4708772d6a58SWilliam Kucharski 	static int is_libbe_ent = 0;
47097c478bd9Sstevel@tonic-gate 
47107c478bd9Sstevel@tonic-gate 	line_t	*lp;
47117c478bd9Sstevel@tonic-gate 	char *cmd, *sep, *arg;
47127c478bd9Sstevel@tonic-gate 	char save, *cp, *line;
47137c478bd9Sstevel@tonic-gate 	menu_flag_t flag = BAM_INVALID;
4714eb2bd662Svikram 	const char *fcn = "line_parser()";
47157c478bd9Sstevel@tonic-gate 
4716110570ceSToomas Soome 	cmd = NULL;
47177c478bd9Sstevel@tonic-gate 	if (str == NULL) {
47187c478bd9Sstevel@tonic-gate 		return;
47197c478bd9Sstevel@tonic-gate 	}
47207c478bd9Sstevel@tonic-gate 
47217c478bd9Sstevel@tonic-gate 	/*
47227c478bd9Sstevel@tonic-gate 	 * First save a copy of the entire line.
47237c478bd9Sstevel@tonic-gate 	 * We use this later to set the line field.
47247c478bd9Sstevel@tonic-gate 	 */
47257c478bd9Sstevel@tonic-gate 	line = s_strdup(str);
47267c478bd9Sstevel@tonic-gate 
47277c478bd9Sstevel@tonic-gate 	/* Eat up leading whitespace */
47287c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
47297c478bd9Sstevel@tonic-gate 		str++;
47307c478bd9Sstevel@tonic-gate 
47317c478bd9Sstevel@tonic-gate 	if (*str == '#') {		/* comment */
47327c478bd9Sstevel@tonic-gate 		cmd = s_strdup("#");
47337c478bd9Sstevel@tonic-gate 		sep = NULL;
47347c478bd9Sstevel@tonic-gate 		arg = s_strdup(str + 1);
47357c478bd9Sstevel@tonic-gate 		flag = BAM_COMMENT;
4736ae115bc7Smrj 		if (strstr(arg, BAM_LU_HDR) != NULL) {
4737ae115bc7Smrj 			in_liveupgrade = 1;
4738ae115bc7Smrj 		} else if (strstr(arg, BAM_LU_FTR) != NULL) {
4739ae115bc7Smrj 			in_liveupgrade = 0;
4740772d6a58SWilliam Kucharski 		} else if (strstr(arg, BAM_LIBBE_FTR) != NULL) {
4741772d6a58SWilliam Kucharski 			is_libbe_ent = 1;
4742ae115bc7Smrj 		}
47437c478bd9Sstevel@tonic-gate 	} else if (*str == '\0') {	/* blank line */
47447c478bd9Sstevel@tonic-gate 		cmd = sep = arg = NULL;
47457c478bd9Sstevel@tonic-gate 		flag = BAM_EMPTY;
47467c478bd9Sstevel@tonic-gate 	} else {
47477c478bd9Sstevel@tonic-gate 		/*
47487c478bd9Sstevel@tonic-gate 		 * '=' is not a documented separator in grub syntax.
47497c478bd9Sstevel@tonic-gate 		 * However various development bits use '=' as a
47507c478bd9Sstevel@tonic-gate 		 * separator. In addition, external users also
47517c478bd9Sstevel@tonic-gate 		 * use = as a separator. So we will allow that usage.
47527c478bd9Sstevel@tonic-gate 		 */
47537c478bd9Sstevel@tonic-gate 		cp = str;
47547c478bd9Sstevel@tonic-gate 		while (*str != ' ' && *str != '\t' && *str != '=') {
47557c478bd9Sstevel@tonic-gate 			if (*str == '\0') {
47567c478bd9Sstevel@tonic-gate 				cmd = s_strdup(cp);
47577c478bd9Sstevel@tonic-gate 				sep = arg = NULL;
47587c478bd9Sstevel@tonic-gate 				break;
47597c478bd9Sstevel@tonic-gate 			}
47607c478bd9Sstevel@tonic-gate 			str++;
47617c478bd9Sstevel@tonic-gate 		}
47627c478bd9Sstevel@tonic-gate 
47637c478bd9Sstevel@tonic-gate 		if (*str != '\0') {
47647c478bd9Sstevel@tonic-gate 			save = *str;
47657c478bd9Sstevel@tonic-gate 			*str = '\0';
47667c478bd9Sstevel@tonic-gate 			cmd = s_strdup(cp);
47677c478bd9Sstevel@tonic-gate 			*str = save;
47687c478bd9Sstevel@tonic-gate 
47697c478bd9Sstevel@tonic-gate 			str++;
47707c478bd9Sstevel@tonic-gate 			save = *str;
47717c478bd9Sstevel@tonic-gate 			*str = '\0';
47727c478bd9Sstevel@tonic-gate 			sep = s_strdup(str - 1);
47737c478bd9Sstevel@tonic-gate 			*str = save;
47747c478bd9Sstevel@tonic-gate 
47757c478bd9Sstevel@tonic-gate 			while (*str == ' ' || *str == '\t')
47767c478bd9Sstevel@tonic-gate 				str++;
47777c478bd9Sstevel@tonic-gate 			if (*str == '\0')
47787c478bd9Sstevel@tonic-gate 				arg = NULL;
47797c478bd9Sstevel@tonic-gate 			else
47807c478bd9Sstevel@tonic-gate 				arg = s_strdup(str);
47817c478bd9Sstevel@tonic-gate 		}
47827c478bd9Sstevel@tonic-gate 	}
47837c478bd9Sstevel@tonic-gate 
47847c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
47857c478bd9Sstevel@tonic-gate 
47867c478bd9Sstevel@tonic-gate 	lp->cmd = cmd;
47877c478bd9Sstevel@tonic-gate 	lp->sep = sep;
47887c478bd9Sstevel@tonic-gate 	lp->arg = arg;
47897c478bd9Sstevel@tonic-gate 	lp->line = line;
47907c478bd9Sstevel@tonic-gate 	lp->lineNum = ++(*lineNum);
47917c478bd9Sstevel@tonic-gate 	if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) {
47927c478bd9Sstevel@tonic-gate 		lp->entryNum = ++(*entryNum);
47937c478bd9Sstevel@tonic-gate 		lp->flags = BAM_TITLE;
47947c478bd9Sstevel@tonic-gate 		if (prev && prev->flags == BAM_COMMENT &&
4795ae115bc7Smrj 		    prev->arg && strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
47967c478bd9Sstevel@tonic-gate 			prev->entryNum = lp->entryNum;
47978c1b6884Sszhou 			curr_ent = boot_entry_new(mp, prev, lp);
4798eb2bd662Svikram 			curr_ent->flags |= BAM_ENTRY_BOOTADM;
4799f64ca102SToomas Soome 			BAM_DPRINTF(("%s: is bootadm(1M) entry: %s\n",
4800f64ca102SToomas Soome 			    fcn, arg));
48018c1b6884Sszhou 		} else {
48028c1b6884Sszhou 			curr_ent = boot_entry_new(mp, lp, lp);
4803ae115bc7Smrj 			if (in_liveupgrade) {
4804eb2bd662Svikram 				curr_ent->flags |= BAM_ENTRY_LU;
4805f64ca102SToomas Soome 				BAM_DPRINTF(("%s: is LU entry: %s\n",
4806f64ca102SToomas Soome 				    fcn, arg));
48078c1b6884Sszhou 			}
4808ae115bc7Smrj 		}
4809ae115bc7Smrj 		curr_ent->entryNum = *entryNum;
48107c478bd9Sstevel@tonic-gate 	} else if (flag != BAM_INVALID) {
48117c478bd9Sstevel@tonic-gate 		/*
48127c478bd9Sstevel@tonic-gate 		 * For header comments, the entry# is "fixed up"
48137c478bd9Sstevel@tonic-gate 		 * by the subsequent title
48147c478bd9Sstevel@tonic-gate 		 */
48157c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
48167c478bd9Sstevel@tonic-gate 		lp->flags = flag;
48177c478bd9Sstevel@tonic-gate 	} else {
48187c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
4819ae115bc7Smrj 
4820ae115bc7Smrj 		if (*entryNum == ENTRY_INIT) {
4821ae115bc7Smrj 			lp->flags = BAM_GLOBAL;
4822ae115bc7Smrj 		} else {
4823ae115bc7Smrj 			lp->flags = BAM_ENTRY;
4824ae115bc7Smrj 
4825ae115bc7Smrj 			if (cmd && arg) {
4826eb2bd662Svikram 				if (strcmp(cmd, menu_cmds[ROOT_CMD]) == 0) {
4827f64ca102SToomas Soome 					BAM_DPRINTF(("%s: setting ROOT: %s\n",
4828f64ca102SToomas Soome 					    fcn, arg));
4829ae115bc7Smrj 					curr_ent->flags |= BAM_ENTRY_ROOT;
4830eb2bd662Svikram 				} else if (strcmp(cmd, menu_cmds[FINDROOT_CMD])
4831eb2bd662Svikram 				    == 0) {
4832f64ca102SToomas Soome 					BAM_DPRINTF(("%s: setting "
4833f64ca102SToomas Soome 					    "FINDROOT: %s\n", fcn, arg));
4834eb2bd662Svikram 					curr_ent->flags |= BAM_ENTRY_FINDROOT;
4835eb2bd662Svikram 				} else if (strcmp(cmd,
4836eb2bd662Svikram 				    menu_cmds[CHAINLOADER_CMD]) == 0) {
4837f64ca102SToomas Soome 					BAM_DPRINTF(("%s: setting "
4838f64ca102SToomas Soome 					    "CHAINLOADER: %s\n", fcn, arg));
4839ae115bc7Smrj 					curr_ent->flags |=
4840ae115bc7Smrj 					    BAM_ENTRY_CHAINLOADER;
4841eb2bd662Svikram 				} else if (kernel_parser(curr_ent, cmd, arg,
4842eb2bd662Svikram 				    lp->lineNum) != BAM_SUCCESS) {
4843eb2bd662Svikram 					(void) module_parser(curr_ent, cmd,
4844eb2bd662Svikram 					    arg, lp->lineNum);
4845eb2bd662Svikram 				}
4846ae115bc7Smrj 			}
4847ae115bc7Smrj 		}
48487c478bd9Sstevel@tonic-gate 	}
48497c478bd9Sstevel@tonic-gate 
48508c1b6884Sszhou 	/* record default, old default, and entry line ranges */
4851eac223ccSWilliam Kucharski 	if (lp->flags == BAM_GLOBAL && lp->cmd != NULL &&
48528c1b6884Sszhou 	    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) {
48538c1b6884Sszhou 		mp->curdefault = lp;
48548c1b6884Sszhou 	} else if (lp->flags == BAM_COMMENT &&
48558c1b6884Sszhou 	    strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) {
48568c1b6884Sszhou 		mp->olddefault = lp;
4857ae115bc7Smrj 	} else if (lp->flags == BAM_COMMENT &&
4858ae115bc7Smrj 	    strncmp(lp->arg, BAM_OLD_RC_DEF, strlen(BAM_OLD_RC_DEF)) == 0) {
4859ae115bc7Smrj 		mp->old_rc_default = lp;
48608c1b6884Sszhou 	} else if (lp->flags == BAM_ENTRY ||
4861ae115bc7Smrj 	    (lp->flags == BAM_COMMENT &&
4862772d6a58SWilliam Kucharski 	    ((strcmp(lp->arg, BAM_BOOTADM_FTR) == 0) || is_libbe_ent))) {
4863772d6a58SWilliam Kucharski 		if (is_libbe_ent) {
4864772d6a58SWilliam Kucharski 			curr_ent->flags |= BAM_ENTRY_LIBBE;
4865772d6a58SWilliam Kucharski 			is_libbe_ent = 0;
4866772d6a58SWilliam Kucharski 		}
4867772d6a58SWilliam Kucharski 
48688c1b6884Sszhou 		boot_entry_addline(curr_ent, lp);
48698c1b6884Sszhou 	}
48707c478bd9Sstevel@tonic-gate 	append_line(mp, lp);
48717c478bd9Sstevel@tonic-gate 
48727c478bd9Sstevel@tonic-gate 	prev = lp;
48737c478bd9Sstevel@tonic-gate }
48747c478bd9Sstevel@tonic-gate 
4875eb2bd662Svikram void
487640541d5dSvikram update_numbering(menu_t *mp)
487740541d5dSvikram {
487840541d5dSvikram 	int lineNum;
487940541d5dSvikram 	int entryNum;
488040541d5dSvikram 	int old_default_value;
488140541d5dSvikram 	line_t *lp, *prev, *default_lp, *default_entry;
488240541d5dSvikram 	char buf[PATH_MAX];
488340541d5dSvikram 
488440541d5dSvikram 	if (mp->start == NULL) {
488540541d5dSvikram 		return;
488640541d5dSvikram 	}
488740541d5dSvikram 
488840541d5dSvikram 	lineNum = LINE_INIT;
488940541d5dSvikram 	entryNum = ENTRY_INIT;
489040541d5dSvikram 	old_default_value = ENTRY_INIT;
489140541d5dSvikram 	lp = default_lp = default_entry = NULL;
489240541d5dSvikram 
489340541d5dSvikram 	prev = NULL;
489440541d5dSvikram 	for (lp = mp->start; lp; prev = lp, lp = lp->next) {
489540541d5dSvikram 		lp->lineNum = ++lineNum;
489640541d5dSvikram 
489740541d5dSvikram 		/*
489840541d5dSvikram 		 * Get the value of the default command
489940541d5dSvikram 		 */
4900eac223ccSWilliam Kucharski 		if (lp->entryNum == ENTRY_INIT && lp->cmd != NULL &&
490140541d5dSvikram 		    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 &&
490240541d5dSvikram 		    lp->arg) {
490340541d5dSvikram 			old_default_value = atoi(lp->arg);
490440541d5dSvikram 			default_lp = lp;
490540541d5dSvikram 		}
490640541d5dSvikram 
490740541d5dSvikram 		/*
4908eb2bd662Svikram 		 * If not a booting entry, nothing else to fix for this
490940541d5dSvikram 		 * entry
491040541d5dSvikram 		 */
491140541d5dSvikram 		if (lp->entryNum == ENTRY_INIT)
491240541d5dSvikram 			continue;
491340541d5dSvikram 
491440541d5dSvikram 		/*
491540541d5dSvikram 		 * Record the position of the default entry.
491640541d5dSvikram 		 * The following works because global
491740541d5dSvikram 		 * commands like default and timeout should precede
491840541d5dSvikram 		 * actual boot entries, so old_default_value
491940541d5dSvikram 		 * is already known (or default cmd is missing).
492040541d5dSvikram 		 */
492140541d5dSvikram 		if (default_entry == NULL &&
492240541d5dSvikram 		    old_default_value != ENTRY_INIT &&
492340541d5dSvikram 		    lp->entryNum == old_default_value) {
492440541d5dSvikram 			default_entry = lp;
492540541d5dSvikram 		}
492640541d5dSvikram 
492740541d5dSvikram 		/*
492840541d5dSvikram 		 * Now fixup the entry number
492940541d5dSvikram 		 */
4930eac223ccSWilliam Kucharski 		if (lp->cmd != NULL &&
4931eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) {
493240541d5dSvikram 			lp->entryNum = ++entryNum;
493340541d5dSvikram 			/* fixup the bootadm header */
493440541d5dSvikram 			if (prev && prev->flags == BAM_COMMENT &&
4935ae115bc7Smrj 			    prev->arg &&
4936ae115bc7Smrj 			    strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
493740541d5dSvikram 				prev->entryNum = lp->entryNum;
493840541d5dSvikram 			}
493940541d5dSvikram 		} else {
494040541d5dSvikram 			lp->entryNum = entryNum;
494140541d5dSvikram 		}
494240541d5dSvikram 	}
494340541d5dSvikram 
494440541d5dSvikram 	/*
494540541d5dSvikram 	 * No default command in menu, simply return
494640541d5dSvikram 	 */
494740541d5dSvikram 	if (default_lp == NULL) {
494840541d5dSvikram 		return;
494940541d5dSvikram 	}
495040541d5dSvikram 
495140541d5dSvikram 	free(default_lp->arg);
495240541d5dSvikram 	free(default_lp->line);
495340541d5dSvikram 
495440541d5dSvikram 	if (default_entry == NULL) {
495540541d5dSvikram 		default_lp->arg = s_strdup("0");
495640541d5dSvikram 	} else {
495740541d5dSvikram 		(void) snprintf(buf, sizeof (buf), "%d",
495840541d5dSvikram 		    default_entry->entryNum);
495940541d5dSvikram 		default_lp->arg = s_strdup(buf);
496040541d5dSvikram 	}
496140541d5dSvikram 
496240541d5dSvikram 	/*
496340541d5dSvikram 	 * The following is required since only the line field gets
496440541d5dSvikram 	 * written back to menu.lst
496540541d5dSvikram 	 */
496640541d5dSvikram 	(void) snprintf(buf, sizeof (buf), "%s%s%s",
496740541d5dSvikram 	    menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg);
496840541d5dSvikram 	default_lp->line = s_strdup(buf);
496940541d5dSvikram }
497040541d5dSvikram 
497140541d5dSvikram 
49727c478bd9Sstevel@tonic-gate static menu_t *
49737c478bd9Sstevel@tonic-gate menu_read(char *menu_path)
49747c478bd9Sstevel@tonic-gate {
49757c478bd9Sstevel@tonic-gate 	FILE *fp;
49767c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE], *cp;
49777c478bd9Sstevel@tonic-gate 	menu_t *mp;
49787c478bd9Sstevel@tonic-gate 	int line, entry, len, n;
49797c478bd9Sstevel@tonic-gate 
49807c478bd9Sstevel@tonic-gate 	mp = s_calloc(1, sizeof (menu_t));
49817c478bd9Sstevel@tonic-gate 
49827c478bd9Sstevel@tonic-gate 	fp = fopen(menu_path, "r");
49837c478bd9Sstevel@tonic-gate 	if (fp == NULL) { /* Let the caller handle this error */
498444da779fSWilliam Kucharski 		free(mp);
498544da779fSWilliam Kucharski 		return (NULL);
49867c478bd9Sstevel@tonic-gate 	}
49877c478bd9Sstevel@tonic-gate 
49887c478bd9Sstevel@tonic-gate 	/* Note: GRUB boot entry number starts with 0 */
49897c478bd9Sstevel@tonic-gate 	line = LINE_INIT;
49907c478bd9Sstevel@tonic-gate 	entry = ENTRY_INIT;
49917c478bd9Sstevel@tonic-gate 	cp = buf;
49927c478bd9Sstevel@tonic-gate 	len = sizeof (buf);
49937c478bd9Sstevel@tonic-gate 	while (s_fgets(cp, len, fp) != NULL) {
49947c478bd9Sstevel@tonic-gate 		n = strlen(cp);
49957c478bd9Sstevel@tonic-gate 		if (cp[n - 1] == '\\') {
49967c478bd9Sstevel@tonic-gate 			len -= n - 1;
49977c478bd9Sstevel@tonic-gate 			assert(len >= 2);
49987c478bd9Sstevel@tonic-gate 			cp += n - 1;
49997c478bd9Sstevel@tonic-gate 			continue;
50007c478bd9Sstevel@tonic-gate 		}
50017c478bd9Sstevel@tonic-gate 		line_parser(mp, buf, &line, &entry);
50027c478bd9Sstevel@tonic-gate 		cp = buf;
50037c478bd9Sstevel@tonic-gate 		len = sizeof (buf);
50047c478bd9Sstevel@tonic-gate 	}
50057c478bd9Sstevel@tonic-gate 
50067c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
5007f64ca102SToomas Soome 		bam_error(_("failed to close file: %s: %s\n"), menu_path,
5008f64ca102SToomas Soome 		    strerror(errno));
50097c478bd9Sstevel@tonic-gate 	}
50107c478bd9Sstevel@tonic-gate 
50117c478bd9Sstevel@tonic-gate 	return (mp);
50127c478bd9Sstevel@tonic-gate }
50137c478bd9Sstevel@tonic-gate 
50147c478bd9Sstevel@tonic-gate static error_t
50157c478bd9Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title)
50167c478bd9Sstevel@tonic-gate {
50177c478bd9Sstevel@tonic-gate 	char *eq;
50187c478bd9Sstevel@tonic-gate 	char *opt_dup;
50197c478bd9Sstevel@tonic-gate 	int entryNum;
50207c478bd9Sstevel@tonic-gate 
50217c478bd9Sstevel@tonic-gate 	assert(mp);
50227c478bd9Sstevel@tonic-gate 	assert(mp->start);
50237c478bd9Sstevel@tonic-gate 	assert(opt);
50247c478bd9Sstevel@tonic-gate 
50257c478bd9Sstevel@tonic-gate 	opt_dup = s_strdup(opt);
50267c478bd9Sstevel@tonic-gate 
50277c478bd9Sstevel@tonic-gate 	if (entry)
50287c478bd9Sstevel@tonic-gate 		*entry = ENTRY_INIT;
50297c478bd9Sstevel@tonic-gate 	if (title)
50307c478bd9Sstevel@tonic-gate 		*title = NULL;
50317c478bd9Sstevel@tonic-gate 
50327c478bd9Sstevel@tonic-gate 	eq = strchr(opt_dup, '=');
50337c478bd9Sstevel@tonic-gate 	if (eq == NULL) {
5034f64ca102SToomas Soome 		bam_error(_("invalid option: %s\n"), opt);
50357c478bd9Sstevel@tonic-gate 		free(opt_dup);
50367c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
50377c478bd9Sstevel@tonic-gate 	}
50387c478bd9Sstevel@tonic-gate 
50397c478bd9Sstevel@tonic-gate 	*eq = '\0';
50407c478bd9Sstevel@tonic-gate 	if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) {
50417c478bd9Sstevel@tonic-gate 		assert(mp->end);
50427c478bd9Sstevel@tonic-gate 		entryNum = s_strtol(eq + 1);
50437c478bd9Sstevel@tonic-gate 		if (entryNum < 0 || entryNum > mp->end->entryNum) {
5044f64ca102SToomas Soome 			bam_error(_("invalid boot entry number: %s\n"), eq + 1);
50457c478bd9Sstevel@tonic-gate 			free(opt_dup);
50467c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
50477c478bd9Sstevel@tonic-gate 		}
50487c478bd9Sstevel@tonic-gate 		*entry = entryNum;
50497c478bd9Sstevel@tonic-gate 	} else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) {
50507c478bd9Sstevel@tonic-gate 		*title = opt + (eq - opt_dup) + 1;
50517c478bd9Sstevel@tonic-gate 	} else {
5052f64ca102SToomas Soome 		bam_error(_("invalid option: %s\n"), opt);
50537c478bd9Sstevel@tonic-gate 		free(opt_dup);
50547c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
50557c478bd9Sstevel@tonic-gate 	}
50567c478bd9Sstevel@tonic-gate 
50577c478bd9Sstevel@tonic-gate 	free(opt_dup);
50587c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
50597c478bd9Sstevel@tonic-gate }
50607c478bd9Sstevel@tonic-gate 
50617c478bd9Sstevel@tonic-gate /*
50627c478bd9Sstevel@tonic-gate  * If invoked with no titles/entries (opt == NULL)
50637c478bd9Sstevel@tonic-gate  * only title lines in file are printed.
50647c478bd9Sstevel@tonic-gate  *
50657c478bd9Sstevel@tonic-gate  * If invoked with a title or entry #, all
50667c478bd9Sstevel@tonic-gate  * lines in *every* matching entry are listed
50677c478bd9Sstevel@tonic-gate  */
50687c478bd9Sstevel@tonic-gate static error_t
50697c478bd9Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt)
50707c478bd9Sstevel@tonic-gate {
50717c478bd9Sstevel@tonic-gate 	line_t *lp;
50727c478bd9Sstevel@tonic-gate 	int entry = ENTRY_INIT;
50737c478bd9Sstevel@tonic-gate 	int found;
50747c478bd9Sstevel@tonic-gate 	char *title = NULL;
50757c478bd9Sstevel@tonic-gate 
50767c478bd9Sstevel@tonic-gate 	assert(mp);
50777c478bd9Sstevel@tonic-gate 	assert(menu_path);
50787c478bd9Sstevel@tonic-gate 
5079eb2bd662Svikram 	/* opt is optional */
5080f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", "list_entry", menu_path,
5081eb2bd662Svikram 	    opt ? opt : "<NULL>"));
5082eb2bd662Svikram 
50837c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
5084f64ca102SToomas Soome 		bam_error(_("menu file not found: %s\n"), menu_path);
50857c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
50867c478bd9Sstevel@tonic-gate 	}
50877c478bd9Sstevel@tonic-gate 
50887c478bd9Sstevel@tonic-gate 	if (opt != NULL) {
50897c478bd9Sstevel@tonic-gate 		if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
50907c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
50917c478bd9Sstevel@tonic-gate 		}
50927c478bd9Sstevel@tonic-gate 		assert((entry != ENTRY_INIT) ^ (title != NULL));
50937c478bd9Sstevel@tonic-gate 	} else {
50947c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0);
50957c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0);
50967c478bd9Sstevel@tonic-gate 	}
50977c478bd9Sstevel@tonic-gate 
50987c478bd9Sstevel@tonic-gate 	found = 0;
50997c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
51007c478bd9Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY)
51017c478bd9Sstevel@tonic-gate 			continue;
51027c478bd9Sstevel@tonic-gate 		if (opt == NULL && lp->flags == BAM_TITLE) {
5103f64ca102SToomas Soome 			bam_print(_("%d %s\n"), lp->entryNum,
51047c478bd9Sstevel@tonic-gate 			    lp->arg);
51057c478bd9Sstevel@tonic-gate 			found = 1;
51067c478bd9Sstevel@tonic-gate 			continue;
51077c478bd9Sstevel@tonic-gate 		}
51087c478bd9Sstevel@tonic-gate 		if (entry != ENTRY_INIT && lp->entryNum == entry) {
5109f64ca102SToomas Soome 			bam_print(_("%s\n"), lp->line);
51107c478bd9Sstevel@tonic-gate 			found = 1;
51117c478bd9Sstevel@tonic-gate 			continue;
51127c478bd9Sstevel@tonic-gate 		}
51137c478bd9Sstevel@tonic-gate 
51147c478bd9Sstevel@tonic-gate 		/*
51157c478bd9Sstevel@tonic-gate 		 * We set the entry value here so that all lines
51167c478bd9Sstevel@tonic-gate 		 * in entry get printed. If we subsequently match
51177c478bd9Sstevel@tonic-gate 		 * title in other entries, all lines in those
51187c478bd9Sstevel@tonic-gate 		 * entries get printed as well.
51197c478bd9Sstevel@tonic-gate 		 */
51207c478bd9Sstevel@tonic-gate 		if (title && lp->flags == BAM_TITLE && lp->arg &&
51217c478bd9Sstevel@tonic-gate 		    strncmp(title, lp->arg, strlen(title)) == 0) {
5122f64ca102SToomas Soome 			bam_print(_("%s\n"), lp->line);
51237c478bd9Sstevel@tonic-gate 			entry = lp->entryNum;
51247c478bd9Sstevel@tonic-gate 			found = 1;
51257c478bd9Sstevel@tonic-gate 			continue;
51267c478bd9Sstevel@tonic-gate 		}
51277c478bd9Sstevel@tonic-gate 	}
51287c478bd9Sstevel@tonic-gate 
51297c478bd9Sstevel@tonic-gate 	if (!found) {
5130f64ca102SToomas Soome 		bam_error(_("no matching entry found\n"));
51317c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
51327c478bd9Sstevel@tonic-gate 	}
51337c478bd9Sstevel@tonic-gate 
51347c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
51357c478bd9Sstevel@tonic-gate }
51367c478bd9Sstevel@tonic-gate 
5137843e1988Sjohnlev int
51387c478bd9Sstevel@tonic-gate add_boot_entry(menu_t *mp,
51397c478bd9Sstevel@tonic-gate     char *title,
5140eb2bd662Svikram     char *findroot,
51417c478bd9Sstevel@tonic-gate     char *kernel,
5142843e1988Sjohnlev     char *mod_kernel,
514344da779fSWilliam Kucharski     char *module,
514444da779fSWilliam Kucharski     char *bootfs)
51457c478bd9Sstevel@tonic-gate {
5146eb2bd662Svikram 	int		lineNum;
5147eb2bd662Svikram 	int		entryNum;
51487c478bd9Sstevel@tonic-gate 	char		linebuf[BAM_MAXLINE];
5149eb2bd662Svikram 	menu_cmd_t	k_cmd;
5150eb2bd662Svikram 	menu_cmd_t	m_cmd;
5151eb2bd662Svikram 	const char	*fcn = "add_boot_entry()";
51527c478bd9Sstevel@tonic-gate 
51537c478bd9Sstevel@tonic-gate 	assert(mp);
51547c478bd9Sstevel@tonic-gate 
5155eb2bd662Svikram 	INJECT_ERROR1("ADD_BOOT_ENTRY_FINDROOT_NULL", findroot = NULL);
5156eb2bd662Svikram 	if (findroot == NULL) {
5157f64ca102SToomas Soome 		bam_error(_("can't find argument for findroot command\n"));
5158eb2bd662Svikram 		return (BAM_ERROR);
5159eb2bd662Svikram 	}
5160eb2bd662Svikram 
51617c478bd9Sstevel@tonic-gate 	if (title == NULL) {
5162ee3b8144Sszhou 		title = "Solaris";	/* default to Solaris */
51637c478bd9Sstevel@tonic-gate 	}
51647c478bd9Sstevel@tonic-gate 	if (kernel == NULL) {
5165f64ca102SToomas Soome 		bam_error(_("missing suboption: %s\n"), menu_cmds[KERNEL_CMD]);
51667c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
51677c478bd9Sstevel@tonic-gate 	}
51687c478bd9Sstevel@tonic-gate 	if (module == NULL) {
5169ae115bc7Smrj 		if (bam_direct != BAM_DIRECT_DBOOT) {
5170f64ca102SToomas Soome 			bam_error(_("missing suboption: %s\n"),
5171f64ca102SToomas Soome 			    menu_cmds[MODULE_CMD]);
51727c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
51737c478bd9Sstevel@tonic-gate 		}
51747c478bd9Sstevel@tonic-gate 
5175ae115bc7Smrj 		/* Figure the commands out from the kernel line */
5176ae115bc7Smrj 		if (strstr(kernel, "$ISADIR") != NULL) {
5177ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE;
5178ae115bc7Smrj 		} else if (strstr(kernel, "amd64") != NULL) {
5179ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE_64;
5180ae115bc7Smrj 		} else {
5181ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE_32;
5182ae115bc7Smrj 		}
51839adc5a4eSEdward Gillett 	}
51849adc5a4eSEdward Gillett 
5185ae115bc7Smrj 	k_cmd = KERNEL_DOLLAR_CMD;
5186ae115bc7Smrj 	m_cmd = MODULE_DOLLAR_CMD;
5187ae115bc7Smrj 
51887c478bd9Sstevel@tonic-gate 	if (mp->start) {
51897c478bd9Sstevel@tonic-gate 		lineNum = mp->end->lineNum;
51907c478bd9Sstevel@tonic-gate 		entryNum = mp->end->entryNum;
51917c478bd9Sstevel@tonic-gate 	} else {
51927c478bd9Sstevel@tonic-gate 		lineNum = LINE_INIT;
51937c478bd9Sstevel@tonic-gate 		entryNum = ENTRY_INIT;
51947c478bd9Sstevel@tonic-gate 	}
51957c478bd9Sstevel@tonic-gate 
51967c478bd9Sstevel@tonic-gate 	/*
51977c478bd9Sstevel@tonic-gate 	 * No separator for comment (HDR/FTR) commands
51987c478bd9Sstevel@tonic-gate 	 * The syntax for comments is #<comment>
51997c478bd9Sstevel@tonic-gate 	 */
52007c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
5201ae115bc7Smrj 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_HDR);
52028c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
52037c478bd9Sstevel@tonic-gate 
52047c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
52057c478bd9Sstevel@tonic-gate 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
52068c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
52077c478bd9Sstevel@tonic-gate 
52087c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
5209eb2bd662Svikram 	    menu_cmds[FINDROOT_CMD], menu_cmds[SEP_CMD], findroot);
52108c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
5211f64ca102SToomas Soome 	BAM_DPRINTF(("%s: findroot added: line#: %d: entry#: %d\n",
5212f64ca102SToomas Soome 	    fcn, lineNum, entryNum));
52137c478bd9Sstevel@tonic-gate 
521444da779fSWilliam Kucharski 	if (bootfs != NULL) {
521544da779fSWilliam Kucharski 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
521644da779fSWilliam Kucharski 		    menu_cmds[BOOTFS_CMD], menu_cmds[SEP_CMD], bootfs);
521744da779fSWilliam Kucharski 		line_parser(mp, linebuf, &lineNum, &entryNum);
521844da779fSWilliam Kucharski 	}
521944da779fSWilliam Kucharski 
52207c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
5221ae115bc7Smrj 	    menu_cmds[k_cmd], menu_cmds[SEP_CMD], kernel);
52228c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
52237c478bd9Sstevel@tonic-gate 
5224843e1988Sjohnlev 	if (mod_kernel != NULL) {
5225843e1988Sjohnlev 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
5226843e1988Sjohnlev 		    menu_cmds[m_cmd], menu_cmds[SEP_CMD], mod_kernel);
5227843e1988Sjohnlev 		line_parser(mp, linebuf, &lineNum, &entryNum);
5228843e1988Sjohnlev 	}
5229843e1988Sjohnlev 
52307c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
5231ae115bc7Smrj 	    menu_cmds[m_cmd], menu_cmds[SEP_CMD], module);
52328c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
52337c478bd9Sstevel@tonic-gate 
52347c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
5235ae115bc7Smrj 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_FTR);
52368c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
52377c478bd9Sstevel@tonic-gate 
52387c478bd9Sstevel@tonic-gate 	return (entryNum);
52397c478bd9Sstevel@tonic-gate }
52407c478bd9Sstevel@tonic-gate 
524144da779fSWilliam Kucharski error_t
524244da779fSWilliam Kucharski delete_boot_entry(menu_t *mp, int entryNum, int quiet)
52437c478bd9Sstevel@tonic-gate {
5244eb2bd662Svikram 	line_t		*lp;
5245eb2bd662Svikram 	line_t		*freed;
5246eb2bd662Svikram 	entry_t		*ent;
5247eb2bd662Svikram 	entry_t		*tmp;
524844da779fSWilliam Kucharski 	int		deleted = 0;
524944da779fSWilliam Kucharski 	const char	*fcn = "delete_boot_entry()";
52507c478bd9Sstevel@tonic-gate 
52517c478bd9Sstevel@tonic-gate 	assert(entryNum != ENTRY_INIT);
52527c478bd9Sstevel@tonic-gate 
5253eb2bd662Svikram 	tmp = NULL;
5254eb2bd662Svikram 
52558c1b6884Sszhou 	ent = mp->entries;
52568c1b6884Sszhou 	while (ent) {
52578c1b6884Sszhou 		lp = ent->start;
5258772d6a58SWilliam Kucharski 
5259772d6a58SWilliam Kucharski 		/*
5260772d6a58SWilliam Kucharski 		 * Check entry number and make sure it's a modifiable entry.
5261772d6a58SWilliam Kucharski 		 *
5262772d6a58SWilliam Kucharski 		 * Guidelines:
5263772d6a58SWilliam Kucharski 		 *	+ We can modify a bootadm-created entry
5264772d6a58SWilliam Kucharski 		 *	+ We can modify a libbe-created entry
5265772d6a58SWilliam Kucharski 		 */
5266772d6a58SWilliam Kucharski 		if ((lp->flags != BAM_COMMENT &&
5267772d6a58SWilliam Kucharski 		    (((ent->flags & BAM_ENTRY_LIBBE) == 0) &&
5268772d6a58SWilliam Kucharski 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0)) ||
52698c1b6884Sszhou 		    (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) {
52708c1b6884Sszhou 			ent = ent->next;
52717c478bd9Sstevel@tonic-gate 			continue;
52727c478bd9Sstevel@tonic-gate 		}
52737c478bd9Sstevel@tonic-gate 
52748c1b6884Sszhou 		/* free the entry content */
52758c1b6884Sszhou 		do {
52768c1b6884Sszhou 			freed = lp;
52778c1b6884Sszhou 			lp = lp->next;	/* prev stays the same */
5278f64ca102SToomas Soome 			BAM_DPRINTF(("%s: freeing line: %d\n",
5279f64ca102SToomas Soome 			    fcn, freed->lineNum));
52808c1b6884Sszhou 			unlink_line(mp, freed);
52818c1b6884Sszhou 			line_free(freed);
52828c1b6884Sszhou 		} while (freed != ent->end);
52837c478bd9Sstevel@tonic-gate 
52848c1b6884Sszhou 		/* free the entry_t structure */
5285eb2bd662Svikram 		assert(tmp == NULL);
52868c1b6884Sszhou 		tmp = ent;
52878c1b6884Sszhou 		ent = ent->next;
52888c1b6884Sszhou 		if (tmp->prev)
52898c1b6884Sszhou 			tmp->prev->next = ent;
52907c478bd9Sstevel@tonic-gate 		else
52918c1b6884Sszhou 			mp->entries = ent;
52928c1b6884Sszhou 		if (ent)
52938c1b6884Sszhou 			ent->prev = tmp->prev;
5294f64ca102SToomas Soome 		BAM_DPRINTF(("%s: freeing entry: %d\n", fcn, tmp->entryNum));
5295eb2bd662Svikram 		free(tmp);
5296eb2bd662Svikram 		tmp = NULL;
52977c478bd9Sstevel@tonic-gate 		deleted = 1;
52987c478bd9Sstevel@tonic-gate 	}
52997c478bd9Sstevel@tonic-gate 
5300eb2bd662Svikram 	assert(tmp == NULL);
5301eb2bd662Svikram 
53027c478bd9Sstevel@tonic-gate 	if (!deleted && entryNum != ALL_ENTRIES) {
530344da779fSWilliam Kucharski 		if (quiet == DBE_PRINTERR)
5304f64ca102SToomas Soome 			bam_error(_("no matching bootadm entry found\n"));
53057c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
53067c478bd9Sstevel@tonic-gate 	}
53077c478bd9Sstevel@tonic-gate 
530840541d5dSvikram 	/*
530940541d5dSvikram 	 * Now that we have deleted an entry, update
531040541d5dSvikram 	 * the entry numbering and the default cmd.
531140541d5dSvikram 	 */
531240541d5dSvikram 	update_numbering(mp);
531340541d5dSvikram 
53147c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
53157c478bd9Sstevel@tonic-gate }
53167c478bd9Sstevel@tonic-gate 
53177c478bd9Sstevel@tonic-gate static error_t
5318eb2bd662Svikram delete_all_entries(menu_t *mp, char *dummy, char *opt)
53197c478bd9Sstevel@tonic-gate {
53207c478bd9Sstevel@tonic-gate 	assert(mp);
5321eb2bd662Svikram 	assert(dummy == NULL);
53227c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
53237c478bd9Sstevel@tonic-gate 
5324f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. No args\n", "delete_all_entries"));
5325eb2bd662Svikram 
53267c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
5327f64ca102SToomas Soome 		bam_print(_("the GRUB menu is empty\n"));
53287c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
53297c478bd9Sstevel@tonic-gate 	}
53307c478bd9Sstevel@tonic-gate 
533144da779fSWilliam Kucharski 	if (delete_boot_entry(mp, ALL_ENTRIES, DBE_PRINTERR) != BAM_SUCCESS) {
53327c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
53337c478bd9Sstevel@tonic-gate 	}
53347c478bd9Sstevel@tonic-gate 
53357c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
53367c478bd9Sstevel@tonic-gate }
53377c478bd9Sstevel@tonic-gate 
53387c478bd9Sstevel@tonic-gate static FILE *
5339eb2bd662Svikram create_diskmap(char *osroot)
53407c478bd9Sstevel@tonic-gate {
53417c478bd9Sstevel@tonic-gate 	FILE *fp;
5342cedc7e57SEnrico Perla - Sun Microsystems 	char cmd[PATH_MAX + 16];
5343cedc7e57SEnrico Perla - Sun Microsystems 	char path[PATH_MAX];
5344eb2bd662Svikram 	const char *fcn = "create_diskmap()";
53457c478bd9Sstevel@tonic-gate 
53467c478bd9Sstevel@tonic-gate 	/* make sure we have a map file */
53477c478bd9Sstevel@tonic-gate 	fp = fopen(GRUBDISK_MAP, "r");
53487c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
5349cedc7e57SEnrico Perla - Sun Microsystems 		int	ret;
5350cedc7e57SEnrico Perla - Sun Microsystems 
5351cedc7e57SEnrico Perla - Sun Microsystems 		ret = snprintf(path, sizeof (path), "%s/%s", osroot,
5352cedc7e57SEnrico Perla - Sun Microsystems 		    CREATE_DISKMAP);
5353cedc7e57SEnrico Perla - Sun Microsystems 		if (ret >= sizeof (path)) {
5354f64ca102SToomas Soome 			bam_error(_("unable to create path on mountpoint %s, "
5355f64ca102SToomas Soome 			    "path too long\n"), osroot);
5356cedc7e57SEnrico Perla - Sun Microsystems 			return (NULL);
5357cedc7e57SEnrico Perla - Sun Microsystems 		}
5358cedc7e57SEnrico Perla - Sun Microsystems 		if (is_safe_exec(path) == BAM_ERROR)
5359cedc7e57SEnrico Perla - Sun Microsystems 			return (NULL);
5360cedc7e57SEnrico Perla - Sun Microsystems 
53617c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd),
5362eb2bd662Svikram 		    "%s/%s > /dev/null", osroot, CREATE_DISKMAP);
5363eb2bd662Svikram 		if (exec_cmd(cmd, NULL) != 0)
5364eb2bd662Svikram 			return (NULL);
53657c478bd9Sstevel@tonic-gate 		fp = fopen(GRUBDISK_MAP, "r");
5366eb2bd662Svikram 		INJECT_ERROR1("DISKMAP_CREATE_FAIL", fp = NULL);
5367eb2bd662Svikram 		if (fp) {
5368f64ca102SToomas Soome 			BAM_DPRINTF(("%s: created diskmap file: %s\n",
5369f64ca102SToomas Soome 			    fcn, GRUBDISK_MAP));
5370eb2bd662Svikram 		} else {
5371f64ca102SToomas Soome 			BAM_DPRINTF(("%s: FAILED to create diskmap file: %s\n",
5372f64ca102SToomas Soome 			    fcn, GRUBDISK_MAP));
5373eb2bd662Svikram 		}
53747c478bd9Sstevel@tonic-gate 	}
53757c478bd9Sstevel@tonic-gate 	return (fp);
53767c478bd9Sstevel@tonic-gate }
53777c478bd9Sstevel@tonic-gate 
53787c478bd9Sstevel@tonic-gate #define	SECTOR_SIZE	512
53797c478bd9Sstevel@tonic-gate 
53807c478bd9Sstevel@tonic-gate static int
53817c478bd9Sstevel@tonic-gate get_partition(char *device)
53827c478bd9Sstevel@tonic-gate {
53831a902ef8SHans Rosenfeld 	int i, fd, is_pcfs, partno = PARTNO_NOTFOUND;
53847c478bd9Sstevel@tonic-gate 	struct mboot *mboot;
53857c478bd9Sstevel@tonic-gate 	char boot_sect[SECTOR_SIZE];
53867c478bd9Sstevel@tonic-gate 	char *wholedisk, *slice;
5387e998e519SSheshadri Vasudevan #ifdef i386
5388e998e519SSheshadri Vasudevan 	ext_part_t *epp;
5389e998e519SSheshadri Vasudevan 	uint32_t secnum, numsec;
53901a902ef8SHans Rosenfeld 	int rval, pno, ext_partno = PARTNO_NOTFOUND;
5391e998e519SSheshadri Vasudevan #endif
53927c478bd9Sstevel@tonic-gate 
53937c478bd9Sstevel@tonic-gate 	/* form whole disk (p0) */
53947c478bd9Sstevel@tonic-gate 	slice = device + strlen(device) - 2;
53957c478bd9Sstevel@tonic-gate 	is_pcfs = (*slice != 's');
53967c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
53977c478bd9Sstevel@tonic-gate 		*slice = '\0';
53987c478bd9Sstevel@tonic-gate 	wholedisk = s_calloc(1, strlen(device) + 3);
53997c478bd9Sstevel@tonic-gate 	(void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device);
54007c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
54017c478bd9Sstevel@tonic-gate 		*slice = 's';
54027c478bd9Sstevel@tonic-gate 
54037c478bd9Sstevel@tonic-gate 	/* read boot sector */
54047c478bd9Sstevel@tonic-gate 	fd = open(wholedisk, O_RDONLY);
54057c478bd9Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
54067c478bd9Sstevel@tonic-gate 		return (partno);
54077c478bd9Sstevel@tonic-gate 	}
54087c478bd9Sstevel@tonic-gate 	(void) close(fd);
54097c478bd9Sstevel@tonic-gate 
5410e998e519SSheshadri Vasudevan #ifdef i386
5411e998e519SSheshadri Vasudevan 	/* Read/Initialize extended partition information */
5412e998e519SSheshadri Vasudevan 	if ((rval = libfdisk_init(&epp, wholedisk, NULL, FDISK_READ_DISK))
5413e998e519SSheshadri Vasudevan 	    != FDISK_SUCCESS) {
5414e998e519SSheshadri Vasudevan 		switch (rval) {
5415e998e519SSheshadri Vasudevan 			/*
5416e998e519SSheshadri Vasudevan 			 * FDISK_EBADLOGDRIVE and FDISK_ENOLOGDRIVE can
5417e998e519SSheshadri Vasudevan 			 * be considered as soft errors and hence
5418e998e519SSheshadri Vasudevan 			 * we do not return
5419e998e519SSheshadri Vasudevan 			 */
5420e998e519SSheshadri Vasudevan 			case FDISK_EBADLOGDRIVE:
5421e998e519SSheshadri Vasudevan 				break;
5422e998e519SSheshadri Vasudevan 			case FDISK_ENOLOGDRIVE:
5423e998e519SSheshadri Vasudevan 				break;
54246cb5747bSSharath M Srinivasan 			case FDISK_EBADMAGIC:
54256cb5747bSSharath M Srinivasan 				/*FALLTHROUGH*/
5426e998e519SSheshadri Vasudevan 			default:
5427e998e519SSheshadri Vasudevan 				free(wholedisk);
54286cb5747bSSharath M Srinivasan 				libfdisk_fini(&epp);
5429e998e519SSheshadri Vasudevan 				return (partno);
5430e998e519SSheshadri Vasudevan 		}
5431e998e519SSheshadri Vasudevan 	}
5432e998e519SSheshadri Vasudevan #endif
5433e998e519SSheshadri Vasudevan 	free(wholedisk);
5434e998e519SSheshadri Vasudevan 
54357c478bd9Sstevel@tonic-gate 	/* parse fdisk table */
54367c478bd9Sstevel@tonic-gate 	mboot = (struct mboot *)((void *)boot_sect);
54377c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
54387c478bd9Sstevel@tonic-gate 		struct ipart *part =
54397c478bd9Sstevel@tonic-gate 		    (struct ipart *)(uintptr_t)mboot->parts + i;
54407c478bd9Sstevel@tonic-gate 		if (is_pcfs) {	/* looking for solaris boot part */
54417c478bd9Sstevel@tonic-gate 			if (part->systid == 0xbe) {
54427c478bd9Sstevel@tonic-gate 				partno = i;
54437c478bd9Sstevel@tonic-gate 				break;
54447c478bd9Sstevel@tonic-gate 			}
54457c478bd9Sstevel@tonic-gate 		} else {	/* look for solaris partition, old and new */
54461a902ef8SHans Rosenfeld 			if (part->systid == EFI_PMBR) {
54471a902ef8SHans Rosenfeld 				partno = PARTNO_EFI;
54481a902ef8SHans Rosenfeld 				break;
54491a902ef8SHans Rosenfeld 			}
54501a902ef8SHans Rosenfeld 
5451e998e519SSheshadri Vasudevan #ifdef i386
5452e998e519SSheshadri Vasudevan 			if ((part->systid == SUNIXOS &&
5453e998e519SSheshadri Vasudevan 			    (fdisk_is_linux_swap(epp, part->relsect,
5454e998e519SSheshadri Vasudevan 			    NULL) != 0)) || part->systid == SUNIXOS2) {
5455e998e519SSheshadri Vasudevan #else
54567c478bd9Sstevel@tonic-gate 			if (part->systid == SUNIXOS ||
54577c478bd9Sstevel@tonic-gate 			    part->systid == SUNIXOS2) {
5458e998e519SSheshadri Vasudevan #endif
54597c478bd9Sstevel@tonic-gate 				partno = i;
54607c478bd9Sstevel@tonic-gate 				break;
54617c478bd9Sstevel@tonic-gate 			}
546228074f82SSheshadri Vasudevan 
546328074f82SSheshadri Vasudevan #ifdef i386
546428074f82SSheshadri Vasudevan 			if (fdisk_is_dos_extended(part->systid))
546528074f82SSheshadri Vasudevan 				ext_partno = i;
546628074f82SSheshadri Vasudevan #endif
54677c478bd9Sstevel@tonic-gate 		}
54687c478bd9Sstevel@tonic-gate 	}
5469e998e519SSheshadri Vasudevan #ifdef i386
547028074f82SSheshadri Vasudevan 	/* If no primary solaris partition, check extended partition */
54711a902ef8SHans Rosenfeld 	if ((partno == PARTNO_NOTFOUND) && (ext_partno != PARTNO_NOTFOUND)) {
547228074f82SSheshadri Vasudevan 		rval = fdisk_get_solaris_part(epp, &pno, &secnum, &numsec);
547328074f82SSheshadri Vasudevan 		if (rval == FDISK_SUCCESS) {
547428074f82SSheshadri Vasudevan 			partno = pno - 1;
547528074f82SSheshadri Vasudevan 		}
547628074f82SSheshadri Vasudevan 	}
5477e998e519SSheshadri Vasudevan 	libfdisk_fini(&epp);
5478e998e519SSheshadri Vasudevan #endif
54797c478bd9Sstevel@tonic-gate 	return (partno);
54807c478bd9Sstevel@tonic-gate }
54817c478bd9Sstevel@tonic-gate 
5482eb2bd662Svikram char *
5483eb2bd662Svikram get_grubroot(char *osroot, char *osdev, char *menu_root)
54847c478bd9Sstevel@tonic-gate {
5485eb2bd662Svikram 	char		*grubroot;	/* (hd#,#,#) */
54867c478bd9Sstevel@tonic-gate 	char		*slice;
5487110570ceSToomas Soome 	char		*grubhd = NULL;
54887c478bd9Sstevel@tonic-gate 	int		fdiskpart;
54897c478bd9Sstevel@tonic-gate 	int		found = 0;
5490eb2bd662Svikram 	char		*devname;
5491eb2bd662Svikram 	char		*ctdname = strstr(osdev, "dsk/");
54927c478bd9Sstevel@tonic-gate 	char		linebuf[PATH_MAX];
5493eb2bd662Svikram 	FILE		*fp;
54947c478bd9Sstevel@tonic-gate 
5495eb2bd662Svikram 	INJECT_ERROR1("GRUBROOT_INVALID_OSDEV", ctdname = NULL);
5496eb2bd662Svikram 	if (ctdname == NULL) {
5497f64ca102SToomas Soome 		bam_error(_("not a /dev/[r]dsk name: %s\n"), osdev);
54987c478bd9Sstevel@tonic-gate 		return (NULL);
5499eb2bd662Svikram 	}
5500eb2bd662Svikram 
5501eb2bd662Svikram 	if (menu_root && !menu_on_bootdisk(osroot, menu_root)) {
5502eb2bd662Svikram 		/* menu bears no resemblance to our reality */
5503f64ca102SToomas Soome 		bam_error(_("cannot get (hd?,?,?) for menu. menu not on "
5504f64ca102SToomas Soome 		    "bootdisk: %s\n"), osdev);
5505eb2bd662Svikram 		return (NULL);
5506eb2bd662Svikram 	}
55077c478bd9Sstevel@tonic-gate 
55087c478bd9Sstevel@tonic-gate 	ctdname += strlen("dsk/");
55097c478bd9Sstevel@tonic-gate 	slice = strrchr(ctdname, 's');
55107c478bd9Sstevel@tonic-gate 	if (slice)
55117c478bd9Sstevel@tonic-gate 		*slice = '\0';
55127c478bd9Sstevel@tonic-gate 
5513eb2bd662Svikram 	fp = create_diskmap(osroot);
5514eb2bd662Svikram 	if (fp == NULL) {
5515f64ca102SToomas Soome 		bam_error(_("create_diskmap command failed for OS root: %s.\n"),
5516f64ca102SToomas Soome 		    osroot);
5517eb2bd662Svikram 		return (NULL);
5518eb2bd662Svikram 	}
5519eb2bd662Svikram 
55207c478bd9Sstevel@tonic-gate 	rewind(fp);
55217c478bd9Sstevel@tonic-gate 	while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) {
55227c478bd9Sstevel@tonic-gate 		grubhd = strtok(linebuf, " \t\n");
55237c478bd9Sstevel@tonic-gate 		if (grubhd)
55247c478bd9Sstevel@tonic-gate 			devname = strtok(NULL, " \t\n");
55257c478bd9Sstevel@tonic-gate 		else
55267c478bd9Sstevel@tonic-gate 			devname = NULL;
55277c478bd9Sstevel@tonic-gate 		if (devname && strcmp(devname, ctdname) == 0) {
55287c478bd9Sstevel@tonic-gate 			found = 1;
55297c478bd9Sstevel@tonic-gate 			break;
55307c478bd9Sstevel@tonic-gate 		}
55317c478bd9Sstevel@tonic-gate 	}
55327c478bd9Sstevel@tonic-gate 
55337c478bd9Sstevel@tonic-gate 	if (slice)
55347c478bd9Sstevel@tonic-gate 		*slice = 's';
55357c478bd9Sstevel@tonic-gate 
5536eb2bd662Svikram 	(void) fclose(fp);
5537eb2bd662Svikram 	fp = NULL;
5538eb2bd662Svikram 
5539eb2bd662Svikram 	INJECT_ERROR1("GRUBROOT_BIOSDEV_FAIL", found = 0);
55407c478bd9Sstevel@tonic-gate 	if (found == 0) {
5541f64ca102SToomas Soome 		bam_error(_("not using biosdev command for disk: %s.\n"),
5542f64ca102SToomas Soome 		    osdev);
5543eb2bd662Svikram 		return (NULL);
55447c478bd9Sstevel@tonic-gate 	}
55457c478bd9Sstevel@tonic-gate 
5546eb2bd662Svikram 	fdiskpart = get_partition(osdev);
55471a902ef8SHans Rosenfeld 	INJECT_ERROR1("GRUBROOT_FDISK_FAIL", fdiskpart = PARTNO_NOTFOUND);
55481a902ef8SHans Rosenfeld 	if (fdiskpart == PARTNO_NOTFOUND) {
5549f64ca102SToomas Soome 		bam_error(_("failed to determine fdisk partition: %s\n"),
5550f64ca102SToomas Soome 		    osdev);
55517c478bd9Sstevel@tonic-gate 		return (NULL);
5552eb2bd662Svikram 	}
55537c478bd9Sstevel@tonic-gate 
5554eb2bd662Svikram 	grubroot = s_calloc(1, 10);
55551a902ef8SHans Rosenfeld 	if (fdiskpart == PARTNO_EFI) {
55561a902ef8SHans Rosenfeld 		fdiskpart = atoi(&slice[1]);
55571a902ef8SHans Rosenfeld 		slice = NULL;
55581a902ef8SHans Rosenfeld 	}
55591a902ef8SHans Rosenfeld 
55607c478bd9Sstevel@tonic-gate 	if (slice) {
5561eb2bd662Svikram 		(void) snprintf(grubroot, 10, "(hd%s,%d,%c)",
55627c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart, slice[1] + 'a' - '0');
55637c478bd9Sstevel@tonic-gate 	} else
5564eb2bd662Svikram 		(void) snprintf(grubroot, 10, "(hd%s,%d)",
55657c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart);
55667c478bd9Sstevel@tonic-gate 
5567eb2bd662Svikram 	assert(fp == NULL);
5568eb2bd662Svikram 	assert(strncmp(grubroot, "(hd", strlen("(hd")) == 0);
5569eb2bd662Svikram 	return (grubroot);
5570eb2bd662Svikram }
5571eb2bd662Svikram 
5572eb2bd662Svikram static char *
5573eb2bd662Svikram find_primary_common(char *mntpt, char *fstype)
5574eb2bd662Svikram {
5575eb2bd662Svikram 	char		signdir[PATH_MAX];
5576eb2bd662Svikram 	char		tmpsign[MAXNAMELEN + 1];
5577eb2bd662Svikram 	char		*lu;
5578eb2bd662Svikram 	char		*ufs;
5579eb2bd662Svikram 	char		*zfs;
5580eb2bd662Svikram 	DIR		*dirp = NULL;
5581eb2bd662Svikram 	struct dirent	*entp;
5582eb2bd662Svikram 	struct stat	sb;
5583eb2bd662Svikram 	const char	*fcn = "find_primary_common()";
5584eb2bd662Svikram 
5585eb2bd662Svikram 	(void) snprintf(signdir, sizeof (signdir), "%s/%s",
5586eb2bd662Svikram 	    mntpt, GRUBSIGN_DIR);
5587eb2bd662Svikram 
5588eb2bd662Svikram 	if (stat(signdir, &sb) == -1) {
5589f64ca102SToomas Soome 		BAM_DPRINTF(("%s: no sign dir: %s\n", fcn, signdir));
5590eb2bd662Svikram 		return (NULL);
5591eb2bd662Svikram 	}
5592eb2bd662Svikram 
5593eb2bd662Svikram 	dirp = opendir(signdir);
5594eb2bd662Svikram 	INJECT_ERROR1("SIGNDIR_OPENDIR_FAIL", dirp = NULL);
5595eb2bd662Svikram 	if (dirp == NULL) {
5596f64ca102SToomas Soome 		bam_error(_("opendir of %s failed: %s\n"), signdir,
5597f64ca102SToomas Soome 		    strerror(errno));
5598eb2bd662Svikram 		return (NULL);
5599eb2bd662Svikram 	}
5600eb2bd662Svikram 
5601eb2bd662Svikram 	ufs = zfs = lu = NULL;
5602eb2bd662Svikram 
5603110570ceSToomas Soome 	while ((entp = readdir(dirp)) != NULL) {
5604eb2bd662Svikram 		if (strcmp(entp->d_name, ".") == 0 ||
5605eb2bd662Svikram 		    strcmp(entp->d_name, "..") == 0)
5606eb2bd662Svikram 			continue;
5607eb2bd662Svikram 
5608eb2bd662Svikram 		(void) snprintf(tmpsign, sizeof (tmpsign), "%s", entp->d_name);
5609eb2bd662Svikram 
5610eb2bd662Svikram 		if (lu == NULL &&
5611eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_LU_PREFIX,
5612eb2bd662Svikram 		    strlen(GRUBSIGN_LU_PREFIX)) == 0) {
5613eb2bd662Svikram 			lu = s_strdup(tmpsign);
5614eb2bd662Svikram 		}
5615eb2bd662Svikram 
5616eb2bd662Svikram 		if (ufs == NULL &&
5617eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
5618eb2bd662Svikram 		    strlen(GRUBSIGN_UFS_PREFIX)) == 0) {
5619eb2bd662Svikram 			ufs = s_strdup(tmpsign);
5620eb2bd662Svikram 		}
5621eb2bd662Svikram 
5622eb2bd662Svikram 		if (zfs == NULL &&
5623eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_ZFS_PREFIX,
5624eb2bd662Svikram 		    strlen(GRUBSIGN_ZFS_PREFIX)) == 0) {
5625eb2bd662Svikram 			zfs = s_strdup(tmpsign);
5626eb2bd662Svikram 		}
5627eb2bd662Svikram 	}
5628eb2bd662Svikram 
5629f64ca102SToomas Soome 	BAM_DPRINTF(("%s: existing primary signs: zfs=%s ufs=%s lu=%s\n", fcn,
5630eb2bd662Svikram 	    zfs ? zfs : "NULL",
5631eb2bd662Svikram 	    ufs ? ufs : "NULL",
5632eb2bd662Svikram 	    lu ? lu : "NULL"));
5633eb2bd662Svikram 
5634eb2bd662Svikram 	if (dirp) {
5635eb2bd662Svikram 		(void) closedir(dirp);
5636eb2bd662Svikram 		dirp = NULL;
5637eb2bd662Svikram 	}
5638eb2bd662Svikram 
5639eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0 && zfs) {
5640f64ca102SToomas Soome 		bam_error(_("found mismatched boot signature %s for "
5641f64ca102SToomas Soome 		    "filesystem type: %s.\n"), zfs, "ufs");
5642eb2bd662Svikram 		free(zfs);
5643eb2bd662Svikram 		zfs = NULL;
5644eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0 && ufs) {
5645f64ca102SToomas Soome 		bam_error(_("found mismatched boot signature %s for "
5646f64ca102SToomas Soome 		    "filesystem type: %s.\n"), ufs, "zfs");
5647eb2bd662Svikram 		free(ufs);
5648eb2bd662Svikram 		ufs = NULL;
5649eb2bd662Svikram 	}
5650eb2bd662Svikram 
5651eb2bd662Svikram 	assert(dirp == NULL);
5652eb2bd662Svikram 
5653eb2bd662Svikram 	/* For now, we let Live Upgrade take care of its signature itself */
5654eb2bd662Svikram 	if (lu) {
5655f64ca102SToomas Soome 		BAM_DPRINTF(("%s: feeing LU sign: %s\n", fcn, lu));
5656eb2bd662Svikram 		free(lu);
5657eb2bd662Svikram 		lu = NULL;
5658eb2bd662Svikram 	}
5659eb2bd662Svikram 
5660eb2bd662Svikram 	return (zfs ? zfs : ufs);
5661eb2bd662Svikram }
5662eb2bd662Svikram 
5663eb2bd662Svikram static char *
5664eb2bd662Svikram find_backup_common(char *mntpt, char *fstype)
5665eb2bd662Svikram {
5666eb2bd662Svikram 	FILE		*bfp = NULL;
5667eb2bd662Svikram 	char		tmpsign[MAXNAMELEN + 1];
5668eb2bd662Svikram 	char		backup[PATH_MAX];
5669eb2bd662Svikram 	char		*ufs;
5670eb2bd662Svikram 	char		*zfs;
5671eb2bd662Svikram 	char		*lu;
5672eb2bd662Svikram 	int		error;
5673eb2bd662Svikram 	const char	*fcn = "find_backup_common()";
5674eb2bd662Svikram 
5675eb2bd662Svikram 	/*
5676eb2bd662Svikram 	 * We didn't find it in the primary directory.
5677eb2bd662Svikram 	 * Look at the backup
5678eb2bd662Svikram 	 */
5679eb2bd662Svikram 	(void) snprintf(backup, sizeof (backup), "%s%s",
5680eb2bd662Svikram 	    mntpt, GRUBSIGN_BACKUP);
5681eb2bd662Svikram 
5682eb2bd662Svikram 	bfp = fopen(backup, "r");
5683eb2bd662Svikram 	if (bfp == NULL) {
5684eb2bd662Svikram 		error = errno;
5685eb2bd662Svikram 		if (bam_verbose) {
5686f64ca102SToomas Soome 			bam_error(_("failed to open file: %s: %s\n"),
5687f64ca102SToomas Soome 			    backup, strerror(error));
5688eb2bd662Svikram 		}
5689f64ca102SToomas Soome 		BAM_DPRINTF(("%s: failed to open %s: %s\n",
5690f64ca102SToomas Soome 		    fcn, backup, strerror(error)));
5691eb2bd662Svikram 		return (NULL);
5692eb2bd662Svikram 	}
5693eb2bd662Svikram 
5694eb2bd662Svikram 	ufs = zfs = lu = NULL;
5695eb2bd662Svikram 
5696eb2bd662Svikram 	while (s_fgets(tmpsign, sizeof (tmpsign), bfp) != NULL) {
5697eb2bd662Svikram 
5698eb2bd662Svikram 		if (lu == NULL &&
5699eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_LU_PREFIX,
5700eb2bd662Svikram 		    strlen(GRUBSIGN_LU_PREFIX)) == 0) {
5701eb2bd662Svikram 			lu = s_strdup(tmpsign);
5702eb2bd662Svikram 		}
5703eb2bd662Svikram 
5704eb2bd662Svikram 		if (ufs == NULL &&
5705eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
5706eb2bd662Svikram 		    strlen(GRUBSIGN_UFS_PREFIX)) == 0) {
5707eb2bd662Svikram 			ufs = s_strdup(tmpsign);
5708eb2bd662Svikram 		}
5709eb2bd662Svikram 
5710eb2bd662Svikram 		if (zfs == NULL &&
5711eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_ZFS_PREFIX,
5712eb2bd662Svikram 		    strlen(GRUBSIGN_ZFS_PREFIX)) == 0) {
5713eb2bd662Svikram 			zfs = s_strdup(tmpsign);
5714eb2bd662Svikram 		}
5715eb2bd662Svikram 	}
5716eb2bd662Svikram 
5717f64ca102SToomas Soome 	BAM_DPRINTF(("%s: existing backup signs: zfs=%s ufs=%s lu=%s\n", fcn,
5718eb2bd662Svikram 	    zfs ? zfs : "NULL",
5719eb2bd662Svikram 	    ufs ? ufs : "NULL",
5720eb2bd662Svikram 	    lu ? lu : "NULL"));
5721eb2bd662Svikram 
5722eb2bd662Svikram 	if (bfp) {
5723eb2bd662Svikram 		(void) fclose(bfp);
5724eb2bd662Svikram 		bfp = NULL;
5725eb2bd662Svikram 	}
5726eb2bd662Svikram 
5727eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0 && zfs) {
5728f64ca102SToomas Soome 		bam_error(_("found mismatched boot signature %s for "
5729f64ca102SToomas Soome 		    "filesystem type: %s.\n"), zfs, "ufs");
5730eb2bd662Svikram 		free(zfs);
5731eb2bd662Svikram 		zfs = NULL;
5732eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0 && ufs) {
5733f64ca102SToomas Soome 		bam_error(_("found mismatched boot signature %s for "
5734f64ca102SToomas Soome 		    "filesystem type: %s.\n"), ufs, "zfs");
5735eb2bd662Svikram 		free(ufs);
5736eb2bd662Svikram 		ufs = NULL;
5737eb2bd662Svikram 	}
5738eb2bd662Svikram 
5739eb2bd662Svikram 	assert(bfp == NULL);
5740eb2bd662Svikram 
5741eb2bd662Svikram 	/* For now, we let Live Upgrade take care of its signature itself */
5742eb2bd662Svikram 	if (lu) {
5743f64ca102SToomas Soome 		BAM_DPRINTF(("%s: feeing LU sign: %s\n", fcn, lu));
5744eb2bd662Svikram 		free(lu);
5745eb2bd662Svikram 		lu = NULL;
5746eb2bd662Svikram 	}
5747eb2bd662Svikram 
5748eb2bd662Svikram 	return (zfs ? zfs : ufs);
5749eb2bd662Svikram }
5750eb2bd662Svikram 
5751eb2bd662Svikram static char *
5752eb2bd662Svikram find_ufs_existing(char *osroot)
5753eb2bd662Svikram {
5754eb2bd662Svikram 	char		*sign;
5755eb2bd662Svikram 	const char	*fcn = "find_ufs_existing()";
5756eb2bd662Svikram 
5757eb2bd662Svikram 	sign = find_primary_common(osroot, "ufs");
5758eb2bd662Svikram 	if (sign == NULL) {
5759eb2bd662Svikram 		sign = find_backup_common(osroot, "ufs");
5760f64ca102SToomas Soome 		BAM_DPRINTF(("%s: existing backup sign: %s\n", fcn,
5761f64ca102SToomas Soome 		    sign ? sign : "NULL"));
5762eb2bd662Svikram 	} else {
5763f64ca102SToomas Soome 		BAM_DPRINTF(("%s: existing primary sign: %s\n", fcn, sign));
5764eb2bd662Svikram 	}
5765eb2bd662Svikram 
5766eb2bd662Svikram 	return (sign);
5767eb2bd662Svikram }
5768eb2bd662Svikram 
5769eb2bd662Svikram char *
5770eb2bd662Svikram get_mountpoint(char *special, char *fstype)
5771eb2bd662Svikram {
5772eb2bd662Svikram 	FILE		*mntfp;
5773eb2bd662Svikram 	struct mnttab	mp = {0};
5774eb2bd662Svikram 	struct mnttab	mpref = {0};
5775eb2bd662Svikram 	int		error;
5776eb2bd662Svikram 	int		ret;
5777eb2bd662Svikram 	const char	*fcn = "get_mountpoint()";
5778eb2bd662Svikram 
5779f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, special, fstype));
5780eb2bd662Svikram 
5781eb2bd662Svikram 	mntfp = fopen(MNTTAB, "r");
5782eb2bd662Svikram 	error = errno;
5783eb2bd662Svikram 	INJECT_ERROR1("MNTTAB_ERR_GET_MNTPT", mntfp = NULL);
5784eb2bd662Svikram 	if (mntfp == NULL) {
5785f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"),
5786f64ca102SToomas Soome 		    MNTTAB, strerror(error));
5787eb2bd662Svikram 		return (NULL);
5788eb2bd662Svikram 	}
5789eb2bd662Svikram 
5790eb2bd662Svikram 	mpref.mnt_special = special;
5791eb2bd662Svikram 	mpref.mnt_fstype = fstype;
5792eb2bd662Svikram 
5793eb2bd662Svikram 	ret = getmntany(mntfp, &mp, &mpref);
5794eb2bd662Svikram 	INJECT_ERROR1("GET_MOUNTPOINT_MNTANY", ret = 1);
5795eb2bd662Svikram 	if (ret != 0) {
5796eb2bd662Svikram 		(void) fclose(mntfp);
5797f64ca102SToomas Soome 		BAM_DPRINTF(("%s: no mount-point for special=%s and "
5798f64ca102SToomas Soome 		    "fstype=%s\n", fcn, special, fstype));
5799eb2bd662Svikram 		return (NULL);
5800eb2bd662Svikram 	}
5801eb2bd662Svikram 	(void) fclose(mntfp);
5802eb2bd662Svikram 
5803eb2bd662Svikram 	assert(mp.mnt_mountp);
5804eb2bd662Svikram 
5805f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning mount-point for special %s: %s\n",
5806f64ca102SToomas Soome 	    fcn, special, mp.mnt_mountp));
5807eb2bd662Svikram 
5808eb2bd662Svikram 	return (s_strdup(mp.mnt_mountp));
5809eb2bd662Svikram }
5810eb2bd662Svikram 
5811eb2bd662Svikram /*
5812eb2bd662Svikram  * Mounts a "legacy" top dataset (if needed)
5813eb2bd662Svikram  * Returns:	The mountpoint of the legacy top dataset or NULL on error
5814eb2bd662Svikram  * 		mnted returns one of the above values defined for zfs_mnted_t
5815eb2bd662Svikram  */
5816eb2bd662Svikram static char *
5817eb2bd662Svikram mount_legacy_dataset(char *pool, zfs_mnted_t *mnted)
5818eb2bd662Svikram {
5819eb2bd662Svikram 	char		cmd[PATH_MAX];
5820eb2bd662Svikram 	char		tmpmnt[PATH_MAX];
5821eb2bd662Svikram 	filelist_t	flist = {0};
5822eb2bd662Svikram 	char		*is_mounted;
5823eb2bd662Svikram 	struct stat	sb;
5824eb2bd662Svikram 	int		ret;
5825eb2bd662Svikram 	const char	*fcn = "mount_legacy_dataset()";
5826eb2bd662Svikram 
5827f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. arg: %s\n", fcn, pool));
5828eb2bd662Svikram 
5829eb2bd662Svikram 	*mnted = ZFS_MNT_ERROR;
5830eb2bd662Svikram 
5831eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5832eb2bd662Svikram 	    "/sbin/zfs get -Ho value mounted %s",
5833eb2bd662Svikram 	    pool);
5834eb2bd662Svikram 
5835eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5836eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_CMD", ret = 1);
5837eb2bd662Svikram 	if (ret != 0) {
5838f64ca102SToomas Soome 		bam_error(_("failed to determine mount status of ZFS "
5839f64ca102SToomas Soome 		    "pool %s\n"), pool);
5840eb2bd662Svikram 		return (NULL);
5841eb2bd662Svikram 	}
5842eb2bd662Svikram 
5843eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_OUT", flist.head = NULL);
5844eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5845f64ca102SToomas Soome 		bam_error(_("ZFS pool %s has bad mount status\n"), pool);
5846eb2bd662Svikram 		filelist_free(&flist);
5847eb2bd662Svikram 		return (NULL);
5848eb2bd662Svikram 	}
5849eb2bd662Svikram 
5850eb2bd662Svikram 	is_mounted = strtok(flist.head->line, " \t\n");
5851eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_STRTOK_YES", is_mounted = "yes");
5852eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_STRTOK_NO", is_mounted = "no");
5853eb2bd662Svikram 	if (strcmp(is_mounted, "no") != 0) {
5854eb2bd662Svikram 		filelist_free(&flist);
5855eb2bd662Svikram 		*mnted = LEGACY_ALREADY;
5856eb2bd662Svikram 		/* get_mountpoint returns a strdup'ed string */
5857f64ca102SToomas Soome 		BAM_DPRINTF(("%s: legacy pool %s already mounted\n",
5858f64ca102SToomas Soome 		    fcn, pool));
5859eb2bd662Svikram 		return (get_mountpoint(pool, "zfs"));
5860eb2bd662Svikram 	}
5861eb2bd662Svikram 
5862eb2bd662Svikram 	filelist_free(&flist);
5863eb2bd662Svikram 
5864eb2bd662Svikram 	/*
5865eb2bd662Svikram 	 * legacy top dataset is not mounted. Mount it now
5866eb2bd662Svikram 	 * First create a mountpoint.
5867eb2bd662Svikram 	 */
5868eb2bd662Svikram 	(void) snprintf(tmpmnt, sizeof (tmpmnt), "%s.%d",
5869eb2bd662Svikram 	    ZFS_LEGACY_MNTPT, getpid());
5870eb2bd662Svikram 
5871eb2bd662Svikram 	ret = stat(tmpmnt, &sb);
5872eb2bd662Svikram 	if (ret == -1) {
5873f64ca102SToomas Soome 		BAM_DPRINTF(("%s: legacy pool %s mount-point %s absent\n",
5874f64ca102SToomas Soome 		    fcn, pool, tmpmnt));
587548847494SEnrico Perla - Sun Microsystems 		ret = mkdirp(tmpmnt, DIR_PERMS);
5876eb2bd662Svikram 		INJECT_ERROR1("Z_MOUNT_TOP_LEG_MNTPT_MKDIRP", ret = -1);
5877eb2bd662Svikram 		if (ret == -1) {
5878f64ca102SToomas Soome 			bam_error(_("mkdir of %s failed: %s\n"), tmpmnt,
5879f64ca102SToomas Soome 			    strerror(errno));
5880eb2bd662Svikram 			return (NULL);
5881eb2bd662Svikram 		}
5882eb2bd662Svikram 	} else {
5883f64ca102SToomas Soome 		BAM_DPRINTF(("%s: legacy pool %s mount-point %s is already "
5884f64ca102SToomas Soome 		    "present\n", fcn, pool, tmpmnt));
5885eb2bd662Svikram 	}
5886eb2bd662Svikram 
5887eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5888eb2bd662Svikram 	    "/sbin/mount -F zfs %s %s",
5889eb2bd662Svikram 	    pool, tmpmnt);
5890eb2bd662Svikram 
5891eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
5892eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_LEG_MOUNT_CMD", ret = 1);
5893eb2bd662Svikram 	if (ret != 0) {
5894f64ca102SToomas Soome 		bam_error(_("mount of ZFS pool %s failed\n"), pool);
5895eb2bd662Svikram 		(void) rmdir(tmpmnt);
5896eb2bd662Svikram 		return (NULL);
5897eb2bd662Svikram 	}
5898eb2bd662Svikram 
5899eb2bd662Svikram 	*mnted = LEGACY_MOUNTED;
5900f64ca102SToomas Soome 	BAM_DPRINTF(("%s: legacy pool %s successfully mounted at %s\n",
5901f64ca102SToomas Soome 	    fcn, pool, tmpmnt));
5902eb2bd662Svikram 	return (s_strdup(tmpmnt));
5903eb2bd662Svikram }
5904eb2bd662Svikram 
5905eb2bd662Svikram /*
5906eb2bd662Svikram  * Mounts the top dataset (if needed)
5907eb2bd662Svikram  * Returns:	The mountpoint of the top dataset or NULL on error
5908eb2bd662Svikram  * 		mnted returns one of the above values defined for zfs_mnted_t
5909eb2bd662Svikram  */
5910f64ca102SToomas Soome char *
5911eb2bd662Svikram mount_top_dataset(char *pool, zfs_mnted_t *mnted)
5912eb2bd662Svikram {
5913eb2bd662Svikram 	char		cmd[PATH_MAX];
5914eb2bd662Svikram 	filelist_t	flist = {0};
5915eb2bd662Svikram 	char		*is_mounted;
5916eb2bd662Svikram 	char		*mntpt;
5917eb2bd662Svikram 	char		*zmntpt;
5918eb2bd662Svikram 	int		ret;
5919eb2bd662Svikram 	const char	*fcn = "mount_top_dataset()";
5920eb2bd662Svikram 
5921eb2bd662Svikram 	*mnted = ZFS_MNT_ERROR;
5922eb2bd662Svikram 
5923f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. arg: %s\n", fcn, pool));
5924eb2bd662Svikram 
5925eb2bd662Svikram 	/*
5926eb2bd662Svikram 	 * First check if the top dataset is a "legacy" dataset
5927eb2bd662Svikram 	 */
5928eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5929eb2bd662Svikram 	    "/sbin/zfs get -Ho value mountpoint %s",
5930eb2bd662Svikram 	    pool);
5931eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5932eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_GET_MNTPT", ret = 1);
5933eb2bd662Svikram 	if (ret != 0) {
5934f64ca102SToomas Soome 		bam_error(_("failed to determine mount point of ZFS pool %s\n"),
5935f64ca102SToomas Soome 		    pool);
5936eb2bd662Svikram 		return (NULL);
5937eb2bd662Svikram 	}
5938eb2bd662Svikram 
5939eb2bd662Svikram 	if (flist.head && (flist.head == flist.tail)) {
5940eb2bd662Svikram 		char *legacy = strtok(flist.head->line, " \t\n");
5941eb2bd662Svikram 		if (legacy && strcmp(legacy, "legacy") == 0) {
5942eb2bd662Svikram 			filelist_free(&flist);
5943f64ca102SToomas Soome 			BAM_DPRINTF(("%s: is legacy, pool=%s\n", fcn, pool));
5944eb2bd662Svikram 			return (mount_legacy_dataset(pool, mnted));
5945eb2bd662Svikram 		}
5946eb2bd662Svikram 	}
5947eb2bd662Svikram 
5948eb2bd662Svikram 	filelist_free(&flist);
5949eb2bd662Svikram 
5950f64ca102SToomas Soome 	BAM_DPRINTF(("%s: is *NOT* legacy, pool=%s\n", fcn, pool));
5951eb2bd662Svikram 
5952eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5953eb2bd662Svikram 	    "/sbin/zfs get -Ho value mounted %s",
5954eb2bd662Svikram 	    pool);
5955eb2bd662Svikram 
5956eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5957eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED", ret = 1);
5958eb2bd662Svikram 	if (ret != 0) {
5959f64ca102SToomas Soome 		bam_error(_("failed to determine mount status of ZFS "
5960f64ca102SToomas Soome 		    "pool %s\n"), pool);
5961eb2bd662Svikram 		return (NULL);
5962eb2bd662Svikram 	}
5963eb2bd662Svikram 
5964eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_VAL", flist.head = NULL);
5965eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5966f64ca102SToomas Soome 		bam_error(_("ZFS pool %s has bad mount status\n"), pool);
5967eb2bd662Svikram 		filelist_free(&flist);
5968eb2bd662Svikram 		return (NULL);
5969eb2bd662Svikram 	}
5970eb2bd662Svikram 
5971eb2bd662Svikram 	is_mounted = strtok(flist.head->line, " \t\n");
5972eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_YES", is_mounted = "yes");
5973eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_NO", is_mounted = "no");
5974eb2bd662Svikram 	if (strcmp(is_mounted, "no") != 0) {
5975eb2bd662Svikram 		filelist_free(&flist);
5976eb2bd662Svikram 		*mnted = ZFS_ALREADY;
5977f64ca102SToomas Soome 		BAM_DPRINTF(("%s: non-legacy pool %s mounted already\n",
5978f64ca102SToomas Soome 		    fcn, pool));
5979eb2bd662Svikram 		goto mounted;
5980eb2bd662Svikram 	}
5981eb2bd662Svikram 
5982eb2bd662Svikram 	filelist_free(&flist);
5983f64ca102SToomas Soome 	BAM_DPRINTF(("%s: non-legacy pool %s *NOT* already mounted\n",
5984f64ca102SToomas Soome 	    fcn, pool));
5985eb2bd662Svikram 
5986eb2bd662Svikram 	/* top dataset is not mounted. Mount it now */
5987eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5988eb2bd662Svikram 	    "/sbin/zfs mount %s", pool);
5989eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
5990eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_MOUNT_CMD", ret = 1);
5991eb2bd662Svikram 	if (ret != 0) {
5992f64ca102SToomas Soome 		bam_error(_("mount of ZFS pool %s failed\n"), pool);
5993eb2bd662Svikram 		return (NULL);
5994eb2bd662Svikram 	}
5995eb2bd662Svikram 	*mnted = ZFS_MOUNTED;
5996f64ca102SToomas Soome 	BAM_DPRINTF(("%s: non-legacy pool %s mounted now\n", fcn, pool));
5997eb2bd662Svikram 	/*FALLTHRU*/
5998eb2bd662Svikram mounted:
5999eb2bd662Svikram 	/*
6000eb2bd662Svikram 	 * Now get the mountpoint
6001eb2bd662Svikram 	 */
6002eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6003eb2bd662Svikram 	    "/sbin/zfs get -Ho value mountpoint %s",
6004eb2bd662Svikram 	    pool);
6005eb2bd662Svikram 
6006eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
6007eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_CMD", ret = 1);
6008eb2bd662Svikram 	if (ret != 0) {
6009f64ca102SToomas Soome 		bam_error(_("failed to determine mount point of ZFS pool %s\n"),
6010f64ca102SToomas Soome 		    pool);
6011eb2bd662Svikram 		goto error;
6012eb2bd662Svikram 	}
6013eb2bd662Svikram 
6014eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_OUT", flist.head = NULL);
6015eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
6016f64ca102SToomas Soome 		bam_error(_("ZFS pool %s has no mount-point\n"), pool);
6017eb2bd662Svikram 		goto error;
6018eb2bd662Svikram 	}
6019eb2bd662Svikram 
6020eb2bd662Svikram 	mntpt = strtok(flist.head->line, " \t\n");
6021eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_STRTOK", mntpt = "foo");
6022eb2bd662Svikram 	if (*mntpt != '/') {
6023f64ca102SToomas Soome 		bam_error(_("ZFS pool %s has bad mount-point %s\n"),
6024f64ca102SToomas Soome 		    pool, mntpt);
6025eb2bd662Svikram 		goto error;
6026eb2bd662Svikram 	}
6027eb2bd662Svikram 	zmntpt = s_strdup(mntpt);
6028eb2bd662Svikram 
6029eb2bd662Svikram 	filelist_free(&flist);
6030eb2bd662Svikram 
6031f64ca102SToomas Soome 	BAM_DPRINTF(("%s: non-legacy pool %s is mounted at %s\n",
6032f64ca102SToomas Soome 	    fcn, pool, zmntpt));
6033eb2bd662Svikram 
6034eb2bd662Svikram 	return (zmntpt);
6035eb2bd662Svikram 
6036eb2bd662Svikram error:
6037eb2bd662Svikram 	filelist_free(&flist);
6038eb2bd662Svikram 	(void) umount_top_dataset(pool, *mnted, NULL);
6039f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
6040eb2bd662Svikram 	return (NULL);
6041eb2bd662Svikram }
6042eb2bd662Svikram 
6043f64ca102SToomas Soome int
6044eb2bd662Svikram umount_top_dataset(char *pool, zfs_mnted_t mnted, char *mntpt)
6045eb2bd662Svikram {
6046eb2bd662Svikram 	char		cmd[PATH_MAX];
6047eb2bd662Svikram 	int		ret;
6048eb2bd662Svikram 	const char	*fcn = "umount_top_dataset()";
6049eb2bd662Svikram 
6050eb2bd662Svikram 	INJECT_ERROR1("Z_UMOUNT_TOP_INVALID_STATE", mnted = ZFS_MNT_ERROR);
6051eb2bd662Svikram 	switch (mnted) {
6052eb2bd662Svikram 	case LEGACY_ALREADY:
6053eb2bd662Svikram 	case ZFS_ALREADY:
6054eb2bd662Svikram 		/* nothing to do */
6055f64ca102SToomas Soome 		BAM_DPRINTF(("%s: pool %s was already mounted at %s, Nothing "
6056f64ca102SToomas Soome 		    "to umount\n", fcn, pool, mntpt ? mntpt : "NULL"));
6057eb2bd662Svikram 		free(mntpt);
6058eb2bd662Svikram 		return (BAM_SUCCESS);
6059eb2bd662Svikram 	case LEGACY_MOUNTED:
6060eb2bd662Svikram 		(void) snprintf(cmd, sizeof (cmd),
6061eb2bd662Svikram 		    "/sbin/umount %s", pool);
6062eb2bd662Svikram 		ret = exec_cmd(cmd, NULL);
6063eb2bd662Svikram 		INJECT_ERROR1("Z_UMOUNT_TOP_LEGACY_UMOUNT_FAIL", ret = 1);
6064eb2bd662Svikram 		if (ret != 0) {
6065f64ca102SToomas Soome 			bam_error(_("umount of %s failed\n"), pool);
6066eb2bd662Svikram 			free(mntpt);
6067eb2bd662Svikram 			return (BAM_ERROR);
6068eb2bd662Svikram 		}
6069eb2bd662Svikram 		if (mntpt)
6070eb2bd662Svikram 			(void) rmdir(mntpt);
6071eb2bd662Svikram 		free(mntpt);
6072f64ca102SToomas Soome 		BAM_DPRINTF(("%s: legacy pool %s was mounted by us, "
6073f64ca102SToomas Soome 		    "successfully unmounted\n", fcn, pool));
6074eb2bd662Svikram 		return (BAM_SUCCESS);
6075eb2bd662Svikram 	case ZFS_MOUNTED:
6076eb2bd662Svikram 		free(mntpt);
6077eb2bd662Svikram 		(void) snprintf(cmd, sizeof (cmd),
6078eb2bd662Svikram 		    "/sbin/zfs unmount %s", pool);
6079eb2bd662Svikram 		ret = exec_cmd(cmd, NULL);
6080eb2bd662Svikram 		INJECT_ERROR1("Z_UMOUNT_TOP_NONLEG_UMOUNT_FAIL", ret = 1);
6081eb2bd662Svikram 		if (ret != 0) {
6082f64ca102SToomas Soome 			bam_error(_("umount of %s failed\n"), pool);
6083eb2bd662Svikram 			return (BAM_ERROR);
6084eb2bd662Svikram 		}
6085f64ca102SToomas Soome 		BAM_DPRINTF(("%s: nonleg pool %s was mounted by us, "
6086f64ca102SToomas Soome 		    "successfully unmounted\n", fcn, pool));
6087eb2bd662Svikram 		return (BAM_SUCCESS);
6088eb2bd662Svikram 	default:
6089f64ca102SToomas Soome 		bam_error(_("Internal error: bad saved mount state for "
6090f64ca102SToomas Soome 		    "pool %s\n"), pool);
6091eb2bd662Svikram 		return (BAM_ERROR);
6092eb2bd662Svikram 	}
6093eb2bd662Svikram 	/*NOTREACHED*/
6094eb2bd662Svikram }
6095eb2bd662Svikram 
6096eb2bd662Svikram /*
6097eb2bd662Svikram  * For ZFS, osdev can be one of two forms
6098eb2bd662Svikram  * It can be a "special" file as seen in mnttab: rpool/ROOT/szboot_0402
6099eb2bd662Svikram  * It can be a /dev/[r]dsk special file. We handle both instances
6100eb2bd662Svikram  */
6101eb2bd662Svikram static char *
6102eb2bd662Svikram get_pool(char *osdev)
6103eb2bd662Svikram {
6104eb2bd662Svikram 	char		cmd[PATH_MAX];
6105eb2bd662Svikram 	char		buf[PATH_MAX];
6106eb2bd662Svikram 	filelist_t	flist = {0};
6107eb2bd662Svikram 	char		*pool;
6108eb2bd662Svikram 	char		*cp;
6109eb2bd662Svikram 	char		*slash;
6110eb2bd662Svikram 	int		ret;
6111eb2bd662Svikram 	const char	*fcn = "get_pool()";
6112eb2bd662Svikram 
6113eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_OSDEV", osdev = NULL);
6114eb2bd662Svikram 	if (osdev == NULL) {
6115f64ca102SToomas Soome 		bam_error(_("NULL device: cannot determine pool name\n"));
6116eb2bd662Svikram 		return (NULL);
6117eb2bd662Svikram 	}
6118eb2bd662Svikram 
6119f64ca102SToomas Soome 	BAM_DPRINTF(("%s: osdev arg = %s\n", fcn, osdev));
6120eb2bd662Svikram 
6121eb2bd662Svikram 	if (osdev[0] != '/') {
6122eb2bd662Svikram 		(void) strlcpy(buf, osdev, sizeof (buf));
6123eb2bd662Svikram 		slash = strchr(buf, '/');
6124eb2bd662Svikram 		if (slash)
6125eb2bd662Svikram 			*slash = '\0';
6126eb2bd662Svikram 		pool = s_strdup(buf);
6127f64ca102SToomas Soome 		BAM_DPRINTF(("%s: got pool. pool = %s\n", fcn, pool));
6128eb2bd662Svikram 		return (pool);
6129eb2bd662Svikram 	} else if (strncmp(osdev, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
6130eb2bd662Svikram 	    strncmp(osdev, "/dev/rdsk/", strlen("/dev/rdsk/")) != 0) {
6131f64ca102SToomas Soome 		bam_error(_("invalid device %s: cannot determine pool name\n"),
6132f64ca102SToomas Soome 		    osdev);
6133eb2bd662Svikram 		return (NULL);
6134eb2bd662Svikram 	}
6135eb2bd662Svikram 
6136d5cb36d3SJan Setje-Eilers 	/*
6137d5cb36d3SJan Setje-Eilers 	 * Call the zfs fstyp directly since this is a zpool. This avoids
6138d5cb36d3SJan Setje-Eilers 	 * potential pcfs conflicts if the first block wasn't cleared.
6139d5cb36d3SJan Setje-Eilers 	 */
6140eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6141d5cb36d3SJan Setje-Eilers 	    "/usr/lib/fs/zfs/fstyp -a %s 2>/dev/null | /bin/grep '^name:'",
6142eb2bd662Svikram 	    osdev);
6143eb2bd662Svikram 
6144eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
6145eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_FSTYP", ret = 1);
6146eb2bd662Svikram 	if (ret != 0) {
6147f64ca102SToomas Soome 		bam_error(_("fstyp -a on device %s failed\n"), osdev);
6148eb2bd662Svikram 		return (NULL);
6149eb2bd662Svikram 	}
6150eb2bd662Svikram 
6151eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_FSTYP_OUT", flist.head = NULL);
6152eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
6153f64ca102SToomas Soome 		bam_error(_("NULL fstyp -a output for device %s\n"), osdev);
6154eb2bd662Svikram 		filelist_free(&flist);
6155eb2bd662Svikram 		return (NULL);
6156eb2bd662Svikram 	}
6157eb2bd662Svikram 
6158eb2bd662Svikram 	(void) strtok(flist.head->line, "'");
6159eb2bd662Svikram 	cp = strtok(NULL, "'");
6160eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_FSTYP_STRTOK", cp = NULL);
6161eb2bd662Svikram 	if (cp == NULL) {
6162f64ca102SToomas Soome 		bam_error(_("bad fstyp -a output for device %s\n"), osdev);
6163eb2bd662Svikram 		filelist_free(&flist);
6164eb2bd662Svikram 		return (NULL);
6165eb2bd662Svikram 	}
6166eb2bd662Svikram 
6167eb2bd662Svikram 	pool = s_strdup(cp);
6168eb2bd662Svikram 
6169eb2bd662Svikram 	filelist_free(&flist);
6170eb2bd662Svikram 
6171f64ca102SToomas Soome 	BAM_DPRINTF(("%s: got pool. pool = %s\n", fcn, pool));
6172eb2bd662Svikram 
6173eb2bd662Svikram 	return (pool);
6174eb2bd662Svikram }
6175eb2bd662Svikram 
6176eb2bd662Svikram static char *
6177eb2bd662Svikram find_zfs_existing(char *osdev)
6178eb2bd662Svikram {
6179eb2bd662Svikram 	char		*pool;
6180eb2bd662Svikram 	zfs_mnted_t	mnted;
6181eb2bd662Svikram 	char		*mntpt;
6182eb2bd662Svikram 	char		*sign;
6183eb2bd662Svikram 	const char	*fcn = "find_zfs_existing()";
6184eb2bd662Svikram 
6185eb2bd662Svikram 	pool = get_pool(osdev);
6186eb2bd662Svikram 	INJECT_ERROR1("ZFS_FIND_EXIST_POOL", pool = NULL);
6187eb2bd662Svikram 	if (pool == NULL) {
6188f64ca102SToomas Soome 		bam_error(_("failed to get pool for device: %s\n"), osdev);
6189eb2bd662Svikram 		return (NULL);
6190eb2bd662Svikram 	}
6191eb2bd662Svikram 
6192eb2bd662Svikram 	mntpt = mount_top_dataset(pool, &mnted);
6193eb2bd662Svikram 	INJECT_ERROR1("ZFS_FIND_EXIST_MOUNT_TOP", mntpt = NULL);
6194eb2bd662Svikram 	if (mntpt == NULL) {
6195f64ca102SToomas Soome 		bam_error(_("failed to mount top dataset for pool: %s\n"),
6196f64ca102SToomas Soome 		    pool);
6197eb2bd662Svikram 		free(pool);
6198eb2bd662Svikram 		return (NULL);
6199eb2bd662Svikram 	}
6200eb2bd662Svikram 
6201eb2bd662Svikram 	sign = find_primary_common(mntpt, "zfs");
6202eb2bd662Svikram 	if (sign == NULL) {
6203eb2bd662Svikram 		sign = find_backup_common(mntpt, "zfs");
6204f64ca102SToomas Soome 		BAM_DPRINTF(("%s: existing backup sign: %s\n", fcn,
6205f64ca102SToomas Soome 		    sign ? sign : "NULL"));
6206eb2bd662Svikram 	} else {
6207f64ca102SToomas Soome 		BAM_DPRINTF(("%s: existing primary sign: %s\n", fcn, sign));
6208eb2bd662Svikram 	}
6209eb2bd662Svikram 
6210eb2bd662Svikram 	(void) umount_top_dataset(pool, mnted, mntpt);
6211eb2bd662Svikram 
6212eb2bd662Svikram 	free(pool);
6213eb2bd662Svikram 
6214eb2bd662Svikram 	return (sign);
6215eb2bd662Svikram }
6216eb2bd662Svikram 
6217eb2bd662Svikram static char *
6218eb2bd662Svikram find_existing_sign(char *osroot, char *osdev, char *fstype)
6219eb2bd662Svikram {
6220eb2bd662Svikram 	const char		*fcn = "find_existing_sign()";
6221eb2bd662Svikram 
6222eb2bd662Svikram 	INJECT_ERROR1("FIND_EXIST_NOTSUP_FS", fstype = "foofs");
6223eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0) {
6224f64ca102SToomas Soome 		BAM_DPRINTF(("%s: checking for existing UFS sign\n", fcn));
6225eb2bd662Svikram 		return (find_ufs_existing(osroot));
6226eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0) {
6227f64ca102SToomas Soome 		BAM_DPRINTF(("%s: checking for existing ZFS sign\n", fcn));
6228eb2bd662Svikram 		return (find_zfs_existing(osdev));
6229eb2bd662Svikram 	} else {
6230f64ca102SToomas Soome 		bam_error(_("boot signature not supported for fstype: %s\n"),
6231f64ca102SToomas Soome 		    fstype);
6232eb2bd662Svikram 		return (NULL);
6233eb2bd662Svikram 	}
6234eb2bd662Svikram }
6235eb2bd662Svikram 
6236eb2bd662Svikram #define	MH_HASH_SZ	16
6237eb2bd662Svikram 
6238eb2bd662Svikram typedef enum {
6239eb2bd662Svikram 	MH_ERROR = -1,
6240eb2bd662Svikram 	MH_NOMATCH,
6241eb2bd662Svikram 	MH_MATCH
6242eb2bd662Svikram } mh_search_t;
6243eb2bd662Svikram 
6244eb2bd662Svikram typedef struct mcache {
6245eb2bd662Svikram 	char	*mc_special;
6246eb2bd662Svikram 	char	*mc_mntpt;
6247eb2bd662Svikram 	char	*mc_fstype;
6248eb2bd662Svikram 	struct mcache *mc_next;
6249eb2bd662Svikram } mcache_t;
6250eb2bd662Svikram 
6251eb2bd662Svikram typedef struct mhash {
6252eb2bd662Svikram 	mcache_t *mh_hash[MH_HASH_SZ];
6253eb2bd662Svikram } mhash_t;
6254eb2bd662Svikram 
6255eb2bd662Svikram static int
6256eb2bd662Svikram mhash_fcn(char *key)
6257eb2bd662Svikram {
6258eb2bd662Svikram 	int		i;
6259eb2bd662Svikram 	uint64_t	sum = 0;
6260eb2bd662Svikram 
6261eb2bd662Svikram 	for (i = 0; key[i] != '\0'; i++) {
6262eb2bd662Svikram 		sum += (uchar_t)key[i];
6263eb2bd662Svikram 	}
6264eb2bd662Svikram 
6265eb2bd662Svikram 	sum %= MH_HASH_SZ;
6266eb2bd662Svikram 
6267eb2bd662Svikram 	assert(sum < MH_HASH_SZ);
6268eb2bd662Svikram 
6269eb2bd662Svikram 	return (sum);
6270eb2bd662Svikram }
6271eb2bd662Svikram 
6272eb2bd662Svikram static mhash_t *
6273eb2bd662Svikram cache_mnttab(void)
6274eb2bd662Svikram {
6275eb2bd662Svikram 	FILE		*mfp;
6276eb2bd662Svikram 	struct extmnttab mnt;
6277eb2bd662Svikram 	mcache_t	*mcp;
6278eb2bd662Svikram 	mhash_t		*mhp;
6279eb2bd662Svikram 	char		*ctds;
6280eb2bd662Svikram 	int		idx;
6281eb2bd662Svikram 	int		error;
6282eb2bd662Svikram 	char		*special_dup;
6283eb2bd662Svikram 	const char	*fcn = "cache_mnttab()";
6284eb2bd662Svikram 
6285eb2bd662Svikram 	mfp = fopen(MNTTAB, "r");
6286eb2bd662Svikram 	error = errno;
6287eb2bd662Svikram 	INJECT_ERROR1("CACHE_MNTTAB_MNTTAB_ERR", mfp = NULL);
6288eb2bd662Svikram 	if (mfp == NULL) {
6289f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), MNTTAB,
6290f64ca102SToomas Soome 		    strerror(error));
6291eb2bd662Svikram 		return (NULL);
6292eb2bd662Svikram 	}
6293eb2bd662Svikram 
6294eb2bd662Svikram 	mhp = s_calloc(1, sizeof (mhash_t));
6295eb2bd662Svikram 
6296eb2bd662Svikram 	resetmnttab(mfp);
6297eb2bd662Svikram 
6298eb2bd662Svikram 	while (getextmntent(mfp, &mnt, sizeof (mnt)) == 0) {
6299eb2bd662Svikram 		/* only cache ufs */
6300eb2bd662Svikram 		if (strcmp(mnt.mnt_fstype, "ufs") != 0)
6301eb2bd662Svikram 			continue;
6302eb2bd662Svikram 
6303eb2bd662Svikram 		/* basename() modifies its arg, so dup it */
6304eb2bd662Svikram 		special_dup = s_strdup(mnt.mnt_special);
6305eb2bd662Svikram 		ctds = basename(special_dup);
6306eb2bd662Svikram 
6307eb2bd662Svikram 		mcp = s_calloc(1, sizeof (mcache_t));
6308eb2bd662Svikram 		mcp->mc_special = s_strdup(ctds);
6309eb2bd662Svikram 		mcp->mc_mntpt = s_strdup(mnt.mnt_mountp);
6310eb2bd662Svikram 		mcp->mc_fstype = s_strdup(mnt.mnt_fstype);
6311f64ca102SToomas Soome 		BAM_DPRINTF(("%s: caching mount: special=%s, mntpt=%s, "
6312f64ca102SToomas Soome 		    "fstype=%s\n", fcn, ctds, mnt.mnt_mountp, mnt.mnt_fstype));
6313eb2bd662Svikram 		idx = mhash_fcn(ctds);
6314eb2bd662Svikram 		mcp->mc_next = mhp->mh_hash[idx];
6315eb2bd662Svikram 		mhp->mh_hash[idx] = mcp;
6316eb2bd662Svikram 		free(special_dup);
6317eb2bd662Svikram 	}
6318eb2bd662Svikram 
6319eb2bd662Svikram 	(void) fclose(mfp);
6320eb2bd662Svikram 
6321eb2bd662Svikram 	return (mhp);
6322eb2bd662Svikram }
6323eb2bd662Svikram 
6324eb2bd662Svikram static void
6325eb2bd662Svikram free_mnttab(mhash_t *mhp)
6326eb2bd662Svikram {
6327eb2bd662Svikram 	mcache_t	*mcp;
6328eb2bd662Svikram 	int		i;
6329eb2bd662Svikram 
6330eb2bd662Svikram 	for (i = 0; i < MH_HASH_SZ; i++) {
6331110570ceSToomas Soome 		while ((mcp = mhp->mh_hash[i]) != NULL) {
6332eb2bd662Svikram 			mhp->mh_hash[i] = mcp->mc_next;
6333eb2bd662Svikram 			free(mcp->mc_special);
6334eb2bd662Svikram 			free(mcp->mc_mntpt);
6335eb2bd662Svikram 			free(mcp->mc_fstype);
6336eb2bd662Svikram 			free(mcp);
6337eb2bd662Svikram 		}
6338eb2bd662Svikram 	}
6339eb2bd662Svikram 
6340eb2bd662Svikram 	for (i = 0; i < MH_HASH_SZ; i++) {
6341eb2bd662Svikram 		assert(mhp->mh_hash[i] == NULL);
6342eb2bd662Svikram 	}
6343eb2bd662Svikram 	free(mhp);
6344eb2bd662Svikram }
6345eb2bd662Svikram 
6346eb2bd662Svikram static mh_search_t
6347eb2bd662Svikram search_hash(mhash_t *mhp, char *special, char **mntpt)
6348eb2bd662Svikram {
6349eb2bd662Svikram 	int		idx;
6350eb2bd662Svikram 	mcache_t	*mcp;
6351eb2bd662Svikram 	const char 	*fcn = "search_hash()";
6352eb2bd662Svikram 
6353eb2bd662Svikram 	assert(mntpt);
6354eb2bd662Svikram 
6355eb2bd662Svikram 	*mntpt = NULL;
6356eb2bd662Svikram 
6357eb2bd662Svikram 	INJECT_ERROR1("SEARCH_HASH_FULL_PATH", special = "/foo");
6358eb2bd662Svikram 	if (strchr(special, '/')) {
6359f64ca102SToomas Soome 		bam_error(_("invalid key for mnttab hash: %s\n"), special);
6360eb2bd662Svikram 		return (MH_ERROR);
6361eb2bd662Svikram 	}
6362eb2bd662Svikram 
6363eb2bd662Svikram 	idx = mhash_fcn(special);
6364eb2bd662Svikram 
6365eb2bd662Svikram 	for (mcp = mhp->mh_hash[idx]; mcp; mcp = mcp->mc_next) {
6366eb2bd662Svikram 		if (strcmp(mcp->mc_special, special) == 0)
6367eb2bd662Svikram 			break;
6368eb2bd662Svikram 	}
6369eb2bd662Svikram 
6370eb2bd662Svikram 	if (mcp == NULL) {
6371f64ca102SToomas Soome 		BAM_DPRINTF(("%s: no match in cache for: %s\n", fcn, special));
6372eb2bd662Svikram 		return (MH_NOMATCH);
6373eb2bd662Svikram 	}
6374eb2bd662Svikram 
6375eb2bd662Svikram 	assert(strcmp(mcp->mc_fstype, "ufs") == 0);
6376eb2bd662Svikram 	*mntpt = mcp->mc_mntpt;
6377f64ca102SToomas Soome 	BAM_DPRINTF(("%s: *MATCH* in cache for: %s\n", fcn, special));
6378eb2bd662Svikram 	return (MH_MATCH);
6379eb2bd662Svikram }
6380eb2bd662Svikram 
6381eb2bd662Svikram static int
6382eb2bd662Svikram check_add_ufs_sign_to_list(FILE *tfp, char *mntpt)
6383eb2bd662Svikram {
6384eb2bd662Svikram 	char		*sign;
6385eb2bd662Svikram 	char		*signline;
6386eb2bd662Svikram 	char		signbuf[MAXNAMELEN];
6387eb2bd662Svikram 	int		len;
6388eb2bd662Svikram 	int		error;
6389eb2bd662Svikram 	const char	*fcn = "check_add_ufs_sign_to_list()";
6390eb2bd662Svikram 
6391eb2bd662Svikram 	/* safe to specify NULL as "osdev" arg for UFS */
6392eb2bd662Svikram 	sign = find_existing_sign(mntpt, NULL, "ufs");
6393eb2bd662Svikram 	if (sign == NULL) {
6394eb2bd662Svikram 		/* No existing signature, nothing to add to list */
6395f64ca102SToomas Soome 		BAM_DPRINTF(("%s: no sign on %s to add to signlist\n",
6396f64ca102SToomas Soome 		    fcn, mntpt));
6397eb2bd662Svikram 		return (0);
6398eb2bd662Svikram 	}
6399eb2bd662Svikram 
6400eb2bd662Svikram 	(void) snprintf(signbuf, sizeof (signbuf), "%s\n", sign);
6401eb2bd662Svikram 	signline = signbuf;
6402eb2bd662Svikram 
6403eb2bd662Svikram 	INJECT_ERROR1("UFS_MNTPT_SIGN_NOTUFS", signline = "pool_rpool10\n");
6404eb2bd662Svikram 	if (strncmp(signline, GRUBSIGN_UFS_PREFIX,
6405eb2bd662Svikram 	    strlen(GRUBSIGN_UFS_PREFIX))) {
6406f64ca102SToomas Soome 		bam_error(_("invalid UFS boot signature %s\n"), sign);
6407eb2bd662Svikram 		free(sign);
6408eb2bd662Svikram 		/* ignore invalid signatures */
6409eb2bd662Svikram 		return (0);
6410eb2bd662Svikram 	}
6411eb2bd662Svikram 
6412eb2bd662Svikram 	len = fputs(signline, tfp);
6413eb2bd662Svikram 	error = errno;
6414eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_PUTS_ERROR", len = 0);
6415eb2bd662Svikram 	if (len != strlen(signline)) {
6416f64ca102SToomas Soome 		bam_error(_("failed to write signature %s to signature "
6417f64ca102SToomas Soome 		    "list: %s\n"), sign, strerror(error));
6418eb2bd662Svikram 		free(sign);
6419eb2bd662Svikram 		return (-1);
6420eb2bd662Svikram 	}
6421eb2bd662Svikram 
6422eb2bd662Svikram 	free(sign);
6423eb2bd662Svikram 
6424f64ca102SToomas Soome 	BAM_DPRINTF(("%s: successfully added sign on %s to signlist\n",
6425f64ca102SToomas Soome 	    fcn, mntpt));
6426eb2bd662Svikram 	return (0);
6427eb2bd662Svikram }
6428eb2bd662Svikram 
6429eb2bd662Svikram /*
6430eb2bd662Svikram  * slice is a basename not a full pathname
6431eb2bd662Svikram  */
6432eb2bd662Svikram static int
6433eb2bd662Svikram process_slice_common(char *slice, FILE *tfp, mhash_t *mhp, char *tmpmnt)
6434eb2bd662Svikram {
6435eb2bd662Svikram 	int		ret;
6436eb2bd662Svikram 	char		cmd[PATH_MAX];
6437eb2bd662Svikram 	char		path[PATH_MAX];
6438eb2bd662Svikram 	struct stat	sbuf;
6439eb2bd662Svikram 	char		*mntpt;
6440eb2bd662Svikram 	filelist_t	flist = {0};
6441eb2bd662Svikram 	char		*fstype;
6442eb2bd662Svikram 	char		blkslice[PATH_MAX];
6443eb2bd662Svikram 	const char	*fcn = "process_slice_common()";
6444eb2bd662Svikram 
6445eb2bd662Svikram 
6446eb2bd662Svikram 	ret = search_hash(mhp, slice, &mntpt);
6447eb2bd662Svikram 	switch (ret) {
6448eb2bd662Svikram 		case MH_MATCH:
6449eb2bd662Svikram 			if (check_add_ufs_sign_to_list(tfp, mntpt) == -1)
6450eb2bd662Svikram 				return (-1);
6451eb2bd662Svikram 			else
6452eb2bd662Svikram 				return (0);
6453eb2bd662Svikram 		case MH_NOMATCH:
6454eb2bd662Svikram 			break;
6455eb2bd662Svikram 		case MH_ERROR:
6456eb2bd662Svikram 		default:
6457eb2bd662Svikram 			return (-1);
6458eb2bd662Svikram 	}
6459eb2bd662Svikram 
6460eb2bd662Svikram 	(void) snprintf(path, sizeof (path), "/dev/rdsk/%s", slice);
6461eb2bd662Svikram 	if (stat(path, &sbuf) == -1) {
6462f64ca102SToomas Soome 		BAM_DPRINTF(("%s: slice does not exist: %s\n", fcn, path));
6463eb2bd662Svikram 		return (0);
6464eb2bd662Svikram 	}
6465eb2bd662Svikram 
6466d5cb36d3SJan Setje-Eilers 	/* Check if ufs. Call ufs fstyp directly to avoid pcfs conflicts. */
6467eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6468d5cb36d3SJan Setje-Eilers 	    "/usr/lib/fs/ufs/fstyp /dev/rdsk/%s 2>/dev/null",
6469eb2bd662Svikram 	    slice);
6470eb2bd662Svikram 
6471eb2bd662Svikram 	if (exec_cmd(cmd, &flist) != 0) {
6472eb2bd662Svikram 		if (bam_verbose)
6473f64ca102SToomas Soome 			bam_print(_("fstyp failed for slice: %s\n"), slice);
6474eb2bd662Svikram 		return (0);
6475eb2bd662Svikram 	}
6476eb2bd662Svikram 
6477eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
6478eb2bd662Svikram 		if (bam_verbose)
6479f64ca102SToomas Soome 			bam_print(_("bad output from fstyp for slice: %s\n"),
6480f64ca102SToomas Soome 			    slice);
6481eb2bd662Svikram 		filelist_free(&flist);
6482eb2bd662Svikram 		return (0);
6483eb2bd662Svikram 	}
6484eb2bd662Svikram 
6485eb2bd662Svikram 	fstype = strtok(flist.head->line, " \t\n");
6486eb2bd662Svikram 	if (fstype == NULL || strcmp(fstype, "ufs") != 0) {
6487eb2bd662Svikram 		if (bam_verbose)
6488f64ca102SToomas Soome 			bam_print(_("%s is not a ufs slice: %s\n"),
6489f64ca102SToomas Soome 			    slice, fstype);
6490eb2bd662Svikram 		filelist_free(&flist);
6491eb2bd662Svikram 		return (0);
6492eb2bd662Svikram 	}
6493eb2bd662Svikram 
6494eb2bd662Svikram 	filelist_free(&flist);
6495eb2bd662Svikram 
6496eb2bd662Svikram 	/*
6497eb2bd662Svikram 	 * Since we are mounting the filesystem read-only, the
6498eb2bd662Svikram 	 * the last mount field of the superblock is unchanged
6499eb2bd662Svikram 	 * and does not need to be fixed up post-mount;
6500eb2bd662Svikram 	 */
6501eb2bd662Svikram 
6502eb2bd662Svikram 	(void) snprintf(blkslice, sizeof (blkslice), "/dev/dsk/%s",
6503eb2bd662Svikram 	    slice);
6504eb2bd662Svikram 
6505eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6506eb2bd662Svikram 	    "/usr/sbin/mount -F ufs -o ro %s %s "
6507eb2bd662Svikram 	    "> /dev/null 2>&1", blkslice, tmpmnt);
6508eb2bd662Svikram 
6509eb2bd662Svikram 	if (exec_cmd(cmd, NULL) != 0) {
6510eb2bd662Svikram 		if (bam_verbose)
6511f64ca102SToomas Soome 			bam_print(_("mount of %s (fstype %s) failed\n"),
6512f64ca102SToomas Soome 			    blkslice, "ufs");
6513eb2bd662Svikram 		return (0);
6514eb2bd662Svikram 	}
6515eb2bd662Svikram 
6516eb2bd662Svikram 	ret = check_add_ufs_sign_to_list(tfp, tmpmnt);
6517eb2bd662Svikram 
6518eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6519eb2bd662Svikram 	    "/usr/sbin/umount -f %s > /dev/null 2>&1",
6520eb2bd662Svikram 	    tmpmnt);
6521eb2bd662Svikram 
6522eb2bd662Svikram 	if (exec_cmd(cmd, NULL) != 0) {
6523f64ca102SToomas Soome 		bam_print(_("umount of %s failed\n"), slice);
6524eb2bd662Svikram 		return (0);
6525eb2bd662Svikram 	}
6526eb2bd662Svikram 
6527eb2bd662Svikram 	return (ret);
6528eb2bd662Svikram }
6529eb2bd662Svikram 
6530eb2bd662Svikram static int
6531eb2bd662Svikram process_vtoc_slices(
6532eb2bd662Svikram 	char *s0,
6533eb2bd662Svikram 	struct vtoc *vtoc,
6534eb2bd662Svikram 	FILE *tfp,
6535eb2bd662Svikram 	mhash_t *mhp,
6536eb2bd662Svikram 	char *tmpmnt)
6537eb2bd662Svikram {
6538eb2bd662Svikram 	int		idx;
6539eb2bd662Svikram 	char		slice[PATH_MAX];
6540eb2bd662Svikram 	size_t		len;
6541eb2bd662Svikram 	char		*cp;
6542eb2bd662Svikram 	const char	*fcn = "process_vtoc_slices()";
6543eb2bd662Svikram 
6544eb2bd662Svikram 	len = strlen(s0);
6545eb2bd662Svikram 
6546eb2bd662Svikram 	assert(s0[len - 2] == 's' && s0[len - 1] == '0');
6547eb2bd662Svikram 
6548eb2bd662Svikram 	s0[len - 1] = '\0';
6549eb2bd662Svikram 
6550eb2bd662Svikram 	(void) strlcpy(slice, s0, sizeof (slice));
6551eb2bd662Svikram 
6552eb2bd662Svikram 	s0[len - 1] = '0';
6553eb2bd662Svikram 
6554eb2bd662Svikram 	cp = slice + len - 1;
6555eb2bd662Svikram 
6556eb2bd662Svikram 	for (idx = 0; idx < vtoc->v_nparts; idx++) {
6557eb2bd662Svikram 
6558eb2bd662Svikram 		(void) snprintf(cp, sizeof (slice) - (len - 1), "%u", idx);
6559eb2bd662Svikram 
6560eb2bd662Svikram 		if (vtoc->v_part[idx].p_size == 0) {
6561f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: skipping 0-length slice: %s\n",
6562f64ca102SToomas Soome 			    fcn, slice));
6563eb2bd662Svikram 			continue;
6564eb2bd662Svikram 		}
6565eb2bd662Svikram 
6566eb2bd662Svikram 		/* Skip "SWAP", "USR", "BACKUP", "VAR", "HOME", "ALTSCTR" */
6567eb2bd662Svikram 		switch (vtoc->v_part[idx].p_tag) {
6568eb2bd662Svikram 		case V_SWAP:
6569eb2bd662Svikram 		case V_USR:
6570eb2bd662Svikram 		case V_BACKUP:
6571eb2bd662Svikram 		case V_VAR:
6572eb2bd662Svikram 		case V_HOME:
6573eb2bd662Svikram 		case V_ALTSCTR:
6574f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: unsupported tag, "
6575f64ca102SToomas Soome 			    "skipping: %s\n", fcn, slice));
6576eb2bd662Svikram 			continue;
6577eb2bd662Svikram 		default:
6578f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: supported tag, checking: %s\n",
6579f64ca102SToomas Soome 			    fcn, slice));
6580eb2bd662Svikram 			break;
6581eb2bd662Svikram 		}
6582eb2bd662Svikram 
6583eb2bd662Svikram 		/* skip unmountable and readonly slices */
6584eb2bd662Svikram 		switch (vtoc->v_part[idx].p_flag) {
6585eb2bd662Svikram 		case V_UNMNT:
6586eb2bd662Svikram 		case V_RONLY:
6587f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: non-RDWR flag, skipping: %s\n",
6588f64ca102SToomas Soome 			    fcn, slice));
6589eb2bd662Svikram 			continue;
6590eb2bd662Svikram 		default:
6591f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: RDWR flag, checking: %s\n",
6592f64ca102SToomas Soome 			    fcn, slice));
6593eb2bd662Svikram 			break;
6594eb2bd662Svikram 		}
6595eb2bd662Svikram 
6596eb2bd662Svikram 		if (process_slice_common(slice, tfp, mhp, tmpmnt) == -1) {
6597eb2bd662Svikram 			return (-1);
6598eb2bd662Svikram 		}
6599eb2bd662Svikram 	}
6600eb2bd662Svikram 
6601eb2bd662Svikram 	return (0);
6602eb2bd662Svikram }
6603eb2bd662Svikram 
6604eb2bd662Svikram static int
6605eb2bd662Svikram process_efi_slices(
6606eb2bd662Svikram 	char *s0,
6607eb2bd662Svikram 	struct dk_gpt *efi,
6608eb2bd662Svikram 	FILE *tfp,
6609eb2bd662Svikram 	mhash_t *mhp,
6610eb2bd662Svikram 	char *tmpmnt)
6611eb2bd662Svikram {
6612eb2bd662Svikram 	int		idx;
6613eb2bd662Svikram 	char		slice[PATH_MAX];
6614eb2bd662Svikram 	size_t		len;
6615eb2bd662Svikram 	char		*cp;
6616eb2bd662Svikram 	const char	*fcn = "process_efi_slices()";
6617eb2bd662Svikram 
6618eb2bd662Svikram 	len = strlen(s0);
6619eb2bd662Svikram 
6620eb2bd662Svikram 	assert(s0[len - 2] == 's' && s0[len - 1] == '0');
6621eb2bd662Svikram 
6622eb2bd662Svikram 	s0[len - 1] = '\0';
6623eb2bd662Svikram 
6624eb2bd662Svikram 	(void) strlcpy(slice, s0, sizeof (slice));
6625eb2bd662Svikram 
6626eb2bd662Svikram 	s0[len - 1] = '0';
6627eb2bd662Svikram 
6628eb2bd662Svikram 	cp = slice + len - 1;
6629eb2bd662Svikram 
6630eb2bd662Svikram 	for (idx = 0; idx < efi->efi_nparts; idx++) {
6631eb2bd662Svikram 
6632eb2bd662Svikram 		(void) snprintf(cp, sizeof (slice) - (len - 1), "%u", idx);
6633eb2bd662Svikram 
6634eb2bd662Svikram 		if (efi->efi_parts[idx].p_size == 0) {
6635f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: skipping 0-length slice: %s\n",
6636f64ca102SToomas Soome 			    fcn, slice));
6637eb2bd662Svikram 			continue;
6638eb2bd662Svikram 		}
6639eb2bd662Svikram 
6640eb2bd662Svikram 		/* Skip "SWAP", "USR", "BACKUP", "VAR", "HOME", "ALTSCTR" */
6641eb2bd662Svikram 		switch (efi->efi_parts[idx].p_tag) {
6642eb2bd662Svikram 		case V_SWAP:
6643eb2bd662Svikram 		case V_USR:
6644eb2bd662Svikram 		case V_BACKUP:
6645eb2bd662Svikram 		case V_VAR:
6646eb2bd662Svikram 		case V_HOME:
6647eb2bd662Svikram 		case V_ALTSCTR:
6648f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: unsupported tag, skipping: %s\n",
6649f64ca102SToomas Soome 			    fcn, slice));
6650eb2bd662Svikram 			continue;
6651eb2bd662Svikram 		default:
6652f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: supported tag, checking: %s\n",
6653f64ca102SToomas Soome 			    fcn, slice));
6654eb2bd662Svikram 			break;
6655eb2bd662Svikram 		}
6656eb2bd662Svikram 
6657eb2bd662Svikram 		/* skip unmountable and readonly slices */
6658eb2bd662Svikram 		switch (efi->efi_parts[idx].p_flag) {
6659eb2bd662Svikram 		case V_UNMNT:
6660eb2bd662Svikram 		case V_RONLY:
6661f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: non-RDWR flag, skipping: %s\n",
6662f64ca102SToomas Soome 			    fcn, slice));
6663eb2bd662Svikram 			continue;
6664eb2bd662Svikram 		default:
6665f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: RDWR flag, checking: %s\n",
6666f64ca102SToomas Soome 			    fcn, slice));
6667eb2bd662Svikram 			break;
6668eb2bd662Svikram 		}
6669eb2bd662Svikram 
6670eb2bd662Svikram 		if (process_slice_common(slice, tfp, mhp, tmpmnt) == -1) {
6671eb2bd662Svikram 			return (-1);
6672eb2bd662Svikram 		}
6673eb2bd662Svikram 	}
6674eb2bd662Svikram 
6675eb2bd662Svikram 	return (0);
6676eb2bd662Svikram }
6677eb2bd662Svikram 
6678eb2bd662Svikram /*
6679eb2bd662Svikram  * s0 is a basename not a full path
6680eb2bd662Svikram  */
6681eb2bd662Svikram static int
6682eb2bd662Svikram process_slice0(char *s0, FILE *tfp, mhash_t *mhp, char *tmpmnt)
6683eb2bd662Svikram {
6684eb2bd662Svikram 	struct vtoc		vtoc;
6685eb2bd662Svikram 	struct dk_gpt		*efi;
6686eb2bd662Svikram 	char			s0path[PATH_MAX];
6687eb2bd662Svikram 	struct stat		sbuf;
6688eb2bd662Svikram 	int			e_flag;
6689eb2bd662Svikram 	int			v_flag;
6690eb2bd662Svikram 	int			retval;
6691eb2bd662Svikram 	int			err;
6692eb2bd662Svikram 	int			fd;
6693eb2bd662Svikram 	const char		*fcn = "process_slice0()";
6694eb2bd662Svikram 
6695eb2bd662Svikram 	(void) snprintf(s0path, sizeof (s0path), "/dev/rdsk/%s", s0);
6696eb2bd662Svikram 
6697eb2bd662Svikram 	if (stat(s0path, &sbuf) == -1) {
6698f64ca102SToomas Soome 		BAM_DPRINTF(("%s: slice 0 does not exist: %s\n", fcn, s0path));
6699eb2bd662Svikram 		return (0);
6700eb2bd662Svikram 	}
6701eb2bd662Svikram 
6702eb2bd662Svikram 	fd = open(s0path, O_NONBLOCK|O_RDONLY);
6703eb2bd662Svikram 	if (fd == -1) {
6704f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), s0path,
6705f64ca102SToomas Soome 		    strerror(errno));
6706eb2bd662Svikram 		return (0);
6707eb2bd662Svikram 	}
6708eb2bd662Svikram 
6709eb2bd662Svikram 	e_flag = v_flag = 0;
6710eb2bd662Svikram 	retval = ((err = read_vtoc(fd, &vtoc)) >= 0) ? 0 : err;
6711eb2bd662Svikram 	switch (retval) {
6712eb2bd662Svikram 		case VT_EIO:
6713f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: failed to read: %s\n",
6714f64ca102SToomas Soome 			    fcn, s0path));
6715eb2bd662Svikram 			break;
6716eb2bd662Svikram 		case VT_EINVAL:
6717f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: is INVALID: %s\n",
6718f64ca102SToomas Soome 			    fcn, s0path));
6719eb2bd662Svikram 			break;
6720eb2bd662Svikram 		case VT_ERROR:
6721f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: unknown error while "
6722f64ca102SToomas Soome 			    "reading: %s\n", fcn, s0path));
6723eb2bd662Svikram 			break;
6724eb2bd662Svikram 		case VT_ENOTSUP:
6725eb2bd662Svikram 			e_flag = 1;
6726f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: not supported: %s\n",
6727f64ca102SToomas Soome 			    fcn, s0path));
6728eb2bd662Svikram 			break;
6729eb2bd662Svikram 		case 0:
6730eb2bd662Svikram 			v_flag = 1;
6731f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: SUCCESS reading: %s\n",
6732f64ca102SToomas Soome 			    fcn, s0path));
6733eb2bd662Svikram 			break;
6734eb2bd662Svikram 		default:
6735f64ca102SToomas Soome 			BAM_DPRINTF(("%s: VTOC: READ: unknown return "
6736f64ca102SToomas Soome 			    "code: %s\n", fcn, s0path));
6737eb2bd662Svikram 			break;
6738eb2bd662Svikram 	}
6739eb2bd662Svikram 
6740eb2bd662Svikram 
6741eb2bd662Svikram 	if (e_flag) {
6742eb2bd662Svikram 		e_flag = 0;
6743eb2bd662Svikram 		retval = ((err = efi_alloc_and_read(fd, &efi)) >= 0) ? 0 : err;
6744eb2bd662Svikram 		switch (retval) {
6745eb2bd662Svikram 		case VT_EIO:
6746f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: failed to read: %s\n",
6747f64ca102SToomas Soome 			    fcn, s0path));
6748eb2bd662Svikram 			break;
6749eb2bd662Svikram 		case VT_EINVAL:
6750f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: is INVALID: %s\n", fcn, s0path));
6751eb2bd662Svikram 			break;
6752eb2bd662Svikram 		case VT_ERROR:
6753f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: unknown error while "
6754f64ca102SToomas Soome 			    "reading: %s\n", fcn, s0path));
6755eb2bd662Svikram 			break;
6756eb2bd662Svikram 		case VT_ENOTSUP:
6757f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: not supported: %s\n",
6758f64ca102SToomas Soome 			    fcn, s0path));
6759eb2bd662Svikram 			break;
6760eb2bd662Svikram 		case 0:
6761eb2bd662Svikram 			e_flag = 1;
6762f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: SUCCESS reading: %s\n",
6763f64ca102SToomas Soome 			    fcn, s0path));
6764eb2bd662Svikram 			break;
6765eb2bd662Svikram 		default:
6766f64ca102SToomas Soome 			BAM_DPRINTF(("%s: EFI: READ: unknown return code: %s\n",
6767f64ca102SToomas Soome 			    fcn, s0path));
6768eb2bd662Svikram 			break;
6769eb2bd662Svikram 		}
6770eb2bd662Svikram 	}
6771eb2bd662Svikram 
6772eb2bd662Svikram 	(void) close(fd);
6773eb2bd662Svikram 
6774eb2bd662Svikram 	if (v_flag) {
6775eb2bd662Svikram 		retval = process_vtoc_slices(s0,
6776eb2bd662Svikram 		    &vtoc, tfp, mhp, tmpmnt);
6777eb2bd662Svikram 	} else if (e_flag) {
6778eb2bd662Svikram 		retval = process_efi_slices(s0,
6779eb2bd662Svikram 		    efi, tfp, mhp, tmpmnt);
6780eb2bd662Svikram 	} else {
6781f64ca102SToomas Soome 		BAM_DPRINTF(("%s: disk has neither VTOC nor EFI: %s\n",
6782f64ca102SToomas Soome 		    fcn, s0path));
6783eb2bd662Svikram 		return (0);
6784eb2bd662Svikram 	}
6785eb2bd662Svikram 
6786eb2bd662Svikram 	return (retval);
6787eb2bd662Svikram }
6788eb2bd662Svikram 
6789eb2bd662Svikram /*
6790eb2bd662Svikram  * Find and create a list of all existing UFS boot signatures
6791eb2bd662Svikram  */
6792eb2bd662Svikram static int
6793eb2bd662Svikram FindAllUfsSignatures(void)
6794eb2bd662Svikram {
6795eb2bd662Svikram 	mhash_t		*mnttab_hash;
6796eb2bd662Svikram 	DIR		*dirp = NULL;
6797eb2bd662Svikram 	struct dirent	*dp;
6798eb2bd662Svikram 	char		tmpmnt[PATH_MAX];
6799eb2bd662Svikram 	char		cmd[PATH_MAX];
6800eb2bd662Svikram 	struct stat	sb;
6801eb2bd662Svikram 	int		fd;
6802eb2bd662Svikram 	FILE		*tfp;
6803eb2bd662Svikram 	size_t		len;
6804eb2bd662Svikram 	int		ret;
6805eb2bd662Svikram 	int		error;
6806eb2bd662Svikram 	const char	*fcn = "FindAllUfsSignatures()";
6807eb2bd662Svikram 
6808eb2bd662Svikram 	if (stat(UFS_SIGNATURE_LIST, &sb) != -1)  {
6809f64ca102SToomas Soome 		bam_print(_("       - signature list %s exists\n"),
6810f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST);
6811eb2bd662Svikram 		return (0);
6812eb2bd662Svikram 	}
6813eb2bd662Svikram 
6814eb2bd662Svikram 	fd = open(UFS_SIGNATURE_LIST".tmp",
6815eb2bd662Svikram 	    O_RDWR|O_CREAT|O_TRUNC, 0644);
6816eb2bd662Svikram 	error = errno;
6817eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_TMP_TRUNC", fd = -1);
6818eb2bd662Svikram 	if (fd == -1) {
6819f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"),
6820f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST".tmp", strerror(error));
6821eb2bd662Svikram 		return (-1);
6822eb2bd662Svikram 	}
6823eb2bd662Svikram 
6824eb2bd662Svikram 	ret = close(fd);
6825eb2bd662Svikram 	error = errno;
6826eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_TMP_CLOSE", ret = -1);
6827eb2bd662Svikram 	if (ret == -1) {
6828f64ca102SToomas Soome 		bam_error(_("failed to close file: %s: %s\n"),
6829f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST".tmp", strerror(error));
6830eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6831eb2bd662Svikram 		return (-1);
6832eb2bd662Svikram 	}
6833eb2bd662Svikram 
6834eb2bd662Svikram 	tfp = fopen(UFS_SIGNATURE_LIST".tmp", "a");
6835eb2bd662Svikram 	error = errno;
6836eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_APPEND_FOPEN", tfp = NULL);
6837eb2bd662Svikram 	if (tfp == NULL) {
6838f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"),
6839f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST".tmp", strerror(error));
6840eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6841eb2bd662Svikram 		return (-1);
6842eb2bd662Svikram 	}
6843eb2bd662Svikram 
6844eb2bd662Svikram 	mnttab_hash = cache_mnttab();
6845eb2bd662Svikram 	INJECT_ERROR1("CACHE_MNTTAB_ERROR", mnttab_hash = NULL);
6846eb2bd662Svikram 	if (mnttab_hash == NULL) {
6847eb2bd662Svikram 		(void) fclose(tfp);
6848eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6849f64ca102SToomas Soome 		bam_error(_("%s: failed to cache /etc/mnttab\n"), fcn);
6850eb2bd662Svikram 		return (-1);
6851eb2bd662Svikram 	}
6852eb2bd662Svikram 
6853eb2bd662Svikram 	(void) snprintf(tmpmnt, sizeof (tmpmnt),
6854eb2bd662Svikram 	    "/tmp/bootadm_ufs_sign_mnt.%d", getpid());
6855eb2bd662Svikram 	(void) unlink(tmpmnt);
6856eb2bd662Svikram 
685748847494SEnrico Perla - Sun Microsystems 	ret = mkdirp(tmpmnt, DIR_PERMS);
6858eb2bd662Svikram 	error = errno;
6859eb2bd662Svikram 	INJECT_ERROR1("MKDIRP_SIGN_MNT", ret = -1);
6860eb2bd662Svikram 	if (ret == -1) {
6861f64ca102SToomas Soome 		bam_error(_("mkdir of %s failed: %s\n"), tmpmnt,
6862f64ca102SToomas Soome 		    strerror(error));
6863eb2bd662Svikram 		free_mnttab(mnttab_hash);
6864eb2bd662Svikram 		(void) fclose(tfp);
6865eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6866eb2bd662Svikram 		return (-1);
6867eb2bd662Svikram 	}
6868eb2bd662Svikram 
6869eb2bd662Svikram 	dirp = opendir("/dev/rdsk");
6870eb2bd662Svikram 	error = errno;
6871eb2bd662Svikram 	INJECT_ERROR1("OPENDIR_DEV_RDSK", dirp = NULL);
6872eb2bd662Svikram 	if (dirp == NULL) {
6873f64ca102SToomas Soome 		bam_error(_("opendir of %s failed: %s\n"), "/dev/rdsk",
6874f64ca102SToomas Soome 		    strerror(error));
6875eb2bd662Svikram 		goto fail;
6876eb2bd662Svikram 	}
6877eb2bd662Svikram 
6878110570ceSToomas Soome 	while ((dp = readdir(dirp)) != NULL) {
6879eb2bd662Svikram 		if (strcmp(dp->d_name, ".") == 0 ||
6880eb2bd662Svikram 		    strcmp(dp->d_name, "..") == 0)
6881eb2bd662Svikram 			continue;
6882eb2bd662Svikram 
6883eb2bd662Svikram 		/*
6884eb2bd662Svikram 		 * we only look for the s0 slice. This is guranteed to
6885eb2bd662Svikram 		 * have 's' at len - 2.
6886eb2bd662Svikram 		 */
6887eb2bd662Svikram 		len = strlen(dp->d_name);
6888eb2bd662Svikram 		if (dp->d_name[len - 2 ] != 's' || dp->d_name[len - 1] != '0') {
6889f64ca102SToomas Soome 			BAM_DPRINTF(("%s: skipping non-s0 slice: %s\n",
6890f64ca102SToomas Soome 			    fcn, dp->d_name));
6891eb2bd662Svikram 			continue;
6892eb2bd662Svikram 		}
6893eb2bd662Svikram 
6894eb2bd662Svikram 		ret = process_slice0(dp->d_name, tfp, mnttab_hash, tmpmnt);
6895eb2bd662Svikram 		INJECT_ERROR1("PROCESS_S0_FAIL", ret = -1);
6896eb2bd662Svikram 		if (ret == -1)
6897eb2bd662Svikram 			goto fail;
6898eb2bd662Svikram 	}
6899eb2bd662Svikram 
6900eb2bd662Svikram 	(void) closedir(dirp);
6901eb2bd662Svikram 	free_mnttab(mnttab_hash);
6902eb2bd662Svikram 	(void) rmdir(tmpmnt);
6903eb2bd662Svikram 
6904eb2bd662Svikram 	ret = fclose(tfp);
6905eb2bd662Svikram 	error = errno;
6906eb2bd662Svikram 	INJECT_ERROR1("FCLOSE_SIGNLIST_TMP", ret = EOF);
6907eb2bd662Svikram 	if (ret == EOF) {
6908f64ca102SToomas Soome 		bam_error(_("failed to close file: %s: %s\n"),
6909f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST".tmp", strerror(error));
6910eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6911eb2bd662Svikram 		return (-1);
6912eb2bd662Svikram 	}
6913eb2bd662Svikram 
6914eb2bd662Svikram 	/* We have a list of existing GRUB signatures. Sort it first */
6915eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6916eb2bd662Svikram 	    "/usr/bin/sort -u %s.tmp > %s.sorted",
6917eb2bd662Svikram 	    UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST);
6918eb2bd662Svikram 
6919eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
6920eb2bd662Svikram 	INJECT_ERROR1("SORT_SIGN_LIST", ret = 1);
6921eb2bd662Svikram 	if (ret != 0) {
6922f64ca102SToomas Soome 		bam_error(_("error sorting GRUB UFS boot signatures\n"));
6923eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
6924eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6925eb2bd662Svikram 		return (-1);
6926eb2bd662Svikram 	}
6927eb2bd662Svikram 
6928eb2bd662Svikram 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
6929eb2bd662Svikram 
6930eb2bd662Svikram 	ret = rename(UFS_SIGNATURE_LIST".sorted", UFS_SIGNATURE_LIST);
6931eb2bd662Svikram 	error = errno;
6932eb2bd662Svikram 	INJECT_ERROR1("RENAME_TMP_SIGNLIST", ret = -1);
6933eb2bd662Svikram 	if (ret == -1) {
6934f64ca102SToomas Soome 		bam_error(_("rename to file failed: %s: %s\n"),
6935f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST, strerror(error));
6936eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
6937eb2bd662Svikram 		return (-1);
6938eb2bd662Svikram 	}
6939eb2bd662Svikram 
6940eb2bd662Svikram 	if (stat(UFS_SIGNATURE_LIST, &sb) == 0 && sb.st_size == 0) {
6941f64ca102SToomas Soome 		BAM_DPRINTF(("%s: generated zero length signlist: %s.\n",
6942f64ca102SToomas Soome 		    fcn, UFS_SIGNATURE_LIST));
6943eb2bd662Svikram 	}
6944eb2bd662Svikram 
6945f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
6946eb2bd662Svikram 	return (0);
6947eb2bd662Svikram 
6948eb2bd662Svikram fail:
6949eb2bd662Svikram 	if (dirp)
6950eb2bd662Svikram 		(void) closedir(dirp);
6951eb2bd662Svikram 	free_mnttab(mnttab_hash);
6952eb2bd662Svikram 	(void) rmdir(tmpmnt);
6953eb2bd662Svikram 	(void) fclose(tfp);
6954eb2bd662Svikram 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
6955f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
6956eb2bd662Svikram 	return (-1);
6957eb2bd662Svikram }
6958eb2bd662Svikram 
6959eb2bd662Svikram static char *
6960eb2bd662Svikram create_ufs_sign(void)
6961eb2bd662Svikram {
6962eb2bd662Svikram 	struct stat	sb;
6963eb2bd662Svikram 	int		signnum = -1;
6964eb2bd662Svikram 	char		tmpsign[MAXNAMELEN + 1];
6965eb2bd662Svikram 	char		*numstr;
6966eb2bd662Svikram 	int		i;
6967eb2bd662Svikram 	FILE		*tfp;
6968eb2bd662Svikram 	int		ret;
6969eb2bd662Svikram 	int		error;
6970eb2bd662Svikram 	const char	*fcn = "create_ufs_sign()";
6971eb2bd662Svikram 
6972f64ca102SToomas Soome 	bam_print(_("  - searching for UFS boot signatures\n"));
6973eb2bd662Svikram 
6974eb2bd662Svikram 	ret = FindAllUfsSignatures();
6975eb2bd662Svikram 	INJECT_ERROR1("FIND_ALL_UFS", ret = -1);
6976eb2bd662Svikram 	if (ret == -1) {
6977f64ca102SToomas Soome 		bam_error(_("search for UFS boot signatures failed\n"));
6978eb2bd662Svikram 		return (NULL);
6979eb2bd662Svikram 	}
6980eb2bd662Svikram 
6981eb2bd662Svikram 	/* Make sure the list exists and is owned by root */
6982eb2bd662Svikram 	INJECT_ERROR1("SIGNLIST_NOT_CREATED",
6983eb2bd662Svikram 	    (void) unlink(UFS_SIGNATURE_LIST));
6984eb2bd662Svikram 	if (stat(UFS_SIGNATURE_LIST, &sb) == -1 || sb.st_uid != 0) {
6985eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6986f64ca102SToomas Soome 		bam_error(_("missing UFS signature list file: %s\n"),
6987f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST);
6988eb2bd662Svikram 		return (NULL);
6989eb2bd662Svikram 	}
6990eb2bd662Svikram 
6991eb2bd662Svikram 	if (sb.st_size == 0) {
6992f64ca102SToomas Soome 		bam_print(_("   - no existing UFS boot signatures\n"));
6993eb2bd662Svikram 		i = 0;
6994eb2bd662Svikram 		goto found;
6995eb2bd662Svikram 	}
6996eb2bd662Svikram 
6997eb2bd662Svikram 	/* The signature list was sorted when it was created */
6998eb2bd662Svikram 	tfp = fopen(UFS_SIGNATURE_LIST, "r");
6999eb2bd662Svikram 	error = errno;
7000eb2bd662Svikram 	INJECT_ERROR1("FOPEN_SIGN_LIST", tfp = NULL);
7001eb2bd662Svikram 	if (tfp == NULL) {
7002f64ca102SToomas Soome 		bam_error(_("error opening UFS boot signature list "
7003f64ca102SToomas Soome 		    "file %s: %s\n"), UFS_SIGNATURE_LIST, strerror(error));
7004eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
7005eb2bd662Svikram 		return (NULL);
7006eb2bd662Svikram 	}
7007eb2bd662Svikram 
7008eb2bd662Svikram 	for (i = 0; s_fgets(tmpsign, sizeof (tmpsign), tfp); i++) {
7009eb2bd662Svikram 
7010eb2bd662Svikram 		if (strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
7011eb2bd662Svikram 		    strlen(GRUBSIGN_UFS_PREFIX)) != 0) {
7012eb2bd662Svikram 			(void) fclose(tfp);
7013eb2bd662Svikram 			(void) unlink(UFS_SIGNATURE_LIST);
7014f64ca102SToomas Soome 			bam_error(_("bad UFS boot signature: %s\n"), tmpsign);
7015eb2bd662Svikram 			return (NULL);
7016eb2bd662Svikram 		}
7017eb2bd662Svikram 		numstr = tmpsign + strlen(GRUBSIGN_UFS_PREFIX);
7018eb2bd662Svikram 
7019eb2bd662Svikram 		if (numstr[0] == '\0' || !isdigit(numstr[0])) {
7020eb2bd662Svikram 			(void) fclose(tfp);
7021eb2bd662Svikram 			(void) unlink(UFS_SIGNATURE_LIST);
7022f64ca102SToomas Soome 			bam_error(_("bad UFS boot signature: %s\n"), tmpsign);
7023eb2bd662Svikram 			return (NULL);
7024eb2bd662Svikram 		}
7025eb2bd662Svikram 
7026eb2bd662Svikram 		signnum = atoi(numstr);
7027eb2bd662Svikram 		INJECT_ERROR1("NEGATIVE_SIGN", signnum = -1);
7028eb2bd662Svikram 		if (signnum < 0) {
7029eb2bd662Svikram 			(void) fclose(tfp);
7030eb2bd662Svikram 			(void) unlink(UFS_SIGNATURE_LIST);
7031f64ca102SToomas Soome 			bam_error(_("bad UFS boot signature: %s\n"), tmpsign);
7032eb2bd662Svikram 			return (NULL);
7033eb2bd662Svikram 		}
7034eb2bd662Svikram 
7035eb2bd662Svikram 		if (i != signnum) {
7036f64ca102SToomas Soome 			BAM_DPRINTF(("%s: found hole %d in sign list.\n",
7037f64ca102SToomas Soome 			    fcn, i));
7038eb2bd662Svikram 			break;
7039eb2bd662Svikram 		}
7040eb2bd662Svikram 	}
7041eb2bd662Svikram 
7042eb2bd662Svikram 	(void) fclose(tfp);
7043eb2bd662Svikram 
7044eb2bd662Svikram found:
7045eb2bd662Svikram 	(void) snprintf(tmpsign, sizeof (tmpsign), "rootfs%d", i);
7046eb2bd662Svikram 
7047eb2bd662Svikram 	/* add the ufs signature to the /var/run list of signatures */
7048eb2bd662Svikram 	ret = ufs_add_to_sign_list(tmpsign);
7049eb2bd662Svikram 	INJECT_ERROR1("UFS_ADD_TO_SIGN_LIST", ret = -1);
7050eb2bd662Svikram 	if (ret == -1) {
7051eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
7052f64ca102SToomas Soome 		bam_error(_("failed to add sign %s to signlist.\n"), tmpsign);
7053eb2bd662Svikram 		return (NULL);
7054eb2bd662Svikram 	}
7055eb2bd662Svikram 
7056f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7057eb2bd662Svikram 
7058eb2bd662Svikram 	return (s_strdup(tmpsign));
7059eb2bd662Svikram }
7060eb2bd662Svikram 
7061eb2bd662Svikram static char *
7062eb2bd662Svikram get_fstype(char *osroot)
7063eb2bd662Svikram {
7064eb2bd662Svikram 	FILE		*mntfp;
7065eb2bd662Svikram 	struct mnttab	mp = {0};
7066eb2bd662Svikram 	struct mnttab	mpref = {0};
7067eb2bd662Svikram 	int		error;
7068eb2bd662Svikram 	int		ret;
7069eb2bd662Svikram 	const char	*fcn = "get_fstype()";
7070eb2bd662Svikram 
7071eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_OSROOT", osroot = NULL);
7072eb2bd662Svikram 	if (osroot == NULL) {
7073f64ca102SToomas Soome 		bam_error(_("no OS mountpoint. Cannot determine fstype\n"));
7074eb2bd662Svikram 		return (NULL);
7075eb2bd662Svikram 	}
7076eb2bd662Svikram 
7077eb2bd662Svikram 	mntfp = fopen(MNTTAB, "r");
7078eb2bd662Svikram 	error = errno;
7079eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_FOPEN", mntfp = NULL);
7080eb2bd662Svikram 	if (mntfp == NULL) {
7081f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), MNTTAB,
7082f64ca102SToomas Soome 		    strerror(error));
7083eb2bd662Svikram 		return (NULL);
7084eb2bd662Svikram 	}
7085eb2bd662Svikram 
7086eb2bd662Svikram 	if (*osroot == '\0')
7087eb2bd662Svikram 		mpref.mnt_mountp = "/";
7088eb2bd662Svikram 	else
7089eb2bd662Svikram 		mpref.mnt_mountp = osroot;
7090eb2bd662Svikram 
7091eb2bd662Svikram 	ret = getmntany(mntfp, &mp, &mpref);
7092eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_GETMNTANY", ret = 1);
7093eb2bd662Svikram 	if (ret != 0) {
7094f64ca102SToomas Soome 		bam_error(_("failed to find OS mountpoint %s in %s\n"),
7095f64ca102SToomas Soome 		    osroot, MNTTAB);
7096eb2bd662Svikram 		(void) fclose(mntfp);
7097eb2bd662Svikram 		return (NULL);
7098eb2bd662Svikram 	}
7099eb2bd662Svikram 	(void) fclose(mntfp);
7100eb2bd662Svikram 
7101eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_NULL", mp.mnt_fstype = NULL);
7102eb2bd662Svikram 	if (mp.mnt_fstype == NULL) {
7103f64ca102SToomas Soome 		bam_error(_("NULL fstype found for OS root %s\n"), osroot);
7104eb2bd662Svikram 		return (NULL);
7105eb2bd662Svikram 	}
7106eb2bd662Svikram 
7107f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7108eb2bd662Svikram 
7109eb2bd662Svikram 	return (s_strdup(mp.mnt_fstype));
7110eb2bd662Svikram }
7111eb2bd662Svikram 
7112eb2bd662Svikram static char *
7113eb2bd662Svikram create_zfs_sign(char *osdev)
7114eb2bd662Svikram {
7115eb2bd662Svikram 	char		tmpsign[PATH_MAX];
7116eb2bd662Svikram 	char		*pool;
7117eb2bd662Svikram 	const char	*fcn = "create_zfs_sign()";
7118eb2bd662Svikram 
7119f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. arg: %s\n", fcn, osdev));
7120eb2bd662Svikram 
7121eb2bd662Svikram 	/*
7122eb2bd662Svikram 	 * First find the pool name
7123eb2bd662Svikram 	 */
7124eb2bd662Svikram 	pool = get_pool(osdev);
7125eb2bd662Svikram 	INJECT_ERROR1("CREATE_ZFS_SIGN_GET_POOL", pool = NULL);
7126eb2bd662Svikram 	if (pool == NULL) {
7127f64ca102SToomas Soome 		bam_error(_("failed to get pool name from %s\n"), osdev);
7128eb2bd662Svikram 		return (NULL);
7129eb2bd662Svikram 	}
7130eb2bd662Svikram 
7131eb2bd662Svikram 	(void) snprintf(tmpsign, sizeof (tmpsign), "pool_%s", pool);
7132eb2bd662Svikram 
7133f64ca102SToomas Soome 	BAM_DPRINTF(("%s: created ZFS sign: %s\n", fcn, tmpsign));
7134eb2bd662Svikram 
7135eb2bd662Svikram 	free(pool);
7136eb2bd662Svikram 
7137f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7138eb2bd662Svikram 
7139eb2bd662Svikram 	return (s_strdup(tmpsign));
7140eb2bd662Svikram }
7141eb2bd662Svikram 
7142eb2bd662Svikram static char *
7143eb2bd662Svikram create_new_sign(char *osdev, char *fstype)
7144eb2bd662Svikram {
7145eb2bd662Svikram 	char		*sign;
7146eb2bd662Svikram 	const char	*fcn = "create_new_sign()";
7147eb2bd662Svikram 
7148eb2bd662Svikram 	INJECT_ERROR1("NEW_SIGN_FSTYPE", fstype = "foofs");
7149eb2bd662Svikram 
7150eb2bd662Svikram 	if (strcmp(fstype, "zfs") == 0) {
7151f64ca102SToomas Soome 		BAM_DPRINTF(("%s: created new ZFS sign\n", fcn));
7152eb2bd662Svikram 		sign = create_zfs_sign(osdev);
7153eb2bd662Svikram 	} else if (strcmp(fstype, "ufs") == 0) {
7154f64ca102SToomas Soome 		BAM_DPRINTF(("%s: created new UFS sign\n", fcn));
7155eb2bd662Svikram 		sign = create_ufs_sign();
7156eb2bd662Svikram 	} else {
7157f64ca102SToomas Soome 		bam_error(_("boot signature not supported for fstype: %s\n"),
7158f64ca102SToomas Soome 		    fstype);
7159eb2bd662Svikram 		sign = NULL;
7160eb2bd662Svikram 	}
7161eb2bd662Svikram 
7162f64ca102SToomas Soome 	BAM_DPRINTF(("%s: created new sign: %s\n", fcn,
7163f64ca102SToomas Soome 	    sign ? sign : "<NULL>"));
7164eb2bd662Svikram 	return (sign);
7165eb2bd662Svikram }
7166eb2bd662Svikram 
7167eb2bd662Svikram static int
7168eb2bd662Svikram set_backup_common(char *mntpt, char *sign)
7169eb2bd662Svikram {
7170eb2bd662Svikram 	FILE		*bfp;
7171eb2bd662Svikram 	char		backup[PATH_MAX];
7172eb2bd662Svikram 	char		tmpsign[PATH_MAX];
7173eb2bd662Svikram 	int		error;
7174eb2bd662Svikram 	char		*bdir;
7175eb2bd662Svikram 	char		*backup_dup;
7176eb2bd662Svikram 	struct stat	sb;
7177eb2bd662Svikram 	int		ret;
7178eb2bd662Svikram 	const char	*fcn = "set_backup_common()";
7179eb2bd662Svikram 
7180eb2bd662Svikram 	(void) snprintf(backup, sizeof (backup), "%s%s",
7181eb2bd662Svikram 	    mntpt, GRUBSIGN_BACKUP);
7182eb2bd662Svikram 
7183eb2bd662Svikram 	/* First read the backup */
7184eb2bd662Svikram 	bfp = fopen(backup, "r");
7185eb2bd662Svikram 	if (bfp != NULL) {
7186eb2bd662Svikram 		while (s_fgets(tmpsign, sizeof (tmpsign), bfp)) {
7187eb2bd662Svikram 			if (strcmp(tmpsign, sign) == 0) {
7188f64ca102SToomas Soome 				BAM_DPRINTF(("%s: found sign (%s) in backup.\n",
7189f64ca102SToomas Soome 				    fcn, sign));
7190eb2bd662Svikram 				(void) fclose(bfp);
7191eb2bd662Svikram 				return (0);
7192eb2bd662Svikram 			}
7193eb2bd662Svikram 		}
7194eb2bd662Svikram 		(void) fclose(bfp);
7195f64ca102SToomas Soome 		BAM_DPRINTF(("%s: backup exists but sign %s not found\n",
7196f64ca102SToomas Soome 		    fcn, sign));
7197eb2bd662Svikram 	} else {
7198f64ca102SToomas Soome 		BAM_DPRINTF(("%s: no backup file (%s) found.\n", fcn, backup));
7199eb2bd662Svikram 	}
7200eb2bd662Svikram 
7201eb2bd662Svikram 	/*
7202eb2bd662Svikram 	 * Didn't find the correct signature. First create
7203eb2bd662Svikram 	 * the directory if necessary.
7204eb2bd662Svikram 	 */
7205eb2bd662Svikram 
7206eb2bd662Svikram 	/* dirname() modifies its argument so dup it */
7207eb2bd662Svikram 	backup_dup = s_strdup(backup);
7208eb2bd662Svikram 	bdir = dirname(backup_dup);
7209eb2bd662Svikram 	assert(bdir);
7210eb2bd662Svikram 
7211eb2bd662Svikram 	ret = stat(bdir, &sb);
7212eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_STAT", ret = -1);
7213eb2bd662Svikram 	if (ret == -1) {
7214f64ca102SToomas Soome 		BAM_DPRINTF(("%s: backup dir (%s) does not exist.\n",
7215f64ca102SToomas Soome 		    fcn, bdir));
721648847494SEnrico Perla - Sun Microsystems 		ret = mkdirp(bdir, DIR_PERMS);
7217eb2bd662Svikram 		error = errno;
7218eb2bd662Svikram 		INJECT_ERROR1("SET_BACKUP_MKDIRP", ret = -1);
7219eb2bd662Svikram 		if (ret == -1) {
7220f64ca102SToomas Soome 			bam_error(_("mkdirp() of backup dir failed: %s: %s\n"),
7221eb2bd662Svikram 			    GRUBSIGN_BACKUP, strerror(error));
7222eb2bd662Svikram 			free(backup_dup);
7223eb2bd662Svikram 			return (-1);
7224eb2bd662Svikram 		}
7225eb2bd662Svikram 	}
7226eb2bd662Svikram 	free(backup_dup);
7227eb2bd662Svikram 
7228eb2bd662Svikram 	/*
7229eb2bd662Svikram 	 * Open the backup in append mode to add the correct
7230eb2bd662Svikram 	 * signature;
7231eb2bd662Svikram 	 */
7232eb2bd662Svikram 	bfp = fopen(backup, "a");
7233eb2bd662Svikram 	error = errno;
7234eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_FOPEN_A", bfp = NULL);
7235eb2bd662Svikram 	if (bfp == NULL) {
7236f64ca102SToomas Soome 		bam_error(_("error opening boot signature backup "
7237f64ca102SToomas Soome 		    "file %s: %s\n"), GRUBSIGN_BACKUP, strerror(error));
7238eb2bd662Svikram 		return (-1);
7239eb2bd662Svikram 	}
7240eb2bd662Svikram 
7241eb2bd662Svikram 	(void) snprintf(tmpsign, sizeof (tmpsign), "%s\n", sign);
7242eb2bd662Svikram 
7243eb2bd662Svikram 	ret = fputs(tmpsign, bfp);
7244eb2bd662Svikram 	error = errno;
7245eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_FPUTS", ret = 0);
7246eb2bd662Svikram 	if (ret != strlen(tmpsign)) {
7247f64ca102SToomas Soome 		bam_error(_("error writing boot signature backup "
7248f64ca102SToomas Soome 		    "file %s: %s\n"), GRUBSIGN_BACKUP, strerror(error));
7249eb2bd662Svikram 		(void) fclose(bfp);
7250eb2bd662Svikram 		return (-1);
7251eb2bd662Svikram 	}
7252eb2bd662Svikram 
7253eb2bd662Svikram 	(void) fclose(bfp);
7254eb2bd662Svikram 
7255eb2bd662Svikram 	if (bam_verbose)
7256f64ca102SToomas Soome 		bam_print(_("updated boot signature backup file %s\n"),
7257f64ca102SToomas Soome 		    GRUBSIGN_BACKUP);
7258eb2bd662Svikram 
7259f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7260eb2bd662Svikram 
7261eb2bd662Svikram 	return (0);
7262eb2bd662Svikram }
7263eb2bd662Svikram 
7264eb2bd662Svikram static int
7265eb2bd662Svikram set_backup_ufs(char *osroot, char *sign)
7266eb2bd662Svikram {
7267eb2bd662Svikram 	const char	*fcn = "set_backup_ufs()";
7268eb2bd662Svikram 
7269f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osroot, sign));
7270eb2bd662Svikram 	return (set_backup_common(osroot, sign));
7271eb2bd662Svikram }
7272eb2bd662Svikram 
7273eb2bd662Svikram static int
7274eb2bd662Svikram set_backup_zfs(char *osdev, char *sign)
7275eb2bd662Svikram {
7276eb2bd662Svikram 	char		*pool;
7277eb2bd662Svikram 	char		*mntpt;
7278eb2bd662Svikram 	zfs_mnted_t	mnted;
7279eb2bd662Svikram 	int		ret;
7280eb2bd662Svikram 	const char	*fcn = "set_backup_zfs()";
7281eb2bd662Svikram 
7282f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osdev, sign));
7283eb2bd662Svikram 
7284eb2bd662Svikram 	pool = get_pool(osdev);
7285eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_GET_POOL", pool = NULL);
7286eb2bd662Svikram 	if (pool == NULL) {
7287f64ca102SToomas Soome 		bam_error(_("failed to get pool name from %s\n"), osdev);
7288eb2bd662Svikram 		return (-1);
7289eb2bd662Svikram 	}
7290eb2bd662Svikram 
7291eb2bd662Svikram 	mntpt = mount_top_dataset(pool, &mnted);
7292eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_MOUNT_DATASET", mntpt = NULL);
7293eb2bd662Svikram 	if (mntpt == NULL) {
7294f64ca102SToomas Soome 		bam_error(_("failed to mount top dataset for %s\n"), pool);
7295eb2bd662Svikram 		free(pool);
7296eb2bd662Svikram 		return (-1);
7297eb2bd662Svikram 	}
7298eb2bd662Svikram 
7299eb2bd662Svikram 	ret = set_backup_common(mntpt, sign);
7300eb2bd662Svikram 
7301eb2bd662Svikram 	(void) umount_top_dataset(pool, mnted, mntpt);
7302eb2bd662Svikram 
7303eb2bd662Svikram 	free(pool);
7304eb2bd662Svikram 
7305eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_ZFS_FAIL", ret = 1);
7306eb2bd662Svikram 	if (ret == 0) {
7307f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7308eb2bd662Svikram 	} else {
7309f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
7310eb2bd662Svikram 	}
7311eb2bd662Svikram 
7312eb2bd662Svikram 	return (ret);
7313eb2bd662Svikram }
7314eb2bd662Svikram 
7315eb2bd662Svikram static int
7316eb2bd662Svikram set_backup(char *osroot, char *osdev, char *sign, char *fstype)
7317eb2bd662Svikram {
7318eb2bd662Svikram 	const char	*fcn = "set_backup()";
7319eb2bd662Svikram 	int		ret;
7320eb2bd662Svikram 
7321eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_FSTYPE", fstype = "foofs");
7322eb2bd662Svikram 
7323eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0) {
7324f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting UFS backup sign\n", fcn));
7325eb2bd662Svikram 		ret = set_backup_ufs(osroot, sign);
7326eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0) {
7327f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting ZFS backup sign\n", fcn));
7328eb2bd662Svikram 		ret = set_backup_zfs(osdev, sign);
7329eb2bd662Svikram 	} else {
7330f64ca102SToomas Soome 		bam_error(_("boot signature not supported for fstype: %s\n"),
7331f64ca102SToomas Soome 		    fstype);
7332eb2bd662Svikram 		ret = -1;
7333eb2bd662Svikram 	}
7334eb2bd662Svikram 
7335eb2bd662Svikram 	if (ret == 0) {
7336f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7337eb2bd662Svikram 	} else {
7338f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
7339eb2bd662Svikram 	}
7340eb2bd662Svikram 
7341eb2bd662Svikram 	return (ret);
7342eb2bd662Svikram }
7343eb2bd662Svikram 
7344eb2bd662Svikram static int
7345eb2bd662Svikram set_primary_common(char *mntpt, char *sign)
7346eb2bd662Svikram {
7347eb2bd662Svikram 	char		signfile[PATH_MAX];
7348eb2bd662Svikram 	char		signdir[PATH_MAX];
7349eb2bd662Svikram 	struct stat	sb;
7350eb2bd662Svikram 	int		fd;
7351eb2bd662Svikram 	int		error;
7352eb2bd662Svikram 	int		ret;
7353eb2bd662Svikram 	const char	*fcn = "set_primary_common()";
7354eb2bd662Svikram 
7355eb2bd662Svikram 	(void) snprintf(signfile, sizeof (signfile), "%s/%s/%s",
7356eb2bd662Svikram 	    mntpt, GRUBSIGN_DIR, sign);
7357eb2bd662Svikram 
7358eb2bd662Svikram 	if (stat(signfile, &sb) != -1) {
7359eb2bd662Svikram 		if (bam_verbose)
7360f64ca102SToomas Soome 			bam_print(_("primary sign %s exists\n"), sign);
7361eb2bd662Svikram 		return (0);
7362eb2bd662Svikram 	} else {
7363f64ca102SToomas Soome 		BAM_DPRINTF(("%s: primary sign (%s) does not exist\n",
7364f64ca102SToomas Soome 		    fcn, signfile));
7365eb2bd662Svikram 	}
7366eb2bd662Svikram 
7367eb2bd662Svikram 	(void) snprintf(signdir, sizeof (signdir), "%s/%s",
7368eb2bd662Svikram 	    mntpt, GRUBSIGN_DIR);
7369eb2bd662Svikram 
7370eb2bd662Svikram 	if (stat(signdir, &sb) == -1) {
7371f64ca102SToomas Soome 		BAM_DPRINTF(("%s: primary signdir (%s) does not exist\n",
7372f64ca102SToomas Soome 		    fcn, signdir));
737348847494SEnrico Perla - Sun Microsystems 		ret = mkdirp(signdir, DIR_PERMS);
7374eb2bd662Svikram 		error = errno;
7375eb2bd662Svikram 		INJECT_ERROR1("SET_PRIMARY_MKDIRP", ret = -1);
7376eb2bd662Svikram 		if (ret == -1) {
7377f64ca102SToomas Soome 			bam_error(_("error creating boot signature "
7378f64ca102SToomas Soome 			    "directory %s: %s\n"), signdir, strerror(errno));
7379eb2bd662Svikram 			return (-1);
7380eb2bd662Svikram 		}
7381eb2bd662Svikram 	}
7382eb2bd662Svikram 
7383eb2bd662Svikram 	fd = open(signfile, O_RDWR|O_CREAT|O_TRUNC, 0444);
7384eb2bd662Svikram 	error = errno;
7385eb2bd662Svikram 	INJECT_ERROR1("PRIMARY_SIGN_CREAT", fd = -1);
7386eb2bd662Svikram 	if (fd == -1) {
7387f64ca102SToomas Soome 		bam_error(_("error creating primary boot signature %s: %s\n"),
7388f64ca102SToomas Soome 		    signfile, strerror(error));
7389eb2bd662Svikram 		return (-1);
7390eb2bd662Svikram 	}
7391eb2bd662Svikram 
7392eb2bd662Svikram 	ret = fsync(fd);
7393eb2bd662Svikram 	error = errno;
7394eb2bd662Svikram 	INJECT_ERROR1("PRIMARY_FSYNC", ret = -1);
7395eb2bd662Svikram 	if (ret != 0) {
7396f64ca102SToomas Soome 		bam_error(_("error syncing primary boot signature %s: %s\n"),
7397f64ca102SToomas Soome 		    signfile, strerror(error));
7398eb2bd662Svikram 	}
7399eb2bd662Svikram 
7400eb2bd662Svikram 	(void) close(fd);
7401eb2bd662Svikram 
7402eb2bd662Svikram 	if (bam_verbose)
7403f64ca102SToomas Soome 		bam_print(_("created primary GRUB boot signature: %s\n"),
7404f64ca102SToomas Soome 		    signfile);
7405eb2bd662Svikram 
7406f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7407eb2bd662Svikram 
7408eb2bd662Svikram 	return (0);
7409eb2bd662Svikram }
7410eb2bd662Svikram 
7411eb2bd662Svikram static int
7412eb2bd662Svikram set_primary_ufs(char *osroot, char *sign)
7413eb2bd662Svikram {
7414eb2bd662Svikram 	const char	*fcn = "set_primary_ufs()";
7415eb2bd662Svikram 
7416f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osroot, sign));
7417eb2bd662Svikram 	return (set_primary_common(osroot, sign));
7418eb2bd662Svikram }
7419eb2bd662Svikram 
7420eb2bd662Svikram static int
7421eb2bd662Svikram set_primary_zfs(char *osdev, char *sign)
7422eb2bd662Svikram {
7423eb2bd662Svikram 	char		*pool;
7424eb2bd662Svikram 	char		*mntpt;
7425eb2bd662Svikram 	zfs_mnted_t	mnted;
7426eb2bd662Svikram 	int		ret;
7427eb2bd662Svikram 	const char	*fcn = "set_primary_zfs()";
7428eb2bd662Svikram 
7429f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osdev, sign));
7430eb2bd662Svikram 
7431eb2bd662Svikram 	pool = get_pool(osdev);
7432eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_GET_POOL", pool = NULL);
7433eb2bd662Svikram 	if (pool == NULL) {
7434f64ca102SToomas Soome 		bam_error(_("failed to get pool name from %s\n"), osdev);
7435eb2bd662Svikram 		return (-1);
7436eb2bd662Svikram 	}
7437eb2bd662Svikram 
7438eb2bd662Svikram 	/* Pool name must exist in the sign */
7439eb2bd662Svikram 	ret = (strstr(sign, pool) != NULL);
7440eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_POOL_SIGN_INCOMPAT", ret = 0);
7441eb2bd662Svikram 	if (ret == 0) {
7442f64ca102SToomas Soome 		bam_error(_("pool name %s not present in signature %s\n"),
7443f64ca102SToomas Soome 		    pool, sign);
7444eb2bd662Svikram 		free(pool);
7445eb2bd662Svikram 		return (-1);
7446eb2bd662Svikram 	}
7447eb2bd662Svikram 
7448eb2bd662Svikram 	mntpt = mount_top_dataset(pool, &mnted);
7449eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_MOUNT_DATASET", mntpt = NULL);
7450eb2bd662Svikram 	if (mntpt == NULL) {
7451f64ca102SToomas Soome 		bam_error(_("failed to mount top dataset for %s\n"), pool);
7452eb2bd662Svikram 		free(pool);
7453eb2bd662Svikram 		return (-1);
7454eb2bd662Svikram 	}
7455eb2bd662Svikram 
7456eb2bd662Svikram 	ret = set_primary_common(mntpt, sign);
7457eb2bd662Svikram 
7458eb2bd662Svikram 	(void) umount_top_dataset(pool, mnted, mntpt);
7459eb2bd662Svikram 
7460eb2bd662Svikram 	free(pool);
7461eb2bd662Svikram 
7462eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_FAIL", ret = 1);
7463eb2bd662Svikram 	if (ret == 0) {
7464f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7465eb2bd662Svikram 	} else {
7466f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
7467eb2bd662Svikram 	}
7468eb2bd662Svikram 
7469eb2bd662Svikram 	return (ret);
7470eb2bd662Svikram }
7471eb2bd662Svikram 
7472eb2bd662Svikram static int
7473eb2bd662Svikram set_primary(char *osroot, char *osdev, char *sign, char *fstype)
7474eb2bd662Svikram {
7475eb2bd662Svikram 	const char	*fcn = "set_primary()";
7476eb2bd662Svikram 	int		ret;
7477eb2bd662Svikram 
7478eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_FSTYPE", fstype = "foofs");
7479eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0) {
7480f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting UFS primary sign\n", fcn));
7481eb2bd662Svikram 		ret = set_primary_ufs(osroot, sign);
7482eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0) {
7483f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting ZFS primary sign\n", fcn));
7484eb2bd662Svikram 		ret = set_primary_zfs(osdev, sign);
7485eb2bd662Svikram 	} else {
7486f64ca102SToomas Soome 		bam_error(_("boot signature not supported for fstype: %s\n"),
7487f64ca102SToomas Soome 		    fstype);
7488eb2bd662Svikram 		ret = -1;
7489eb2bd662Svikram 	}
7490eb2bd662Svikram 
7491eb2bd662Svikram 	if (ret == 0) {
7492f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7493eb2bd662Svikram 	} else {
7494f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
7495eb2bd662Svikram 	}
7496eb2bd662Svikram 
7497eb2bd662Svikram 	return (ret);
7498eb2bd662Svikram }
7499eb2bd662Svikram 
7500eb2bd662Svikram static int
7501eb2bd662Svikram ufs_add_to_sign_list(char *sign)
7502eb2bd662Svikram {
7503eb2bd662Svikram 	FILE		*tfp;
7504eb2bd662Svikram 	char		signline[MAXNAMELEN];
7505eb2bd662Svikram 	char		cmd[PATH_MAX];
7506eb2bd662Svikram 	int		ret;
7507eb2bd662Svikram 	int		error;
7508eb2bd662Svikram 	const char	*fcn = "ufs_add_to_sign_list()";
7509eb2bd662Svikram 
7510eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_NOT_UFS", sign = "pool_rpool5");
7511eb2bd662Svikram 	if (strncmp(sign, GRUBSIGN_UFS_PREFIX,
7512eb2bd662Svikram 	    strlen(GRUBSIGN_UFS_PREFIX)) != 0) {
7513f64ca102SToomas Soome 		bam_error(_("invalid UFS boot signature %s\n"), sign);
7514eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
7515eb2bd662Svikram 		return (-1);
7516eb2bd662Svikram 	}
7517eb2bd662Svikram 
7518eb2bd662Svikram 	/*
7519eb2bd662Svikram 	 * most failures in this routine are not a fatal error
7520eb2bd662Svikram 	 * We simply unlink the /var/run file and continue
7521eb2bd662Svikram 	 */
7522eb2bd662Svikram 
7523eb2bd662Svikram 	ret = rename(UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST".tmp");
7524eb2bd662Svikram 	error = errno;
7525eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_RENAME", ret = -1);
7526eb2bd662Svikram 	if (ret == -1) {
7527f64ca102SToomas Soome 		bam_error(_("rename to file failed: %s: %s\n"),
7528f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST".tmp", strerror(error));
7529eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
7530eb2bd662Svikram 		return (0);
7531eb2bd662Svikram 	}
7532eb2bd662Svikram 
7533eb2bd662Svikram 	tfp = fopen(UFS_SIGNATURE_LIST".tmp", "a");
7534eb2bd662Svikram 	error = errno;
7535eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FOPEN", tfp = NULL);
7536eb2bd662Svikram 	if (tfp == NULL) {
7537f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"),
7538f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST".tmp", strerror(error));
7539eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
7540eb2bd662Svikram 		return (0);
7541eb2bd662Svikram 	}
7542eb2bd662Svikram 
7543eb2bd662Svikram 	(void) snprintf(signline, sizeof (signline), "%s\n", sign);
7544eb2bd662Svikram 
7545eb2bd662Svikram 	ret = fputs(signline, tfp);
7546eb2bd662Svikram 	error = errno;
7547eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FPUTS", ret = 0);
7548eb2bd662Svikram 	if (ret != strlen(signline)) {
7549f64ca102SToomas Soome 		bam_error(_("failed to write signature %s to signature "
7550f64ca102SToomas Soome 		    "list: %s\n"), sign, strerror(error));
7551eb2bd662Svikram 		(void) fclose(tfp);
7552eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
7553eb2bd662Svikram 		return (0);
7554eb2bd662Svikram 	}
7555eb2bd662Svikram 
7556eb2bd662Svikram 	ret = fclose(tfp);
7557eb2bd662Svikram 	error = errno;
7558eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FCLOSE", ret = EOF);
7559eb2bd662Svikram 	if (ret == EOF) {
7560f64ca102SToomas Soome 		bam_error(_("failed to close file: %s: %s\n"),
7561f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST".tmp", strerror(error));
7562eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
7563eb2bd662Svikram 		return (0);
7564eb2bd662Svikram 	}
7565eb2bd662Svikram 
7566eb2bd662Svikram 	/* Sort the list again */
7567eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
7568eb2bd662Svikram 	    "/usr/bin/sort -u %s.tmp > %s.sorted",
7569eb2bd662Svikram 	    UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST);
7570eb2bd662Svikram 
7571eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
7572eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_SORT", ret = 1);
7573eb2bd662Svikram 	if (ret != 0) {
7574f64ca102SToomas Soome 		bam_error(_("error sorting GRUB UFS boot signatures\n"));
7575eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
7576eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
7577eb2bd662Svikram 		return (0);
7578eb2bd662Svikram 	}
7579eb2bd662Svikram 
7580eb2bd662Svikram 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
7581eb2bd662Svikram 
7582eb2bd662Svikram 	ret = rename(UFS_SIGNATURE_LIST".sorted", UFS_SIGNATURE_LIST);
7583eb2bd662Svikram 	error = errno;
7584eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_RENAME2", ret = -1);
7585eb2bd662Svikram 	if (ret == -1) {
7586f64ca102SToomas Soome 		bam_error(_("rename to file failed: %s: %s\n"),
7587f64ca102SToomas Soome 		    UFS_SIGNATURE_LIST, strerror(error));
7588eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
7589eb2bd662Svikram 		return (0);
7590eb2bd662Svikram 	}
7591eb2bd662Svikram 
7592f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7593eb2bd662Svikram 
7594eb2bd662Svikram 	return (0);
7595eb2bd662Svikram }
7596eb2bd662Svikram 
7597eb2bd662Svikram static int
7598eb2bd662Svikram set_signature(char *osroot, char *osdev, char *sign, char *fstype)
7599eb2bd662Svikram {
7600eb2bd662Svikram 	int		ret;
7601eb2bd662Svikram 	const char	*fcn = "set_signature()";
7602eb2bd662Svikram 
7603f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s %s %s\n", fcn,
7604f64ca102SToomas Soome 	    osroot, osdev, sign, fstype));
7605eb2bd662Svikram 
7606eb2bd662Svikram 	ret = set_backup(osroot, osdev, sign, fstype);
7607eb2bd662Svikram 	INJECT_ERROR1("SET_SIGNATURE_BACKUP", ret = -1);
7608eb2bd662Svikram 	if (ret == -1) {
7609f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
7610f64ca102SToomas Soome 		bam_error(_("failed to set backup sign (%s) for %s: %s\n"),
7611f64ca102SToomas Soome 		    sign, osroot, osdev);
7612eb2bd662Svikram 		return (-1);
7613eb2bd662Svikram 	}
7614eb2bd662Svikram 
7615eb2bd662Svikram 	ret = set_primary(osroot, osdev, sign, fstype);
7616eb2bd662Svikram 	INJECT_ERROR1("SET_SIGNATURE_PRIMARY", ret = -1);
7617eb2bd662Svikram 
7618eb2bd662Svikram 	if (ret == 0) {
7619f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7620eb2bd662Svikram 	} else {
7621f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
7622f64ca102SToomas Soome 		bam_error(_("failed to set primary sign (%s) for %s: %s\n"),
7623f64ca102SToomas Soome 		    sign, osroot, osdev);
7624eb2bd662Svikram 
7625eb2bd662Svikram 	}
7626eb2bd662Svikram 	return (ret);
7627eb2bd662Svikram }
7628eb2bd662Svikram 
7629eb2bd662Svikram char *
7630eb2bd662Svikram get_grubsign(char *osroot, char *osdev)
7631eb2bd662Svikram {
7632eb2bd662Svikram 	char		*grubsign;	/* (<sign>,#,#) */
7633eb2bd662Svikram 	char		*slice;
7634eb2bd662Svikram 	int		fdiskpart;
7635eb2bd662Svikram 	char		*sign;
7636eb2bd662Svikram 	char		*fstype;
7637eb2bd662Svikram 	int		ret;
7638eb2bd662Svikram 	const char	*fcn = "get_grubsign()";
7639eb2bd662Svikram 
7640f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osroot, osdev));
7641eb2bd662Svikram 	fstype = get_fstype(osroot);
7642eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBSIGN_FSTYPE", fstype = NULL);
7643eb2bd662Svikram 	if (fstype == NULL) {
7644f64ca102SToomas Soome 		bam_error(_("failed to get fstype for %s\n"), osroot);
7645eb2bd662Svikram 		return (NULL);
7646eb2bd662Svikram 	}
7647eb2bd662Svikram 
7648eb2bd662Svikram 	sign = find_existing_sign(osroot, osdev, fstype);
7649eb2bd662Svikram 	INJECT_ERROR1("FIND_EXISTING_SIGN", sign = NULL);
7650eb2bd662Svikram 	if (sign == NULL) {
7651f64ca102SToomas Soome 		BAM_DPRINTF(("%s: no existing grubsign for %s: %s\n",
7652f64ca102SToomas Soome 		    fcn, osroot, osdev));
7653eb2bd662Svikram 		sign = create_new_sign(osdev, fstype);
7654eb2bd662Svikram 		INJECT_ERROR1("CREATE_NEW_SIGN", sign = NULL);
7655eb2bd662Svikram 		if (sign == NULL) {
7656f64ca102SToomas Soome 			bam_error(_("failed to create GRUB boot signature for "
7657f64ca102SToomas Soome 			    "device: %s\n"), osdev);
7658eb2bd662Svikram 			free(fstype);
7659eb2bd662Svikram 			return (NULL);
7660eb2bd662Svikram 		}
7661eb2bd662Svikram 	}
7662eb2bd662Svikram 
7663eb2bd662Svikram 	ret = set_signature(osroot, osdev, sign, fstype);
7664eb2bd662Svikram 	INJECT_ERROR1("SET_SIGNATURE_FAIL", ret = -1);
7665eb2bd662Svikram 	if (ret == -1) {
7666f64ca102SToomas Soome 		bam_error(_("failed to write GRUB boot signature for "
7667f64ca102SToomas Soome 		    "device: %s\n"), osdev);
7668eb2bd662Svikram 		free(sign);
7669eb2bd662Svikram 		free(fstype);
7670eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
7671eb2bd662Svikram 		return (NULL);
7672eb2bd662Svikram 	}
7673eb2bd662Svikram 
7674eb2bd662Svikram 	free(fstype);
7675eb2bd662Svikram 
7676eb2bd662Svikram 	if (bam_verbose)
7677f64ca102SToomas Soome 		bam_print(_("found or created GRUB signature %s for %s\n"),
7678f64ca102SToomas Soome 		    sign, osdev);
7679eb2bd662Svikram 
7680eb2bd662Svikram 	fdiskpart = get_partition(osdev);
76811a902ef8SHans Rosenfeld 	INJECT_ERROR1("GET_GRUBSIGN_FDISK", fdiskpart = PARTNO_NOTFOUND);
76821a902ef8SHans Rosenfeld 	if (fdiskpart == PARTNO_NOTFOUND) {
7683f64ca102SToomas Soome 		bam_error(_("failed to determine fdisk partition: %s\n"),
7684f64ca102SToomas Soome 		    osdev);
7685eb2bd662Svikram 		free(sign);
7686eb2bd662Svikram 		return (NULL);
7687eb2bd662Svikram 	}
7688eb2bd662Svikram 
7689eb2bd662Svikram 	slice = strrchr(osdev, 's');
7690eb2bd662Svikram 
76911a902ef8SHans Rosenfeld 	if (fdiskpart == PARTNO_EFI) {
76921a902ef8SHans Rosenfeld 		fdiskpart = atoi(&slice[1]);
76931a902ef8SHans Rosenfeld 		slice = NULL;
76941a902ef8SHans Rosenfeld 	}
76951a902ef8SHans Rosenfeld 
7696eb2bd662Svikram 	grubsign = s_calloc(1, MAXNAMELEN + 10);
7697eb2bd662Svikram 	if (slice) {
7698eb2bd662Svikram 		(void) snprintf(grubsign, MAXNAMELEN + 10, "(%s,%d,%c)",
7699eb2bd662Svikram 		    sign, fdiskpart, slice[1] + 'a' - '0');
7700eb2bd662Svikram 	} else
7701eb2bd662Svikram 		(void) snprintf(grubsign, MAXNAMELEN + 10, "(%s,%d)",
7702eb2bd662Svikram 		    sign, fdiskpart);
7703eb2bd662Svikram 
7704eb2bd662Svikram 	free(sign);
7705eb2bd662Svikram 
7706f64ca102SToomas Soome 	BAM_DPRINTF(("%s: successfully created grubsign %s\n", fcn, grubsign));
7707eb2bd662Svikram 
7708eb2bd662Svikram 	return (grubsign);
77097c478bd9Sstevel@tonic-gate }
77107c478bd9Sstevel@tonic-gate 
7711ee3b8144Sszhou static char *
7712ee3b8144Sszhou get_title(char *rootdir)
77137c478bd9Sstevel@tonic-gate {
7714eb2bd662Svikram 	static char	title[80];
7715eb2bd662Svikram 	char		*cp = NULL;
7716eb2bd662Svikram 	char		release[PATH_MAX];
77177c478bd9Sstevel@tonic-gate 	FILE		*fp;
7718eb2bd662Svikram 	const char	*fcn = "get_title()";
77197c478bd9Sstevel@tonic-gate 
77207c478bd9Sstevel@tonic-gate 	/* open the /etc/release file */
77217c478bd9Sstevel@tonic-gate 	(void) snprintf(release, sizeof (release), "%s/etc/release", rootdir);
77227c478bd9Sstevel@tonic-gate 
77237c478bd9Sstevel@tonic-gate 	fp = fopen(release, "r");
7724eb2bd662Svikram 	if (fp == NULL) {
7725f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), release,
7726f64ca102SToomas Soome 		    strerror(errno));
7727eb2bd662Svikram 		cp = NULL;
7728eb2bd662Svikram 		goto out;
7729eb2bd662Svikram 	}
77307c478bd9Sstevel@tonic-gate 
7731655ac28eSGeorge Asaad 	/* grab first line of /etc/release */
7732655ac28eSGeorge Asaad 	cp = s_fgets(title, sizeof (title), fp);
7733655ac28eSGeorge Asaad 	if (cp) {
7734655ac28eSGeorge Asaad 		while (isspace(*cp))    /* remove leading spaces */
7735655ac28eSGeorge Asaad 			cp++;
77367c478bd9Sstevel@tonic-gate 	}
7737655ac28eSGeorge Asaad 
77387c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
7739eb2bd662Svikram 
7740eb2bd662Svikram out:
7741655ac28eSGeorge Asaad 	cp = cp ? cp : "Oracle Solaris";
7742eb2bd662Svikram 
7743f64ca102SToomas Soome 	BAM_DPRINTF(("%s: got title: %s\n", fcn, cp));
7744eb2bd662Svikram 
7745eb2bd662Svikram 	return (cp);
77467c478bd9Sstevel@tonic-gate }
77477c478bd9Sstevel@tonic-gate 
7748ae115bc7Smrj char *
77497c478bd9Sstevel@tonic-gate get_special(char *mountp)
77507c478bd9Sstevel@tonic-gate {
77517c478bd9Sstevel@tonic-gate 	FILE		*mntfp;
7752eb2bd662Svikram 	struct mnttab	mp = {0};
7753eb2bd662Svikram 	struct mnttab	mpref = {0};
7754eb2bd662Svikram 	int		error;
7755eb2bd662Svikram 	int		ret;
7756eb2bd662Svikram 	const char 	*fcn = "get_special()";
7757eb2bd662Svikram 
7758eb2bd662Svikram 	INJECT_ERROR1("GET_SPECIAL_MNTPT", mountp = NULL);
7759eb2bd662Svikram 	if (mountp == NULL) {
7760f64ca102SToomas Soome 		bam_error(_("cannot get special file: NULL mount-point\n"));
7761eb2bd662Svikram 		return (NULL);
7762eb2bd662Svikram 	}
77637c478bd9Sstevel@tonic-gate 
77647c478bd9Sstevel@tonic-gate 	mntfp = fopen(MNTTAB, "r");
7765eb2bd662Svikram 	error = errno;
7766eb2bd662Svikram 	INJECT_ERROR1("GET_SPECIAL_MNTTAB_OPEN", mntfp = NULL);
77677c478bd9Sstevel@tonic-gate 	if (mntfp == NULL) {
7768f64ca102SToomas Soome 		bam_error(_("failed to open file: %s: %s\n"), MNTTAB,
7769f64ca102SToomas Soome 		    strerror(error));
7770eb2bd662Svikram 		return (NULL);
77717c478bd9Sstevel@tonic-gate 	}
77727c478bd9Sstevel@tonic-gate 
77737c478bd9Sstevel@tonic-gate 	if (*mountp == '\0')
77747c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = "/";
77757c478bd9Sstevel@tonic-gate 	else
77767c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = mountp;
7777eb2bd662Svikram 
7778eb2bd662Svikram 	ret = getmntany(mntfp, &mp, &mpref);
7779eb2bd662Svikram 	INJECT_ERROR1("GET_SPECIAL_MNTTAB_SEARCH", ret = 1);
7780eb2bd662Svikram 	if (ret != 0) {
77817c478bd9Sstevel@tonic-gate 		(void) fclose(mntfp);
7782f64ca102SToomas Soome 		BAM_DPRINTF(("%s: Cannot get special file:  mount-point %s "
7783f64ca102SToomas Soome 		    "not in mnttab\n", fcn, mountp));
77847c478bd9Sstevel@tonic-gate 		return (NULL);
77857c478bd9Sstevel@tonic-gate 	}
77867c478bd9Sstevel@tonic-gate 	(void) fclose(mntfp);
77877c478bd9Sstevel@tonic-gate 
7788f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning special: %s\n", fcn, mp.mnt_special));
7789eb2bd662Svikram 
77907c478bd9Sstevel@tonic-gate 	return (s_strdup(mp.mnt_special));
77917c478bd9Sstevel@tonic-gate }
77927c478bd9Sstevel@tonic-gate 
7793eb2bd662Svikram static void
7794eb2bd662Svikram free_physarray(char **physarray, int n)
77957c478bd9Sstevel@tonic-gate {
7796eb2bd662Svikram 	int			i;
7797eb2bd662Svikram 	const char		*fcn = "free_physarray()";
77987c478bd9Sstevel@tonic-gate 
7799eb2bd662Svikram 	assert(physarray);
7800eb2bd662Svikram 	assert(n);
7801eb2bd662Svikram 
7802f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entering args: %d\n", fcn, n));
7803eb2bd662Svikram 
7804eb2bd662Svikram 	for (i = 0; i < n; i++) {
7805eb2bd662Svikram 		free(physarray[i]);
78067c478bd9Sstevel@tonic-gate 	}
7807eb2bd662Svikram 	free(physarray);
7808eb2bd662Svikram 
7809f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7810eb2bd662Svikram }
7811eb2bd662Svikram 
7812eb2bd662Svikram static int
7813eb2bd662Svikram zfs_get_physical(char *special, char ***physarray, int *n)
7814eb2bd662Svikram {
7815eb2bd662Svikram 	char			sdup[PATH_MAX];
7816eb2bd662Svikram 	char			cmd[PATH_MAX];
7817eb2bd662Svikram 	char			dsk[PATH_MAX];
7818eb2bd662Svikram 	char			*pool;
7819eb2bd662Svikram 	filelist_t		flist = {0};
7820eb2bd662Svikram 	line_t			*lp;
7821eb2bd662Svikram 	line_t			*startlp;
7822eb2bd662Svikram 	char			*comp1;
7823eb2bd662Svikram 	int			i;
7824eb2bd662Svikram 	int			ret;
7825eb2bd662Svikram 	const char		*fcn = "zfs_get_physical()";
7826eb2bd662Svikram 
7827eb2bd662Svikram 	assert(special);
7828eb2bd662Svikram 
7829f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. arg: %s\n", fcn, special));
7830eb2bd662Svikram 
7831eb2bd662Svikram 	INJECT_ERROR1("INVALID_ZFS_SPECIAL", special = "/foo");
7832eb2bd662Svikram 	if (special[0] == '/') {
7833f64ca102SToomas Soome 		bam_error(_("invalid device for ZFS filesystem: %s\n"),
7834f64ca102SToomas Soome 		    special);
7835eb2bd662Svikram 		return (-1);
7836eb2bd662Svikram 	}
7837eb2bd662Svikram 
7838eb2bd662Svikram 	(void) strlcpy(sdup, special, sizeof (sdup));
7839eb2bd662Svikram 
7840eb2bd662Svikram 	pool = strtok(sdup, "/");
7841eb2bd662Svikram 	INJECT_ERROR1("ZFS_GET_PHYS_POOL", pool = NULL);
7842eb2bd662Svikram 	if (pool == NULL) {
7843f64ca102SToomas Soome 		bam_error(_("cannot derive ZFS pool from special: %s\n"),
7844f64ca102SToomas Soome 		    special);
7845eb2bd662Svikram 		return (-1);
7846eb2bd662Svikram 	}
7847eb2bd662Svikram 
7848eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/zpool status %s", pool);
7849eb2bd662Svikram 
7850eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
7851eb2bd662Svikram 	INJECT_ERROR1("ZFS_GET_PHYS_STATUS", ret = 1);
7852eb2bd662Svikram 	if (ret != 0) {
7853f64ca102SToomas Soome 		bam_error(_("cannot get zpool status for pool: %s\n"), pool);
7854eb2bd662Svikram 		return (-1);
7855eb2bd662Svikram 	}
7856eb2bd662Svikram 
7857eb2bd662Svikram 	INJECT_ERROR1("ZFS_GET_PHYS_STATUS_OUT", flist.head = NULL);
7858eb2bd662Svikram 	if (flist.head == NULL) {
7859f64ca102SToomas Soome 		bam_error(_("bad zpool status for pool=%s\n"), pool);
7860eb2bd662Svikram 		filelist_free(&flist);
7861eb2bd662Svikram 		return (-1);
7862eb2bd662Svikram 	}
7863eb2bd662Svikram 
7864eb2bd662Svikram 	for (lp = flist.head; lp; lp = lp->next) {
7865f64ca102SToomas Soome 		BAM_DPRINTF(("%s: strtok() zpool status line=%s\n",
7866f64ca102SToomas Soome 		    fcn, lp->line));
7867eb2bd662Svikram 		comp1 = strtok(lp->line, " \t");
7868eb2bd662Svikram 		if (comp1 == NULL) {
7869eb2bd662Svikram 			free(lp->line);
7870eb2bd662Svikram 			lp->line = NULL;
7871eb2bd662Svikram 		} else {
7872eb2bd662Svikram 			comp1 = s_strdup(comp1);
7873eb2bd662Svikram 			free(lp->line);
7874eb2bd662Svikram 			lp->line = comp1;
7875eb2bd662Svikram 		}
7876eb2bd662Svikram 	}
7877eb2bd662Svikram 
7878eb2bd662Svikram 	for (lp = flist.head; lp; lp = lp->next) {
7879eb2bd662Svikram 		if (lp->line == NULL)
7880eb2bd662Svikram 			continue;
7881eb2bd662Svikram 		if (strcmp(lp->line, pool) == 0) {
7882f64ca102SToomas Soome 			BAM_DPRINTF(("%s: found pool name: %s in zpool "
7883f64ca102SToomas Soome 			    "status\n", fcn, pool));
7884eb2bd662Svikram 			break;
7885eb2bd662Svikram 		}
7886eb2bd662Svikram 	}
7887eb2bd662Svikram 
7888eb2bd662Svikram 	if (lp == NULL) {
7889f64ca102SToomas Soome 		bam_error(_("no pool name %s in zpool status\n"), pool);
7890eb2bd662Svikram 		filelist_free(&flist);
7891eb2bd662Svikram 		return (-1);
7892eb2bd662Svikram 	}
7893eb2bd662Svikram 
7894eb2bd662Svikram 	startlp = lp->next;
7895eb2bd662Svikram 	for (i = 0, lp = startlp; lp; lp = lp->next) {
7896eb2bd662Svikram 		if (lp->line == NULL)
7897eb2bd662Svikram 			continue;
7898eb2bd662Svikram 		if (strcmp(lp->line, "mirror") == 0)
7899eb2bd662Svikram 			continue;
7900eb2bd662Svikram 		if (lp->line[0] == '\0' || strcmp(lp->line, "errors:") == 0)
7901eb2bd662Svikram 			break;
7902eb2bd662Svikram 		i++;
7903f64ca102SToomas Soome 		BAM_DPRINTF(("%s: counting phys slices in zpool status: %d\n",
7904f64ca102SToomas Soome 		    fcn, i));
7905eb2bd662Svikram 	}
7906eb2bd662Svikram 
7907eb2bd662Svikram 	if (i == 0) {
7908f64ca102SToomas Soome 		bam_error(_("no physical device in zpool status for pool=%s\n"),
7909f64ca102SToomas Soome 		    pool);
7910eb2bd662Svikram 		filelist_free(&flist);
7911eb2bd662Svikram 		return (-1);
7912eb2bd662Svikram 	}
7913eb2bd662Svikram 
7914eb2bd662Svikram 	*n = i;
7915eb2bd662Svikram 	*physarray = s_calloc(*n, sizeof (char *));
7916eb2bd662Svikram 	for (i = 0, lp = startlp; lp; lp = lp->next) {
7917eb2bd662Svikram 		if (lp->line == NULL)
7918eb2bd662Svikram 			continue;
7919eb2bd662Svikram 		if (strcmp(lp->line, "mirror") == 0)
7920eb2bd662Svikram 			continue;
7921eb2bd662Svikram 		if (strcmp(lp->line, "errors:") == 0)
7922eb2bd662Svikram 			break;
7923eb2bd662Svikram 		if (strncmp(lp->line, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
7924eb2bd662Svikram 		    strncmp(lp->line, "/dev/rdsk/",
7925eb2bd662Svikram 		    strlen("/dev/rdsk/")) != 0)  {
7926e998e519SSheshadri Vasudevan 			(void) snprintf(dsk, sizeof (dsk), "/dev/rdsk/%s",
7927eb2bd662Svikram 			    lp->line);
7928eb2bd662Svikram 		} else {
7929eb2bd662Svikram 			(void) strlcpy(dsk, lp->line, sizeof (dsk));
7930eb2bd662Svikram 		}
7931f64ca102SToomas Soome 		BAM_DPRINTF(("%s: adding phys slice=%s from pool %s status\n",
7932f64ca102SToomas Soome 		    fcn, dsk, pool));
7933eb2bd662Svikram 		(*physarray)[i++] = s_strdup(dsk);
7934eb2bd662Svikram 	}
7935eb2bd662Svikram 
7936eb2bd662Svikram 	assert(i == *n);
7937eb2bd662Svikram 
7938eb2bd662Svikram 	filelist_free(&flist);
7939eb2bd662Svikram 
7940f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7941eb2bd662Svikram 	return (0);
7942eb2bd662Svikram }
7943eb2bd662Svikram 
7944eb2bd662Svikram static int
7945eb2bd662Svikram get_physical(char *menu_root, char ***physarray, int *n)
7946eb2bd662Svikram {
7947eb2bd662Svikram 	char			*special;
7948eb2bd662Svikram 	int			ret;
7949eb2bd662Svikram 	const char		*fcn = "get_physical()";
7950eb2bd662Svikram 
7951eb2bd662Svikram 	assert(menu_root);
7952eb2bd662Svikram 	assert(physarray);
7953eb2bd662Svikram 	assert(n);
7954eb2bd662Svikram 
7955eb2bd662Svikram 	*physarray = NULL;
7956eb2bd662Svikram 	*n = 0;
7957eb2bd662Svikram 
7958f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. arg: %s\n", fcn, menu_root));
7959eb2bd662Svikram 
7960eb2bd662Svikram 	/* First get the device special file from /etc/mnttab */
7961eb2bd662Svikram 	special = get_special(menu_root);
7962eb2bd662Svikram 	INJECT_ERROR1("GET_PHYSICAL_SPECIAL", special = NULL);
7963eb2bd662Svikram 	if (special == NULL) {
7964f64ca102SToomas Soome 		bam_error(_("cannot get special file for mount-point: %s\n"),
7965f64ca102SToomas Soome 		    menu_root);
7966eb2bd662Svikram 		return (-1);
7967eb2bd662Svikram 	}
7968eb2bd662Svikram 
7969eb2bd662Svikram 	/* If already a physical device nothing to do */
7970eb2bd662Svikram 	if (strncmp(special, "/dev/dsk/", strlen("/dev/dsk/")) == 0 ||
7971eb2bd662Svikram 	    strncmp(special, "/dev/rdsk/", strlen("/dev/rdsk/")) == 0) {
7972f64ca102SToomas Soome 		BAM_DPRINTF(("%s: got physical device already directly for "
7973f64ca102SToomas Soome 		    "menu_root=%s special=%s\n", fcn, menu_root, special));
7974f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
7975eb2bd662Svikram 		*physarray = s_calloc(1, sizeof (char *));
7976eb2bd662Svikram 		(*physarray)[0] = special;
7977eb2bd662Svikram 		*n = 1;
7978eb2bd662Svikram 		return (0);
7979eb2bd662Svikram 	}
7980eb2bd662Svikram 
7981eb2bd662Svikram 	if (is_zfs(menu_root)) {
7982eb2bd662Svikram 		ret = zfs_get_physical(special, physarray, n);
7983eb2bd662Svikram 	} else {
7984f64ca102SToomas Soome 		bam_error(_("cannot derive physical device for %s (%s), "
7985f64ca102SToomas Soome 		    "unsupported filesystem\n"), menu_root, special);
7986eb2bd662Svikram 		ret = -1;
7987eb2bd662Svikram 	}
7988eb2bd662Svikram 
7989eb2bd662Svikram 	free(special);
7990eb2bd662Svikram 
7991eb2bd662Svikram 	INJECT_ERROR1("GET_PHYSICAL_RET", ret = -1);
7992eb2bd662Svikram 	if (ret == -1) {
7993f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
7994eb2bd662Svikram 	} else {
7995eb2bd662Svikram 		int	i;
7996eb2bd662Svikram 		assert (*n > 0);
7997eb2bd662Svikram 		for (i = 0; i < *n; i++) {
7998f64ca102SToomas Soome 			BAM_DPRINTF(("%s: returning physical=%s\n",
7999f64ca102SToomas Soome 			    fcn, (*physarray)[i]));
8000eb2bd662Svikram 		}
8001eb2bd662Svikram 	}
8002eb2bd662Svikram 
8003eb2bd662Svikram 	return (ret);
8004eb2bd662Svikram }
8005eb2bd662Svikram 
8006eb2bd662Svikram static int
8007eb2bd662Svikram is_bootdisk(char *osroot, char *physical)
8008eb2bd662Svikram {
8009eb2bd662Svikram 	int			ret;
8010eb2bd662Svikram 	char			*grubroot;
8011eb2bd662Svikram 	char			*bootp;
8012eb2bd662Svikram 	const char		*fcn = "is_bootdisk()";
8013eb2bd662Svikram 
8014eb2bd662Svikram 	assert(osroot);
8015eb2bd662Svikram 	assert(physical);
8016eb2bd662Svikram 
8017f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osroot, physical));
8018eb2bd662Svikram 
8019eb2bd662Svikram 	bootp = strstr(physical, "p0:boot");
8020eb2bd662Svikram 	if (bootp)
8021eb2bd662Svikram 		*bootp = '\0';
8022eb2bd662Svikram 	/*
8023eb2bd662Svikram 	 * We just want the BIOS mapping for menu disk.
8024eb2bd662Svikram 	 * Don't pass menu_root to get_grubroot() as the
8025eb2bd662Svikram 	 * check that it is used for is not relevant here.
8026eb2bd662Svikram 	 * The osroot is immaterial as well - it is only used to
8027eb2bd662Svikram 	 * to find create_diskmap script. Everything hinges on
8028eb2bd662Svikram 	 * "physical"
8029eb2bd662Svikram 	 */
8030eb2bd662Svikram 	grubroot = get_grubroot(osroot, physical, NULL);
8031eb2bd662Svikram 
8032eb2bd662Svikram 	INJECT_ERROR1("IS_BOOTDISK_GRUBROOT", grubroot = NULL);
8033eb2bd662Svikram 	if (grubroot == NULL) {
803437eb779cSVikram Hegde 		if (bam_verbose)
8035f64ca102SToomas Soome 			bam_error(_("cannot determine BIOS disk ID 'hd?' for "
8036f64ca102SToomas Soome 			    "disk: %s\n"), physical);
8037eb2bd662Svikram 		return (0);
8038eb2bd662Svikram 	}
8039eb2bd662Svikram 	ret = grubroot[3] == '0';
8040eb2bd662Svikram 	free(grubroot);
8041eb2bd662Svikram 
8042f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning ret = %d\n", fcn, ret));
8043eb2bd662Svikram 
8044eb2bd662Svikram 	return (ret);
8045eb2bd662Svikram }
8046eb2bd662Svikram 
8047eb2bd662Svikram /*
8048eb2bd662Svikram  * Check if menu is on the boot device
80497c478bd9Sstevel@tonic-gate  * Return 0 (false) on error
80507c478bd9Sstevel@tonic-gate  */
80517c478bd9Sstevel@tonic-gate static int
8052eb2bd662Svikram menu_on_bootdisk(char *osroot, char *menu_root)
80537c478bd9Sstevel@tonic-gate {
8054eb2bd662Svikram 	char		**physarray;
80557c478bd9Sstevel@tonic-gate 	int		ret;
8056eb2bd662Svikram 	int		n;
8057eb2bd662Svikram 	int		i;
8058eb2bd662Svikram 	int		on_bootdisk;
8059eb2bd662Svikram 	const char	*fcn = "menu_on_bootdisk()";
80607c478bd9Sstevel@tonic-gate 
8061f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osroot, menu_root));
80627c478bd9Sstevel@tonic-gate 
8063eb2bd662Svikram 	ret = get_physical(menu_root, &physarray, &n);
8064eb2bd662Svikram 	INJECT_ERROR1("MENU_ON_BOOTDISK_PHYSICAL", ret = -1);
8065eb2bd662Svikram 	if (ret != 0) {
8066f64ca102SToomas Soome 		bam_error(_("cannot get physical device special file for menu "
8067f64ca102SToomas Soome 		    "root: %s\n"), menu_root);
80687c478bd9Sstevel@tonic-gate 		return (0);
8069eb2bd662Svikram 	}
8070eb2bd662Svikram 
8071eb2bd662Svikram 	assert(physarray);
8072eb2bd662Svikram 	assert(n > 0);
8073eb2bd662Svikram 
8074eb2bd662Svikram 	on_bootdisk = 0;
8075eb2bd662Svikram 	for (i = 0; i < n; i++) {
8076eb2bd662Svikram 		assert(strncmp(physarray[i], "/dev/dsk/",
8077eb2bd662Svikram 		    strlen("/dev/dsk/")) == 0 ||
8078eb2bd662Svikram 		    strncmp(physarray[i], "/dev/rdsk/",
8079eb2bd662Svikram 		    strlen("/dev/rdsk/")) == 0);
8080eb2bd662Svikram 
8081f64ca102SToomas Soome 		BAM_DPRINTF(("%s: checking if phys-device=%s is on bootdisk\n",
8082f64ca102SToomas Soome 		    fcn, physarray[i]));
8083eb2bd662Svikram 		if (is_bootdisk(osroot, physarray[i])) {
8084eb2bd662Svikram 			on_bootdisk = 1;
8085f64ca102SToomas Soome 			BAM_DPRINTF(("%s: phys-device=%s *IS* on bootdisk\n",
8086f64ca102SToomas Soome 			    fcn, physarray[i]));
8087eb2bd662Svikram 		}
8088eb2bd662Svikram 	}
8089eb2bd662Svikram 
8090eb2bd662Svikram 	free_physarray(physarray, n);
8091eb2bd662Svikram 
8092eb2bd662Svikram 	INJECT_ERROR1("ON_BOOTDISK_YES", on_bootdisk = 1);
8093eb2bd662Svikram 	INJECT_ERROR1("ON_BOOTDISK_NO", on_bootdisk = 0);
8094eb2bd662Svikram 	if (on_bootdisk) {
8095f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
8096eb2bd662Svikram 	} else {
8097f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
8098eb2bd662Svikram 	}
8099eb2bd662Svikram 
8100eb2bd662Svikram 	return (on_bootdisk);
8101eb2bd662Svikram }
8102eb2bd662Svikram 
8103eb2bd662Svikram void
8104eb2bd662Svikram bam_add_line(menu_t *mp, entry_t *entry, line_t *prev, line_t *lp)
8105eb2bd662Svikram {
8106eb2bd662Svikram 	const char	*fcn = "bam_add_line()";
8107eb2bd662Svikram 
8108eb2bd662Svikram 	assert(mp);
8109eb2bd662Svikram 	assert(entry);
8110eb2bd662Svikram 	assert(prev);
8111eb2bd662Svikram 	assert(lp);
8112eb2bd662Svikram 
8113eb2bd662Svikram 	lp->next = prev->next;
8114eb2bd662Svikram 	if (prev->next) {
8115f64ca102SToomas Soome 		BAM_DPRINTF(("%s: previous next exists\n", fcn));
8116eb2bd662Svikram 		prev->next->prev = lp;
8117eb2bd662Svikram 	} else {
8118f64ca102SToomas Soome 		BAM_DPRINTF(("%s: previous next does not exist\n", fcn));
8119eb2bd662Svikram 	}
8120eb2bd662Svikram 	prev->next = lp;
8121eb2bd662Svikram 	lp->prev = prev;
8122eb2bd662Svikram 
8123eb2bd662Svikram 	if (entry->end == prev) {
8124f64ca102SToomas Soome 		BAM_DPRINTF(("%s: last line in entry\n", fcn));
8125eb2bd662Svikram 		entry->end = lp;
8126eb2bd662Svikram 	}
8127eb2bd662Svikram 	if (mp->end == prev) {
8128eb2bd662Svikram 		assert(lp->next == NULL);
8129eb2bd662Svikram 		mp->end = lp;
8130f64ca102SToomas Soome 		BAM_DPRINTF(("%s: last line in menu\n", fcn));
8131eb2bd662Svikram 	}
81327c478bd9Sstevel@tonic-gate }
81337c478bd9Sstevel@tonic-gate 
81348c1b6884Sszhou /*
81358c1b6884Sszhou  * look for matching bootadm entry with specified parameters
81368c1b6884Sszhou  * Here are the rules (based on existing usage):
81378c1b6884Sszhou  * - If title is specified, match on title only
8138eb2bd662Svikram  * - Else, match on root/findroot, kernel, and module.
8139eb2bd662Svikram  *   Note that, if root_opt is non-zero, the absence of
8140eb2bd662Svikram  *   root line is considered a match.
81418c1b6884Sszhou  */
81428c1b6884Sszhou static entry_t *
8143eb2bd662Svikram find_boot_entry(
8144eb2bd662Svikram 	menu_t *mp,
8145eb2bd662Svikram 	char *title,
8146eb2bd662Svikram 	char *kernel,
8147eb2bd662Svikram 	char *findroot,
8148eb2bd662Svikram 	char *root,
8149eb2bd662Svikram 	char *module,
8150eb2bd662Svikram 	int root_opt,
8151eb2bd662Svikram 	int *entry_num)
81528c1b6884Sszhou {
81538c1b6884Sszhou 	int		i;
81548c1b6884Sszhou 	line_t		*lp;
81558c1b6884Sszhou 	entry_t		*ent;
8156eb2bd662Svikram 	const char	*fcn = "find_boot_entry()";
8157eb2bd662Svikram 
8158eb2bd662Svikram 	if (entry_num)
8159eb2bd662Svikram 		*entry_num = BAM_ERROR;
81608c1b6884Sszhou 
81618c1b6884Sszhou 	/* find matching entry */
81628c1b6884Sszhou 	for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) {
81638c1b6884Sszhou 		lp = ent->start;
81648c1b6884Sszhou 
81658c1b6884Sszhou 		/* first line of entry must be bootadm comment */
81668c1b6884Sszhou 		lp = ent->start;
8167ae115bc7Smrj 		if (lp->flags != BAM_COMMENT ||
8168ae115bc7Smrj 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0) {
81698c1b6884Sszhou 			continue;
81708c1b6884Sszhou 		}
81718c1b6884Sszhou 
81728c1b6884Sszhou 		/* advance to title line */
81738c1b6884Sszhou 		lp = lp->next;
81748c1b6884Sszhou 		if (title) {
81758c1b6884Sszhou 			if (lp->flags == BAM_TITLE && lp->arg &&
8176eb2bd662Svikram 			    strcmp(lp->arg, title) == 0) {
8177f64ca102SToomas Soome 				BAM_DPRINTF(("%s: matched title: %s\n",
8178f64ca102SToomas Soome 				    fcn, title));
81798c1b6884Sszhou 				break;
8180eb2bd662Svikram 			}
8181f64ca102SToomas Soome 			BAM_DPRINTF(("%s: no match title: %s, %s\n",
8182f64ca102SToomas Soome 			    fcn, title, lp->arg));
81838c1b6884Sszhou 			continue;	/* check title only */
81848c1b6884Sszhou 		}
81858c1b6884Sszhou 
81868c1b6884Sszhou 		lp = lp->next;	/* advance to root line */
8187843e1988Sjohnlev 		if (lp == NULL) {
8188843e1988Sjohnlev 			continue;
8189eac223ccSWilliam Kucharski 		} else if (lp->cmd != NULL &&
8190eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[FINDROOT_CMD]) == 0) {
8191eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_NULL_FINDROOT",
8192eb2bd662Svikram 			    findroot = NULL);
8193eb2bd662Svikram 			if (findroot == NULL) {
8194f64ca102SToomas Soome 				BAM_DPRINTF(("%s: no match line has findroot, "
8195f64ca102SToomas Soome 				    "we don't: %s\n", fcn, lp->arg));
81968c1b6884Sszhou 				continue;
81978c1b6884Sszhou 			}
8198eb2bd662Svikram 			/* findroot command found, try match  */
8199eb2bd662Svikram 			if (strcmp(lp->arg, findroot) != 0) {
8200f64ca102SToomas Soome 				BAM_DPRINTF(("%s: no match findroot: %s, %s\n",
8201eb2bd662Svikram 				    fcn, findroot, lp->arg));
8202eb2bd662Svikram 				continue;
8203eb2bd662Svikram 			}
8204f64ca102SToomas Soome 			BAM_DPRINTF(("%s: matched findroot: %s\n",
8205f64ca102SToomas Soome 			    fcn, findroot));
8206eb2bd662Svikram 			lp = lp->next;	/* advance to kernel line */
8207eac223ccSWilliam Kucharski 		} else if (lp->cmd != NULL &&
8208eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
8209eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_NULL_ROOT", root = NULL);
8210eb2bd662Svikram 			if (root == NULL) {
8211f64ca102SToomas Soome 				BAM_DPRINTF(("%s: no match, line has root, we "
8212f64ca102SToomas Soome 				    "don't: %s\n", fcn, lp->arg));
8213eb2bd662Svikram 				continue;
8214eb2bd662Svikram 			}
8215eb2bd662Svikram 			/* root cmd found, try match */
8216eb2bd662Svikram 			if (strcmp(lp->arg, root) != 0) {
8217f64ca102SToomas Soome 				BAM_DPRINTF(("%s: no match root: %s, %s\n",
8218eb2bd662Svikram 				    fcn, root, lp->arg));
8219eb2bd662Svikram 				continue;
8220eb2bd662Svikram 			}
8221f64ca102SToomas Soome 			BAM_DPRINTF(("%s: matched root: %s\n", fcn, root));
82228c1b6884Sszhou 			lp = lp->next;	/* advance to kernel line */
82238c1b6884Sszhou 		} else {
8224eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_ROOT_OPT_NO",
8225eb2bd662Svikram 			    root_opt = 0);
8226eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_ROOT_OPT_YES",
8227eb2bd662Svikram 			    root_opt = 1);
82288c1b6884Sszhou 			/* no root command, see if root is optional */
82298c1b6884Sszhou 			if (root_opt == 0) {
8230f64ca102SToomas Soome 				BAM_DPRINTF(("%s: root NOT optional\n", fcn));
82318c1b6884Sszhou 				continue;
82328c1b6884Sszhou 			}
8233f64ca102SToomas Soome 			BAM_DPRINTF(("%s: root IS optional\n", fcn));
82348c1b6884Sszhou 		}
82358c1b6884Sszhou 
82368c1b6884Sszhou 		if (lp == NULL || lp->next == NULL) {
82378c1b6884Sszhou 			continue;
82388c1b6884Sszhou 		}
82398c1b6884Sszhou 
8240843e1988Sjohnlev 		if (kernel &&
8241843e1988Sjohnlev 		    (!check_cmd(lp->cmd, KERNEL_CMD, lp->arg, kernel))) {
8242bbcc54bdSEnrico Perla - Sun Microsystems 			if (!(ent->flags & BAM_ENTRY_FAILSAFE) ||
8243bbcc54bdSEnrico Perla - Sun Microsystems 			    !(ent->flags & BAM_ENTRY_DBOOT) ||
8244bbcc54bdSEnrico Perla - Sun Microsystems 			    strcmp(kernel, DIRECT_BOOT_FAILSAFE_LINE) != 0)
82458c1b6884Sszhou 				continue;
8246bbcc54bdSEnrico Perla - Sun Microsystems 
8247bbcc54bdSEnrico Perla - Sun Microsystems 			ent->flags |= BAM_ENTRY_UPGFSKERNEL;
8248bbcc54bdSEnrico Perla - Sun Microsystems 
82498c1b6884Sszhou 		}
8250f64ca102SToomas Soome 		BAM_DPRINTF(("%s: kernel match: %s, %s\n", fcn,
8251f64ca102SToomas Soome 		    kernel, lp->arg));
8252843e1988Sjohnlev 
8253843e1988Sjohnlev 		/*
8254843e1988Sjohnlev 		 * Check for matching module entry (failsafe or normal).
8255843e1988Sjohnlev 		 * If it fails to match, we go around the loop again.
8256843e1988Sjohnlev 		 * For xpv entries, there are two module lines, so we
8257843e1988Sjohnlev 		 * do the check twice.
8258843e1988Sjohnlev 		 */
8259843e1988Sjohnlev 		lp = lp->next;	/* advance to module line */
8260843e1988Sjohnlev 		if (check_cmd(lp->cmd, MODULE_CMD, lp->arg, module) ||
8261843e1988Sjohnlev 		    (((lp = lp->next) != NULL) &&
8262843e1988Sjohnlev 		    check_cmd(lp->cmd, MODULE_CMD, lp->arg, module))) {
8263843e1988Sjohnlev 			/* match found */
8264f64ca102SToomas Soome 			BAM_DPRINTF(("%s: module match: %s, %s\n", fcn,
8265f64ca102SToomas Soome 			    module, lp->arg));
8266843e1988Sjohnlev 			break;
8267843e1988Sjohnlev 		}
8268bbcc54bdSEnrico Perla - Sun Microsystems 
8269bbcc54bdSEnrico Perla - Sun Microsystems 		if (strcmp(module, FAILSAFE_ARCHIVE) == 0 &&
8270bbcc54bdSEnrico Perla - Sun Microsystems 		    (strcmp(lp->prev->arg, FAILSAFE_ARCHIVE_32) == 0 ||
8271bbcc54bdSEnrico Perla - Sun Microsystems 		    strcmp(lp->prev->arg, FAILSAFE_ARCHIVE_64) == 0)) {
8272bbcc54bdSEnrico Perla - Sun Microsystems 			ent->flags |= BAM_ENTRY_UPGFSMODULE;
8273bbcc54bdSEnrico Perla - Sun Microsystems 			break;
8274bbcc54bdSEnrico Perla - Sun Microsystems 		}
8275bbcc54bdSEnrico Perla - Sun Microsystems 
82768c1b6884Sszhou 	}
82778c1b6884Sszhou 
8278eb2bd662Svikram 	if (ent && entry_num) {
82798c1b6884Sszhou 		*entry_num = i;
8280843e1988Sjohnlev 	}
8281eb2bd662Svikram 
8282eb2bd662Svikram 	if (ent) {
8283f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning ret = %d\n", fcn, i));
8284eb2bd662Svikram 	} else {
8285f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning ret = %d\n", fcn, BAM_ERROR));
8286eb2bd662Svikram 	}
82878c1b6884Sszhou 	return (ent);
82888c1b6884Sszhou }
82898c1b6884Sszhou 
82908c1b6884Sszhou static int
8291eb2bd662Svikram update_boot_entry(menu_t *mp, char *title, char *findroot, char *root,
8292eb2bd662Svikram     char *kernel, char *mod_kernel, char *module, int root_opt)
82938c1b6884Sszhou {
8294eb2bd662Svikram 	int		i;
8295eb2bd662Svikram 	int		change_kernel = 0;
82968c1b6884Sszhou 	entry_t		*ent;
82978c1b6884Sszhou 	line_t		*lp;
8298eb2bd662Svikram 	line_t		*tlp;
82998c1b6884Sszhou 	char		linebuf[BAM_MAXLINE];
8300eb2bd662Svikram 	const char	*fcn = "update_boot_entry()";
83018c1b6884Sszhou 
83028c1b6884Sszhou 	/* note: don't match on title, it's updated on upgrade */
8303eb2bd662Svikram 	ent = find_boot_entry(mp, NULL, kernel, findroot, root, module,
8304eb2bd662Svikram 	    root_opt, &i);
8305ae115bc7Smrj 	if ((ent == NULL) && (bam_direct == BAM_DIRECT_DBOOT)) {
8306ae115bc7Smrj 		/*
8307ae115bc7Smrj 		 * We may be upgrading a kernel from multiboot to
8308eb2bd662Svikram 		 * directboot.  Look for a multiboot entry. A multiboot
8309eb2bd662Svikram 		 * entry will not have a findroot line.
8310ae115bc7Smrj 		 */
8311eb2bd662Svikram 		ent = find_boot_entry(mp, NULL, "multiboot", NULL, root,
8312eb2bd662Svikram 		    MULTIBOOT_ARCHIVE, root_opt, &i);
8313ae115bc7Smrj 		if (ent != NULL) {
8314f64ca102SToomas Soome 			BAM_DPRINTF(("%s: upgrading entry from dboot to "
8315f64ca102SToomas Soome 			    "multiboot: root = %s\n", fcn, root));
8316ae115bc7Smrj 			change_kernel = 1;
8317ae115bc7Smrj 		}
8318eb2bd662Svikram 	} else if (ent) {
8319f64ca102SToomas Soome 		BAM_DPRINTF(("%s: found entry with matching findroot: %s\n",
8320f64ca102SToomas Soome 		    fcn, findroot));
8321ae115bc7Smrj 	}
83228c1b6884Sszhou 
8323eb2bd662Svikram 	if (ent == NULL) {
8324f64ca102SToomas Soome 		BAM_DPRINTF(("%s: boot entry not found in menu. Creating "
8325f64ca102SToomas Soome 		    "new entry, findroot = %s\n", fcn, findroot));
8326eb2bd662Svikram 		return (add_boot_entry(mp, title, findroot,
832744da779fSWilliam Kucharski 		    kernel, mod_kernel, module, NULL));
8328eb2bd662Svikram 	}
8329eb2bd662Svikram 
8330eb2bd662Svikram 	/* replace title of existing entry and update findroot line */
83318c1b6884Sszhou 	lp = ent->start;
83328c1b6884Sszhou 	lp = lp->next;	/* title line */
83338c1b6884Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
83348c1b6884Sszhou 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
83358c1b6884Sszhou 	free(lp->arg);
83368c1b6884Sszhou 	free(lp->line);
83378c1b6884Sszhou 	lp->arg = s_strdup(title);
83388c1b6884Sszhou 	lp->line = s_strdup(linebuf);
8339f64ca102SToomas Soome 	BAM_DPRINTF(("%s: changing title to: %s\n", fcn, title));
83408c1b6884Sszhou 
8341eb2bd662Svikram 	tlp = lp;	/* title line */
83428c1b6884Sszhou 	lp = lp->next;	/* root line */
8343eb2bd662Svikram 
8344eb2bd662Svikram 	/* if no root or findroot command, create a new line_t */
8345eac223ccSWilliam Kucharski 	if ((lp->cmd != NULL) && (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) != 0 &&
8346eac223ccSWilliam Kucharski 	    strcmp(lp->cmd, menu_cmds[FINDROOT_CMD]) != 0)) {
8347eb2bd662Svikram 		lp = s_calloc(1, sizeof (line_t));
8348eb2bd662Svikram 		bam_add_line(mp, ent, tlp, lp);
8349eb2bd662Svikram 	} else {
8350eac223ccSWilliam Kucharski 		if (lp->cmd != NULL)
8351eb2bd662Svikram 			free(lp->cmd);
8352eac223ccSWilliam Kucharski 
8353eb2bd662Svikram 		free(lp->sep);
8354eb2bd662Svikram 		free(lp->arg);
8355eb2bd662Svikram 		free(lp->line);
83568c1b6884Sszhou 	}
8357ae115bc7Smrj 
8358eb2bd662Svikram 	lp->cmd = s_strdup(menu_cmds[FINDROOT_CMD]);
8359eb2bd662Svikram 	lp->sep = s_strdup(menu_cmds[SEP_CMD]);
8360eb2bd662Svikram 	lp->arg = s_strdup(findroot);
8361eb2bd662Svikram 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
8362eb2bd662Svikram 	    menu_cmds[FINDROOT_CMD], menu_cmds[SEP_CMD], findroot);
8363eb2bd662Svikram 	lp->line = s_strdup(linebuf);
8364f64ca102SToomas Soome 	BAM_DPRINTF(("%s: adding findroot line: %s\n", fcn, findroot));
8365eb2bd662Svikram 
8366eb2bd662Svikram 	/* kernel line */
8367eb2bd662Svikram 	lp = lp->next;
8368eb2bd662Svikram 
8369bbcc54bdSEnrico Perla - Sun Microsystems 	if (ent->flags & BAM_ENTRY_UPGFSKERNEL) {
8370bbcc54bdSEnrico Perla - Sun Microsystems 		char		*params = NULL;
8371bbcc54bdSEnrico Perla - Sun Microsystems 
8372bbcc54bdSEnrico Perla - Sun Microsystems 		params = strstr(lp->line, "-s");
8373bbcc54bdSEnrico Perla - Sun Microsystems 		if (params != NULL)
8374bbcc54bdSEnrico Perla - Sun Microsystems 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s%s",
8375bbcc54bdSEnrico Perla - Sun Microsystems 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
8376bbcc54bdSEnrico Perla - Sun Microsystems 			    kernel, params+2);
8377bbcc54bdSEnrico Perla - Sun Microsystems 		else
8378bbcc54bdSEnrico Perla - Sun Microsystems 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
8379bbcc54bdSEnrico Perla - Sun Microsystems 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
8380bbcc54bdSEnrico Perla - Sun Microsystems 			    kernel);
8381bbcc54bdSEnrico Perla - Sun Microsystems 
8382eac223ccSWilliam Kucharski 		if (lp->cmd != NULL)
8383bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->cmd);
8384eac223ccSWilliam Kucharski 
8385bbcc54bdSEnrico Perla - Sun Microsystems 		free(lp->arg);
8386bbcc54bdSEnrico Perla - Sun Microsystems 		free(lp->line);
8387bbcc54bdSEnrico Perla - Sun Microsystems 		lp->cmd = s_strdup(menu_cmds[KERNEL_DOLLAR_CMD]);
8388bbcc54bdSEnrico Perla - Sun Microsystems 		lp->arg = s_strdup(strstr(linebuf, "/"));
8389bbcc54bdSEnrico Perla - Sun Microsystems 		lp->line = s_strdup(linebuf);
8390bbcc54bdSEnrico Perla - Sun Microsystems 		ent->flags &= ~BAM_ENTRY_UPGFSKERNEL;
8391f64ca102SToomas Soome 		BAM_DPRINTF(("%s: adding new kernel$ line: %s\n",
8392f64ca102SToomas Soome 		    fcn, lp->prev->cmd));
8393bbcc54bdSEnrico Perla - Sun Microsystems 	}
8394bbcc54bdSEnrico Perla - Sun Microsystems 
8395ae115bc7Smrj 	if (change_kernel) {
8396ae115bc7Smrj 		/*
8397ae115bc7Smrj 		 * We're upgrading from multiboot to directboot.
8398ae115bc7Smrj 		 */
8399eac223ccSWilliam Kucharski 		if (lp->cmd != NULL &&
8400eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[KERNEL_CMD]) == 0) {
8401ae115bc7Smrj 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
8402ae115bc7Smrj 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
8403ae115bc7Smrj 			    kernel);
8404eb2bd662Svikram 			free(lp->cmd);
8405ae115bc7Smrj 			free(lp->arg);
8406ae115bc7Smrj 			free(lp->line);
8407eb2bd662Svikram 			lp->cmd = s_strdup(menu_cmds[KERNEL_DOLLAR_CMD]);
8408ae115bc7Smrj 			lp->arg = s_strdup(kernel);
8409ae115bc7Smrj 			lp->line = s_strdup(linebuf);
8410ae115bc7Smrj 			lp = lp->next;
8411f64ca102SToomas Soome 			BAM_DPRINTF(("%s: adding new kernel$ line: %s\n",
8412f64ca102SToomas Soome 			    fcn, kernel));
8413ae115bc7Smrj 		}
8414eac223ccSWilliam Kucharski 		if (lp->cmd != NULL &&
8415eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) {
8416ae115bc7Smrj 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
8417ae115bc7Smrj 			    menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD],
8418ae115bc7Smrj 			    module);
8419eb2bd662Svikram 			free(lp->cmd);
8420ae115bc7Smrj 			free(lp->arg);
8421ae115bc7Smrj 			free(lp->line);
8422eb2bd662Svikram 			lp->cmd = s_strdup(menu_cmds[MODULE_DOLLAR_CMD]);
8423ae115bc7Smrj 			lp->arg = s_strdup(module);
8424ae115bc7Smrj 			lp->line = s_strdup(linebuf);
8425ae115bc7Smrj 			lp = lp->next;
8426f64ca102SToomas Soome 			BAM_DPRINTF(("%s: adding new module$ line: %s\n",
8427f64ca102SToomas Soome 			    fcn, module));
8428ae115bc7Smrj 		}
8429ae115bc7Smrj 	}
8430bbcc54bdSEnrico Perla - Sun Microsystems 
8431bbcc54bdSEnrico Perla - Sun Microsystems 	/* module line */
8432bbcc54bdSEnrico Perla - Sun Microsystems 	lp = lp->next;
8433bbcc54bdSEnrico Perla - Sun Microsystems 
8434bbcc54bdSEnrico Perla - Sun Microsystems 	if (ent->flags & BAM_ENTRY_UPGFSMODULE) {
8435eac223ccSWilliam Kucharski 		if (lp->cmd != NULL &&
8436eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) {
8437bbcc54bdSEnrico Perla - Sun Microsystems 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
8438bbcc54bdSEnrico Perla - Sun Microsystems 			    menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD],
8439bbcc54bdSEnrico Perla - Sun Microsystems 			    module);
8440bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->cmd);
8441bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->arg);
8442bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->line);
8443bbcc54bdSEnrico Perla - Sun Microsystems 			lp->cmd = s_strdup(menu_cmds[MODULE_DOLLAR_CMD]);
8444bbcc54bdSEnrico Perla - Sun Microsystems 			lp->arg = s_strdup(module);
8445bbcc54bdSEnrico Perla - Sun Microsystems 			lp->line = s_strdup(linebuf);
8446bbcc54bdSEnrico Perla - Sun Microsystems 			lp = lp->next;
8447bbcc54bdSEnrico Perla - Sun Microsystems 			ent->flags &= ~BAM_ENTRY_UPGFSMODULE;
8448f64ca102SToomas Soome 			BAM_DPRINTF(("%s: adding new module$ line: %s\n",
8449f64ca102SToomas Soome 			    fcn, module));
8450bbcc54bdSEnrico Perla - Sun Microsystems 		}
8451bbcc54bdSEnrico Perla - Sun Microsystems 	}
8452bbcc54bdSEnrico Perla - Sun Microsystems 
8453f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning ret = %d\n", fcn, i));
84548c1b6884Sszhou 	return (i);
84558c1b6884Sszhou }
84568c1b6884Sszhou 
8457eb2bd662Svikram int
8458eb2bd662Svikram root_optional(char *osroot, char *menu_root)
8459eb2bd662Svikram {
8460eb2bd662Svikram 	char			*ospecial;
8461eb2bd662Svikram 	char			*mspecial;
8462eb2bd662Svikram 	char			*slash;
8463eb2bd662Svikram 	int			root_opt;
8464eb2bd662Svikram 	int			ret1;
8465eb2bd662Svikram 	int			ret2;
8466eb2bd662Svikram 	const char		*fcn = "root_optional()";
8467eb2bd662Svikram 
8468f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn, osroot, menu_root));
8469eb2bd662Svikram 
8470eb2bd662Svikram 	/*
8471eb2bd662Svikram 	 * For all filesystems except ZFS, a straight compare of osroot
8472eb2bd662Svikram 	 * and menu_root will tell us if root is optional.
8473eb2bd662Svikram 	 * For ZFS, the situation is complicated by the fact that
8474eb2bd662Svikram 	 * menu_root and osroot are always different
8475eb2bd662Svikram 	 */
8476eb2bd662Svikram 	ret1 = is_zfs(osroot);
8477eb2bd662Svikram 	ret2 = is_zfs(menu_root);
8478eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPT_NOT_ZFS", ret1 = 0);
8479eb2bd662Svikram 	if (!ret1 || !ret2) {
8480f64ca102SToomas Soome 		BAM_DPRINTF(("%s: one or more non-ZFS filesystems (%s, %s)\n",
8481f64ca102SToomas Soome 		    fcn, osroot, menu_root));
8482eb2bd662Svikram 		root_opt = (strcmp(osroot, menu_root) == 0);
8483eb2bd662Svikram 		goto out;
8484eb2bd662Svikram 	}
8485eb2bd662Svikram 
8486eb2bd662Svikram 	ospecial = get_special(osroot);
8487eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_OSPECIAL", ospecial = NULL);
8488eb2bd662Svikram 	if (ospecial == NULL) {
8489f64ca102SToomas Soome 		bam_error(_("failed to get special file for osroot: %s\n"),
8490f64ca102SToomas Soome 		    osroot);
8491eb2bd662Svikram 		return (0);
8492eb2bd662Svikram 	}
8493f64ca102SToomas Soome 	BAM_DPRINTF(("%s: ospecial=%s for osroot=%s\n", fcn, ospecial, osroot));
8494eb2bd662Svikram 
8495eb2bd662Svikram 	mspecial = get_special(menu_root);
8496eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_MSPECIAL", mspecial = NULL);
8497eb2bd662Svikram 	if (mspecial == NULL) {
8498f64ca102SToomas Soome 		bam_error(_("failed to get special file for menu_root: %s\n"),
8499f64ca102SToomas Soome 		    menu_root);
8500eb2bd662Svikram 		free(ospecial);
8501eb2bd662Svikram 		return (0);
8502eb2bd662Svikram 	}
8503f64ca102SToomas Soome 	BAM_DPRINTF(("%s: mspecial=%s for menu_root=%s\n",
8504f64ca102SToomas Soome 	    fcn, mspecial, menu_root));
8505eb2bd662Svikram 
8506eb2bd662Svikram 	slash = strchr(ospecial, '/');
8507eb2bd662Svikram 	if (slash)
8508eb2bd662Svikram 		*slash = '\0';
8509f64ca102SToomas Soome 	BAM_DPRINTF(("%s: FIXED ospecial=%s for osroot=%s\n",
8510f64ca102SToomas Soome 	    fcn, ospecial, osroot));
8511eb2bd662Svikram 
8512eb2bd662Svikram 	root_opt = (strcmp(ospecial, mspecial) == 0);
8513eb2bd662Svikram 
8514eb2bd662Svikram 	free(ospecial);
8515eb2bd662Svikram 	free(mspecial);
8516eb2bd662Svikram 
8517eb2bd662Svikram out:
8518eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_NO", root_opt = 0);
8519eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_YES", root_opt = 1);
8520eb2bd662Svikram 	if (root_opt) {
8521f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
8522eb2bd662Svikram 	} else {
8523f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
8524eb2bd662Svikram 	}
8525eb2bd662Svikram 
8526eb2bd662Svikram 	return (root_opt);
8527eb2bd662Svikram }
8528eb2bd662Svikram 
85297c478bd9Sstevel@tonic-gate /*ARGSUSED*/
85307c478bd9Sstevel@tonic-gate static error_t
8531eb2bd662Svikram update_entry(menu_t *mp, char *menu_root, char *osdev)
85327c478bd9Sstevel@tonic-gate {
85337c478bd9Sstevel@tonic-gate 	int		entry;
8534eb2bd662Svikram 	char		*grubsign;
8535eb2bd662Svikram 	char		*grubroot;
8536eb2bd662Svikram 	char		*title;
8537eb2bd662Svikram 	char		osroot[PATH_MAX];
8538eb2bd662Svikram 	char		*failsafe_kernel = NULL;
85398c1b6884Sszhou 	struct stat	sbuf;
85408c1b6884Sszhou 	char		failsafe[256];
8541bbcc54bdSEnrico Perla - Sun Microsystems 	char		failsafe_64[256];
8542eb2bd662Svikram 	int		ret;
8543eb2bd662Svikram 	const char	*fcn = "update_entry()";
85447c478bd9Sstevel@tonic-gate 
85457c478bd9Sstevel@tonic-gate 	assert(mp);
8546eb2bd662Svikram 	assert(menu_root);
8547eb2bd662Svikram 	assert(osdev);
8548eb2bd662Svikram 	assert(bam_root);
85497c478bd9Sstevel@tonic-gate 
8550f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s %s\n", fcn, menu_root, osdev,
8551f64ca102SToomas Soome 	    bam_root));
8552eb2bd662Svikram 
8553eb2bd662Svikram 	(void) strlcpy(osroot, bam_root, sizeof (osroot));
8554eb2bd662Svikram 
85557c478bd9Sstevel@tonic-gate 	title = get_title(osroot);
8556eb2bd662Svikram 	assert(title);
85577c478bd9Sstevel@tonic-gate 
8558eb2bd662Svikram 	grubsign = get_grubsign(osroot, osdev);
8559eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBSIGN_FAIL", grubsign = NULL);
8560eb2bd662Svikram 	if (grubsign == NULL) {
8561f64ca102SToomas Soome 		bam_error(_("failed to get grubsign for root: %s, device %s\n"),
8562f64ca102SToomas Soome 		    osroot, osdev);
85637c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
85647c478bd9Sstevel@tonic-gate 	}
8565eb2bd662Svikram 
8566eb2bd662Svikram 	/*
8567eb2bd662Svikram 	 * It is not a fatal error if get_grubroot() fails
8568eb2bd662Svikram 	 * We no longer rely on biosdev to populate the
8569eb2bd662Svikram 	 * menu
8570eb2bd662Svikram 	 */
8571eb2bd662Svikram 	grubroot = get_grubroot(osroot, osdev, menu_root);
8572eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBROOT_FAIL", grubroot = NULL);
8573eb2bd662Svikram 	if (grubroot) {
8574f64ca102SToomas Soome 		BAM_DPRINTF(("%s: get_grubroot success. osroot=%s, osdev=%s, "
8575f64ca102SToomas Soome 		    "menu_root=%s\n", fcn, osroot, osdev, menu_root));
8576eb2bd662Svikram 	} else {
8577f64ca102SToomas Soome 		BAM_DPRINTF(("%s: get_grubroot failed. osroot=%s, osdev=%s, "
8578f64ca102SToomas Soome 		    "menu_root=%s\n", fcn, osroot, osdev, menu_root));
85797c478bd9Sstevel@tonic-gate 	}
85807c478bd9Sstevel@tonic-gate 
85817c478bd9Sstevel@tonic-gate 	/* add the entry for normal Solaris */
8582eb2bd662Svikram 	INJECT_ERROR1("UPDATE_ENTRY_MULTIBOOT",
8583eb2bd662Svikram 	    bam_direct = BAM_DIRECT_MULTIBOOT);
8584ae115bc7Smrj 	if (bam_direct == BAM_DIRECT_DBOOT) {
8585eb2bd662Svikram 		entry = update_boot_entry(mp, title, grubsign, grubroot,
8586e7cbe64fSgw25295 		    (bam_zfs ? DIRECT_BOOT_KERNEL_ZFS : DIRECT_BOOT_KERNEL),
8587eb2bd662Svikram 		    NULL, DIRECT_BOOT_ARCHIVE,
8588eb2bd662Svikram 		    root_optional(osroot, menu_root));
8589f64ca102SToomas Soome 		BAM_DPRINTF(("%s: updated boot entry bam_zfs=%d, "
8590f64ca102SToomas Soome 		    "grubsign = %s\n", fcn, bam_zfs, grubsign));
8591843e1988Sjohnlev 		if ((entry != BAM_ERROR) && (bam_is_hv == BAM_HV_PRESENT)) {
8592eb2bd662Svikram 			(void) update_boot_entry(mp, NEW_HV_ENTRY, grubsign,
8593eb2bd662Svikram 			    grubroot, XEN_MENU, bam_zfs ?
8594eb2bd662Svikram 			    XEN_KERNEL_MODULE_LINE_ZFS : XEN_KERNEL_MODULE_LINE,
8595eb2bd662Svikram 			    DIRECT_BOOT_ARCHIVE,
8596eb2bd662Svikram 			    root_optional(osroot, menu_root));
8597f64ca102SToomas Soome 			BAM_DPRINTF(("%s: updated HV entry bam_zfs=%d, "
8598f64ca102SToomas Soome 			    "grubsign = %s\n", fcn, bam_zfs, grubsign));
8599ae115bc7Smrj 		}
8600843e1988Sjohnlev 	} else {
8601eb2bd662Svikram 		entry = update_boot_entry(mp, title, grubsign, grubroot,
8602eb2bd662Svikram 		    MULTI_BOOT, NULL, MULTIBOOT_ARCHIVE,
8603eb2bd662Svikram 		    root_optional(osroot, menu_root));
8604eb2bd662Svikram 
8605f64ca102SToomas Soome 		BAM_DPRINTF(("%s: updated MULTIBOOT entry grubsign = %s\n",
8606f64ca102SToomas Soome 		    fcn, grubsign));
8607843e1988Sjohnlev 	}
86087c478bd9Sstevel@tonic-gate 
8609843e1988Sjohnlev 	/*
8610843e1988Sjohnlev 	 * Add the entry for failsafe archive.  On a bfu'd system, the
8611843e1988Sjohnlev 	 * failsafe may be different than the installed kernel.
8612843e1988Sjohnlev 	 */
8613eb2bd662Svikram 	(void) snprintf(failsafe, sizeof (failsafe), "%s%s",
8614bbcc54bdSEnrico Perla - Sun Microsystems 	    osroot, FAILSAFE_ARCHIVE_32);
8615bbcc54bdSEnrico Perla - Sun Microsystems 	(void) snprintf(failsafe_64, sizeof (failsafe_64), "%s%s",
8616bbcc54bdSEnrico Perla - Sun Microsystems 	    osroot, FAILSAFE_ARCHIVE_64);
8617bbcc54bdSEnrico Perla - Sun Microsystems 
8618bbcc54bdSEnrico Perla - Sun Microsystems 	/*
8619bbcc54bdSEnrico Perla - Sun Microsystems 	 * Check if at least one of the two archives exists
8620bbcc54bdSEnrico Perla - Sun Microsystems 	 * Using $ISADIR as the default line, we have an entry which works
8621bbcc54bdSEnrico Perla - Sun Microsystems 	 * for both the cases.
8622bbcc54bdSEnrico Perla - Sun Microsystems 	 */
8623bbcc54bdSEnrico Perla - Sun Microsystems 
8624bbcc54bdSEnrico Perla - Sun Microsystems 	if (stat(failsafe, &sbuf) == 0 || stat(failsafe_64, &sbuf) == 0) {
862560d0a590Srscott 
862660d0a590Srscott 		/* Figure out where the kernel line should point */
862760d0a590Srscott 		(void) snprintf(failsafe, sizeof (failsafe), "%s%s", osroot,
8628bbcc54bdSEnrico Perla - Sun Microsystems 		    DIRECT_BOOT_FAILSAFE_32);
8629bbcc54bdSEnrico Perla - Sun Microsystems 		(void) snprintf(failsafe_64, sizeof (failsafe_64), "%s%s",
8630bbcc54bdSEnrico Perla - Sun Microsystems 		    osroot, DIRECT_BOOT_FAILSAFE_64);
8631bbcc54bdSEnrico Perla - Sun Microsystems 		if (stat(failsafe, &sbuf) == 0 ||
8632bbcc54bdSEnrico Perla - Sun Microsystems 		    stat(failsafe_64, &sbuf) == 0) {
8633963390b4Svikram 			failsafe_kernel = DIRECT_BOOT_FAILSAFE_LINE;
863460d0a590Srscott 		} else {
863560d0a590Srscott 			(void) snprintf(failsafe, sizeof (failsafe), "%s%s",
863660d0a590Srscott 			    osroot, MULTI_BOOT_FAILSAFE);
863760d0a590Srscott 			if (stat(failsafe, &sbuf) == 0) {
863860d0a590Srscott 				failsafe_kernel = MULTI_BOOT_FAILSAFE_LINE;
863960d0a590Srscott 			}
864060d0a590Srscott 		}
864160d0a590Srscott 		if (failsafe_kernel != NULL) {
8642eb2bd662Svikram 			(void) update_boot_entry(mp, FAILSAFE_TITLE, grubsign,
8643eb2bd662Svikram 			    grubroot, failsafe_kernel, NULL, FAILSAFE_ARCHIVE,
8644eb2bd662Svikram 			    root_optional(osroot, menu_root));
8645f64ca102SToomas Soome 			BAM_DPRINTF(("%s: updated FAILSAFE entry "
8646f64ca102SToomas Soome 			    "failsafe_kernel = %s\n", fcn, failsafe_kernel));
864760d0a590Srscott 		}
8648ae115bc7Smrj 	}
8649eb2bd662Svikram 	free(grubroot);
86507c478bd9Sstevel@tonic-gate 
8651eb2bd662Svikram 	INJECT_ERROR1("UPDATE_ENTRY_ERROR", entry = BAM_ERROR);
86527c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
8653f64ca102SToomas Soome 		bam_error(_("failed to add boot entry with title=%s, grub "
8654f64ca102SToomas Soome 		    "signature=%s\n"), title, grubsign);
8655eb2bd662Svikram 		free(grubsign);
86567c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
86577c478bd9Sstevel@tonic-gate 	}
8658eb2bd662Svikram 	free(grubsign);
8659eb2bd662Svikram 
8660eb2bd662Svikram 	update_numbering(mp);
8661eb2bd662Svikram 	ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
8662eb2bd662Svikram 	INJECT_ERROR1("SET_DEFAULT_ERROR", ret = BAM_ERROR);
8663eb2bd662Svikram 	if (ret == BAM_ERROR) {
8664f64ca102SToomas Soome 		bam_error(_("failed to set GRUB menu default to %d\n"), entry);
8665eb2bd662Svikram 	}
8666f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
86677c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
86687c478bd9Sstevel@tonic-gate }
86697c478bd9Sstevel@tonic-gate 
86708c1b6884Sszhou static void
8671ae115bc7Smrj save_default_entry(menu_t *mp, const char *which)
86728c1b6884Sszhou {
8673eb2bd662Svikram 	int		lineNum;
8674eb2bd662Svikram 	int		entryNum;
86758c1b6884Sszhou 	int		entry = 0;	/* default is 0 */
86768c1b6884Sszhou 	char		linebuf[BAM_MAXLINE];
86778c1b6884Sszhou 	line_t		*lp = mp->curdefault;
8678eb2bd662Svikram 	const char	*fcn = "save_default_entry()";
86798c1b6884Sszhou 
8680bff269d0Svikram 	if (mp->start) {
8681bff269d0Svikram 		lineNum = mp->end->lineNum;
8682bff269d0Svikram 		entryNum = mp->end->entryNum;
8683bff269d0Svikram 	} else {
8684bff269d0Svikram 		lineNum = LINE_INIT;
8685bff269d0Svikram 		entryNum = ENTRY_INIT;
8686bff269d0Svikram 	}
8687bff269d0Svikram 
86888c1b6884Sszhou 	if (lp)
86898c1b6884Sszhou 		entry = s_strtol(lp->arg);
86908c1b6884Sszhou 
8691ae115bc7Smrj 	(void) snprintf(linebuf, sizeof (linebuf), "#%s%d", which, entry);
8692f64ca102SToomas Soome 	BAM_DPRINTF(("%s: saving default to: %s\n", fcn, linebuf));
86938c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
8694f64ca102SToomas Soome 	BAM_DPRINTF(("%s: saved default to lineNum=%d, entryNum=%d\n", fcn,
8695f64ca102SToomas Soome 	    lineNum, entryNum));
86968c1b6884Sszhou }
86978c1b6884Sszhou 
86988c1b6884Sszhou static void
8699ae115bc7Smrj restore_default_entry(menu_t *mp, const char *which, line_t *lp)
87008c1b6884Sszhou {
87018c1b6884Sszhou 	int		entry;
87028c1b6884Sszhou 	char		*str;
8703eb2bd662Svikram 	const char	*fcn = "restore_default_entry()";
87048c1b6884Sszhou 
8705eb2bd662Svikram 	if (lp == NULL) {
8706f64ca102SToomas Soome 		BAM_DPRINTF(("%s: NULL saved default\n", fcn));
87078c1b6884Sszhou 		return;		/* nothing to restore */
8708eb2bd662Svikram 	}
8709eb2bd662Svikram 
8710f64ca102SToomas Soome 	BAM_DPRINTF(("%s: saved default string: %s\n", fcn, which));
87118c1b6884Sszhou 
8712ae115bc7Smrj 	str = lp->arg + strlen(which);
87138c1b6884Sszhou 	entry = s_strtol(str);
87148c1b6884Sszhou 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
87158c1b6884Sszhou 
8716f64ca102SToomas Soome 	BAM_DPRINTF(("%s: restored default to entryNum: %d\n", fcn, entry));
8717eb2bd662Svikram 
87188c1b6884Sszhou 	/* delete saved old default line */
87198c1b6884Sszhou 	unlink_line(mp, lp);
87208c1b6884Sszhou 	line_free(lp);
87218c1b6884Sszhou }
87228c1b6884Sszhou 
87237c478bd9Sstevel@tonic-gate /*
87247c478bd9Sstevel@tonic-gate  * This function is for supporting reboot with args.
87257c478bd9Sstevel@tonic-gate  * The opt value can be:
87267c478bd9Sstevel@tonic-gate  * NULL		delete temp entry, if present
8727eb2bd662Svikram  * entry=<n>	switches default entry to <n>
87287c478bd9Sstevel@tonic-gate  * else		treated as boot-args and setup a temperary menu entry
87297c478bd9Sstevel@tonic-gate  *		and make it the default
8730eb2bd662Svikram  * Note that we are always rebooting the current OS instance
8731eb2bd662Svikram  * so osroot == / always.
87327c478bd9Sstevel@tonic-gate  */
87337c478bd9Sstevel@tonic-gate #define	REBOOT_TITLE	"Solaris_reboot_transient"
87347c478bd9Sstevel@tonic-gate 
87358c1b6884Sszhou /*ARGSUSED*/
87367c478bd9Sstevel@tonic-gate static error_t
8737eb2bd662Svikram update_temp(menu_t *mp, char *dummy, char *opt)
87387c478bd9Sstevel@tonic-gate {
87397c478bd9Sstevel@tonic-gate 	int		entry;
8740eb2bd662Svikram 	char		*osdev;
8741eb2bd662Svikram 	char		*fstype;
8742eb2bd662Svikram 	char		*sign;
8743eb2bd662Svikram 	char		*opt_ptr;
8744eb2bd662Svikram 	char		*path;
8745ae115bc7Smrj 	char		kernbuf[BUFSIZ];
8746ae115bc7Smrj 	char		args_buf[BUFSIZ];
8747eb2bd662Svikram 	char		signbuf[PATH_MAX];
8748eb2bd662Svikram 	int		ret;
8749eb2bd662Svikram 	const char	*fcn = "update_temp()";
87507c478bd9Sstevel@tonic-gate 
87517c478bd9Sstevel@tonic-gate 	assert(mp);
8752eb2bd662Svikram 	assert(dummy == NULL);
8753eb2bd662Svikram 
8754eb2bd662Svikram 	/* opt can be NULL */
8755f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. arg: %s\n", fcn, opt ? opt : "<NULL>"));
8756f64ca102SToomas Soome 	BAM_DPRINTF(("%s: bam_alt_root: %d, bam_root: %s\n", fcn,
8757f64ca102SToomas Soome 	    bam_alt_root, bam_root));
8758eb2bd662Svikram 
8759eb2bd662Svikram 	if (bam_alt_root || bam_rootlen != 1 ||
8760eb2bd662Svikram 	    strcmp(bam_root, "/") != 0 ||
8761eb2bd662Svikram 	    strcmp(rootbuf, "/") != 0) {
8762f64ca102SToomas Soome 		bam_error(_("an alternate root (%s) cannot be used with this "
8763f64ca102SToomas Soome 		    "sub-command\n"), bam_root);
8764eb2bd662Svikram 		return (BAM_ERROR);
8765eb2bd662Svikram 	}
87667c478bd9Sstevel@tonic-gate 
87678c1b6884Sszhou 	/* If no option, delete exiting reboot menu entry */
87688c1b6884Sszhou 	if (opt == NULL) {
8769eb2bd662Svikram 		entry_t		*ent;
8770f64ca102SToomas Soome 		BAM_DPRINTF(("%s: opt is NULL\n", fcn));
8771eb2bd662Svikram 		ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL,
8772eb2bd662Svikram 		    NULL, NULL, 0, &entry);
8773eb2bd662Svikram 		if (ent == NULL) {	/* not found is ok */
8774f64ca102SToomas Soome 			BAM_DPRINTF(("%s: transient entry not found\n", fcn));
87758c1b6884Sszhou 			return (BAM_SUCCESS);
8776eb2bd662Svikram 		}
877744da779fSWilliam Kucharski 		(void) delete_boot_entry(mp, entry, DBE_PRINTERR);
8778ae115bc7Smrj 		restore_default_entry(mp, BAM_OLDDEF, mp->olddefault);
8779ae115bc7Smrj 		mp->olddefault = NULL;
8780f64ca102SToomas Soome 		BAM_DPRINTF(("%s: restored old default\n", fcn));
8781f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
87828c1b6884Sszhou 		return (BAM_WRITE);
87838c1b6884Sszhou 	}
87848c1b6884Sszhou 
87858c1b6884Sszhou 	/* if entry= is specified, set the default entry */
8786eb2bd662Svikram 	if (strncmp(opt, "entry=", strlen("entry=")) == 0) {
8787eb2bd662Svikram 		int entryNum = s_strtol(opt + strlen("entry="));
8788f64ca102SToomas Soome 		BAM_DPRINTF(("%s: opt has entry=: %s\n", fcn, opt));
8789eb2bd662Svikram 		if (selector(mp, opt, &entry, NULL) == BAM_SUCCESS) {
87907c478bd9Sstevel@tonic-gate 			/* this is entry=# option */
8791eb2bd662Svikram 			ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
8792f64ca102SToomas Soome 			BAM_DPRINTF(("%s: default set to %d, "
8793f64ca102SToomas Soome 			    "set_default ret=%d\n", fcn, entry, ret));
8794eb2bd662Svikram 			return (ret);
8795eb2bd662Svikram 		} else {
8796f64ca102SToomas Soome 			bam_error(_("failed to set GRUB menu default to %d\n"),
8797f64ca102SToomas Soome 			    entryNum);
8798eb2bd662Svikram 			return (BAM_ERROR);
8799eb2bd662Svikram 		}
88007c478bd9Sstevel@tonic-gate 	}
88017c478bd9Sstevel@tonic-gate 
88027c478bd9Sstevel@tonic-gate 	/*
8803eb2bd662Svikram 	 * add a new menu entry based on opt and make it the default
8804b610f78eSvikram 	 */
8805eb2bd662Svikram 
8806eb2bd662Svikram 	fstype = get_fstype("/");
8807eb2bd662Svikram 	INJECT_ERROR1("REBOOT_FSTYPE_NULL", fstype = NULL);
8808eb2bd662Svikram 	if (fstype == NULL) {
8809f64ca102SToomas Soome 		bam_error(_("failed to determine filesystem type for \"/\". "
8810f64ca102SToomas Soome 		    "Reboot with \narguments failed.\n"));
8811eb2bd662Svikram 		return (BAM_ERROR);
88127c478bd9Sstevel@tonic-gate 	}
8813eb2bd662Svikram 
8814eb2bd662Svikram 	osdev = get_special("/");
8815eb2bd662Svikram 	INJECT_ERROR1("REBOOT_SPECIAL_NULL", osdev = NULL);
8816eb2bd662Svikram 	if (osdev == NULL) {
8817eb2bd662Svikram 		free(fstype);
8818f64ca102SToomas Soome 		bam_error(_("failed to find device special file for \"/\". "
8819f64ca102SToomas Soome 		    "Reboot with \narguments failed.\n"));
8820eb2bd662Svikram 		return (BAM_ERROR);
8821b610f78eSvikram 	}
8822eb2bd662Svikram 
8823eb2bd662Svikram 	sign = find_existing_sign("/", osdev, fstype);
8824eb2bd662Svikram 	INJECT_ERROR1("REBOOT_SIGN_NULL", sign = NULL);
8825eb2bd662Svikram 	if (sign == NULL) {
8826eb2bd662Svikram 		free(fstype);
8827eb2bd662Svikram 		free(osdev);
8828f64ca102SToomas Soome 		bam_error(_("failed to find boot signature. Reboot with "
8829f64ca102SToomas Soome 		    "arguments failed.\n"));
8830eb2bd662Svikram 		return (BAM_ERROR);
8831eb2bd662Svikram 	}
8832eb2bd662Svikram 
8833eb2bd662Svikram 	free(osdev);
8834eb2bd662Svikram 	(void) strlcpy(signbuf, sign, sizeof (signbuf));
8835eb2bd662Svikram 	free(sign);
8836eb2bd662Svikram 
8837eb2bd662Svikram 	assert(strchr(signbuf, '(') == NULL && strchr(signbuf, ',') == NULL &&
8838eb2bd662Svikram 	    strchr(signbuf, ')') == NULL);
8839eb2bd662Svikram 
8840eb2bd662Svikram 	/*
8841eb2bd662Svikram 	 * There is no alternate root while doing reboot with args
8842eb2bd662Svikram 	 * This version of bootadm is only delivered with a DBOOT
8843eb2bd662Svikram 	 * version of Solaris.
8844eb2bd662Svikram 	 */
8845eb2bd662Svikram 	INJECT_ERROR1("REBOOT_NOT_DBOOT", bam_direct = BAM_DIRECT_MULTIBOOT);
8846eb2bd662Svikram 	if (bam_direct != BAM_DIRECT_DBOOT) {
88479bd6d269SLin Ling 		free(fstype);
8848f64ca102SToomas Soome 		bam_error(_("the root filesystem is not a dboot Solaris "
8849f64ca102SToomas Soome 		    "instance. \nThis version of bootadm is not supported "
8850f64ca102SToomas Soome 		    "on this version of Solaris.\n"));
88517c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
88527c478bd9Sstevel@tonic-gate 	}
88537c478bd9Sstevel@tonic-gate 
88547c478bd9Sstevel@tonic-gate 	/* add an entry for Solaris reboot */
8855ae115bc7Smrj 	if (opt[0] == '-') {
8856ae115bc7Smrj 		/* It's an option - first see if boot-file is set */
8857eb2bd662Svikram 		ret = get_kernel(mp, KERNEL_CMD, kernbuf, sizeof (kernbuf));
8858eb2bd662Svikram 		INJECT_ERROR1("REBOOT_GET_KERNEL", ret = BAM_ERROR);
8859eb2bd662Svikram 		if (ret != BAM_SUCCESS) {
88609bd6d269SLin Ling 			free(fstype);
8861f64ca102SToomas Soome 			bam_error(_("reboot with arguments: error querying "
8862f64ca102SToomas Soome 			    "current boot-file settings\n"));
8863ae115bc7Smrj 			return (BAM_ERROR);
8864eb2bd662Svikram 		}
8865ae115bc7Smrj 		if (kernbuf[0] == '\0')
8866eb2bd662Svikram 			(void) strlcpy(kernbuf, DIRECT_BOOT_KERNEL,
8867eb2bd662Svikram 			    sizeof (kernbuf));
88689bd6d269SLin Ling 		/*
88699bd6d269SLin Ling 		 * If this is a zfs file system and kernbuf does not
88709bd6d269SLin Ling 		 * have "-B $ZFS-BOOTFS" string yet, add it.
88719bd6d269SLin Ling 		 */
88729bd6d269SLin Ling 		if (strcmp(fstype, "zfs") == 0 && !strstr(kernbuf, ZFS_BOOT)) {
88739bd6d269SLin Ling 			(void) strlcat(kernbuf, " ", sizeof (kernbuf));
88749bd6d269SLin Ling 			(void) strlcat(kernbuf, ZFS_BOOT, sizeof (kernbuf));
88759bd6d269SLin Ling 		}
8876eb2bd662Svikram 		(void) strlcat(kernbuf, " ", sizeof (kernbuf));
8877eb2bd662Svikram 		(void) strlcat(kernbuf, opt, sizeof (kernbuf));
8878f64ca102SToomas Soome 		BAM_DPRINTF(("%s: reboot with args, option specified: "
8879f64ca102SToomas Soome 		    "kern=%s\n", fcn, kernbuf));
8880ae115bc7Smrj 	} else if (opt[0] == '/') {
8881455710d3Srscott 		/* It's a full path, so write it out. */
8882eb2bd662Svikram 		(void) strlcpy(kernbuf, opt, sizeof (kernbuf));
8883455710d3Srscott 
8884455710d3Srscott 		/*
8885455710d3Srscott 		 * If someone runs:
8886455710d3Srscott 		 *
8887455710d3Srscott 		 *	# eeprom boot-args='-kd'
8888455710d3Srscott 		 *	# reboot /platform/i86pc/kernel/unix
8889455710d3Srscott 		 *
8890455710d3Srscott 		 * we want to use the boot-args as part of the boot
8891455710d3Srscott 		 * line.  On the other hand, if someone runs:
8892455710d3Srscott 		 *
8893455710d3Srscott 		 *	# reboot "/platform/i86pc/kernel/unix -kd"
8894455710d3Srscott 		 *
8895455710d3Srscott 		 * we don't need to mess with boot-args.  If there's
8896455710d3Srscott 		 * no space in the options string, assume we're in the
8897455710d3Srscott 		 * first case.
8898455710d3Srscott 		 */
8899455710d3Srscott 		if (strchr(opt, ' ') == NULL) {
8900eb2bd662Svikram 			ret = get_kernel(mp, ARGS_CMD, args_buf,
8901eb2bd662Svikram 			    sizeof (args_buf));
8902eb2bd662Svikram 			INJECT_ERROR1("REBOOT_GET_ARGS", ret = BAM_ERROR);
8903eb2bd662Svikram 			if (ret != BAM_SUCCESS) {
89049bd6d269SLin Ling 				free(fstype);
8905f64ca102SToomas Soome 				bam_error(_("reboot with arguments: error "
8906f64ca102SToomas Soome 				    "querying current boot-args settings\n"));
8907455710d3Srscott 				return (BAM_ERROR);
8908eb2bd662Svikram 			}
8909455710d3Srscott 
8910455710d3Srscott 			if (args_buf[0] != '\0') {
8911eb2bd662Svikram 				(void) strlcat(kernbuf, " ", sizeof (kernbuf));
8912455710d3Srscott 				(void) strlcat(kernbuf, args_buf,
8913eb2bd662Svikram 				    sizeof (kernbuf));
8914455710d3Srscott 			}
8915455710d3Srscott 		}
8916f64ca102SToomas Soome 		BAM_DPRINTF(("%s: reboot with args, abspath specified: "
8917f64ca102SToomas Soome 		    "kern=%s\n", fcn, kernbuf));
8918ae115bc7Smrj 	} else {
8919455710d3Srscott 		/*
8920455710d3Srscott 		 * It may be a partial path, or it may be a partial
8921455710d3Srscott 		 * path followed by options.  Assume that only options
8922455710d3Srscott 		 * follow a space.  If someone sends us a kernel path
8923455710d3Srscott 		 * that includes a space, they deserve to be broken.
8924455710d3Srscott 		 */
8925455710d3Srscott 		opt_ptr = strchr(opt, ' ');
8926455710d3Srscott 		if (opt_ptr != NULL) {
8927455710d3Srscott 			*opt_ptr = '\0';
8928455710d3Srscott 		}
8929455710d3Srscott 
8930ae115bc7Smrj 		path = expand_path(opt);
8931ae115bc7Smrj 		if (path != NULL) {
8932eb2bd662Svikram 			(void) strlcpy(kernbuf, path, sizeof (kernbuf));
8933ae115bc7Smrj 			free(path);
8934455710d3Srscott 
8935455710d3Srscott 			/*
8936455710d3Srscott 			 * If there were options given, use those.
8937455710d3Srscott 			 * Otherwise, copy over the default options.
8938455710d3Srscott 			 */
8939455710d3Srscott 			if (opt_ptr != NULL) {
8940455710d3Srscott 				/* Restore the space in opt string */
8941455710d3Srscott 				*opt_ptr = ' ';
8942455710d3Srscott 				(void) strlcat(kernbuf, opt_ptr,
8943eb2bd662Svikram 				    sizeof (kernbuf));
8944455710d3Srscott 			} else {
8945eb2bd662Svikram 				ret = get_kernel(mp, ARGS_CMD, args_buf,
8946eb2bd662Svikram 				    sizeof (args_buf));
8947eb2bd662Svikram 				INJECT_ERROR1("UPDATE_TEMP_PARTIAL_ARGS",
8948eb2bd662Svikram 				    ret = BAM_ERROR);
8949eb2bd662Svikram 				if (ret != BAM_SUCCESS) {
89509bd6d269SLin Ling 					free(fstype);
8951f64ca102SToomas Soome 					bam_error(_("reboot with arguments: "
8952f64ca102SToomas Soome 					    "error querying current boot-args "
8953f64ca102SToomas Soome 					    "settings\n"));
8954ae115bc7Smrj 					return (BAM_ERROR);
8955eb2bd662Svikram 				}
8956ae115bc7Smrj 
8957ae115bc7Smrj 				if (args_buf[0] != '\0') {
8958ae115bc7Smrj 					(void) strlcat(kernbuf, " ",
8959eb2bd662Svikram 					    sizeof (kernbuf));
8960ae115bc7Smrj 					(void) strlcat(kernbuf,
8961eb2bd662Svikram 					    args_buf, sizeof (kernbuf));
8962ae115bc7Smrj 				}
8963ae115bc7Smrj 			}
8964f64ca102SToomas Soome 			BAM_DPRINTF(("%s: resolved partial path: %s\n",
8965f64ca102SToomas Soome 			    fcn, kernbuf));
8966455710d3Srscott 		} else {
89679bd6d269SLin Ling 			free(fstype);
8968f64ca102SToomas Soome 			bam_error(_("unable to expand %s to a full file"
8969f64ca102SToomas Soome 			    " path.\n"), opt);
8970f64ca102SToomas Soome 			bam_print_stderr(_("Rebooting with default kernel "
8971f64ca102SToomas Soome 			    "and options.\n"));
8972455710d3Srscott 			return (BAM_ERROR);
8973ae115bc7Smrj 		}
8974ae115bc7Smrj 	}
89759bd6d269SLin Ling 	free(fstype);
8976eb2bd662Svikram 	entry = add_boot_entry(mp, REBOOT_TITLE, signbuf, kernbuf,
897744da779fSWilliam Kucharski 	    NULL, NULL, NULL);
8978eb2bd662Svikram 	INJECT_ERROR1("REBOOT_ADD_BOOT_ENTRY", entry = BAM_ERROR);
89797c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
8980f64ca102SToomas Soome 		bam_error(_("Cannot update menu. Cannot reboot with "
8981f64ca102SToomas Soome 		    "requested arguments\n"));
89827c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
89837c478bd9Sstevel@tonic-gate 	}
89848c1b6884Sszhou 
8985ae115bc7Smrj 	save_default_entry(mp, BAM_OLDDEF);
8986eb2bd662Svikram 	ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
8987eb2bd662Svikram 	INJECT_ERROR1("REBOOT_SET_GLOBAL", ret = BAM_ERROR);
8988eb2bd662Svikram 	if (ret == BAM_ERROR) {
8989f64ca102SToomas Soome 		bam_error(_("reboot with arguments: setting GRUB menu default "
8990f64ca102SToomas Soome 		    "to %d failed\n"), entry);
8991eb2bd662Svikram 	}
8992f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
89937c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
89947c478bd9Sstevel@tonic-gate }
89957c478bd9Sstevel@tonic-gate 
899644da779fSWilliam Kucharski error_t
89977c478bd9Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val)
89987c478bd9Sstevel@tonic-gate {
8999eb2bd662Svikram 	line_t		*lp;
9000eb2bd662Svikram 	line_t		*found;
9001eb2bd662Svikram 	line_t		*last;
9002eb2bd662Svikram 	char		*cp;
9003eb2bd662Svikram 	char		*str;
90047c478bd9Sstevel@tonic-gate 	char		prefix[BAM_MAXLINE];
90057c478bd9Sstevel@tonic-gate 	size_t		len;
9006eb2bd662Svikram 	const char	*fcn = "set_global()";
90077c478bd9Sstevel@tonic-gate 
90087c478bd9Sstevel@tonic-gate 	assert(mp);
90097c478bd9Sstevel@tonic-gate 	assert(globalcmd);
90107c478bd9Sstevel@tonic-gate 
9011b610f78eSvikram 	if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) {
9012eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_VAL_NEG", val = -1);
9013eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_MENU_EMPTY", mp->end = NULL);
9014eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_VAL_TOO_BIG", val = 100);
9015b610f78eSvikram 		if (val < 0 || mp->end == NULL || val > mp->end->entryNum) {
9016b610f78eSvikram 			(void) snprintf(prefix, sizeof (prefix), "%d", val);
9017f64ca102SToomas Soome 			bam_error(_("invalid boot entry number: %s\n"), prefix);
9018b610f78eSvikram 			return (BAM_ERROR);
9019b610f78eSvikram 		}
9020b610f78eSvikram 	}
9021b610f78eSvikram 
90227c478bd9Sstevel@tonic-gate 	found = last = NULL;
90237c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
90247c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
90257c478bd9Sstevel@tonic-gate 			continue;
90267c478bd9Sstevel@tonic-gate 
90277c478bd9Sstevel@tonic-gate 		last = lp; /* track the last global found */
90287c478bd9Sstevel@tonic-gate 
9029eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_NULL_CMD", lp->cmd = NULL);
90307c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
9031f64ca102SToomas Soome 			bam_error(_("no command at line %d\n"), lp->lineNum);
90327c478bd9Sstevel@tonic-gate 			continue;
90337c478bd9Sstevel@tonic-gate 		}
90347c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
90357c478bd9Sstevel@tonic-gate 			continue;
90367c478bd9Sstevel@tonic-gate 
9037f64ca102SToomas Soome 		BAM_DPRINTF(("%s: found matching global command: %s\n",
9038f64ca102SToomas Soome 		    fcn, globalcmd));
9039eb2bd662Svikram 
90407c478bd9Sstevel@tonic-gate 		if (found) {
9041f64ca102SToomas Soome 			bam_error(_("duplicate command %s at line %d of "
9042f64ca102SToomas Soome 			    "%sboot/grub/menu.lst\n"), globalcmd,
9043f64ca102SToomas Soome 			    lp->lineNum, bam_root);
90447c478bd9Sstevel@tonic-gate 		}
90457c478bd9Sstevel@tonic-gate 		found = lp;
90467c478bd9Sstevel@tonic-gate 	}
90477c478bd9Sstevel@tonic-gate 
90487c478bd9Sstevel@tonic-gate 	if (found == NULL) {
90497c478bd9Sstevel@tonic-gate 		lp = s_calloc(1, sizeof (line_t));
90507c478bd9Sstevel@tonic-gate 		if (last == NULL) {
90517c478bd9Sstevel@tonic-gate 			lp->next = mp->start;
90527c478bd9Sstevel@tonic-gate 			mp->start = lp;
90537c478bd9Sstevel@tonic-gate 			mp->end = (mp->end) ? mp->end : lp;
90547c478bd9Sstevel@tonic-gate 		} else {
90557c478bd9Sstevel@tonic-gate 			lp->next = last->next;
90567c478bd9Sstevel@tonic-gate 			last->next = lp;
90577c478bd9Sstevel@tonic-gate 			if (lp->next == NULL)
90587c478bd9Sstevel@tonic-gate 				mp->end = lp;
90597c478bd9Sstevel@tonic-gate 		}
90607c478bd9Sstevel@tonic-gate 		lp->flags = BAM_GLOBAL; /* other fields not needed for writes */
90617c478bd9Sstevel@tonic-gate 		len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]);
90627c478bd9Sstevel@tonic-gate 		len += 10;	/* val < 10 digits */
90637c478bd9Sstevel@tonic-gate 		lp->line = s_calloc(1, len);
90647c478bd9Sstevel@tonic-gate 		(void) snprintf(lp->line, len, "%s%s%d",
90657c478bd9Sstevel@tonic-gate 		    globalcmd, menu_cmds[SEP_CMD], val);
9066f64ca102SToomas Soome 		BAM_DPRINTF(("%s: wrote new global line: %s\n", fcn, lp->line));
9067f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
90687c478bd9Sstevel@tonic-gate 		return (BAM_WRITE);
90697c478bd9Sstevel@tonic-gate 	}
90707c478bd9Sstevel@tonic-gate 
90717c478bd9Sstevel@tonic-gate 	/*
90727c478bd9Sstevel@tonic-gate 	 * We are changing an existing entry. Retain any prefix whitespace,
90737c478bd9Sstevel@tonic-gate 	 * but overwrite everything else. This preserves tabs added for
90747c478bd9Sstevel@tonic-gate 	 * readability.
90757c478bd9Sstevel@tonic-gate 	 */
90767c478bd9Sstevel@tonic-gate 	str = found->line;
90777c478bd9Sstevel@tonic-gate 	cp = prefix;
90787c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
90797c478bd9Sstevel@tonic-gate 		*(cp++) = *(str++);
90807c478bd9Sstevel@tonic-gate 	*cp = '\0'; /* Terminate prefix */
90817c478bd9Sstevel@tonic-gate 	len = strlen(prefix) + strlen(globalcmd);
90827c478bd9Sstevel@tonic-gate 	len += strlen(menu_cmds[SEP_CMD]) + 10;
90837c478bd9Sstevel@tonic-gate 
90847c478bd9Sstevel@tonic-gate 	free(found->line);
90857c478bd9Sstevel@tonic-gate 	found->line = s_calloc(1, len);
90867c478bd9Sstevel@tonic-gate 	(void) snprintf(found->line, len,
90877c478bd9Sstevel@tonic-gate 	    "%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val);
90887c478bd9Sstevel@tonic-gate 
9089f64ca102SToomas Soome 	BAM_DPRINTF(("%s: replaced global line with: %s\n", fcn, found->line));
9090f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
90917c478bd9Sstevel@tonic-gate 	return (BAM_WRITE); /* need a write to menu */
90927c478bd9Sstevel@tonic-gate }
90937c478bd9Sstevel@tonic-gate 
9094ae115bc7Smrj /*
9095ae115bc7Smrj  * partial_path may be anything like "kernel/unix" or "kmdb".  Try to
9096455710d3Srscott  * expand it to a full unix path.  The calling function is expected to
9097455710d3Srscott  * output a message if an error occurs and NULL is returned.
9098ae115bc7Smrj  */
9099ae115bc7Smrj static char *
9100ae115bc7Smrj expand_path(const char *partial_path)
9101ae115bc7Smrj {
9102ae115bc7Smrj 	int		new_path_len;
9103eb2bd662Svikram 	char		*new_path;
9104eb2bd662Svikram 	char		new_path2[PATH_MAX];
9105ae115bc7Smrj 	struct stat	sb;
9106eb2bd662Svikram 	const char	*fcn = "expand_path()";
9107ae115bc7Smrj 
9108ae115bc7Smrj 	new_path_len = strlen(partial_path) + 64;
9109ae115bc7Smrj 	new_path = s_calloc(1, new_path_len);
9110ae115bc7Smrj 
9111ae115bc7Smrj 	/* First, try the simplest case - something like "kernel/unix" */
9112ae115bc7Smrj 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s",
9113ae115bc7Smrj 	    partial_path);
9114ae115bc7Smrj 	if (stat(new_path, &sb) == 0) {
9115f64ca102SToomas Soome 		BAM_DPRINTF(("%s: expanded path: %s\n", fcn, new_path));
9116ae115bc7Smrj 		return (new_path);
9117ae115bc7Smrj 	}
9118ae115bc7Smrj 
9119ae115bc7Smrj 	if (strcmp(partial_path, "kmdb") == 0) {
9120ae115bc7Smrj 		(void) snprintf(new_path, new_path_len, "%s -k",
9121ae115bc7Smrj 		    DIRECT_BOOT_KERNEL);
9122f64ca102SToomas Soome 		BAM_DPRINTF(("%s: expanded path: %s\n", fcn, new_path));
9123ae115bc7Smrj 		return (new_path);
9124ae115bc7Smrj 	}
9125ae115bc7Smrj 
9126ae115bc7Smrj 	/*
9127ae115bc7Smrj 	 * We've quickly reached unsupported usage.  Try once more to
9128ae115bc7Smrj 	 * see if we were just given a glom name.
9129ae115bc7Smrj 	 */
9130ae115bc7Smrj 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s/unix",
9131ae115bc7Smrj 	    partial_path);
9132ae115bc7Smrj 	(void) snprintf(new_path2, PATH_MAX, "/platform/i86pc/%s/amd64/unix",
9133ae115bc7Smrj 	    partial_path);
9134ae115bc7Smrj 	if (stat(new_path, &sb) == 0) {
9135ae115bc7Smrj 		if (stat(new_path2, &sb) == 0) {
9136ae115bc7Smrj 			/*
9137ae115bc7Smrj 			 * We matched both, so we actually
9138ae115bc7Smrj 			 * want to write the $ISADIR version.
9139ae115bc7Smrj 			 */
9140ae115bc7Smrj 			(void) snprintf(new_path, new_path_len,
9141ae115bc7Smrj 			    "/platform/i86pc/kernel/%s/$ISADIR/unix",
9142ae115bc7Smrj 			    partial_path);
9143ae115bc7Smrj 		}
9144f64ca102SToomas Soome 		BAM_DPRINTF(("%s: expanded path: %s\n", fcn, new_path));
9145ae115bc7Smrj 		return (new_path);
9146ae115bc7Smrj 	}
9147ae115bc7Smrj 
9148ae115bc7Smrj 	free(new_path);
9149f64ca102SToomas Soome 	BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
9150ae115bc7Smrj 	return (NULL);
9151ae115bc7Smrj }
9152ae115bc7Smrj 
9153ae115bc7Smrj /*
9154ae115bc7Smrj  * The kernel cmd and arg have been changed, so
9155ae115bc7Smrj  * check whether the archive line needs to change.
9156ae115bc7Smrj  */
9157ae115bc7Smrj static void
9158ae115bc7Smrj set_archive_line(entry_t *entryp, line_t *kernelp)
9159ae115bc7Smrj {
9160ae115bc7Smrj 	line_t		*lp = entryp->start;
9161ae115bc7Smrj 	char		*new_archive;
9162ae115bc7Smrj 	menu_cmd_t	m_cmd;
9163eb2bd662Svikram 	const char	*fcn = "set_archive_line()";
9164ae115bc7Smrj 
9165ae115bc7Smrj 	for (; lp != NULL; lp = lp->next) {
9166eac223ccSWilliam Kucharski 		if (lp->cmd != NULL && strncmp(lp->cmd, menu_cmds[MODULE_CMD],
9167ae115bc7Smrj 		    sizeof (menu_cmds[MODULE_CMD]) - 1) == 0) {
9168ae115bc7Smrj 			break;
9169ae115bc7Smrj 		}
9170eb2bd662Svikram 
9171eb2bd662Svikram 		INJECT_ERROR1("SET_ARCHIVE_LINE_END_ENTRY", lp = entryp->end);
9172eb2bd662Svikram 		if (lp == entryp->end) {
9173f64ca102SToomas Soome 			BAM_DPRINTF(("%s: no module/archive line for entry: "
9174f64ca102SToomas Soome 			    "%d\n", fcn, entryp->entryNum));
9175ae115bc7Smrj 			return;
9176ae115bc7Smrj 		}
9177eb2bd662Svikram 	}
9178eb2bd662Svikram 	INJECT_ERROR1("SET_ARCHIVE_LINE_END_MENU", lp = NULL);
9179eb2bd662Svikram 	if (lp == NULL) {
9180f64ca102SToomas Soome 		BAM_DPRINTF(("%s: no module/archive line for entry: %d\n",
9181f64ca102SToomas Soome 		    fcn, entryp->entryNum));
9182ae115bc7Smrj 		return;
9183eb2bd662Svikram 	}
9184ae115bc7Smrj 
9185ae115bc7Smrj 	if (strstr(kernelp->arg, "$ISADIR") != NULL) {
9186ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE;
9187ae115bc7Smrj 		m_cmd = MODULE_DOLLAR_CMD;
9188ae115bc7Smrj 	} else if (strstr(kernelp->arg, "amd64") != NULL) {
9189ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE_64;
9190ae115bc7Smrj 		m_cmd = MODULE_CMD;
9191ae115bc7Smrj 	} else {
9192ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE_32;
9193ae115bc7Smrj 		m_cmd = MODULE_CMD;
9194ae115bc7Smrj 	}
9195ae115bc7Smrj 
9196eb2bd662Svikram 	if (strcmp(lp->arg, new_archive) == 0) {
9197f64ca102SToomas Soome 		BAM_DPRINTF(("%s: no change for line: %s\n", fcn, lp->arg));
9198ae115bc7Smrj 		return;
9199eb2bd662Svikram 	}
9200ae115bc7Smrj 
9201eac223ccSWilliam Kucharski 	if (lp->cmd != NULL && strcmp(lp->cmd, menu_cmds[m_cmd]) != 0) {
9202ae115bc7Smrj 		free(lp->cmd);
9203ae115bc7Smrj 		lp->cmd = s_strdup(menu_cmds[m_cmd]);
9204ae115bc7Smrj 	}
9205ae115bc7Smrj 
9206ae115bc7Smrj 	free(lp->arg);
9207ae115bc7Smrj 	lp->arg = s_strdup(new_archive);
9208ae115bc7Smrj 	update_line(lp);
9209f64ca102SToomas Soome 	BAM_DPRINTF(("%s: replaced for line: %s\n", fcn, lp->line));
9210ae115bc7Smrj }
9211ae115bc7Smrj 
9212ae115bc7Smrj /*
9213ae115bc7Smrj  * Title for an entry to set properties that once went in bootenv.rc.
9214ae115bc7Smrj  */
9215ae115bc7Smrj #define	BOOTENV_RC_TITLE	"Solaris bootenv rc"
9216ae115bc7Smrj 
9217ae115bc7Smrj /*
9218ae115bc7Smrj  * If path is NULL, return the kernel (optnum == KERNEL_CMD) or arguments
9219ae115bc7Smrj  * (optnum == ARGS_CMD) in the argument buf.  If path is a zero-length
9220ae115bc7Smrj  * string, reset the value to the default.  If path is a non-zero-length
9221ae115bc7Smrj  * string, set the kernel or arguments.
9222ae115bc7Smrj  */
9223ae115bc7Smrj static error_t
9224eb2bd662Svikram get_set_kernel(
9225eb2bd662Svikram 	menu_t *mp,
9226eb2bd662Svikram 	menu_cmd_t optnum,
9227eb2bd662Svikram 	char *path,
9228eb2bd662Svikram 	char *buf,
9229eb2bd662Svikram 	size_t bufsize)
9230ae115bc7Smrj {
9231eb2bd662Svikram 	int		entryNum;
9232eb2bd662Svikram 	int		rv = BAM_SUCCESS;
9233eb2bd662Svikram 	int		free_new_path = 0;
9234ae115bc7Smrj 	entry_t		*entryp;
9235eb2bd662Svikram 	line_t		*ptr;
9236eb2bd662Svikram 	line_t		*kernelp;
9237eb2bd662Svikram 	char		*new_arg;
9238eb2bd662Svikram 	char		*old_args;
9239eb2bd662Svikram 	char		*space;
9240eb2bd662Svikram 	char		*new_path;
9241ae115bc7Smrj 	char		old_space;
9242110570ceSToomas Soome 	size_t		old_kernel_len = 0;
9243eb2bd662Svikram 	size_t		new_str_len;
9244eb2bd662Svikram 	char		*fstype;
9245eb2bd662Svikram 	char		*osdev;
9246eb2bd662Svikram 	char		*sign;
9247eb2bd662Svikram 	char		signbuf[PATH_MAX];
9248eb2bd662Svikram 	int		ret;
9249eb2bd662Svikram 	const char	*fcn = "get_set_kernel()";
9250ae115bc7Smrj 
9251ae115bc7Smrj 	assert(bufsize > 0);
9252ae115bc7Smrj 
9253ae115bc7Smrj 	ptr = kernelp = NULL;
9254ae115bc7Smrj 	new_arg = old_args = space = NULL;
9255eb2bd662Svikram 	new_path = NULL;
9256ae115bc7Smrj 	buf[0] = '\0';
9257ae115bc7Smrj 
9258eb2bd662Svikram 	INJECT_ERROR1("GET_SET_KERNEL_NOT_DBOOT",
9259eb2bd662Svikram 	    bam_direct = BAM_DIRECT_MULTIBOOT);
9260ae115bc7Smrj 	if (bam_direct != BAM_DIRECT_DBOOT) {
9261f64ca102SToomas Soome 		bam_error(_("bootadm set-menu %s may only be run on "
9262f64ca102SToomas Soome 		    "directboot kernels.\n"),
9263f64ca102SToomas Soome 		    optnum == KERNEL_CMD ? "kernel" : "args");
9264ae115bc7Smrj 		return (BAM_ERROR);
9265ae115bc7Smrj 	}
9266ae115bc7Smrj 
9267ae115bc7Smrj 	/*
9268ae115bc7Smrj 	 * If a user changed the default entry to a non-bootadm controlled
9269ae115bc7Smrj 	 * one, we don't want to mess with it.  Just print an error and
9270ae115bc7Smrj 	 * return.
9271ae115bc7Smrj 	 */
9272ae115bc7Smrj 	if (mp->curdefault) {
9273ae115bc7Smrj 		entryNum = s_strtol(mp->curdefault->arg);
9274ae115bc7Smrj 		for (entryp = mp->entries; entryp; entryp = entryp->next) {
9275ae115bc7Smrj 			if (entryp->entryNum == entryNum)
9276ae115bc7Smrj 				break;
9277ae115bc7Smrj 		}
9278ae115bc7Smrj 		if ((entryp != NULL) &&
9279ae115bc7Smrj 		    ((entryp->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU)) == 0)) {
9280f64ca102SToomas Soome 			bam_error(_("Default /boot/grub/menu.lst entry is not "
9281f64ca102SToomas Soome 			    "controlled by bootadm.  Exiting\n"));
9282ae115bc7Smrj 			return (BAM_ERROR);
9283ae115bc7Smrj 		}
9284ae115bc7Smrj 	}
9285ae115bc7Smrj 
9286eb2bd662Svikram 	entryp = find_boot_entry(mp, BOOTENV_RC_TITLE, NULL, NULL, NULL, NULL,
9287eb2bd662Svikram 	    0, &entryNum);
9288ae115bc7Smrj 
9289ae115bc7Smrj 	if (entryp != NULL) {
9290ae115bc7Smrj 		for (ptr = entryp->start; ptr && ptr != entryp->end;
9291ae115bc7Smrj 		    ptr = ptr->next) {
9292ae115bc7Smrj 			if (strncmp(ptr->cmd, menu_cmds[KERNEL_CMD],
9293ae115bc7Smrj 			    sizeof (menu_cmds[KERNEL_CMD]) - 1) == 0) {
9294ae115bc7Smrj 				kernelp = ptr;
9295ae115bc7Smrj 				break;
9296ae115bc7Smrj 			}
9297ae115bc7Smrj 		}
9298ae115bc7Smrj 		if (kernelp == NULL) {
9299f64ca102SToomas Soome 			bam_error(_("no kernel line found in entry %d\n"),
9300f64ca102SToomas Soome 			    entryNum);
9301ae115bc7Smrj 			return (BAM_ERROR);
9302ae115bc7Smrj 		}
9303ae115bc7Smrj 
9304ae115bc7Smrj 		old_kernel_len = strcspn(kernelp->arg, " \t");
9305ae115bc7Smrj 		space = old_args = kernelp->arg + old_kernel_len;
9306ae115bc7Smrj 		while ((*old_args == ' ') || (*old_args == '\t'))
9307ae115bc7Smrj 			old_args++;
9308ae115bc7Smrj 	}
9309ae115bc7Smrj 
9310ae115bc7Smrj 	if (path == NULL) {
9311eb2bd662Svikram 		if (entryp == NULL) {
9312f64ca102SToomas Soome 			BAM_DPRINTF(("%s: no RC entry, nothing to report\n",
9313f64ca102SToomas Soome 			    fcn));
9314f64ca102SToomas Soome 			BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
9315ae115bc7Smrj 			return (BAM_SUCCESS);
9316eb2bd662Svikram 		}
9317eb2bd662Svikram 		assert(kernelp);
9318ae115bc7Smrj 		if (optnum == ARGS_CMD) {
9319eb2bd662Svikram 			if (old_args[0] != '\0') {
9320ae115bc7Smrj 				(void) strlcpy(buf, old_args, bufsize);
9321f64ca102SToomas Soome 				BAM_DPRINTF(("%s: read menu boot-args: %s\n",
9322f64ca102SToomas Soome 				    fcn, buf));
9323eb2bd662Svikram 			}
9324ae115bc7Smrj 		} else {
9325ae115bc7Smrj 			/*
9326ae115bc7Smrj 			 * We need to print the kernel, so we just turn the
9327ae115bc7Smrj 			 * first space into a '\0' and print the beginning.
9328ae115bc7Smrj 			 * We don't print anything if it's the default kernel.
9329ae115bc7Smrj 			 */
9330ae115bc7Smrj 			old_space = *space;
9331ae115bc7Smrj 			*space = '\0';
9332eb2bd662Svikram 			if (strcmp(kernelp->arg, DIRECT_BOOT_KERNEL) != 0) {
9333ae115bc7Smrj 				(void) strlcpy(buf, kernelp->arg, bufsize);
9334f64ca102SToomas Soome 				BAM_DPRINTF(("%s: read menu boot-file: %s\n",
9335f64ca102SToomas Soome 				    fcn, buf));
9336eb2bd662Svikram 			}
9337ae115bc7Smrj 			*space = old_space;
9338ae115bc7Smrj 		}
9339f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
9340ae115bc7Smrj 		return (BAM_SUCCESS);
9341ae115bc7Smrj 	}
9342ae115bc7Smrj 
9343ae115bc7Smrj 	/*
9344ae115bc7Smrj 	 * First, check if we're resetting an entry to the default.
9345ae115bc7Smrj 	 */
9346ae115bc7Smrj 	if ((path[0] == '\0') ||
9347ae115bc7Smrj 	    ((optnum == KERNEL_CMD) &&
9348ae115bc7Smrj 	    (strcmp(path, DIRECT_BOOT_KERNEL) == 0))) {
9349ae115bc7Smrj 		if ((entryp == NULL) || (kernelp == NULL)) {
9350ae115bc7Smrj 			/* No previous entry, it's already the default */
9351f64ca102SToomas Soome 			BAM_DPRINTF(("%s: no reset, already has default\n",
9352f64ca102SToomas Soome 			    fcn));
9353ae115bc7Smrj 			return (BAM_SUCCESS);
9354ae115bc7Smrj 		}
9355ae115bc7Smrj 
9356ae115bc7Smrj 		/*
9357ae115bc7Smrj 		 * Check if we can delete the entry.  If we're resetting the
9358ae115bc7Smrj 		 * kernel command, and the args is already empty, or if we're
9359ae115bc7Smrj 		 * resetting the args command, and the kernel is already the
9360ae115bc7Smrj 		 * default, we can restore the old default and delete the entry.
9361ae115bc7Smrj 		 */
9362ae115bc7Smrj 		if (((optnum == KERNEL_CMD) &&
9363ae115bc7Smrj 		    ((old_args == NULL) || (old_args[0] == '\0'))) ||
9364ae115bc7Smrj 		    ((optnum == ARGS_CMD) &&
9365ae115bc7Smrj 		    (strncmp(kernelp->arg, DIRECT_BOOT_KERNEL,
9366ae115bc7Smrj 		    sizeof (DIRECT_BOOT_KERNEL) - 1) == 0))) {
9367ae115bc7Smrj 			kernelp = NULL;
936844da779fSWilliam Kucharski 			(void) delete_boot_entry(mp, entryNum, DBE_PRINTERR);
9369ae115bc7Smrj 			restore_default_entry(mp, BAM_OLD_RC_DEF,
9370ae115bc7Smrj 			    mp->old_rc_default);
9371ae115bc7Smrj 			mp->old_rc_default = NULL;
9372ae115bc7Smrj 			rv = BAM_WRITE;
9373f64ca102SToomas Soome 			BAM_DPRINTF(("%s: resetting to default\n", fcn));
9374ae115bc7Smrj 			goto done;
9375ae115bc7Smrj 		}
9376ae115bc7Smrj 
9377ae115bc7Smrj 		if (optnum == KERNEL_CMD) {
9378ae115bc7Smrj 			/*
9379ae115bc7Smrj 			 * At this point, we've already checked that old_args
9380ae115bc7Smrj 			 * and entryp are valid pointers.  The "+ 2" is for
9381ae115bc7Smrj 			 * a space a the string termination character.
9382ae115bc7Smrj 			 */
9383ae115bc7Smrj 			new_str_len = (sizeof (DIRECT_BOOT_KERNEL) - 1) +
9384ae115bc7Smrj 			    strlen(old_args) + 2;
9385ae115bc7Smrj 			new_arg = s_calloc(1, new_str_len);
9386ae115bc7Smrj 			(void) snprintf(new_arg, new_str_len, "%s %s",
9387ae115bc7Smrj 			    DIRECT_BOOT_KERNEL, old_args);
9388ae115bc7Smrj 			free(kernelp->arg);
9389ae115bc7Smrj 			kernelp->arg = new_arg;
9390ae115bc7Smrj 
9391ae115bc7Smrj 			/*
9392ae115bc7Smrj 			 * We have changed the kernel line, so we may need
9393ae115bc7Smrj 			 * to update the archive line as well.
9394ae115bc7Smrj 			 */
9395ae115bc7Smrj 			set_archive_line(entryp, kernelp);
9396f64ca102SToomas Soome 			BAM_DPRINTF(("%s: reset kernel to default, but "
9397f64ca102SToomas Soome 			    "retained old args: %s\n", fcn, kernelp->arg));
9398ae115bc7Smrj 		} else {
9399ae115bc7Smrj 			/*
9400ae115bc7Smrj 			 * We're resetting the boot args to nothing, so
9401ae115bc7Smrj 			 * we only need to copy the kernel.  We've already
9402ae115bc7Smrj 			 * checked that the kernel is not the default.
9403ae115bc7Smrj 			 */
9404ae115bc7Smrj 			new_arg = s_calloc(1, old_kernel_len + 1);
9405ae115bc7Smrj 			(void) snprintf(new_arg, old_kernel_len + 1, "%s",
9406ae115bc7Smrj 			    kernelp->arg);
9407ae115bc7Smrj 			free(kernelp->arg);
9408ae115bc7Smrj 			kernelp->arg = new_arg;
9409f64ca102SToomas Soome 			BAM_DPRINTF(("%s: reset args to default, but retained "
9410f64ca102SToomas Soome 			    "old kernel: %s\n", fcn, kernelp->arg));
9411ae115bc7Smrj 		}
9412ae115bc7Smrj 		rv = BAM_WRITE;
9413ae115bc7Smrj 		goto done;
9414ae115bc7Smrj 	}
9415ae115bc7Smrj 
9416ae115bc7Smrj 	/*
9417ae115bc7Smrj 	 * Expand the kernel file to a full path, if necessary
9418ae115bc7Smrj 	 */
9419ae115bc7Smrj 	if ((optnum == KERNEL_CMD) && (path[0] != '/')) {
9420ae115bc7Smrj 		new_path = expand_path(path);
9421ae115bc7Smrj 		if (new_path == NULL) {
9422f64ca102SToomas Soome 			bam_error(_("unable to expand %s to a full file "
9423f64ca102SToomas Soome 			    "path.\n"), path);
9424f64ca102SToomas Soome 			BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
9425ae115bc7Smrj 			return (BAM_ERROR);
9426ae115bc7Smrj 		}
9427ae115bc7Smrj 		free_new_path = 1;
9428ae115bc7Smrj 	} else {
9429ae115bc7Smrj 		new_path = path;
9430ae115bc7Smrj 		free_new_path = 0;
9431ae115bc7Smrj 	}
9432ae115bc7Smrj 
9433ae115bc7Smrj 	/*
9434ae115bc7Smrj 	 * At this point, we know we're setting a new value.  First, take care
9435ae115bc7Smrj 	 * of the case where there was no previous entry.
9436ae115bc7Smrj 	 */
9437ae115bc7Smrj 	if (entryp == NULL) {
9438eb2bd662Svikram 
9439ae115bc7Smrj 		/* Similar to code in update_temp */
9440eb2bd662Svikram 		fstype = get_fstype("/");
9441eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_FSTYPE", fstype = NULL);
9442eb2bd662Svikram 		if (fstype == NULL) {
9443f64ca102SToomas Soome 			bam_error(_("cannot determine filesystem type for "
9444f64ca102SToomas Soome 			    "\"/\".\nCannot generate GRUB menu entry with "
9445f64ca102SToomas Soome 			    "EEPROM arguments.\n"));
9446ae115bc7Smrj 			rv = BAM_ERROR;
9447ae115bc7Smrj 			goto done;
9448ae115bc7Smrj 		}
9449eb2bd662Svikram 
9450eb2bd662Svikram 		osdev = get_special("/");
9451eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_SPECIAL", osdev = NULL);
9452eb2bd662Svikram 		if (osdev == NULL) {
9453eb2bd662Svikram 			free(fstype);
9454f64ca102SToomas Soome 			bam_error(_("cannot determine device special file for "
9455f64ca102SToomas Soome 			    "\"/\".\nCannot generate GRUB menu entry with "
9456f64ca102SToomas Soome 			    "EEPROM arguments.\n"));
9457eb2bd662Svikram 			rv = BAM_ERROR;
9458eb2bd662Svikram 			goto done;
9459eb2bd662Svikram 		}
9460eb2bd662Svikram 
9461eb2bd662Svikram 		sign = find_existing_sign("/", osdev, fstype);
9462eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_SIGN", sign = NULL);
9463eb2bd662Svikram 		if (sign == NULL) {
9464eb2bd662Svikram 			free(fstype);
9465eb2bd662Svikram 			free(osdev);
9466f64ca102SToomas Soome 			bam_error(_("cannot determine boot signature for "
9467f64ca102SToomas Soome 			    "\"/\".\nCannot generate GRUB menu entry with "
9468f64ca102SToomas Soome 			    "EEPROM arguments.\n"));
9469eb2bd662Svikram 			rv = BAM_ERROR;
9470eb2bd662Svikram 			goto done;
9471eb2bd662Svikram 		}
9472eb2bd662Svikram 
9473eb2bd662Svikram 		free(osdev);
9474eb2bd662Svikram 		(void) strlcpy(signbuf, sign, sizeof (signbuf));
9475eb2bd662Svikram 		free(sign);
9476eb2bd662Svikram 		assert(strchr(signbuf, '(') == NULL &&
9477eb2bd662Svikram 		    strchr(signbuf, ',') == NULL &&
9478eb2bd662Svikram 		    strchr(signbuf, ')') == NULL);
9479eb2bd662Svikram 
9480ae115bc7Smrj 		if (optnum == KERNEL_CMD) {
94814e483a70SGangadhar Mylapuram 			if (strcmp(fstype, "zfs") == 0) {
94824e483a70SGangadhar Mylapuram 				new_str_len = strlen(new_path) +
94834e483a70SGangadhar Mylapuram 				    strlen(ZFS_BOOT) + 8;
94844e483a70SGangadhar Mylapuram 				new_arg = s_calloc(1, new_str_len);
94854e483a70SGangadhar Mylapuram 				(void) snprintf(new_arg, new_str_len, "%s %s",
94864e483a70SGangadhar Mylapuram 				    new_path, ZFS_BOOT);
9487f64ca102SToomas Soome 				BAM_DPRINTF(("%s: new kernel=%s\n", fcn,
94884e483a70SGangadhar Mylapuram 				    new_arg));
94894e483a70SGangadhar Mylapuram 				entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
94904e483a70SGangadhar Mylapuram 				    signbuf, new_arg, NULL, NULL, NULL);
94914e483a70SGangadhar Mylapuram 				free(new_arg);
94924e483a70SGangadhar Mylapuram 			} else {
9493f64ca102SToomas Soome 				BAM_DPRINTF(("%s: new kernel=%s\n", fcn,
94944e483a70SGangadhar Mylapuram 				    new_path));
9495ae115bc7Smrj 				entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
949644da779fSWilliam Kucharski 				    signbuf, new_path, NULL, NULL, NULL);
94974e483a70SGangadhar Mylapuram 			}
9498ae115bc7Smrj 		} else {
94994487ce1dSGangadhar Mylapuram 			new_str_len = strlen(path) + 8;
95004487ce1dSGangadhar Mylapuram 			if (strcmp(fstype, "zfs") == 0) {
95014487ce1dSGangadhar Mylapuram 				new_str_len += strlen(DIRECT_BOOT_KERNEL_ZFS);
9502ae115bc7Smrj 				new_arg = s_calloc(1, new_str_len);
95034487ce1dSGangadhar Mylapuram 				(void) snprintf(new_arg, new_str_len, "%s %s",
95044487ce1dSGangadhar Mylapuram 				    DIRECT_BOOT_KERNEL_ZFS, path);
95054487ce1dSGangadhar Mylapuram 			} else {
95064487ce1dSGangadhar Mylapuram 				new_str_len += strlen(DIRECT_BOOT_KERNEL);
95074487ce1dSGangadhar Mylapuram 				new_arg = s_calloc(1, new_str_len);
9508ae115bc7Smrj 				(void) snprintf(new_arg, new_str_len, "%s %s",
9509ae115bc7Smrj 				    DIRECT_BOOT_KERNEL, path);
95104487ce1dSGangadhar Mylapuram 			}
95114487ce1dSGangadhar Mylapuram 
9512f64ca102SToomas Soome 			BAM_DPRINTF(("%s: new args=%s\n", fcn, new_arg));
9513ae115bc7Smrj 			entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
951444da779fSWilliam Kucharski 			    signbuf, new_arg, NULL, DIRECT_BOOT_ARCHIVE, NULL);
9515eb2bd662Svikram 			free(new_arg);
9516eb2bd662Svikram 		}
95174487ce1dSGangadhar Mylapuram 		free(fstype);
9518eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_ADD_BOOT_ENTRY",
9519eb2bd662Svikram 		    entryNum = BAM_ERROR);
9520eb2bd662Svikram 		if (entryNum == BAM_ERROR) {
9521f64ca102SToomas Soome 			bam_error(_("failed to add boot entry: %s\n"),
9522eb2bd662Svikram 			    BOOTENV_RC_TITLE);
9523eb2bd662Svikram 			rv = BAM_ERROR;
9524eb2bd662Svikram 			goto done;
9525ae115bc7Smrj 		}
9526ae115bc7Smrj 		save_default_entry(mp, BAM_OLD_RC_DEF);
9527eb2bd662Svikram 		ret = set_global(mp, menu_cmds[DEFAULT_CMD], entryNum);
9528eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_SET_GLOBAL", ret = BAM_ERROR);
9529eb2bd662Svikram 		if (ret == BAM_ERROR) {
9530f64ca102SToomas Soome 			bam_error(_("failed to set default to: %d\n"),
9531f64ca102SToomas Soome 			    entryNum);
9532eb2bd662Svikram 		}
9533ae115bc7Smrj 		rv = BAM_WRITE;
9534ae115bc7Smrj 		goto done;
9535ae115bc7Smrj 	}
9536ae115bc7Smrj 
9537ae115bc7Smrj 	/*
9538ae115bc7Smrj 	 * There was already an bootenv entry which we need to edit.
9539ae115bc7Smrj 	 */
9540ae115bc7Smrj 	if (optnum == KERNEL_CMD) {
9541ae115bc7Smrj 		new_str_len = strlen(new_path) + strlen(old_args) + 2;
9542ae115bc7Smrj 		new_arg = s_calloc(1, new_str_len);
9543ae115bc7Smrj 		(void) snprintf(new_arg, new_str_len, "%s %s", new_path,
9544ae115bc7Smrj 		    old_args);
9545ae115bc7Smrj 		free(kernelp->arg);
9546ae115bc7Smrj 		kernelp->arg = new_arg;
9547ae115bc7Smrj 
9548ae115bc7Smrj 		/*
9549ae115bc7Smrj 		 * If we have changed the kernel line, we may need to update
9550ae115bc7Smrj 		 * the archive line as well.
9551ae115bc7Smrj 		 */
9552ae115bc7Smrj 		set_archive_line(entryp, kernelp);
9553f64ca102SToomas Soome 		BAM_DPRINTF(("%s: rc line exists, replaced kernel, same "
9554f64ca102SToomas Soome 		    "args: %s\n", fcn, kernelp->arg));
9555ae115bc7Smrj 	} else {
9556ae115bc7Smrj 		new_str_len = old_kernel_len + strlen(path) + 8;
9557ae115bc7Smrj 		new_arg = s_calloc(1, new_str_len);
9558ae115bc7Smrj 		(void) strncpy(new_arg, kernelp->arg, old_kernel_len);
9559ae115bc7Smrj 		(void) strlcat(new_arg, " ", new_str_len);
9560ae115bc7Smrj 		(void) strlcat(new_arg, path, new_str_len);
9561ae115bc7Smrj 		free(kernelp->arg);
9562ae115bc7Smrj 		kernelp->arg = new_arg;
9563f64ca102SToomas Soome 		BAM_DPRINTF(("%s: rc line exists, same kernel, but new "
9564f64ca102SToomas Soome 		    "args: %s\n", fcn, kernelp->arg));
9565ae115bc7Smrj 	}
9566ae115bc7Smrj 	rv = BAM_WRITE;
9567ae115bc7Smrj 
9568ae115bc7Smrj done:
9569ae115bc7Smrj 	if ((rv == BAM_WRITE) && kernelp)
9570ae115bc7Smrj 		update_line(kernelp);
9571ae115bc7Smrj 	if (free_new_path)
9572ae115bc7Smrj 		free(new_path);
9573eb2bd662Svikram 	if (rv == BAM_WRITE) {
9574f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
9575eb2bd662Svikram 	} else {
9576f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
9577eb2bd662Svikram 	}
9578ae115bc7Smrj 	return (rv);
9579ae115bc7Smrj }
9580ae115bc7Smrj 
9581eb2bd662Svikram static error_t
9582eb2bd662Svikram get_kernel(menu_t *mp, menu_cmd_t optnum, char *buf, size_t bufsize)
9583eb2bd662Svikram {
9584eb2bd662Svikram 	const char	*fcn = "get_kernel()";
9585f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. arg: %s\n", fcn, menu_cmds[optnum]));
9586eb2bd662Svikram 	return (get_set_kernel(mp, optnum, NULL, buf, bufsize));
9587eb2bd662Svikram }
9588eb2bd662Svikram 
9589eb2bd662Svikram static error_t
9590eb2bd662Svikram set_kernel(menu_t *mp, menu_cmd_t optnum, char *path, char *buf, size_t bufsize)
9591eb2bd662Svikram {
9592eb2bd662Svikram 	const char	*fcn = "set_kernel()";
9593eb2bd662Svikram 	assert(path != NULL);
9594f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. args: %s %s\n", fcn,
9595f64ca102SToomas Soome 	    menu_cmds[optnum], path));
9596eb2bd662Svikram 	return (get_set_kernel(mp, optnum, path, buf, bufsize));
9597eb2bd662Svikram }
9598eb2bd662Svikram 
95997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
96007c478bd9Sstevel@tonic-gate static error_t
9601eb2bd662Svikram set_option(menu_t *mp, char *dummy, char *opt)
96027c478bd9Sstevel@tonic-gate {
9603eb2bd662Svikram 	int		optnum;
9604eb2bd662Svikram 	int		optval;
96057c478bd9Sstevel@tonic-gate 	char		*val;
9606ae115bc7Smrj 	char		buf[BUFSIZ] = "";
9607ae115bc7Smrj 	error_t		rv;
9608eb2bd662Svikram 	const char	*fcn = "set_option()";
96097c478bd9Sstevel@tonic-gate 
96107c478bd9Sstevel@tonic-gate 	assert(mp);
96117c478bd9Sstevel@tonic-gate 	assert(opt);
9612eb2bd662Svikram 	assert(dummy == NULL);
9613eb2bd662Svikram 
9614eb2bd662Svikram 	/* opt is set from bam_argv[0] and is always non-NULL */
9615f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered. arg: %s\n", fcn, opt));
96167c478bd9Sstevel@tonic-gate 
96177c478bd9Sstevel@tonic-gate 	val = strchr(opt, '=');
9618ae115bc7Smrj 	if (val != NULL) {
9619ae115bc7Smrj 		*val = '\0';
96207c478bd9Sstevel@tonic-gate 	}
96217c478bd9Sstevel@tonic-gate 
96227c478bd9Sstevel@tonic-gate 	if (strcmp(opt, "default") == 0) {
96237c478bd9Sstevel@tonic-gate 		optnum = DEFAULT_CMD;
96247c478bd9Sstevel@tonic-gate 	} else if (strcmp(opt, "timeout") == 0) {
96257c478bd9Sstevel@tonic-gate 		optnum = TIMEOUT_CMD;
9626ae115bc7Smrj 	} else if (strcmp(opt, menu_cmds[KERNEL_CMD]) == 0) {
9627ae115bc7Smrj 		optnum = KERNEL_CMD;
9628ae115bc7Smrj 	} else if (strcmp(opt, menu_cmds[ARGS_CMD]) == 0) {
9629ae115bc7Smrj 		optnum = ARGS_CMD;
96307c478bd9Sstevel@tonic-gate 	} else {
9631f64ca102SToomas Soome 		bam_error(_("invalid option: %s\n"), opt);
96327c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
96337c478bd9Sstevel@tonic-gate 	}
96347c478bd9Sstevel@tonic-gate 
9635ae115bc7Smrj 	/*
9636ae115bc7Smrj 	 * kernel and args are allowed without "=new_value" strings.  All
9637ae115bc7Smrj 	 * others cause errors
9638ae115bc7Smrj 	 */
9639ae115bc7Smrj 	if ((val == NULL) && (optnum != KERNEL_CMD) && (optnum != ARGS_CMD)) {
9640f64ca102SToomas Soome 		bam_error(_("option has no argument: %s\n"), opt);
9641ae115bc7Smrj 		return (BAM_ERROR);
9642ae115bc7Smrj 	} else if (val != NULL) {
96437c478bd9Sstevel@tonic-gate 		*val = '=';
9644ae115bc7Smrj 	}
9645ae115bc7Smrj 
9646ae115bc7Smrj 	if ((optnum == KERNEL_CMD) || (optnum == ARGS_CMD)) {
9647f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting %s option to %s\n",
9648f64ca102SToomas Soome 		    fcn, menu_cmds[optnum], val ? val + 1 : "NULL"));
9649eb2bd662Svikram 
9650eb2bd662Svikram 		if (val)
9651eb2bd662Svikram 			rv = set_kernel(mp, optnum, val + 1, buf, sizeof (buf));
9652eb2bd662Svikram 		else
9653eb2bd662Svikram 			rv = get_kernel(mp, optnum, buf, sizeof (buf));
9654ae115bc7Smrj 		if ((rv == BAM_SUCCESS) && (buf[0] != '\0'))
9655ae115bc7Smrj 			(void) printf("%s\n", buf);
9656ae115bc7Smrj 	} else {
9657ae115bc7Smrj 		optval = s_strtol(val + 1);
9658f64ca102SToomas Soome 		BAM_DPRINTF(("%s: setting %s option to %s\n", fcn,
9659f64ca102SToomas Soome 		    menu_cmds[optnum], val + 1));
9660eb2bd662Svikram 		rv = set_global(mp, menu_cmds[optnum], optval);
96617c478bd9Sstevel@tonic-gate 	}
9662eb2bd662Svikram 
9663eb2bd662Svikram 	if (rv == BAM_WRITE || rv == BAM_SUCCESS) {
9664f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning SUCCESS\n", fcn));
9665eb2bd662Svikram 	} else {
9666f64ca102SToomas Soome 		BAM_DPRINTF(("%s: returning FAILURE\n", fcn));
9667eb2bd662Svikram 	}
9668eb2bd662Svikram 
9669eb2bd662Svikram 	return (rv);
9670ae115bc7Smrj }
96717c478bd9Sstevel@tonic-gate 
96727c478bd9Sstevel@tonic-gate /*
96737c478bd9Sstevel@tonic-gate  * The quiet argument suppresses messages. This is used
96747c478bd9Sstevel@tonic-gate  * when invoked in the context of other commands (e.g. list_entry)
96757c478bd9Sstevel@tonic-gate  */
96767c478bd9Sstevel@tonic-gate static error_t
96777c478bd9Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet)
96787c478bd9Sstevel@tonic-gate {
96797c478bd9Sstevel@tonic-gate 	line_t *lp;
96807c478bd9Sstevel@tonic-gate 	char *arg;
96817c478bd9Sstevel@tonic-gate 	int done, ret = BAM_SUCCESS;
96827c478bd9Sstevel@tonic-gate 
96837c478bd9Sstevel@tonic-gate 	assert(mp);
96847c478bd9Sstevel@tonic-gate 	assert(menu_path);
96857c478bd9Sstevel@tonic-gate 	assert(globalcmd);
96867c478bd9Sstevel@tonic-gate 
96877c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
96887c478bd9Sstevel@tonic-gate 		if (!quiet)
9689f64ca102SToomas Soome 			bam_error(_("menu file not found: %s\n"), menu_path);
96907c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
96917c478bd9Sstevel@tonic-gate 	}
96927c478bd9Sstevel@tonic-gate 
96937c478bd9Sstevel@tonic-gate 	done = 0;
96947c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
96957c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
96967c478bd9Sstevel@tonic-gate 			continue;
96977c478bd9Sstevel@tonic-gate 
96987c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
96997c478bd9Sstevel@tonic-gate 			if (!quiet)
9700f64ca102SToomas Soome 				bam_error(_("no command at line %d\n"),
9701f64ca102SToomas Soome 				    lp->lineNum);
97027c478bd9Sstevel@tonic-gate 			continue;
97037c478bd9Sstevel@tonic-gate 		}
97047c478bd9Sstevel@tonic-gate 
97057c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
97067c478bd9Sstevel@tonic-gate 			continue;
97077c478bd9Sstevel@tonic-gate 
97087c478bd9Sstevel@tonic-gate 		/* Found global. Check for duplicates */
97097c478bd9Sstevel@tonic-gate 		if (done && !quiet) {
9710f64ca102SToomas Soome 			bam_error(_("duplicate command %s at line %d of "
9711f64ca102SToomas Soome 			    "%sboot/grub/menu.lst\n"), globalcmd,
9712f64ca102SToomas Soome 			    lp->lineNum, bam_root);
97137c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
97147c478bd9Sstevel@tonic-gate 		}
97157c478bd9Sstevel@tonic-gate 
97167c478bd9Sstevel@tonic-gate 		arg = lp->arg ? lp->arg : "";
9717f64ca102SToomas Soome 		bam_print(_("%s %s\n"), globalcmd, arg);
97187c478bd9Sstevel@tonic-gate 		done = 1;
97197c478bd9Sstevel@tonic-gate 	}
97207c478bd9Sstevel@tonic-gate 
97217c478bd9Sstevel@tonic-gate 	if (!done && bam_verbose)
9722f64ca102SToomas Soome 		bam_print(_("no %s entry found\n"), globalcmd);
97237c478bd9Sstevel@tonic-gate 
97247c478bd9Sstevel@tonic-gate 	return (ret);
97257c478bd9Sstevel@tonic-gate }
97267c478bd9Sstevel@tonic-gate 
97277c478bd9Sstevel@tonic-gate static error_t
97287c478bd9Sstevel@tonic-gate menu_write(char *root, menu_t *mp)
97297c478bd9Sstevel@tonic-gate {
9730eb2bd662Svikram 	const char *fcn = "menu_write()";
9731eb2bd662Svikram 
9732f64ca102SToomas Soome 	BAM_DPRINTF(("%s: entered menu_write() for root: <%s>\n", fcn, root));
97337c478bd9Sstevel@tonic-gate 	return (list2file(root, MENU_TMP, GRUB_MENU, mp->start));
97347c478bd9Sstevel@tonic-gate }
97357c478bd9Sstevel@tonic-gate 
9736eb2bd662Svikram void
97377c478bd9Sstevel@tonic-gate line_free(line_t *lp)
97387c478bd9Sstevel@tonic-gate {
97397c478bd9Sstevel@tonic-gate 	if (lp == NULL)
97407c478bd9Sstevel@tonic-gate 		return;
97417c478bd9Sstevel@tonic-gate 
9742eac223ccSWilliam Kucharski 	if (lp->cmd != NULL)
97437c478bd9Sstevel@tonic-gate 		free(lp->cmd);
97447c478bd9Sstevel@tonic-gate 	if (lp->sep)
97457c478bd9Sstevel@tonic-gate 		free(lp->sep);
97467c478bd9Sstevel@tonic-gate 	if (lp->arg)
97477c478bd9Sstevel@tonic-gate 		free(lp->arg);
97487c478bd9Sstevel@tonic-gate 	if (lp->line)
97497c478bd9Sstevel@tonic-gate 		free(lp->line);
97507c478bd9Sstevel@tonic-gate 	free(lp);
97517c478bd9Sstevel@tonic-gate }
97527c478bd9Sstevel@tonic-gate 
97537c478bd9Sstevel@tonic-gate static void
97547c478bd9Sstevel@tonic-gate linelist_free(line_t *start)
97557c478bd9Sstevel@tonic-gate {
97567c478bd9Sstevel@tonic-gate 	line_t *lp;
97577c478bd9Sstevel@tonic-gate 
97587c478bd9Sstevel@tonic-gate 	while (start) {
97597c478bd9Sstevel@tonic-gate 		lp = start;
97607c478bd9Sstevel@tonic-gate 		start = start->next;
97617c478bd9Sstevel@tonic-gate 		line_free(lp);
97627c478bd9Sstevel@tonic-gate 	}
97637c478bd9Sstevel@tonic-gate }
97647c478bd9Sstevel@tonic-gate 
97657c478bd9Sstevel@tonic-gate static void
97667c478bd9Sstevel@tonic-gate filelist_free(filelist_t *flistp)
97677c478bd9Sstevel@tonic-gate {
97687c478bd9Sstevel@tonic-gate 	linelist_free(flistp->head);
97697c478bd9Sstevel@tonic-gate 	flistp->head = NULL;
97707c478bd9Sstevel@tonic-gate 	flistp->tail = NULL;
97717c478bd9Sstevel@tonic-gate }
97727c478bd9Sstevel@tonic-gate 
97737c478bd9Sstevel@tonic-gate static void
97747c478bd9Sstevel@tonic-gate menu_free(menu_t *mp)
97757c478bd9Sstevel@tonic-gate {
97768c1b6884Sszhou 	entry_t *ent, *tmp;
97777c478bd9Sstevel@tonic-gate 	assert(mp);
97787c478bd9Sstevel@tonic-gate 
97797c478bd9Sstevel@tonic-gate 	if (mp->start)
97807c478bd9Sstevel@tonic-gate 		linelist_free(mp->start);
97818c1b6884Sszhou 	ent = mp->entries;
97828c1b6884Sszhou 	while (ent) {
97838c1b6884Sszhou 		tmp = ent;
97848c1b6884Sszhou 		ent = tmp->next;
97858c1b6884Sszhou 		free(tmp);
97868c1b6884Sszhou 	}
97877c478bd9Sstevel@tonic-gate 
97888c1b6884Sszhou 	free(mp);
97897c478bd9Sstevel@tonic-gate }
97907c478bd9Sstevel@tonic-gate 
97917c478bd9Sstevel@tonic-gate /*
97927c478bd9Sstevel@tonic-gate  * Utility routines
97937c478bd9Sstevel@tonic-gate  */
97947c478bd9Sstevel@tonic-gate 
97957c478bd9Sstevel@tonic-gate 
97967c478bd9Sstevel@tonic-gate /*
97977c478bd9Sstevel@tonic-gate  * Returns 0 on success
97987c478bd9Sstevel@tonic-gate  * Any other value indicates an error
97997c478bd9Sstevel@tonic-gate  */
98007c478bd9Sstevel@tonic-gate static int
9801986fd29aSsetje exec_cmd(char *cmdline, filelist_t *flistp)
98027c478bd9Sstevel@tonic-gate {
98037c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
98047c478bd9Sstevel@tonic-gate 	int ret;
98057c478bd9Sstevel@tonic-gate 	FILE *ptr;
98067c478bd9Sstevel@tonic-gate 	sigset_t set;
98077c478bd9Sstevel@tonic-gate 	void (*disp)(int);
98087c478bd9Sstevel@tonic-gate 
98097c478bd9Sstevel@tonic-gate 	/*
98107c478bd9Sstevel@tonic-gate 	 * For security
98117c478bd9Sstevel@tonic-gate 	 * - only absolute paths are allowed
98127c478bd9Sstevel@tonic-gate 	 * - set IFS to space and tab
98137c478bd9Sstevel@tonic-gate 	 */
98147c478bd9Sstevel@tonic-gate 	if (*cmdline != '/') {
9815f64ca102SToomas Soome 		bam_error(_("path is not absolute: %s\n"), cmdline);
98167c478bd9Sstevel@tonic-gate 		return (-1);
98177c478bd9Sstevel@tonic-gate 	}
98187c478bd9Sstevel@tonic-gate 	(void) putenv("IFS= \t");
98197c478bd9Sstevel@tonic-gate 
98207c478bd9Sstevel@tonic-gate 	/*
98217c478bd9Sstevel@tonic-gate 	 * We may have been exec'ed with SIGCHLD blocked
98227c478bd9Sstevel@tonic-gate 	 * unblock it here
98237c478bd9Sstevel@tonic-gate 	 */
98247c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
98257c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
98267c478bd9Sstevel@tonic-gate 	if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
9827f64ca102SToomas Soome 		bam_error(_("cannot unblock SIGCHLD: %s\n"), strerror(errno));
98287c478bd9Sstevel@tonic-gate 		return (-1);
98297c478bd9Sstevel@tonic-gate 	}
98307c478bd9Sstevel@tonic-gate 
98317c478bd9Sstevel@tonic-gate 	/*
98327c478bd9Sstevel@tonic-gate 	 * Set SIGCHLD disposition to SIG_DFL for popen/pclose
98337c478bd9Sstevel@tonic-gate 	 */
98347c478bd9Sstevel@tonic-gate 	disp = sigset(SIGCHLD, SIG_DFL);
98357c478bd9Sstevel@tonic-gate 	if (disp == SIG_ERR) {
9836f64ca102SToomas Soome 		bam_error(_("cannot set SIGCHLD disposition: %s\n"),
9837f64ca102SToomas Soome 		    strerror(errno));
98387c478bd9Sstevel@tonic-gate 		return (-1);
98397c478bd9Sstevel@tonic-gate 	}
98407c478bd9Sstevel@tonic-gate 	if (disp == SIG_HOLD) {
9841f64ca102SToomas Soome 		bam_error(_("SIGCHLD signal blocked. Cannot exec: %s\n"),
9842f64ca102SToomas Soome 		    cmdline);
98437c478bd9Sstevel@tonic-gate 		return (-1);
98447c478bd9Sstevel@tonic-gate 	}
98457c478bd9Sstevel@tonic-gate 
98467c478bd9Sstevel@tonic-gate 	ptr = popen(cmdline, "r");
98477c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
9848f64ca102SToomas Soome 		bam_error(_("popen failed: %s: %s\n"), cmdline,
9849f64ca102SToomas Soome 		    strerror(errno));
98507c478bd9Sstevel@tonic-gate 		return (-1);
98517c478bd9Sstevel@tonic-gate 	}
98527c478bd9Sstevel@tonic-gate 
98537c478bd9Sstevel@tonic-gate 	/*
98547c478bd9Sstevel@tonic-gate 	 * If we simply do a pclose() following a popen(), pclose()
98557c478bd9Sstevel@tonic-gate 	 * will close the reader end of the pipe immediately even
98567c478bd9Sstevel@tonic-gate 	 * if the child process has not started/exited. pclose()
98577c478bd9Sstevel@tonic-gate 	 * does wait for cmd to terminate before returning though.
98587c478bd9Sstevel@tonic-gate 	 * When the executed command writes its output to the pipe
98597c478bd9Sstevel@tonic-gate 	 * there is no reader process and the command dies with
98607c478bd9Sstevel@tonic-gate 	 * SIGPIPE. To avoid this we read repeatedly until read
98617c478bd9Sstevel@tonic-gate 	 * terminates with EOF. This indicates that the command
98627c478bd9Sstevel@tonic-gate 	 * (writer) has closed the pipe and we can safely do a
98637c478bd9Sstevel@tonic-gate 	 * pclose().
98647c478bd9Sstevel@tonic-gate 	 *
98657c478bd9Sstevel@tonic-gate 	 * Since pclose() does wait for the command to exit,
98667c478bd9Sstevel@tonic-gate 	 * we can safely reap the exit status of the command
98677c478bd9Sstevel@tonic-gate 	 * from the value returned by pclose()
98687c478bd9Sstevel@tonic-gate 	 */
9869986fd29aSsetje 	while (s_fgets(buf, sizeof (buf), ptr) != NULL) {
9870986fd29aSsetje 		if (flistp == NULL) {
9871986fd29aSsetje 			/* s_fgets strips newlines, so insert them at the end */
9872f64ca102SToomas Soome 			bam_print(_("%s\n"), buf);
9873986fd29aSsetje 		} else {
9874986fd29aSsetje 			append_to_flist(flistp, buf);
98757c478bd9Sstevel@tonic-gate 		}
98767c478bd9Sstevel@tonic-gate 	}
98777c478bd9Sstevel@tonic-gate 
98787c478bd9Sstevel@tonic-gate 	ret = pclose(ptr);
98797c478bd9Sstevel@tonic-gate 	if (ret == -1) {
9880f64ca102SToomas Soome 		bam_error(_("pclose failed: %s: %s\n"), cmdline,
9881f64ca102SToomas Soome 		    strerror(errno));
98827c478bd9Sstevel@tonic-gate 		return (-1);
98837c478bd9Sstevel@tonic-gate 	}
98847c478bd9Sstevel@tonic-gate 
98857c478bd9Sstevel@tonic-gate 	if (WIFEXITED(ret)) {
98867c478bd9Sstevel@tonic-gate 		return (WEXITSTATUS(ret));
98877c478bd9Sstevel@tonic-gate 	} else {
9888f64ca102SToomas Soome 		bam_error(_("command terminated abnormally: %s: %d\n"),
9889f64ca102SToomas Soome 		    cmdline, ret);
98907c478bd9Sstevel@tonic-gate 		return (-1);
98917c478bd9Sstevel@tonic-gate 	}
98927c478bd9Sstevel@tonic-gate }
98937c478bd9Sstevel@tonic-gate 
98947c478bd9Sstevel@tonic-gate /*
98957c478bd9Sstevel@tonic-gate  * Since this function returns -1 on error
98967c478bd9Sstevel@tonic-gate  * it cannot be used to convert -1. However,
98977c478bd9Sstevel@tonic-gate  * that is sufficient for what we need.
98987c478bd9Sstevel@tonic-gate  */
98997c478bd9Sstevel@tonic-gate static long
99007c478bd9Sstevel@tonic-gate s_strtol(char *str)
99017c478bd9Sstevel@tonic-gate {
99027c478bd9Sstevel@tonic-gate 	long l;
99037c478bd9Sstevel@tonic-gate 	char *res = NULL;
99047c478bd9Sstevel@tonic-gate 
99057c478bd9Sstevel@tonic-gate 	if (str == NULL) {
99067c478bd9Sstevel@tonic-gate 		return (-1);
99077c478bd9Sstevel@tonic-gate 	}
99087c478bd9Sstevel@tonic-gate 
99097c478bd9Sstevel@tonic-gate 	errno = 0;
99107c478bd9Sstevel@tonic-gate 	l = strtol(str, &res, 10);
99117c478bd9Sstevel@tonic-gate 	if (errno || *res != '\0') {
99127c478bd9Sstevel@tonic-gate 		return (-1);
99137c478bd9Sstevel@tonic-gate 	}
99147c478bd9Sstevel@tonic-gate 
99157c478bd9Sstevel@tonic-gate 	return (l);
99167c478bd9Sstevel@tonic-gate }
99177c478bd9Sstevel@tonic-gate 
99187c478bd9Sstevel@tonic-gate /*
99197c478bd9Sstevel@tonic-gate  * Wrapper around fputs, that adds a newline (since fputs doesn't)
99207c478bd9Sstevel@tonic-gate  */
99217c478bd9Sstevel@tonic-gate static int
99227c478bd9Sstevel@tonic-gate s_fputs(char *str, FILE *fp)
99237c478bd9Sstevel@tonic-gate {
99247c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
99257c478bd9Sstevel@tonic-gate 
99267c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s\n", str);
99277c478bd9Sstevel@tonic-gate 	return (fputs(linebuf, fp));
99287c478bd9Sstevel@tonic-gate }
99297c478bd9Sstevel@tonic-gate 
99307c478bd9Sstevel@tonic-gate /*
99317c478bd9Sstevel@tonic-gate  * Wrapper around fgets, that strips newlines returned by fgets
99327c478bd9Sstevel@tonic-gate  */
9933ae115bc7Smrj char *
99347c478bd9Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp)
99357c478bd9Sstevel@tonic-gate {
99367c478bd9Sstevel@tonic-gate 	int n;
99377c478bd9Sstevel@tonic-gate 
99387c478bd9Sstevel@tonic-gate 	buf = fgets(buf, buflen, fp);
99397c478bd9Sstevel@tonic-gate 	if (buf) {
99407c478bd9Sstevel@tonic-gate 		n = strlen(buf);
99417c478bd9Sstevel@tonic-gate 		if (n == buflen - 1 && buf[n-1] != '\n')
9942f64ca102SToomas Soome 			bam_error(_("the following line is too long "
9943f64ca102SToomas Soome 			    "(> %d chars)\n\t%s\n"), buflen - 1, buf);
99447c478bd9Sstevel@tonic-gate 		buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1];
99457c478bd9Sstevel@tonic-gate 	}
99467c478bd9Sstevel@tonic-gate 
99477c478bd9Sstevel@tonic-gate 	return (buf);
99487c478bd9Sstevel@tonic-gate }
99497c478bd9Sstevel@tonic-gate 
9950ae115bc7Smrj void *
99517c478bd9Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz)
99527c478bd9Sstevel@tonic-gate {
99537c478bd9Sstevel@tonic-gate 	void *ptr;
99547c478bd9Sstevel@tonic-gate 
99557c478bd9Sstevel@tonic-gate 	ptr = calloc(nelem, sz);
99567c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
9957f64ca102SToomas Soome 		bam_error(_("could not allocate memory: size = %u\n"),
9958f64ca102SToomas Soome 		    nelem*sz);
99597c478bd9Sstevel@tonic-gate 		bam_exit(1);
99607c478bd9Sstevel@tonic-gate 	}
99617c478bd9Sstevel@tonic-gate 	return (ptr);
99627c478bd9Sstevel@tonic-gate }
99637c478bd9Sstevel@tonic-gate 
9964ae115bc7Smrj void *
9965ae115bc7Smrj s_realloc(void *ptr, size_t sz)
9966ae115bc7Smrj {
9967ae115bc7Smrj 	ptr = realloc(ptr, sz);
9968ae115bc7Smrj 	if (ptr == NULL) {
9969f64ca102SToomas Soome 		bam_error(_("could not allocate memory: size = %u\n"), sz);
9970ae115bc7Smrj 		bam_exit(1);
9971ae115bc7Smrj 	}
9972ae115bc7Smrj 	return (ptr);
9973ae115bc7Smrj }
9974ae115bc7Smrj 
9975eb2bd662Svikram char *
99767c478bd9Sstevel@tonic-gate s_strdup(char *str)
99777c478bd9Sstevel@tonic-gate {
99787c478bd9Sstevel@tonic-gate 	char *ptr;
99797c478bd9Sstevel@tonic-gate 
99807c478bd9Sstevel@tonic-gate 	if (str == NULL)
99817c478bd9Sstevel@tonic-gate 		return (NULL);
99827c478bd9Sstevel@tonic-gate 
99837c478bd9Sstevel@tonic-gate 	ptr = strdup(str);
99847c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
9985f64ca102SToomas Soome 		bam_error(_("could not allocate memory: size = %u\n"),
9986f64ca102SToomas Soome 		    strlen(str) + 1);
99877c478bd9Sstevel@tonic-gate 		bam_exit(1);
99887c478bd9Sstevel@tonic-gate 	}
99897c478bd9Sstevel@tonic-gate 	return (ptr);
99907c478bd9Sstevel@tonic-gate }
99917c478bd9Sstevel@tonic-gate 
99927c478bd9Sstevel@tonic-gate /*
99937c478bd9Sstevel@tonic-gate  * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients)
99947c478bd9Sstevel@tonic-gate  * Returns 0 otherwise
99957c478bd9Sstevel@tonic-gate  */
99967c478bd9Sstevel@tonic-gate static int
99977c478bd9Sstevel@tonic-gate is_amd64(void)
99987c478bd9Sstevel@tonic-gate {
99997c478bd9Sstevel@tonic-gate 	static int amd64 = -1;
100007c478bd9Sstevel@tonic-gate 	char isabuf[257];	/* from sysinfo(2) manpage */
100017c478bd9Sstevel@tonic-gate 
100027c478bd9Sstevel@tonic-gate 	if (amd64 != -1)
100037c478bd9Sstevel@tonic-gate 		return (amd64);
100047c478bd9Sstevel@tonic-gate 
10005d876c67dSjg 	if (bam_alt_platform) {
10006d876c67dSjg 		if (strcmp(bam_platform, "i86pc") == 0) {
100077c478bd9Sstevel@tonic-gate 			amd64 = 1;		/* diskless server */
10008d876c67dSjg 		}
10009d876c67dSjg 	} else {
10010d876c67dSjg 		if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 &&
10011d876c67dSjg 		    strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0) {
10012d876c67dSjg 			amd64 = 1;
10013d876c67dSjg 		} else if (strstr(isabuf, "i386") == NULL) {
10014d876c67dSjg 			amd64 = 1;		/* diskless server */
10015d876c67dSjg 		}
10016d876c67dSjg 	}
10017d876c67dSjg 	if (amd64 == -1)
100187c478bd9Sstevel@tonic-gate 		amd64 = 0;
100197c478bd9Sstevel@tonic-gate 
100207c478bd9Sstevel@tonic-gate 	return (amd64);
100217c478bd9Sstevel@tonic-gate }
100227c478bd9Sstevel@tonic-gate 
1002379755401Ssetje static char *
1002479755401Ssetje get_machine(void)
10025986fd29aSsetje {
1002679755401Ssetje 	static int cached = -1;
1002779755401Ssetje 	static char mbuf[257];	/* from sysinfo(2) manpage */
10028986fd29aSsetje 
1002979755401Ssetje 	if (cached == 0)
1003079755401Ssetje 		return (mbuf);
10031d876c67dSjg 
10032d876c67dSjg 	if (bam_alt_platform) {
1003379755401Ssetje 		return (bam_platform);
10034d876c67dSjg 	} else {
1003579755401Ssetje 		if (sysinfo(SI_MACHINE, mbuf, sizeof (mbuf)) > 0) {
1003679755401Ssetje 			cached = 1;
10037d876c67dSjg 		}
10038d876c67dSjg 	}
1003979755401Ssetje 	if (cached == -1) {
1004079755401Ssetje 		mbuf[0] = '\0';
1004179755401Ssetje 		cached = 0;
10042986fd29aSsetje 	}
10043986fd29aSsetje 
1004479755401Ssetje 	return (mbuf);
10045986fd29aSsetje }
10046986fd29aSsetje 
10047eb2bd662Svikram int
10048eb2bd662Svikram is_sparc(void)
10049eb2bd662Svikram {
1005079755401Ssetje 	static int issparc = -1;
1005179755401Ssetje 	char mbuf[257];	/* from sysinfo(2) manpage */
1005279755401Ssetje 
1005379755401Ssetje 	if (issparc != -1)
1005479755401Ssetje 		return (issparc);
1005579755401Ssetje 
1005679755401Ssetje 	if (bam_alt_platform) {
1005779755401Ssetje 		if (strncmp(bam_platform, "sun4", 4) == 0) {
1005879755401Ssetje 			issparc = 1;
1005979755401Ssetje 		}
1006079755401Ssetje 	} else {
1006179755401Ssetje 		if (sysinfo(SI_ARCHITECTURE, mbuf, sizeof (mbuf)) > 0 &&
10062d7d0f93bSjg 		    strcmp(mbuf, "sparc") == 0) {
1006379755401Ssetje 			issparc = 1;
1006479755401Ssetje 		}
10065d7d0f93bSjg 	}
10066d7d0f93bSjg 	if (issparc == -1)
10067d7d0f93bSjg 		issparc = 0;
1006879755401Ssetje 
1006979755401Ssetje 	return (issparc);
10070eb2bd662Svikram }
10071986fd29aSsetje 
100727c478bd9Sstevel@tonic-gate static void
100737c478bd9Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s)
100747c478bd9Sstevel@tonic-gate {
100757c478bd9Sstevel@tonic-gate 	line_t *lp;
100767c478bd9Sstevel@tonic-gate 
100777c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
100787c478bd9Sstevel@tonic-gate 	lp->line = s_strdup(s);
100797c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL)
100807c478bd9Sstevel@tonic-gate 		flistp->head = lp;
100817c478bd9Sstevel@tonic-gate 	else
100827c478bd9Sstevel@tonic-gate 		flistp->tail->next = lp;
100837c478bd9Sstevel@tonic-gate 	flistp->tail = lp;
100847c478bd9Sstevel@tonic-gate }
100852449e17fSsherrym 
1008679c28b70SToomas Soome #if !defined(_OBP)
100872449e17fSsherrym 
100882449e17fSsherrym UCODE_VENDORS;
100892449e17fSsherrym 
100902449e17fSsherrym /*ARGSUSED*/
100912449e17fSsherrym static void
100922449e17fSsherrym ucode_install(char *root)
100932449e17fSsherrym {
100942449e17fSsherrym 	int i;
100952449e17fSsherrym 
100962449e17fSsherrym 	for (i = 0; ucode_vendors[i].filestr != NULL; i++) {
100972449e17fSsherrym 		int cmd_len = PATH_MAX + 256;
100982449e17fSsherrym 		char cmd[PATH_MAX + 256];
100992449e17fSsherrym 		char file[PATH_MAX];
101002449e17fSsherrym 		char timestamp[PATH_MAX];
101012449e17fSsherrym 		struct stat fstatus, tstatus;
101022449e17fSsherrym 		struct utimbuf u_times;
101032449e17fSsherrym 
10104adc586deSMark Johnson 		(void) snprintf(file, PATH_MAX, "%s/%s/%s-ucode.%s",
10105adc586deSMark Johnson 		    bam_root, UCODE_INSTALL_PATH, ucode_vendors[i].filestr,
10106adc586deSMark Johnson 		    ucode_vendors[i].extstr);
101072449e17fSsherrym 
101082449e17fSsherrym 		if (stat(file, &fstatus) != 0 || !(S_ISREG(fstatus.st_mode)))
101092449e17fSsherrym 			continue;
101102449e17fSsherrym 
101112449e17fSsherrym 		(void) snprintf(timestamp, PATH_MAX, "%s.ts", file);
101122449e17fSsherrym 
101132449e17fSsherrym 		if (stat(timestamp, &tstatus) == 0 &&
101142449e17fSsherrym 		    fstatus.st_mtime <= tstatus.st_mtime)
101152449e17fSsherrym 			continue;
101162449e17fSsherrym 
101172449e17fSsherrym 		(void) snprintf(cmd, cmd_len, "/usr/sbin/ucodeadm -i -R "
101182449e17fSsherrym 		    "%s/%s/%s %s > /dev/null 2>&1", bam_root,
101192449e17fSsherrym 		    UCODE_INSTALL_PATH, ucode_vendors[i].vendorstr, file);
101202449e17fSsherrym 		if (system(cmd) != 0)
101212449e17fSsherrym 			return;
101222449e17fSsherrym 
101232449e17fSsherrym 		if (creat(timestamp, S_IRUSR | S_IWUSR) == -1)
101242449e17fSsherrym 			return;
101252449e17fSsherrym 
101262449e17fSsherrym 		u_times.actime = fstatus.st_atime;
101272449e17fSsherrym 		u_times.modtime = fstatus.st_mtime;
101282449e17fSsherrym 		(void) utime(timestamp, &u_times);
101292449e17fSsherrym 	}
101302449e17fSsherrym }
101312449e17fSsherrym #endif
10132