xref: /titanic_50/usr/src/cmd/boot/bootadm/bootadm.c (revision 40a5c998e5462ed47916e2edfcc5016073cf8851)
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 /*
229adc5a4eSEdward Gillett  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
26*40a5c998SMatthew Ahrens  * Copyright 2012 Milan Jurik. All rights reserved.
271a902ef8SHans Rosenfeld  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
28*40a5c998SMatthew Ahrens  * Copyright (c) 2015 by Delphix. All rights reserved.
296debd3f5SAlexander Eremin  */
306debd3f5SAlexander Eremin 
316debd3f5SAlexander Eremin /*
327c478bd9Sstevel@tonic-gate  * bootadm(1M) is a new utility for managing bootability of
337c478bd9Sstevel@tonic-gate  * Solaris *Newboot* environments. It has two primary tasks:
347c478bd9Sstevel@tonic-gate  * 	- Allow end users to manage bootability of Newboot Solaris instances
357c478bd9Sstevel@tonic-gate  *	- Provide services to other subsystems in Solaris (primarily Install)
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /* Headers */
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <string.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
4644da779fSWilliam Kucharski #include <alloca.h>
477c478bd9Sstevel@tonic-gate #include <stdarg.h>
487c478bd9Sstevel@tonic-gate #include <limits.h>
497c478bd9Sstevel@tonic-gate #include <signal.h>
507c478bd9Sstevel@tonic-gate #include <sys/wait.h>
517c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
52f904d32dSJerry Gilliam #include <sys/mntent.h>
537c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
547c478bd9Sstevel@tonic-gate #include <libnvpair.h>
557c478bd9Sstevel@tonic-gate #include <ftw.h>
567c478bd9Sstevel@tonic-gate #include <fcntl.h>
577c478bd9Sstevel@tonic-gate #include <strings.h>
582449e17fSsherrym #include <utime.h>
597c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
607c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
6158091fd8Ssetje #include <sys/param.h>
62eb2bd662Svikram #include <dirent.h>
63eb2bd662Svikram #include <ctype.h>
64eb2bd662Svikram #include <libgen.h>
65e7cbe64fSgw25295 #include <sys/sysmacros.h>
6648847494SEnrico Perla - Sun Microsystems #include <sys/elf.h>
67963390b4Svikram #include <libscf.h>
6848847494SEnrico Perla - Sun Microsystems #include <zlib.h>
6948847494SEnrico Perla - Sun Microsystems #include <sys/lockfs.h>
7048847494SEnrico Perla - Sun Microsystems #include <sys/filio.h>
716debd3f5SAlexander Eremin #include <libbe.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 
897c478bd9Sstevel@tonic-gate #include "message.h"
90ae115bc7Smrj #include "bootadm.h"
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
937c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
947c478bd9Sstevel@tonic-gate #endif	/* TEXT_DOMAIN */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /* Type definitions */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /* Primary subcmds */
997c478bd9Sstevel@tonic-gate typedef enum {
1007c478bd9Sstevel@tonic-gate 	BAM_MENU = 3,
101c7c0ceafSToomas Soome 	BAM_ARCHIVE,
102c7c0ceafSToomas Soome 	BAM_INSTALL
1037c478bd9Sstevel@tonic-gate } subcmd_t;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate typedef enum {
1067c478bd9Sstevel@tonic-gate     OPT_ABSENT = 0,	/* No option */
1077c478bd9Sstevel@tonic-gate     OPT_REQ,		/* option required */
1087c478bd9Sstevel@tonic-gate     OPT_OPTIONAL	/* option may or may not be present */
1097c478bd9Sstevel@tonic-gate } option_t;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate typedef struct {
1127c478bd9Sstevel@tonic-gate 	char	*subcmd;
1137c478bd9Sstevel@tonic-gate 	option_t option;
1147c478bd9Sstevel@tonic-gate 	error_t (*handler)();
1151a97e40eSvikram 	int	unpriv;			/* is this an unprivileged command */
1167c478bd9Sstevel@tonic-gate } subcmd_defn_t;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #define	LINE_INIT	0	/* lineNum initial value */
1197c478bd9Sstevel@tonic-gate #define	ENTRY_INIT	-1	/* entryNum initial value */
1207c478bd9Sstevel@tonic-gate #define	ALL_ENTRIES	-2	/* selects all boot entries */
1217c478bd9Sstevel@tonic-gate 
1221a902ef8SHans Rosenfeld #define	PARTNO_NOTFOUND -1	/* Solaris partition not found */
1231a902ef8SHans Rosenfeld #define	PARTNO_EFI	-2	/* EFI partition table found */
1241a902ef8SHans Rosenfeld 
1257c478bd9Sstevel@tonic-gate #define	GRUB_DIR		"/boot/grub"
126963390b4Svikram #define	GRUB_STAGE2		GRUB_DIR "/stage2"
1277c478bd9Sstevel@tonic-gate #define	GRUB_MENU		"/boot/grub/menu.lst"
1287c478bd9Sstevel@tonic-gate #define	MENU_TMP		"/boot/grub/menu.lst.tmp"
129963390b4Svikram #define	GRUB_BACKUP_MENU	"/etc/lu/GRUB_backup_menu"
13008db0dbcSAlexander Eremin #define	RAMDISK_SPECIAL		"/dev/ramdisk/"
13140541d5dSvikram #define	STUBBOOT		"/stubboot"
132eb2bd662Svikram #define	MULTIBOOT		"/platform/i86pc/multiboot"
133eb2bd662Svikram #define	GRUBSIGN_DIR		"/boot/grub/bootsign"
134eb2bd662Svikram #define	GRUBSIGN_BACKUP		"/etc/bootsign"
135eb2bd662Svikram #define	GRUBSIGN_UFS_PREFIX	"rootfs"
136eb2bd662Svikram #define	GRUBSIGN_ZFS_PREFIX	"pool_"
137eb2bd662Svikram #define	GRUBSIGN_LU_PREFIX	"BE_"
138eb2bd662Svikram #define	UFS_SIGNATURE_LIST	"/var/run/grub_ufs_signatures"
139eb2bd662Svikram #define	ZFS_LEGACY_MNTPT	"/tmp/bootadm_mnt_zfs_legacy"
140eb2bd662Svikram 
141eb2bd662Svikram #define	BOOTADM_RDONLY_TEST	"BOOTADM_RDONLY_TEST"
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /* lock related */
1447c478bd9Sstevel@tonic-gate #define	BAM_LOCK_FILE		"/var/run/bootadm.lock"
1457c478bd9Sstevel@tonic-gate #define	LOCK_FILE_PERMS		(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
1467c478bd9Sstevel@tonic-gate 
147986fd29aSsetje #define	CREATE_RAMDISK		"boot/solaris/bin/create_ramdisk"
148986fd29aSsetje #define	CREATE_DISKMAP		"boot/solaris/bin/create_diskmap"
149986fd29aSsetje #define	EXTRACT_BOOT_FILELIST	"boot/solaris/bin/extract_boot_filelist"
1507c478bd9Sstevel@tonic-gate #define	GRUBDISK_MAP		"/var/run/solaris_grubdisk.map"
1517c478bd9Sstevel@tonic-gate 
152b610f78eSvikram #define	GRUB_slice		"/etc/lu/GRUB_slice"
153b610f78eSvikram #define	GRUB_root		"/etc/lu/GRUB_root"
154fbac2b2bSvikram #define	GRUB_fdisk		"/etc/lu/GRUB_fdisk"
155fbac2b2bSvikram #define	GRUB_fdisk_target	"/etc/lu/GRUB_fdisk_target"
156963390b4Svikram #define	FINDROOT_INSTALLGRUB	"/etc/lu/installgrub.findroot"
157963390b4Svikram #define	LULIB			"/usr/lib/lu/lulib"
158963390b4Svikram #define	LULIB_PROPAGATE_FILE	"lulib_propagate_file"
159963390b4Svikram #define	CKSUM			"/usr/bin/cksum"
160963390b4Svikram #define	LU_MENU_CKSUM		"/etc/lu/menu.cksum"
161963390b4Svikram #define	BOOTADM			"/sbin/bootadm"
162b610f78eSvikram 
163b610f78eSvikram #define	INSTALLGRUB		"/sbin/installgrub"
164b610f78eSvikram #define	STAGE1			"/boot/grub/stage1"
165b610f78eSvikram #define	STAGE2			"/boot/grub/stage2"
166b610f78eSvikram 
167eb2bd662Svikram typedef enum zfs_mnted {
168eb2bd662Svikram 	ZFS_MNT_ERROR = -1,
169eb2bd662Svikram 	LEGACY_MOUNTED = 1,
170eb2bd662Svikram 	LEGACY_ALREADY,
171eb2bd662Svikram 	ZFS_MOUNTED,
172eb2bd662Svikram 	ZFS_ALREADY
173eb2bd662Svikram } zfs_mnted_t;
174eb2bd662Svikram 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * Default file attributes
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_MODE	0644	/* default permissions */
1797c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_UID		0	/* user root */
1807c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_GID		3	/* group sys */
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate  * Menu related
1847c478bd9Sstevel@tonic-gate  * menu_cmd_t and menu_cmds must be kept in sync
1857c478bd9Sstevel@tonic-gate  */
186ae115bc7Smrj char *menu_cmds[] = {
1877c478bd9Sstevel@tonic-gate 	"default",	/* DEFAULT_CMD */
1887c478bd9Sstevel@tonic-gate 	"timeout",	/* TIMEOUT_CMD */
1897c478bd9Sstevel@tonic-gate 	"title",	/* TITLE_CMD */
1907c478bd9Sstevel@tonic-gate 	"root",		/* ROOT_CMD */
1917c478bd9Sstevel@tonic-gate 	"kernel",	/* KERNEL_CMD */
192ae115bc7Smrj 	"kernel$",	/* KERNEL_DOLLAR_CMD */
1937c478bd9Sstevel@tonic-gate 	"module",	/* MODULE_CMD */
194ae115bc7Smrj 	"module$",	/* MODULE_DOLLAR_CMD */
1957c478bd9Sstevel@tonic-gate 	" ",		/* SEP_CMD */
1967c478bd9Sstevel@tonic-gate 	"#",		/* COMMENT_CMD */
197ae115bc7Smrj 	"chainloader",	/* CHAINLOADER_CMD */
198ae115bc7Smrj 	"args",		/* ARGS_CMD */
199eb2bd662Svikram 	"findroot",	/* FINDROOT_CMD */
20044da779fSWilliam Kucharski 	"bootfs",	/* BOOTFS_CMD */
2017c478bd9Sstevel@tonic-gate 	NULL
2027c478bd9Sstevel@tonic-gate };
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate #define	OPT_ENTRY_NUM	"entry"
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
207eb2bd662Svikram  * exec_cmd related
2087c478bd9Sstevel@tonic-gate  */
2097c478bd9Sstevel@tonic-gate typedef struct {
2107c478bd9Sstevel@tonic-gate 	line_t *head;
2117c478bd9Sstevel@tonic-gate 	line_t *tail;
2127c478bd9Sstevel@tonic-gate } filelist_t;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate #define	BOOT_FILE_LIST	"boot/solaris/filelist.ramdisk"
2157c478bd9Sstevel@tonic-gate #define	ETC_FILE_LIST	"etc/boot/solaris/filelist.ramdisk"
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate #define	FILE_STAT	"boot/solaris/filestat.ramdisk"
2187c478bd9Sstevel@tonic-gate #define	FILE_STAT_TMP	"boot/solaris/filestat.ramdisk.tmp"
2197c478bd9Sstevel@tonic-gate #define	DIR_PERMS	(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
2207c478bd9Sstevel@tonic-gate #define	FILE_STAT_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
2217c478bd9Sstevel@tonic-gate 
2224a9df875SEnrico Perla - Sun Microsystems #define	FILE_STAT_TIMESTAMP	"boot/solaris/timestamp.cache"
2234a9df875SEnrico Perla - Sun Microsystems 
2247c478bd9Sstevel@tonic-gate /* Globals */
225ae115bc7Smrj int bam_verbose;
226ae115bc7Smrj int bam_force;
227eb2bd662Svikram int bam_debug;
2287c478bd9Sstevel@tonic-gate static char *prog;
2297c478bd9Sstevel@tonic-gate static subcmd_t bam_cmd;
2307c478bd9Sstevel@tonic-gate static char *bam_root;
2317c478bd9Sstevel@tonic-gate static int bam_rootlen;
2327c478bd9Sstevel@tonic-gate static int bam_root_readonly;
23340541d5dSvikram static int bam_alt_root;
23448847494SEnrico Perla - Sun Microsystems static int bam_extend = 0;
23548847494SEnrico Perla - Sun Microsystems static int bam_purge = 0;
2367c478bd9Sstevel@tonic-gate static char *bam_subcmd;
2377c478bd9Sstevel@tonic-gate static char *bam_opt;
2387c478bd9Sstevel@tonic-gate static char **bam_argv;
239c7c0ceafSToomas Soome static char *bam_pool;
2407c478bd9Sstevel@tonic-gate static int bam_argc;
2417c478bd9Sstevel@tonic-gate static int bam_check;
2425eea6091SEnrico Perla - Sun Microsystems static int bam_saved_check;
2437c478bd9Sstevel@tonic-gate static int bam_smf_check;
2447c478bd9Sstevel@tonic-gate static int bam_lock_fd = -1;
245e7cbe64fSgw25295 static int bam_zfs;
246c7c0ceafSToomas Soome static int bam_mbr;
2477c478bd9Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/";
248b610f78eSvikram static int bam_update_all;
249d876c67dSjg static int bam_alt_platform;
250d876c67dSjg static char *bam_platform;
251cedc7e57SEnrico Perla - Sun Microsystems static char *bam_home_env = NULL;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /* function prototypes */
254986fd29aSsetje static void parse_args_internal(int, char *[]);
255986fd29aSsetje static void parse_args(int, char *argv[]);
256986fd29aSsetje static error_t bam_menu(char *, char *, int, char *[]);
257c7c0ceafSToomas Soome static error_t bam_install(char *, char *);
258986fd29aSsetje static error_t bam_archive(char *, char *);
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate static void bam_lock(void);
2617c478bd9Sstevel@tonic-gate static void bam_unlock(void);
2627c478bd9Sstevel@tonic-gate 
263986fd29aSsetje static int exec_cmd(char *, filelist_t *);
264986fd29aSsetje static error_t read_globals(menu_t *, char *, char *, int);
265eb2bd662Svikram static int menu_on_bootdisk(char *os_root, char *menu_root);
266986fd29aSsetje static menu_t *menu_read(char *);
267986fd29aSsetje static error_t menu_write(char *, menu_t *);
268986fd29aSsetje static void linelist_free(line_t *);
269986fd29aSsetje static void menu_free(menu_t *);
270986fd29aSsetje static void filelist_free(filelist_t *);
271986fd29aSsetje static error_t list2file(char *, char *, char *, line_t *);
272986fd29aSsetje static error_t list_entry(menu_t *, char *, char *);
27344da779fSWilliam Kucharski static error_t list_setting(menu_t *, char *, char *);
274986fd29aSsetje static error_t delete_all_entries(menu_t *, char *, char *);
275eb2bd662Svikram static error_t update_entry(menu_t *mp, char *menu_root, char *opt);
276eb2bd662Svikram static error_t update_temp(menu_t *mp, char *dummy, char *opt);
2777c478bd9Sstevel@tonic-gate 
278c7c0ceafSToomas Soome static error_t install_bootloader(void);
279986fd29aSsetje static error_t update_archive(char *, char *);
280986fd29aSsetje static error_t list_archive(char *, char *);
281986fd29aSsetje static error_t update_all(char *, char *);
282986fd29aSsetje static error_t read_list(char *, filelist_t *);
283986fd29aSsetje static error_t set_option(menu_t *, char *, char *);
284986fd29aSsetje static error_t set_kernel(menu_t *, menu_cmd_t, char *, char *, size_t);
285eb2bd662Svikram static error_t get_kernel(menu_t *, menu_cmd_t, char *, size_t);
286986fd29aSsetje static char *expand_path(const char *);
2877c478bd9Sstevel@tonic-gate 
288986fd29aSsetje static long s_strtol(char *);
289986fd29aSsetje static int s_fputs(char *, FILE *);
2907c478bd9Sstevel@tonic-gate 
291eb2bd662Svikram static int is_zfs(char *root);
292eb2bd662Svikram static int is_ufs(char *root);
293eb2bd662Svikram static int is_pcfs(char *root);
2947c478bd9Sstevel@tonic-gate static int is_amd64(void);
29579755401Ssetje static char *get_machine(void);
2967c478bd9Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *);
297eb2bd662Svikram static char *mount_top_dataset(char *pool, zfs_mnted_t *mnted);
298eb2bd662Svikram static int umount_top_dataset(char *pool, zfs_mnted_t mnted, char *mntpt);
299eb2bd662Svikram static int ufs_add_to_sign_list(char *sign);
300963390b4Svikram static error_t synchronize_BE_menu(void);
3017c478bd9Sstevel@tonic-gate 
30279c28b70SToomas Soome #if !defined(_OBP)
3032449e17fSsherrym static void ucode_install();
3042449e17fSsherrym #endif
3052449e17fSsherrym 
3067c478bd9Sstevel@tonic-gate /* Menu related sub commands */
3077c478bd9Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = {
308eb2bd662Svikram 	"set_option",		OPT_ABSENT,	set_option, 0,	/* PUB */
3091a97e40eSvikram 	"list_entry",		OPT_OPTIONAL,	list_entry, 1,	/* PUB */
3101a97e40eSvikram 	"delete_all_entries",	OPT_ABSENT,	delete_all_entries, 0, /* PVT */
3111a97e40eSvikram 	"update_entry",		OPT_REQ,	update_entry, 0, /* menu */
3121a97e40eSvikram 	"update_temp",		OPT_OPTIONAL,	update_temp, 0,	/* reboot */
313ae115bc7Smrj 	"upgrade",		OPT_ABSENT,	upgrade_menu, 0, /* menu */
31444da779fSWilliam Kucharski 	"list_setting",		OPT_OPTIONAL,	list_setting, 1, /* menu */
31544da779fSWilliam Kucharski 	"disable_hypervisor",	OPT_ABSENT,	cvt_to_metal, 0, /* menu */
31644da779fSWilliam Kucharski 	"enable_hypervisor",	OPT_ABSENT,	cvt_to_hyper, 0, /* menu */
3171a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
3187c478bd9Sstevel@tonic-gate };
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /* Archive related sub commands */
3217c478bd9Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = {
3221a97e40eSvikram 	"update",		OPT_ABSENT,	update_archive, 0, /* PUB */
3231a97e40eSvikram 	"update_all",		OPT_ABSENT,	update_all, 0,	/* PVT */
3241a97e40eSvikram 	"list",			OPT_OPTIONAL,	list_archive, 1, /* PUB */
3251a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
3267c478bd9Sstevel@tonic-gate };
3277c478bd9Sstevel@tonic-gate 
328c7c0ceafSToomas Soome /* Install related sub commands */
329c7c0ceafSToomas Soome static subcmd_defn_t inst_subcmds[] = {
330c7c0ceafSToomas Soome 	"install_bootloader",	OPT_ABSENT,	install_bootloader, 0, /* PUB */
331c7c0ceafSToomas Soome 	NULL,			0,		NULL, 0	/* must be last */
332c7c0ceafSToomas Soome };
333c7c0ceafSToomas Soome 
33448847494SEnrico Perla - Sun Microsystems enum dircache_copy_opt {
33548847494SEnrico Perla - Sun Microsystems 	FILE32 = 0,
33648847494SEnrico Perla - Sun Microsystems 	FILE64,
33748847494SEnrico Perla - Sun Microsystems 	CACHEDIR_NUM
33848847494SEnrico Perla - Sun Microsystems };
33948847494SEnrico Perla - Sun Microsystems 
34048847494SEnrico Perla - Sun Microsystems /*
34148847494SEnrico Perla - Sun Microsystems  * Directory specific flags:
34248847494SEnrico Perla - Sun Microsystems  * NEED_UPDATE : the specified archive needs to be updated
34348847494SEnrico Perla - Sun Microsystems  * NO_MULTI : don't extend the specified archive, but recreate it
34448847494SEnrico Perla - Sun Microsystems  */
34548847494SEnrico Perla - Sun Microsystems #define	NEED_UPDATE		0x00000001
34648847494SEnrico Perla - Sun Microsystems #define	NO_MULTI		0x00000002
34748847494SEnrico Perla - Sun Microsystems 
34848847494SEnrico Perla - Sun Microsystems #define	set_dir_flag(id, f)	(walk_arg.dirinfo[id].flags |= f)
34948847494SEnrico Perla - Sun Microsystems #define	unset_dir_flag(id, f)	(walk_arg.dirinfo[id].flags &= ~f)
35048847494SEnrico Perla - Sun Microsystems #define	is_dir_flag_on(id, f)	(walk_arg.dirinfo[id].flags & f ? 1 : 0)
35148847494SEnrico Perla - Sun Microsystems 
35248847494SEnrico Perla - Sun Microsystems #define	get_cachedir(id)	(walk_arg.dirinfo[id].cdir_path)
35348847494SEnrico Perla - Sun Microsystems #define	get_updatedir(id)	(walk_arg.dirinfo[id].update_path)
35448847494SEnrico Perla - Sun Microsystems #define	get_count(id)		(walk_arg.dirinfo[id].count)
35548847494SEnrico Perla - Sun Microsystems #define	has_cachedir(id)	(walk_arg.dirinfo[id].has_dir)
35648847494SEnrico Perla - Sun Microsystems #define	set_dir_present(id)	(walk_arg.dirinfo[id].has_dir = 1)
35748847494SEnrico Perla - Sun Microsystems 
35848847494SEnrico Perla - Sun Microsystems /*
35948847494SEnrico Perla - Sun Microsystems  * dirinfo_t (specific cache directory information):
36048847494SEnrico Perla - Sun Microsystems  * cdir_path:   path to the archive cache directory
36148847494SEnrico Perla - Sun Microsystems  * update_path: path to the update directory (contains the files that will be
36248847494SEnrico Perla - Sun Microsystems  *              used to extend the archive)
36348847494SEnrico Perla - Sun Microsystems  * has_dir:	the specified cache directory is active
36448847494SEnrico Perla - Sun Microsystems  * count:	the number of files to update
36548847494SEnrico Perla - Sun Microsystems  * flags:	directory specific flags
36648847494SEnrico Perla - Sun Microsystems  */
36748847494SEnrico Perla - Sun Microsystems typedef struct _dirinfo {
36848847494SEnrico Perla - Sun Microsystems 	char	cdir_path[PATH_MAX];
36948847494SEnrico Perla - Sun Microsystems 	char	update_path[PATH_MAX];
37048847494SEnrico Perla - Sun Microsystems 	int	has_dir;
37148847494SEnrico Perla - Sun Microsystems 	int	count;
37248847494SEnrico Perla - Sun Microsystems 	int	flags;
37348847494SEnrico Perla - Sun Microsystems } dirinfo_t;
37448847494SEnrico Perla - Sun Microsystems 
37548847494SEnrico Perla - Sun Microsystems /*
37648847494SEnrico Perla - Sun Microsystems  * Update flags:
37748847494SEnrico Perla - Sun Microsystems  * NEED_CACHE_DIR : cache directory is missing and needs to be created
37848847494SEnrico Perla - Sun Microsystems  * IS_SPARC_TARGET : the target mountpoint is a SPARC environment
37948847494SEnrico Perla - Sun Microsystems  * UPDATE_ERROR : an error occourred while traversing the list of files
38048847494SEnrico Perla - Sun Microsystems  * RDONLY_FSCHK : the target filesystem is read-only
38148847494SEnrico Perla - Sun Microsystems  * RAMDSK_FSCHK : the target filesystem is on a ramdisk
38248847494SEnrico Perla - Sun Microsystems  */
38348847494SEnrico Perla - Sun Microsystems #define	NEED_CACHE_DIR		0x00000001
38448847494SEnrico Perla - Sun Microsystems #define	IS_SPARC_TARGET		0x00000002
38548847494SEnrico Perla - Sun Microsystems #define	UPDATE_ERROR		0x00000004
38648847494SEnrico Perla - Sun Microsystems #define	RDONLY_FSCHK		0x00000008
3874a9df875SEnrico Perla - Sun Microsystems #define	INVALIDATE_CACHE	0x00000010
38848847494SEnrico Perla - Sun Microsystems 
38948847494SEnrico Perla - Sun Microsystems #define	is_flag_on(flag)	(walk_arg.update_flags & flag ? 1 : 0)
39048847494SEnrico Perla - Sun Microsystems #define	set_flag(flag)		(walk_arg.update_flags |= flag)
39148847494SEnrico Perla - Sun Microsystems #define	unset_flag(flag)	(walk_arg.update_flags &= ~flag)
39248847494SEnrico Perla - Sun Microsystems 
39348847494SEnrico Perla - Sun Microsystems /*
39448847494SEnrico Perla - Sun Microsystems  * struct walk_arg :
39548847494SEnrico Perla - Sun Microsystems  * update_flags: flags related to the current updating process
39648847494SEnrico Perla - Sun Microsystems  * new_nvlp/old_nvlp: new and old list of archive-files / attributes pairs
39748847494SEnrico Perla - Sun Microsystems  * sparcfile: list of file paths for mkisofs -path-list (SPARC only)
39848847494SEnrico Perla - Sun Microsystems  */
3997c478bd9Sstevel@tonic-gate static struct {
40048847494SEnrico Perla - Sun Microsystems 	int 		update_flags;
4017c478bd9Sstevel@tonic-gate 	nvlist_t 	*new_nvlp;
4027c478bd9Sstevel@tonic-gate 	nvlist_t 	*old_nvlp;
40348847494SEnrico Perla - Sun Microsystems 	FILE 		*sparcfile;
40448847494SEnrico Perla - Sun Microsystems 	dirinfo_t	dirinfo[CACHEDIR_NUM];
4057c478bd9Sstevel@tonic-gate } walk_arg;
4067c478bd9Sstevel@tonic-gate 
4079632cfadSsetje struct safefile {
40858091fd8Ssetje 	char *name;
40958091fd8Ssetje 	struct safefile *next;
41058091fd8Ssetje };
41158091fd8Ssetje 
412ae115bc7Smrj static struct safefile *safefiles = NULL;
4133b133becSGangadhar Mylapuram 
4143b133becSGangadhar Mylapuram /*
4153b133becSGangadhar Mylapuram  * svc:/system/filesystem/usr:default service checks for this file and
4163b133becSGangadhar Mylapuram  * does a boot archive update and then reboot the system.
4173b133becSGangadhar Mylapuram  */
41858091fd8Ssetje #define	NEED_UPDATE_FILE "/etc/svc/volatile/boot_archive_needs_update"
41958091fd8Ssetje 
4203b133becSGangadhar Mylapuram /*
4213b133becSGangadhar Mylapuram  * svc:/system/boot-archive-update:default checks for this file and
4223b133becSGangadhar Mylapuram  * updates the boot archive.
4233b133becSGangadhar Mylapuram  */
4243b133becSGangadhar Mylapuram #define	NEED_UPDATE_SAFE_FILE "/etc/svc/volatile/boot_archive_safefile_update"
4253b133becSGangadhar Mylapuram 
42648847494SEnrico Perla - Sun Microsystems /* Thanks growisofs */
42748847494SEnrico Perla - Sun Microsystems #define	CD_BLOCK	((off64_t)2048)
42848847494SEnrico Perla - Sun Microsystems #define	VOLDESC_OFF	16
42948847494SEnrico Perla - Sun Microsystems #define	DVD_BLOCK	(32*1024)
43048847494SEnrico Perla - Sun Microsystems #define	MAX_IVDs	16
43148847494SEnrico Perla - Sun Microsystems 
43248847494SEnrico Perla - Sun Microsystems struct iso_pdesc {
43348847494SEnrico Perla - Sun Microsystems     unsigned char type	[1];
43448847494SEnrico Perla - Sun Microsystems     unsigned char id	[5];
43548847494SEnrico Perla - Sun Microsystems     unsigned char void1	[80-5-1];
43648847494SEnrico Perla - Sun Microsystems     unsigned char volume_space_size [8];
43748847494SEnrico Perla - Sun Microsystems     unsigned char void2	[2048-80-8];
43848847494SEnrico Perla - Sun Microsystems };
43948847494SEnrico Perla - Sun Microsystems 
44048847494SEnrico Perla - Sun Microsystems /*
44148847494SEnrico Perla - Sun Microsystems  * COUNT_MAX:	maximum number of changed files to justify a multisession update
44248847494SEnrico Perla - Sun Microsystems  * BA_SIZE_MAX:	maximum size of the boot_archive to justify a multisession
44348847494SEnrico Perla - Sun Microsystems  * 		update
44448847494SEnrico Perla - Sun Microsystems  */
44548847494SEnrico Perla - Sun Microsystems #define	COUNT_MAX		50
44648847494SEnrico Perla - Sun Microsystems #define	BA_SIZE_MAX		(50 * 1024 * 1024)
44748847494SEnrico Perla - Sun Microsystems 
44848847494SEnrico Perla - Sun Microsystems #define	bam_nowrite()		(bam_check || bam_smf_check)
44948847494SEnrico Perla - Sun Microsystems 
45019397407SSherry Moore static int sync_menu = 1;	/* whether we need to sync the BE menus */
45119397407SSherry Moore 
4527c478bd9Sstevel@tonic-gate static void
4537c478bd9Sstevel@tonic-gate usage(void)
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "USAGE:\n");
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	/* archive usage */
458d876c67dSjg 	(void) fprintf(stderr,
459c365cc98SToomas Soome 	    "\t%s update-archive [-vn] [-R altroot [-p platform]]\n", prog);
460d876c67dSjg 	(void) fprintf(stderr,
461c365cc98SToomas Soome 	    "\t%s list-archive [-R altroot [-p platform]]\n", prog);
46279c28b70SToomas Soome #if defined(_OBP)
463c7c0ceafSToomas Soome 	(void) fprintf(stderr,
464c7c0ceafSToomas Soome 	    "\t%s install-bootloader [-fv] [-R altroot] [-P pool]\n", prog);
465c7c0ceafSToomas Soome #else
466c7c0ceafSToomas Soome 	(void) fprintf(stderr,
467c7c0ceafSToomas Soome 	    "\t%s install-bootloader [-Mfv] [-R altroot] [-P pool]\n", prog);
468c7c0ceafSToomas Soome #endif
46979c28b70SToomas Soome #if !defined(_OBP)
4707c478bd9Sstevel@tonic-gate 	/* x86 only */
4717c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog);
4727c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog);
4737c478bd9Sstevel@tonic-gate #endif
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
476cedc7e57SEnrico Perla - Sun Microsystems /*
477cedc7e57SEnrico Perla - Sun Microsystems  * Best effort attempt to restore the $HOME value.
478cedc7e57SEnrico Perla - Sun Microsystems  */
479cedc7e57SEnrico Perla - Sun Microsystems static void
480cedc7e57SEnrico Perla - Sun Microsystems restore_env()
481cedc7e57SEnrico Perla - Sun Microsystems {
482cedc7e57SEnrico Perla - Sun Microsystems 	char	home_env[PATH_MAX];
483cedc7e57SEnrico Perla - Sun Microsystems 
484cedc7e57SEnrico Perla - Sun Microsystems 	if (bam_home_env) {
485cedc7e57SEnrico Perla - Sun Microsystems 		(void) snprintf(home_env, sizeof (home_env), "HOME=%s",
486cedc7e57SEnrico Perla - Sun Microsystems 		    bam_home_env);
487cedc7e57SEnrico Perla - Sun Microsystems 		(void) putenv(home_env);
488cedc7e57SEnrico Perla - Sun Microsystems 	}
489cedc7e57SEnrico Perla - Sun Microsystems }
490cedc7e57SEnrico Perla - Sun Microsystems 
491cedc7e57SEnrico Perla - Sun Microsystems 
492cedc7e57SEnrico Perla - Sun Microsystems #define		SLEEP_TIME	5
493cedc7e57SEnrico Perla - Sun Microsystems #define		MAX_TRIES	4
494cedc7e57SEnrico Perla - Sun Microsystems 
495cedc7e57SEnrico Perla - Sun Microsystems /*
496cedc7e57SEnrico Perla - Sun Microsystems  * Sanitize the environment in which bootadm will execute its sub-processes
497cedc7e57SEnrico Perla - Sun Microsystems  * (ex. mkisofs). This is done to prevent those processes from attempting
498cedc7e57SEnrico Perla - Sun Microsystems  * to access files (ex. .mkisofsrc) or stat paths that might be on NFS
499cedc7e57SEnrico Perla - Sun Microsystems  * or, potentially, insecure.
500cedc7e57SEnrico Perla - Sun Microsystems  */
501cedc7e57SEnrico Perla - Sun Microsystems static void
502cedc7e57SEnrico Perla - Sun Microsystems sanitize_env()
503cedc7e57SEnrico Perla - Sun Microsystems {
504cedc7e57SEnrico Perla - Sun Microsystems 	int	stry = 0;
505cedc7e57SEnrico Perla - Sun Microsystems 
506cedc7e57SEnrico Perla - Sun Microsystems 	/* don't depend on caller umask */
507cedc7e57SEnrico Perla - Sun Microsystems 	(void) umask(0022);
508cedc7e57SEnrico Perla - Sun Microsystems 
509cedc7e57SEnrico Perla - Sun Microsystems 	/* move away from a potential unsafe current working directory */
510cedc7e57SEnrico Perla - Sun Microsystems 	while (chdir("/") == -1) {
511cedc7e57SEnrico Perla - Sun Microsystems 		if (errno != EINTR) {
512cedc7e57SEnrico Perla - Sun Microsystems 			bam_print("WARNING: unable to chdir to /");
513cedc7e57SEnrico Perla - Sun Microsystems 			break;
514cedc7e57SEnrico Perla - Sun Microsystems 		}
515cedc7e57SEnrico Perla - Sun Microsystems 	}
516cedc7e57SEnrico Perla - Sun Microsystems 
517cedc7e57SEnrico Perla - Sun Microsystems 	bam_home_env = getenv("HOME");
518cedc7e57SEnrico Perla - Sun Microsystems 	while (bam_home_env != NULL && putenv("HOME=/") == -1) {
519cedc7e57SEnrico Perla - Sun Microsystems 		if (errno == ENOMEM) {
520cedc7e57SEnrico Perla - Sun Microsystems 			/* retry no more than MAX_TRIES times */
521cedc7e57SEnrico Perla - Sun Microsystems 			if (++stry > MAX_TRIES) {
522cedc7e57SEnrico Perla - Sun Microsystems 				bam_print("WARNING: unable to recover from "
523cedc7e57SEnrico Perla - Sun Microsystems 				    "system memory pressure... aborting \n");
524cedc7e57SEnrico Perla - Sun Microsystems 				bam_exit(EXIT_FAILURE);
525cedc7e57SEnrico Perla - Sun Microsystems 			}
526cedc7e57SEnrico Perla - Sun Microsystems 			/* memory is tight, try to sleep */
527cedc7e57SEnrico Perla - Sun Microsystems 			bam_print("Attempting to recover from memory pressure: "
528cedc7e57SEnrico Perla - Sun Microsystems 			    "sleeping for %d seconds\n", SLEEP_TIME * stry);
529cedc7e57SEnrico Perla - Sun Microsystems 			(void) sleep(SLEEP_TIME * stry);
530cedc7e57SEnrico Perla - Sun Microsystems 		} else {
531cedc7e57SEnrico Perla - Sun Microsystems 			bam_print("WARNING: unable to sanitize HOME\n");
532cedc7e57SEnrico Perla - Sun Microsystems 		}
533cedc7e57SEnrico Perla - Sun Microsystems 	}
534cedc7e57SEnrico Perla - Sun Microsystems }
535cedc7e57SEnrico Perla - Sun Microsystems 
5367c478bd9Sstevel@tonic-gate int
5377c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate 	error_t ret;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
5427c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
5457c478bd9Sstevel@tonic-gate 		prog = argv[0];
5467c478bd9Sstevel@tonic-gate 	} else {
5477c478bd9Sstevel@tonic-gate 		prog++;
5487c478bd9Sstevel@tonic-gate 	}
5497c478bd9Sstevel@tonic-gate 
550eb2bd662Svikram 	INJECT_ERROR1("ASSERT_ON", assert(0))
5517c478bd9Sstevel@tonic-gate 
552cedc7e57SEnrico Perla - Sun Microsystems 	sanitize_env();
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	switch (bam_cmd) {
5577c478bd9Sstevel@tonic-gate 		case BAM_MENU:
5587c478bd9Sstevel@tonic-gate 			ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv);
5597c478bd9Sstevel@tonic-gate 			break;
5607c478bd9Sstevel@tonic-gate 		case BAM_ARCHIVE:
5617c478bd9Sstevel@tonic-gate 			ret = bam_archive(bam_subcmd, bam_opt);
5627c478bd9Sstevel@tonic-gate 			break;
563c7c0ceafSToomas Soome 		case BAM_INSTALL:
564c7c0ceafSToomas Soome 			ret = bam_install(bam_subcmd, bam_opt);
565c7c0ceafSToomas Soome 			break;
5667c478bd9Sstevel@tonic-gate 		default:
5677c478bd9Sstevel@tonic-gate 			usage();
5687c478bd9Sstevel@tonic-gate 			bam_exit(1);
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS)
57244da779fSWilliam Kucharski 		bam_exit((ret == BAM_NOCHANGE) ? 2 : 1);
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	bam_unlock();
5757c478bd9Sstevel@tonic-gate 	return (0);
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate /*
5797c478bd9Sstevel@tonic-gate  * Equivalence of public and internal commands:
5807c478bd9Sstevel@tonic-gate  *	update-archive  -- -a update
5817c478bd9Sstevel@tonic-gate  *	list-archive	-- -a list
5827c478bd9Sstevel@tonic-gate  *	set-menu	-- -m set_option
5837c478bd9Sstevel@tonic-gate  *	list-menu	-- -m list_entry
5847c478bd9Sstevel@tonic-gate  *	update-menu	-- -m update_entry
585c7c0ceafSToomas Soome  *	install-bootloader	-- -i install_bootloader
5867c478bd9Sstevel@tonic-gate  */
5877c478bd9Sstevel@tonic-gate static struct cmd_map {
5887c478bd9Sstevel@tonic-gate 	char *bam_cmdname;
5897c478bd9Sstevel@tonic-gate 	int bam_cmd;
5907c478bd9Sstevel@tonic-gate 	char *bam_subcmd;
5917c478bd9Sstevel@tonic-gate } cmd_map[] = {
5927c478bd9Sstevel@tonic-gate 	{ "update-archive",	BAM_ARCHIVE,	"update"},
5937c478bd9Sstevel@tonic-gate 	{ "list-archive",	BAM_ARCHIVE,	"list"},
5947c478bd9Sstevel@tonic-gate 	{ "set-menu",		BAM_MENU,	"set_option"},
5957c478bd9Sstevel@tonic-gate 	{ "list-menu",		BAM_MENU,	"list_entry"},
5967c478bd9Sstevel@tonic-gate 	{ "update-menu",	BAM_MENU,	"update_entry"},
597c7c0ceafSToomas Soome 	{ "install-bootloader",	BAM_INSTALL,	"install_bootloader"},
5987c478bd9Sstevel@tonic-gate 	{ NULL,			0,		NULL}
5997c478bd9Sstevel@tonic-gate };
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate /*
6027c478bd9Sstevel@tonic-gate  * Commands syntax published in bootadm(1M) are parsed here
6037c478bd9Sstevel@tonic-gate  */
6047c478bd9Sstevel@tonic-gate static void
6057c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[])
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	struct cmd_map *cmp = cmd_map;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	/* command conforming to the final spec */
6107c478bd9Sstevel@tonic-gate 	if (argc > 1 && argv[1][0] != '-') {
6117c478bd9Sstevel@tonic-gate 		/*
6127c478bd9Sstevel@tonic-gate 		 * Map commands to internal table.
6137c478bd9Sstevel@tonic-gate 		 */
6147c478bd9Sstevel@tonic-gate 		while (cmp->bam_cmdname) {
6157c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], cmp->bam_cmdname) == 0) {
6167c478bd9Sstevel@tonic-gate 				bam_cmd = cmp->bam_cmd;
6177c478bd9Sstevel@tonic-gate 				bam_subcmd = cmp->bam_subcmd;
6187c478bd9Sstevel@tonic-gate 				break;
6197c478bd9Sstevel@tonic-gate 			}
6207c478bd9Sstevel@tonic-gate 			cmp++;
6217c478bd9Sstevel@tonic-gate 		}
6227c478bd9Sstevel@tonic-gate 		if (cmp->bam_cmdname == NULL) {
6237c478bd9Sstevel@tonic-gate 			usage();
6247c478bd9Sstevel@tonic-gate 			bam_exit(1);
6257c478bd9Sstevel@tonic-gate 		}
6267c478bd9Sstevel@tonic-gate 		argc--;
6277c478bd9Sstevel@tonic-gate 		argv++;
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	parse_args_internal(argc, argv);
6317c478bd9Sstevel@tonic-gate }
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate /*
6347c478bd9Sstevel@tonic-gate  * A combination of public and private commands are parsed here.
6357c478bd9Sstevel@tonic-gate  * The internal syntax and the corresponding functionality are:
6367c478bd9Sstevel@tonic-gate  *	-a update			-- update-archive
6377c478bd9Sstevel@tonic-gate  *	-a list				-- list-archive
63844da779fSWilliam Kucharski  *	-a update-all			-- (reboot to sync all mnted OS archive)
639c7c0ceafSToomas Soome  *	-i install_bootloader		-- install-bootloader
6407c478bd9Sstevel@tonic-gate  *	-m update_entry			-- update-menu
6417c478bd9Sstevel@tonic-gate  *	-m list_entry			-- list-menu
6427c478bd9Sstevel@tonic-gate  *	-m update_temp			-- (reboot -- [boot-args])
6437c478bd9Sstevel@tonic-gate  *	-m delete_all_entries		-- (called from install)
64444da779fSWilliam Kucharski  *	-m enable_hypervisor [args]	-- cvt_to_hyper
64544da779fSWilliam Kucharski  *	-m disable_hypervisor		-- cvt_to_metal
64644da779fSWilliam Kucharski  *	-m list_setting [entry] [value]	-- list_setting
64744da779fSWilliam Kucharski  *
64848847494SEnrico Perla - Sun Microsystems  * A set of private flags is there too:
64948847494SEnrico Perla - Sun Microsystems  *	-F		-- purge the cache directories and rebuild them
65048847494SEnrico Perla - Sun Microsystems  *	-e		-- use the (faster) archive update approach (used by
65148847494SEnrico Perla - Sun Microsystems  *			   reboot)
6527c478bd9Sstevel@tonic-gate  */
6537c478bd9Sstevel@tonic-gate static void
6547c478bd9Sstevel@tonic-gate parse_args_internal(int argc, char *argv[])
6557c478bd9Sstevel@tonic-gate {
6567c478bd9Sstevel@tonic-gate 	int c, error;
6577c478bd9Sstevel@tonic-gate 	extern char *optarg;
6587c478bd9Sstevel@tonic-gate 	extern int optind, opterr;
65979c28b70SToomas Soome #if defined(_OBP)
660c7c0ceafSToomas Soome 	const char *optstring = "a:d:fi:m:no:veFCR:p:P:XZ";
661c7c0ceafSToomas Soome #else
662c7c0ceafSToomas Soome 	const char *optstring = "a:d:fi:m:no:veFCMR:p:P:XZ";
663c7c0ceafSToomas Soome #endif
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	/* Suppress error message from getopt */
6667c478bd9Sstevel@tonic-gate 	opterr = 0;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	error = 0;
669c7c0ceafSToomas Soome 	while ((c = getopt(argc, argv, optstring)) != -1) {
6707c478bd9Sstevel@tonic-gate 		switch (c) {
6717c478bd9Sstevel@tonic-gate 		case 'a':
6727c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
6737c478bd9Sstevel@tonic-gate 				error = 1;
6747c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
6757c478bd9Sstevel@tonic-gate 			}
6767c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_ARCHIVE;
6777c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
6787c478bd9Sstevel@tonic-gate 			break;
6797c478bd9Sstevel@tonic-gate 		case 'd':
6807c478bd9Sstevel@tonic-gate 			if (bam_debug) {
6817c478bd9Sstevel@tonic-gate 				error = 1;
6827c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
6837c478bd9Sstevel@tonic-gate 			}
6847c478bd9Sstevel@tonic-gate 			bam_debug = s_strtol(optarg);
6857c478bd9Sstevel@tonic-gate 			break;
6867c478bd9Sstevel@tonic-gate 		case 'f':
6877c478bd9Sstevel@tonic-gate 			bam_force = 1;
6887c478bd9Sstevel@tonic-gate 			break;
68948847494SEnrico Perla - Sun Microsystems 		case 'F':
69048847494SEnrico Perla - Sun Microsystems 			bam_purge = 1;
69148847494SEnrico Perla - Sun Microsystems 			break;
692c7c0ceafSToomas Soome 		case 'i':
693c7c0ceafSToomas Soome 			if (bam_cmd) {
694c7c0ceafSToomas Soome 				error = 1;
695c7c0ceafSToomas Soome 				bam_error(MULT_CMDS, c);
696c7c0ceafSToomas Soome 			}
697c7c0ceafSToomas Soome 			bam_cmd = BAM_INSTALL;
698c7c0ceafSToomas Soome 			bam_subcmd = optarg;
699c7c0ceafSToomas Soome 			break;
7007c478bd9Sstevel@tonic-gate 		case 'm':
7017c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
7027c478bd9Sstevel@tonic-gate 				error = 1;
7037c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
7047c478bd9Sstevel@tonic-gate 			}
7057c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_MENU;
7067c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
7077c478bd9Sstevel@tonic-gate 			break;
70879c28b70SToomas Soome #if !defined(_OBP)
709c7c0ceafSToomas Soome 		case 'M':
710c7c0ceafSToomas Soome 			bam_mbr = 1;
711c7c0ceafSToomas Soome 			break;
712c7c0ceafSToomas Soome #endif
7137c478bd9Sstevel@tonic-gate 		case 'n':
7147c478bd9Sstevel@tonic-gate 			bam_check = 1;
7155eea6091SEnrico Perla - Sun Microsystems 			/*
7165eea6091SEnrico Perla - Sun Microsystems 			 * We save the original value of bam_check. The new
7175eea6091SEnrico Perla - Sun Microsystems 			 * approach in case of a read-only filesystem is to
7185eea6091SEnrico Perla - Sun Microsystems 			 * behave as a check, so we need a way to restore the
7195eea6091SEnrico Perla - Sun Microsystems 			 * original value after the evaluation of the read-only
7205eea6091SEnrico Perla - Sun Microsystems 			 * filesystem has been done.
7215eea6091SEnrico Perla - Sun Microsystems 			 * Even if we don't allow at the moment a check with
7225eea6091SEnrico Perla - Sun Microsystems 			 * update_all, this approach is more robust than
7235eea6091SEnrico Perla - Sun Microsystems 			 * simply resetting bam_check to zero.
7245eea6091SEnrico Perla - Sun Microsystems 			 */
7255eea6091SEnrico Perla - Sun Microsystems 			bam_saved_check = 1;
7267c478bd9Sstevel@tonic-gate 			break;
7277c478bd9Sstevel@tonic-gate 		case 'o':
7287c478bd9Sstevel@tonic-gate 			if (bam_opt) {
7297c478bd9Sstevel@tonic-gate 				error = 1;
7307c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
7317c478bd9Sstevel@tonic-gate 			}
7327c478bd9Sstevel@tonic-gate 			bam_opt = optarg;
7337c478bd9Sstevel@tonic-gate 			break;
7347c478bd9Sstevel@tonic-gate 		case 'v':
7357c478bd9Sstevel@tonic-gate 			bam_verbose = 1;
7367c478bd9Sstevel@tonic-gate 			break;
7378c1b6884Sszhou 		case 'C':
7388c1b6884Sszhou 			bam_smf_check = 1;
7398c1b6884Sszhou 			break;
740c7c0ceafSToomas Soome 		case 'P':
741c7c0ceafSToomas Soome 			if (bam_pool != NULL) {
742c7c0ceafSToomas Soome 				error = 1;
743c7c0ceafSToomas Soome 				bam_error(DUP_OPT, c);
744c7c0ceafSToomas Soome 			}
745c7c0ceafSToomas Soome 			bam_pool = optarg;
746c7c0ceafSToomas Soome 			break;
7477c478bd9Sstevel@tonic-gate 		case 'R':
7487c478bd9Sstevel@tonic-gate 			if (bam_root) {
7497c478bd9Sstevel@tonic-gate 				error = 1;
7507c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
7517c478bd9Sstevel@tonic-gate 				break;
7527c478bd9Sstevel@tonic-gate 			} else if (realpath(optarg, rootbuf) == NULL) {
7537c478bd9Sstevel@tonic-gate 				error = 1;
7547c478bd9Sstevel@tonic-gate 				bam_error(CANT_RESOLVE, optarg,
7557c478bd9Sstevel@tonic-gate 				    strerror(errno));
7567c478bd9Sstevel@tonic-gate 				break;
7577c478bd9Sstevel@tonic-gate 			}
75840541d5dSvikram 			bam_alt_root = 1;
7597c478bd9Sstevel@tonic-gate 			bam_root = rootbuf;
7607c478bd9Sstevel@tonic-gate 			bam_rootlen = strlen(rootbuf);
7617c478bd9Sstevel@tonic-gate 			break;
762d876c67dSjg 		case 'p':
763d876c67dSjg 			bam_alt_platform = 1;
764d876c67dSjg 			bam_platform = optarg;
765d876c67dSjg 			if ((strcmp(bam_platform, "i86pc") != 0) &&
766d876c67dSjg 			    (strcmp(bam_platform, "sun4u") != 0) &&
767d876c67dSjg 			    (strcmp(bam_platform, "sun4v") != 0)) {
768d876c67dSjg 				error = 1;
769d876c67dSjg 				bam_error(INVALID_PLAT, bam_platform);
770d876c67dSjg 			}
771d876c67dSjg 			break;
7723028dfd6SFrank Van Der Linden 		case 'X':
7733028dfd6SFrank Van Der Linden 			bam_is_hv = BAM_HV_PRESENT;
7743028dfd6SFrank Van Der Linden 			break;
775e7cbe64fSgw25295 		case 'Z':
776e7cbe64fSgw25295 			bam_zfs = 1;
777e7cbe64fSgw25295 			break;
77848847494SEnrico Perla - Sun Microsystems 		case 'e':
77948847494SEnrico Perla - Sun Microsystems 			bam_extend = 1;
78048847494SEnrico Perla - Sun Microsystems 			break;
7817c478bd9Sstevel@tonic-gate 		case '?':
7827c478bd9Sstevel@tonic-gate 			error = 1;
7837c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, optopt);
7847c478bd9Sstevel@tonic-gate 			break;
7857c478bd9Sstevel@tonic-gate 		default :
7867c478bd9Sstevel@tonic-gate 			error = 1;
7877c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, c);
7887c478bd9Sstevel@tonic-gate 			break;
7897c478bd9Sstevel@tonic-gate 		}
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	/*
793d876c67dSjg 	 * An alternate platform requires an alternate root
794d876c67dSjg 	 */
795d876c67dSjg 	if (bam_alt_platform && bam_alt_root == 0) {
796d876c67dSjg 		usage();
797d876c67dSjg 		bam_exit(0);
798d876c67dSjg 	}
799d876c67dSjg 
800d876c67dSjg 	/*
8017c478bd9Sstevel@tonic-gate 	 * A command option must be specfied
8027c478bd9Sstevel@tonic-gate 	 */
8037c478bd9Sstevel@tonic-gate 	if (!bam_cmd) {
8047c478bd9Sstevel@tonic-gate 		if (bam_opt && strcmp(bam_opt, "all") == 0) {
8057c478bd9Sstevel@tonic-gate 			usage();
8067c478bd9Sstevel@tonic-gate 			bam_exit(0);
8077c478bd9Sstevel@tonic-gate 		}
8087c478bd9Sstevel@tonic-gate 		bam_error(NEED_CMD);
8097c478bd9Sstevel@tonic-gate 		error = 1;
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	if (error) {
8137c478bd9Sstevel@tonic-gate 		usage();
8147c478bd9Sstevel@tonic-gate 		bam_exit(1);
8157c478bd9Sstevel@tonic-gate 	}
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	if (optind > argc) {
8187c478bd9Sstevel@tonic-gate 		bam_error(INT_ERROR, "parse_args");
8197c478bd9Sstevel@tonic-gate 		bam_exit(1);
8207c478bd9Sstevel@tonic-gate 	} else if (optind < argc) {
8217c478bd9Sstevel@tonic-gate 		bam_argv = &argv[optind];
8227c478bd9Sstevel@tonic-gate 		bam_argc = argc - optind;
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	/*
826c7c0ceafSToomas Soome 	 * mbr and pool are options for install_bootloader
827c7c0ceafSToomas Soome 	 */
828c7c0ceafSToomas Soome 	if (bam_cmd != BAM_INSTALL && (bam_mbr || bam_pool != NULL)) {
829c7c0ceafSToomas Soome 		usage();
830c7c0ceafSToomas Soome 		bam_exit(0);
831c7c0ceafSToomas Soome 	}
832c7c0ceafSToomas Soome 
833c7c0ceafSToomas Soome 	/*
8347c478bd9Sstevel@tonic-gate 	 * -n implies verbose mode
8357c478bd9Sstevel@tonic-gate 	 */
8367c478bd9Sstevel@tonic-gate 	if (bam_check)
8377c478bd9Sstevel@tonic-gate 		bam_verbose = 1;
8387c478bd9Sstevel@tonic-gate }
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate static error_t
8417c478bd9Sstevel@tonic-gate check_subcmd_and_options(
8427c478bd9Sstevel@tonic-gate 	char *subcmd,
8437c478bd9Sstevel@tonic-gate 	char *opt,
8447c478bd9Sstevel@tonic-gate 	subcmd_defn_t *table,
8457c478bd9Sstevel@tonic-gate 	error_t (**fp)())
8467c478bd9Sstevel@tonic-gate {
8477c478bd9Sstevel@tonic-gate 	int i;
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	if (subcmd == NULL) {
8507c478bd9Sstevel@tonic-gate 		bam_error(NEED_SUBCMD);
8517c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
8527c478bd9Sstevel@tonic-gate 	}
8537c478bd9Sstevel@tonic-gate 
854eb2bd662Svikram 	if (strcmp(subcmd, "set_option") == 0) {
855eb2bd662Svikram 		if (bam_argc == 0 || bam_argv == NULL || bam_argv[0] == NULL) {
856eb2bd662Svikram 			bam_error(MISSING_ARG);
857eb2bd662Svikram 			usage();
858eb2bd662Svikram 			return (BAM_ERROR);
859eb2bd662Svikram 		} else if (bam_argc > 1 || bam_argv[1] != NULL) {
8601a97e40eSvikram 			bam_error(TRAILING_ARGS);
8611a97e40eSvikram 			usage();
8621a97e40eSvikram 			return (BAM_ERROR);
8631a97e40eSvikram 		}
86419397407SSherry Moore 	} else if (strcmp(subcmd, "update_all") == 0) {
86519397407SSherry Moore 		/*
86619397407SSherry Moore 		 * The only option we accept for the "update_all"
86719397407SSherry Moore 		 * subcmd is "fastboot".
86819397407SSherry Moore 		 */
86919397407SSherry Moore 		if (bam_argc > 1 || (bam_argc == 1 &&
87019397407SSherry Moore 		    strcmp(bam_argv[0], "fastboot") != 0)) {
87119397407SSherry Moore 			bam_error(TRAILING_ARGS);
87219397407SSherry Moore 			usage();
87319397407SSherry Moore 			return (BAM_ERROR);
87419397407SSherry Moore 		}
87519397407SSherry Moore 		if (bam_argc == 1)
87619397407SSherry Moore 			sync_menu = 0;
87744da779fSWilliam Kucharski 	} else if (((strcmp(subcmd, "enable_hypervisor") != 0) &&
87844da779fSWilliam Kucharski 	    (strcmp(subcmd, "list_setting") != 0)) && (bam_argc || bam_argv)) {
87944da779fSWilliam Kucharski 		/*
88044da779fSWilliam Kucharski 		 * Of the remaining subcommands, only "enable_hypervisor" and
88144da779fSWilliam Kucharski 		 * "list_setting" take trailing arguments.
88244da779fSWilliam Kucharski 		 */
883eb2bd662Svikram 		bam_error(TRAILING_ARGS);
884eb2bd662Svikram 		usage();
885eb2bd662Svikram 		return (BAM_ERROR);
8861a97e40eSvikram 	}
8871a97e40eSvikram 
8887c478bd9Sstevel@tonic-gate 	if (bam_root == NULL) {
8897c478bd9Sstevel@tonic-gate 		bam_root = rootbuf;
8907c478bd9Sstevel@tonic-gate 		bam_rootlen = 1;
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	/* verify that subcmd is valid */
8947c478bd9Sstevel@tonic-gate 	for (i = 0; table[i].subcmd != NULL; i++) {
8957c478bd9Sstevel@tonic-gate 		if (strcmp(table[i].subcmd, subcmd) == 0)
8967c478bd9Sstevel@tonic-gate 			break;
8977c478bd9Sstevel@tonic-gate 	}
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	if (table[i].subcmd == NULL) {
9007c478bd9Sstevel@tonic-gate 		bam_error(INVALID_SUBCMD, subcmd);
9017c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 
9041a97e40eSvikram 	if (table[i].unpriv == 0 && geteuid() != 0) {
9051a97e40eSvikram 		bam_error(MUST_BE_ROOT);
9061a97e40eSvikram 		return (BAM_ERROR);
9071a97e40eSvikram 	}
9081a97e40eSvikram 
9091a97e40eSvikram 	/*
9101a97e40eSvikram 	 * Currently only privileged commands need a lock
9111a97e40eSvikram 	 */
9121a97e40eSvikram 	if (table[i].unpriv == 0)
9131a97e40eSvikram 		bam_lock();
9141a97e40eSvikram 
9157c478bd9Sstevel@tonic-gate 	/* subcmd verifies that opt is appropriate */
9167c478bd9Sstevel@tonic-gate 	if (table[i].option != OPT_OPTIONAL) {
9177c478bd9Sstevel@tonic-gate 		if ((table[i].option == OPT_REQ) ^ (opt != NULL)) {
9187c478bd9Sstevel@tonic-gate 			if (opt)
9197c478bd9Sstevel@tonic-gate 				bam_error(NO_OPT_REQ, subcmd);
9207c478bd9Sstevel@tonic-gate 			else
9217c478bd9Sstevel@tonic-gate 				bam_error(MISS_OPT, subcmd);
9227c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
9237c478bd9Sstevel@tonic-gate 		}
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	*fp = table[i].handler;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
9297c478bd9Sstevel@tonic-gate }
9307c478bd9Sstevel@tonic-gate 
93140541d5dSvikram /*
93240541d5dSvikram  * NOTE: A single "/" is also considered a trailing slash and will
93340541d5dSvikram  * be deleted.
93440541d5dSvikram  */
93540541d5dSvikram static void
93640541d5dSvikram elide_trailing_slash(const char *src, char *dst, size_t dstsize)
93740541d5dSvikram {
93840541d5dSvikram 	size_t dstlen;
93940541d5dSvikram 
94040541d5dSvikram 	assert(src);
94140541d5dSvikram 	assert(dst);
94240541d5dSvikram 
94340541d5dSvikram 	(void) strlcpy(dst, src, dstsize);
94440541d5dSvikram 
94540541d5dSvikram 	dstlen = strlen(dst);
94640541d5dSvikram 	if (dst[dstlen - 1] == '/') {
94740541d5dSvikram 		dst[dstlen - 1] = '\0';
94840541d5dSvikram 	}
94940541d5dSvikram }
95040541d5dSvikram 
951cedc7e57SEnrico Perla - Sun Microsystems static int
952cedc7e57SEnrico Perla - Sun Microsystems is_safe_exec(char *path)
953cedc7e57SEnrico Perla - Sun Microsystems {
954cedc7e57SEnrico Perla - Sun Microsystems 	struct stat	sb;
955cedc7e57SEnrico Perla - Sun Microsystems 
956cedc7e57SEnrico Perla - Sun Microsystems 	if (lstat(path, &sb) != 0) {
957cedc7e57SEnrico Perla - Sun Microsystems 		bam_error(STAT_FAIL, path, strerror(errno));
958cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
959cedc7e57SEnrico Perla - Sun Microsystems 	}
960cedc7e57SEnrico Perla - Sun Microsystems 
961cedc7e57SEnrico Perla - Sun Microsystems 	if (!S_ISREG(sb.st_mode)) {
962cedc7e57SEnrico Perla - Sun Microsystems 		bam_error(PATH_EXEC_LINK, path);
963cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
964cedc7e57SEnrico Perla - Sun Microsystems 	}
965cedc7e57SEnrico Perla - Sun Microsystems 
966cedc7e57SEnrico Perla - Sun Microsystems 	if (sb.st_uid != getuid()) {
967cedc7e57SEnrico Perla - Sun Microsystems 		bam_error(PATH_EXEC_OWNER, path, getuid());
968cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
969cedc7e57SEnrico Perla - Sun Microsystems 	}
970cedc7e57SEnrico Perla - Sun Microsystems 
971cedc7e57SEnrico Perla - Sun Microsystems 	if (sb.st_mode & S_IWOTH || sb.st_mode & S_IWGRP) {
972cedc7e57SEnrico Perla - Sun Microsystems 		bam_error(PATH_EXEC_PERMS, path);
973cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
974cedc7e57SEnrico Perla - Sun Microsystems 	}
975cedc7e57SEnrico Perla - Sun Microsystems 
976cedc7e57SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
977cedc7e57SEnrico Perla - Sun Microsystems }
978cedc7e57SEnrico Perla - Sun Microsystems 
9797c478bd9Sstevel@tonic-gate static error_t
98044da779fSWilliam Kucharski list_setting(menu_t *mp, char *which, char *setting)
98144da779fSWilliam Kucharski {
98244da779fSWilliam Kucharski 	line_t	*lp;
98344da779fSWilliam Kucharski 	entry_t	*ent;
98444da779fSWilliam Kucharski 
98544da779fSWilliam Kucharski 	char	*p = which;
98644da779fSWilliam Kucharski 	int	entry;
98744da779fSWilliam Kucharski 
98844da779fSWilliam Kucharski 	int	found;
98944da779fSWilliam Kucharski 
99044da779fSWilliam Kucharski 	assert(which);
99144da779fSWilliam Kucharski 	assert(setting);
99244da779fSWilliam Kucharski 
99344da779fSWilliam Kucharski 	if (*which != NULL) {
99444da779fSWilliam Kucharski 		/*
99544da779fSWilliam Kucharski 		 * If "which" is not a number, assume it's a setting we want
99644da779fSWilliam Kucharski 		 * to look for and so set up the routine to look for "which"
99744da779fSWilliam Kucharski 		 * in the default entry.
99844da779fSWilliam Kucharski 		 */
99944da779fSWilliam Kucharski 		while (*p != NULL)
100044da779fSWilliam Kucharski 			if (!(isdigit((int)*p++))) {
100144da779fSWilliam Kucharski 				setting = which;
100244da779fSWilliam Kucharski 				which = mp->curdefault->arg;
100344da779fSWilliam Kucharski 				break;
100444da779fSWilliam Kucharski 			}
100544da779fSWilliam Kucharski 	} else {
100644da779fSWilliam Kucharski 		which = mp->curdefault->arg;
100744da779fSWilliam Kucharski 	}
100844da779fSWilliam Kucharski 
100944da779fSWilliam Kucharski 	entry = atoi(which);
101044da779fSWilliam Kucharski 
101144da779fSWilliam Kucharski 	for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != entry));
101244da779fSWilliam Kucharski 	    ent = ent->next)
101344da779fSWilliam Kucharski 		;
101444da779fSWilliam Kucharski 
101544da779fSWilliam Kucharski 	if (!ent) {
101644da779fSWilliam Kucharski 		bam_error(NO_MATCH_ENTRY);
101744da779fSWilliam Kucharski 		return (BAM_ERROR);
101844da779fSWilliam Kucharski 	}
101944da779fSWilliam Kucharski 
102044da779fSWilliam Kucharski 	found = (*setting == NULL);
102144da779fSWilliam Kucharski 
102244da779fSWilliam Kucharski 	for (lp = ent->start; lp != NULL; lp = lp->next) {
102344da779fSWilliam Kucharski 		if ((*setting == NULL) && (lp->flags != BAM_COMMENT))
102444da779fSWilliam Kucharski 			bam_print(PRINT, lp->line);
1025eac223ccSWilliam Kucharski 		else if (lp->cmd != NULL && strcmp(setting, lp->cmd) == 0) {
102644da779fSWilliam Kucharski 			bam_print(PRINT, lp->arg);
102744da779fSWilliam Kucharski 			found = 1;
102844da779fSWilliam Kucharski 		}
102944da779fSWilliam Kucharski 
103044da779fSWilliam Kucharski 		if (lp == ent->end)
103144da779fSWilliam Kucharski 			break;
103244da779fSWilliam Kucharski 	}
103344da779fSWilliam Kucharski 
103444da779fSWilliam Kucharski 	if (!found) {
103544da779fSWilliam Kucharski 		bam_error(NO_MATCH_ENTRY);
103644da779fSWilliam Kucharski 		return (BAM_ERROR);
103744da779fSWilliam Kucharski 	}
103844da779fSWilliam Kucharski 
103944da779fSWilliam Kucharski 	return (BAM_SUCCESS);
104044da779fSWilliam Kucharski }
104144da779fSWilliam Kucharski 
104244da779fSWilliam Kucharski static error_t
1043c7c0ceafSToomas Soome install_bootloader(void)
1044c7c0ceafSToomas Soome {
1045c7c0ceafSToomas Soome 	nvlist_t	*nvl;
1046c7c0ceafSToomas Soome 	uint16_t	flags = 0;
1047c7c0ceafSToomas Soome 	int		found = 0;
1048c7c0ceafSToomas Soome 	struct extmnttab mnt;
1049c7c0ceafSToomas Soome 	struct stat	statbuf = {0};
1050c7c0ceafSToomas Soome 	be_node_list_t	*be_nodes, *node;
1051c7c0ceafSToomas Soome 	FILE		*fp;
1052c7c0ceafSToomas Soome 	char		*root_ds = NULL;
1053c7c0ceafSToomas Soome 	int		ret;
1054c7c0ceafSToomas Soome 
1055c7c0ceafSToomas Soome 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
1056c7c0ceafSToomas Soome 		bam_error(_("out of memory\n"));
1057c7c0ceafSToomas Soome 		return (BAM_ERROR);
1058c7c0ceafSToomas Soome 	}
1059c7c0ceafSToomas Soome 
1060c7c0ceafSToomas Soome 	/*
1061c7c0ceafSToomas Soome 	 * if bam_alt_root is set, the stage files are used from alt root.
1062c7c0ceafSToomas Soome 	 * if pool is set, the target devices are pool devices, stage files
1063c7c0ceafSToomas Soome 	 * are read from pool bootfs unless alt root is set.
1064c7c0ceafSToomas Soome 	 *
1065c7c0ceafSToomas Soome 	 * use arguments as targets, stage files are from alt or current root
1066c7c0ceafSToomas Soome 	 * if no arguments and no pool, install on current boot pool.
1067c7c0ceafSToomas Soome 	 */
1068c7c0ceafSToomas Soome 
1069c7c0ceafSToomas Soome 	if (bam_alt_root) {
1070c7c0ceafSToomas Soome 		if (stat(bam_root, &statbuf) != 0) {
1071c7c0ceafSToomas Soome 			bam_error(STAT_FAIL, bam_root, strerror(errno));
1072c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1073c7c0ceafSToomas Soome 			goto done;
1074c7c0ceafSToomas Soome 		}
1075c7c0ceafSToomas Soome 		if ((fp = fopen(MNTTAB, "r")) == NULL) {
1076c7c0ceafSToomas Soome 			bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
1077c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1078c7c0ceafSToomas Soome 			goto done;
1079c7c0ceafSToomas Soome 		}
1080c7c0ceafSToomas Soome 		resetmnttab(fp);
1081c7c0ceafSToomas Soome 		while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
1082c7c0ceafSToomas Soome 			if (mnt.mnt_major == major(statbuf.st_dev) &&
1083c7c0ceafSToomas Soome 			    mnt.mnt_minor == minor(statbuf.st_dev)) {
1084c7c0ceafSToomas Soome 				found = 1;
1085c7c0ceafSToomas Soome 				root_ds = strdup(mnt.mnt_special);
1086c7c0ceafSToomas Soome 				break;
1087c7c0ceafSToomas Soome 			}
1088c7c0ceafSToomas Soome 		}
1089c7c0ceafSToomas Soome 		(void) fclose(fp);
1090c7c0ceafSToomas Soome 
1091c7c0ceafSToomas Soome 		if (found == 0) {
1092c7c0ceafSToomas Soome 			bam_error(NOT_IN_MNTTAB, bam_root);
1093c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1094c7c0ceafSToomas Soome 			goto done;
1095c7c0ceafSToomas Soome 		}
1096c7c0ceafSToomas Soome 		if (root_ds == NULL) {
1097c7c0ceafSToomas Soome 			bam_error(_("out of memory\n"));
1098c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1099c7c0ceafSToomas Soome 			goto done;
1100c7c0ceafSToomas Soome 		}
1101c7c0ceafSToomas Soome 
1102c7c0ceafSToomas Soome 		if (be_list(NULL, &be_nodes) != BE_SUCCESS) {
1103c7c0ceafSToomas Soome 			bam_error(_("No BE's found\n"));
1104c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1105c7c0ceafSToomas Soome 			goto done;
1106c7c0ceafSToomas Soome 		}
1107c7c0ceafSToomas Soome 		for (node = be_nodes; node != NULL; node = node->be_next_node)
1108c7c0ceafSToomas Soome 			if (strcmp(root_ds, node->be_root_ds) == 0)
1109c7c0ceafSToomas Soome 				break;
1110c7c0ceafSToomas Soome 
1111c7c0ceafSToomas Soome 		if (node == NULL)
1112c7c0ceafSToomas Soome 			bam_error(_("BE (%s) does not exist\n"), root_ds);
1113c7c0ceafSToomas Soome 
1114c7c0ceafSToomas Soome 		free(root_ds);
1115c7c0ceafSToomas Soome 		root_ds = NULL;
1116c7c0ceafSToomas Soome 		if (node == NULL) {
1117c7c0ceafSToomas Soome 			be_free_list(be_nodes);
1118c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1119c7c0ceafSToomas Soome 			goto done;
1120c7c0ceafSToomas Soome 		}
1121c7c0ceafSToomas Soome 		ret = nvlist_add_string(nvl, BE_ATTR_ORIG_BE_NAME,
1122c7c0ceafSToomas Soome 		    node->be_node_name);
1123c7c0ceafSToomas Soome 		ret |= nvlist_add_string(nvl, BE_ATTR_ORIG_BE_ROOT,
1124c7c0ceafSToomas Soome 		    node->be_root_ds);
1125c7c0ceafSToomas Soome 		be_free_list(be_nodes);
1126c7c0ceafSToomas Soome 		if (ret) {
1127c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1128c7c0ceafSToomas Soome 			goto done;
1129c7c0ceafSToomas Soome 		}
1130c7c0ceafSToomas Soome 	}
1131c7c0ceafSToomas Soome 
1132c7c0ceafSToomas Soome 	if (bam_force)
1133c7c0ceafSToomas Soome 		flags |= BE_INSTALLBOOT_FLAG_FORCE;
1134c7c0ceafSToomas Soome 	if (bam_mbr)
1135c7c0ceafSToomas Soome 		flags |= BE_INSTALLBOOT_FLAG_MBR;
1136c7c0ceafSToomas Soome 	if (bam_verbose)
1137c7c0ceafSToomas Soome 		flags |= BE_INSTALLBOOT_FLAG_VERBOSE;
1138c7c0ceafSToomas Soome 
1139c7c0ceafSToomas Soome 	if (nvlist_add_uint16(nvl, BE_ATTR_INSTALL_FLAGS, flags) != 0) {
1140c7c0ceafSToomas Soome 		bam_error(_("out of memory\n"));
1141c7c0ceafSToomas Soome 		goto done;
1142c7c0ceafSToomas Soome 	}
1143c7c0ceafSToomas Soome 
1144c7c0ceafSToomas Soome 	/*
1145c7c0ceafSToomas Soome 	 * if altroot was set, we got be name and be root, only need
1146c7c0ceafSToomas Soome 	 * to set pool name as target.
1147c7c0ceafSToomas Soome 	 * if no altroot, need to find be name and root from pool.
1148c7c0ceafSToomas Soome 	 */
1149c7c0ceafSToomas Soome 	if (bam_pool != NULL) {
1150c7c0ceafSToomas Soome 		ret = nvlist_add_string(nvl, BE_ATTR_ORIG_BE_POOL, bam_pool);
1151c7c0ceafSToomas Soome 		if (ret) {
1152c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1153c7c0ceafSToomas Soome 			goto done;
1154c7c0ceafSToomas Soome 		}
1155c7c0ceafSToomas Soome 		if (found) {
1156c7c0ceafSToomas Soome 			ret = be_installboot(nvl);
1157c7c0ceafSToomas Soome 			if (ret)
1158c7c0ceafSToomas Soome 				ret = BAM_ERROR;
1159c7c0ceafSToomas Soome 			goto done;
1160c7c0ceafSToomas Soome 		}
1161c7c0ceafSToomas Soome 	}
1162c7c0ceafSToomas Soome 
1163c7c0ceafSToomas Soome 	if (be_list(NULL, &be_nodes) != BE_SUCCESS) {
1164c7c0ceafSToomas Soome 		bam_error(_("No BE's found\n"));
1165c7c0ceafSToomas Soome 		ret = BAM_ERROR;
1166c7c0ceafSToomas Soome 		goto done;
1167c7c0ceafSToomas Soome 	}
1168c7c0ceafSToomas Soome 
1169c7c0ceafSToomas Soome 	if (bam_pool != NULL) {
1170c7c0ceafSToomas Soome 		/*
1171c7c0ceafSToomas Soome 		 * find active be_node in bam_pool
1172c7c0ceafSToomas Soome 		 */
1173c7c0ceafSToomas Soome 		for (node = be_nodes; node != NULL; node = node->be_next_node) {
1174c7c0ceafSToomas Soome 			if (strcmp(bam_pool, node->be_rpool) != 0)
1175c7c0ceafSToomas Soome 				continue;
1176c7c0ceafSToomas Soome 			if (node->be_active_on_boot)
1177c7c0ceafSToomas Soome 				break;
1178c7c0ceafSToomas Soome 		}
1179c7c0ceafSToomas Soome 		if (node == NULL) {
1180c7c0ceafSToomas Soome 			bam_error(_("No active BE in %s\n"), bam_pool);
1181c7c0ceafSToomas Soome 			be_free_list(be_nodes);
1182c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1183c7c0ceafSToomas Soome 			goto done;
1184c7c0ceafSToomas Soome 		}
1185c7c0ceafSToomas Soome 		ret = nvlist_add_string(nvl, BE_ATTR_ORIG_BE_NAME,
1186c7c0ceafSToomas Soome 		    node->be_node_name);
1187c7c0ceafSToomas Soome 		ret |= nvlist_add_string(nvl, BE_ATTR_ORIG_BE_ROOT,
1188c7c0ceafSToomas Soome 		    node->be_root_ds);
1189c7c0ceafSToomas Soome 		be_free_list(be_nodes);
1190c7c0ceafSToomas Soome 		if (ret) {
1191c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1192c7c0ceafSToomas Soome 			goto done;
1193c7c0ceafSToomas Soome 		}
1194c7c0ceafSToomas Soome 		ret = be_installboot(nvl);
1195c7c0ceafSToomas Soome 		if (ret)
1196c7c0ceafSToomas Soome 			ret = BAM_ERROR;
1197c7c0ceafSToomas Soome 		goto done;
1198c7c0ceafSToomas Soome 	}
1199c7c0ceafSToomas Soome 
1200c7c0ceafSToomas Soome 	/*
1201c7c0ceafSToomas Soome 	 * get dataset for "/" and fill up the args.
1202c7c0ceafSToomas Soome 	 */
1203c7c0ceafSToomas Soome 	if ((fp = fopen(MNTTAB, "r")) == NULL) {
1204c7c0ceafSToomas Soome 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
1205c7c0ceafSToomas Soome 		ret = BAM_ERROR;
1206c7c0ceafSToomas Soome 		be_free_list(be_nodes);
1207c7c0ceafSToomas Soome 		goto done;
1208c7c0ceafSToomas Soome 	}
1209c7c0ceafSToomas Soome 	resetmnttab(fp);
1210c7c0ceafSToomas Soome 	found = 0;
1211c7c0ceafSToomas Soome 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
1212c7c0ceafSToomas Soome 		if (strcmp(mnt.mnt_mountp, "/") == 0) {
1213c7c0ceafSToomas Soome 			found = 1;
1214c7c0ceafSToomas Soome 			root_ds = strdup(mnt.mnt_special);
1215c7c0ceafSToomas Soome 			break;
1216c7c0ceafSToomas Soome 		}
1217c7c0ceafSToomas Soome 	}
1218c7c0ceafSToomas Soome 	(void) fclose(fp);
1219c7c0ceafSToomas Soome 
1220c7c0ceafSToomas Soome 	if (found == 0) {
1221c7c0ceafSToomas Soome 		bam_error(NOT_IN_MNTTAB, "/");
1222c7c0ceafSToomas Soome 		ret = BAM_ERROR;
1223c7c0ceafSToomas Soome 		be_free_list(be_nodes);
1224c7c0ceafSToomas Soome 		goto done;
1225c7c0ceafSToomas Soome 	}
1226c7c0ceafSToomas Soome 	if (root_ds == NULL) {
1227c7c0ceafSToomas Soome 		bam_error(_("out of memory\n"));
1228c7c0ceafSToomas Soome 		ret = BAM_ERROR;
1229c7c0ceafSToomas Soome 		be_free_list(be_nodes);
1230c7c0ceafSToomas Soome 		goto done;
1231c7c0ceafSToomas Soome 	}
1232c7c0ceafSToomas Soome 
1233c7c0ceafSToomas Soome 	for (node = be_nodes; node != NULL; node = node->be_next_node) {
1234c7c0ceafSToomas Soome 		if (strcmp(root_ds, node->be_root_ds) == 0)
1235c7c0ceafSToomas Soome 			break;
1236c7c0ceafSToomas Soome 	}
1237c7c0ceafSToomas Soome 
1238c7c0ceafSToomas Soome 	if (node == NULL) {
1239c7c0ceafSToomas Soome 		bam_error(_("No such BE: %s\n"), root_ds);
1240c7c0ceafSToomas Soome 		free(root_ds);
1241c7c0ceafSToomas Soome 		be_free_list(be_nodes);
1242c7c0ceafSToomas Soome 		ret = BAM_ERROR;
1243c7c0ceafSToomas Soome 		goto done;
1244c7c0ceafSToomas Soome 	}
1245c7c0ceafSToomas Soome 	free(root_ds);
1246c7c0ceafSToomas Soome 
1247c7c0ceafSToomas Soome 	ret = nvlist_add_string(nvl, BE_ATTR_ORIG_BE_NAME, node->be_node_name);
1248c7c0ceafSToomas Soome 	ret |= nvlist_add_string(nvl, BE_ATTR_ORIG_BE_ROOT, node->be_root_ds);
1249c7c0ceafSToomas Soome 	ret |= nvlist_add_string(nvl, BE_ATTR_ORIG_BE_POOL, node->be_rpool);
1250c7c0ceafSToomas Soome 	be_free_list(be_nodes);
1251c7c0ceafSToomas Soome 
1252c7c0ceafSToomas Soome 	if (ret)
1253c7c0ceafSToomas Soome 		ret = BAM_ERROR;
1254c7c0ceafSToomas Soome 	else
1255c7c0ceafSToomas Soome 		ret = be_installboot(nvl) ? BAM_ERROR : 0;
1256c7c0ceafSToomas Soome done:
1257c7c0ceafSToomas Soome 	nvlist_free(nvl);
1258c7c0ceafSToomas Soome 
1259c7c0ceafSToomas Soome 	return (ret);
1260c7c0ceafSToomas Soome }
1261c7c0ceafSToomas Soome 
1262c7c0ceafSToomas Soome static error_t
1263c7c0ceafSToomas Soome bam_install(char *subcmd, char *opt)
1264c7c0ceafSToomas Soome {
1265c7c0ceafSToomas Soome 	error_t (*f)(void);
1266c7c0ceafSToomas Soome 
1267c7c0ceafSToomas Soome 	/*
1268c7c0ceafSToomas Soome 	 * Check arguments
1269c7c0ceafSToomas Soome 	 */
1270c7c0ceafSToomas Soome 	if (check_subcmd_and_options(subcmd, opt, inst_subcmds, &f) ==
1271c7c0ceafSToomas Soome 	    BAM_ERROR)
1272c7c0ceafSToomas Soome 		return (BAM_ERROR);
1273c7c0ceafSToomas Soome 
1274c7c0ceafSToomas Soome 	return (f());
1275c7c0ceafSToomas Soome }
1276c7c0ceafSToomas Soome 
1277c7c0ceafSToomas Soome static error_t
12787c478bd9Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[])
12797c478bd9Sstevel@tonic-gate {
12807c478bd9Sstevel@tonic-gate 	error_t			ret;
12817c478bd9Sstevel@tonic-gate 	char			menu_path[PATH_MAX];
1282eb2bd662Svikram 	char			clean_menu_root[PATH_MAX];
1283ae115bc7Smrj 	char			path[PATH_MAX];
12847c478bd9Sstevel@tonic-gate 	menu_t			*menu;
1285eb2bd662Svikram 	char			menu_root[PATH_MAX];
1286b610f78eSvikram 	struct stat		sb;
12877c478bd9Sstevel@tonic-gate 	error_t (*f)(menu_t *mp, char *menu_path, char *opt);
1288eb2bd662Svikram 	char			*special;
1289eb2bd662Svikram 	char			*pool = NULL;
1290eb2bd662Svikram 	zfs_mnted_t		zmnted;
1291eb2bd662Svikram 	char			*zmntpt;
1292eb2bd662Svikram 	char			*osdev;
1293eb2bd662Svikram 	char			*osroot;
1294eb2bd662Svikram 	const char		*fcn = "bam_menu()";
1295986fd29aSsetje 
1296986fd29aSsetje 	/*
1297986fd29aSsetje 	 * Menu sub-command only applies to GRUB (i.e. x86)
1298986fd29aSsetje 	 */
1299eb2bd662Svikram 	if (!is_grub(bam_alt_root ? bam_root : "/")) {
1300eb2bd662Svikram 		bam_error(NOT_GRUB_BOOT);
1301986fd29aSsetje 		return (BAM_ERROR);
1302986fd29aSsetje 	}
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	/*
13057c478bd9Sstevel@tonic-gate 	 * Check arguments
13067c478bd9Sstevel@tonic-gate 	 */
13077c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
13087c478bd9Sstevel@tonic-gate 	if (ret == BAM_ERROR) {
13097c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13107c478bd9Sstevel@tonic-gate 	}
13117c478bd9Sstevel@tonic-gate 
1312eb2bd662Svikram 	assert(bam_root);
1313eb2bd662Svikram 
1314eb2bd662Svikram 	(void) strlcpy(menu_root, bam_root, sizeof (menu_root));
1315eb2bd662Svikram 	osdev = osroot = NULL;
1316eb2bd662Svikram 
1317eb2bd662Svikram 	if (strcmp(subcmd, "update_entry") == 0) {
1318eb2bd662Svikram 		assert(opt);
1319eb2bd662Svikram 
1320eb2bd662Svikram 		osdev = strtok(opt, ",");
1321eb2bd662Svikram 		assert(osdev);
1322eb2bd662Svikram 		osroot = strtok(NULL, ",");
1323eb2bd662Svikram 		if (osroot) {
1324eb2bd662Svikram 			/* fixup bam_root so that it points at osroot */
1325eb2bd662Svikram 			if (realpath(osroot, rootbuf) == NULL) {
1326eb2bd662Svikram 				bam_error(CANT_RESOLVE, osroot,
1327eb2bd662Svikram 				    strerror(errno));
1328eb2bd662Svikram 				return (BAM_ERROR);
1329eb2bd662Svikram 			}
1330eb2bd662Svikram 			bam_alt_root = 1;
1331eb2bd662Svikram 			bam_root  = rootbuf;
1332eb2bd662Svikram 			bam_rootlen = strlen(rootbuf);
1333eb2bd662Svikram 		}
1334eb2bd662Svikram 	}
133540541d5dSvikram 
133640541d5dSvikram 	/*
1337eb2bd662Svikram 	 * We support menu on PCFS (under certain conditions), but
1338eb2bd662Svikram 	 * not the OS root
133940541d5dSvikram 	 */
1340eb2bd662Svikram 	if (is_pcfs(bam_root)) {
1341eb2bd662Svikram 		bam_error(PCFS_ROOT_NOTSUP, bam_root);
1342eb2bd662Svikram 		return (BAM_ERROR);
1343ae115bc7Smrj 	}
1344ae115bc7Smrj 
1345eb2bd662Svikram 	if (stat(menu_root, &sb) == -1) {
134640541d5dSvikram 		bam_error(CANNOT_LOCATE_GRUB_MENU);
134740541d5dSvikram 		return (BAM_ERROR);
134840541d5dSvikram 	}
134940541d5dSvikram 
1350eb2bd662Svikram 	BAM_DPRINTF((D_MENU_ROOT, fcn, menu_root));
1351e7cbe64fSgw25295 
1352eb2bd662Svikram 	/*
1353eb2bd662Svikram 	 * We no longer use the GRUB slice file. If it exists, then
1354eb2bd662Svikram 	 * the user is doing something that is unsupported (such as
1355eb2bd662Svikram 	 * standard upgrading an old Live Upgrade BE). If that
1356eb2bd662Svikram 	 * happens, mimic existing behavior i.e. pretend that it is
1357eb2bd662Svikram 	 * not a BE. Emit a warning though.
1358eb2bd662Svikram 	 */
1359eb2bd662Svikram 	if (bam_alt_root) {
1360eb2bd662Svikram 		(void) snprintf(path, sizeof (path), "%s%s", bam_root,
1361eb2bd662Svikram 		    GRUB_slice);
1362eb2bd662Svikram 	} else {
1363eb2bd662Svikram 		(void) snprintf(path, sizeof (path), "%s", GRUB_slice);
1364e7cbe64fSgw25295 	}
1365e7cbe64fSgw25295 
136637eb779cSVikram Hegde 	if (bam_verbose && stat(path, &sb) == 0)
1367eb2bd662Svikram 		bam_error(GRUB_SLICE_FILE_EXISTS, 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) {
1374eb2bd662Svikram 			bam_error(CANT_FIND_SPECIAL, menu_root);
1375eb2bd662Svikram 			return (BAM_ERROR);
1376eb2bd662Svikram 		}
1377eb2bd662Svikram 		pool = strtok(special, "/");
1378eb2bd662Svikram 		INJECT_ERROR1("Z_MENU_GET_POOL", pool = NULL);
1379eb2bd662Svikram 		if (pool == NULL) {
1380eb2bd662Svikram 			free(special);
1381eb2bd662Svikram 			bam_error(CANT_FIND_POOL, menu_root);
1382eb2bd662Svikram 			return (BAM_ERROR);
1383eb2bd662Svikram 		}
1384eb2bd662Svikram 		BAM_DPRINTF((D_Z_MENU_GET_POOL_FROM_SPECIAL, fcn, pool));
1385eb2bd662Svikram 
1386eb2bd662Svikram 		zmntpt = mount_top_dataset(pool, &zmnted);
1387eb2bd662Svikram 		INJECT_ERROR1("Z_MENU_MOUNT_TOP_DATASET", zmntpt = NULL);
1388eb2bd662Svikram 		if (zmntpt == NULL) {
1389eb2bd662Svikram 			bam_error(CANT_MOUNT_POOL_DATASET, pool);
1390eb2bd662Svikram 			free(special);
1391eb2bd662Svikram 			return (BAM_ERROR);
1392eb2bd662Svikram 		}
1393eb2bd662Svikram 		BAM_DPRINTF((D_Z_GET_MENU_MOUNT_TOP_DATASET, fcn, zmntpt));
1394eb2bd662Svikram 
1395eb2bd662Svikram 		(void) strlcpy(menu_root, zmntpt, sizeof (menu_root));
1396eb2bd662Svikram 		BAM_DPRINTF((D_Z_GET_MENU_MENU_ROOT, fcn, menu_root));
1397eb2bd662Svikram 	}
1398eb2bd662Svikram 
1399eb2bd662Svikram 	elide_trailing_slash(menu_root, clean_menu_root,
1400eb2bd662Svikram 	    sizeof (clean_menu_root));
1401eb2bd662Svikram 
1402eb2bd662Svikram 	BAM_DPRINTF((D_CLEAN_MENU_ROOT, fcn, clean_menu_root));
1403eb2bd662Svikram 
1404eb2bd662Svikram 	(void) strlcpy(menu_path, clean_menu_root, sizeof (menu_path));
140540541d5dSvikram 	(void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path));
140640541d5dSvikram 
1407eb2bd662Svikram 	BAM_DPRINTF((D_MENU_PATH, fcn, menu_path));
1408eb2bd662Svikram 
140940541d5dSvikram 	/*
1410eb2bd662Svikram 	 * If listing the menu, display the menu location
141140541d5dSvikram 	 */
141244da779fSWilliam Kucharski 	if (strcmp(subcmd, "list_entry") == 0)
1413eb2bd662Svikram 		bam_print(GRUB_MENU_PATH, menu_path);
141444da779fSWilliam Kucharski 
141544da779fSWilliam Kucharski 	if ((menu = menu_read(menu_path)) == NULL) {
141644da779fSWilliam Kucharski 		bam_error(CANNOT_LOCATE_GRUB_MENU_FILE, menu_path);
141744da779fSWilliam Kucharski 		if (special != NULL)
141844da779fSWilliam Kucharski 			free(special);
141944da779fSWilliam Kucharski 
142044da779fSWilliam Kucharski 		return (BAM_ERROR);
142140541d5dSvikram 	}
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	/*
1424eb2bd662Svikram 	 * We already checked the following case in
1425eb2bd662Svikram 	 * check_subcmd_and_suboptions() above. Complete the
1426eb2bd662Svikram 	 * final step now.
14277c478bd9Sstevel@tonic-gate 	 */
14287c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "set_option") == 0) {
1429eb2bd662Svikram 		assert(largc == 1 && largv[0] && largv[1] == NULL);
14307c478bd9Sstevel@tonic-gate 		opt = largv[0];
143144da779fSWilliam Kucharski 	} else if ((strcmp(subcmd, "enable_hypervisor") != 0) &&
143244da779fSWilliam Kucharski 	    (strcmp(subcmd, "list_setting") != 0)) {
1433eb2bd662Svikram 		assert(largc == 0 && largv == NULL);
14347c478bd9Sstevel@tonic-gate 	}
14357c478bd9Sstevel@tonic-gate 
1436eb2bd662Svikram 	ret = get_boot_cap(bam_root);
1437eb2bd662Svikram 	if (ret != BAM_SUCCESS) {
1438eb2bd662Svikram 		BAM_DPRINTF((D_BOOT_GET_CAP_FAILED, fcn));
1439eb2bd662Svikram 		goto out;
1440eb2bd662Svikram 	}
1441ae115bc7Smrj 
14427c478bd9Sstevel@tonic-gate 	/*
14437c478bd9Sstevel@tonic-gate 	 * Once the sub-cmd handler has run
14447c478bd9Sstevel@tonic-gate 	 * only the line field is guaranteed to have valid values
14457c478bd9Sstevel@tonic-gate 	 */
144644da779fSWilliam Kucharski 	if (strcmp(subcmd, "update_entry") == 0) {
1447eb2bd662Svikram 		ret = f(menu, menu_root, osdev);
144844da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "upgrade") == 0) {
1449eb2bd662Svikram 		ret = f(menu, bam_root, menu_root);
145044da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "list_entry") == 0) {
14517c478bd9Sstevel@tonic-gate 		ret = f(menu, menu_path, opt);
145244da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "list_setting") == 0) {
145344da779fSWilliam Kucharski 		ret = f(menu, ((largc > 0) ? largv[0] : ""),
145444da779fSWilliam Kucharski 		    ((largc > 1) ? largv[1] : ""));
145544da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "disable_hypervisor") == 0) {
145644da779fSWilliam Kucharski 		if (is_sparc()) {
145744da779fSWilliam Kucharski 			bam_error(NO_SPARC, subcmd);
145844da779fSWilliam Kucharski 			ret = BAM_ERROR;
145944da779fSWilliam Kucharski 		} else {
146044da779fSWilliam Kucharski 			ret = f(menu, bam_root, NULL);
146144da779fSWilliam Kucharski 		}
146244da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "enable_hypervisor") == 0) {
146344da779fSWilliam Kucharski 		if (is_sparc()) {
146444da779fSWilliam Kucharski 			bam_error(NO_SPARC, subcmd);
146544da779fSWilliam Kucharski 			ret = BAM_ERROR;
146644da779fSWilliam Kucharski 		} else {
146744da779fSWilliam Kucharski 			char *extra_args = NULL;
146844da779fSWilliam Kucharski 
146944da779fSWilliam Kucharski 			/*
147044da779fSWilliam Kucharski 			 * Compress all arguments passed in the largv[] array
147144da779fSWilliam Kucharski 			 * into one string that can then be appended to the
147244da779fSWilliam Kucharski 			 * end of the kernel$ string the routine to enable the
147344da779fSWilliam Kucharski 			 * hypervisor will build.
147444da779fSWilliam Kucharski 			 *
147544da779fSWilliam Kucharski 			 * This allows the caller to supply arbitrary unparsed
147644da779fSWilliam Kucharski 			 * arguments, such as dom0 memory settings or APIC
147744da779fSWilliam Kucharski 			 * options.
147844da779fSWilliam Kucharski 			 *
147944da779fSWilliam Kucharski 			 * This concatenation will be done without ANY syntax
148044da779fSWilliam Kucharski 			 * checking whatsoever, so it's the responsibility of
148144da779fSWilliam Kucharski 			 * the caller to make sure the arguments are valid and
148244da779fSWilliam Kucharski 			 * do not duplicate arguments the conversion routines
148344da779fSWilliam Kucharski 			 * may create.
148444da779fSWilliam Kucharski 			 */
148544da779fSWilliam Kucharski 			if (largc > 0) {
148644da779fSWilliam Kucharski 				int extra_len, i;
148744da779fSWilliam Kucharski 
148844da779fSWilliam Kucharski 				for (extra_len = 0, i = 0; i < largc; i++)
148944da779fSWilliam Kucharski 					extra_len += strlen(largv[i]);
149044da779fSWilliam Kucharski 
149144da779fSWilliam Kucharski 				/*
149244da779fSWilliam Kucharski 				 * Allocate space for argument strings,
149344da779fSWilliam Kucharski 				 * intervening spaces and terminating NULL.
149444da779fSWilliam Kucharski 				 */
149544da779fSWilliam Kucharski 				extra_args = alloca(extra_len + largc);
149644da779fSWilliam Kucharski 
149744da779fSWilliam Kucharski 				(void) strcpy(extra_args, largv[0]);
149844da779fSWilliam Kucharski 
149944da779fSWilliam Kucharski 				for (i = 1; i < largc; i++) {
150044da779fSWilliam Kucharski 					(void) strcat(extra_args, " ");
150144da779fSWilliam Kucharski 					(void) strcat(extra_args, largv[i]);
150244da779fSWilliam Kucharski 				}
150344da779fSWilliam Kucharski 			}
150444da779fSWilliam Kucharski 
150544da779fSWilliam Kucharski 			ret = f(menu, bam_root, extra_args);
150644da779fSWilliam Kucharski 		}
150744da779fSWilliam Kucharski 	} else
1508eb2bd662Svikram 		ret = f(menu, NULL, opt);
1509eb2bd662Svikram 
15107c478bd9Sstevel@tonic-gate 	if (ret == BAM_WRITE) {
1511eb2bd662Svikram 		BAM_DPRINTF((D_WRITING_MENU_ROOT, fcn, clean_menu_root));
1512eb2bd662Svikram 		ret = menu_write(clean_menu_root, menu);
15137c478bd9Sstevel@tonic-gate 	}
15147c478bd9Sstevel@tonic-gate 
1515eb2bd662Svikram out:
1516eb2bd662Svikram 	INJECT_ERROR1("POOL_SET", pool = "/pooldata");
1517eb2bd662Svikram 	assert((is_zfs(menu_root)) ^ (pool == NULL));
1518eb2bd662Svikram 	if (pool) {
1519eb2bd662Svikram 		(void) umount_top_dataset(pool, zmnted, zmntpt);
1520eb2bd662Svikram 		free(special);
1521eb2bd662Svikram 	}
15227c478bd9Sstevel@tonic-gate 	menu_free(menu);
15237c478bd9Sstevel@tonic-gate 	return (ret);
15247c478bd9Sstevel@tonic-gate }
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate static error_t
15287c478bd9Sstevel@tonic-gate bam_archive(
15297c478bd9Sstevel@tonic-gate 	char *subcmd,
15307c478bd9Sstevel@tonic-gate 	char *opt)
15317c478bd9Sstevel@tonic-gate {
15327c478bd9Sstevel@tonic-gate 	error_t			ret;
15337c478bd9Sstevel@tonic-gate 	error_t			(*f)(char *root, char *opt);
1534eb2bd662Svikram 	const char		*fcn = "bam_archive()";
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 	/*
15378c1b6884Sszhou 	 * Add trailing / for archive subcommands
15388c1b6884Sszhou 	 */
15398c1b6884Sszhou 	if (rootbuf[strlen(rootbuf) - 1] != '/')
15408c1b6884Sszhou 		(void) strcat(rootbuf, "/");
15418c1b6884Sszhou 	bam_rootlen = strlen(rootbuf);
15428c1b6884Sszhou 
15438c1b6884Sszhou 	/*
15447c478bd9Sstevel@tonic-gate 	 * Check arguments
15457c478bd9Sstevel@tonic-gate 	 */
15467c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f);
15477c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS) {
15487c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15497c478bd9Sstevel@tonic-gate 	}
15507c478bd9Sstevel@tonic-gate 
1551eb2bd662Svikram 	ret = get_boot_cap(rootbuf);
1552eb2bd662Svikram 	if (ret != BAM_SUCCESS) {
1553eb2bd662Svikram 		BAM_DPRINTF((D_BOOT_GET_CAP_FAILED, fcn));
1554ae115bc7Smrj 		return (ret);
1555eb2bd662Svikram 	}
1556ae115bc7Smrj 
15577c478bd9Sstevel@tonic-gate 	/*
15587c478bd9Sstevel@tonic-gate 	 * Check archive not supported with update_all
15597c478bd9Sstevel@tonic-gate 	 * since it is awkward to display out-of-sync
15607c478bd9Sstevel@tonic-gate 	 * information for each BE.
15617c478bd9Sstevel@tonic-gate 	 */
15627c478bd9Sstevel@tonic-gate 	if (bam_check && strcmp(subcmd, "update_all") == 0) {
15637c478bd9Sstevel@tonic-gate 		bam_error(CHECK_NOT_SUPPORTED, subcmd);
15647c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15657c478bd9Sstevel@tonic-gate 	}
15667c478bd9Sstevel@tonic-gate 
1567b610f78eSvikram 	if (strcmp(subcmd, "update_all") == 0)
1568b610f78eSvikram 		bam_update_all = 1;
1569b610f78eSvikram 
157079c28b70SToomas Soome #if !defined(_OBP)
15712449e17fSsherrym 	ucode_install(bam_root);
15722449e17fSsherrym #endif
15732449e17fSsherrym 
1574b610f78eSvikram 	ret = f(bam_root, opt);
1575b610f78eSvikram 
1576b610f78eSvikram 	bam_update_all = 0;
1577b610f78eSvikram 
1578b610f78eSvikram 	return (ret);
15797c478bd9Sstevel@tonic-gate }
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1582ae115bc7Smrj void
15837c478bd9Sstevel@tonic-gate bam_error(char *format, ...)
15847c478bd9Sstevel@tonic-gate {
15857c478bd9Sstevel@tonic-gate 	va_list ap;
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	va_start(ap, format);
15887c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", prog);
15897c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
15907c478bd9Sstevel@tonic-gate 	va_end(ap);
15917c478bd9Sstevel@tonic-gate }
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1594eb2bd662Svikram void
1595eb2bd662Svikram bam_derror(char *format, ...)
1596eb2bd662Svikram {
1597eb2bd662Svikram 	va_list ap;
1598eb2bd662Svikram 
1599eb2bd662Svikram 	assert(bam_debug);
1600eb2bd662Svikram 
1601eb2bd662Svikram 	va_start(ap, format);
1602eb2bd662Svikram 	(void) fprintf(stderr, "DEBUG: ");
1603eb2bd662Svikram 	(void) vfprintf(stderr, format, ap);
1604eb2bd662Svikram 	va_end(ap);
1605eb2bd662Svikram }
1606eb2bd662Svikram 
1607eb2bd662Svikram /*PRINTFLIKE1*/
1608eb2bd662Svikram void
16097c478bd9Sstevel@tonic-gate bam_print(char *format, ...)
16107c478bd9Sstevel@tonic-gate {
16117c478bd9Sstevel@tonic-gate 	va_list ap;
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 	va_start(ap, format);
16147c478bd9Sstevel@tonic-gate 	(void) vfprintf(stdout, format, ap);
16157c478bd9Sstevel@tonic-gate 	va_end(ap);
16167c478bd9Sstevel@tonic-gate }
16177c478bd9Sstevel@tonic-gate 
1618ae115bc7Smrj /*PRINTFLIKE1*/
1619ae115bc7Smrj void
1620ae115bc7Smrj bam_print_stderr(char *format, ...)
1621ae115bc7Smrj {
1622ae115bc7Smrj 	va_list ap;
1623ae115bc7Smrj 
1624ae115bc7Smrj 	va_start(ap, format);
1625ae115bc7Smrj 	(void) vfprintf(stderr, format, ap);
1626ae115bc7Smrj 	va_end(ap);
1627ae115bc7Smrj }
1628ae115bc7Smrj 
162966b6aef6SWilliam Kucharski void
16307c478bd9Sstevel@tonic-gate bam_exit(int excode)
16317c478bd9Sstevel@tonic-gate {
1632cedc7e57SEnrico Perla - Sun Microsystems 	restore_env();
16337c478bd9Sstevel@tonic-gate 	bam_unlock();
16347c478bd9Sstevel@tonic-gate 	exit(excode);
16357c478bd9Sstevel@tonic-gate }
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate static void
16387c478bd9Sstevel@tonic-gate bam_lock(void)
16397c478bd9Sstevel@tonic-gate {
16407c478bd9Sstevel@tonic-gate 	struct flock lock;
16417c478bd9Sstevel@tonic-gate 	pid_t pid;
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS);
16447c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
16457c478bd9Sstevel@tonic-gate 		/*
16467c478bd9Sstevel@tonic-gate 		 * We may be invoked early in boot for archive verification.
16477c478bd9Sstevel@tonic-gate 		 * In this case, root is readonly and /var/run may not exist.
16487c478bd9Sstevel@tonic-gate 		 * Proceed without the lock
16497c478bd9Sstevel@tonic-gate 		 */
16507c478bd9Sstevel@tonic-gate 		if (errno == EROFS || errno == ENOENT) {
16517c478bd9Sstevel@tonic-gate 			bam_root_readonly = 1;
16527c478bd9Sstevel@tonic-gate 			return;
16537c478bd9Sstevel@tonic-gate 		}
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno));
16567c478bd9Sstevel@tonic-gate 		bam_exit(1);
16577c478bd9Sstevel@tonic-gate 	}
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
16607c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
16617c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
16627c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) {
16657c478bd9Sstevel@tonic-gate 		if (errno != EACCES && errno != EAGAIN) {
16667c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
16677c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
16687c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
16697c478bd9Sstevel@tonic-gate 			bam_exit(1);
16707c478bd9Sstevel@tonic-gate 		}
16717c478bd9Sstevel@tonic-gate 		pid = 0;
16727c478bd9Sstevel@tonic-gate 		(void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0);
16737c478bd9Sstevel@tonic-gate 		bam_print(FILE_LOCKED, pid);
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 		lock.l_type = F_WRLCK;
16767c478bd9Sstevel@tonic-gate 		lock.l_whence = SEEK_SET;
16777c478bd9Sstevel@tonic-gate 		lock.l_start = 0;
16787c478bd9Sstevel@tonic-gate 		lock.l_len = 0;
16797c478bd9Sstevel@tonic-gate 		if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) {
16807c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
16817c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
16827c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
16837c478bd9Sstevel@tonic-gate 			bam_exit(1);
16847c478bd9Sstevel@tonic-gate 		}
16857c478bd9Sstevel@tonic-gate 	}
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate 	/* We own the lock now */
16887c478bd9Sstevel@tonic-gate 	pid = getpid();
16897c478bd9Sstevel@tonic-gate 	(void) write(bam_lock_fd, &pid, sizeof (pid));
16907c478bd9Sstevel@tonic-gate }
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate static void
16937c478bd9Sstevel@tonic-gate bam_unlock(void)
16947c478bd9Sstevel@tonic-gate {
16957c478bd9Sstevel@tonic-gate 	struct flock unlock;
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	/*
16987c478bd9Sstevel@tonic-gate 	 * NOP if we don't hold the lock
16997c478bd9Sstevel@tonic-gate 	 */
17007c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
17017c478bd9Sstevel@tonic-gate 		return;
17027c478bd9Sstevel@tonic-gate 	}
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
17057c478bd9Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
17067c478bd9Sstevel@tonic-gate 	unlock.l_start = 0;
17077c478bd9Sstevel@tonic-gate 	unlock.l_len = 0;
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) {
17107c478bd9Sstevel@tonic-gate 		bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
17117c478bd9Sstevel@tonic-gate 	}
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 	if (close(bam_lock_fd) == -1) {
17147c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno));
17157c478bd9Sstevel@tonic-gate 	}
17167c478bd9Sstevel@tonic-gate 	bam_lock_fd = -1;
17177c478bd9Sstevel@tonic-gate }
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate static error_t
17207c478bd9Sstevel@tonic-gate list_archive(char *root, char *opt)
17217c478bd9Sstevel@tonic-gate {
17227c478bd9Sstevel@tonic-gate 	filelist_t flist;
17237c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
17247c478bd9Sstevel@tonic-gate 	line_t *lp;
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 	assert(root);
17277c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
17307c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
17317c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
17327c478bd9Sstevel@tonic-gate 	}
17337c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
17367c478bd9Sstevel@tonic-gate 		bam_print(PRINT, lp->line);
17377c478bd9Sstevel@tonic-gate 	}
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
17427c478bd9Sstevel@tonic-gate }
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate /*
17457c478bd9Sstevel@tonic-gate  * This routine writes a list of lines to a file.
17467c478bd9Sstevel@tonic-gate  * The list is *not* freed
17477c478bd9Sstevel@tonic-gate  */
17487c478bd9Sstevel@tonic-gate static error_t
17497c478bd9Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start)
17507c478bd9Sstevel@tonic-gate {
17517c478bd9Sstevel@tonic-gate 	char		tmpfile[PATH_MAX];
17527c478bd9Sstevel@tonic-gate 	char		path[PATH_MAX];
17537c478bd9Sstevel@tonic-gate 	FILE		*fp;
17547c478bd9Sstevel@tonic-gate 	int		ret;
17557c478bd9Sstevel@tonic-gate 	struct stat	sb;
17567c478bd9Sstevel@tonic-gate 	mode_t		mode;
17577c478bd9Sstevel@tonic-gate 	uid_t		root_uid;
17587c478bd9Sstevel@tonic-gate 	gid_t		sys_gid;
17597c478bd9Sstevel@tonic-gate 	struct passwd	*pw;
17607c478bd9Sstevel@tonic-gate 	struct group	*gp;
1761eb2bd662Svikram 	const char	*fcn = "list2file()";
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, final);
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 	if (start == NULL) {
1766eb2bd662Svikram 		/* Empty GRUB menu */
17677c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != -1) {
17687c478bd9Sstevel@tonic-gate 			bam_print(UNLINK_EMPTY, path);
17697c478bd9Sstevel@tonic-gate 			if (unlink(path) != 0) {
17707c478bd9Sstevel@tonic-gate 				bam_error(UNLINK_FAIL, path, strerror(errno));
17717c478bd9Sstevel@tonic-gate 				return (BAM_ERROR);
17727c478bd9Sstevel@tonic-gate 			} else {
17737c478bd9Sstevel@tonic-gate 				return (BAM_SUCCESS);
17747c478bd9Sstevel@tonic-gate 			}
17757c478bd9Sstevel@tonic-gate 		}
1776eb2bd662Svikram 		return (BAM_SUCCESS);
17777c478bd9Sstevel@tonic-gate 	}
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	/*
17807c478bd9Sstevel@tonic-gate 	 * Preserve attributes of existing file if possible,
17817c478bd9Sstevel@tonic-gate 	 * otherwise ask the system for uid/gid of root/sys.
17827c478bd9Sstevel@tonic-gate 	 * If all fails, fall back on hard-coded defaults.
17837c478bd9Sstevel@tonic-gate 	 */
17847c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != -1) {
17857c478bd9Sstevel@tonic-gate 		mode = sb.st_mode;
17867c478bd9Sstevel@tonic-gate 		root_uid = sb.st_uid;
17877c478bd9Sstevel@tonic-gate 		sys_gid = sb.st_gid;
17887c478bd9Sstevel@tonic-gate 	} else {
17897c478bd9Sstevel@tonic-gate 		mode = DEFAULT_DEV_MODE;
17907c478bd9Sstevel@tonic-gate 		if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
17917c478bd9Sstevel@tonic-gate 			root_uid = pw->pw_uid;
17927c478bd9Sstevel@tonic-gate 		} else {
17937c478bd9Sstevel@tonic-gate 			bam_error(CANT_FIND_USER,
17947c478bd9Sstevel@tonic-gate 			    DEFAULT_DEV_USER, DEFAULT_DEV_UID);
17957c478bd9Sstevel@tonic-gate 			root_uid = (uid_t)DEFAULT_DEV_UID;
17967c478bd9Sstevel@tonic-gate 		}
17977c478bd9Sstevel@tonic-gate 		if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
17987c478bd9Sstevel@tonic-gate 			sys_gid = gp->gr_gid;
17997c478bd9Sstevel@tonic-gate 		} else {
18007c478bd9Sstevel@tonic-gate 			bam_error(CANT_FIND_GROUP,
18017c478bd9Sstevel@tonic-gate 			    DEFAULT_DEV_GROUP, DEFAULT_DEV_GID);
18027c478bd9Sstevel@tonic-gate 			sys_gid = (gid_t)DEFAULT_DEV_GID;
18037c478bd9Sstevel@tonic-gate 		}
18047c478bd9Sstevel@tonic-gate 	}
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	(void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp);
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	/* Truncate tmpfile first */
18097c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "w");
18107c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
18117c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
18127c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18137c478bd9Sstevel@tonic-gate 	}
1814963390b4Svikram 	ret = fclose(fp);
1815963390b4Svikram 	INJECT_ERROR1("LIST2FILE_TRUNC_FCLOSE", ret = EOF);
1816963390b4Svikram 	if (ret == EOF) {
18177c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
18187c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18197c478bd9Sstevel@tonic-gate 	}
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate 	/* Now open it in append mode */
18227c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "a");
18237c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
18247c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
18257c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18267c478bd9Sstevel@tonic-gate 	}
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 	for (; start; start = start->next) {
1829963390b4Svikram 		ret = s_fputs(start->line, fp);
1830963390b4Svikram 		INJECT_ERROR1("LIST2FILE_FPUTS", ret = EOF);
1831963390b4Svikram 		if (ret == EOF) {
18327c478bd9Sstevel@tonic-gate 			bam_error(WRITE_FAIL, tmpfile, strerror(errno));
18337c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
18347c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
18357c478bd9Sstevel@tonic-gate 		}
18367c478bd9Sstevel@tonic-gate 	}
18377c478bd9Sstevel@tonic-gate 
1838963390b4Svikram 	ret = fclose(fp);
1839963390b4Svikram 	INJECT_ERROR1("LIST2FILE_APPEND_FCLOSE", ret = EOF);
1840963390b4Svikram 	if (ret == EOF) {
18417c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
18427c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18437c478bd9Sstevel@tonic-gate 	}
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	/*
1846de531be4Sjg 	 * Set up desired attributes.  Ignore failures on filesystems
1847de531be4Sjg 	 * not supporting these operations - pcfs reports unsupported
1848de531be4Sjg 	 * operations as EINVAL.
18497c478bd9Sstevel@tonic-gate 	 */
18507c478bd9Sstevel@tonic-gate 	ret = chmod(tmpfile, mode);
1851de531be4Sjg 	if (ret == -1 &&
1852de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
18537c478bd9Sstevel@tonic-gate 		bam_error(CHMOD_FAIL, tmpfile, strerror(errno));
18547c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18557c478bd9Sstevel@tonic-gate 	}
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 	ret = chown(tmpfile, root_uid, sys_gid);
1858de531be4Sjg 	if (ret == -1 &&
1859de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
18607c478bd9Sstevel@tonic-gate 		bam_error(CHOWN_FAIL, tmpfile, strerror(errno));
18617c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18627c478bd9Sstevel@tonic-gate 	}
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 	/*
18657c478bd9Sstevel@tonic-gate 	 * Do an atomic rename
18667c478bd9Sstevel@tonic-gate 	 */
18677c478bd9Sstevel@tonic-gate 	ret = rename(tmpfile, path);
1868963390b4Svikram 	INJECT_ERROR1("LIST2FILE_RENAME", ret = -1);
18697c478bd9Sstevel@tonic-gate 	if (ret != 0) {
18707c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path, strerror(errno));
18717c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18727c478bd9Sstevel@tonic-gate 	}
18737c478bd9Sstevel@tonic-gate 
1874eb2bd662Svikram 	BAM_DPRINTF((D_WROTE_FILE, fcn, path));
18757c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
18767c478bd9Sstevel@tonic-gate }
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate /*
187948847494SEnrico Perla - Sun Microsystems  * Checks if the path specified (without the file name at the end) exists
188048847494SEnrico Perla - Sun Microsystems  * and creates it if not. If the path exists and is not a directory, an attempt
188148847494SEnrico Perla - Sun Microsystems  * to unlink is made.
18827c478bd9Sstevel@tonic-gate  */
188348847494SEnrico Perla - Sun Microsystems static int
188448847494SEnrico Perla - Sun Microsystems setup_path(char *path)
188548847494SEnrico Perla - Sun Microsystems {
188648847494SEnrico Perla - Sun Microsystems 	char 		*p;
188748847494SEnrico Perla - Sun Microsystems 	int		ret;
188848847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
188948847494SEnrico Perla - Sun Microsystems 
189048847494SEnrico Perla - Sun Microsystems 	p = strrchr(path, '/');
189148847494SEnrico Perla - Sun Microsystems 	if (p != NULL) {
189248847494SEnrico Perla - Sun Microsystems 		*p = '\0';
189348847494SEnrico Perla - Sun Microsystems 		if (stat(path, &sb) != 0 || !(S_ISDIR(sb.st_mode))) {
189448847494SEnrico Perla - Sun Microsystems 			/* best effort attempt, mkdirp will catch the error */
189548847494SEnrico Perla - Sun Microsystems 			(void) unlink(path);
189648847494SEnrico Perla - Sun Microsystems 			if (bam_verbose)
189748847494SEnrico Perla - Sun Microsystems 				bam_print(NEED_DIRPATH, path);
189848847494SEnrico Perla - Sun Microsystems 			ret = mkdirp(path, DIR_PERMS);
189948847494SEnrico Perla - Sun Microsystems 			if (ret == -1) {
190048847494SEnrico Perla - Sun Microsystems 				bam_error(MKDIR_FAILED, path, strerror(errno));
190148847494SEnrico Perla - Sun Microsystems 				*p = '/';
190248847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
190348847494SEnrico Perla - Sun Microsystems 			}
190448847494SEnrico Perla - Sun Microsystems 		}
190548847494SEnrico Perla - Sun Microsystems 		*p = '/';
190648847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
190748847494SEnrico Perla - Sun Microsystems 	}
190848847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
190948847494SEnrico Perla - Sun Microsystems }
191048847494SEnrico Perla - Sun Microsystems 
191148847494SEnrico Perla - Sun Microsystems typedef union {
191248847494SEnrico Perla - Sun Microsystems 	gzFile	gzfile;
191348847494SEnrico Perla - Sun Microsystems 	int	fdfile;
191448847494SEnrico Perla - Sun Microsystems } outfile;
191548847494SEnrico Perla - Sun Microsystems 
191648847494SEnrico Perla - Sun Microsystems typedef struct {
191748847494SEnrico Perla - Sun Microsystems 	char		path[PATH_MAX];
191848847494SEnrico Perla - Sun Microsystems 	outfile		out;
191948847494SEnrico Perla - Sun Microsystems } cachefile;
192048847494SEnrico Perla - Sun Microsystems 
192148847494SEnrico Perla - Sun Microsystems static int
192248847494SEnrico Perla - Sun Microsystems setup_file(char *base, const char *path, cachefile *cf)
192348847494SEnrico Perla - Sun Microsystems {
192448847494SEnrico Perla - Sun Microsystems 	int	ret;
192548847494SEnrico Perla - Sun Microsystems 	char	*strip;
192648847494SEnrico Perla - Sun Microsystems 
192748847494SEnrico Perla - Sun Microsystems 	/* init gzfile or fdfile in case we fail before opening */
192848847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT)
192948847494SEnrico Perla - Sun Microsystems 		cf->out.gzfile = NULL;
193048847494SEnrico Perla - Sun Microsystems 	else
193148847494SEnrico Perla - Sun Microsystems 		cf->out.fdfile = -1;
193248847494SEnrico Perla - Sun Microsystems 
193348847494SEnrico Perla - Sun Microsystems 	/* strip the trailing altroot path */
193448847494SEnrico Perla - Sun Microsystems 	strip = (char *)path + strlen(rootbuf);
193548847494SEnrico Perla - Sun Microsystems 
193648847494SEnrico Perla - Sun Microsystems 	ret = snprintf(cf->path, sizeof (cf->path), "%s/%s", base, strip);
193748847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (cf->path)) {
193848847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
193948847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
194048847494SEnrico Perla - Sun Microsystems 	}
194148847494SEnrico Perla - Sun Microsystems 
194248847494SEnrico Perla - Sun Microsystems 	/* Check if path is present in the archive cache directory */
194348847494SEnrico Perla - Sun Microsystems 	if (setup_path(cf->path) == BAM_ERROR)
194448847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
194548847494SEnrico Perla - Sun Microsystems 
194648847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT) {
194748847494SEnrico Perla - Sun Microsystems 		if ((cf->out.gzfile = gzopen(cf->path, "wb")) == NULL) {
1948cedc7e57SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, cf->path, strerror(errno));
194948847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
195048847494SEnrico Perla - Sun Microsystems 		}
195148847494SEnrico Perla - Sun Microsystems 		(void) gzsetparams(cf->out.gzfile, Z_BEST_SPEED,
195248847494SEnrico Perla - Sun Microsystems 		    Z_DEFAULT_STRATEGY);
195348847494SEnrico Perla - Sun Microsystems 	} else {
195448847494SEnrico Perla - Sun Microsystems 		if ((cf->out.fdfile = open(cf->path, O_WRONLY | O_CREAT, 0644))
195548847494SEnrico Perla - Sun Microsystems 		    == -1) {
195648847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, cf->path, strerror(errno));
195748847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
195848847494SEnrico Perla - Sun Microsystems 		}
195948847494SEnrico Perla - Sun Microsystems 	}
196048847494SEnrico Perla - Sun Microsystems 
196148847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
196248847494SEnrico Perla - Sun Microsystems }
196348847494SEnrico Perla - Sun Microsystems 
196448847494SEnrico Perla - Sun Microsystems static int
196548847494SEnrico Perla - Sun Microsystems cache_write(cachefile cf, char *buf, int size)
196648847494SEnrico Perla - Sun Microsystems {
196748847494SEnrico Perla - Sun Microsystems 	int	err;
196848847494SEnrico Perla - Sun Microsystems 
196948847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT) {
197048847494SEnrico Perla - Sun Microsystems 		if (gzwrite(cf.out.gzfile, buf, size) < 1) {
197148847494SEnrico Perla - Sun Microsystems 			bam_error(GZ_WRITE_FAIL, gzerror(cf.out.gzfile, &err));
1972cedc7e57SEnrico Perla - Sun Microsystems 			if (err == Z_ERRNO && bam_verbose) {
197348847494SEnrico Perla - Sun Microsystems 				bam_error(WRITE_FAIL, cf.path, strerror(errno));
1974cedc7e57SEnrico Perla - Sun Microsystems 			}
197548847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
197648847494SEnrico Perla - Sun Microsystems 		}
197748847494SEnrico Perla - Sun Microsystems 	} else {
197848847494SEnrico Perla - Sun Microsystems 		if (write(cf.out.fdfile, buf, size) < 1) {
197948847494SEnrico Perla - Sun Microsystems 			bam_error(WRITE_FAIL, cf.path, strerror(errno));
198048847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
198148847494SEnrico Perla - Sun Microsystems 		}
198248847494SEnrico Perla - Sun Microsystems 	}
198348847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
198448847494SEnrico Perla - Sun Microsystems }
198548847494SEnrico Perla - Sun Microsystems 
198648847494SEnrico Perla - Sun Microsystems static int
198748847494SEnrico Perla - Sun Microsystems cache_close(cachefile cf)
198848847494SEnrico Perla - Sun Microsystems {
198948847494SEnrico Perla - Sun Microsystems 	int	ret;
199048847494SEnrico Perla - Sun Microsystems 
199148847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT) {
199248847494SEnrico Perla - Sun Microsystems 		if (cf.out.gzfile) {
199348847494SEnrico Perla - Sun Microsystems 			ret = gzclose(cf.out.gzfile);
1994cedc7e57SEnrico Perla - Sun Microsystems 			if (ret != Z_OK) {
199548847494SEnrico Perla - Sun Microsystems 				bam_error(CLOSE_FAIL, cf.path, strerror(errno));
199648847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
199748847494SEnrico Perla - Sun Microsystems 			}
1998cedc7e57SEnrico Perla - Sun Microsystems 		}
199948847494SEnrico Perla - Sun Microsystems 	} else {
200048847494SEnrico Perla - Sun Microsystems 		if (cf.out.fdfile != -1) {
200148847494SEnrico Perla - Sun Microsystems 			ret = close(cf.out.fdfile);
200248847494SEnrico Perla - Sun Microsystems 			if (ret != 0) {
200348847494SEnrico Perla - Sun Microsystems 				bam_error(CLOSE_FAIL, cf.path, strerror(errno));
200448847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
200548847494SEnrico Perla - Sun Microsystems 			}
200648847494SEnrico Perla - Sun Microsystems 		}
200748847494SEnrico Perla - Sun Microsystems 	}
200848847494SEnrico Perla - Sun Microsystems 
200948847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
201048847494SEnrico Perla - Sun Microsystems }
201148847494SEnrico Perla - Sun Microsystems 
201248847494SEnrico Perla - Sun Microsystems static int
201348847494SEnrico Perla - Sun Microsystems dircache_updatefile(const char *path, int what)
201448847494SEnrico Perla - Sun Microsystems {
201548847494SEnrico Perla - Sun Microsystems 	int 		ret, exitcode;
201648847494SEnrico Perla - Sun Microsystems 	char 		buf[4096 * 4];
201748847494SEnrico Perla - Sun Microsystems 	FILE 		*infile;
201848847494SEnrico Perla - Sun Microsystems 	cachefile 	outfile, outupdt;
201948847494SEnrico Perla - Sun Microsystems 
2020cedc7e57SEnrico Perla - Sun Microsystems 	if (bam_nowrite()) {
2021cedc7e57SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NEED_UPDATE);
2022cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
2023cedc7e57SEnrico Perla - Sun Microsystems 	}
2024cedc7e57SEnrico Perla - Sun Microsystems 
2025cedc7e57SEnrico Perla - Sun Microsystems 	if (!has_cachedir(what))
202648847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
202748847494SEnrico Perla - Sun Microsystems 
202848847494SEnrico Perla - Sun Microsystems 	if ((infile = fopen(path, "rb")) == NULL) {
202948847494SEnrico Perla - Sun Microsystems 		bam_error(OPEN_FAIL, path, strerror(errno));
203048847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
203148847494SEnrico Perla - Sun Microsystems 	}
203248847494SEnrico Perla - Sun Microsystems 
203348847494SEnrico Perla - Sun Microsystems 	ret = setup_file(get_cachedir(what), path, &outfile);
203448847494SEnrico Perla - Sun Microsystems 	if (ret == BAM_ERROR) {
203548847494SEnrico Perla - Sun Microsystems 		exitcode = BAM_ERROR;
203648847494SEnrico Perla - Sun Microsystems 		goto out;
203748847494SEnrico Perla - Sun Microsystems 	}
203848847494SEnrico Perla - Sun Microsystems 	if (!is_dir_flag_on(what, NO_MULTI)) {
203948847494SEnrico Perla - Sun Microsystems 		ret = setup_file(get_updatedir(what), path, &outupdt);
204048847494SEnrico Perla - Sun Microsystems 		if (ret == BAM_ERROR)
204148847494SEnrico Perla - Sun Microsystems 			set_dir_flag(what, NO_MULTI);
204248847494SEnrico Perla - Sun Microsystems 	}
204348847494SEnrico Perla - Sun Microsystems 
204448847494SEnrico Perla - Sun Microsystems 	while ((ret = fread(buf, 1, sizeof (buf), infile)) > 0) {
204548847494SEnrico Perla - Sun Microsystems 		if (cache_write(outfile, buf, ret) == BAM_ERROR) {
204648847494SEnrico Perla - Sun Microsystems 			exitcode = BAM_ERROR;
204748847494SEnrico Perla - Sun Microsystems 			goto out;
204848847494SEnrico Perla - Sun Microsystems 		}
204948847494SEnrico Perla - Sun Microsystems 		if (!is_dir_flag_on(what, NO_MULTI))
205048847494SEnrico Perla - Sun Microsystems 			if (cache_write(outupdt, buf, ret) == BAM_ERROR)
205148847494SEnrico Perla - Sun Microsystems 				set_dir_flag(what, NO_MULTI);
205248847494SEnrico Perla - Sun Microsystems 	}
205348847494SEnrico Perla - Sun Microsystems 
205448847494SEnrico Perla - Sun Microsystems 	set_dir_flag(what, NEED_UPDATE);
205548847494SEnrico Perla - Sun Microsystems 	get_count(what)++;
2056cedc7e57SEnrico Perla - Sun Microsystems 	if (get_count(what) > COUNT_MAX)
2057cedc7e57SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
205848847494SEnrico Perla - Sun Microsystems 	exitcode = BAM_SUCCESS;
205948847494SEnrico Perla - Sun Microsystems out:
206048847494SEnrico Perla - Sun Microsystems 	(void) fclose(infile);
206148847494SEnrico Perla - Sun Microsystems 	if (cache_close(outfile) == BAM_ERROR)
206248847494SEnrico Perla - Sun Microsystems 		exitcode = BAM_ERROR;
206348847494SEnrico Perla - Sun Microsystems 	if (!is_dir_flag_on(what, NO_MULTI) &&
206448847494SEnrico Perla - Sun Microsystems 	    cache_close(outupdt) == BAM_ERROR)
206548847494SEnrico Perla - Sun Microsystems 		exitcode = BAM_ERROR;
206648847494SEnrico Perla - Sun Microsystems 	if (exitcode == BAM_ERROR)
206748847494SEnrico Perla - Sun Microsystems 		set_flag(UPDATE_ERROR);
206848847494SEnrico Perla - Sun Microsystems 	return (exitcode);
206948847494SEnrico Perla - Sun Microsystems }
207048847494SEnrico Perla - Sun Microsystems 
207148847494SEnrico Perla - Sun Microsystems static int
207248847494SEnrico Perla - Sun Microsystems dircache_updatedir(const char *path, int what, int updt)
207348847494SEnrico Perla - Sun Microsystems {
207448847494SEnrico Perla - Sun Microsystems 	int		ret;
207548847494SEnrico Perla - Sun Microsystems 	char		dpath[PATH_MAX];
207648847494SEnrico Perla - Sun Microsystems 	char		*strip;
207748847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
207848847494SEnrico Perla - Sun Microsystems 
207948847494SEnrico Perla - Sun Microsystems 	strip = (char *)path + strlen(rootbuf);
208048847494SEnrico Perla - Sun Microsystems 
208148847494SEnrico Perla - Sun Microsystems 	ret = snprintf(dpath, sizeof (dpath), "%s/%s", updt ?
208248847494SEnrico Perla - Sun Microsystems 	    get_updatedir(what) : get_cachedir(what), strip);
208348847494SEnrico Perla - Sun Microsystems 
208448847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (dpath)) {
208548847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
208648847494SEnrico Perla - Sun Microsystems 		set_flag(UPDATE_ERROR);
208748847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
208848847494SEnrico Perla - Sun Microsystems 	}
208948847494SEnrico Perla - Sun Microsystems 
209048847494SEnrico Perla - Sun Microsystems 	if (stat(dpath, &sb) == 0 && S_ISDIR(sb.st_mode))
209148847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
209248847494SEnrico Perla - Sun Microsystems 
209348847494SEnrico Perla - Sun Microsystems 	if (updt) {
209448847494SEnrico Perla - Sun Microsystems 		if (!is_dir_flag_on(what, NO_MULTI))
209548847494SEnrico Perla - Sun Microsystems 			if (!bam_nowrite() && mkdirp(dpath, DIR_PERMS) == -1)
209648847494SEnrico Perla - Sun Microsystems 				set_dir_flag(what, NO_MULTI);
209748847494SEnrico Perla - Sun Microsystems 	} else {
209848847494SEnrico Perla - Sun Microsystems 		if (!bam_nowrite() && mkdirp(dpath, DIR_PERMS) == -1) {
209948847494SEnrico Perla - Sun Microsystems 			set_flag(UPDATE_ERROR);
210048847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
210148847494SEnrico Perla - Sun Microsystems 		}
210248847494SEnrico Perla - Sun Microsystems 	}
210348847494SEnrico Perla - Sun Microsystems 
210448847494SEnrico Perla - Sun Microsystems 	set_dir_flag(what, NEED_UPDATE);
210548847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
210648847494SEnrico Perla - Sun Microsystems }
210748847494SEnrico Perla - Sun Microsystems 
210848847494SEnrico Perla - Sun Microsystems #define	DO_CACHE_DIR	0
210948847494SEnrico Perla - Sun Microsystems #define	DO_UPDATE_DIR	1
211048847494SEnrico Perla - Sun Microsystems 
211148847494SEnrico Perla - Sun Microsystems #if defined(_LP64) || defined(_LONGLONG_TYPE)
211248847494SEnrico Perla - Sun Microsystems typedef		Elf64_Ehdr	_elfhdr;
211348847494SEnrico Perla - Sun Microsystems #else
211448847494SEnrico Perla - Sun Microsystems typedef		Elf32_Ehdr	_elfhdr;
211548847494SEnrico Perla - Sun Microsystems #endif
211648847494SEnrico Perla - Sun Microsystems 
211748847494SEnrico Perla - Sun Microsystems /*
211848847494SEnrico Perla - Sun Microsystems  * This routine updates the contents of the cache directory
211948847494SEnrico Perla - Sun Microsystems  */
212048847494SEnrico Perla - Sun Microsystems static int
212148847494SEnrico Perla - Sun Microsystems update_dircache(const char *path, int flags)
212248847494SEnrico Perla - Sun Microsystems {
212348847494SEnrico Perla - Sun Microsystems 	int rc = BAM_SUCCESS;
212448847494SEnrico Perla - Sun Microsystems 
212548847494SEnrico Perla - Sun Microsystems 	switch (flags) {
212648847494SEnrico Perla - Sun Microsystems 	case FTW_F:
212748847494SEnrico Perla - Sun Microsystems 		{
212848847494SEnrico Perla - Sun Microsystems 		int	fd;
212948847494SEnrico Perla - Sun Microsystems 		_elfhdr	elf;
213048847494SEnrico Perla - Sun Microsystems 
213148847494SEnrico Perla - Sun Microsystems 		if ((fd = open(path, O_RDONLY)) < 0) {
213248847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, path, strerror(errno));
213348847494SEnrico Perla - Sun Microsystems 			set_flag(UPDATE_ERROR);
213448847494SEnrico Perla - Sun Microsystems 			rc = BAM_ERROR;
213548847494SEnrico Perla - Sun Microsystems 			break;
213648847494SEnrico Perla - Sun Microsystems 		}
213748847494SEnrico Perla - Sun Microsystems 
213848847494SEnrico Perla - Sun Microsystems 		/*
213948847494SEnrico Perla - Sun Microsystems 		 * libelf and gelf would be a cleaner and easier way to handle
214048847494SEnrico Perla - Sun Microsystems 		 * this, but libelf fails compilation if _ILP32 is defined &&
214148847494SEnrico Perla - Sun Microsystems 		 * _FILE_OFFSET_BITS is != 32 ...
214248847494SEnrico Perla - Sun Microsystems 		 */
214348847494SEnrico Perla - Sun Microsystems 		if (read(fd, (void *)&elf, sizeof (_elfhdr)) < 0) {
214448847494SEnrico Perla - Sun Microsystems 			bam_error(READ_FAIL, path, strerror(errno));
214548847494SEnrico Perla - Sun Microsystems 			set_flag(UPDATE_ERROR);
214648847494SEnrico Perla - Sun Microsystems 			(void) close(fd);
214748847494SEnrico Perla - Sun Microsystems 			rc = BAM_ERROR;
214848847494SEnrico Perla - Sun Microsystems 			break;
214948847494SEnrico Perla - Sun Microsystems 		}
215048847494SEnrico Perla - Sun Microsystems 		(void) close(fd);
215148847494SEnrico Perla - Sun Microsystems 
215248847494SEnrico Perla - Sun Microsystems 		/*
215348847494SEnrico Perla - Sun Microsystems 		 * If the file is not an executable and is not inside an amd64
215448847494SEnrico Perla - Sun Microsystems 		 * directory, we copy it in both the cache directories,
215548847494SEnrico Perla - Sun Microsystems 		 * otherwise, we only copy it inside the 64-bit one.
215648847494SEnrico Perla - Sun Microsystems 		 */
215748847494SEnrico Perla - Sun Microsystems 		if (memcmp(elf.e_ident, ELFMAG, 4) != 0) {
215848847494SEnrico Perla - Sun Microsystems 			if (strstr(path, "/amd64")) {
215948847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE64);
216048847494SEnrico Perla - Sun Microsystems 			} else {
216148847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE32);
2162cedc7e57SEnrico Perla - Sun Microsystems 				if (rc == BAM_SUCCESS)
2163cedc7e57SEnrico Perla - Sun Microsystems 					rc = dircache_updatefile(path, FILE64);
216448847494SEnrico Perla - Sun Microsystems 			}
216548847494SEnrico Perla - Sun Microsystems 		} else {
216648847494SEnrico Perla - Sun Microsystems 			/*
216748847494SEnrico Perla - Sun Microsystems 			 * Based on the ELF class we copy the file in the 32-bit
216848847494SEnrico Perla - Sun Microsystems 			 * or the 64-bit cache directory.
216948847494SEnrico Perla - Sun Microsystems 			 */
2170cedc7e57SEnrico Perla - Sun Microsystems 			if (elf.e_ident[EI_CLASS] == ELFCLASS32) {
217148847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE32);
2172cedc7e57SEnrico Perla - Sun Microsystems 			} else if (elf.e_ident[EI_CLASS] == ELFCLASS64) {
217348847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE64);
2174cedc7e57SEnrico Perla - Sun Microsystems 			} else {
217548847494SEnrico Perla - Sun Microsystems 				bam_print(NO3264ELF, path);
217648847494SEnrico Perla - Sun Microsystems 				/* paranoid */
217748847494SEnrico Perla - Sun Microsystems 				rc  = dircache_updatefile(path, FILE32);
217848847494SEnrico Perla - Sun Microsystems 				if (rc == BAM_SUCCESS)
217948847494SEnrico Perla - Sun Microsystems 					rc = dircache_updatefile(path, FILE64);
218048847494SEnrico Perla - Sun Microsystems 			}
218148847494SEnrico Perla - Sun Microsystems 		}
218248847494SEnrico Perla - Sun Microsystems 		break;
218348847494SEnrico Perla - Sun Microsystems 		}
218448847494SEnrico Perla - Sun Microsystems 	case FTW_D:
218548847494SEnrico Perla - Sun Microsystems 		if (strstr(path, "/amd64") == NULL) {
218648847494SEnrico Perla - Sun Microsystems 			rc = dircache_updatedir(path, FILE32, DO_UPDATE_DIR);
218748847494SEnrico Perla - Sun Microsystems 			if (rc == BAM_SUCCESS)
218848847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatedir(path, FILE32,
218948847494SEnrico Perla - Sun Microsystems 				    DO_CACHE_DIR);
219048847494SEnrico Perla - Sun Microsystems 		} else {
219148847494SEnrico Perla - Sun Microsystems 			if (has_cachedir(FILE64)) {
219248847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatedir(path, FILE64,
219348847494SEnrico Perla - Sun Microsystems 				    DO_UPDATE_DIR);
219448847494SEnrico Perla - Sun Microsystems 				if (rc == BAM_SUCCESS)
219548847494SEnrico Perla - Sun Microsystems 					rc = dircache_updatedir(path, FILE64,
219648847494SEnrico Perla - Sun Microsystems 					    DO_CACHE_DIR);
219748847494SEnrico Perla - Sun Microsystems 			}
219848847494SEnrico Perla - Sun Microsystems 		}
219948847494SEnrico Perla - Sun Microsystems 		break;
220048847494SEnrico Perla - Sun Microsystems 	default:
220148847494SEnrico Perla - Sun Microsystems 		rc = BAM_ERROR;
220248847494SEnrico Perla - Sun Microsystems 		break;
220348847494SEnrico Perla - Sun Microsystems 	}
220448847494SEnrico Perla - Sun Microsystems 
220548847494SEnrico Perla - Sun Microsystems 	return (rc);
220648847494SEnrico Perla - Sun Microsystems }
220748847494SEnrico Perla - Sun Microsystems 
22087c478bd9Sstevel@tonic-gate /*ARGSUSED*/
22097c478bd9Sstevel@tonic-gate static int
22107c478bd9Sstevel@tonic-gate cmpstat(
22117c478bd9Sstevel@tonic-gate 	const char *file,
221248847494SEnrico Perla - Sun Microsystems 	const struct stat *st,
22137c478bd9Sstevel@tonic-gate 	int flags,
22147c478bd9Sstevel@tonic-gate 	struct FTW *ftw)
22157c478bd9Sstevel@tonic-gate {
22167c478bd9Sstevel@tonic-gate 	uint_t 		sz;
22177c478bd9Sstevel@tonic-gate 	uint64_t 	*value;
22187c478bd9Sstevel@tonic-gate 	uint64_t 	filestat[2];
221989c3ee43SGangadhar Mylapuram 	int 		error, ret, status;
22207c478bd9Sstevel@tonic-gate 
222158091fd8Ssetje 	struct safefile *safefilep;
222258091fd8Ssetje 	FILE 		*fp;
222348847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
222489c3ee43SGangadhar Mylapuram 	regex_t re;
222558091fd8Ssetje 
22267c478bd9Sstevel@tonic-gate 	/*
222748847494SEnrico Perla - Sun Microsystems 	 * On SPARC we create/update links too.
22287c478bd9Sstevel@tonic-gate 	 */
222948847494SEnrico Perla - Sun Microsystems 	if (flags != FTW_F && flags != FTW_D && (flags == FTW_SL &&
223048847494SEnrico Perla - Sun Microsystems 	    !is_flag_on(IS_SPARC_TARGET)))
223148847494SEnrico Perla - Sun Microsystems 		return (0);
223248847494SEnrico Perla - Sun Microsystems 
223348847494SEnrico Perla - Sun Microsystems 	/*
223448847494SEnrico Perla - Sun Microsystems 	 * Ignore broken links
223548847494SEnrico Perla - Sun Microsystems 	 */
223648847494SEnrico Perla - Sun Microsystems 	if (flags == FTW_SL && stat(file, &sb) < 0)
22377c478bd9Sstevel@tonic-gate 		return (0);
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate 	/*
22407c478bd9Sstevel@tonic-gate 	 * new_nvlp may be NULL if there were errors earlier
22417c478bd9Sstevel@tonic-gate 	 * but this is not fatal to update determination.
22427c478bd9Sstevel@tonic-gate 	 */
22437c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp) {
224448847494SEnrico Perla - Sun Microsystems 		filestat[0] = st->st_size;
224548847494SEnrico Perla - Sun Microsystems 		filestat[1] = st->st_mtime;
22467c478bd9Sstevel@tonic-gate 		error = nvlist_add_uint64_array(walk_arg.new_nvlp,
22477c478bd9Sstevel@tonic-gate 		    file + bam_rootlen, filestat, 2);
22487c478bd9Sstevel@tonic-gate 		if (error)
22497c478bd9Sstevel@tonic-gate 			bam_error(NVADD_FAIL, file, strerror(error));
22507c478bd9Sstevel@tonic-gate 	}
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 	/*
2253d876c67dSjg 	 * If we are invoked as part of system/filesystem/boot-archive, then
225458091fd8Ssetje 	 * there are a number of things we should not worry about
22557c478bd9Sstevel@tonic-gate 	 */
225658091fd8Ssetje 	if (bam_smf_check) {
225758091fd8Ssetje 		/* ignore amd64 modules unless we are booted amd64. */
225858091fd8Ssetje 		if (!is_amd64() && strstr(file, "/amd64/") != 0)
22597c478bd9Sstevel@tonic-gate 			return (0);
22607c478bd9Sstevel@tonic-gate 
226158091fd8Ssetje 		/* read in list of safe files */
226258091fd8Ssetje 		if (safefiles == NULL)
226358091fd8Ssetje 			if (fp = fopen("/boot/solaris/filelist.safe", "r")) {
226458091fd8Ssetje 				safefiles = s_calloc(1,
226558091fd8Ssetje 				    sizeof (struct safefile));
226658091fd8Ssetje 				safefilep = safefiles;
226758091fd8Ssetje 				safefilep->name = s_calloc(1, MAXPATHLEN +
226858091fd8Ssetje 				    MAXNAMELEN);
226958091fd8Ssetje 				safefilep->next = NULL;
227058091fd8Ssetje 				while (s_fgets(safefilep->name, MAXPATHLEN +
227158091fd8Ssetje 				    MAXNAMELEN, fp) != NULL) {
227258091fd8Ssetje 					safefilep->next = s_calloc(1,
227358091fd8Ssetje 					    sizeof (struct safefile));
227458091fd8Ssetje 					safefilep = safefilep->next;
227558091fd8Ssetje 					safefilep->name = s_calloc(1,
227658091fd8Ssetje 					    MAXPATHLEN + MAXNAMELEN);
227758091fd8Ssetje 					safefilep->next = NULL;
227858091fd8Ssetje 				}
227958091fd8Ssetje 				(void) fclose(fp);
228058091fd8Ssetje 			}
228158091fd8Ssetje 	}
228258091fd8Ssetje 
22837c478bd9Sstevel@tonic-gate 	/*
228448847494SEnrico Perla - Sun Microsystems 	 * On SPARC we create a -path-list file for mkisofs
228548847494SEnrico Perla - Sun Microsystems 	 */
228648847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET) && !bam_nowrite()) {
228748847494SEnrico Perla - Sun Microsystems 		if (flags != FTW_D) {
228848847494SEnrico Perla - Sun Microsystems 			char	*strip;
228948847494SEnrico Perla - Sun Microsystems 
229048847494SEnrico Perla - Sun Microsystems 			strip = (char *)file + strlen(rootbuf);
229148847494SEnrico Perla - Sun Microsystems 			(void) fprintf(walk_arg.sparcfile, "/%s=%s\n", strip,
229248847494SEnrico Perla - Sun Microsystems 			    file);
229348847494SEnrico Perla - Sun Microsystems 		}
229448847494SEnrico Perla - Sun Microsystems 	}
229548847494SEnrico Perla - Sun Microsystems 
229648847494SEnrico Perla - Sun Microsystems 	/*
229748847494SEnrico Perla - Sun Microsystems 	 * We are transitioning from the old model to the dircache or the cache
229848847494SEnrico Perla - Sun Microsystems 	 * directory was removed: create the entry without further checkings.
229948847494SEnrico Perla - Sun Microsystems 	 */
230048847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(NEED_CACHE_DIR)) {
230148847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
230248847494SEnrico Perla - Sun Microsystems 			bam_print(PARSEABLE_NEW_FILE, file);
230348847494SEnrico Perla - Sun Microsystems 
230448847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(IS_SPARC_TARGET)) {
230548847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
230648847494SEnrico Perla - Sun Microsystems 			return (0);
230748847494SEnrico Perla - Sun Microsystems 		}
230848847494SEnrico Perla - Sun Microsystems 
230948847494SEnrico Perla - Sun Microsystems 		ret = update_dircache(file, flags);
231048847494SEnrico Perla - Sun Microsystems 		if (ret == BAM_ERROR) {
231148847494SEnrico Perla - Sun Microsystems 			bam_error(UPDT_CACHE_FAIL, file);
231248847494SEnrico Perla - Sun Microsystems 			return (-1);
231348847494SEnrico Perla - Sun Microsystems 		}
231448847494SEnrico Perla - Sun Microsystems 
231548847494SEnrico Perla - Sun Microsystems 		return (0);
231648847494SEnrico Perla - Sun Microsystems 	}
231748847494SEnrico Perla - Sun Microsystems 
231848847494SEnrico Perla - Sun Microsystems 	/*
23197c478bd9Sstevel@tonic-gate 	 * We need an update if file doesn't exist in old archive
23207c478bd9Sstevel@tonic-gate 	 */
23217c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp == NULL ||
23227c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint64_array(walk_arg.old_nvlp,
23237c478bd9Sstevel@tonic-gate 	    file + bam_rootlen, &value, &sz) != 0) {
23247c478bd9Sstevel@tonic-gate 		if (bam_smf_check)	/* ignore new during smf check */
23257c478bd9Sstevel@tonic-gate 			return (0);
232648847494SEnrico Perla - Sun Microsystems 
232748847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(IS_SPARC_TARGET)) {
232848847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
232948847494SEnrico Perla - Sun Microsystems 		} else {
233048847494SEnrico Perla - Sun Microsystems 			ret = update_dircache(file, flags);
233148847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_ERROR) {
233248847494SEnrico Perla - Sun Microsystems 				bam_error(UPDT_CACHE_FAIL, file);
233348847494SEnrico Perla - Sun Microsystems 				return (-1);
233448847494SEnrico Perla - Sun Microsystems 			}
233548847494SEnrico Perla - Sun Microsystems 		}
233648847494SEnrico Perla - Sun Microsystems 
23377c478bd9Sstevel@tonic-gate 		if (bam_verbose)
23387c478bd9Sstevel@tonic-gate 			bam_print(PARSEABLE_NEW_FILE, file);
23397c478bd9Sstevel@tonic-gate 		return (0);
23407c478bd9Sstevel@tonic-gate 	}
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 	/*
234348847494SEnrico Perla - Sun Microsystems 	 * If we got there, the file is already listed as to be included in the
234448847494SEnrico Perla - Sun Microsystems 	 * iso image. We just need to know if we are going to rebuild it or not
234548847494SEnrico Perla - Sun Microsystems 	 */
234648847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET) &&
2347cedc7e57SEnrico Perla - Sun Microsystems 	    is_dir_flag_on(FILE64, NEED_UPDATE) && !bam_nowrite())
234848847494SEnrico Perla - Sun Microsystems 		return (0);
234948847494SEnrico Perla - Sun Microsystems 	/*
23507c478bd9Sstevel@tonic-gate 	 * File exists in old archive. Check if file has changed
23517c478bd9Sstevel@tonic-gate 	 */
23527c478bd9Sstevel@tonic-gate 	assert(sz == 2);
23537c478bd9Sstevel@tonic-gate 	bcopy(value, filestat, sizeof (filestat));
23547c478bd9Sstevel@tonic-gate 
235548847494SEnrico Perla - Sun Microsystems 	if (flags != FTW_D && (filestat[0] != st->st_size ||
235648847494SEnrico Perla - Sun Microsystems 	    filestat[1] != st->st_mtime)) {
23577c609327Ssetje 		if (bam_smf_check) {
23587c609327Ssetje 			safefilep = safefiles;
235989c3ee43SGangadhar Mylapuram 			while (safefilep != NULL &&
236089c3ee43SGangadhar Mylapuram 			    safefilep->name[0] != '\0') {
236189c3ee43SGangadhar Mylapuram 				if (regcomp(&re, safefilep->name,
236289c3ee43SGangadhar Mylapuram 				    REG_EXTENDED|REG_NOSUB) == 0) {
236389c3ee43SGangadhar Mylapuram 					status = regexec(&re,
236489c3ee43SGangadhar Mylapuram 					    file + bam_rootlen, 0, NULL, 0);
236589c3ee43SGangadhar Mylapuram 					regfree(&re);
236689c3ee43SGangadhar Mylapuram 					if (status == 0) {
23673b133becSGangadhar Mylapuram 						(void) creat(
23683b133becSGangadhar Mylapuram 						    NEED_UPDATE_SAFE_FILE,
236989c3ee43SGangadhar Mylapuram 						    0644);
23707c609327Ssetje 						return (0);
23717c609327Ssetje 					}
237289c3ee43SGangadhar Mylapuram 				}
23737c609327Ssetje 				safefilep = safefilep->next;
23747c609327Ssetje 			}
23757c609327Ssetje 		}
237648847494SEnrico Perla - Sun Microsystems 
237748847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(IS_SPARC_TARGET)) {
237848847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
237948847494SEnrico Perla - Sun Microsystems 		} else {
238048847494SEnrico Perla - Sun Microsystems 			ret = update_dircache(file, flags);
238148847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_ERROR) {
238248847494SEnrico Perla - Sun Microsystems 				bam_error(UPDT_CACHE_FAIL, file);
238348847494SEnrico Perla - Sun Microsystems 				return (-1);
238448847494SEnrico Perla - Sun Microsystems 			}
238548847494SEnrico Perla - Sun Microsystems 		}
238648847494SEnrico Perla - Sun Microsystems 
23877c478bd9Sstevel@tonic-gate 		if (bam_verbose)
23887c478bd9Sstevel@tonic-gate 			if (bam_smf_check)
23897c478bd9Sstevel@tonic-gate 				bam_print("    %s\n", file);
23907c478bd9Sstevel@tonic-gate 			else
23917c478bd9Sstevel@tonic-gate 				bam_print(PARSEABLE_OUT_DATE, file);
23927c478bd9Sstevel@tonic-gate 	}
23937c478bd9Sstevel@tonic-gate 
23947c478bd9Sstevel@tonic-gate 	return (0);
23957c478bd9Sstevel@tonic-gate }
23967c478bd9Sstevel@tonic-gate 
23977c478bd9Sstevel@tonic-gate /*
239848847494SEnrico Perla - Sun Microsystems  * Remove a directory path recursively
239948847494SEnrico Perla - Sun Microsystems  */
240048847494SEnrico Perla - Sun Microsystems static int
240148847494SEnrico Perla - Sun Microsystems rmdir_r(char *path)
240248847494SEnrico Perla - Sun Microsystems {
240348847494SEnrico Perla - Sun Microsystems 	struct dirent 	*d = NULL;
240448847494SEnrico Perla - Sun Microsystems 	DIR 		*dir = NULL;
240548847494SEnrico Perla - Sun Microsystems 	char 		tpath[PATH_MAX];
240648847494SEnrico Perla - Sun Microsystems 	struct stat 	sb;
240748847494SEnrico Perla - Sun Microsystems 
240848847494SEnrico Perla - Sun Microsystems 	if ((dir = opendir(path)) == NULL)
240948847494SEnrico Perla - Sun Microsystems 		return (-1);
241048847494SEnrico Perla - Sun Microsystems 
241148847494SEnrico Perla - Sun Microsystems 	while (d = readdir(dir)) {
241248847494SEnrico Perla - Sun Microsystems 		if ((strcmp(d->d_name, ".") != 0) &&
241348847494SEnrico Perla - Sun Microsystems 		    (strcmp(d->d_name, "..") != 0)) {
241448847494SEnrico Perla - Sun Microsystems 			(void) snprintf(tpath, sizeof (tpath), "%s/%s",
241548847494SEnrico Perla - Sun Microsystems 			    path, d->d_name);
241648847494SEnrico Perla - Sun Microsystems 			if (stat(tpath, &sb) == 0) {
241748847494SEnrico Perla - Sun Microsystems 				if (sb.st_mode & S_IFDIR)
241848847494SEnrico Perla - Sun Microsystems 					(void) rmdir_r(tpath);
241948847494SEnrico Perla - Sun Microsystems 				else
242048847494SEnrico Perla - Sun Microsystems 					(void) remove(tpath);
242148847494SEnrico Perla - Sun Microsystems 			}
242248847494SEnrico Perla - Sun Microsystems 		}
242348847494SEnrico Perla - Sun Microsystems 	}
242448847494SEnrico Perla - Sun Microsystems 	return (remove(path));
242548847494SEnrico Perla - Sun Microsystems }
242648847494SEnrico Perla - Sun Microsystems 
242748847494SEnrico Perla - Sun Microsystems /*
242848847494SEnrico Perla - Sun Microsystems  * Check if cache directory exists and, if not, create it and update flags
242948847494SEnrico Perla - Sun Microsystems  * accordingly. If the path exists, but it's not a directory, a best effort
243048847494SEnrico Perla - Sun Microsystems  * attempt to remove and recreate it is made.
243148847494SEnrico Perla - Sun Microsystems  * If the user requested a 'purge', always recreate the directory from scratch.
243248847494SEnrico Perla - Sun Microsystems  */
243348847494SEnrico Perla - Sun Microsystems static int
243448847494SEnrico Perla - Sun Microsystems set_cache_dir(char *root, int what)
243548847494SEnrico Perla - Sun Microsystems {
243648847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
243748847494SEnrico Perla - Sun Microsystems 	int		ret = 0;
243848847494SEnrico Perla - Sun Microsystems 
243948847494SEnrico Perla - Sun Microsystems 	ret = snprintf(get_cachedir(what), sizeof (get_cachedir(what)),
244048847494SEnrico Perla - Sun Microsystems 	    "%s%s%s%s%s", root, ARCHIVE_PREFIX, get_machine(), what == FILE64 ?
244148847494SEnrico Perla - Sun Microsystems 	    "/amd64" : "", CACHEDIR_SUFFIX);
244248847494SEnrico Perla - Sun Microsystems 
244348847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (get_cachedir(what))) {
244448847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
244548847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
244648847494SEnrico Perla - Sun Microsystems 	}
244748847494SEnrico Perla - Sun Microsystems 
24484a9df875SEnrico Perla - Sun Microsystems 	if (bam_purge || is_flag_on(INVALIDATE_CACHE))
244948847494SEnrico Perla - Sun Microsystems 		(void) rmdir_r(get_cachedir(what));
245048847494SEnrico Perla - Sun Microsystems 
245148847494SEnrico Perla - Sun Microsystems 	if (stat(get_cachedir(what), &sb) != 0 || !(S_ISDIR(sb.st_mode))) {
245248847494SEnrico Perla - Sun Microsystems 		/* best effort unlink attempt, mkdir will catch errors */
245348847494SEnrico Perla - Sun Microsystems 		(void) unlink(get_cachedir(what));
245448847494SEnrico Perla - Sun Microsystems 
245548847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
245648847494SEnrico Perla - Sun Microsystems 			bam_print(UPDATE_CDIR_MISS, get_cachedir(what));
245748847494SEnrico Perla - Sun Microsystems 		ret = mkdir(get_cachedir(what), DIR_PERMS);
245848847494SEnrico Perla - Sun Microsystems 		if (ret < 0) {
245948847494SEnrico Perla - Sun Microsystems 			bam_error(MKDIR_FAILED, get_cachedir(what),
246048847494SEnrico Perla - Sun Microsystems 			    strerror(errno));
246148847494SEnrico Perla - Sun Microsystems 			get_cachedir(what)[0] = '\0';
246248847494SEnrico Perla - Sun Microsystems 			return (ret);
246348847494SEnrico Perla - Sun Microsystems 		}
246448847494SEnrico Perla - Sun Microsystems 		set_flag(NEED_CACHE_DIR);
246548847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
246648847494SEnrico Perla - Sun Microsystems 	}
246748847494SEnrico Perla - Sun Microsystems 
246848847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
246948847494SEnrico Perla - Sun Microsystems }
247048847494SEnrico Perla - Sun Microsystems 
247148847494SEnrico Perla - Sun Microsystems static int
247248847494SEnrico Perla - Sun Microsystems set_update_dir(char *root, int what)
247348847494SEnrico Perla - Sun Microsystems {
247448847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
247548847494SEnrico Perla - Sun Microsystems 	int		ret;
247648847494SEnrico Perla - Sun Microsystems 
247748847494SEnrico Perla - Sun Microsystems 	if (is_dir_flag_on(what, NO_MULTI))
247848847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
247948847494SEnrico Perla - Sun Microsystems 
248048847494SEnrico Perla - Sun Microsystems 	if (!bam_extend) {
248148847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
248248847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
248348847494SEnrico Perla - Sun Microsystems 	}
248448847494SEnrico Perla - Sun Microsystems 
248548847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
248648847494SEnrico Perla - Sun Microsystems 		ret = snprintf(get_updatedir(what),
248748847494SEnrico Perla - Sun Microsystems 		    sizeof (get_updatedir(what)), "%s%s%s/amd64%s", root,
248848847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_PREFIX, get_machine(), UPDATEDIR_SUFFIX);
248948847494SEnrico Perla - Sun Microsystems 	else
249048847494SEnrico Perla - Sun Microsystems 		ret = snprintf(get_updatedir(what),
249148847494SEnrico Perla - Sun Microsystems 		    sizeof (get_updatedir(what)), "%s%s%s%s", root,
249248847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_PREFIX, get_machine(), UPDATEDIR_SUFFIX);
249348847494SEnrico Perla - Sun Microsystems 
249448847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (get_updatedir(what))) {
249548847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
249648847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
249748847494SEnrico Perla - Sun Microsystems 	}
249848847494SEnrico Perla - Sun Microsystems 
249948847494SEnrico Perla - Sun Microsystems 	if (stat(get_updatedir(what), &sb) == 0) {
250048847494SEnrico Perla - Sun Microsystems 		if (S_ISDIR(sb.st_mode))
250148847494SEnrico Perla - Sun Microsystems 			ret = rmdir_r(get_updatedir(what));
250248847494SEnrico Perla - Sun Microsystems 		else
250348847494SEnrico Perla - Sun Microsystems 			ret = unlink(get_updatedir(what));
250448847494SEnrico Perla - Sun Microsystems 
250548847494SEnrico Perla - Sun Microsystems 		if (ret != 0)
250648847494SEnrico Perla - Sun Microsystems 			set_dir_flag(what, NO_MULTI);
250748847494SEnrico Perla - Sun Microsystems 	}
250848847494SEnrico Perla - Sun Microsystems 
250948847494SEnrico Perla - Sun Microsystems 	if (mkdir(get_updatedir(what), DIR_PERMS) < 0)
251048847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
251148847494SEnrico Perla - Sun Microsystems 
251248847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
251348847494SEnrico Perla - Sun Microsystems }
251448847494SEnrico Perla - Sun Microsystems 
251548847494SEnrico Perla - Sun Microsystems static int
251648847494SEnrico Perla - Sun Microsystems is_valid_archive(char *root, int what)
251748847494SEnrico Perla - Sun Microsystems {
251848847494SEnrico Perla - Sun Microsystems 	char 		archive_path[PATH_MAX];
25194a9df875SEnrico Perla - Sun Microsystems 	char		timestamp_path[PATH_MAX];
25204a9df875SEnrico Perla - Sun Microsystems 	struct stat 	sb, timestamp;
252148847494SEnrico Perla - Sun Microsystems 	int 		ret;
252248847494SEnrico Perla - Sun Microsystems 
252348847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
252448847494SEnrico Perla - Sun Microsystems 		ret = snprintf(archive_path, sizeof (archive_path),
252548847494SEnrico Perla - Sun Microsystems 		    "%s%s%s/amd64%s", root, ARCHIVE_PREFIX, get_machine(),
252648847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_SUFFIX);
252748847494SEnrico Perla - Sun Microsystems 	else
252848847494SEnrico Perla - Sun Microsystems 		ret = snprintf(archive_path, sizeof (archive_path), "%s%s%s%s",
252948847494SEnrico Perla - Sun Microsystems 		    root, ARCHIVE_PREFIX, get_machine(), ARCHIVE_SUFFIX);
253048847494SEnrico Perla - Sun Microsystems 
253148847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (archive_path)) {
253248847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
253348847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
253448847494SEnrico Perla - Sun Microsystems 	}
253548847494SEnrico Perla - Sun Microsystems 
253648847494SEnrico Perla - Sun Microsystems 	if (stat(archive_path, &sb) != 0) {
253748847494SEnrico Perla - Sun Microsystems 		if (bam_verbose && !bam_check)
253848847494SEnrico Perla - Sun Microsystems 			bam_print(UPDATE_ARCH_MISS, archive_path);
253948847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NEED_UPDATE);
254048847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
254148847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
254248847494SEnrico Perla - Sun Microsystems 	}
254348847494SEnrico Perla - Sun Microsystems 
25444a9df875SEnrico Perla - Sun Microsystems 	/*
25454a9df875SEnrico Perla - Sun Microsystems 	 * The timestamp file is used to prevent stale files in the archive
25464a9df875SEnrico Perla - Sun Microsystems 	 * cache.
25474a9df875SEnrico Perla - Sun Microsystems 	 * Stale files can happen if the system is booted back and forth across
25484a9df875SEnrico Perla - Sun Microsystems 	 * the transition from bootadm-before-the-cache to
25494a9df875SEnrico Perla - Sun Microsystems 	 * bootadm-after-the-cache, since older versions of bootadm don't know
25504a9df875SEnrico Perla - Sun Microsystems 	 * about the existence of the archive cache.
25514a9df875SEnrico Perla - Sun Microsystems 	 *
25524a9df875SEnrico Perla - Sun Microsystems 	 * Since only bootadm-after-the-cache versions know about about this
25534a9df875SEnrico Perla - Sun Microsystems 	 * file, we require that the boot archive be older than this file.
25544a9df875SEnrico Perla - Sun Microsystems 	 */
25554a9df875SEnrico Perla - Sun Microsystems 	ret = snprintf(timestamp_path, sizeof (timestamp_path), "%s%s", root,
25564a9df875SEnrico Perla - Sun Microsystems 	    FILE_STAT_TIMESTAMP);
25574a9df875SEnrico Perla - Sun Microsystems 
25584a9df875SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (timestamp_path)) {
25594a9df875SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
25604a9df875SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
25614a9df875SEnrico Perla - Sun Microsystems 	}
25624a9df875SEnrico Perla - Sun Microsystems 
25634a9df875SEnrico Perla - Sun Microsystems 	if (stat(timestamp_path, &timestamp) != 0 ||
25644a9df875SEnrico Perla - Sun Microsystems 	    sb.st_mtime > timestamp.st_mtime) {
25654a9df875SEnrico Perla - Sun Microsystems 		if (bam_verbose && !bam_check)
256618961ad2SToomas Soome 			bam_print(UPDATE_CACHE_OLD);
25674a9df875SEnrico Perla - Sun Microsystems 		/*
25684a9df875SEnrico Perla - Sun Microsystems 		 * Don't generate a false positive for the boot-archive service
25694a9df875SEnrico Perla - Sun Microsystems 		 * but trigger an update of the archive cache in
25704a9df875SEnrico Perla - Sun Microsystems 		 * boot-archive-update.
25714a9df875SEnrico Perla - Sun Microsystems 		 */
25724a9df875SEnrico Perla - Sun Microsystems 		if (bam_smf_check) {
25734a9df875SEnrico Perla - Sun Microsystems 			(void) creat(NEED_UPDATE_FILE, 0644);
25744a9df875SEnrico Perla - Sun Microsystems 			return (BAM_SUCCESS);
25754a9df875SEnrico Perla - Sun Microsystems 		}
25764a9df875SEnrico Perla - Sun Microsystems 
25774a9df875SEnrico Perla - Sun Microsystems 		set_flag(INVALIDATE_CACHE);
25784a9df875SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NEED_UPDATE);
25794a9df875SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
25804a9df875SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
25814a9df875SEnrico Perla - Sun Microsystems 	}
25824a9df875SEnrico Perla - Sun Microsystems 
258348847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET))
258448847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
258548847494SEnrico Perla - Sun Microsystems 
258648847494SEnrico Perla - Sun Microsystems 	if (bam_extend && sb.st_size > BA_SIZE_MAX) {
258748847494SEnrico Perla - Sun Microsystems 		if (bam_verbose && !bam_check)
258848847494SEnrico Perla - Sun Microsystems 			bam_print(MULTI_SIZE, archive_path, BA_SIZE_MAX);
258948847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
259048847494SEnrico Perla - Sun Microsystems 	}
259148847494SEnrico Perla - Sun Microsystems 
259248847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
259348847494SEnrico Perla - Sun Microsystems }
259448847494SEnrico Perla - Sun Microsystems 
259548847494SEnrico Perla - Sun Microsystems /*
259648847494SEnrico Perla - Sun Microsystems  * Check flags and presence of required files and directories.
25977c478bd9Sstevel@tonic-gate  * The force flag and/or absence of files should
25987c478bd9Sstevel@tonic-gate  * trigger an update.
25997c478bd9Sstevel@tonic-gate  * Suppress stdout output if check (-n) option is set
26007c478bd9Sstevel@tonic-gate  * (as -n should only produce parseable output.)
26017c478bd9Sstevel@tonic-gate  */
260248847494SEnrico Perla - Sun Microsystems static int
26037c478bd9Sstevel@tonic-gate check_flags_and_files(char *root)
26047c478bd9Sstevel@tonic-gate {
260548847494SEnrico Perla - Sun Microsystems 
26067c478bd9Sstevel@tonic-gate 	struct stat 	sb;
260748847494SEnrico Perla - Sun Microsystems 	int 		ret;
260848847494SEnrico Perla - Sun Microsystems 
260948847494SEnrico Perla - Sun Microsystems 	/*
261048847494SEnrico Perla - Sun Microsystems 	 * If archive is missing, create archive
261148847494SEnrico Perla - Sun Microsystems 	 */
261248847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET)) {
261348847494SEnrico Perla - Sun Microsystems 		ret = is_valid_archive(root, FILE64);
2614cedc7e57SEnrico Perla - Sun Microsystems 		if (ret == BAM_ERROR)
2615cedc7e57SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
261648847494SEnrico Perla - Sun Microsystems 	} else {
261748847494SEnrico Perla - Sun Microsystems 		int	what = FILE32;
261848847494SEnrico Perla - Sun Microsystems 		do {
261948847494SEnrico Perla - Sun Microsystems 			ret = is_valid_archive(root, what);
262048847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_ERROR)
262148847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
262248847494SEnrico Perla - Sun Microsystems 			what++;
262348847494SEnrico Perla - Sun Microsystems 		} while (bam_direct == BAM_DIRECT_DBOOT && what < CACHEDIR_NUM);
262448847494SEnrico Perla - Sun Microsystems 	}
262548847494SEnrico Perla - Sun Microsystems 
262648847494SEnrico Perla - Sun Microsystems 	if (bam_nowrite())
262748847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
262848847494SEnrico Perla - Sun Microsystems 
262948847494SEnrico Perla - Sun Microsystems 
263048847494SEnrico Perla - Sun Microsystems 	/*
263148847494SEnrico Perla - Sun Microsystems 	 * check if cache directories exist on x86.
263248847494SEnrico Perla - Sun Microsystems 	 * check (and always open) the cache file on SPARC.
263348847494SEnrico Perla - Sun Microsystems 	 */
263448847494SEnrico Perla - Sun Microsystems 	if (is_sparc()) {
263548847494SEnrico Perla - Sun Microsystems 		ret = snprintf(get_cachedir(FILE64),
263648847494SEnrico Perla - Sun Microsystems 		    sizeof (get_cachedir(FILE64)), "%s%s%s/%s", root,
263748847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_PREFIX, get_machine(), CACHEDIR_SUFFIX);
263848847494SEnrico Perla - Sun Microsystems 
263948847494SEnrico Perla - Sun Microsystems 		if (ret >= sizeof (get_cachedir(FILE64))) {
264048847494SEnrico Perla - Sun Microsystems 			bam_error(PATH_TOO_LONG, rootbuf);
264148847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
264248847494SEnrico Perla - Sun Microsystems 		}
264348847494SEnrico Perla - Sun Microsystems 
264448847494SEnrico Perla - Sun Microsystems 		if (stat(get_cachedir(FILE64), &sb) != 0) {
264548847494SEnrico Perla - Sun Microsystems 			set_flag(NEED_CACHE_DIR);
264648847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
264748847494SEnrico Perla - Sun Microsystems 		}
264848847494SEnrico Perla - Sun Microsystems 
264948847494SEnrico Perla - Sun Microsystems 		walk_arg.sparcfile = fopen(get_cachedir(FILE64), "w");
265048847494SEnrico Perla - Sun Microsystems 		if (walk_arg.sparcfile == NULL) {
265148847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, get_cachedir(FILE64),
265248847494SEnrico Perla - Sun Microsystems 			    strerror(errno));
265348847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
265448847494SEnrico Perla - Sun Microsystems 		}
265548847494SEnrico Perla - Sun Microsystems 
265648847494SEnrico Perla - Sun Microsystems 		set_dir_present(FILE64);
265748847494SEnrico Perla - Sun Microsystems 	} else {
265848847494SEnrico Perla - Sun Microsystems 		int	what = FILE32;
265948847494SEnrico Perla - Sun Microsystems 
266048847494SEnrico Perla - Sun Microsystems 		do {
266148847494SEnrico Perla - Sun Microsystems 			if (set_cache_dir(root, what) != 0)
266248847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
266348847494SEnrico Perla - Sun Microsystems 
266448847494SEnrico Perla - Sun Microsystems 			set_dir_present(what);
266548847494SEnrico Perla - Sun Microsystems 
266648847494SEnrico Perla - Sun Microsystems 			if (set_update_dir(root, what) != 0)
266748847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
266848847494SEnrico Perla - Sun Microsystems 			what++;
266948847494SEnrico Perla - Sun Microsystems 		} while (bam_direct == BAM_DIRECT_DBOOT && what < CACHEDIR_NUM);
267048847494SEnrico Perla - Sun Microsystems 	}
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate 	/*
26737c478bd9Sstevel@tonic-gate 	 * if force, create archive unconditionally
26747c478bd9Sstevel@tonic-gate 	 */
26757c478bd9Sstevel@tonic-gate 	if (bam_force) {
267648847494SEnrico Perla - Sun Microsystems 		if (!is_sparc())
267748847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE32, NEED_UPDATE);
267848847494SEnrico Perla - Sun Microsystems 		set_dir_flag(FILE64, NEED_UPDATE);
267948847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
26807c478bd9Sstevel@tonic-gate 			bam_print(UPDATE_FORCE);
268148847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
26827c478bd9Sstevel@tonic-gate 	}
26837c478bd9Sstevel@tonic-gate 
268448847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
26857c478bd9Sstevel@tonic-gate }
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate static error_t
26887c478bd9Sstevel@tonic-gate read_one_list(char *root, filelist_t  *flistp, char *filelist)
26897c478bd9Sstevel@tonic-gate {
26907c478bd9Sstevel@tonic-gate 	char 		path[PATH_MAX];
26917c478bd9Sstevel@tonic-gate 	FILE 		*fp;
26927c478bd9Sstevel@tonic-gate 	char 		buf[BAM_MAXLINE];
2693eb2bd662Svikram 	const char 	*fcn = "read_one_list()";
26947c478bd9Sstevel@tonic-gate 
26957c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, filelist);
26967c478bd9Sstevel@tonic-gate 
26977c478bd9Sstevel@tonic-gate 	fp = fopen(path, "r");
26987c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
2699eb2bd662Svikram 		BAM_DPRINTF((D_FLIST_FAIL, fcn, path, strerror(errno)));
27007c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27017c478bd9Sstevel@tonic-gate 	}
27027c478bd9Sstevel@tonic-gate 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
2703b610f78eSvikram 		/* skip blank lines */
2704b610f78eSvikram 		if (strspn(buf, " \t") == strlen(buf))
2705b610f78eSvikram 			continue;
27067c478bd9Sstevel@tonic-gate 		append_to_flist(flistp, buf);
27077c478bd9Sstevel@tonic-gate 	}
27087c478bd9Sstevel@tonic-gate 	if (fclose(fp) != 0) {
27097c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, path, strerror(errno));
27107c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27117c478bd9Sstevel@tonic-gate 	}
27127c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
27137c478bd9Sstevel@tonic-gate }
27147c478bd9Sstevel@tonic-gate 
27157c478bd9Sstevel@tonic-gate static error_t
27167c478bd9Sstevel@tonic-gate read_list(char *root, filelist_t  *flistp)
27177c478bd9Sstevel@tonic-gate {
2718986fd29aSsetje 	char 		path[PATH_MAX];
2719986fd29aSsetje 	char 		cmd[PATH_MAX];
2720986fd29aSsetje 	struct stat 	sb;
2721986fd29aSsetje 	int 		n, rval;
2722eb2bd662Svikram 	const char 	*fcn = "read_list()";
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
27257c478bd9Sstevel@tonic-gate 
27267c478bd9Sstevel@tonic-gate 	/*
2727986fd29aSsetje 	 * build and check path to extract_boot_filelist.ksh
2728986fd29aSsetje 	 */
2729986fd29aSsetje 	n = snprintf(path, sizeof (path), "%s%s", root, EXTRACT_BOOT_FILELIST);
2730986fd29aSsetje 	if (n >= sizeof (path)) {
2731986fd29aSsetje 		bam_error(NO_FLIST);
2732986fd29aSsetje 		return (BAM_ERROR);
2733986fd29aSsetje 	}
2734986fd29aSsetje 
2735cedc7e57SEnrico Perla - Sun Microsystems 	if (is_safe_exec(path) == BAM_ERROR)
2736cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
2737cedc7e57SEnrico Perla - Sun Microsystems 
2738986fd29aSsetje 	/*
2739986fd29aSsetje 	 * If extract_boot_filelist is present, exec it, otherwise read
2740986fd29aSsetje 	 * the filelists directly, for compatibility with older images.
2741986fd29aSsetje 	 */
2742986fd29aSsetje 	if (stat(path, &sb) == 0) {
2743986fd29aSsetje 		/*
2744986fd29aSsetje 		 * build arguments to exec extract_boot_filelist.ksh
2745986fd29aSsetje 		 */
2746d876c67dSjg 		char *rootarg, *platarg;
2747d876c67dSjg 		int platarglen = 1, rootarglen = 1;
2748d876c67dSjg 		if (strlen(root) > 1)
2749d876c67dSjg 			rootarglen += strlen(root) + strlen("-R ");
2750d876c67dSjg 		if (bam_alt_platform)
2751d876c67dSjg 			platarglen += strlen(bam_platform) + strlen("-p ");
2752d876c67dSjg 		platarg = s_calloc(1, platarglen);
2753d876c67dSjg 		rootarg = s_calloc(1, rootarglen);
2754d876c67dSjg 		*platarg = 0;
2755d876c67dSjg 		*rootarg = 0;
2756d876c67dSjg 
2757986fd29aSsetje 		if (strlen(root) > 1) {
2758d876c67dSjg 			(void) snprintf(rootarg, rootarglen,
2759d876c67dSjg 			    "-R %s", root);
2760986fd29aSsetje 		}
2761d876c67dSjg 		if (bam_alt_platform) {
2762d876c67dSjg 			(void) snprintf(platarg, platarglen,
2763d876c67dSjg 			    "-p %s", bam_platform);
2764d876c67dSjg 		}
2765d876c67dSjg 		n = snprintf(cmd, sizeof (cmd), "%s %s %s /%s /%s",
2766d876c67dSjg 		    path, rootarg, platarg, BOOT_FILE_LIST, ETC_FILE_LIST);
2767d876c67dSjg 		free(platarg);
2768d876c67dSjg 		free(rootarg);
2769986fd29aSsetje 		if (n >= sizeof (cmd)) {
2770986fd29aSsetje 			bam_error(NO_FLIST);
2771986fd29aSsetje 			return (BAM_ERROR);
2772986fd29aSsetje 		}
2773986fd29aSsetje 		if (exec_cmd(cmd, flistp) != 0) {
2774eb2bd662Svikram 			BAM_DPRINTF((D_FLIST_FAIL, fcn, path, strerror(errno)));
2775986fd29aSsetje 			return (BAM_ERROR);
2776986fd29aSsetje 		}
2777986fd29aSsetje 	} else {
2778986fd29aSsetje 		/*
27797c478bd9Sstevel@tonic-gate 		 * Read current lists of files - only the first is mandatory
27807c478bd9Sstevel@tonic-gate 		 */
27817c478bd9Sstevel@tonic-gate 		rval = read_one_list(root, flistp, BOOT_FILE_LIST);
27827c478bd9Sstevel@tonic-gate 		if (rval != BAM_SUCCESS)
27837c478bd9Sstevel@tonic-gate 			return (rval);
27847c478bd9Sstevel@tonic-gate 		(void) read_one_list(root, flistp, ETC_FILE_LIST);
2785986fd29aSsetje 	}
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL) {
27887c478bd9Sstevel@tonic-gate 		bam_error(NO_FLIST);
27897c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27907c478bd9Sstevel@tonic-gate 	}
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
27937c478bd9Sstevel@tonic-gate }
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate static void
27967c478bd9Sstevel@tonic-gate getoldstat(char *root)
27977c478bd9Sstevel@tonic-gate {
27987c478bd9Sstevel@tonic-gate 	char 		path[PATH_MAX];
27997c478bd9Sstevel@tonic-gate 	int 		fd, error;
28007c478bd9Sstevel@tonic-gate 	struct stat 	sb;
28017c478bd9Sstevel@tonic-gate 	char 		*ostat;
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
28047c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDONLY);
28057c478bd9Sstevel@tonic-gate 	if (fd == -1) {
28067c478bd9Sstevel@tonic-gate 		if (bam_verbose)
28077c478bd9Sstevel@tonic-gate 			bam_print(OPEN_FAIL, path, strerror(errno));
280848847494SEnrico Perla - Sun Microsystems 		goto out_err;
28097c478bd9Sstevel@tonic-gate 	}
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate 	if (fstat(fd, &sb) != 0) {
28127c478bd9Sstevel@tonic-gate 		bam_error(STAT_FAIL, path, strerror(errno));
281348847494SEnrico Perla - Sun Microsystems 		goto out_err;
28147c478bd9Sstevel@tonic-gate 	}
28157c478bd9Sstevel@tonic-gate 
28167c478bd9Sstevel@tonic-gate 	ostat = s_calloc(1, sb.st_size);
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate 	if (read(fd, ostat, sb.st_size) != sb.st_size) {
28197c478bd9Sstevel@tonic-gate 		bam_error(READ_FAIL, path, strerror(errno));
28207c478bd9Sstevel@tonic-gate 		free(ostat);
282148847494SEnrico Perla - Sun Microsystems 		goto out_err;
28227c478bd9Sstevel@tonic-gate 	}
28237c478bd9Sstevel@tonic-gate 
28247c478bd9Sstevel@tonic-gate 	(void) close(fd);
282548847494SEnrico Perla - Sun Microsystems 	fd = -1;
28267c478bd9Sstevel@tonic-gate 
28277c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
28287c478bd9Sstevel@tonic-gate 	error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0);
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate 	free(ostat);
28317c478bd9Sstevel@tonic-gate 
28327c478bd9Sstevel@tonic-gate 	if (error) {
28337c478bd9Sstevel@tonic-gate 		bam_error(UNPACK_FAIL, path, strerror(error));
28347c478bd9Sstevel@tonic-gate 		walk_arg.old_nvlp = NULL;
283548847494SEnrico Perla - Sun Microsystems 		goto out_err;
283648847494SEnrico Perla - Sun Microsystems 	} else {
28377c478bd9Sstevel@tonic-gate 		return;
28387c478bd9Sstevel@tonic-gate 	}
283948847494SEnrico Perla - Sun Microsystems 
284048847494SEnrico Perla - Sun Microsystems out_err:
284148847494SEnrico Perla - Sun Microsystems 	if (fd != -1)
284248847494SEnrico Perla - Sun Microsystems 		(void) close(fd);
2843cedc7e57SEnrico Perla - Sun Microsystems 	if (!is_flag_on(IS_SPARC_TARGET))
284448847494SEnrico Perla - Sun Microsystems 		set_dir_flag(FILE32, NEED_UPDATE);
284548847494SEnrico Perla - Sun Microsystems 	set_dir_flag(FILE64, NEED_UPDATE);
284648847494SEnrico Perla - Sun Microsystems }
284748847494SEnrico Perla - Sun Microsystems 
284848847494SEnrico Perla - Sun Microsystems /* Best effort stale entry removal */
284948847494SEnrico Perla - Sun Microsystems static void
285048847494SEnrico Perla - Sun Microsystems delete_stale(char *file, int what)
285148847494SEnrico Perla - Sun Microsystems {
285248847494SEnrico Perla - Sun Microsystems 	char		path[PATH_MAX];
285348847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
285448847494SEnrico Perla - Sun Microsystems 
285548847494SEnrico Perla - Sun Microsystems 	(void) snprintf(path, sizeof (path), "%s/%s", get_cachedir(what), file);
285648847494SEnrico Perla - Sun Microsystems 	if (!bam_check && stat(path, &sb) == 0) {
285748847494SEnrico Perla - Sun Microsystems 		if (sb.st_mode & S_IFDIR)
285848847494SEnrico Perla - Sun Microsystems 			(void) rmdir_r(path);
285948847494SEnrico Perla - Sun Microsystems 		else
286048847494SEnrico Perla - Sun Microsystems 			(void) unlink(path);
286148847494SEnrico Perla - Sun Microsystems 
286248847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, (NEED_UPDATE | NO_MULTI));
286348847494SEnrico Perla - Sun Microsystems 	}
28647c478bd9Sstevel@tonic-gate }
28657c478bd9Sstevel@tonic-gate 
2866a28d77b8Svikram /*
2867a28d77b8Svikram  * Checks if a file in the current (old) archive has
2868a28d77b8Svikram  * been deleted from the root filesystem. This is needed for
2869a28d77b8Svikram  * software like Trusted Extensions (TX) that switch early
2870a28d77b8Svikram  * in boot based on presence/absence of a kernel module.
2871a28d77b8Svikram  */
2872a28d77b8Svikram static void
2873a28d77b8Svikram check4stale(char *root)
2874a28d77b8Svikram {
2875a28d77b8Svikram 	nvpair_t	*nvp;
2876a28d77b8Svikram 	nvlist_t	*nvlp;
2877a28d77b8Svikram 	char 		*file;
2878a28d77b8Svikram 	char		path[PATH_MAX];
2879a28d77b8Svikram 
2880a28d77b8Svikram 	/*
2881a28d77b8Svikram 	 * Skip stale file check during smf check
2882a28d77b8Svikram 	 */
2883a28d77b8Svikram 	if (bam_smf_check)
2884a28d77b8Svikram 		return;
2885a28d77b8Svikram 
288648847494SEnrico Perla - Sun Microsystems 	/*
288748847494SEnrico Perla - Sun Microsystems 	 * If we need to (re)create the cache, there's no need to check for
288848847494SEnrico Perla - Sun Microsystems 	 * stale files
288948847494SEnrico Perla - Sun Microsystems 	 */
289048847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(NEED_CACHE_DIR))
289148847494SEnrico Perla - Sun Microsystems 		return;
289248847494SEnrico Perla - Sun Microsystems 
2893a28d77b8Svikram 	/* Nothing to do if no old stats */
2894a28d77b8Svikram 	if ((nvlp = walk_arg.old_nvlp) == NULL)
2895a28d77b8Svikram 		return;
2896a28d77b8Svikram 
2897a28d77b8Svikram 	for (nvp = nvlist_next_nvpair(nvlp, NULL); nvp;
2898a28d77b8Svikram 	    nvp = nvlist_next_nvpair(nvlp, nvp)) {
2899a28d77b8Svikram 		file = nvpair_name(nvp);
2900a28d77b8Svikram 		if (file == NULL)
2901a28d77b8Svikram 			continue;
2902a28d77b8Svikram 		(void) snprintf(path, sizeof (path), "%s/%s",
2903a28d77b8Svikram 		    root, file);
290448847494SEnrico Perla - Sun Microsystems 		if (access(path, F_OK) < 0) {
290548847494SEnrico Perla - Sun Microsystems 			int	what;
290648847494SEnrico Perla - Sun Microsystems 
2907cedc7e57SEnrico Perla - Sun Microsystems 			if (bam_verbose)
2908cedc7e57SEnrico Perla - Sun Microsystems 				bam_print(PARSEABLE_STALE_FILE, path);
290948847494SEnrico Perla - Sun Microsystems 
2910cedc7e57SEnrico Perla - Sun Microsystems 			if (is_flag_on(IS_SPARC_TARGET)) {
2911cedc7e57SEnrico Perla - Sun Microsystems 				set_dir_flag(FILE64, NEED_UPDATE);
2912cedc7e57SEnrico Perla - Sun Microsystems 			} else {
291348847494SEnrico Perla - Sun Microsystems 				for (what = FILE32; what < CACHEDIR_NUM; what++)
291448847494SEnrico Perla - Sun Microsystems 					if (has_cachedir(what))
291548847494SEnrico Perla - Sun Microsystems 						delete_stale(file, what);
2916a28d77b8Svikram 			}
2917a28d77b8Svikram 		}
2918a28d77b8Svikram 	}
2919cedc7e57SEnrico Perla - Sun Microsystems }
2920a28d77b8Svikram 
29217c478bd9Sstevel@tonic-gate static void
29227c478bd9Sstevel@tonic-gate create_newstat(void)
29237c478bd9Sstevel@tonic-gate {
29247c478bd9Sstevel@tonic-gate 	int error;
29257c478bd9Sstevel@tonic-gate 
29267c478bd9Sstevel@tonic-gate 	error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0);
29277c478bd9Sstevel@tonic-gate 	if (error) {
29287c478bd9Sstevel@tonic-gate 		/*
29297c478bd9Sstevel@tonic-gate 		 * Not fatal - we can still create archive
29307c478bd9Sstevel@tonic-gate 		 */
29317c478bd9Sstevel@tonic-gate 		walk_arg.new_nvlp = NULL;
29327c478bd9Sstevel@tonic-gate 		bam_error(NVALLOC_FAIL, strerror(error));
29337c478bd9Sstevel@tonic-gate 	}
29347c478bd9Sstevel@tonic-gate }
29357c478bd9Sstevel@tonic-gate 
293648847494SEnrico Perla - Sun Microsystems static int
29377c478bd9Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp)
29387c478bd9Sstevel@tonic-gate {
29397c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
29407c478bd9Sstevel@tonic-gate 	line_t *lp;
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
2943986fd29aSsetje 		/*
2944986fd29aSsetje 		 * Don't follow symlinks.  A symlink must refer to
2945986fd29aSsetje 		 * a file that would appear in the archive through
2946986fd29aSsetje 		 * a direct reference.  This matches the archive
2947986fd29aSsetje 		 * construction behavior.
2948986fd29aSsetje 		 */
29497c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, lp->line);
2950986fd29aSsetje 		if (nftw(path, cmpstat, 20, FTW_PHYS) == -1) {
295148847494SEnrico Perla - Sun Microsystems 			if (is_flag_on(UPDATE_ERROR))
295248847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
29537c478bd9Sstevel@tonic-gate 			/*
29547c478bd9Sstevel@tonic-gate 			 * Some files may not exist.
29557c478bd9Sstevel@tonic-gate 			 * For example: etc/rtc_config on a x86 diskless system
29567c478bd9Sstevel@tonic-gate 			 * Emit verbose message only
29577c478bd9Sstevel@tonic-gate 			 */
29587c478bd9Sstevel@tonic-gate 			if (bam_verbose)
29597c478bd9Sstevel@tonic-gate 				bam_print(NFTW_FAIL, path, strerror(errno));
29607c478bd9Sstevel@tonic-gate 		}
29617c478bd9Sstevel@tonic-gate 	}
296248847494SEnrico Perla - Sun Microsystems 
296348847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
29647c478bd9Sstevel@tonic-gate }
29657c478bd9Sstevel@tonic-gate 
29664a9df875SEnrico Perla - Sun Microsystems /*
29674a9df875SEnrico Perla - Sun Microsystems  * Update the timestamp file.
29684a9df875SEnrico Perla - Sun Microsystems  */
29694a9df875SEnrico Perla - Sun Microsystems static void
29704a9df875SEnrico Perla - Sun Microsystems update_timestamp(char *root)
29714a9df875SEnrico Perla - Sun Microsystems {
29724a9df875SEnrico Perla - Sun Microsystems 	char	timestamp_path[PATH_MAX];
29734a9df875SEnrico Perla - Sun Microsystems 
29744a9df875SEnrico Perla - Sun Microsystems 	/* this path length has already been checked in check_flags_and_files */
29754a9df875SEnrico Perla - Sun Microsystems 	(void) snprintf(timestamp_path, sizeof (timestamp_path), "%s%s", root,
29764a9df875SEnrico Perla - Sun Microsystems 	    FILE_STAT_TIMESTAMP);
29774a9df875SEnrico Perla - Sun Microsystems 
29784a9df875SEnrico Perla - Sun Microsystems 	/*
29794a9df875SEnrico Perla - Sun Microsystems 	 * recreate the timestamp file. Since an outdated or absent timestamp
29804a9df875SEnrico Perla - Sun Microsystems 	 * file translates in a complete rebuild of the archive cache, notify
29814a9df875SEnrico Perla - Sun Microsystems 	 * the user of the performance issue.
29824a9df875SEnrico Perla - Sun Microsystems 	 */
29834a9df875SEnrico Perla - Sun Microsystems 	if (creat(timestamp_path, FILE_STAT_MODE) < 0) {
29844a9df875SEnrico Perla - Sun Microsystems 		bam_error(OPEN_FAIL, timestamp_path, strerror(errno));
298518961ad2SToomas Soome 		bam_error(TIMESTAMP_FAIL);
29864a9df875SEnrico Perla - Sun Microsystems 	}
29874a9df875SEnrico Perla - Sun Microsystems }
29884a9df875SEnrico Perla - Sun Microsystems 
29894a9df875SEnrico Perla - Sun Microsystems 
29907c478bd9Sstevel@tonic-gate static void
29917c478bd9Sstevel@tonic-gate savenew(char *root)
29927c478bd9Sstevel@tonic-gate {
29937c478bd9Sstevel@tonic-gate 	char 	path[PATH_MAX];
29947c478bd9Sstevel@tonic-gate 	char 	path2[PATH_MAX];
29957c478bd9Sstevel@tonic-gate 	size_t 	sz;
29967c478bd9Sstevel@tonic-gate 	char 	*nstat;
29977c478bd9Sstevel@tonic-gate 	int 	fd, wrote, error;
29987c478bd9Sstevel@tonic-gate 
29997c478bd9Sstevel@tonic-gate 	nstat = NULL;
30007c478bd9Sstevel@tonic-gate 	sz = 0;
30017c478bd9Sstevel@tonic-gate 	error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz,
30027c478bd9Sstevel@tonic-gate 	    NV_ENCODE_XDR, 0);
30037c478bd9Sstevel@tonic-gate 	if (error) {
30047c478bd9Sstevel@tonic-gate 		bam_error(PACK_FAIL, strerror(error));
30057c478bd9Sstevel@tonic-gate 		return;
30067c478bd9Sstevel@tonic-gate 	}
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP);
30097c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE);
30107c478bd9Sstevel@tonic-gate 	if (fd == -1) {
30117c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, path, strerror(errno));
30127c478bd9Sstevel@tonic-gate 		free(nstat);
30137c478bd9Sstevel@tonic-gate 		return;
30147c478bd9Sstevel@tonic-gate 	}
30157c478bd9Sstevel@tonic-gate 	wrote = write(fd, nstat, sz);
30167c478bd9Sstevel@tonic-gate 	if (wrote != sz) {
30177c478bd9Sstevel@tonic-gate 		bam_error(WRITE_FAIL, path, strerror(errno));
30187c478bd9Sstevel@tonic-gate 		(void) close(fd);
30197c478bd9Sstevel@tonic-gate 		free(nstat);
30207c478bd9Sstevel@tonic-gate 		return;
30217c478bd9Sstevel@tonic-gate 	}
30227c478bd9Sstevel@tonic-gate 	(void) close(fd);
30237c478bd9Sstevel@tonic-gate 	free(nstat);
30247c478bd9Sstevel@tonic-gate 
30257c478bd9Sstevel@tonic-gate 	(void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT);
30267c478bd9Sstevel@tonic-gate 	if (rename(path, path2) != 0) {
30277c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path2, strerror(errno));
30287c478bd9Sstevel@tonic-gate 	}
30297c478bd9Sstevel@tonic-gate }
30307c478bd9Sstevel@tonic-gate 
303148847494SEnrico Perla - Sun Microsystems #define	init_walk_args()	bzero(&walk_arg, sizeof (walk_arg))
303248847494SEnrico Perla - Sun Microsystems 
30337c478bd9Sstevel@tonic-gate static void
30347c478bd9Sstevel@tonic-gate clear_walk_args(void)
30357c478bd9Sstevel@tonic-gate {
30367c478bd9Sstevel@tonic-gate 	nvlist_free(walk_arg.old_nvlp);
30377c478bd9Sstevel@tonic-gate 	nvlist_free(walk_arg.new_nvlp);
303848847494SEnrico Perla - Sun Microsystems 	if (walk_arg.sparcfile)
303948847494SEnrico Perla - Sun Microsystems 		(void) fclose(walk_arg.sparcfile);
30407c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
30417c478bd9Sstevel@tonic-gate 	walk_arg.new_nvlp = NULL;
304248847494SEnrico Perla - Sun Microsystems 	walk_arg.sparcfile = NULL;
30437c478bd9Sstevel@tonic-gate }
30447c478bd9Sstevel@tonic-gate 
30457c478bd9Sstevel@tonic-gate /*
30467c478bd9Sstevel@tonic-gate  * Returns:
30477c478bd9Sstevel@tonic-gate  *	0 - no update necessary
30487c478bd9Sstevel@tonic-gate  *	1 - update required.
30497c478bd9Sstevel@tonic-gate  *	BAM_ERROR (-1) - An error occurred
30507c478bd9Sstevel@tonic-gate  *
30517c478bd9Sstevel@tonic-gate  * Special handling for check (-n):
30527c478bd9Sstevel@tonic-gate  * ================================
30537c478bd9Sstevel@tonic-gate  * The check (-n) option produces parseable output.
30547c478bd9Sstevel@tonic-gate  * To do this, we suppress all stdout messages unrelated
30557c478bd9Sstevel@tonic-gate  * to out of sync files.
30567c478bd9Sstevel@tonic-gate  * All stderr messages are still printed though.
30577c478bd9Sstevel@tonic-gate  *
30587c478bd9Sstevel@tonic-gate  */
30597c478bd9Sstevel@tonic-gate static int
30607c478bd9Sstevel@tonic-gate update_required(char *root)
30617c478bd9Sstevel@tonic-gate {
30627c478bd9Sstevel@tonic-gate 	struct stat 	sb;
30637c478bd9Sstevel@tonic-gate 	char 		path[PATH_MAX];
30647c478bd9Sstevel@tonic-gate 	filelist_t 	flist;
30657c478bd9Sstevel@tonic-gate 	filelist_t 	*flistp = &flist;
306648847494SEnrico Perla - Sun Microsystems 	int 		ret;
30677c478bd9Sstevel@tonic-gate 
30687c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
30697c478bd9Sstevel@tonic-gate 
307048847494SEnrico Perla - Sun Microsystems 	if (is_sparc())
307148847494SEnrico Perla - Sun Microsystems 		set_flag(IS_SPARC_TARGET);
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate 	/*
307448847494SEnrico Perla - Sun Microsystems 	 * Check if cache directories and archives are present
30757c478bd9Sstevel@tonic-gate 	 */
307648847494SEnrico Perla - Sun Microsystems 
307748847494SEnrico Perla - Sun Microsystems 	ret = check_flags_and_files(root);
307848847494SEnrico Perla - Sun Microsystems 	if (ret < 0)
307948847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
30807c478bd9Sstevel@tonic-gate 
30817c478bd9Sstevel@tonic-gate 	/*
30827c478bd9Sstevel@tonic-gate 	 * In certain deployment scenarios, filestat may not
3083eff168e0SEnrico Perla - Sun Microsystems 	 * exist. Do not stop the boot process, but trigger an update
3084eff168e0SEnrico Perla - Sun Microsystems 	 * of the archives (which will recreate filestat.ramdisk).
30857c478bd9Sstevel@tonic-gate 	 */
30867c478bd9Sstevel@tonic-gate 	if (bam_smf_check) {
30877c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
3088eff168e0SEnrico Perla - Sun Microsystems 		if (stat(path, &sb) != 0) {
3089eff168e0SEnrico Perla - Sun Microsystems 			(void) creat(NEED_UPDATE_FILE, 0644);
30907c478bd9Sstevel@tonic-gate 			return (0);
30917c478bd9Sstevel@tonic-gate 		}
3092eff168e0SEnrico Perla - Sun Microsystems 	}
30937c478bd9Sstevel@tonic-gate 
30947c478bd9Sstevel@tonic-gate 	getoldstat(root);
30957c478bd9Sstevel@tonic-gate 
30967c478bd9Sstevel@tonic-gate 	/*
3097a28d77b8Svikram 	 * Check if the archive contains files that are no longer
3098a28d77b8Svikram 	 * present on the root filesystem.
3099a28d77b8Svikram 	 */
3100a28d77b8Svikram 	check4stale(root);
3101a28d77b8Svikram 
3102a28d77b8Svikram 	/*
31037c478bd9Sstevel@tonic-gate 	 * read list of files
31047c478bd9Sstevel@tonic-gate 	 */
31057c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
31067c478bd9Sstevel@tonic-gate 		clear_walk_args();
31077c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
31087c478bd9Sstevel@tonic-gate 	}
31097c478bd9Sstevel@tonic-gate 
31107c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
31117c478bd9Sstevel@tonic-gate 
31127c478bd9Sstevel@tonic-gate 	/*
31137c478bd9Sstevel@tonic-gate 	 * At this point either the update is required
31147c478bd9Sstevel@tonic-gate 	 * or the decision is pending. In either case
31157c478bd9Sstevel@tonic-gate 	 * we need to create new stat nvlist
31167c478bd9Sstevel@tonic-gate 	 */
31177c478bd9Sstevel@tonic-gate 	create_newstat();
31187c478bd9Sstevel@tonic-gate 	/*
31197c478bd9Sstevel@tonic-gate 	 * This walk does 2 things:
31207c478bd9Sstevel@tonic-gate 	 *  	- gets new stat data for every file
31217c478bd9Sstevel@tonic-gate 	 *	- (optional) compare old and new stat data
31227c478bd9Sstevel@tonic-gate 	 */
312348847494SEnrico Perla - Sun Microsystems 	ret = walk_list(root, &flist);
31247c478bd9Sstevel@tonic-gate 
31257c478bd9Sstevel@tonic-gate 	/* done with the file list */
31267c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
31277c478bd9Sstevel@tonic-gate 
312848847494SEnrico Perla - Sun Microsystems 	/* something went wrong */
312948847494SEnrico Perla - Sun Microsystems 
313048847494SEnrico Perla - Sun Microsystems 	if (ret == BAM_ERROR) {
313148847494SEnrico Perla - Sun Microsystems 		bam_error(CACHE_FAIL);
313248847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
31337c478bd9Sstevel@tonic-gate 	}
31347c478bd9Sstevel@tonic-gate 
313548847494SEnrico Perla - Sun Microsystems 	if (walk_arg.new_nvlp == NULL) {
3136cedc7e57SEnrico Perla - Sun Microsystems 		if (walk_arg.sparcfile != NULL)
313748847494SEnrico Perla - Sun Microsystems 			(void) fclose(walk_arg.sparcfile);
313848847494SEnrico Perla - Sun Microsystems 		bam_error(NO_NEW_STAT);
313948847494SEnrico Perla - Sun Microsystems 	}
31407c478bd9Sstevel@tonic-gate 
314148847494SEnrico Perla - Sun Microsystems 	/* If nothing was updated, discard newstat. */
314248847494SEnrico Perla - Sun Microsystems 
314348847494SEnrico Perla - Sun Microsystems 	if (!is_dir_flag_on(FILE32, NEED_UPDATE) &&
314448847494SEnrico Perla - Sun Microsystems 	    !is_dir_flag_on(FILE64, NEED_UPDATE)) {
31457c478bd9Sstevel@tonic-gate 		clear_walk_args();
31467c478bd9Sstevel@tonic-gate 		return (0);
31477c478bd9Sstevel@tonic-gate 	}
31487c478bd9Sstevel@tonic-gate 
3149cedc7e57SEnrico Perla - Sun Microsystems 	if (walk_arg.sparcfile != NULL)
315048847494SEnrico Perla - Sun Microsystems 		(void) fclose(walk_arg.sparcfile);
315148847494SEnrico Perla - Sun Microsystems 
31527c478bd9Sstevel@tonic-gate 	return (1);
31537c478bd9Sstevel@tonic-gate }
31547c478bd9Sstevel@tonic-gate 
315548847494SEnrico Perla - Sun Microsystems static int
315648847494SEnrico Perla - Sun Microsystems flushfs(char *root)
315748847494SEnrico Perla - Sun Microsystems {
315848847494SEnrico Perla - Sun Microsystems 	char	cmd[PATH_MAX + 30];
315948847494SEnrico Perla - Sun Microsystems 
316048847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmd, sizeof (cmd), "%s -f \"%s\" 2>/dev/null",
316148847494SEnrico Perla - Sun Microsystems 	    LOCKFS_PATH, root);
316248847494SEnrico Perla - Sun Microsystems 
316348847494SEnrico Perla - Sun Microsystems 	return (exec_cmd(cmd, NULL));
316448847494SEnrico Perla - Sun Microsystems }
316548847494SEnrico Perla - Sun Microsystems 
316648847494SEnrico Perla - Sun Microsystems static int
316748847494SEnrico Perla - Sun Microsystems do_archive_copy(char *source, char *dest)
316848847494SEnrico Perla - Sun Microsystems {
316948847494SEnrico Perla - Sun Microsystems 
3170cedc7e57SEnrico Perla - Sun Microsystems 	sync();
317148847494SEnrico Perla - Sun Microsystems 
317248847494SEnrico Perla - Sun Microsystems 	/* the equivalent of mv archive-new-$pid boot_archive */
3173cedc7e57SEnrico Perla - Sun Microsystems 	if (rename(source, dest) != 0) {
3174cedc7e57SEnrico Perla - Sun Microsystems 		(void) unlink(source);
317548847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
3176cedc7e57SEnrico Perla - Sun Microsystems 	}
317748847494SEnrico Perla - Sun Microsystems 
317848847494SEnrico Perla - Sun Microsystems 	if (flushfs(bam_root) != 0)
3179cedc7e57SEnrico Perla - Sun Microsystems 		sync();
318048847494SEnrico Perla - Sun Microsystems 
318148847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
318248847494SEnrico Perla - Sun Microsystems }
318348847494SEnrico Perla - Sun Microsystems 
318448847494SEnrico Perla - Sun Microsystems static int
318548847494SEnrico Perla - Sun Microsystems check_cmdline(filelist_t flist)
318648847494SEnrico Perla - Sun Microsystems {
318748847494SEnrico Perla - Sun Microsystems 	line_t	*lp;
318848847494SEnrico Perla - Sun Microsystems 
318948847494SEnrico Perla - Sun Microsystems 	for (lp = flist.head; lp; lp = lp->next) {
319048847494SEnrico Perla - Sun Microsystems 		if (strstr(lp->line, "Error:") != NULL ||
319148847494SEnrico Perla - Sun Microsystems 		    strstr(lp->line, "Inode number overflow") != NULL) {
3192cedc7e57SEnrico Perla - Sun Microsystems 			(void) fprintf(stderr, "%s\n", lp->line);
319348847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
319448847494SEnrico Perla - Sun Microsystems 		}
319548847494SEnrico Perla - Sun Microsystems 	}
319648847494SEnrico Perla - Sun Microsystems 
319748847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
319848847494SEnrico Perla - Sun Microsystems }
319948847494SEnrico Perla - Sun Microsystems 
3200cedc7e57SEnrico Perla - Sun Microsystems static void
3201cedc7e57SEnrico Perla - Sun Microsystems dump_errormsg(filelist_t flist)
3202cedc7e57SEnrico Perla - Sun Microsystems {
3203cedc7e57SEnrico Perla - Sun Microsystems 	line_t	*lp;
3204cedc7e57SEnrico Perla - Sun Microsystems 
3205cedc7e57SEnrico Perla - Sun Microsystems 	for (lp = flist.head; lp; lp = lp->next)
3206cedc7e57SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, "%s\n", lp->line);
3207cedc7e57SEnrico Perla - Sun Microsystems }
3208cedc7e57SEnrico Perla - Sun Microsystems 
320948847494SEnrico Perla - Sun Microsystems static int
321048847494SEnrico Perla - Sun Microsystems check_archive(char *dest)
321148847494SEnrico Perla - Sun Microsystems {
321248847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
321348847494SEnrico Perla - Sun Microsystems 
321448847494SEnrico Perla - Sun Microsystems 	if (stat(dest, &sb) != 0 || !S_ISREG(sb.st_mode) ||
321548847494SEnrico Perla - Sun Microsystems 	    sb.st_size < 10000) {
321648847494SEnrico Perla - Sun Microsystems 		bam_error(ARCHIVE_BAD, dest);
321748847494SEnrico Perla - Sun Microsystems 		(void) unlink(dest);
321848847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
321948847494SEnrico Perla - Sun Microsystems 	}
322048847494SEnrico Perla - Sun Microsystems 
322148847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
322248847494SEnrico Perla - Sun Microsystems }
322348847494SEnrico Perla - Sun Microsystems 
32246debd3f5SAlexander Eremin static boolean_t
32256debd3f5SAlexander Eremin is_be(char *root)
32266debd3f5SAlexander Eremin {
32276debd3f5SAlexander Eremin 	zfs_handle_t	*zhp;
32286debd3f5SAlexander Eremin 	libzfs_handle_t	*hdl;
32296debd3f5SAlexander Eremin 	be_node_list_t	*be_nodes = NULL;
32306debd3f5SAlexander Eremin 	be_node_list_t	*cur_be;
32316debd3f5SAlexander Eremin 	boolean_t	be_exist = B_FALSE;
3232*40a5c998SMatthew Ahrens 	char		ds_path[ZFS_MAX_DATASET_NAME_LEN];
32336debd3f5SAlexander Eremin 
32346debd3f5SAlexander Eremin 	if (!is_zfs(root))
32356debd3f5SAlexander Eremin 		return (B_FALSE);
32366debd3f5SAlexander Eremin 	/*
32376debd3f5SAlexander Eremin 	 * Get dataset for mountpoint
32386debd3f5SAlexander Eremin 	 */
32396debd3f5SAlexander Eremin 	if ((hdl = libzfs_init()) == NULL)
32406debd3f5SAlexander Eremin 		return (B_FALSE);
32416debd3f5SAlexander Eremin 
32426debd3f5SAlexander Eremin 	if ((zhp = zfs_path_to_zhandle(hdl, root,
32436debd3f5SAlexander Eremin 	    ZFS_TYPE_FILESYSTEM)) == NULL) {
32446debd3f5SAlexander Eremin 		libzfs_fini(hdl);
32456debd3f5SAlexander Eremin 		return (B_FALSE);
32466debd3f5SAlexander Eremin 	}
32476debd3f5SAlexander Eremin 
32486debd3f5SAlexander Eremin 	(void) strlcpy(ds_path, zfs_get_name(zhp), sizeof (ds_path));
32496debd3f5SAlexander Eremin 
32506debd3f5SAlexander Eremin 	/*
32516debd3f5SAlexander Eremin 	 * Check if the current dataset is BE
32526debd3f5SAlexander Eremin 	 */
32536debd3f5SAlexander Eremin 	if (be_list(NULL, &be_nodes) == BE_SUCCESS) {
32546debd3f5SAlexander Eremin 		for (cur_be = be_nodes; cur_be != NULL;
32556debd3f5SAlexander Eremin 		    cur_be = cur_be->be_next_node) {
32566debd3f5SAlexander Eremin 
32576debd3f5SAlexander Eremin 			/*
32586debd3f5SAlexander Eremin 			 * Because we guarantee that cur_be->be_root_ds
32596debd3f5SAlexander Eremin 			 * is null-terminated by internal data structure,
32606debd3f5SAlexander Eremin 			 * we can safely use strcmp()
32616debd3f5SAlexander Eremin 			 */
32626debd3f5SAlexander Eremin 			if (strcmp(ds_path, cur_be->be_root_ds) == 0) {
32636debd3f5SAlexander Eremin 				be_exist = B_TRUE;
32646debd3f5SAlexander Eremin 				break;
32656debd3f5SAlexander Eremin 			}
32666debd3f5SAlexander Eremin 		}
32676debd3f5SAlexander Eremin 		be_free_list(be_nodes);
32686debd3f5SAlexander Eremin 	}
32696debd3f5SAlexander Eremin 	zfs_close(zhp);
32706debd3f5SAlexander Eremin 	libzfs_fini(hdl);
32716debd3f5SAlexander Eremin 
32726debd3f5SAlexander Eremin 	return (be_exist);
32736debd3f5SAlexander Eremin }
32746debd3f5SAlexander Eremin 
327548847494SEnrico Perla - Sun Microsystems /*
327648847494SEnrico Perla - Sun Microsystems  * Returns 1 if mkiso is in the expected PATH, 0 otherwise
327748847494SEnrico Perla - Sun Microsystems  */
327848847494SEnrico Perla - Sun Microsystems static int
327948847494SEnrico Perla - Sun Microsystems is_mkisofs()
328048847494SEnrico Perla - Sun Microsystems {
3281cedc7e57SEnrico Perla - Sun Microsystems 	if (access(MKISOFS_PATH, X_OK) == 0)
328248847494SEnrico Perla - Sun Microsystems 		return (1);
328348847494SEnrico Perla - Sun Microsystems 	return (0);
328448847494SEnrico Perla - Sun Microsystems }
328548847494SEnrico Perla - Sun Microsystems 
328648847494SEnrico Perla - Sun Microsystems #define	MKISO_PARAMS	" -quiet -graft-points -dlrDJN -relaxed-filenames "
328748847494SEnrico Perla - Sun Microsystems 
328848847494SEnrico Perla - Sun Microsystems static int
328948847494SEnrico Perla - Sun Microsystems create_sparc_archive(char *archive, char *tempname, char *bootblk, char *list)
329048847494SEnrico Perla - Sun Microsystems {
329148847494SEnrico Perla - Sun Microsystems 	int		ret;
329248847494SEnrico Perla - Sun Microsystems 	char		cmdline[3 * PATH_MAX + 64];
329348847494SEnrico Perla - Sun Microsystems 	filelist_t	flist = {0};
329448847494SEnrico Perla - Sun Microsystems 	const char	*func = "create_sparc_archive()";
329548847494SEnrico Perla - Sun Microsystems 
329648847494SEnrico Perla - Sun Microsystems 	if (access(bootblk, R_OK) == 1) {
329748847494SEnrico Perla - Sun Microsystems 		bam_error(BOOTBLK_FAIL, bootblk);
329848847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
329948847494SEnrico Perla - Sun Microsystems 	}
330048847494SEnrico Perla - Sun Microsystems 
330148847494SEnrico Perla - Sun Microsystems 	/*
330248847494SEnrico Perla - Sun Microsystems 	 * Prepare mkisofs command line and execute it
330348847494SEnrico Perla - Sun Microsystems 	 */
330448847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s -G %s -o \"%s\" "
3305cedc7e57SEnrico Perla - Sun Microsystems 	    "-path-list \"%s\" 2>&1", MKISOFS_PATH, MKISO_PARAMS, bootblk,
330648847494SEnrico Perla - Sun Microsystems 	    tempname, list);
330748847494SEnrico Perla - Sun Microsystems 
330848847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
330948847494SEnrico Perla - Sun Microsystems 
331048847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
3311cedc7e57SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
3312cedc7e57SEnrico Perla - Sun Microsystems 		dump_errormsg(flist);
331348847494SEnrico Perla - Sun Microsystems 		goto out_err;
3314cedc7e57SEnrico Perla - Sun Microsystems 	}
331548847494SEnrico Perla - Sun Microsystems 
331648847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
331748847494SEnrico Perla - Sun Microsystems 
331848847494SEnrico Perla - Sun Microsystems 	/*
331948847494SEnrico Perla - Sun Microsystems 	 * Prepare dd command line to copy the bootblk on the new archive and
332048847494SEnrico Perla - Sun Microsystems 	 * execute it
332148847494SEnrico Perla - Sun Microsystems 	 */
332248847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s if=\"%s\" of=\"%s\""
3323cedc7e57SEnrico Perla - Sun Microsystems 	    " bs=1b oseek=1 count=15 conv=notrunc conv=sync 2>&1", DD_PATH_USR,
332448847494SEnrico Perla - Sun Microsystems 	    bootblk, tempname);
332548847494SEnrico Perla - Sun Microsystems 
332648847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
332748847494SEnrico Perla - Sun Microsystems 
332848847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
332948847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR)
333048847494SEnrico Perla - Sun Microsystems 		goto out_err;
333148847494SEnrico Perla - Sun Microsystems 
333248847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
333348847494SEnrico Perla - Sun Microsystems 
333448847494SEnrico Perla - Sun Microsystems 	/* Did we get a valid archive ? */
333548847494SEnrico Perla - Sun Microsystems 	if (check_archive(tempname) == BAM_ERROR)
333648847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
333748847494SEnrico Perla - Sun Microsystems 
333848847494SEnrico Perla - Sun Microsystems 	return (do_archive_copy(tempname, archive));
333948847494SEnrico Perla - Sun Microsystems 
334048847494SEnrico Perla - Sun Microsystems out_err:
334148847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
334248847494SEnrico Perla - Sun Microsystems 	bam_error(ARCHIVE_FAIL, cmdline);
334348847494SEnrico Perla - Sun Microsystems 	(void) unlink(tempname);
334448847494SEnrico Perla - Sun Microsystems 	return (BAM_ERROR);
334548847494SEnrico Perla - Sun Microsystems }
334648847494SEnrico Perla - Sun Microsystems 
334748847494SEnrico Perla - Sun Microsystems static unsigned int
334848847494SEnrico Perla - Sun Microsystems from_733(unsigned char *s)
334948847494SEnrico Perla - Sun Microsystems {
335048847494SEnrico Perla - Sun Microsystems 	int		i;
335148847494SEnrico Perla - Sun Microsystems 	unsigned int	ret = 0;
335248847494SEnrico Perla - Sun Microsystems 
335348847494SEnrico Perla - Sun Microsystems 	for (i = 0; i < 4; i++)
335448847494SEnrico Perla - Sun Microsystems 		ret |= s[i] << (8 * i);
335548847494SEnrico Perla - Sun Microsystems 
335648847494SEnrico Perla - Sun Microsystems 	return (ret);
335748847494SEnrico Perla - Sun Microsystems }
335848847494SEnrico Perla - Sun Microsystems 
335948847494SEnrico Perla - Sun Microsystems static void
336048847494SEnrico Perla - Sun Microsystems to_733(unsigned char *s, unsigned int val)
336148847494SEnrico Perla - Sun Microsystems {
336248847494SEnrico Perla - Sun Microsystems 	int	i;
336348847494SEnrico Perla - Sun Microsystems 
336448847494SEnrico Perla - Sun Microsystems 	for (i = 0; i < 4; i++)
336548847494SEnrico Perla - Sun Microsystems 		s[i] = s[7-i] = (val >> (8 * i)) & 0xFF;
336648847494SEnrico Perla - Sun Microsystems }
336748847494SEnrico Perla - Sun Microsystems 
336848847494SEnrico Perla - Sun Microsystems /*
336948847494SEnrico Perla - Sun Microsystems  * Extends the current boot archive without recreating it from scratch
337048847494SEnrico Perla - Sun Microsystems  */
337148847494SEnrico Perla - Sun Microsystems static int
337248847494SEnrico Perla - Sun Microsystems extend_iso_archive(char *archive, char *tempname, char *update_dir)
337348847494SEnrico Perla - Sun Microsystems {
337448847494SEnrico Perla - Sun Microsystems 	int			fd = -1, newfd = -1, ret, i;
337548847494SEnrico Perla - Sun Microsystems 	int			next_session = 0, new_size = 0;
337648847494SEnrico Perla - Sun Microsystems 	char			cmdline[3 * PATH_MAX + 64];
337748847494SEnrico Perla - Sun Microsystems 	const char		*func = "extend_iso_archive()";
337848847494SEnrico Perla - Sun Microsystems 	filelist_t		flist = {0};
337948847494SEnrico Perla - Sun Microsystems 	struct iso_pdesc	saved_desc[MAX_IVDs];
338048847494SEnrico Perla - Sun Microsystems 
338148847494SEnrico Perla - Sun Microsystems 	fd = open(archive, O_RDWR);
338248847494SEnrico Perla - Sun Microsystems 	if (fd == -1) {
3383cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
338448847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, archive, strerror(errno));
338548847494SEnrico Perla - Sun Microsystems 		goto out_err;
338648847494SEnrico Perla - Sun Microsystems 	}
338748847494SEnrico Perla - Sun Microsystems 
338848847494SEnrico Perla - Sun Microsystems 	/*
338948847494SEnrico Perla - Sun Microsystems 	 * A partial read is likely due to a corrupted file
339048847494SEnrico Perla - Sun Microsystems 	 */
339148847494SEnrico Perla - Sun Microsystems 	ret = pread64(fd, saved_desc, sizeof (saved_desc),
339248847494SEnrico Perla - Sun Microsystems 	    VOLDESC_OFF * CD_BLOCK);
339348847494SEnrico Perla - Sun Microsystems 	if (ret != sizeof (saved_desc)) {
3394cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
339548847494SEnrico Perla - Sun Microsystems 			bam_error(READ_FAIL, archive, strerror(errno));
339648847494SEnrico Perla - Sun Microsystems 		goto out_err;
339748847494SEnrico Perla - Sun Microsystems 	}
339848847494SEnrico Perla - Sun Microsystems 
339948847494SEnrico Perla - Sun Microsystems 	if (memcmp(saved_desc[0].type, "\1CD001", 6)) {
3400cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
340148847494SEnrico Perla - Sun Microsystems 			bam_error(SIGN_FAIL, archive);
340248847494SEnrico Perla - Sun Microsystems 		goto out_err;
340348847494SEnrico Perla - Sun Microsystems 	}
340448847494SEnrico Perla - Sun Microsystems 
340548847494SEnrico Perla - Sun Microsystems 	/*
340648847494SEnrico Perla - Sun Microsystems 	 * Read primary descriptor and locate next_session offset (it should
340748847494SEnrico Perla - Sun Microsystems 	 * point to the end of the archive)
340848847494SEnrico Perla - Sun Microsystems 	 */
340948847494SEnrico Perla - Sun Microsystems 	next_session = P2ROUNDUP(from_733(saved_desc[0].volume_space_size), 16);
341048847494SEnrico Perla - Sun Microsystems 
341148847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s -C 16,%d -M %s %s -o \""
3412cedc7e57SEnrico Perla - Sun Microsystems 	    "%s\" \"%s\" 2>&1", MKISOFS_PATH, next_session, archive,
3413cedc7e57SEnrico Perla - Sun Microsystems 	    MKISO_PARAMS, tempname, update_dir);
341448847494SEnrico Perla - Sun Microsystems 
341548847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
341648847494SEnrico Perla - Sun Microsystems 
341748847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
341848847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
3419cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose) {
342048847494SEnrico Perla - Sun Microsystems 			bam_error(MULTI_FAIL, cmdline);
3421cedc7e57SEnrico Perla - Sun Microsystems 			dump_errormsg(flist);
3422cedc7e57SEnrico Perla - Sun Microsystems 		}
342348847494SEnrico Perla - Sun Microsystems 		goto out_flist_err;
342448847494SEnrico Perla - Sun Microsystems 	}
342548847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
342648847494SEnrico Perla - Sun Microsystems 
342748847494SEnrico Perla - Sun Microsystems 	newfd = open(tempname, O_RDONLY);
342848847494SEnrico Perla - Sun Microsystems 	if (newfd == -1) {
3429cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
343048847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, archive, strerror(errno));
343148847494SEnrico Perla - Sun Microsystems 		goto out_err;
343248847494SEnrico Perla - Sun Microsystems 	}
343348847494SEnrico Perla - Sun Microsystems 
343448847494SEnrico Perla - Sun Microsystems 	ret = pread64(newfd, saved_desc, sizeof (saved_desc),
343548847494SEnrico Perla - Sun Microsystems 	    VOLDESC_OFF * CD_BLOCK);
343648847494SEnrico Perla - Sun Microsystems 	if (ret != sizeof (saved_desc)) {
3437cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
343848847494SEnrico Perla - Sun Microsystems 			bam_error(READ_FAIL, archive, strerror(errno));
343948847494SEnrico Perla - Sun Microsystems 		goto out_err;
344048847494SEnrico Perla - Sun Microsystems 	}
344148847494SEnrico Perla - Sun Microsystems 
344248847494SEnrico Perla - Sun Microsystems 	if (memcmp(saved_desc[0].type, "\1CD001", 6)) {
3443cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
344448847494SEnrico Perla - Sun Microsystems 			bam_error(SIGN_FAIL, archive);
344548847494SEnrico Perla - Sun Microsystems 		goto out_err;
344648847494SEnrico Perla - Sun Microsystems 	}
344748847494SEnrico Perla - Sun Microsystems 
344848847494SEnrico Perla - Sun Microsystems 	new_size = from_733(saved_desc[0].volume_space_size) + next_session;
344948847494SEnrico Perla - Sun Microsystems 	to_733(saved_desc[0].volume_space_size, new_size);
345048847494SEnrico Perla - Sun Microsystems 
345148847494SEnrico Perla - Sun Microsystems 	for (i = 1; i < MAX_IVDs; i++) {
345248847494SEnrico Perla - Sun Microsystems 		if (saved_desc[i].type[0] == (unsigned char)255)
345348847494SEnrico Perla - Sun Microsystems 			break;
345448847494SEnrico Perla - Sun Microsystems 		if (memcmp(saved_desc[i].id, "CD001", 5))
345548847494SEnrico Perla - Sun Microsystems 			break;
345648847494SEnrico Perla - Sun Microsystems 
345748847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
345848847494SEnrico Perla - Sun Microsystems 			bam_print("%s: Updating descriptor entry [%d]\n", func,
345948847494SEnrico Perla - Sun Microsystems 			    i);
346048847494SEnrico Perla - Sun Microsystems 
346148847494SEnrico Perla - Sun Microsystems 		to_733(saved_desc[i].volume_space_size, new_size);
346248847494SEnrico Perla - Sun Microsystems 	}
346348847494SEnrico Perla - Sun Microsystems 
346448847494SEnrico Perla - Sun Microsystems 	ret = pwrite64(fd, saved_desc, DVD_BLOCK, VOLDESC_OFF*CD_BLOCK);
346548847494SEnrico Perla - Sun Microsystems 	if (ret != DVD_BLOCK) {
3466cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
346748847494SEnrico Perla - Sun Microsystems 			bam_error(WRITE_FAIL, archive, strerror(errno));
346848847494SEnrico Perla - Sun Microsystems 		goto out_err;
346948847494SEnrico Perla - Sun Microsystems 	}
347048847494SEnrico Perla - Sun Microsystems 	(void) close(newfd);
347148847494SEnrico Perla - Sun Microsystems 	newfd = -1;
347248847494SEnrico Perla - Sun Microsystems 
3473cedc7e57SEnrico Perla - Sun Microsystems 	ret = fsync(fd);
3474cedc7e57SEnrico Perla - Sun Microsystems 	if (ret != 0)
3475cedc7e57SEnrico Perla - Sun Microsystems 		sync();
3476cedc7e57SEnrico Perla - Sun Microsystems 
347748847494SEnrico Perla - Sun Microsystems 	ret = close(fd);
347848847494SEnrico Perla - Sun Microsystems 	if (ret != 0) {
3479cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
348048847494SEnrico Perla - Sun Microsystems 			bam_error(CLOSE_FAIL, archive, strerror(errno));
348148847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
348248847494SEnrico Perla - Sun Microsystems 	}
348348847494SEnrico Perla - Sun Microsystems 	fd = -1;
348448847494SEnrico Perla - Sun Microsystems 
348548847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s if=%s of=%s bs=32k "
3486cedc7e57SEnrico Perla - Sun Microsystems 	    "seek=%d conv=sync 2>&1", DD_PATH_USR, tempname, archive,
348748847494SEnrico Perla - Sun Microsystems 	    (next_session/16));
348848847494SEnrico Perla - Sun Microsystems 
348948847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
349048847494SEnrico Perla - Sun Microsystems 
349148847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
349248847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
3493cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
349448847494SEnrico Perla - Sun Microsystems 			bam_error(MULTI_FAIL, cmdline);
349548847494SEnrico Perla - Sun Microsystems 		goto out_flist_err;
349648847494SEnrico Perla - Sun Microsystems 	}
349748847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
349848847494SEnrico Perla - Sun Microsystems 
349948847494SEnrico Perla - Sun Microsystems 	(void) unlink(tempname);
350048847494SEnrico Perla - Sun Microsystems 
3501cedc7e57SEnrico Perla - Sun Microsystems 	if (flushfs(bam_root) != 0)
3502cedc7e57SEnrico Perla - Sun Microsystems 		sync();
3503cedc7e57SEnrico Perla - Sun Microsystems 
350448847494SEnrico Perla - Sun Microsystems 	if (bam_verbose)
350548847494SEnrico Perla - Sun Microsystems 		bam_print("boot archive updated successfully\n");
350648847494SEnrico Perla - Sun Microsystems 
350748847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
350848847494SEnrico Perla - Sun Microsystems 
350948847494SEnrico Perla - Sun Microsystems out_flist_err:
351048847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
351148847494SEnrico Perla - Sun Microsystems out_err:
351248847494SEnrico Perla - Sun Microsystems 	if (fd != -1)
351348847494SEnrico Perla - Sun Microsystems 		(void) close(fd);
351448847494SEnrico Perla - Sun Microsystems 	if (newfd != -1)
351548847494SEnrico Perla - Sun Microsystems 		(void) close(newfd);
351648847494SEnrico Perla - Sun Microsystems 	return (BAM_ERROR);
351748847494SEnrico Perla - Sun Microsystems }
351848847494SEnrico Perla - Sun Microsystems 
351948847494SEnrico Perla - Sun Microsystems static int
352048847494SEnrico Perla - Sun Microsystems create_x86_archive(char *archive, char *tempname, char *update_dir)
352148847494SEnrico Perla - Sun Microsystems {
352248847494SEnrico Perla - Sun Microsystems 	int		ret;
352348847494SEnrico Perla - Sun Microsystems 	char		cmdline[3 * PATH_MAX + 64];
352448847494SEnrico Perla - Sun Microsystems 	filelist_t	flist = {0};
352548847494SEnrico Perla - Sun Microsystems 	const char	*func = "create_x86_archive()";
352648847494SEnrico Perla - Sun Microsystems 
352748847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s -o \"%s\" \"%s\" "
3528cedc7e57SEnrico Perla - Sun Microsystems 	    "2>&1", MKISOFS_PATH, MKISO_PARAMS, tempname, update_dir);
352948847494SEnrico Perla - Sun Microsystems 
353048847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
353148847494SEnrico Perla - Sun Microsystems 
353248847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
353348847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
353448847494SEnrico Perla - Sun Microsystems 		bam_error(ARCHIVE_FAIL, cmdline);
3535cedc7e57SEnrico Perla - Sun Microsystems 		dump_errormsg(flist);
353648847494SEnrico Perla - Sun Microsystems 		filelist_free(&flist);
353748847494SEnrico Perla - Sun Microsystems 		(void) unlink(tempname);
353848847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
353948847494SEnrico Perla - Sun Microsystems 	}
354048847494SEnrico Perla - Sun Microsystems 
354148847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
354248847494SEnrico Perla - Sun Microsystems 
354348847494SEnrico Perla - Sun Microsystems 	if (check_archive(tempname) == BAM_ERROR)
354448847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
354548847494SEnrico Perla - Sun Microsystems 
354648847494SEnrico Perla - Sun Microsystems 	return (do_archive_copy(tempname, archive));
354748847494SEnrico Perla - Sun Microsystems }
354848847494SEnrico Perla - Sun Microsystems 
354948847494SEnrico Perla - Sun Microsystems static int
355048847494SEnrico Perla - Sun Microsystems mkisofs_archive(char *root, int what)
355148847494SEnrico Perla - Sun Microsystems {
355248847494SEnrico Perla - Sun Microsystems 	int		ret;
355348847494SEnrico Perla - Sun Microsystems 	char		temp[PATH_MAX];
355448847494SEnrico Perla - Sun Microsystems 	char 		bootblk[PATH_MAX];
355548847494SEnrico Perla - Sun Microsystems 	char		boot_archive[PATH_MAX];
355648847494SEnrico Perla - Sun Microsystems 
355748847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
355848847494SEnrico Perla - Sun Microsystems 		ret = snprintf(temp, sizeof (temp),
355948847494SEnrico Perla - Sun Microsystems 		    "%s%s%s/amd64/archive-new-%d", root, ARCHIVE_PREFIX,
356048847494SEnrico Perla - Sun Microsystems 		    get_machine(), getpid());
356148847494SEnrico Perla - Sun Microsystems 	else
356248847494SEnrico Perla - Sun Microsystems 		ret = snprintf(temp, sizeof (temp), "%s%s%s/archive-new-%d",
356348847494SEnrico Perla - Sun Microsystems 		    root, ARCHIVE_PREFIX, get_machine(), getpid());
356448847494SEnrico Perla - Sun Microsystems 
356548847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (temp))
356648847494SEnrico Perla - Sun Microsystems 		goto out_path_err;
356748847494SEnrico Perla - Sun Microsystems 
356848847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
356948847494SEnrico Perla - Sun Microsystems 		ret = snprintf(boot_archive, sizeof (boot_archive),
357048847494SEnrico Perla - Sun Microsystems 		    "%s%s%s/amd64%s", root, ARCHIVE_PREFIX, get_machine(),
357148847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_SUFFIX);
357248847494SEnrico Perla - Sun Microsystems 	else
357348847494SEnrico Perla - Sun Microsystems 		ret = snprintf(boot_archive, sizeof (boot_archive),
357448847494SEnrico Perla - Sun Microsystems 		    "%s%s%s%s", root, ARCHIVE_PREFIX, get_machine(),
357548847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_SUFFIX);
357648847494SEnrico Perla - Sun Microsystems 
357748847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (boot_archive))
357848847494SEnrico Perla - Sun Microsystems 		goto out_path_err;
357948847494SEnrico Perla - Sun Microsystems 
358048847494SEnrico Perla - Sun Microsystems 	bam_print("updating %s\n", boot_archive);
358148847494SEnrico Perla - Sun Microsystems 
358248847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET)) {
358348847494SEnrico Perla - Sun Microsystems 		ret = snprintf(bootblk, sizeof (bootblk),
358448847494SEnrico Perla - Sun Microsystems 		    "%s/platform/%s/lib/fs/hsfs/bootblk", root, get_machine());
358548847494SEnrico Perla - Sun Microsystems 		if (ret >= sizeof (bootblk))
358648847494SEnrico Perla - Sun Microsystems 			goto out_path_err;
358748847494SEnrico Perla - Sun Microsystems 
358848847494SEnrico Perla - Sun Microsystems 		ret = create_sparc_archive(boot_archive, temp, bootblk,
358948847494SEnrico Perla - Sun Microsystems 		    get_cachedir(what));
359048847494SEnrico Perla - Sun Microsystems 	} else {
359148847494SEnrico Perla - Sun Microsystems 		if (!is_dir_flag_on(what, NO_MULTI)) {
359248847494SEnrico Perla - Sun Microsystems 			if (bam_verbose)
359348847494SEnrico Perla - Sun Microsystems 				bam_print("Attempting to extend x86 archive: "
359448847494SEnrico Perla - Sun Microsystems 				    "%s\n", boot_archive);
359548847494SEnrico Perla - Sun Microsystems 
359648847494SEnrico Perla - Sun Microsystems 			ret = extend_iso_archive(boot_archive, temp,
359748847494SEnrico Perla - Sun Microsystems 			    get_updatedir(what));
359848847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_SUCCESS) {
359948847494SEnrico Perla - Sun Microsystems 				if (bam_verbose)
360048847494SEnrico Perla - Sun Microsystems 					bam_print("Successfully extended %s\n",
360148847494SEnrico Perla - Sun Microsystems 					    boot_archive);
360248847494SEnrico Perla - Sun Microsystems 
360348847494SEnrico Perla - Sun Microsystems 				(void) rmdir_r(get_updatedir(what));
360448847494SEnrico Perla - Sun Microsystems 				return (BAM_SUCCESS);
360548847494SEnrico Perla - Sun Microsystems 			}
360648847494SEnrico Perla - Sun Microsystems 		}
360748847494SEnrico Perla - Sun Microsystems 		/*
360848847494SEnrico Perla - Sun Microsystems 		 * The boot archive will be recreated from scratch. We get here
360948847494SEnrico Perla - Sun Microsystems 		 * if at least one of these conditions is true:
361048847494SEnrico Perla - Sun Microsystems 		 * - bootadm was called without the -e switch
361148847494SEnrico Perla - Sun Microsystems 		 * - the archive (or the archive cache) doesn't exist
361248847494SEnrico Perla - Sun Microsystems 		 * - archive size is bigger than BA_SIZE_MAX
361348847494SEnrico Perla - Sun Microsystems 		 * - more than COUNT_MAX files need to be updated
361448847494SEnrico Perla - Sun Microsystems 		 * - an error occourred either populating the /updates directory
361548847494SEnrico Perla - Sun Microsystems 		 *   or extend_iso_archive() failed
361648847494SEnrico Perla - Sun Microsystems 		 */
361748847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
361848847494SEnrico Perla - Sun Microsystems 			bam_print("Unable to extend %s... rebuilding archive\n",
361948847494SEnrico Perla - Sun Microsystems 			    boot_archive);
362048847494SEnrico Perla - Sun Microsystems 
362148847494SEnrico Perla - Sun Microsystems 		if (get_updatedir(what)[0] != '\0')
362248847494SEnrico Perla - Sun Microsystems 			(void) rmdir_r(get_updatedir(what));
362348847494SEnrico Perla - Sun Microsystems 
362448847494SEnrico Perla - Sun Microsystems 
362548847494SEnrico Perla - Sun Microsystems 		ret = create_x86_archive(boot_archive, temp,
362648847494SEnrico Perla - Sun Microsystems 		    get_cachedir(what));
362748847494SEnrico Perla - Sun Microsystems 	}
362848847494SEnrico Perla - Sun Microsystems 
362948847494SEnrico Perla - Sun Microsystems 	if (ret == BAM_SUCCESS && bam_verbose)
363048847494SEnrico Perla - Sun Microsystems 		bam_print("Successfully created %s\n", boot_archive);
363148847494SEnrico Perla - Sun Microsystems 
363248847494SEnrico Perla - Sun Microsystems 	return (ret);
363348847494SEnrico Perla - Sun Microsystems 
363448847494SEnrico Perla - Sun Microsystems out_path_err:
363548847494SEnrico Perla - Sun Microsystems 	bam_error(PATH_TOO_LONG, root);
363648847494SEnrico Perla - Sun Microsystems 	return (BAM_ERROR);
363748847494SEnrico Perla - Sun Microsystems }
363848847494SEnrico Perla - Sun Microsystems 
36397c478bd9Sstevel@tonic-gate static error_t
36407c478bd9Sstevel@tonic-gate create_ramdisk(char *root)
36417c478bd9Sstevel@tonic-gate {
36427c478bd9Sstevel@tonic-gate 	char *cmdline, path[PATH_MAX];
36437c478bd9Sstevel@tonic-gate 	size_t len;
36447c478bd9Sstevel@tonic-gate 	struct stat sb;
3645cedc7e57SEnrico Perla - Sun Microsystems 	int ret, what, status = BAM_SUCCESS;
364648847494SEnrico Perla - Sun Microsystems 
364748847494SEnrico Perla - Sun Microsystems 	/* If there is mkisofs, use it to create the required archives */
364848847494SEnrico Perla - Sun Microsystems 	if (is_mkisofs()) {
364948847494SEnrico Perla - Sun Microsystems 		for (what = FILE32; what < CACHEDIR_NUM; what++) {
3650cedc7e57SEnrico Perla - Sun Microsystems 			if (has_cachedir(what) && is_dir_flag_on(what,
3651cedc7e57SEnrico Perla - Sun Microsystems 			    NEED_UPDATE)) {
365248847494SEnrico Perla - Sun Microsystems 				ret = mkisofs_archive(root, what);
365348847494SEnrico Perla - Sun Microsystems 				if (ret != 0)
3654cedc7e57SEnrico Perla - Sun Microsystems 					status = BAM_ERROR;
365548847494SEnrico Perla - Sun Microsystems 			}
365648847494SEnrico Perla - Sun Microsystems 		}
3657cedc7e57SEnrico Perla - Sun Microsystems 		return (status);
365848847494SEnrico Perla - Sun Microsystems 	}
36597c478bd9Sstevel@tonic-gate 
36607c478bd9Sstevel@tonic-gate 	/*
366148847494SEnrico Perla - Sun Microsystems 	 * Else setup command args for create_ramdisk.ksh for the UFS archives
36627c478bd9Sstevel@tonic-gate 	 */
366348847494SEnrico Perla - Sun Microsystems 	if (bam_verbose)
366448847494SEnrico Perla - Sun Microsystems 		bam_print("mkisofs not found, creating UFS archive\n");
366548847494SEnrico Perla - Sun Microsystems 
3666986fd29aSsetje 	(void) snprintf(path, sizeof (path), "%s/%s", root, CREATE_RAMDISK);
36677c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
36687c478bd9Sstevel@tonic-gate 		bam_error(ARCH_EXEC_MISS, path, strerror(errno));
36697c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
36707c478bd9Sstevel@tonic-gate 	}
36717c478bd9Sstevel@tonic-gate 
3672cedc7e57SEnrico Perla - Sun Microsystems 	if (is_safe_exec(path) == BAM_ERROR)
3673cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
3674cedc7e57SEnrico Perla - Sun Microsystems 
36757c478bd9Sstevel@tonic-gate 	len = strlen(path) + strlen(root) + 10;	/* room for space + -R */
3676d876c67dSjg 	if (bam_alt_platform)
3677d876c67dSjg 		len += strlen(bam_platform) + strlen("-p ");
36787c478bd9Sstevel@tonic-gate 	cmdline = s_calloc(1, len);
36797c478bd9Sstevel@tonic-gate 
3680d876c67dSjg 	if (bam_alt_platform) {
3681d876c67dSjg 		assert(strlen(root) > 1);
3682d876c67dSjg 		(void) snprintf(cmdline, len, "%s -p %s -R %s",
3683d876c67dSjg 		    path, bam_platform, root);
3684d876c67dSjg 		/* chop off / at the end */
3685d876c67dSjg 		cmdline[strlen(cmdline) - 1] = '\0';
3686d876c67dSjg 	} else if (strlen(root) > 1) {
36877c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s -R %s", path, root);
36887c478bd9Sstevel@tonic-gate 		/* chop off / at the end */
36897c478bd9Sstevel@tonic-gate 		cmdline[strlen(cmdline) - 1] = '\0';
36907c478bd9Sstevel@tonic-gate 	} else
36917c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s", path);
36927c478bd9Sstevel@tonic-gate 
3693986fd29aSsetje 	if (exec_cmd(cmdline, NULL) != 0) {
36947c478bd9Sstevel@tonic-gate 		bam_error(ARCHIVE_FAIL, cmdline);
36957c478bd9Sstevel@tonic-gate 		free(cmdline);
36967c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
36977c478bd9Sstevel@tonic-gate 	}
36987c478bd9Sstevel@tonic-gate 	free(cmdline);
36997c478bd9Sstevel@tonic-gate 	/*
3700986fd29aSsetje 	 * The existence of the expected archives used to be
3701986fd29aSsetje 	 * verified here. This check is done in create_ramdisk as
3702986fd29aSsetje 	 * it needs to be in sync with the altroot operated upon.
37037c478bd9Sstevel@tonic-gate 	 */
37047c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
37057c478bd9Sstevel@tonic-gate }
37067c478bd9Sstevel@tonic-gate 
37077c478bd9Sstevel@tonic-gate /*
37087c478bd9Sstevel@tonic-gate  * Checks if target filesystem is on a ramdisk
37097c478bd9Sstevel@tonic-gate  * 1 - is miniroot
37107c478bd9Sstevel@tonic-gate  * 0 - is not
37117c478bd9Sstevel@tonic-gate  * When in doubt assume it is not a ramdisk.
37127c478bd9Sstevel@tonic-gate  */
37137c478bd9Sstevel@tonic-gate static int
37147c478bd9Sstevel@tonic-gate is_ramdisk(char *root)
37157c478bd9Sstevel@tonic-gate {
37167c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
37177c478bd9Sstevel@tonic-gate 	FILE *fp;
37187c478bd9Sstevel@tonic-gate 	int found;
3719b610f78eSvikram 	char mntpt[PATH_MAX];
3720b610f78eSvikram 	char *cp;
37217c478bd9Sstevel@tonic-gate 
37227c478bd9Sstevel@tonic-gate 	/*
37237c478bd9Sstevel@tonic-gate 	 * There are 3 situations where creating archive is
37247c478bd9Sstevel@tonic-gate 	 * of dubious value:
3725b610f78eSvikram 	 *	- create boot_archive on a lofi-mounted boot_archive
37267c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk which is the root filesystem
37277c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk mounted somewhere else
37287c478bd9Sstevel@tonic-gate 	 * The first is not easy to detect and checking for it is not
37297c478bd9Sstevel@tonic-gate 	 * worth it.
37307c478bd9Sstevel@tonic-gate 	 * The other two conditions are handled here
37317c478bd9Sstevel@tonic-gate 	 */
37327c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
37337c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
37347c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
37357c478bd9Sstevel@tonic-gate 		return (0);
37367c478bd9Sstevel@tonic-gate 	}
37377c478bd9Sstevel@tonic-gate 
37387c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
37397c478bd9Sstevel@tonic-gate 
3740b610f78eSvikram 	/*
3741b610f78eSvikram 	 * Remove any trailing / from the mount point
3742b610f78eSvikram 	 */
3743b610f78eSvikram 	(void) strlcpy(mntpt, root, sizeof (mntpt));
3744b610f78eSvikram 	if (strcmp(root, "/") != 0) {
3745b610f78eSvikram 		cp = mntpt + strlen(mntpt) - 1;
3746b610f78eSvikram 		if (*cp == '/')
3747b610f78eSvikram 			*cp = '\0';
3748b610f78eSvikram 	}
37497c478bd9Sstevel@tonic-gate 	found = 0;
37507c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
3751b610f78eSvikram 		if (strcmp(mnt.mnt_mountp, mntpt) == 0) {
37527c478bd9Sstevel@tonic-gate 			found = 1;
37537c478bd9Sstevel@tonic-gate 			break;
37547c478bd9Sstevel@tonic-gate 		}
37557c478bd9Sstevel@tonic-gate 	}
37567c478bd9Sstevel@tonic-gate 
37577c478bd9Sstevel@tonic-gate 	if (!found) {
37587c478bd9Sstevel@tonic-gate 		if (bam_verbose)
3759b610f78eSvikram 			bam_error(NOT_IN_MNTTAB, mntpt);
37607c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
37617c478bd9Sstevel@tonic-gate 		return (0);
37627c478bd9Sstevel@tonic-gate 	}
37637c478bd9Sstevel@tonic-gate 
376408db0dbcSAlexander Eremin 	if (strncmp(mnt.mnt_special, RAMDISK_SPECIAL,
376508db0dbcSAlexander Eremin 	    strlen(RAMDISK_SPECIAL)) == 0) {
37667c478bd9Sstevel@tonic-gate 		if (bam_verbose)
37677c478bd9Sstevel@tonic-gate 			bam_error(IS_RAMDISK, bam_root);
37687c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
37697c478bd9Sstevel@tonic-gate 		return (1);
37707c478bd9Sstevel@tonic-gate 	}
37717c478bd9Sstevel@tonic-gate 
37727c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
37737c478bd9Sstevel@tonic-gate 
37747c478bd9Sstevel@tonic-gate 	return (0);
37757c478bd9Sstevel@tonic-gate }
37767c478bd9Sstevel@tonic-gate 
37777c478bd9Sstevel@tonic-gate static int
3778986fd29aSsetje is_boot_archive(char *root)
37797c478bd9Sstevel@tonic-gate {
37807c478bd9Sstevel@tonic-gate 	char		path[PATH_MAX];
37817c478bd9Sstevel@tonic-gate 	struct stat	sb;
3782eb2bd662Svikram 	int		error;
3783eb2bd662Svikram 	const char	*fcn = "is_boot_archive()";
37847c478bd9Sstevel@tonic-gate 
37857c478bd9Sstevel@tonic-gate 	/*
3786986fd29aSsetje 	 * We can't create an archive without the create_ramdisk script
37877c478bd9Sstevel@tonic-gate 	 */
3788986fd29aSsetje 	(void) snprintf(path, sizeof (path), "%s/%s", root, CREATE_RAMDISK);
3789eb2bd662Svikram 	error = stat(path, &sb);
3790eb2bd662Svikram 	INJECT_ERROR1("NOT_ARCHIVE_BASED", error = -1);
3791eb2bd662Svikram 	if (error == -1) {
37927c478bd9Sstevel@tonic-gate 		if (bam_verbose)
37937c478bd9Sstevel@tonic-gate 			bam_print(FILE_MISS, path);
3794eb2bd662Svikram 		BAM_DPRINTF((D_NOT_ARCHIVE_BOOT, fcn, root));
37957c478bd9Sstevel@tonic-gate 		return (0);
37967c478bd9Sstevel@tonic-gate 	}
37977c478bd9Sstevel@tonic-gate 
3798eb2bd662Svikram 	BAM_DPRINTF((D_IS_ARCHIVE_BOOT, fcn, root));
3799986fd29aSsetje 	return (1);
3800986fd29aSsetje }
3801986fd29aSsetje 
38027c478bd9Sstevel@tonic-gate /*
3803986fd29aSsetje  * Need to call this for anything that operates on the GRUB menu
3804963390b4Svikram  * In the x86 live upgrade case the directory /boot/grub may be present
3805963390b4Svikram  * even on pre-newboot BEs. The authoritative way to check for a GRUB target
3806963390b4Svikram  * is to check for the presence of the stage2 binary which is present
3807963390b4Svikram  * only on GRUB targets (even on x86 boot partitions). Checking for the
3808963390b4Svikram  * presence of the multiboot binary is not correct as it is not present
3809963390b4Svikram  * on x86 boot partitions.
3810986fd29aSsetje  */
3811986fd29aSsetje int
3812986fd29aSsetje is_grub(const char *root)
3813986fd29aSsetje {
3814986fd29aSsetje 	char path[PATH_MAX];
3815986fd29aSsetje 	struct stat sb;
3816eb2bd662Svikram 	const char *fcn = "is_grub()";
3817986fd29aSsetje 
3818963390b4Svikram 	(void) snprintf(path, sizeof (path), "%s%s", root, GRUB_STAGE2);
38197c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
3820eb2bd662Svikram 		BAM_DPRINTF((D_NO_GRUB_DIR, fcn, path));
38217c478bd9Sstevel@tonic-gate 		return (0);
38227c478bd9Sstevel@tonic-gate 	}
38237c478bd9Sstevel@tonic-gate 
38247c478bd9Sstevel@tonic-gate 	return (1);
38257c478bd9Sstevel@tonic-gate }
38267c478bd9Sstevel@tonic-gate 
38277c478bd9Sstevel@tonic-gate static int
3828eb2bd662Svikram is_zfs(char *root)
3829eb2bd662Svikram {
3830eb2bd662Svikram 	struct statvfs		vfs;
3831eb2bd662Svikram 	int			ret;
3832eb2bd662Svikram 	const char		*fcn = "is_zfs()";
3833eb2bd662Svikram 
3834eb2bd662Svikram 	ret = statvfs(root, &vfs);
3835eb2bd662Svikram 	INJECT_ERROR1("STATVFS_ZFS", ret = 1);
3836eb2bd662Svikram 	if (ret != 0) {
3837eb2bd662Svikram 		bam_error(STATVFS_FAIL, root, strerror(errno));
3838eb2bd662Svikram 		return (0);
3839eb2bd662Svikram 	}
3840eb2bd662Svikram 
3841eb2bd662Svikram 	if (strncmp(vfs.f_basetype, "zfs", strlen("zfs")) == 0) {
3842eb2bd662Svikram 		BAM_DPRINTF((D_IS_ZFS, fcn, root));
3843eb2bd662Svikram 		return (1);
3844eb2bd662Svikram 	} else {
3845eb2bd662Svikram 		BAM_DPRINTF((D_IS_NOT_ZFS, fcn, root));
3846eb2bd662Svikram 		return (0);
3847eb2bd662Svikram 	}
3848eb2bd662Svikram }
3849eb2bd662Svikram 
3850eb2bd662Svikram static int
3851eb2bd662Svikram is_ufs(char *root)
3852eb2bd662Svikram {
3853eb2bd662Svikram 	struct statvfs		vfs;
3854eb2bd662Svikram 	int			ret;
3855eb2bd662Svikram 	const char		*fcn = "is_ufs()";
3856eb2bd662Svikram 
3857eb2bd662Svikram 	ret = statvfs(root, &vfs);
3858eb2bd662Svikram 	INJECT_ERROR1("STATVFS_UFS", ret = 1);
3859eb2bd662Svikram 	if (ret != 0) {
3860eb2bd662Svikram 		bam_error(STATVFS_FAIL, root, strerror(errno));
3861eb2bd662Svikram 		return (0);
3862eb2bd662Svikram 	}
3863eb2bd662Svikram 
3864eb2bd662Svikram 	if (strncmp(vfs.f_basetype, "ufs", strlen("ufs")) == 0) {
3865eb2bd662Svikram 		BAM_DPRINTF((D_IS_UFS, fcn, root));
3866eb2bd662Svikram 		return (1);
3867eb2bd662Svikram 	} else {
3868eb2bd662Svikram 		BAM_DPRINTF((D_IS_NOT_UFS, fcn, root));
3869eb2bd662Svikram 		return (0);
3870eb2bd662Svikram 	}
3871eb2bd662Svikram }
3872eb2bd662Svikram 
3873eb2bd662Svikram static int
3874eb2bd662Svikram is_pcfs(char *root)
3875eb2bd662Svikram {
3876eb2bd662Svikram 	struct statvfs		vfs;
3877eb2bd662Svikram 	int			ret;
3878eb2bd662Svikram 	const char		*fcn = "is_pcfs()";
3879eb2bd662Svikram 
3880eb2bd662Svikram 	ret = statvfs(root, &vfs);
3881eb2bd662Svikram 	INJECT_ERROR1("STATVFS_PCFS", ret = 1);
3882eb2bd662Svikram 	if (ret != 0) {
3883eb2bd662Svikram 		bam_error(STATVFS_FAIL, root, strerror(errno));
3884eb2bd662Svikram 		return (0);
3885eb2bd662Svikram 	}
3886eb2bd662Svikram 
3887eb2bd662Svikram 	if (strncmp(vfs.f_basetype, "pcfs", strlen("pcfs")) == 0) {
3888eb2bd662Svikram 		BAM_DPRINTF((D_IS_PCFS, fcn, root));
3889eb2bd662Svikram 		return (1);
3890eb2bd662Svikram 	} else {
3891eb2bd662Svikram 		BAM_DPRINTF((D_IS_NOT_PCFS, fcn, root));
3892eb2bd662Svikram 		return (0);
3893eb2bd662Svikram 	}
3894eb2bd662Svikram }
3895eb2bd662Svikram 
3896eb2bd662Svikram static int
38977c478bd9Sstevel@tonic-gate is_readonly(char *root)
38987c478bd9Sstevel@tonic-gate {
3899eb2bd662Svikram 	int		fd;
3900eb2bd662Svikram 	int		error;
3901eb2bd662Svikram 	char		testfile[PATH_MAX];
3902eb2bd662Svikram 	const char	*fcn = "is_readonly()";
39037c478bd9Sstevel@tonic-gate 
39047c478bd9Sstevel@tonic-gate 	/*
3905eb2bd662Svikram 	 * Using statvfs() to check for a read-only filesystem is not
3906eb2bd662Svikram 	 * reliable. The only way to reliably test is to attempt to
3907eb2bd662Svikram 	 * create a file
39087c478bd9Sstevel@tonic-gate 	 */
3909eb2bd662Svikram 	(void) snprintf(testfile, sizeof (testfile), "%s/%s.%d",
3910eb2bd662Svikram 	    root, BOOTADM_RDONLY_TEST, getpid());
39117c478bd9Sstevel@tonic-gate 
3912eb2bd662Svikram 	(void) unlink(testfile);
3913eb2bd662Svikram 
3914eb2bd662Svikram 	errno = 0;
3915eb2bd662Svikram 	fd = open(testfile, O_RDWR|O_CREAT|O_EXCL, 0644);
3916eb2bd662Svikram 	error = errno;
3917eb2bd662Svikram 	INJECT_ERROR2("RDONLY_TEST_ERROR", fd = -1, error = EACCES);
3918eb2bd662Svikram 	if (fd == -1 && error == EROFS) {
3919eb2bd662Svikram 		BAM_DPRINTF((D_RDONLY_FS, fcn, root));
39207c478bd9Sstevel@tonic-gate 		return (1);
3921eb2bd662Svikram 	} else if (fd == -1) {
3922eb2bd662Svikram 		bam_error(RDONLY_TEST_ERROR, root, strerror(error));
39237c478bd9Sstevel@tonic-gate 	}
39247c478bd9Sstevel@tonic-gate 
3925eb2bd662Svikram 	(void) close(fd);
3926eb2bd662Svikram 	(void) unlink(testfile);
39277c478bd9Sstevel@tonic-gate 
3928eb2bd662Svikram 	BAM_DPRINTF((D_RDWR_FS, fcn, root));
3929e7cbe64fSgw25295 	return (0);
3930e7cbe64fSgw25295 }
3931e7cbe64fSgw25295 
39327c478bd9Sstevel@tonic-gate static error_t
39337c478bd9Sstevel@tonic-gate update_archive(char *root, char *opt)
39347c478bd9Sstevel@tonic-gate {
39357c478bd9Sstevel@tonic-gate 	error_t ret;
39367c478bd9Sstevel@tonic-gate 
39377c478bd9Sstevel@tonic-gate 	assert(root);
39387c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
39397c478bd9Sstevel@tonic-gate 
394048847494SEnrico Perla - Sun Microsystems 	init_walk_args();
394148847494SEnrico Perla - Sun Microsystems 	(void) umask(022);
394248847494SEnrico Perla - Sun Microsystems 
39437c478bd9Sstevel@tonic-gate 	/*
39446debd3f5SAlexander Eremin 	 * Never update non-BE root in update_all
39456debd3f5SAlexander Eremin 	 */
39466debd3f5SAlexander Eremin 	if (!is_be(root) && bam_update_all)
39476debd3f5SAlexander Eremin 		return (BAM_SUCCESS);
39486debd3f5SAlexander Eremin 	/*
3949986fd29aSsetje 	 * root must belong to a boot archive based OS,
39507c478bd9Sstevel@tonic-gate 	 */
3951986fd29aSsetje 	if (!is_boot_archive(root)) {
3952b610f78eSvikram 		/*
3953b610f78eSvikram 		 * Emit message only if not in context of update_all.
3954b610f78eSvikram 		 * If in update_all, emit only if verbose flag is set.
3955b610f78eSvikram 		 */
3956b610f78eSvikram 		if (!bam_update_all || bam_verbose)
3957eb2bd662Svikram 			bam_print(NOT_ARCHIVE_BOOT, root);
395848847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
39597c478bd9Sstevel@tonic-gate 	}
39607c478bd9Sstevel@tonic-gate 
39617c478bd9Sstevel@tonic-gate 	/*
39628c1b6884Sszhou 	 * If smf check is requested when / is writable (can happen
39638c1b6884Sszhou 	 * on first reboot following an upgrade because service
39648c1b6884Sszhou 	 * dependency is messed up), skip the check.
39658c1b6884Sszhou 	 */
396648847494SEnrico Perla - Sun Microsystems 	if (bam_smf_check && !bam_root_readonly && !is_zfs(root))
39678c1b6884Sszhou 		return (BAM_SUCCESS);
39688c1b6884Sszhou 
39698c1b6884Sszhou 	/*
39705eea6091SEnrico Perla - Sun Microsystems 	 * Don't generate archive on ramdisk.
39715eea6091SEnrico Perla - Sun Microsystems 	 */
39725eea6091SEnrico Perla - Sun Microsystems 	if (is_ramdisk(root))
39735eea6091SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
39745eea6091SEnrico Perla - Sun Microsystems 
39755eea6091SEnrico Perla - Sun Microsystems 	/*
39768c1b6884Sszhou 	 * root must be writable. This check applies to alternate
39778c1b6884Sszhou 	 * root (-R option); bam_root_readonly applies to '/' only.
397848847494SEnrico Perla - Sun Microsystems 	 * The behaviour translates into being the one of a 'check'.
39797c478bd9Sstevel@tonic-gate 	 */
398061cb17bdSsetje 	if (!bam_smf_check && !bam_check && is_readonly(root)) {
398148847494SEnrico Perla - Sun Microsystems 		set_flag(RDONLY_FSCHK);
398248847494SEnrico Perla - Sun Microsystems 		bam_check = 1;
39837c478bd9Sstevel@tonic-gate 	}
39847c478bd9Sstevel@tonic-gate 
39857c478bd9Sstevel@tonic-gate 	/*
3986cedc7e57SEnrico Perla - Sun Microsystems 	 * Now check if an update is really needed.
39877c478bd9Sstevel@tonic-gate 	 */
39887c478bd9Sstevel@tonic-gate 	ret = update_required(root);
39897c478bd9Sstevel@tonic-gate 
39907c478bd9Sstevel@tonic-gate 	/*
3991cedc7e57SEnrico Perla - Sun Microsystems 	 * The check command (-n) is *not* a dry run.
39927c478bd9Sstevel@tonic-gate 	 * It only checks if the archive is in sync.
3993cedc7e57SEnrico Perla - Sun Microsystems 	 * A readonly filesystem has to be considered an error only if an update
3994cedc7e57SEnrico Perla - Sun Microsystems 	 * is required.
39957c478bd9Sstevel@tonic-gate 	 */
3996cedc7e57SEnrico Perla - Sun Microsystems 	if (bam_nowrite()) {
399748847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(RDONLY_FSCHK)) {
39985eea6091SEnrico Perla - Sun Microsystems 			bam_check = bam_saved_check;
399948847494SEnrico Perla - Sun Microsystems 			if (ret > 0)
400048847494SEnrico Perla - Sun Microsystems 				bam_error(RDONLY_FS, root);
400148847494SEnrico Perla - Sun Microsystems 			if (bam_update_all)
400248847494SEnrico Perla - Sun Microsystems 				return ((ret != 0) ? BAM_ERROR : BAM_SUCCESS);
400348847494SEnrico Perla - Sun Microsystems 		}
400448847494SEnrico Perla - Sun Microsystems 
40057c478bd9Sstevel@tonic-gate 		bam_exit((ret != 0) ? 1 : 0);
40067c478bd9Sstevel@tonic-gate 	}
40077c478bd9Sstevel@tonic-gate 
40087c478bd9Sstevel@tonic-gate 	if (ret == 1) {
40097c478bd9Sstevel@tonic-gate 		/* create the ramdisk */
40107c478bd9Sstevel@tonic-gate 		ret = create_ramdisk(root);
40117c478bd9Sstevel@tonic-gate 	}
4012986fd29aSsetje 
40134a9df875SEnrico Perla - Sun Microsystems 	/*
40144a9df875SEnrico Perla - Sun Microsystems 	 * if the archive is updated, save the new stat data and update the
40154a9df875SEnrico Perla - Sun Microsystems 	 * timestamp file
40164a9df875SEnrico Perla - Sun Microsystems 	 */
4017986fd29aSsetje 	if (ret == 0 && walk_arg.new_nvlp != NULL) {
4018986fd29aSsetje 		savenew(root);
40194a9df875SEnrico Perla - Sun Microsystems 		update_timestamp(root);
4020986fd29aSsetje 	}
4021986fd29aSsetje 
4022986fd29aSsetje 	clear_walk_args();
4023986fd29aSsetje 
40247c478bd9Sstevel@tonic-gate 	return (ret);
40257c478bd9Sstevel@tonic-gate }
40267c478bd9Sstevel@tonic-gate 
402740b108d7SMark J Musante static char *
402840b108d7SMark J Musante find_root_pool()
402940b108d7SMark J Musante {
403040b108d7SMark J Musante 	char *special = get_special("/");
403140b108d7SMark J Musante 	char *p;
403240b108d7SMark J Musante 
403340b108d7SMark J Musante 	if (special == NULL)
403440b108d7SMark J Musante 		return (NULL);
403540b108d7SMark J Musante 
403640b108d7SMark J Musante 	if (*special == '/') {
403740b108d7SMark J Musante 		free(special);
403840b108d7SMark J Musante 		return (NULL);
403940b108d7SMark J Musante 	}
404040b108d7SMark J Musante 
404140b108d7SMark J Musante 	if ((p = strchr(special, '/')) != NULL)
404240b108d7SMark J Musante 		*p = '\0';
404340b108d7SMark J Musante 
404440b108d7SMark J Musante 	return (special);
404540b108d7SMark J Musante }
404640b108d7SMark J Musante 
4047963390b4Svikram static error_t
4048963390b4Svikram synchronize_BE_menu(void)
4049fbac2b2bSvikram {
4050fbac2b2bSvikram 	struct stat	sb;
4051963390b4Svikram 	char		cmdline[PATH_MAX];
4052963390b4Svikram 	char		cksum_line[PATH_MAX];
4053963390b4Svikram 	filelist_t	flist = {0};
4054963390b4Svikram 	char		*old_cksum_str;
4055963390b4Svikram 	char		*old_size_str;
4056963390b4Svikram 	char		*old_file;
4057963390b4Svikram 	char		*curr_cksum_str;
4058963390b4Svikram 	char		*curr_size_str;
4059963390b4Svikram 	char		*curr_file;
40601130146cSGangadhar Mylapuram 	char		*pool = NULL;
40611130146cSGangadhar Mylapuram 	char		*mntpt = NULL;
40621130146cSGangadhar Mylapuram 	zfs_mnted_t	mnted;
4063963390b4Svikram 	FILE		*cfp;
4064963390b4Svikram 	int		found;
4065963390b4Svikram 	int		ret;
4066963390b4Svikram 	const char	*fcn = "synchronize_BE_menu()";
4067fbac2b2bSvikram 
4068963390b4Svikram 	BAM_DPRINTF((D_FUNC_ENTRY0, fcn));
4069fbac2b2bSvikram 
4070963390b4Svikram 	/* Check if findroot enabled LU BE */
4071963390b4Svikram 	if (stat(FINDROOT_INSTALLGRUB, &sb) != 0) {
4072963390b4Svikram 		BAM_DPRINTF((D_NOT_LU_BE, fcn));
4073963390b4Svikram 		return (BAM_SUCCESS);
4074fbac2b2bSvikram 	}
4075fbac2b2bSvikram 
4076963390b4Svikram 	if (stat(LU_MENU_CKSUM, &sb) != 0) {
4077963390b4Svikram 		BAM_DPRINTF((D_NO_CKSUM_FILE, fcn, LU_MENU_CKSUM));
4078963390b4Svikram 		goto menu_sync;
4079fbac2b2bSvikram 	}
4080963390b4Svikram 
4081963390b4Svikram 	cfp = fopen(LU_MENU_CKSUM, "r");
4082963390b4Svikram 	INJECT_ERROR1("CKSUM_FILE_MISSING", cfp = NULL);
4083963390b4Svikram 	if (cfp == NULL) {
4084963390b4Svikram 		bam_error(CANNOT_READ_LU_CKSUM, LU_MENU_CKSUM);
4085963390b4Svikram 		goto menu_sync;
4086963390b4Svikram 	}
4087963390b4Svikram 	BAM_DPRINTF((D_CKSUM_FILE_OPENED, fcn, LU_MENU_CKSUM));
4088963390b4Svikram 
4089963390b4Svikram 	found = 0;
4090963390b4Svikram 	while (s_fgets(cksum_line, sizeof (cksum_line), cfp) != NULL) {
4091963390b4Svikram 		INJECT_ERROR1("MULTIPLE_CKSUM", found = 1);
4092963390b4Svikram 		if (found) {
4093963390b4Svikram 			bam_error(MULTIPLE_LU_CKSUM, LU_MENU_CKSUM);
4094963390b4Svikram 			(void) fclose(cfp);
4095963390b4Svikram 			goto menu_sync;
4096963390b4Svikram 		}
4097963390b4Svikram 		found = 1;
4098963390b4Svikram 	}
4099963390b4Svikram 	BAM_DPRINTF((D_CKSUM_FILE_READ, fcn, LU_MENU_CKSUM));
4100963390b4Svikram 
4101963390b4Svikram 
4102963390b4Svikram 	old_cksum_str = strtok(cksum_line, " \t");
4103963390b4Svikram 	old_size_str = strtok(NULL, " \t");
4104963390b4Svikram 	old_file = strtok(NULL, " \t");
4105963390b4Svikram 
4106963390b4Svikram 	INJECT_ERROR1("OLD_CKSUM_NULL", old_cksum_str = NULL);
4107963390b4Svikram 	INJECT_ERROR1("OLD_SIZE_NULL", old_size_str = NULL);
4108963390b4Svikram 	INJECT_ERROR1("OLD_FILE_NULL", old_file = NULL);
4109963390b4Svikram 	if (old_cksum_str == NULL || old_size_str == NULL || old_file == NULL) {
4110963390b4Svikram 		bam_error(CANNOT_PARSE_LU_CKSUM, LU_MENU_CKSUM);
4111963390b4Svikram 		goto menu_sync;
4112963390b4Svikram 	}
4113963390b4Svikram 	BAM_DPRINTF((D_CKSUM_FILE_PARSED, fcn, LU_MENU_CKSUM));
4114963390b4Svikram 
4115963390b4Svikram 	/* Get checksum of current menu */
411640b108d7SMark J Musante 	pool = find_root_pool();
41171130146cSGangadhar Mylapuram 	if (pool) {
41181130146cSGangadhar Mylapuram 		mntpt = mount_top_dataset(pool, &mnted);
41191130146cSGangadhar Mylapuram 		if (mntpt == NULL) {
41201130146cSGangadhar Mylapuram 			bam_error(FAIL_MNT_TOP_DATASET, pool);
412140b108d7SMark J Musante 			free(pool);
41221130146cSGangadhar Mylapuram 			return (BAM_ERROR);
41231130146cSGangadhar Mylapuram 		}
41241130146cSGangadhar Mylapuram 		(void) snprintf(cmdline, sizeof (cmdline), "%s %s%s",
41251130146cSGangadhar Mylapuram 		    CKSUM, mntpt, GRUB_MENU);
41261130146cSGangadhar Mylapuram 	} else {
41271130146cSGangadhar Mylapuram 		(void) snprintf(cmdline, sizeof (cmdline), "%s %s",
41281130146cSGangadhar Mylapuram 		    CKSUM, GRUB_MENU);
41291130146cSGangadhar Mylapuram 	}
4130963390b4Svikram 	ret = exec_cmd(cmdline, &flist);
41311130146cSGangadhar Mylapuram 	if (pool) {
41321130146cSGangadhar Mylapuram 		(void) umount_top_dataset(pool, mnted, mntpt);
41331130146cSGangadhar Mylapuram 		free(pool);
41341130146cSGangadhar Mylapuram 	}
4135963390b4Svikram 	INJECT_ERROR1("GET_CURR_CKSUM", ret = 1);
4136963390b4Svikram 	if (ret != 0) {
4137963390b4Svikram 		bam_error(MENU_CKSUM_FAIL);
4138963390b4Svikram 		return (BAM_ERROR);
4139963390b4Svikram 	}
4140963390b4Svikram 	BAM_DPRINTF((D_CKSUM_GEN_SUCCESS, fcn));
4141963390b4Svikram 
4142963390b4Svikram 	INJECT_ERROR1("GET_CURR_CKSUM_OUTPUT", flist.head = NULL);
4143963390b4Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
4144963390b4Svikram 		bam_error(BAD_CKSUM);
4145963390b4Svikram 		filelist_free(&flist);
4146963390b4Svikram 		return (BAM_ERROR);
4147963390b4Svikram 	}
4148963390b4Svikram 	BAM_DPRINTF((D_CKSUM_GEN_OUTPUT_VALID, fcn));
4149963390b4Svikram 
4150963390b4Svikram 	curr_cksum_str = strtok(flist.head->line, " \t");
4151963390b4Svikram 	curr_size_str = strtok(NULL, " \t");
4152963390b4Svikram 	curr_file = strtok(NULL, " \t");
4153963390b4Svikram 
4154963390b4Svikram 	INJECT_ERROR1("CURR_CKSUM_NULL", curr_cksum_str = NULL);
4155963390b4Svikram 	INJECT_ERROR1("CURR_SIZE_NULL", curr_size_str = NULL);
4156963390b4Svikram 	INJECT_ERROR1("CURR_FILE_NULL", curr_file = NULL);
4157963390b4Svikram 	if (curr_cksum_str == NULL || curr_size_str == NULL ||
4158963390b4Svikram 	    curr_file == NULL) {
4159963390b4Svikram 		bam_error(BAD_CKSUM_PARSE);
4160963390b4Svikram 		filelist_free(&flist);
4161963390b4Svikram 		return (BAM_ERROR);
4162963390b4Svikram 	}
4163963390b4Svikram 	BAM_DPRINTF((D_CKSUM_GEN_PARSED, fcn));
4164963390b4Svikram 
4165963390b4Svikram 	if (strcmp(old_cksum_str, curr_cksum_str) == 0 &&
4166963390b4Svikram 	    strcmp(old_size_str, curr_size_str) == 0 &&
4167963390b4Svikram 	    strcmp(old_file, curr_file) == 0) {
4168963390b4Svikram 		filelist_free(&flist);
4169963390b4Svikram 		BAM_DPRINTF((D_CKSUM_NO_CHANGE, fcn));
4170963390b4Svikram 		return (BAM_SUCCESS);
4171963390b4Svikram 	}
4172963390b4Svikram 
4173963390b4Svikram 	filelist_free(&flist);
4174963390b4Svikram 
4175963390b4Svikram 	/* cksum doesn't match - the menu has changed */
4176963390b4Svikram 	BAM_DPRINTF((D_CKSUM_HAS_CHANGED, fcn));
4177963390b4Svikram 
4178963390b4Svikram menu_sync:
4179963390b4Svikram 	bam_print(PROP_GRUB_MENU);
4180963390b4Svikram 
4181963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
4182a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s yes > /dev/null'",
4183963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, GRUB_MENU);
4184963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
4185963390b4Svikram 	INJECT_ERROR1("PROPAGATE_MENU", ret = 1);
4186963390b4Svikram 	if (ret != 0) {
4187963390b4Svikram 		bam_error(MENU_PROP_FAIL);
4188963390b4Svikram 		return (BAM_ERROR);
4189963390b4Svikram 	}
4190963390b4Svikram 	BAM_DPRINTF((D_PROPAGATED_MENU, fcn));
4191963390b4Svikram 
4192a6968364Svikram 	(void) snprintf(cmdline, sizeof (cmdline), "/bin/cp %s %s > /dev/null",
4193963390b4Svikram 	    GRUB_MENU, GRUB_BACKUP_MENU);
4194963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
4195963390b4Svikram 	INJECT_ERROR1("CREATE_BACKUP", ret = 1);
4196963390b4Svikram 	if (ret != 0) {
4197963390b4Svikram 		bam_error(MENU_BACKUP_FAIL, GRUB_BACKUP_MENU);
4198963390b4Svikram 		return (BAM_ERROR);
4199963390b4Svikram 	}
4200963390b4Svikram 	BAM_DPRINTF((D_CREATED_BACKUP, fcn, GRUB_BACKUP_MENU));
4201963390b4Svikram 
4202963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
4203a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s no > /dev/null'",
4204963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, GRUB_BACKUP_MENU);
4205963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
4206963390b4Svikram 	INJECT_ERROR1("PROPAGATE_BACKUP", ret = 1);
4207963390b4Svikram 	if (ret != 0) {
4208963390b4Svikram 		bam_error(BACKUP_PROP_FAIL, GRUB_BACKUP_MENU);
4209963390b4Svikram 		return (BAM_ERROR);
4210963390b4Svikram 	}
4211963390b4Svikram 	BAM_DPRINTF((D_PROPAGATED_BACKUP, fcn, GRUB_BACKUP_MENU));
4212963390b4Svikram 
4213963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s > %s",
4214963390b4Svikram 	    CKSUM, GRUB_MENU, LU_MENU_CKSUM);
4215963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
4216963390b4Svikram 	INJECT_ERROR1("CREATE_CKSUM_FILE", ret = 1);
4217963390b4Svikram 	if (ret != 0) {
4218963390b4Svikram 		bam_error(MENU_CKSUM_WRITE_FAIL, LU_MENU_CKSUM);
4219963390b4Svikram 		return (BAM_ERROR);
4220963390b4Svikram 	}
4221963390b4Svikram 	BAM_DPRINTF((D_CREATED_CKSUM_FILE, fcn, LU_MENU_CKSUM));
4222963390b4Svikram 
4223963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
4224a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s no > /dev/null'",
4225963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, LU_MENU_CKSUM);
4226963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
4227963390b4Svikram 	INJECT_ERROR1("PROPAGATE_MENU_CKSUM_FILE", ret = 1);
4228963390b4Svikram 	if (ret != 0) {
4229963390b4Svikram 		bam_error(MENU_CKSUM_PROP_FAIL, LU_MENU_CKSUM);
4230963390b4Svikram 		return (BAM_ERROR);
4231963390b4Svikram 	}
4232963390b4Svikram 	BAM_DPRINTF((D_PROPAGATED_CKSUM_FILE, fcn, LU_MENU_CKSUM));
4233963390b4Svikram 
4234963390b4Svikram 	return (BAM_SUCCESS);
4235fbac2b2bSvikram }
4236fbac2b2bSvikram 
42377c478bd9Sstevel@tonic-gate static error_t
42387c478bd9Sstevel@tonic-gate update_all(char *root, char *opt)
42397c478bd9Sstevel@tonic-gate {
42407c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
42417c478bd9Sstevel@tonic-gate 	struct stat sb;
42427c478bd9Sstevel@tonic-gate 	FILE *fp;
42437c478bd9Sstevel@tonic-gate 	char multibt[PATH_MAX];
4244986fd29aSsetje 	char creatram[PATH_MAX];
42457c478bd9Sstevel@tonic-gate 	error_t ret = BAM_SUCCESS;
42467c478bd9Sstevel@tonic-gate 
424740541d5dSvikram 	assert(root);
42487c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
42497c478bd9Sstevel@tonic-gate 
425040541d5dSvikram 	if (bam_rootlen != 1 || *root != '/') {
425140541d5dSvikram 		elide_trailing_slash(root, multibt, sizeof (multibt));
425240541d5dSvikram 		bam_error(ALT_ROOT_INVALID, multibt);
425340541d5dSvikram 		return (BAM_ERROR);
425440541d5dSvikram 	}
425540541d5dSvikram 
42567c478bd9Sstevel@tonic-gate 	/*
42577c478bd9Sstevel@tonic-gate 	 * First update archive for current root
42587c478bd9Sstevel@tonic-gate 	 */
42597c478bd9Sstevel@tonic-gate 	if (update_archive(root, opt) != BAM_SUCCESS)
42607c478bd9Sstevel@tonic-gate 		ret = BAM_ERROR;
426198892a30Snadkarni 
426298892a30Snadkarni 	if (ret == BAM_ERROR)
426398892a30Snadkarni 		goto out;
42647c478bd9Sstevel@tonic-gate 
42657c478bd9Sstevel@tonic-gate 	/*
42667c478bd9Sstevel@tonic-gate 	 * Now walk the mount table, performing archive update
42677c478bd9Sstevel@tonic-gate 	 * for all mounted Newboot root filesystems
42687c478bd9Sstevel@tonic-gate 	 */
42697c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
42707c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
42717c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
4272b610f78eSvikram 		ret = BAM_ERROR;
4273b610f78eSvikram 		goto out;
42747c478bd9Sstevel@tonic-gate 	}
42757c478bd9Sstevel@tonic-gate 
42767c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
42777c478bd9Sstevel@tonic-gate 
42787c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
42797c478bd9Sstevel@tonic-gate 		if (mnt.mnt_special == NULL)
42807c478bd9Sstevel@tonic-gate 			continue;
4281f904d32dSJerry Gilliam 		if ((strcmp(mnt.mnt_fstype, MNTTYPE_ZFS) != 0) &&
4282f904d32dSJerry Gilliam 		    (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0))
42837c478bd9Sstevel@tonic-gate 			continue;
42847c478bd9Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, "/") == 0)
42857c478bd9Sstevel@tonic-gate 			continue;
42867c478bd9Sstevel@tonic-gate 
4287986fd29aSsetje 		(void) snprintf(creatram, sizeof (creatram), "%s/%s",
4288986fd29aSsetje 		    mnt.mnt_mountp, CREATE_RAMDISK);
42897c478bd9Sstevel@tonic-gate 
4290986fd29aSsetje 		if (stat(creatram, &sb) == -1)
42917c478bd9Sstevel@tonic-gate 			continue;
42927c478bd9Sstevel@tonic-gate 
42937c478bd9Sstevel@tonic-gate 		/*
42947c478bd9Sstevel@tonic-gate 		 * We put a trailing slash to be consistent with root = "/"
42957c478bd9Sstevel@tonic-gate 		 * case, such that we don't have to print // in some cases.
42967c478bd9Sstevel@tonic-gate 		 */
42977c478bd9Sstevel@tonic-gate 		(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
42987c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp);
42997c478bd9Sstevel@tonic-gate 		bam_rootlen = strlen(rootbuf);
4300ae115bc7Smrj 
4301ae115bc7Smrj 		/*
4302ae115bc7Smrj 		 * It's possible that other mounts may be an alternate boot
4303ae115bc7Smrj 		 * architecture, so check it again.
4304ae115bc7Smrj 		 */
4305eb2bd662Svikram 		if ((get_boot_cap(rootbuf) != BAM_SUCCESS) ||
4306ae115bc7Smrj 		    (update_archive(rootbuf, opt) != BAM_SUCCESS))
43077c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
43087c478bd9Sstevel@tonic-gate 	}
43097c478bd9Sstevel@tonic-gate 
43107c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
43117c478bd9Sstevel@tonic-gate 
4312b610f78eSvikram out:
4313fbac2b2bSvikram 	/*
4314963390b4Svikram 	 * We no longer use biosdev for Live Upgrade. Hence
4315963390b4Svikram 	 * there is no need to defer (to shutdown time) any fdisk
4316963390b4Svikram 	 * updates
4317fbac2b2bSvikram 	 */
4318963390b4Svikram 	if (stat(GRUB_fdisk, &sb) == 0 || stat(GRUB_fdisk_target, &sb) == 0) {
4319963390b4Svikram 		bam_error(FDISK_FILES_FOUND, GRUB_fdisk, GRUB_fdisk_target);
4320fbac2b2bSvikram 	}
4321fbac2b2bSvikram 
4322963390b4Svikram 	/*
4323963390b4Svikram 	 * If user has updated menu in current BE, propagate the
4324963390b4Svikram 	 * updates to all BEs.
4325963390b4Svikram 	 */
432619397407SSherry Moore 	if (sync_menu && synchronize_BE_menu() != BAM_SUCCESS)
4327963390b4Svikram 		ret = BAM_ERROR;
4328963390b4Svikram 
43297c478bd9Sstevel@tonic-gate 	return (ret);
43307c478bd9Sstevel@tonic-gate }
43317c478bd9Sstevel@tonic-gate 
43327c478bd9Sstevel@tonic-gate static void
43337c478bd9Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp)
43347c478bd9Sstevel@tonic-gate {
43357c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
43367c478bd9Sstevel@tonic-gate 		mp->start = lp;
43377c478bd9Sstevel@tonic-gate 	} else {
43387c478bd9Sstevel@tonic-gate 		mp->end->next = lp;
43398c1b6884Sszhou 		lp->prev = mp->end;
43407c478bd9Sstevel@tonic-gate 	}
43417c478bd9Sstevel@tonic-gate 	mp->end = lp;
43427c478bd9Sstevel@tonic-gate }
43437c478bd9Sstevel@tonic-gate 
4344eb2bd662Svikram void
43458c1b6884Sszhou unlink_line(menu_t *mp, line_t *lp)
43468c1b6884Sszhou {
43478c1b6884Sszhou 	/* unlink from list */
43488c1b6884Sszhou 	if (lp->prev)
43498c1b6884Sszhou 		lp->prev->next = lp->next;
43508c1b6884Sszhou 	else
43518c1b6884Sszhou 		mp->start = lp->next;
43528c1b6884Sszhou 	if (lp->next)
43538c1b6884Sszhou 		lp->next->prev = lp->prev;
43548c1b6884Sszhou 	else
43558c1b6884Sszhou 		mp->end = lp->prev;
43568c1b6884Sszhou }
43578c1b6884Sszhou 
43588c1b6884Sszhou static entry_t *
43598c1b6884Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end)
43608c1b6884Sszhou {
43618c1b6884Sszhou 	entry_t *ent, *prev;
4362eb2bd662Svikram 	const char *fcn = "boot_entry_new()";
4363eb2bd662Svikram 
4364eb2bd662Svikram 	assert(mp);
4365eb2bd662Svikram 	assert(start);
4366eb2bd662Svikram 	assert(end);
43678c1b6884Sszhou 
43688c1b6884Sszhou 	ent = s_calloc(1, sizeof (entry_t));
4369eb2bd662Svikram 	BAM_DPRINTF((D_ENTRY_NEW, fcn));
43708c1b6884Sszhou 	ent->start = start;
43718c1b6884Sszhou 	ent->end = end;
43728c1b6884Sszhou 
43738c1b6884Sszhou 	if (mp->entries == NULL) {
43748c1b6884Sszhou 		mp->entries = ent;
4375eb2bd662Svikram 		BAM_DPRINTF((D_ENTRY_NEW_FIRST, fcn));
43768c1b6884Sszhou 		return (ent);
43778c1b6884Sszhou 	}
43788c1b6884Sszhou 
43798c1b6884Sszhou 	prev = mp->entries;
43808c1b6884Sszhou 	while (prev->next)
43818c1b6884Sszhou 		prev = prev->next;
43828c1b6884Sszhou 	prev->next = ent;
43838c1b6884Sszhou 	ent->prev = prev;
4384eb2bd662Svikram 	BAM_DPRINTF((D_ENTRY_NEW_LINKED, fcn));
43858c1b6884Sszhou 	return (ent);
43868c1b6884Sszhou }
43878c1b6884Sszhou 
43888c1b6884Sszhou static void
43898c1b6884Sszhou boot_entry_addline(entry_t *ent, line_t *lp)
43908c1b6884Sszhou {
43918c1b6884Sszhou 	if (ent)
43928c1b6884Sszhou 		ent->end = lp;
43938c1b6884Sszhou }
43948c1b6884Sszhou 
43957c478bd9Sstevel@tonic-gate /*
4396843e1988Sjohnlev  * Check whether cmd matches the one indexed by which, and whether arg matches
4397843e1988Sjohnlev  * str.  which must be either KERNEL_CMD or MODULE_CMD, and a match to the
4398843e1988Sjohnlev  * respective *_DOLLAR_CMD is also acceptable.  The arg is searched using
4399843e1988Sjohnlev  * strstr(), so it can be a partial match.
4400843e1988Sjohnlev  */
4401843e1988Sjohnlev static int
4402843e1988Sjohnlev check_cmd(const char *cmd, const int which, const char *arg, const char *str)
4403843e1988Sjohnlev {
4404eb2bd662Svikram 	int			ret;
4405eb2bd662Svikram 	const char		*fcn = "check_cmd()";
4406eb2bd662Svikram 
4407eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, arg, str));
4408eb2bd662Svikram 
4409eac223ccSWilliam Kucharski 	if (cmd != NULL) {
4410843e1988Sjohnlev 		if ((strcmp(cmd, menu_cmds[which]) != 0) &&
4411843e1988Sjohnlev 		    (strcmp(cmd, menu_cmds[which + 1]) != 0)) {
4412eb2bd662Svikram 			BAM_DPRINTF((D_CHECK_CMD_CMD_NOMATCH,
4413eb2bd662Svikram 			    fcn, cmd, menu_cmds[which]));
4414843e1988Sjohnlev 			return (0);
4415843e1988Sjohnlev 		}
4416eb2bd662Svikram 		ret = (strstr(arg, str) != NULL);
4417eac223ccSWilliam Kucharski 	} else
4418eac223ccSWilliam Kucharski 		ret = 0;
4419eb2bd662Svikram 
4420eb2bd662Svikram 	if (ret) {
4421eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
4422eb2bd662Svikram 	} else {
4423eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
4424eb2bd662Svikram 	}
4425eb2bd662Svikram 
4426eb2bd662Svikram 	return (ret);
4427eb2bd662Svikram }
4428eb2bd662Svikram 
4429eb2bd662Svikram static error_t
4430eb2bd662Svikram kernel_parser(entry_t *entry, char *cmd, char *arg, int linenum)
4431eb2bd662Svikram {
4432eb2bd662Svikram 	const char		*fcn  = "kernel_parser()";
4433eb2bd662Svikram 
4434eb2bd662Svikram 	assert(entry);
4435eb2bd662Svikram 	assert(cmd);
4436eb2bd662Svikram 	assert(arg);
4437eb2bd662Svikram 
4438eb2bd662Svikram 	if (strcmp(cmd, menu_cmds[KERNEL_CMD]) != 0 &&
4439eb2bd662Svikram 	    strcmp(cmd, menu_cmds[KERNEL_DOLLAR_CMD]) != 0) {
4440eb2bd662Svikram 		BAM_DPRINTF((D_NOT_KERNEL_CMD, fcn, cmd));
4441eb2bd662Svikram 		return (BAM_ERROR);
4442eb2bd662Svikram 	}
4443eb2bd662Svikram 
4444eb2bd662Svikram 	if (strncmp(arg, DIRECT_BOOT_32, sizeof (DIRECT_BOOT_32) - 1) == 0) {
4445eb2bd662Svikram 		BAM_DPRINTF((D_SET_DBOOT_32, fcn, arg));
4446eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_32BIT;
4447eb2bd662Svikram 	} else if (strncmp(arg, DIRECT_BOOT_KERNEL,
4448eb2bd662Svikram 	    sizeof (DIRECT_BOOT_KERNEL) - 1) == 0) {
4449eb2bd662Svikram 		BAM_DPRINTF((D_SET_DBOOT, fcn, arg));
4450eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT;
4451eb2bd662Svikram 	} else if (strncmp(arg, DIRECT_BOOT_64,
4452eb2bd662Svikram 	    sizeof (DIRECT_BOOT_64) - 1) == 0) {
4453eb2bd662Svikram 		BAM_DPRINTF((D_SET_DBOOT_64, fcn, arg));
4454eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_64BIT;
4455eb2bd662Svikram 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_KERNEL,
4456eb2bd662Svikram 	    sizeof (DIRECT_BOOT_FAILSAFE_KERNEL) - 1) == 0) {
4457eb2bd662Svikram 		BAM_DPRINTF((D_SET_DBOOT_FAILSAFE, fcn, arg));
4458eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE;
4459bbcc54bdSEnrico Perla - Sun Microsystems 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_32,
4460bbcc54bdSEnrico Perla - Sun Microsystems 	    sizeof (DIRECT_BOOT_FAILSAFE_32) - 1) == 0) {
4461bbcc54bdSEnrico Perla - Sun Microsystems 		BAM_DPRINTF((D_SET_DBOOT_FAILSAFE_32, fcn, arg));
4462bbcc54bdSEnrico Perla - Sun Microsystems 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE
4463bbcc54bdSEnrico Perla - Sun Microsystems 		    | BAM_ENTRY_32BIT;
4464bbcc54bdSEnrico Perla - Sun Microsystems 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_64,
4465bbcc54bdSEnrico Perla - Sun Microsystems 	    sizeof (DIRECT_BOOT_FAILSAFE_64) - 1) == 0) {
4466bbcc54bdSEnrico Perla - Sun Microsystems 		BAM_DPRINTF((D_SET_DBOOT_FAILSAFE_64, fcn, arg));
4467bbcc54bdSEnrico Perla - Sun Microsystems 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE
4468bbcc54bdSEnrico Perla - Sun Microsystems 		    | BAM_ENTRY_64BIT;
4469eb2bd662Svikram 	} else if (strncmp(arg, MULTI_BOOT, sizeof (MULTI_BOOT) - 1) == 0) {
4470eb2bd662Svikram 		BAM_DPRINTF((D_SET_MULTIBOOT, fcn, arg));
4471eb2bd662Svikram 		entry->flags |= BAM_ENTRY_MULTIBOOT;
4472eb2bd662Svikram 	} else if (strncmp(arg, MULTI_BOOT_FAILSAFE,
4473eb2bd662Svikram 	    sizeof (MULTI_BOOT_FAILSAFE) - 1) == 0) {
4474eb2bd662Svikram 		BAM_DPRINTF((D_SET_MULTIBOOT_FAILSAFE, fcn, arg));
4475eb2bd662Svikram 		entry->flags |= BAM_ENTRY_MULTIBOOT | BAM_ENTRY_FAILSAFE;
4476eb2bd662Svikram 	} else if (strstr(arg, XEN_KERNEL_SUBSTR)) {
4477eb2bd662Svikram 		BAM_DPRINTF((D_SET_HV, fcn, arg));
4478eb2bd662Svikram 		entry->flags |= BAM_ENTRY_HV;
4479eb2bd662Svikram 	} else if (!(entry->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU))) {
4480eb2bd662Svikram 		BAM_DPRINTF((D_SET_HAND_KERNEL, fcn, arg));
4481eb2bd662Svikram 		return (BAM_ERROR);
448237eb779cSVikram Hegde 	} else if (strncmp(arg, KERNEL_PREFIX, strlen(KERNEL_PREFIX)) == 0 &&
448337eb779cSVikram Hegde 	    strstr(arg, UNIX_SPACE)) {
448437eb779cSVikram Hegde 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_32BIT;
448537eb779cSVikram Hegde 	} else if (strncmp(arg, KERNEL_PREFIX, strlen(KERNEL_PREFIX)) == 0 &&
448637eb779cSVikram Hegde 	    strstr(arg, AMD_UNIX_SPACE)) {
448737eb779cSVikram Hegde 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_64BIT;
4488eb2bd662Svikram 	} else {
4489eb2bd662Svikram 		BAM_DPRINTF((D_IS_UNKNOWN_KERNEL, fcn, arg));
4490eb2bd662Svikram 		bam_error(UNKNOWN_KERNEL_LINE, linenum);
4491eb2bd662Svikram 		return (BAM_ERROR);
4492eb2bd662Svikram 	}
4493eb2bd662Svikram 
4494eb2bd662Svikram 	return (BAM_SUCCESS);
4495eb2bd662Svikram }
4496eb2bd662Svikram 
4497eb2bd662Svikram static error_t
4498eb2bd662Svikram module_parser(entry_t *entry, char *cmd, char *arg, int linenum)
4499eb2bd662Svikram {
4500eb2bd662Svikram 	const char		*fcn = "module_parser()";
4501eb2bd662Svikram 
4502eb2bd662Svikram 	assert(entry);
4503eb2bd662Svikram 	assert(cmd);
4504eb2bd662Svikram 	assert(arg);
4505eb2bd662Svikram 
4506eb2bd662Svikram 	if (strcmp(cmd, menu_cmds[MODULE_CMD]) != 0 &&
4507eb2bd662Svikram 	    strcmp(cmd, menu_cmds[MODULE_DOLLAR_CMD]) != 0) {
4508eb2bd662Svikram 		BAM_DPRINTF((D_NOT_MODULE_CMD, fcn, cmd));
4509eb2bd662Svikram 		return (BAM_ERROR);
4510eb2bd662Svikram 	}
4511eb2bd662Svikram 
4512eb2bd662Svikram 	if (strcmp(arg, DIRECT_BOOT_ARCHIVE) == 0 ||
4513eb2bd662Svikram 	    strcmp(arg, DIRECT_BOOT_ARCHIVE_32) == 0 ||
4514eb2bd662Svikram 	    strcmp(arg, DIRECT_BOOT_ARCHIVE_64) == 0 ||
4515eb2bd662Svikram 	    strcmp(arg, MULTIBOOT_ARCHIVE) == 0 ||
4516eb2bd662Svikram 	    strcmp(arg, FAILSAFE_ARCHIVE) == 0 ||
4517bbcc54bdSEnrico Perla - Sun Microsystems 	    strcmp(arg, FAILSAFE_ARCHIVE_32) == 0 ||
4518bbcc54bdSEnrico Perla - Sun Microsystems 	    strcmp(arg, FAILSAFE_ARCHIVE_64) == 0 ||
4519eb2bd662Svikram 	    strcmp(arg, XEN_KERNEL_MODULE_LINE) == 0 ||
4520eb2bd662Svikram 	    strcmp(arg, XEN_KERNEL_MODULE_LINE_ZFS) == 0) {
4521eb2bd662Svikram 		BAM_DPRINTF((D_BOOTADM_LU_MODULE, fcn, arg));
4522eb2bd662Svikram 		return (BAM_SUCCESS);
4523eb2bd662Svikram 	} else if (!(entry->flags & BAM_ENTRY_BOOTADM) &&
4524eb2bd662Svikram 	    !(entry->flags & BAM_ENTRY_LU)) {
4525eb2bd662Svikram 		/* don't emit warning for hand entries */
4526eb2bd662Svikram 		BAM_DPRINTF((D_IS_HAND_MODULE, fcn, arg));
4527eb2bd662Svikram 		return (BAM_ERROR);
4528eb2bd662Svikram 	} else {
4529eb2bd662Svikram 		BAM_DPRINTF((D_IS_UNKNOWN_MODULE, fcn, arg));
4530eb2bd662Svikram 		bam_error(UNKNOWN_MODULE_LINE, linenum);
4531eb2bd662Svikram 		return (BAM_ERROR);
4532eb2bd662Svikram 	}
4533843e1988Sjohnlev }
4534843e1988Sjohnlev 
4535843e1988Sjohnlev /*
45367c478bd9Sstevel@tonic-gate  * A line in menu.lst looks like
45377c478bd9Sstevel@tonic-gate  * [ ]*<cmd>[ \t=]*<arg>*
45387c478bd9Sstevel@tonic-gate  */
45397c478bd9Sstevel@tonic-gate static void
45407c478bd9Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum)
45417c478bd9Sstevel@tonic-gate {
45427c478bd9Sstevel@tonic-gate 	/*
45437c478bd9Sstevel@tonic-gate 	 * save state across calls. This is so that
45447c478bd9Sstevel@tonic-gate 	 * header gets the right entry# after title has
45457c478bd9Sstevel@tonic-gate 	 * been processed
45467c478bd9Sstevel@tonic-gate 	 */
45478c1b6884Sszhou 	static line_t *prev = NULL;
45488c1b6884Sszhou 	static entry_t *curr_ent = NULL;
4549ae115bc7Smrj 	static int in_liveupgrade = 0;
4550772d6a58SWilliam Kucharski 	static int is_libbe_ent = 0;
45517c478bd9Sstevel@tonic-gate 
45527c478bd9Sstevel@tonic-gate 	line_t	*lp;
45537c478bd9Sstevel@tonic-gate 	char *cmd, *sep, *arg;
45547c478bd9Sstevel@tonic-gate 	char save, *cp, *line;
45557c478bd9Sstevel@tonic-gate 	menu_flag_t flag = BAM_INVALID;
4556eb2bd662Svikram 	const char *fcn = "line_parser()";
45577c478bd9Sstevel@tonic-gate 
45587c478bd9Sstevel@tonic-gate 	if (str == NULL) {
45597c478bd9Sstevel@tonic-gate 		return;
45607c478bd9Sstevel@tonic-gate 	}
45617c478bd9Sstevel@tonic-gate 
45627c478bd9Sstevel@tonic-gate 	/*
45637c478bd9Sstevel@tonic-gate 	 * First save a copy of the entire line.
45647c478bd9Sstevel@tonic-gate 	 * We use this later to set the line field.
45657c478bd9Sstevel@tonic-gate 	 */
45667c478bd9Sstevel@tonic-gate 	line = s_strdup(str);
45677c478bd9Sstevel@tonic-gate 
45687c478bd9Sstevel@tonic-gate 	/* Eat up leading whitespace */
45697c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
45707c478bd9Sstevel@tonic-gate 		str++;
45717c478bd9Sstevel@tonic-gate 
45727c478bd9Sstevel@tonic-gate 	if (*str == '#') {		/* comment */
45737c478bd9Sstevel@tonic-gate 		cmd = s_strdup("#");
45747c478bd9Sstevel@tonic-gate 		sep = NULL;
45757c478bd9Sstevel@tonic-gate 		arg = s_strdup(str + 1);
45767c478bd9Sstevel@tonic-gate 		flag = BAM_COMMENT;
4577ae115bc7Smrj 		if (strstr(arg, BAM_LU_HDR) != NULL) {
4578ae115bc7Smrj 			in_liveupgrade = 1;
4579ae115bc7Smrj 		} else if (strstr(arg, BAM_LU_FTR) != NULL) {
4580ae115bc7Smrj 			in_liveupgrade = 0;
4581772d6a58SWilliam Kucharski 		} else if (strstr(arg, BAM_LIBBE_FTR) != NULL) {
4582772d6a58SWilliam Kucharski 			is_libbe_ent = 1;
4583ae115bc7Smrj 		}
45847c478bd9Sstevel@tonic-gate 	} else if (*str == '\0') {	/* blank line */
45857c478bd9Sstevel@tonic-gate 		cmd = sep = arg = NULL;
45867c478bd9Sstevel@tonic-gate 		flag = BAM_EMPTY;
45877c478bd9Sstevel@tonic-gate 	} else {
45887c478bd9Sstevel@tonic-gate 		/*
45897c478bd9Sstevel@tonic-gate 		 * '=' is not a documented separator in grub syntax.
45907c478bd9Sstevel@tonic-gate 		 * However various development bits use '=' as a
45917c478bd9Sstevel@tonic-gate 		 * separator. In addition, external users also
45927c478bd9Sstevel@tonic-gate 		 * use = as a separator. So we will allow that usage.
45937c478bd9Sstevel@tonic-gate 		 */
45947c478bd9Sstevel@tonic-gate 		cp = str;
45957c478bd9Sstevel@tonic-gate 		while (*str != ' ' && *str != '\t' && *str != '=') {
45967c478bd9Sstevel@tonic-gate 			if (*str == '\0') {
45977c478bd9Sstevel@tonic-gate 				cmd = s_strdup(cp);
45987c478bd9Sstevel@tonic-gate 				sep = arg = NULL;
45997c478bd9Sstevel@tonic-gate 				break;
46007c478bd9Sstevel@tonic-gate 			}
46017c478bd9Sstevel@tonic-gate 			str++;
46027c478bd9Sstevel@tonic-gate 		}
46037c478bd9Sstevel@tonic-gate 
46047c478bd9Sstevel@tonic-gate 		if (*str != '\0') {
46057c478bd9Sstevel@tonic-gate 			save = *str;
46067c478bd9Sstevel@tonic-gate 			*str = '\0';
46077c478bd9Sstevel@tonic-gate 			cmd = s_strdup(cp);
46087c478bd9Sstevel@tonic-gate 			*str = save;
46097c478bd9Sstevel@tonic-gate 
46107c478bd9Sstevel@tonic-gate 			str++;
46117c478bd9Sstevel@tonic-gate 			save = *str;
46127c478bd9Sstevel@tonic-gate 			*str = '\0';
46137c478bd9Sstevel@tonic-gate 			sep = s_strdup(str - 1);
46147c478bd9Sstevel@tonic-gate 			*str = save;
46157c478bd9Sstevel@tonic-gate 
46167c478bd9Sstevel@tonic-gate 			while (*str == ' ' || *str == '\t')
46177c478bd9Sstevel@tonic-gate 				str++;
46187c478bd9Sstevel@tonic-gate 			if (*str == '\0')
46197c478bd9Sstevel@tonic-gate 				arg = NULL;
46207c478bd9Sstevel@tonic-gate 			else
46217c478bd9Sstevel@tonic-gate 				arg = s_strdup(str);
46227c478bd9Sstevel@tonic-gate 		}
46237c478bd9Sstevel@tonic-gate 	}
46247c478bd9Sstevel@tonic-gate 
46257c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
46267c478bd9Sstevel@tonic-gate 
46277c478bd9Sstevel@tonic-gate 	lp->cmd = cmd;
46287c478bd9Sstevel@tonic-gate 	lp->sep = sep;
46297c478bd9Sstevel@tonic-gate 	lp->arg = arg;
46307c478bd9Sstevel@tonic-gate 	lp->line = line;
46317c478bd9Sstevel@tonic-gate 	lp->lineNum = ++(*lineNum);
46327c478bd9Sstevel@tonic-gate 	if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) {
46337c478bd9Sstevel@tonic-gate 		lp->entryNum = ++(*entryNum);
46347c478bd9Sstevel@tonic-gate 		lp->flags = BAM_TITLE;
46357c478bd9Sstevel@tonic-gate 		if (prev && prev->flags == BAM_COMMENT &&
4636ae115bc7Smrj 		    prev->arg && strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
46377c478bd9Sstevel@tonic-gate 			prev->entryNum = lp->entryNum;
46388c1b6884Sszhou 			curr_ent = boot_entry_new(mp, prev, lp);
4639eb2bd662Svikram 			curr_ent->flags |= BAM_ENTRY_BOOTADM;
4640eb2bd662Svikram 			BAM_DPRINTF((D_IS_BOOTADM_ENTRY, fcn, arg));
46418c1b6884Sszhou 		} else {
46428c1b6884Sszhou 			curr_ent = boot_entry_new(mp, lp, lp);
4643ae115bc7Smrj 			if (in_liveupgrade) {
4644eb2bd662Svikram 				curr_ent->flags |= BAM_ENTRY_LU;
4645eb2bd662Svikram 				BAM_DPRINTF((D_IS_LU_ENTRY, fcn, arg));
46468c1b6884Sszhou 			}
4647ae115bc7Smrj 		}
4648ae115bc7Smrj 		curr_ent->entryNum = *entryNum;
46497c478bd9Sstevel@tonic-gate 	} else if (flag != BAM_INVALID) {
46507c478bd9Sstevel@tonic-gate 		/*
46517c478bd9Sstevel@tonic-gate 		 * For header comments, the entry# is "fixed up"
46527c478bd9Sstevel@tonic-gate 		 * by the subsequent title
46537c478bd9Sstevel@tonic-gate 		 */
46547c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
46557c478bd9Sstevel@tonic-gate 		lp->flags = flag;
46567c478bd9Sstevel@tonic-gate 	} else {
46577c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
4658ae115bc7Smrj 
4659ae115bc7Smrj 		if (*entryNum == ENTRY_INIT) {
4660ae115bc7Smrj 			lp->flags = BAM_GLOBAL;
4661ae115bc7Smrj 		} else {
4662ae115bc7Smrj 			lp->flags = BAM_ENTRY;
4663ae115bc7Smrj 
4664ae115bc7Smrj 			if (cmd && arg) {
4665eb2bd662Svikram 				if (strcmp(cmd, menu_cmds[ROOT_CMD]) == 0) {
4666eb2bd662Svikram 					BAM_DPRINTF((D_IS_ROOT_CMD, fcn, arg));
4667ae115bc7Smrj 					curr_ent->flags |= BAM_ENTRY_ROOT;
4668eb2bd662Svikram 				} else if (strcmp(cmd, menu_cmds[FINDROOT_CMD])
4669eb2bd662Svikram 				    == 0) {
4670eb2bd662Svikram 					BAM_DPRINTF((D_IS_FINDROOT_CMD, fcn,
4671eb2bd662Svikram 					    arg));
4672eb2bd662Svikram 					curr_ent->flags |= BAM_ENTRY_FINDROOT;
4673eb2bd662Svikram 				} else if (strcmp(cmd,
4674eb2bd662Svikram 				    menu_cmds[CHAINLOADER_CMD]) == 0) {
4675eb2bd662Svikram 					BAM_DPRINTF((D_IS_CHAINLOADER_CMD, fcn,
4676eb2bd662Svikram 					    arg));
4677ae115bc7Smrj 					curr_ent->flags |=
4678ae115bc7Smrj 					    BAM_ENTRY_CHAINLOADER;
4679eb2bd662Svikram 				} else if (kernel_parser(curr_ent, cmd, arg,
4680eb2bd662Svikram 				    lp->lineNum) != BAM_SUCCESS) {
4681eb2bd662Svikram 					(void) module_parser(curr_ent, cmd,
4682eb2bd662Svikram 					    arg, lp->lineNum);
4683eb2bd662Svikram 				}
4684ae115bc7Smrj 			}
4685ae115bc7Smrj 		}
46867c478bd9Sstevel@tonic-gate 	}
46877c478bd9Sstevel@tonic-gate 
46888c1b6884Sszhou 	/* record default, old default, and entry line ranges */
4689eac223ccSWilliam Kucharski 	if (lp->flags == BAM_GLOBAL && lp->cmd != NULL &&
46908c1b6884Sszhou 	    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) {
46918c1b6884Sszhou 		mp->curdefault = lp;
46928c1b6884Sszhou 	} else if (lp->flags == BAM_COMMENT &&
46938c1b6884Sszhou 	    strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) {
46948c1b6884Sszhou 		mp->olddefault = lp;
4695ae115bc7Smrj 	} else if (lp->flags == BAM_COMMENT &&
4696ae115bc7Smrj 	    strncmp(lp->arg, BAM_OLD_RC_DEF, strlen(BAM_OLD_RC_DEF)) == 0) {
4697ae115bc7Smrj 		mp->old_rc_default = lp;
46988c1b6884Sszhou 	} else if (lp->flags == BAM_ENTRY ||
4699ae115bc7Smrj 	    (lp->flags == BAM_COMMENT &&
4700772d6a58SWilliam Kucharski 	    ((strcmp(lp->arg, BAM_BOOTADM_FTR) == 0) || is_libbe_ent))) {
4701772d6a58SWilliam Kucharski 		if (is_libbe_ent) {
4702772d6a58SWilliam Kucharski 			curr_ent->flags |= BAM_ENTRY_LIBBE;
4703772d6a58SWilliam Kucharski 			is_libbe_ent = 0;
4704772d6a58SWilliam Kucharski 		}
4705772d6a58SWilliam Kucharski 
47068c1b6884Sszhou 		boot_entry_addline(curr_ent, lp);
47078c1b6884Sszhou 	}
47087c478bd9Sstevel@tonic-gate 	append_line(mp, lp);
47097c478bd9Sstevel@tonic-gate 
47107c478bd9Sstevel@tonic-gate 	prev = lp;
47117c478bd9Sstevel@tonic-gate }
47127c478bd9Sstevel@tonic-gate 
4713eb2bd662Svikram void
471440541d5dSvikram update_numbering(menu_t *mp)
471540541d5dSvikram {
471640541d5dSvikram 	int lineNum;
471740541d5dSvikram 	int entryNum;
471840541d5dSvikram 	int old_default_value;
471940541d5dSvikram 	line_t *lp, *prev, *default_lp, *default_entry;
472040541d5dSvikram 	char buf[PATH_MAX];
472140541d5dSvikram 
472240541d5dSvikram 	if (mp->start == NULL) {
472340541d5dSvikram 		return;
472440541d5dSvikram 	}
472540541d5dSvikram 
472640541d5dSvikram 	lineNum = LINE_INIT;
472740541d5dSvikram 	entryNum = ENTRY_INIT;
472840541d5dSvikram 	old_default_value = ENTRY_INIT;
472940541d5dSvikram 	lp = default_lp = default_entry = NULL;
473040541d5dSvikram 
473140541d5dSvikram 	prev = NULL;
473240541d5dSvikram 	for (lp = mp->start; lp; prev = lp, lp = lp->next) {
473340541d5dSvikram 		lp->lineNum = ++lineNum;
473440541d5dSvikram 
473540541d5dSvikram 		/*
473640541d5dSvikram 		 * Get the value of the default command
473740541d5dSvikram 		 */
4738eac223ccSWilliam Kucharski 		if (lp->entryNum == ENTRY_INIT && lp->cmd != NULL &&
473940541d5dSvikram 		    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 &&
474040541d5dSvikram 		    lp->arg) {
474140541d5dSvikram 			old_default_value = atoi(lp->arg);
474240541d5dSvikram 			default_lp = lp;
474340541d5dSvikram 		}
474440541d5dSvikram 
474540541d5dSvikram 		/*
4746eb2bd662Svikram 		 * If not a booting entry, nothing else to fix for this
474740541d5dSvikram 		 * entry
474840541d5dSvikram 		 */
474940541d5dSvikram 		if (lp->entryNum == ENTRY_INIT)
475040541d5dSvikram 			continue;
475140541d5dSvikram 
475240541d5dSvikram 		/*
475340541d5dSvikram 		 * Record the position of the default entry.
475440541d5dSvikram 		 * The following works because global
475540541d5dSvikram 		 * commands like default and timeout should precede
475640541d5dSvikram 		 * actual boot entries, so old_default_value
475740541d5dSvikram 		 * is already known (or default cmd is missing).
475840541d5dSvikram 		 */
475940541d5dSvikram 		if (default_entry == NULL &&
476040541d5dSvikram 		    old_default_value != ENTRY_INIT &&
476140541d5dSvikram 		    lp->entryNum == old_default_value) {
476240541d5dSvikram 			default_entry = lp;
476340541d5dSvikram 		}
476440541d5dSvikram 
476540541d5dSvikram 		/*
476640541d5dSvikram 		 * Now fixup the entry number
476740541d5dSvikram 		 */
4768eac223ccSWilliam Kucharski 		if (lp->cmd != NULL &&
4769eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) {
477040541d5dSvikram 			lp->entryNum = ++entryNum;
477140541d5dSvikram 			/* fixup the bootadm header */
477240541d5dSvikram 			if (prev && prev->flags == BAM_COMMENT &&
4773ae115bc7Smrj 			    prev->arg &&
4774ae115bc7Smrj 			    strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
477540541d5dSvikram 				prev->entryNum = lp->entryNum;
477640541d5dSvikram 			}
477740541d5dSvikram 		} else {
477840541d5dSvikram 			lp->entryNum = entryNum;
477940541d5dSvikram 		}
478040541d5dSvikram 	}
478140541d5dSvikram 
478240541d5dSvikram 	/*
478340541d5dSvikram 	 * No default command in menu, simply return
478440541d5dSvikram 	 */
478540541d5dSvikram 	if (default_lp == NULL) {
478640541d5dSvikram 		return;
478740541d5dSvikram 	}
478840541d5dSvikram 
478940541d5dSvikram 	free(default_lp->arg);
479040541d5dSvikram 	free(default_lp->line);
479140541d5dSvikram 
479240541d5dSvikram 	if (default_entry == NULL) {
479340541d5dSvikram 		default_lp->arg = s_strdup("0");
479440541d5dSvikram 	} else {
479540541d5dSvikram 		(void) snprintf(buf, sizeof (buf), "%d",
479640541d5dSvikram 		    default_entry->entryNum);
479740541d5dSvikram 		default_lp->arg = s_strdup(buf);
479840541d5dSvikram 	}
479940541d5dSvikram 
480040541d5dSvikram 	/*
480140541d5dSvikram 	 * The following is required since only the line field gets
480240541d5dSvikram 	 * written back to menu.lst
480340541d5dSvikram 	 */
480440541d5dSvikram 	(void) snprintf(buf, sizeof (buf), "%s%s%s",
480540541d5dSvikram 	    menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg);
480640541d5dSvikram 	default_lp->line = s_strdup(buf);
480740541d5dSvikram }
480840541d5dSvikram 
480940541d5dSvikram 
48107c478bd9Sstevel@tonic-gate static menu_t *
48117c478bd9Sstevel@tonic-gate menu_read(char *menu_path)
48127c478bd9Sstevel@tonic-gate {
48137c478bd9Sstevel@tonic-gate 	FILE *fp;
48147c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE], *cp;
48157c478bd9Sstevel@tonic-gate 	menu_t *mp;
48167c478bd9Sstevel@tonic-gate 	int line, entry, len, n;
48177c478bd9Sstevel@tonic-gate 
48187c478bd9Sstevel@tonic-gate 	mp = s_calloc(1, sizeof (menu_t));
48197c478bd9Sstevel@tonic-gate 
48207c478bd9Sstevel@tonic-gate 	fp = fopen(menu_path, "r");
48217c478bd9Sstevel@tonic-gate 	if (fp == NULL) { /* Let the caller handle this error */
482244da779fSWilliam Kucharski 		free(mp);
482344da779fSWilliam Kucharski 		return (NULL);
48247c478bd9Sstevel@tonic-gate 	}
48257c478bd9Sstevel@tonic-gate 
48267c478bd9Sstevel@tonic-gate 	/* Note: GRUB boot entry number starts with 0 */
48277c478bd9Sstevel@tonic-gate 	line = LINE_INIT;
48287c478bd9Sstevel@tonic-gate 	entry = ENTRY_INIT;
48297c478bd9Sstevel@tonic-gate 	cp = buf;
48307c478bd9Sstevel@tonic-gate 	len = sizeof (buf);
48317c478bd9Sstevel@tonic-gate 	while (s_fgets(cp, len, fp) != NULL) {
48327c478bd9Sstevel@tonic-gate 		n = strlen(cp);
48337c478bd9Sstevel@tonic-gate 		if (cp[n - 1] == '\\') {
48347c478bd9Sstevel@tonic-gate 			len -= n - 1;
48357c478bd9Sstevel@tonic-gate 			assert(len >= 2);
48367c478bd9Sstevel@tonic-gate 			cp += n - 1;
48377c478bd9Sstevel@tonic-gate 			continue;
48387c478bd9Sstevel@tonic-gate 		}
48397c478bd9Sstevel@tonic-gate 		line_parser(mp, buf, &line, &entry);
48407c478bd9Sstevel@tonic-gate 		cp = buf;
48417c478bd9Sstevel@tonic-gate 		len = sizeof (buf);
48427c478bd9Sstevel@tonic-gate 	}
48437c478bd9Sstevel@tonic-gate 
48447c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
48457c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, menu_path, strerror(errno));
48467c478bd9Sstevel@tonic-gate 	}
48477c478bd9Sstevel@tonic-gate 
48487c478bd9Sstevel@tonic-gate 	return (mp);
48497c478bd9Sstevel@tonic-gate }
48507c478bd9Sstevel@tonic-gate 
48517c478bd9Sstevel@tonic-gate static error_t
48527c478bd9Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title)
48537c478bd9Sstevel@tonic-gate {
48547c478bd9Sstevel@tonic-gate 	char *eq;
48557c478bd9Sstevel@tonic-gate 	char *opt_dup;
48567c478bd9Sstevel@tonic-gate 	int entryNum;
48577c478bd9Sstevel@tonic-gate 
48587c478bd9Sstevel@tonic-gate 	assert(mp);
48597c478bd9Sstevel@tonic-gate 	assert(mp->start);
48607c478bd9Sstevel@tonic-gate 	assert(opt);
48617c478bd9Sstevel@tonic-gate 
48627c478bd9Sstevel@tonic-gate 	opt_dup = s_strdup(opt);
48637c478bd9Sstevel@tonic-gate 
48647c478bd9Sstevel@tonic-gate 	if (entry)
48657c478bd9Sstevel@tonic-gate 		*entry = ENTRY_INIT;
48667c478bd9Sstevel@tonic-gate 	if (title)
48677c478bd9Sstevel@tonic-gate 		*title = NULL;
48687c478bd9Sstevel@tonic-gate 
48697c478bd9Sstevel@tonic-gate 	eq = strchr(opt_dup, '=');
48707c478bd9Sstevel@tonic-gate 	if (eq == NULL) {
48717c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
48727c478bd9Sstevel@tonic-gate 		free(opt_dup);
48737c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
48747c478bd9Sstevel@tonic-gate 	}
48757c478bd9Sstevel@tonic-gate 
48767c478bd9Sstevel@tonic-gate 	*eq = '\0';
48777c478bd9Sstevel@tonic-gate 	if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) {
48787c478bd9Sstevel@tonic-gate 		assert(mp->end);
48797c478bd9Sstevel@tonic-gate 		entryNum = s_strtol(eq + 1);
48807c478bd9Sstevel@tonic-gate 		if (entryNum < 0 || entryNum > mp->end->entryNum) {
48817c478bd9Sstevel@tonic-gate 			bam_error(INVALID_ENTRY, eq + 1);
48827c478bd9Sstevel@tonic-gate 			free(opt_dup);
48837c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
48847c478bd9Sstevel@tonic-gate 		}
48857c478bd9Sstevel@tonic-gate 		*entry = entryNum;
48867c478bd9Sstevel@tonic-gate 	} else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) {
48877c478bd9Sstevel@tonic-gate 		*title = opt + (eq - opt_dup) + 1;
48887c478bd9Sstevel@tonic-gate 	} else {
48897c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
48907c478bd9Sstevel@tonic-gate 		free(opt_dup);
48917c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
48927c478bd9Sstevel@tonic-gate 	}
48937c478bd9Sstevel@tonic-gate 
48947c478bd9Sstevel@tonic-gate 	free(opt_dup);
48957c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
48967c478bd9Sstevel@tonic-gate }
48977c478bd9Sstevel@tonic-gate 
48987c478bd9Sstevel@tonic-gate /*
48997c478bd9Sstevel@tonic-gate  * If invoked with no titles/entries (opt == NULL)
49007c478bd9Sstevel@tonic-gate  * only title lines in file are printed.
49017c478bd9Sstevel@tonic-gate  *
49027c478bd9Sstevel@tonic-gate  * If invoked with a title or entry #, all
49037c478bd9Sstevel@tonic-gate  * lines in *every* matching entry are listed
49047c478bd9Sstevel@tonic-gate  */
49057c478bd9Sstevel@tonic-gate static error_t
49067c478bd9Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt)
49077c478bd9Sstevel@tonic-gate {
49087c478bd9Sstevel@tonic-gate 	line_t *lp;
49097c478bd9Sstevel@tonic-gate 	int entry = ENTRY_INIT;
49107c478bd9Sstevel@tonic-gate 	int found;
49117c478bd9Sstevel@tonic-gate 	char *title = NULL;
49127c478bd9Sstevel@tonic-gate 
49137c478bd9Sstevel@tonic-gate 	assert(mp);
49147c478bd9Sstevel@tonic-gate 	assert(menu_path);
49157c478bd9Sstevel@tonic-gate 
4916eb2bd662Svikram 	/* opt is optional */
4917eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, "list_entry", menu_path,
4918eb2bd662Svikram 	    opt ? opt : "<NULL>"));
4919eb2bd662Svikram 
49207c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
49217c478bd9Sstevel@tonic-gate 		bam_error(NO_MENU, menu_path);
49227c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
49237c478bd9Sstevel@tonic-gate 	}
49247c478bd9Sstevel@tonic-gate 
49257c478bd9Sstevel@tonic-gate 	if (opt != NULL) {
49267c478bd9Sstevel@tonic-gate 		if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
49277c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
49287c478bd9Sstevel@tonic-gate 		}
49297c478bd9Sstevel@tonic-gate 		assert((entry != ENTRY_INIT) ^ (title != NULL));
49307c478bd9Sstevel@tonic-gate 	} else {
49317c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0);
49327c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0);
49337c478bd9Sstevel@tonic-gate 	}
49347c478bd9Sstevel@tonic-gate 
49357c478bd9Sstevel@tonic-gate 	found = 0;
49367c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
49377c478bd9Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY)
49387c478bd9Sstevel@tonic-gate 			continue;
49397c478bd9Sstevel@tonic-gate 		if (opt == NULL && lp->flags == BAM_TITLE) {
49407c478bd9Sstevel@tonic-gate 			bam_print(PRINT_TITLE, lp->entryNum,
49417c478bd9Sstevel@tonic-gate 			    lp->arg);
49427c478bd9Sstevel@tonic-gate 			found = 1;
49437c478bd9Sstevel@tonic-gate 			continue;
49447c478bd9Sstevel@tonic-gate 		}
49457c478bd9Sstevel@tonic-gate 		if (entry != ENTRY_INIT && lp->entryNum == entry) {
49467c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
49477c478bd9Sstevel@tonic-gate 			found = 1;
49487c478bd9Sstevel@tonic-gate 			continue;
49497c478bd9Sstevel@tonic-gate 		}
49507c478bd9Sstevel@tonic-gate 
49517c478bd9Sstevel@tonic-gate 		/*
49527c478bd9Sstevel@tonic-gate 		 * We set the entry value here so that all lines
49537c478bd9Sstevel@tonic-gate 		 * in entry get printed. If we subsequently match
49547c478bd9Sstevel@tonic-gate 		 * title in other entries, all lines in those
49557c478bd9Sstevel@tonic-gate 		 * entries get printed as well.
49567c478bd9Sstevel@tonic-gate 		 */
49577c478bd9Sstevel@tonic-gate 		if (title && lp->flags == BAM_TITLE && lp->arg &&
49587c478bd9Sstevel@tonic-gate 		    strncmp(title, lp->arg, strlen(title)) == 0) {
49597c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
49607c478bd9Sstevel@tonic-gate 			entry = lp->entryNum;
49617c478bd9Sstevel@tonic-gate 			found = 1;
49627c478bd9Sstevel@tonic-gate 			continue;
49637c478bd9Sstevel@tonic-gate 		}
49647c478bd9Sstevel@tonic-gate 	}
49657c478bd9Sstevel@tonic-gate 
49667c478bd9Sstevel@tonic-gate 	if (!found) {
49677c478bd9Sstevel@tonic-gate 		bam_error(NO_MATCH_ENTRY);
49687c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
49697c478bd9Sstevel@tonic-gate 	}
49707c478bd9Sstevel@tonic-gate 
49717c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
49727c478bd9Sstevel@tonic-gate }
49737c478bd9Sstevel@tonic-gate 
4974843e1988Sjohnlev int
49757c478bd9Sstevel@tonic-gate add_boot_entry(menu_t *mp,
49767c478bd9Sstevel@tonic-gate     char *title,
4977eb2bd662Svikram     char *findroot,
49787c478bd9Sstevel@tonic-gate     char *kernel,
4979843e1988Sjohnlev     char *mod_kernel,
498044da779fSWilliam Kucharski     char *module,
498144da779fSWilliam Kucharski     char *bootfs)
49827c478bd9Sstevel@tonic-gate {
4983eb2bd662Svikram 	int		lineNum;
4984eb2bd662Svikram 	int		entryNum;
49857c478bd9Sstevel@tonic-gate 	char		linebuf[BAM_MAXLINE];
4986eb2bd662Svikram 	menu_cmd_t	k_cmd;
4987eb2bd662Svikram 	menu_cmd_t	m_cmd;
4988eb2bd662Svikram 	const char	*fcn = "add_boot_entry()";
49897c478bd9Sstevel@tonic-gate 
49907c478bd9Sstevel@tonic-gate 	assert(mp);
49917c478bd9Sstevel@tonic-gate 
4992eb2bd662Svikram 	INJECT_ERROR1("ADD_BOOT_ENTRY_FINDROOT_NULL", findroot = NULL);
4993eb2bd662Svikram 	if (findroot == NULL) {
4994eb2bd662Svikram 		bam_error(NULL_FINDROOT);
4995eb2bd662Svikram 		return (BAM_ERROR);
4996eb2bd662Svikram 	}
4997eb2bd662Svikram 
49987c478bd9Sstevel@tonic-gate 	if (title == NULL) {
4999ee3b8144Sszhou 		title = "Solaris";	/* default to Solaris */
50007c478bd9Sstevel@tonic-gate 	}
50017c478bd9Sstevel@tonic-gate 	if (kernel == NULL) {
50027c478bd9Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]);
50037c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
50047c478bd9Sstevel@tonic-gate 	}
50057c478bd9Sstevel@tonic-gate 	if (module == NULL) {
5006ae115bc7Smrj 		if (bam_direct != BAM_DIRECT_DBOOT) {
50077c478bd9Sstevel@tonic-gate 			bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]);
50087c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
50097c478bd9Sstevel@tonic-gate 		}
50107c478bd9Sstevel@tonic-gate 
5011ae115bc7Smrj 		/* Figure the commands out from the kernel line */
5012ae115bc7Smrj 		if (strstr(kernel, "$ISADIR") != NULL) {
5013ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE;
5014ae115bc7Smrj 		} else if (strstr(kernel, "amd64") != NULL) {
5015ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE_64;
5016ae115bc7Smrj 		} else {
5017ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE_32;
5018ae115bc7Smrj 		}
50199adc5a4eSEdward Gillett 	}
50209adc5a4eSEdward Gillett 
5021ae115bc7Smrj 	k_cmd = KERNEL_DOLLAR_CMD;
5022ae115bc7Smrj 	m_cmd = MODULE_DOLLAR_CMD;
5023ae115bc7Smrj 
50247c478bd9Sstevel@tonic-gate 	if (mp->start) {
50257c478bd9Sstevel@tonic-gate 		lineNum = mp->end->lineNum;
50267c478bd9Sstevel@tonic-gate 		entryNum = mp->end->entryNum;
50277c478bd9Sstevel@tonic-gate 	} else {
50287c478bd9Sstevel@tonic-gate 		lineNum = LINE_INIT;
50297c478bd9Sstevel@tonic-gate 		entryNum = ENTRY_INIT;
50307c478bd9Sstevel@tonic-gate 	}
50317c478bd9Sstevel@tonic-gate 
50327c478bd9Sstevel@tonic-gate 	/*
50337c478bd9Sstevel@tonic-gate 	 * No separator for comment (HDR/FTR) commands
50347c478bd9Sstevel@tonic-gate 	 * The syntax for comments is #<comment>
50357c478bd9Sstevel@tonic-gate 	 */
50367c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
5037ae115bc7Smrj 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_HDR);
50388c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
50397c478bd9Sstevel@tonic-gate 
50407c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
50417c478bd9Sstevel@tonic-gate 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
50428c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
50437c478bd9Sstevel@tonic-gate 
50447c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
5045eb2bd662Svikram 	    menu_cmds[FINDROOT_CMD], menu_cmds[SEP_CMD], findroot);
50468c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
5047eb2bd662Svikram 	BAM_DPRINTF((D_ADD_FINDROOT_NUM, fcn, lineNum, entryNum));
50487c478bd9Sstevel@tonic-gate 
504944da779fSWilliam Kucharski 	if (bootfs != NULL) {
505044da779fSWilliam Kucharski 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
505144da779fSWilliam Kucharski 		    menu_cmds[BOOTFS_CMD], menu_cmds[SEP_CMD], bootfs);
505244da779fSWilliam Kucharski 		line_parser(mp, linebuf, &lineNum, &entryNum);
505344da779fSWilliam Kucharski 	}
505444da779fSWilliam Kucharski 
50557c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
5056ae115bc7Smrj 	    menu_cmds[k_cmd], menu_cmds[SEP_CMD], kernel);
50578c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
50587c478bd9Sstevel@tonic-gate 
5059843e1988Sjohnlev 	if (mod_kernel != NULL) {
5060843e1988Sjohnlev 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
5061843e1988Sjohnlev 		    menu_cmds[m_cmd], menu_cmds[SEP_CMD], mod_kernel);
5062843e1988Sjohnlev 		line_parser(mp, linebuf, &lineNum, &entryNum);
5063843e1988Sjohnlev 	}
5064843e1988Sjohnlev 
50657c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
5066ae115bc7Smrj 	    menu_cmds[m_cmd], menu_cmds[SEP_CMD], module);
50678c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
50687c478bd9Sstevel@tonic-gate 
50697c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
5070ae115bc7Smrj 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_FTR);
50718c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
50727c478bd9Sstevel@tonic-gate 
50737c478bd9Sstevel@tonic-gate 	return (entryNum);
50747c478bd9Sstevel@tonic-gate }
50757c478bd9Sstevel@tonic-gate 
507644da779fSWilliam Kucharski error_t
507744da779fSWilliam Kucharski delete_boot_entry(menu_t *mp, int entryNum, int quiet)
50787c478bd9Sstevel@tonic-gate {
5079eb2bd662Svikram 	line_t		*lp;
5080eb2bd662Svikram 	line_t		*freed;
5081eb2bd662Svikram 	entry_t		*ent;
5082eb2bd662Svikram 	entry_t		*tmp;
508344da779fSWilliam Kucharski 	int		deleted = 0;
508444da779fSWilliam Kucharski 	const char	*fcn = "delete_boot_entry()";
50857c478bd9Sstevel@tonic-gate 
50867c478bd9Sstevel@tonic-gate 	assert(entryNum != ENTRY_INIT);
50877c478bd9Sstevel@tonic-gate 
5088eb2bd662Svikram 	tmp = NULL;
5089eb2bd662Svikram 
50908c1b6884Sszhou 	ent = mp->entries;
50918c1b6884Sszhou 	while (ent) {
50928c1b6884Sszhou 		lp = ent->start;
5093772d6a58SWilliam Kucharski 
5094772d6a58SWilliam Kucharski 		/*
5095772d6a58SWilliam Kucharski 		 * Check entry number and make sure it's a modifiable entry.
5096772d6a58SWilliam Kucharski 		 *
5097772d6a58SWilliam Kucharski 		 * Guidelines:
5098772d6a58SWilliam Kucharski 		 *	+ We can modify a bootadm-created entry
5099772d6a58SWilliam Kucharski 		 *	+ We can modify a libbe-created entry
5100772d6a58SWilliam Kucharski 		 */
5101772d6a58SWilliam Kucharski 		if ((lp->flags != BAM_COMMENT &&
5102772d6a58SWilliam Kucharski 		    (((ent->flags & BAM_ENTRY_LIBBE) == 0) &&
5103772d6a58SWilliam Kucharski 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0)) ||
51048c1b6884Sszhou 		    (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) {
51058c1b6884Sszhou 			ent = ent->next;
51067c478bd9Sstevel@tonic-gate 			continue;
51077c478bd9Sstevel@tonic-gate 		}
51087c478bd9Sstevel@tonic-gate 
51098c1b6884Sszhou 		/* free the entry content */
51108c1b6884Sszhou 		do {
51118c1b6884Sszhou 			freed = lp;
51128c1b6884Sszhou 			lp = lp->next;	/* prev stays the same */
5113eb2bd662Svikram 			BAM_DPRINTF((D_FREEING_LINE, fcn, freed->lineNum));
51148c1b6884Sszhou 			unlink_line(mp, freed);
51158c1b6884Sszhou 			line_free(freed);
51168c1b6884Sszhou 		} while (freed != ent->end);
51177c478bd9Sstevel@tonic-gate 
51188c1b6884Sszhou 		/* free the entry_t structure */
5119eb2bd662Svikram 		assert(tmp == NULL);
51208c1b6884Sszhou 		tmp = ent;
51218c1b6884Sszhou 		ent = ent->next;
51228c1b6884Sszhou 		if (tmp->prev)
51238c1b6884Sszhou 			tmp->prev->next = ent;
51247c478bd9Sstevel@tonic-gate 		else
51258c1b6884Sszhou 			mp->entries = ent;
51268c1b6884Sszhou 		if (ent)
51278c1b6884Sszhou 			ent->prev = tmp->prev;
5128eb2bd662Svikram 		BAM_DPRINTF((D_FREEING_ENTRY, fcn, tmp->entryNum));
5129eb2bd662Svikram 		free(tmp);
5130eb2bd662Svikram 		tmp = NULL;
51317c478bd9Sstevel@tonic-gate 		deleted = 1;
51327c478bd9Sstevel@tonic-gate 	}
51337c478bd9Sstevel@tonic-gate 
5134eb2bd662Svikram 	assert(tmp == NULL);
5135eb2bd662Svikram 
51367c478bd9Sstevel@tonic-gate 	if (!deleted && entryNum != ALL_ENTRIES) {
513744da779fSWilliam Kucharski 		if (quiet == DBE_PRINTERR)
51387c478bd9Sstevel@tonic-gate 			bam_error(NO_BOOTADM_MATCH);
51397c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
51407c478bd9Sstevel@tonic-gate 	}
51417c478bd9Sstevel@tonic-gate 
514240541d5dSvikram 	/*
514340541d5dSvikram 	 * Now that we have deleted an entry, update
514440541d5dSvikram 	 * the entry numbering and the default cmd.
514540541d5dSvikram 	 */
514640541d5dSvikram 	update_numbering(mp);
514740541d5dSvikram 
51487c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
51497c478bd9Sstevel@tonic-gate }
51507c478bd9Sstevel@tonic-gate 
51517c478bd9Sstevel@tonic-gate static error_t
5152eb2bd662Svikram delete_all_entries(menu_t *mp, char *dummy, char *opt)
51537c478bd9Sstevel@tonic-gate {
51547c478bd9Sstevel@tonic-gate 	assert(mp);
5155eb2bd662Svikram 	assert(dummy == NULL);
51567c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
51577c478bd9Sstevel@tonic-gate 
5158eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY0, "delete_all_entries"));
5159eb2bd662Svikram 
51607c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
5161eb2bd662Svikram 		bam_print(EMPTY_MENU);
51627c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
51637c478bd9Sstevel@tonic-gate 	}
51647c478bd9Sstevel@tonic-gate 
516544da779fSWilliam Kucharski 	if (delete_boot_entry(mp, ALL_ENTRIES, DBE_PRINTERR) != BAM_SUCCESS) {
51667c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
51677c478bd9Sstevel@tonic-gate 	}
51687c478bd9Sstevel@tonic-gate 
51697c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
51707c478bd9Sstevel@tonic-gate }
51717c478bd9Sstevel@tonic-gate 
51727c478bd9Sstevel@tonic-gate static FILE *
5173eb2bd662Svikram create_diskmap(char *osroot)
51747c478bd9Sstevel@tonic-gate {
51757c478bd9Sstevel@tonic-gate 	FILE *fp;
5176cedc7e57SEnrico Perla - Sun Microsystems 	char cmd[PATH_MAX + 16];
5177cedc7e57SEnrico Perla - Sun Microsystems 	char path[PATH_MAX];
5178eb2bd662Svikram 	const char *fcn = "create_diskmap()";
51797c478bd9Sstevel@tonic-gate 
51807c478bd9Sstevel@tonic-gate 	/* make sure we have a map file */
51817c478bd9Sstevel@tonic-gate 	fp = fopen(GRUBDISK_MAP, "r");
51827c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
5183cedc7e57SEnrico Perla - Sun Microsystems 		int	ret;
5184cedc7e57SEnrico Perla - Sun Microsystems 
5185cedc7e57SEnrico Perla - Sun Microsystems 		ret = snprintf(path, sizeof (path), "%s/%s", osroot,
5186cedc7e57SEnrico Perla - Sun Microsystems 		    CREATE_DISKMAP);
5187cedc7e57SEnrico Perla - Sun Microsystems 		if (ret >= sizeof (path)) {
5188cedc7e57SEnrico Perla - Sun Microsystems 			bam_error(PATH_TOO_LONG, osroot);
5189cedc7e57SEnrico Perla - Sun Microsystems 			return (NULL);
5190cedc7e57SEnrico Perla - Sun Microsystems 		}
5191cedc7e57SEnrico Perla - Sun Microsystems 		if (is_safe_exec(path) == BAM_ERROR)
5192cedc7e57SEnrico Perla - Sun Microsystems 			return (NULL);
5193cedc7e57SEnrico Perla - Sun Microsystems 
51947c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd),
5195eb2bd662Svikram 		    "%s/%s > /dev/null", osroot, CREATE_DISKMAP);
5196eb2bd662Svikram 		if (exec_cmd(cmd, NULL) != 0)
5197eb2bd662Svikram 			return (NULL);
51987c478bd9Sstevel@tonic-gate 		fp = fopen(GRUBDISK_MAP, "r");
5199eb2bd662Svikram 		INJECT_ERROR1("DISKMAP_CREATE_FAIL", fp = NULL);
5200eb2bd662Svikram 		if (fp) {
5201eb2bd662Svikram 			BAM_DPRINTF((D_CREATED_DISKMAP, fcn, GRUBDISK_MAP));
5202eb2bd662Svikram 		} else {
5203eb2bd662Svikram 			BAM_DPRINTF((D_CREATE_DISKMAP_FAIL, fcn, GRUBDISK_MAP));
5204eb2bd662Svikram 		}
52057c478bd9Sstevel@tonic-gate 	}
52067c478bd9Sstevel@tonic-gate 	return (fp);
52077c478bd9Sstevel@tonic-gate }
52087c478bd9Sstevel@tonic-gate 
52097c478bd9Sstevel@tonic-gate #define	SECTOR_SIZE	512
52107c478bd9Sstevel@tonic-gate 
52117c478bd9Sstevel@tonic-gate static int
52127c478bd9Sstevel@tonic-gate get_partition(char *device)
52137c478bd9Sstevel@tonic-gate {
52141a902ef8SHans Rosenfeld 	int i, fd, is_pcfs, partno = PARTNO_NOTFOUND;
52157c478bd9Sstevel@tonic-gate 	struct mboot *mboot;
52167c478bd9Sstevel@tonic-gate 	char boot_sect[SECTOR_SIZE];
52177c478bd9Sstevel@tonic-gate 	char *wholedisk, *slice;
5218e998e519SSheshadri Vasudevan #ifdef i386
5219e998e519SSheshadri Vasudevan 	ext_part_t *epp;
5220e998e519SSheshadri Vasudevan 	uint32_t secnum, numsec;
52211a902ef8SHans Rosenfeld 	int rval, pno, ext_partno = PARTNO_NOTFOUND;
5222e998e519SSheshadri Vasudevan #endif
52237c478bd9Sstevel@tonic-gate 
52247c478bd9Sstevel@tonic-gate 	/* form whole disk (p0) */
52257c478bd9Sstevel@tonic-gate 	slice = device + strlen(device) - 2;
52267c478bd9Sstevel@tonic-gate 	is_pcfs = (*slice != 's');
52277c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
52287c478bd9Sstevel@tonic-gate 		*slice = '\0';
52297c478bd9Sstevel@tonic-gate 	wholedisk = s_calloc(1, strlen(device) + 3);
52307c478bd9Sstevel@tonic-gate 	(void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device);
52317c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
52327c478bd9Sstevel@tonic-gate 		*slice = 's';
52337c478bd9Sstevel@tonic-gate 
52347c478bd9Sstevel@tonic-gate 	/* read boot sector */
52357c478bd9Sstevel@tonic-gate 	fd = open(wholedisk, O_RDONLY);
52367c478bd9Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
52377c478bd9Sstevel@tonic-gate 		return (partno);
52387c478bd9Sstevel@tonic-gate 	}
52397c478bd9Sstevel@tonic-gate 	(void) close(fd);
52407c478bd9Sstevel@tonic-gate 
5241e998e519SSheshadri Vasudevan #ifdef i386
5242e998e519SSheshadri Vasudevan 	/* Read/Initialize extended partition information */
5243e998e519SSheshadri Vasudevan 	if ((rval = libfdisk_init(&epp, wholedisk, NULL, FDISK_READ_DISK))
5244e998e519SSheshadri Vasudevan 	    != FDISK_SUCCESS) {
5245e998e519SSheshadri Vasudevan 		switch (rval) {
5246e998e519SSheshadri Vasudevan 			/*
5247e998e519SSheshadri Vasudevan 			 * FDISK_EBADLOGDRIVE and FDISK_ENOLOGDRIVE can
5248e998e519SSheshadri Vasudevan 			 * be considered as soft errors and hence
5249e998e519SSheshadri Vasudevan 			 * we do not return
5250e998e519SSheshadri Vasudevan 			 */
5251e998e519SSheshadri Vasudevan 			case FDISK_EBADLOGDRIVE:
5252e998e519SSheshadri Vasudevan 				break;
5253e998e519SSheshadri Vasudevan 			case FDISK_ENOLOGDRIVE:
5254e998e519SSheshadri Vasudevan 				break;
52556cb5747bSSharath M Srinivasan 			case FDISK_EBADMAGIC:
52566cb5747bSSharath M Srinivasan 				/*FALLTHROUGH*/
5257e998e519SSheshadri Vasudevan 			default:
5258e998e519SSheshadri Vasudevan 				free(wholedisk);
52596cb5747bSSharath M Srinivasan 				libfdisk_fini(&epp);
5260e998e519SSheshadri Vasudevan 				return (partno);
5261e998e519SSheshadri Vasudevan 		}
5262e998e519SSheshadri Vasudevan 	}
5263e998e519SSheshadri Vasudevan #endif
5264e998e519SSheshadri Vasudevan 	free(wholedisk);
5265e998e519SSheshadri Vasudevan 
52667c478bd9Sstevel@tonic-gate 	/* parse fdisk table */
52677c478bd9Sstevel@tonic-gate 	mboot = (struct mboot *)((void *)boot_sect);
52687c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
52697c478bd9Sstevel@tonic-gate 		struct ipart *part =
52707c478bd9Sstevel@tonic-gate 		    (struct ipart *)(uintptr_t)mboot->parts + i;
52717c478bd9Sstevel@tonic-gate 		if (is_pcfs) {	/* looking for solaris boot part */
52727c478bd9Sstevel@tonic-gate 			if (part->systid == 0xbe) {
52737c478bd9Sstevel@tonic-gate 				partno = i;
52747c478bd9Sstevel@tonic-gate 				break;
52757c478bd9Sstevel@tonic-gate 			}
52767c478bd9Sstevel@tonic-gate 		} else {	/* look for solaris partition, old and new */
52771a902ef8SHans Rosenfeld 			if (part->systid == EFI_PMBR) {
52781a902ef8SHans Rosenfeld 				partno = PARTNO_EFI;
52791a902ef8SHans Rosenfeld 				break;
52801a902ef8SHans Rosenfeld 			}
52811a902ef8SHans Rosenfeld 
5282e998e519SSheshadri Vasudevan #ifdef i386
5283e998e519SSheshadri Vasudevan 			if ((part->systid == SUNIXOS &&
5284e998e519SSheshadri Vasudevan 			    (fdisk_is_linux_swap(epp, part->relsect,
5285e998e519SSheshadri Vasudevan 			    NULL) != 0)) || part->systid == SUNIXOS2) {
5286e998e519SSheshadri Vasudevan #else
52877c478bd9Sstevel@tonic-gate 			if (part->systid == SUNIXOS ||
52887c478bd9Sstevel@tonic-gate 			    part->systid == SUNIXOS2) {
5289e998e519SSheshadri Vasudevan #endif
52907c478bd9Sstevel@tonic-gate 				partno = i;
52917c478bd9Sstevel@tonic-gate 				break;
52927c478bd9Sstevel@tonic-gate 			}
529328074f82SSheshadri Vasudevan 
529428074f82SSheshadri Vasudevan #ifdef i386
529528074f82SSheshadri Vasudevan 			if (fdisk_is_dos_extended(part->systid))
529628074f82SSheshadri Vasudevan 				ext_partno = i;
529728074f82SSheshadri Vasudevan #endif
52987c478bd9Sstevel@tonic-gate 		}
52997c478bd9Sstevel@tonic-gate 	}
5300e998e519SSheshadri Vasudevan #ifdef i386
530128074f82SSheshadri Vasudevan 	/* If no primary solaris partition, check extended partition */
53021a902ef8SHans Rosenfeld 	if ((partno == PARTNO_NOTFOUND) && (ext_partno != PARTNO_NOTFOUND)) {
530328074f82SSheshadri Vasudevan 		rval = fdisk_get_solaris_part(epp, &pno, &secnum, &numsec);
530428074f82SSheshadri Vasudevan 		if (rval == FDISK_SUCCESS) {
530528074f82SSheshadri Vasudevan 			partno = pno - 1;
530628074f82SSheshadri Vasudevan 		}
530728074f82SSheshadri Vasudevan 	}
5308e998e519SSheshadri Vasudevan 	libfdisk_fini(&epp);
5309e998e519SSheshadri Vasudevan #endif
53107c478bd9Sstevel@tonic-gate 	return (partno);
53117c478bd9Sstevel@tonic-gate }
53127c478bd9Sstevel@tonic-gate 
5313eb2bd662Svikram char *
5314eb2bd662Svikram get_grubroot(char *osroot, char *osdev, char *menu_root)
53157c478bd9Sstevel@tonic-gate {
5316eb2bd662Svikram 	char		*grubroot;	/* (hd#,#,#) */
53177c478bd9Sstevel@tonic-gate 	char		*slice;
53187c478bd9Sstevel@tonic-gate 	char		*grubhd;
53197c478bd9Sstevel@tonic-gate 	int		fdiskpart;
53207c478bd9Sstevel@tonic-gate 	int		found = 0;
5321eb2bd662Svikram 	char		*devname;
5322eb2bd662Svikram 	char		*ctdname = strstr(osdev, "dsk/");
53237c478bd9Sstevel@tonic-gate 	char		linebuf[PATH_MAX];
5324eb2bd662Svikram 	FILE		*fp;
53257c478bd9Sstevel@tonic-gate 
5326eb2bd662Svikram 	INJECT_ERROR1("GRUBROOT_INVALID_OSDEV", ctdname = NULL);
5327eb2bd662Svikram 	if (ctdname == NULL) {
5328eb2bd662Svikram 		bam_error(INVALID_DEV_DSK, osdev);
53297c478bd9Sstevel@tonic-gate 		return (NULL);
5330eb2bd662Svikram 	}
5331eb2bd662Svikram 
5332eb2bd662Svikram 	if (menu_root && !menu_on_bootdisk(osroot, menu_root)) {
5333eb2bd662Svikram 		/* menu bears no resemblance to our reality */
533437eb779cSVikram Hegde 		bam_error(CANNOT_GRUBROOT_BOOTDISK, osdev);
5335eb2bd662Svikram 		return (NULL);
5336eb2bd662Svikram 	}
53377c478bd9Sstevel@tonic-gate 
53387c478bd9Sstevel@tonic-gate 	ctdname += strlen("dsk/");
53397c478bd9Sstevel@tonic-gate 	slice = strrchr(ctdname, 's');
53407c478bd9Sstevel@tonic-gate 	if (slice)
53417c478bd9Sstevel@tonic-gate 		*slice = '\0';
53427c478bd9Sstevel@tonic-gate 
5343eb2bd662Svikram 	fp = create_diskmap(osroot);
5344eb2bd662Svikram 	if (fp == NULL) {
5345eb2bd662Svikram 		bam_error(DISKMAP_FAIL, osroot);
5346eb2bd662Svikram 		return (NULL);
5347eb2bd662Svikram 	}
5348eb2bd662Svikram 
53497c478bd9Sstevel@tonic-gate 	rewind(fp);
53507c478bd9Sstevel@tonic-gate 	while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) {
53517c478bd9Sstevel@tonic-gate 		grubhd = strtok(linebuf, " \t\n");
53527c478bd9Sstevel@tonic-gate 		if (grubhd)
53537c478bd9Sstevel@tonic-gate 			devname = strtok(NULL, " \t\n");
53547c478bd9Sstevel@tonic-gate 		else
53557c478bd9Sstevel@tonic-gate 			devname = NULL;
53567c478bd9Sstevel@tonic-gate 		if (devname && strcmp(devname, ctdname) == 0) {
53577c478bd9Sstevel@tonic-gate 			found = 1;
53587c478bd9Sstevel@tonic-gate 			break;
53597c478bd9Sstevel@tonic-gate 		}
53607c478bd9Sstevel@tonic-gate 	}
53617c478bd9Sstevel@tonic-gate 
53627c478bd9Sstevel@tonic-gate 	if (slice)
53637c478bd9Sstevel@tonic-gate 		*slice = 's';
53647c478bd9Sstevel@tonic-gate 
5365eb2bd662Svikram 	(void) fclose(fp);
5366eb2bd662Svikram 	fp = NULL;
5367eb2bd662Svikram 
5368eb2bd662Svikram 	INJECT_ERROR1("GRUBROOT_BIOSDEV_FAIL", found = 0);
53697c478bd9Sstevel@tonic-gate 	if (found == 0) {
537037eb779cSVikram Hegde 		bam_error(BIOSDEV_SKIP, osdev);
5371eb2bd662Svikram 		return (NULL);
53727c478bd9Sstevel@tonic-gate 	}
53737c478bd9Sstevel@tonic-gate 
5374eb2bd662Svikram 	fdiskpart = get_partition(osdev);
53751a902ef8SHans Rosenfeld 	INJECT_ERROR1("GRUBROOT_FDISK_FAIL", fdiskpart = PARTNO_NOTFOUND);
53761a902ef8SHans Rosenfeld 	if (fdiskpart == PARTNO_NOTFOUND) {
5377eb2bd662Svikram 		bam_error(FDISKPART_FAIL, osdev);
53787c478bd9Sstevel@tonic-gate 		return (NULL);
5379eb2bd662Svikram 	}
53807c478bd9Sstevel@tonic-gate 
5381eb2bd662Svikram 	grubroot = s_calloc(1, 10);
53821a902ef8SHans Rosenfeld 	if (fdiskpart == PARTNO_EFI) {
53831a902ef8SHans Rosenfeld 		fdiskpart = atoi(&slice[1]);
53841a902ef8SHans Rosenfeld 		slice = NULL;
53851a902ef8SHans Rosenfeld 	}
53861a902ef8SHans Rosenfeld 
53877c478bd9Sstevel@tonic-gate 	if (slice) {
5388eb2bd662Svikram 		(void) snprintf(grubroot, 10, "(hd%s,%d,%c)",
53897c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart, slice[1] + 'a' - '0');
53907c478bd9Sstevel@tonic-gate 	} else
5391eb2bd662Svikram 		(void) snprintf(grubroot, 10, "(hd%s,%d)",
53927c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart);
53937c478bd9Sstevel@tonic-gate 
5394eb2bd662Svikram 	assert(fp == NULL);
5395eb2bd662Svikram 	assert(strncmp(grubroot, "(hd", strlen("(hd")) == 0);
5396eb2bd662Svikram 	return (grubroot);
5397eb2bd662Svikram }
5398eb2bd662Svikram 
5399eb2bd662Svikram static char *
5400eb2bd662Svikram find_primary_common(char *mntpt, char *fstype)
5401eb2bd662Svikram {
5402eb2bd662Svikram 	char		signdir[PATH_MAX];
5403eb2bd662Svikram 	char		tmpsign[MAXNAMELEN + 1];
5404eb2bd662Svikram 	char		*lu;
5405eb2bd662Svikram 	char		*ufs;
5406eb2bd662Svikram 	char		*zfs;
5407eb2bd662Svikram 	DIR		*dirp = NULL;
5408eb2bd662Svikram 	struct dirent	*entp;
5409eb2bd662Svikram 	struct stat	sb;
5410eb2bd662Svikram 	const char	*fcn = "find_primary_common()";
5411eb2bd662Svikram 
5412eb2bd662Svikram 	(void) snprintf(signdir, sizeof (signdir), "%s/%s",
5413eb2bd662Svikram 	    mntpt, GRUBSIGN_DIR);
5414eb2bd662Svikram 
5415eb2bd662Svikram 	if (stat(signdir, &sb) == -1) {
5416eb2bd662Svikram 		BAM_DPRINTF((D_NO_SIGNDIR, fcn, signdir));
5417eb2bd662Svikram 		return (NULL);
5418eb2bd662Svikram 	}
5419eb2bd662Svikram 
5420eb2bd662Svikram 	dirp = opendir(signdir);
5421eb2bd662Svikram 	INJECT_ERROR1("SIGNDIR_OPENDIR_FAIL", dirp = NULL);
5422eb2bd662Svikram 	if (dirp == NULL) {
5423eb2bd662Svikram 		bam_error(OPENDIR_FAILED, signdir, strerror(errno));
5424eb2bd662Svikram 		return (NULL);
5425eb2bd662Svikram 	}
5426eb2bd662Svikram 
5427eb2bd662Svikram 	ufs = zfs = lu = NULL;
5428eb2bd662Svikram 
5429eb2bd662Svikram 	while (entp = readdir(dirp)) {
5430eb2bd662Svikram 		if (strcmp(entp->d_name, ".") == 0 ||
5431eb2bd662Svikram 		    strcmp(entp->d_name, "..") == 0)
5432eb2bd662Svikram 			continue;
5433eb2bd662Svikram 
5434eb2bd662Svikram 		(void) snprintf(tmpsign, sizeof (tmpsign), "%s", entp->d_name);
5435eb2bd662Svikram 
5436eb2bd662Svikram 		if (lu == NULL &&
5437eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_LU_PREFIX,
5438eb2bd662Svikram 		    strlen(GRUBSIGN_LU_PREFIX)) == 0) {
5439eb2bd662Svikram 			lu = s_strdup(tmpsign);
5440eb2bd662Svikram 		}
5441eb2bd662Svikram 
5442eb2bd662Svikram 		if (ufs == NULL &&
5443eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
5444eb2bd662Svikram 		    strlen(GRUBSIGN_UFS_PREFIX)) == 0) {
5445eb2bd662Svikram 			ufs = s_strdup(tmpsign);
5446eb2bd662Svikram 		}
5447eb2bd662Svikram 
5448eb2bd662Svikram 		if (zfs == NULL &&
5449eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_ZFS_PREFIX,
5450eb2bd662Svikram 		    strlen(GRUBSIGN_ZFS_PREFIX)) == 0) {
5451eb2bd662Svikram 			zfs = s_strdup(tmpsign);
5452eb2bd662Svikram 		}
5453eb2bd662Svikram 	}
5454eb2bd662Svikram 
5455eb2bd662Svikram 	BAM_DPRINTF((D_EXIST_PRIMARY_SIGNS, fcn,
5456eb2bd662Svikram 	    zfs ? zfs : "NULL",
5457eb2bd662Svikram 	    ufs ? ufs : "NULL",
5458eb2bd662Svikram 	    lu ? lu : "NULL"));
5459eb2bd662Svikram 
5460eb2bd662Svikram 	if (dirp) {
5461eb2bd662Svikram 		(void) closedir(dirp);
5462eb2bd662Svikram 		dirp = NULL;
5463eb2bd662Svikram 	}
5464eb2bd662Svikram 
5465eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0 && zfs) {
5466eb2bd662Svikram 		bam_error(SIGN_FSTYPE_MISMATCH, zfs, "ufs");
5467eb2bd662Svikram 		free(zfs);
5468eb2bd662Svikram 		zfs = NULL;
5469eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0 && ufs) {
5470eb2bd662Svikram 		bam_error(SIGN_FSTYPE_MISMATCH, ufs, "zfs");
5471eb2bd662Svikram 		free(ufs);
5472eb2bd662Svikram 		ufs = NULL;
5473eb2bd662Svikram 	}
5474eb2bd662Svikram 
5475eb2bd662Svikram 	assert(dirp == NULL);
5476eb2bd662Svikram 
5477eb2bd662Svikram 	/* For now, we let Live Upgrade take care of its signature itself */
5478eb2bd662Svikram 	if (lu) {
5479eb2bd662Svikram 		BAM_DPRINTF((D_FREEING_LU_SIGNS, fcn, lu));
5480eb2bd662Svikram 		free(lu);
5481eb2bd662Svikram 		lu = NULL;
5482eb2bd662Svikram 	}
5483eb2bd662Svikram 
5484eb2bd662Svikram 	return (zfs ? zfs : ufs);
5485eb2bd662Svikram }
5486eb2bd662Svikram 
5487eb2bd662Svikram static char *
5488eb2bd662Svikram find_backup_common(char *mntpt, char *fstype)
5489eb2bd662Svikram {
5490eb2bd662Svikram 	FILE		*bfp = NULL;
5491eb2bd662Svikram 	char		tmpsign[MAXNAMELEN + 1];
5492eb2bd662Svikram 	char		backup[PATH_MAX];
5493eb2bd662Svikram 	char		*ufs;
5494eb2bd662Svikram 	char		*zfs;
5495eb2bd662Svikram 	char		*lu;
5496eb2bd662Svikram 	int		error;
5497eb2bd662Svikram 	const char	*fcn = "find_backup_common()";
5498eb2bd662Svikram 
5499eb2bd662Svikram 	/*
5500eb2bd662Svikram 	 * We didn't find it in the primary directory.
5501eb2bd662Svikram 	 * Look at the backup
5502eb2bd662Svikram 	 */
5503eb2bd662Svikram 	(void) snprintf(backup, sizeof (backup), "%s%s",
5504eb2bd662Svikram 	    mntpt, GRUBSIGN_BACKUP);
5505eb2bd662Svikram 
5506eb2bd662Svikram 	bfp = fopen(backup, "r");
5507eb2bd662Svikram 	if (bfp == NULL) {
5508eb2bd662Svikram 		error = errno;
5509eb2bd662Svikram 		if (bam_verbose) {
5510eb2bd662Svikram 			bam_error(OPEN_FAIL, backup, strerror(error));
5511eb2bd662Svikram 		}
5512eb2bd662Svikram 		BAM_DPRINTF((D_OPEN_FAIL, fcn, backup, strerror(error)));
5513eb2bd662Svikram 		return (NULL);
5514eb2bd662Svikram 	}
5515eb2bd662Svikram 
5516eb2bd662Svikram 	ufs = zfs = lu = NULL;
5517eb2bd662Svikram 
5518eb2bd662Svikram 	while (s_fgets(tmpsign, sizeof (tmpsign), bfp) != NULL) {
5519eb2bd662Svikram 
5520eb2bd662Svikram 		if (lu == NULL &&
5521eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_LU_PREFIX,
5522eb2bd662Svikram 		    strlen(GRUBSIGN_LU_PREFIX)) == 0) {
5523eb2bd662Svikram 			lu = s_strdup(tmpsign);
5524eb2bd662Svikram 		}
5525eb2bd662Svikram 
5526eb2bd662Svikram 		if (ufs == NULL &&
5527eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
5528eb2bd662Svikram 		    strlen(GRUBSIGN_UFS_PREFIX)) == 0) {
5529eb2bd662Svikram 			ufs = s_strdup(tmpsign);
5530eb2bd662Svikram 		}
5531eb2bd662Svikram 
5532eb2bd662Svikram 		if (zfs == NULL &&
5533eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_ZFS_PREFIX,
5534eb2bd662Svikram 		    strlen(GRUBSIGN_ZFS_PREFIX)) == 0) {
5535eb2bd662Svikram 			zfs = s_strdup(tmpsign);
5536eb2bd662Svikram 		}
5537eb2bd662Svikram 	}
5538eb2bd662Svikram 
5539eb2bd662Svikram 	BAM_DPRINTF((D_EXIST_BACKUP_SIGNS, fcn,
5540eb2bd662Svikram 	    zfs ? zfs : "NULL",
5541eb2bd662Svikram 	    ufs ? ufs : "NULL",
5542eb2bd662Svikram 	    lu ? lu : "NULL"));
5543eb2bd662Svikram 
5544eb2bd662Svikram 	if (bfp) {
5545eb2bd662Svikram 		(void) fclose(bfp);
5546eb2bd662Svikram 		bfp = NULL;
5547eb2bd662Svikram 	}
5548eb2bd662Svikram 
5549eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0 && zfs) {
5550eb2bd662Svikram 		bam_error(SIGN_FSTYPE_MISMATCH, zfs, "ufs");
5551eb2bd662Svikram 		free(zfs);
5552eb2bd662Svikram 		zfs = NULL;
5553eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0 && ufs) {
5554eb2bd662Svikram 		bam_error(SIGN_FSTYPE_MISMATCH, ufs, "zfs");
5555eb2bd662Svikram 		free(ufs);
5556eb2bd662Svikram 		ufs = NULL;
5557eb2bd662Svikram 	}
5558eb2bd662Svikram 
5559eb2bd662Svikram 	assert(bfp == NULL);
5560eb2bd662Svikram 
5561eb2bd662Svikram 	/* For now, we let Live Upgrade take care of its signature itself */
5562eb2bd662Svikram 	if (lu) {
5563eb2bd662Svikram 		BAM_DPRINTF((D_FREEING_LU_SIGNS, fcn, lu));
5564eb2bd662Svikram 		free(lu);
5565eb2bd662Svikram 		lu = NULL;
5566eb2bd662Svikram 	}
5567eb2bd662Svikram 
5568eb2bd662Svikram 	return (zfs ? zfs : ufs);
5569eb2bd662Svikram }
5570eb2bd662Svikram 
5571eb2bd662Svikram static char *
5572eb2bd662Svikram find_ufs_existing(char *osroot)
5573eb2bd662Svikram {
5574eb2bd662Svikram 	char		*sign;
5575eb2bd662Svikram 	const char	*fcn = "find_ufs_existing()";
5576eb2bd662Svikram 
5577eb2bd662Svikram 	sign = find_primary_common(osroot, "ufs");
5578eb2bd662Svikram 	if (sign == NULL) {
5579eb2bd662Svikram 		sign = find_backup_common(osroot, "ufs");
5580eb2bd662Svikram 		BAM_DPRINTF((D_EXIST_BACKUP_SIGN, fcn, sign ? sign : "NULL"));
5581eb2bd662Svikram 	} else {
5582eb2bd662Svikram 		BAM_DPRINTF((D_EXIST_PRIMARY_SIGN, fcn, sign));
5583eb2bd662Svikram 	}
5584eb2bd662Svikram 
5585eb2bd662Svikram 	return (sign);
5586eb2bd662Svikram }
5587eb2bd662Svikram 
5588eb2bd662Svikram char *
5589eb2bd662Svikram get_mountpoint(char *special, char *fstype)
5590eb2bd662Svikram {
5591eb2bd662Svikram 	FILE		*mntfp;
5592eb2bd662Svikram 	struct mnttab	mp = {0};
5593eb2bd662Svikram 	struct mnttab	mpref = {0};
5594eb2bd662Svikram 	int		error;
5595eb2bd662Svikram 	int		ret;
5596eb2bd662Svikram 	const char	*fcn = "get_mountpoint()";
5597eb2bd662Svikram 
5598eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, special, fstype));
5599eb2bd662Svikram 
5600eb2bd662Svikram 	mntfp = fopen(MNTTAB, "r");
5601eb2bd662Svikram 	error = errno;
5602eb2bd662Svikram 	INJECT_ERROR1("MNTTAB_ERR_GET_MNTPT", mntfp = NULL);
5603eb2bd662Svikram 	if (mntfp == NULL) {
5604eb2bd662Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
5605eb2bd662Svikram 		return (NULL);
5606eb2bd662Svikram 	}
5607eb2bd662Svikram 
5608eb2bd662Svikram 	mpref.mnt_special = special;
5609eb2bd662Svikram 	mpref.mnt_fstype = fstype;
5610eb2bd662Svikram 
5611eb2bd662Svikram 	ret = getmntany(mntfp, &mp, &mpref);
5612eb2bd662Svikram 	INJECT_ERROR1("GET_MOUNTPOINT_MNTANY", ret = 1);
5613eb2bd662Svikram 	if (ret != 0) {
5614eb2bd662Svikram 		(void) fclose(mntfp);
5615eb2bd662Svikram 		BAM_DPRINTF((D_NO_MNTPT, fcn, special, fstype));
5616eb2bd662Svikram 		return (NULL);
5617eb2bd662Svikram 	}
5618eb2bd662Svikram 	(void) fclose(mntfp);
5619eb2bd662Svikram 
5620eb2bd662Svikram 	assert(mp.mnt_mountp);
5621eb2bd662Svikram 
5622eb2bd662Svikram 	BAM_DPRINTF((D_GET_MOUNTPOINT_RET, fcn, special, mp.mnt_mountp));
5623eb2bd662Svikram 
5624eb2bd662Svikram 	return (s_strdup(mp.mnt_mountp));
5625eb2bd662Svikram }
5626eb2bd662Svikram 
5627eb2bd662Svikram /*
5628eb2bd662Svikram  * Mounts a "legacy" top dataset (if needed)
5629eb2bd662Svikram  * Returns:	The mountpoint of the legacy top dataset or NULL on error
5630eb2bd662Svikram  * 		mnted returns one of the above values defined for zfs_mnted_t
5631eb2bd662Svikram  */
5632eb2bd662Svikram static char *
5633eb2bd662Svikram mount_legacy_dataset(char *pool, zfs_mnted_t *mnted)
5634eb2bd662Svikram {
5635eb2bd662Svikram 	char		cmd[PATH_MAX];
5636eb2bd662Svikram 	char		tmpmnt[PATH_MAX];
5637eb2bd662Svikram 	filelist_t	flist = {0};
5638eb2bd662Svikram 	char		*is_mounted;
5639eb2bd662Svikram 	struct stat	sb;
5640eb2bd662Svikram 	int		ret;
5641eb2bd662Svikram 	const char	*fcn = "mount_legacy_dataset()";
5642eb2bd662Svikram 
5643eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, pool));
5644eb2bd662Svikram 
5645eb2bd662Svikram 	*mnted = ZFS_MNT_ERROR;
5646eb2bd662Svikram 
5647eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5648eb2bd662Svikram 	    "/sbin/zfs get -Ho value mounted %s",
5649eb2bd662Svikram 	    pool);
5650eb2bd662Svikram 
5651eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5652eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_CMD", ret = 1);
5653eb2bd662Svikram 	if (ret != 0) {
5654eb2bd662Svikram 		bam_error(ZFS_MNTED_FAILED, pool);
5655eb2bd662Svikram 		return (NULL);
5656eb2bd662Svikram 	}
5657eb2bd662Svikram 
5658eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_OUT", flist.head = NULL);
5659eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5660eb2bd662Svikram 		bam_error(BAD_ZFS_MNTED, pool);
5661eb2bd662Svikram 		filelist_free(&flist);
5662eb2bd662Svikram 		return (NULL);
5663eb2bd662Svikram 	}
5664eb2bd662Svikram 
5665eb2bd662Svikram 	is_mounted = strtok(flist.head->line, " \t\n");
5666eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_STRTOK_YES", is_mounted = "yes");
5667eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_STRTOK_NO", is_mounted = "no");
5668eb2bd662Svikram 	if (strcmp(is_mounted, "no") != 0) {
5669eb2bd662Svikram 		filelist_free(&flist);
5670eb2bd662Svikram 		*mnted = LEGACY_ALREADY;
5671eb2bd662Svikram 		/* get_mountpoint returns a strdup'ed string */
5672eb2bd662Svikram 		BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_ALREADY, fcn, pool));
5673eb2bd662Svikram 		return (get_mountpoint(pool, "zfs"));
5674eb2bd662Svikram 	}
5675eb2bd662Svikram 
5676eb2bd662Svikram 	filelist_free(&flist);
5677eb2bd662Svikram 
5678eb2bd662Svikram 	/*
5679eb2bd662Svikram 	 * legacy top dataset is not mounted. Mount it now
5680eb2bd662Svikram 	 * First create a mountpoint.
5681eb2bd662Svikram 	 */
5682eb2bd662Svikram 	(void) snprintf(tmpmnt, sizeof (tmpmnt), "%s.%d",
5683eb2bd662Svikram 	    ZFS_LEGACY_MNTPT, getpid());
5684eb2bd662Svikram 
5685eb2bd662Svikram 	ret = stat(tmpmnt, &sb);
5686eb2bd662Svikram 	if (ret == -1) {
5687eb2bd662Svikram 		BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_MNTPT_ABS, fcn, pool, tmpmnt));
568848847494SEnrico Perla - Sun Microsystems 		ret = mkdirp(tmpmnt, DIR_PERMS);
5689eb2bd662Svikram 		INJECT_ERROR1("Z_MOUNT_TOP_LEG_MNTPT_MKDIRP", ret = -1);
5690eb2bd662Svikram 		if (ret == -1) {
5691eb2bd662Svikram 			bam_error(MKDIR_FAILED, tmpmnt, strerror(errno));
5692eb2bd662Svikram 			return (NULL);
5693eb2bd662Svikram 		}
5694eb2bd662Svikram 	} else {
5695eb2bd662Svikram 		BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_MNTPT_PRES, fcn, pool, tmpmnt));
5696eb2bd662Svikram 	}
5697eb2bd662Svikram 
5698eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5699eb2bd662Svikram 	    "/sbin/mount -F zfs %s %s",
5700eb2bd662Svikram 	    pool, tmpmnt);
5701eb2bd662Svikram 
5702eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
5703eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_LEG_MOUNT_CMD", ret = 1);
5704eb2bd662Svikram 	if (ret != 0) {
5705eb2bd662Svikram 		bam_error(ZFS_MOUNT_FAILED, pool);
5706eb2bd662Svikram 		(void) rmdir(tmpmnt);
5707eb2bd662Svikram 		return (NULL);
5708eb2bd662Svikram 	}
5709eb2bd662Svikram 
5710eb2bd662Svikram 	*mnted = LEGACY_MOUNTED;
5711eb2bd662Svikram 	BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_MOUNTED, fcn, pool, tmpmnt));
5712eb2bd662Svikram 	return (s_strdup(tmpmnt));
5713eb2bd662Svikram }
5714eb2bd662Svikram 
5715eb2bd662Svikram /*
5716eb2bd662Svikram  * Mounts the top dataset (if needed)
5717eb2bd662Svikram  * Returns:	The mountpoint of the top dataset or NULL on error
5718eb2bd662Svikram  * 		mnted returns one of the above values defined for zfs_mnted_t
5719eb2bd662Svikram  */
5720eb2bd662Svikram static char *
5721eb2bd662Svikram mount_top_dataset(char *pool, zfs_mnted_t *mnted)
5722eb2bd662Svikram {
5723eb2bd662Svikram 	char		cmd[PATH_MAX];
5724eb2bd662Svikram 	filelist_t	flist = {0};
5725eb2bd662Svikram 	char		*is_mounted;
5726eb2bd662Svikram 	char		*mntpt;
5727eb2bd662Svikram 	char		*zmntpt;
5728eb2bd662Svikram 	int		ret;
5729eb2bd662Svikram 	const char	*fcn = "mount_top_dataset()";
5730eb2bd662Svikram 
5731eb2bd662Svikram 	*mnted = ZFS_MNT_ERROR;
5732eb2bd662Svikram 
5733eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, pool));
5734eb2bd662Svikram 
5735eb2bd662Svikram 	/*
5736eb2bd662Svikram 	 * First check if the top dataset is a "legacy" dataset
5737eb2bd662Svikram 	 */
5738eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5739eb2bd662Svikram 	    "/sbin/zfs get -Ho value mountpoint %s",
5740eb2bd662Svikram 	    pool);
5741eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5742eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_GET_MNTPT", ret = 1);
5743eb2bd662Svikram 	if (ret != 0) {
5744eb2bd662Svikram 		bam_error(ZFS_MNTPT_FAILED, pool);
5745eb2bd662Svikram 		return (NULL);
5746eb2bd662Svikram 	}
5747eb2bd662Svikram 
5748eb2bd662Svikram 	if (flist.head && (flist.head == flist.tail)) {
5749eb2bd662Svikram 		char *legacy = strtok(flist.head->line, " \t\n");
5750eb2bd662Svikram 		if (legacy && strcmp(legacy, "legacy") == 0) {
5751eb2bd662Svikram 			filelist_free(&flist);
5752eb2bd662Svikram 			BAM_DPRINTF((D_Z_IS_LEGACY, fcn, pool));
5753eb2bd662Svikram 			return (mount_legacy_dataset(pool, mnted));
5754eb2bd662Svikram 		}
5755eb2bd662Svikram 	}
5756eb2bd662Svikram 
5757eb2bd662Svikram 	filelist_free(&flist);
5758eb2bd662Svikram 
5759eb2bd662Svikram 	BAM_DPRINTF((D_Z_IS_NOT_LEGACY, fcn, pool));
5760eb2bd662Svikram 
5761eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5762eb2bd662Svikram 	    "/sbin/zfs get -Ho value mounted %s",
5763eb2bd662Svikram 	    pool);
5764eb2bd662Svikram 
5765eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5766eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED", ret = 1);
5767eb2bd662Svikram 	if (ret != 0) {
5768eb2bd662Svikram 		bam_error(ZFS_MNTED_FAILED, pool);
5769eb2bd662Svikram 		return (NULL);
5770eb2bd662Svikram 	}
5771eb2bd662Svikram 
5772eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_VAL", flist.head = NULL);
5773eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5774eb2bd662Svikram 		bam_error(BAD_ZFS_MNTED, pool);
5775eb2bd662Svikram 		filelist_free(&flist);
5776eb2bd662Svikram 		return (NULL);
5777eb2bd662Svikram 	}
5778eb2bd662Svikram 
5779eb2bd662Svikram 	is_mounted = strtok(flist.head->line, " \t\n");
5780eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_YES", is_mounted = "yes");
5781eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_NO", is_mounted = "no");
5782eb2bd662Svikram 	if (strcmp(is_mounted, "no") != 0) {
5783eb2bd662Svikram 		filelist_free(&flist);
5784eb2bd662Svikram 		*mnted = ZFS_ALREADY;
5785eb2bd662Svikram 		BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MOUNTED_ALREADY, fcn, pool));
5786eb2bd662Svikram 		goto mounted;
5787eb2bd662Svikram 	}
5788eb2bd662Svikram 
5789eb2bd662Svikram 	filelist_free(&flist);
5790eb2bd662Svikram 	BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MOUNTED_NOT_ALREADY, fcn, pool));
5791eb2bd662Svikram 
5792eb2bd662Svikram 	/* top dataset is not mounted. Mount it now */
5793eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5794eb2bd662Svikram 	    "/sbin/zfs mount %s", pool);
5795eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
5796eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_MOUNT_CMD", ret = 1);
5797eb2bd662Svikram 	if (ret != 0) {
5798eb2bd662Svikram 		bam_error(ZFS_MOUNT_FAILED, pool);
5799eb2bd662Svikram 		return (NULL);
5800eb2bd662Svikram 	}
5801eb2bd662Svikram 	*mnted = ZFS_MOUNTED;
5802eb2bd662Svikram 	BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MOUNTED_NOW, fcn, pool));
5803eb2bd662Svikram 	/*FALLTHRU*/
5804eb2bd662Svikram mounted:
5805eb2bd662Svikram 	/*
5806eb2bd662Svikram 	 * Now get the mountpoint
5807eb2bd662Svikram 	 */
5808eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5809eb2bd662Svikram 	    "/sbin/zfs get -Ho value mountpoint %s",
5810eb2bd662Svikram 	    pool);
5811eb2bd662Svikram 
5812eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5813eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_CMD", ret = 1);
5814eb2bd662Svikram 	if (ret != 0) {
5815eb2bd662Svikram 		bam_error(ZFS_MNTPT_FAILED, pool);
5816eb2bd662Svikram 		goto error;
5817eb2bd662Svikram 	}
5818eb2bd662Svikram 
5819eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_OUT", flist.head = NULL);
5820eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5821eb2bd662Svikram 		bam_error(NULL_ZFS_MNTPT, pool);
5822eb2bd662Svikram 		goto error;
5823eb2bd662Svikram 	}
5824eb2bd662Svikram 
5825eb2bd662Svikram 	mntpt = strtok(flist.head->line, " \t\n");
5826eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_STRTOK", mntpt = "foo");
5827eb2bd662Svikram 	if (*mntpt != '/') {
5828eb2bd662Svikram 		bam_error(BAD_ZFS_MNTPT, pool, mntpt);
5829eb2bd662Svikram 		goto error;
5830eb2bd662Svikram 	}
5831eb2bd662Svikram 	zmntpt = s_strdup(mntpt);
5832eb2bd662Svikram 
5833eb2bd662Svikram 	filelist_free(&flist);
5834eb2bd662Svikram 
5835eb2bd662Svikram 	BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MNTPT, fcn, pool, zmntpt));
5836eb2bd662Svikram 
5837eb2bd662Svikram 	return (zmntpt);
5838eb2bd662Svikram 
5839eb2bd662Svikram error:
5840eb2bd662Svikram 	filelist_free(&flist);
5841eb2bd662Svikram 	(void) umount_top_dataset(pool, *mnted, NULL);
5842eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_FAILURE, fcn));
5843eb2bd662Svikram 	return (NULL);
5844eb2bd662Svikram }
5845eb2bd662Svikram 
5846eb2bd662Svikram static int
5847eb2bd662Svikram umount_top_dataset(char *pool, zfs_mnted_t mnted, char *mntpt)
5848eb2bd662Svikram {
5849eb2bd662Svikram 	char		cmd[PATH_MAX];
5850eb2bd662Svikram 	int		ret;
5851eb2bd662Svikram 	const char	*fcn = "umount_top_dataset()";
5852eb2bd662Svikram 
5853eb2bd662Svikram 	INJECT_ERROR1("Z_UMOUNT_TOP_INVALID_STATE", mnted = ZFS_MNT_ERROR);
5854eb2bd662Svikram 	switch (mnted) {
5855eb2bd662Svikram 	case LEGACY_ALREADY:
5856eb2bd662Svikram 	case ZFS_ALREADY:
5857eb2bd662Svikram 		/* nothing to do */
5858eb2bd662Svikram 		BAM_DPRINTF((D_Z_UMOUNT_TOP_ALREADY_NOP, fcn, pool,
5859eb2bd662Svikram 		    mntpt ? mntpt : "NULL"));
5860eb2bd662Svikram 		free(mntpt);
5861eb2bd662Svikram 		return (BAM_SUCCESS);
5862eb2bd662Svikram 	case LEGACY_MOUNTED:
5863eb2bd662Svikram 		(void) snprintf(cmd, sizeof (cmd),
5864eb2bd662Svikram 		    "/sbin/umount %s", pool);
5865eb2bd662Svikram 		ret = exec_cmd(cmd, NULL);
5866eb2bd662Svikram 		INJECT_ERROR1("Z_UMOUNT_TOP_LEGACY_UMOUNT_FAIL", ret = 1);
5867eb2bd662Svikram 		if (ret != 0) {
5868eb2bd662Svikram 			bam_error(UMOUNT_FAILED, pool);
5869eb2bd662Svikram 			free(mntpt);
5870eb2bd662Svikram 			return (BAM_ERROR);
5871eb2bd662Svikram 		}
5872eb2bd662Svikram 		if (mntpt)
5873eb2bd662Svikram 			(void) rmdir(mntpt);
5874eb2bd662Svikram 		free(mntpt);
5875eb2bd662Svikram 		BAM_DPRINTF((D_Z_UMOUNT_TOP_LEGACY, fcn, pool));
5876eb2bd662Svikram 		return (BAM_SUCCESS);
5877eb2bd662Svikram 	case ZFS_MOUNTED:
5878eb2bd662Svikram 		free(mntpt);
5879eb2bd662Svikram 		(void) snprintf(cmd, sizeof (cmd),
5880eb2bd662Svikram 		    "/sbin/zfs unmount %s", pool);
5881eb2bd662Svikram 		ret = exec_cmd(cmd, NULL);
5882eb2bd662Svikram 		INJECT_ERROR1("Z_UMOUNT_TOP_NONLEG_UMOUNT_FAIL", ret = 1);
5883eb2bd662Svikram 		if (ret != 0) {
5884eb2bd662Svikram 			bam_error(UMOUNT_FAILED, pool);
5885eb2bd662Svikram 			return (BAM_ERROR);
5886eb2bd662Svikram 		}
5887eb2bd662Svikram 		BAM_DPRINTF((D_Z_UMOUNT_TOP_NONLEG, fcn, pool));
5888eb2bd662Svikram 		return (BAM_SUCCESS);
5889eb2bd662Svikram 	default:
5890eb2bd662Svikram 		bam_error(INT_BAD_MNTSTATE, pool);
5891eb2bd662Svikram 		return (BAM_ERROR);
5892eb2bd662Svikram 	}
5893eb2bd662Svikram 	/*NOTREACHED*/
5894eb2bd662Svikram }
5895eb2bd662Svikram 
5896eb2bd662Svikram /*
5897eb2bd662Svikram  * For ZFS, osdev can be one of two forms
5898eb2bd662Svikram  * It can be a "special" file as seen in mnttab: rpool/ROOT/szboot_0402
5899eb2bd662Svikram  * It can be a /dev/[r]dsk special file. We handle both instances
5900eb2bd662Svikram  */
5901eb2bd662Svikram static char *
5902eb2bd662Svikram get_pool(char *osdev)
5903eb2bd662Svikram {
5904eb2bd662Svikram 	char		cmd[PATH_MAX];
5905eb2bd662Svikram 	char		buf[PATH_MAX];
5906eb2bd662Svikram 	filelist_t	flist = {0};
5907eb2bd662Svikram 	char		*pool;
5908eb2bd662Svikram 	char		*cp;
5909eb2bd662Svikram 	char		*slash;
5910eb2bd662Svikram 	int		ret;
5911eb2bd662Svikram 	const char	*fcn = "get_pool()";
5912eb2bd662Svikram 
5913eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_OSDEV", osdev = NULL);
5914eb2bd662Svikram 	if (osdev == NULL) {
5915eb2bd662Svikram 		bam_error(GET_POOL_OSDEV_NULL);
5916eb2bd662Svikram 		return (NULL);
5917eb2bd662Svikram 	}
5918eb2bd662Svikram 
5919eb2bd662Svikram 	BAM_DPRINTF((D_GET_POOL_OSDEV, fcn, osdev));
5920eb2bd662Svikram 
5921eb2bd662Svikram 	if (osdev[0] != '/') {
5922eb2bd662Svikram 		(void) strlcpy(buf, osdev, sizeof (buf));
5923eb2bd662Svikram 		slash = strchr(buf, '/');
5924eb2bd662Svikram 		if (slash)
5925eb2bd662Svikram 			*slash = '\0';
5926eb2bd662Svikram 		pool = s_strdup(buf);
5927eb2bd662Svikram 		BAM_DPRINTF((D_GET_POOL_RET, fcn, pool));
5928eb2bd662Svikram 		return (pool);
5929eb2bd662Svikram 	} else if (strncmp(osdev, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
5930eb2bd662Svikram 	    strncmp(osdev, "/dev/rdsk/", strlen("/dev/rdsk/")) != 0) {
5931eb2bd662Svikram 		bam_error(GET_POOL_BAD_OSDEV, osdev);
5932eb2bd662Svikram 		return (NULL);
5933eb2bd662Svikram 	}
5934eb2bd662Svikram 
5935d5cb36d3SJan Setje-Eilers 	/*
5936d5cb36d3SJan Setje-Eilers 	 * Call the zfs fstyp directly since this is a zpool. This avoids
5937d5cb36d3SJan Setje-Eilers 	 * potential pcfs conflicts if the first block wasn't cleared.
5938d5cb36d3SJan Setje-Eilers 	 */
5939eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5940d5cb36d3SJan Setje-Eilers 	    "/usr/lib/fs/zfs/fstyp -a %s 2>/dev/null | /bin/grep '^name:'",
5941eb2bd662Svikram 	    osdev);
5942eb2bd662Svikram 
5943eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5944eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_FSTYP", ret = 1);
5945eb2bd662Svikram 	if (ret != 0) {
5946eb2bd662Svikram 		bam_error(FSTYP_A_FAILED, osdev);
5947eb2bd662Svikram 		return (NULL);
5948eb2bd662Svikram 	}
5949eb2bd662Svikram 
5950eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_FSTYP_OUT", flist.head = NULL);
5951eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5952eb2bd662Svikram 		bam_error(NULL_FSTYP_A, osdev);
5953eb2bd662Svikram 		filelist_free(&flist);
5954eb2bd662Svikram 		return (NULL);
5955eb2bd662Svikram 	}
5956eb2bd662Svikram 
5957eb2bd662Svikram 	(void) strtok(flist.head->line, "'");
5958eb2bd662Svikram 	cp = strtok(NULL, "'");
5959eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_FSTYP_STRTOK", cp = NULL);
5960eb2bd662Svikram 	if (cp == NULL) {
5961eb2bd662Svikram 		bam_error(BAD_FSTYP_A, osdev);
5962eb2bd662Svikram 		filelist_free(&flist);
5963eb2bd662Svikram 		return (NULL);
5964eb2bd662Svikram 	}
5965eb2bd662Svikram 
5966eb2bd662Svikram 	pool = s_strdup(cp);
5967eb2bd662Svikram 
5968eb2bd662Svikram 	filelist_free(&flist);
5969eb2bd662Svikram 
5970eb2bd662Svikram 	BAM_DPRINTF((D_GET_POOL_RET, fcn, pool));
5971eb2bd662Svikram 
5972eb2bd662Svikram 	return (pool);
5973eb2bd662Svikram }
5974eb2bd662Svikram 
5975eb2bd662Svikram static char *
5976eb2bd662Svikram find_zfs_existing(char *osdev)
5977eb2bd662Svikram {
5978eb2bd662Svikram 	char		*pool;
5979eb2bd662Svikram 	zfs_mnted_t	mnted;
5980eb2bd662Svikram 	char		*mntpt;
5981eb2bd662Svikram 	char		*sign;
5982eb2bd662Svikram 	const char	*fcn = "find_zfs_existing()";
5983eb2bd662Svikram 
5984eb2bd662Svikram 	pool = get_pool(osdev);
5985eb2bd662Svikram 	INJECT_ERROR1("ZFS_FIND_EXIST_POOL", pool = NULL);
5986eb2bd662Svikram 	if (pool == NULL) {
5987eb2bd662Svikram 		bam_error(ZFS_GET_POOL_FAILED, osdev);
5988eb2bd662Svikram 		return (NULL);
5989eb2bd662Svikram 	}
5990eb2bd662Svikram 
5991eb2bd662Svikram 	mntpt = mount_top_dataset(pool, &mnted);
5992eb2bd662Svikram 	INJECT_ERROR1("ZFS_FIND_EXIST_MOUNT_TOP", mntpt = NULL);
5993eb2bd662Svikram 	if (mntpt == NULL) {
5994eb2bd662Svikram 		bam_error(ZFS_MOUNT_TOP_DATASET_FAILED, pool);
5995eb2bd662Svikram 		free(pool);
5996eb2bd662Svikram 		return (NULL);
5997eb2bd662Svikram 	}
5998eb2bd662Svikram 
5999eb2bd662Svikram 	sign = find_primary_common(mntpt, "zfs");
6000eb2bd662Svikram 	if (sign == NULL) {
6001eb2bd662Svikram 		sign = find_backup_common(mntpt, "zfs");
6002eb2bd662Svikram 		BAM_DPRINTF((D_EXIST_BACKUP_SIGN, fcn, sign ? sign : "NULL"));
6003eb2bd662Svikram 	} else {
6004eb2bd662Svikram 		BAM_DPRINTF((D_EXIST_PRIMARY_SIGN, fcn, sign));
6005eb2bd662Svikram 	}
6006eb2bd662Svikram 
6007eb2bd662Svikram 	(void) umount_top_dataset(pool, mnted, mntpt);
6008eb2bd662Svikram 
6009eb2bd662Svikram 	free(pool);
6010eb2bd662Svikram 
6011eb2bd662Svikram 	return (sign);
6012eb2bd662Svikram }
6013eb2bd662Svikram 
6014eb2bd662Svikram static char *
6015eb2bd662Svikram find_existing_sign(char *osroot, char *osdev, char *fstype)
6016eb2bd662Svikram {
6017eb2bd662Svikram 	const char		*fcn = "find_existing_sign()";
6018eb2bd662Svikram 
6019eb2bd662Svikram 	INJECT_ERROR1("FIND_EXIST_NOTSUP_FS", fstype = "foofs");
6020eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0) {
6021eb2bd662Svikram 		BAM_DPRINTF((D_CHECK_UFS_EXIST_SIGN, fcn));
6022eb2bd662Svikram 		return (find_ufs_existing(osroot));
6023eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0) {
6024eb2bd662Svikram 		BAM_DPRINTF((D_CHECK_ZFS_EXIST_SIGN, fcn));
6025eb2bd662Svikram 		return (find_zfs_existing(osdev));
6026eb2bd662Svikram 	} else {
6027eb2bd662Svikram 		bam_error(GRUBSIGN_NOTSUP, fstype);
6028eb2bd662Svikram 		return (NULL);
6029eb2bd662Svikram 	}
6030eb2bd662Svikram }
6031eb2bd662Svikram 
6032eb2bd662Svikram #define	MH_HASH_SZ	16
6033eb2bd662Svikram 
6034eb2bd662Svikram typedef enum {
6035eb2bd662Svikram 	MH_ERROR = -1,
6036eb2bd662Svikram 	MH_NOMATCH,
6037eb2bd662Svikram 	MH_MATCH
6038eb2bd662Svikram } mh_search_t;
6039eb2bd662Svikram 
6040eb2bd662Svikram typedef struct mcache {
6041eb2bd662Svikram 	char	*mc_special;
6042eb2bd662Svikram 	char	*mc_mntpt;
6043eb2bd662Svikram 	char	*mc_fstype;
6044eb2bd662Svikram 	struct mcache *mc_next;
6045eb2bd662Svikram } mcache_t;
6046eb2bd662Svikram 
6047eb2bd662Svikram typedef struct mhash {
6048eb2bd662Svikram 	mcache_t *mh_hash[MH_HASH_SZ];
6049eb2bd662Svikram } mhash_t;
6050eb2bd662Svikram 
6051eb2bd662Svikram static int
6052eb2bd662Svikram mhash_fcn(char *key)
6053eb2bd662Svikram {
6054eb2bd662Svikram 	int		i;
6055eb2bd662Svikram 	uint64_t	sum = 0;
6056eb2bd662Svikram 
6057eb2bd662Svikram 	for (i = 0; key[i] != '\0'; i++) {
6058eb2bd662Svikram 		sum += (uchar_t)key[i];
6059eb2bd662Svikram 	}
6060eb2bd662Svikram 
6061eb2bd662Svikram 	sum %= MH_HASH_SZ;
6062eb2bd662Svikram 
6063eb2bd662Svikram 	assert(sum < MH_HASH_SZ);
6064eb2bd662Svikram 
6065eb2bd662Svikram 	return (sum);
6066eb2bd662Svikram }
6067eb2bd662Svikram 
6068eb2bd662Svikram static mhash_t *
6069eb2bd662Svikram cache_mnttab(void)
6070eb2bd662Svikram {
6071eb2bd662Svikram 	FILE		*mfp;
6072eb2bd662Svikram 	struct extmnttab mnt;
6073eb2bd662Svikram 	mcache_t	*mcp;
6074eb2bd662Svikram 	mhash_t		*mhp;
6075eb2bd662Svikram 	char		*ctds;
6076eb2bd662Svikram 	int		idx;
6077eb2bd662Svikram 	int		error;
6078eb2bd662Svikram 	char		*special_dup;
6079eb2bd662Svikram 	const char	*fcn = "cache_mnttab()";
6080eb2bd662Svikram 
6081eb2bd662Svikram 	mfp = fopen(MNTTAB, "r");
6082eb2bd662Svikram 	error = errno;
6083eb2bd662Svikram 	INJECT_ERROR1("CACHE_MNTTAB_MNTTAB_ERR", mfp = NULL);
6084eb2bd662Svikram 	if (mfp == NULL) {
6085eb2bd662Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
6086eb2bd662Svikram 		return (NULL);
6087eb2bd662Svikram 	}
6088eb2bd662Svikram 
6089eb2bd662Svikram 	mhp = s_calloc(1, sizeof (mhash_t));
6090eb2bd662Svikram 
6091eb2bd662Svikram 	resetmnttab(mfp);
6092eb2bd662Svikram 
6093eb2bd662Svikram 	while (getextmntent(mfp, &mnt, sizeof (mnt)) == 0) {
6094eb2bd662Svikram 		/* only cache ufs */
6095eb2bd662Svikram 		if (strcmp(mnt.mnt_fstype, "ufs") != 0)
6096eb2bd662Svikram 			continue;
6097eb2bd662Svikram 
6098eb2bd662Svikram 		/* basename() modifies its arg, so dup it */
6099eb2bd662Svikram 		special_dup = s_strdup(mnt.mnt_special);
6100eb2bd662Svikram 		ctds = basename(special_dup);
6101eb2bd662Svikram 
6102eb2bd662Svikram 		mcp = s_calloc(1, sizeof (mcache_t));
6103eb2bd662Svikram 		mcp->mc_special = s_strdup(ctds);
6104eb2bd662Svikram 		mcp->mc_mntpt = s_strdup(mnt.mnt_mountp);
6105eb2bd662Svikram 		mcp->mc_fstype = s_strdup(mnt.mnt_fstype);
6106eb2bd662Svikram 		BAM_DPRINTF((D_CACHE_MNTS, fcn, ctds,
6107eb2bd662Svikram 		    mnt.mnt_mountp, mnt.mnt_fstype));
6108eb2bd662Svikram 		idx = mhash_fcn(ctds);
6109eb2bd662Svikram 		mcp->mc_next = mhp->mh_hash[idx];
6110eb2bd662Svikram 		mhp->mh_hash[idx] = mcp;
6111eb2bd662Svikram 		free(special_dup);
6112eb2bd662Svikram 	}
6113eb2bd662Svikram 
6114eb2bd662Svikram 	(void) fclose(mfp);
6115eb2bd662Svikram 
6116eb2bd662Svikram 	return (mhp);
6117eb2bd662Svikram }
6118eb2bd662Svikram 
6119eb2bd662Svikram static void
6120eb2bd662Svikram free_mnttab(mhash_t *mhp)
6121eb2bd662Svikram {
6122eb2bd662Svikram 	mcache_t	*mcp;
6123eb2bd662Svikram 	int		i;
6124eb2bd662Svikram 
6125eb2bd662Svikram 	for (i = 0; i < MH_HASH_SZ; i++) {
6126eb2bd662Svikram 		/*LINTED*/
6127eb2bd662Svikram 		while (mcp = mhp->mh_hash[i]) {
6128eb2bd662Svikram 			mhp->mh_hash[i] = mcp->mc_next;
6129eb2bd662Svikram 			free(mcp->mc_special);
6130eb2bd662Svikram 			free(mcp->mc_mntpt);
6131eb2bd662Svikram 			free(mcp->mc_fstype);
6132eb2bd662Svikram 			free(mcp);
6133eb2bd662Svikram 		}
6134eb2bd662Svikram 	}
6135eb2bd662Svikram 
6136eb2bd662Svikram 	for (i = 0; i < MH_HASH_SZ; i++) {
6137eb2bd662Svikram 		assert(mhp->mh_hash[i] == NULL);
6138eb2bd662Svikram 	}
6139eb2bd662Svikram 	free(mhp);
6140eb2bd662Svikram }
6141eb2bd662Svikram 
6142eb2bd662Svikram static mh_search_t
6143eb2bd662Svikram search_hash(mhash_t *mhp, char *special, char **mntpt)
6144eb2bd662Svikram {
6145eb2bd662Svikram 	int		idx;
6146eb2bd662Svikram 	mcache_t	*mcp;
6147eb2bd662Svikram 	const char 	*fcn = "search_hash()";
6148eb2bd662Svikram 
6149eb2bd662Svikram 	assert(mntpt);
6150eb2bd662Svikram 
6151eb2bd662Svikram 	*mntpt = NULL;
6152eb2bd662Svikram 
6153eb2bd662Svikram 	INJECT_ERROR1("SEARCH_HASH_FULL_PATH", special = "/foo");
6154eb2bd662Svikram 	if (strchr(special, '/')) {
6155eb2bd662Svikram 		bam_error(INVALID_MHASH_KEY, special);
6156eb2bd662Svikram 		return (MH_ERROR);
6157eb2bd662Svikram 	}
6158eb2bd662Svikram 
6159eb2bd662Svikram 	idx = mhash_fcn(special);
6160eb2bd662Svikram 
6161eb2bd662Svikram 	for (mcp = mhp->mh_hash[idx]; mcp; mcp = mcp->mc_next) {
6162eb2bd662Svikram 		if (strcmp(mcp->mc_special, special) == 0)
6163eb2bd662Svikram 			break;
6164eb2bd662Svikram 	}
6165eb2bd662Svikram 
6166eb2bd662Svikram 	if (mcp == NULL) {
6167eb2bd662Svikram 		BAM_DPRINTF((D_MNTTAB_HASH_NOMATCH, fcn, special));
6168eb2bd662Svikram 		return (MH_NOMATCH);
6169eb2bd662Svikram 	}
6170eb2bd662Svikram 
6171eb2bd662Svikram 	assert(strcmp(mcp->mc_fstype, "ufs") == 0);
6172eb2bd662Svikram 	*mntpt = mcp->mc_mntpt;
6173eb2bd662Svikram 	BAM_DPRINTF((D_MNTTAB_HASH_MATCH, fcn, special));
6174eb2bd662Svikram 	return (MH_MATCH);
6175eb2bd662Svikram }
6176eb2bd662Svikram 
6177eb2bd662Svikram static int
6178eb2bd662Svikram check_add_ufs_sign_to_list(FILE *tfp, char *mntpt)
6179eb2bd662Svikram {
6180eb2bd662Svikram 	char		*sign;
6181eb2bd662Svikram 	char		*signline;
6182eb2bd662Svikram 	char		signbuf[MAXNAMELEN];
6183eb2bd662Svikram 	int		len;
6184eb2bd662Svikram 	int		error;
6185eb2bd662Svikram 	const char	*fcn = "check_add_ufs_sign_to_list()";
6186eb2bd662Svikram 
6187eb2bd662Svikram 	/* safe to specify NULL as "osdev" arg for UFS */
6188eb2bd662Svikram 	sign = find_existing_sign(mntpt, NULL, "ufs");
6189eb2bd662Svikram 	if (sign == NULL) {
6190eb2bd662Svikram 		/* No existing signature, nothing to add to list */
6191eb2bd662Svikram 		BAM_DPRINTF((D_NO_SIGN_TO_LIST, fcn, mntpt));
6192eb2bd662Svikram 		return (0);
6193eb2bd662Svikram 	}
6194eb2bd662Svikram 
6195eb2bd662Svikram 	(void) snprintf(signbuf, sizeof (signbuf), "%s\n", sign);
6196eb2bd662Svikram 	signline = signbuf;
6197eb2bd662Svikram 
6198eb2bd662Svikram 	INJECT_ERROR1("UFS_MNTPT_SIGN_NOTUFS", signline = "pool_rpool10\n");
6199eb2bd662Svikram 	if (strncmp(signline, GRUBSIGN_UFS_PREFIX,
6200eb2bd662Svikram 	    strlen(GRUBSIGN_UFS_PREFIX))) {
6201eb2bd662Svikram 		bam_error(INVALID_UFS_SIGNATURE, sign);
6202eb2bd662Svikram 		free(sign);
6203eb2bd662Svikram 		/* ignore invalid signatures */
6204eb2bd662Svikram 		return (0);
6205eb2bd662Svikram 	}
6206eb2bd662Svikram 
6207eb2bd662Svikram 	len = fputs(signline, tfp);
6208eb2bd662Svikram 	error = errno;
6209eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_PUTS_ERROR", len = 0);
6210eb2bd662Svikram 	if (len != strlen(signline)) {
6211eb2bd662Svikram 		bam_error(SIGN_LIST_FPUTS_ERR, sign, strerror(error));
6212eb2bd662Svikram 		free(sign);
6213eb2bd662Svikram 		return (-1);
6214eb2bd662Svikram 	}
6215eb2bd662Svikram 
6216eb2bd662Svikram 	free(sign);
6217eb2bd662Svikram 
6218eb2bd662Svikram 	BAM_DPRINTF((D_SIGN_LIST_PUTS_DONE, fcn, mntpt));
6219eb2bd662Svikram 	return (0);
6220eb2bd662Svikram }
6221eb2bd662Svikram 
6222eb2bd662Svikram /*
6223eb2bd662Svikram  * slice is a basename not a full pathname
6224eb2bd662Svikram  */
6225eb2bd662Svikram static int
6226eb2bd662Svikram process_slice_common(char *slice, FILE *tfp, mhash_t *mhp, char *tmpmnt)
6227eb2bd662Svikram {
6228eb2bd662Svikram 	int		ret;
6229eb2bd662Svikram 	char		cmd[PATH_MAX];
6230eb2bd662Svikram 	char		path[PATH_MAX];
6231eb2bd662Svikram 	struct stat	sbuf;
6232eb2bd662Svikram 	char		*mntpt;
6233eb2bd662Svikram 	filelist_t	flist = {0};
6234eb2bd662Svikram 	char		*fstype;
6235eb2bd662Svikram 	char		blkslice[PATH_MAX];
6236eb2bd662Svikram 	const char	*fcn = "process_slice_common()";
6237eb2bd662Svikram 
6238eb2bd662Svikram 
6239eb2bd662Svikram 	ret = search_hash(mhp, slice, &mntpt);
6240eb2bd662Svikram 	switch (ret) {
6241eb2bd662Svikram 		case MH_MATCH:
6242eb2bd662Svikram 			if (check_add_ufs_sign_to_list(tfp, mntpt) == -1)
6243eb2bd662Svikram 				return (-1);
6244eb2bd662Svikram 			else
6245eb2bd662Svikram 				return (0);
6246eb2bd662Svikram 		case MH_NOMATCH:
6247eb2bd662Svikram 			break;
6248eb2bd662Svikram 		case MH_ERROR:
6249eb2bd662Svikram 		default:
6250eb2bd662Svikram 			return (-1);
6251eb2bd662Svikram 	}
6252eb2bd662Svikram 
6253eb2bd662Svikram 	(void) snprintf(path, sizeof (path), "/dev/rdsk/%s", slice);
6254eb2bd662Svikram 	if (stat(path, &sbuf) == -1) {
6255eb2bd662Svikram 		BAM_DPRINTF((D_SLICE_ENOENT, fcn, path));
6256eb2bd662Svikram 		return (0);
6257eb2bd662Svikram 	}
6258eb2bd662Svikram 
6259d5cb36d3SJan Setje-Eilers 	/* Check if ufs. Call ufs fstyp directly to avoid pcfs conflicts. */
6260eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6261d5cb36d3SJan Setje-Eilers 	    "/usr/lib/fs/ufs/fstyp /dev/rdsk/%s 2>/dev/null",
6262eb2bd662Svikram 	    slice);
6263eb2bd662Svikram 
6264eb2bd662Svikram 	if (exec_cmd(cmd, &flist) != 0) {
6265eb2bd662Svikram 		if (bam_verbose)
6266eb2bd662Svikram 			bam_print(FSTYP_FAILED, slice);
6267eb2bd662Svikram 		return (0);
6268eb2bd662Svikram 	}
6269eb2bd662Svikram 
6270eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
6271eb2bd662Svikram 		if (bam_verbose)
6272eb2bd662Svikram 			bam_print(FSTYP_BAD, slice);
6273eb2bd662Svikram 		filelist_free(&flist);
6274eb2bd662Svikram 		return (0);
6275eb2bd662Svikram 	}
6276eb2bd662Svikram 
6277eb2bd662Svikram 	fstype = strtok(flist.head->line, " \t\n");
6278eb2bd662Svikram 	if (fstype == NULL || strcmp(fstype, "ufs") != 0) {
6279eb2bd662Svikram 		if (bam_verbose)
6280eb2bd662Svikram 			bam_print(NOT_UFS_SLICE, slice, fstype);
6281eb2bd662Svikram 		filelist_free(&flist);
6282eb2bd662Svikram 		return (0);
6283eb2bd662Svikram 	}
6284eb2bd662Svikram 
6285eb2bd662Svikram 	filelist_free(&flist);
6286eb2bd662Svikram 
6287eb2bd662Svikram 	/*
6288eb2bd662Svikram 	 * Since we are mounting the filesystem read-only, the
6289eb2bd662Svikram 	 * the last mount field of the superblock is unchanged
6290eb2bd662Svikram 	 * and does not need to be fixed up post-mount;
6291eb2bd662Svikram 	 */
6292eb2bd662Svikram 
6293eb2bd662Svikram 	(void) snprintf(blkslice, sizeof (blkslice), "/dev/dsk/%s",
6294eb2bd662Svikram 	    slice);
6295eb2bd662Svikram 
6296eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6297eb2bd662Svikram 	    "/usr/sbin/mount -F ufs -o ro %s %s "
6298eb2bd662Svikram 	    "> /dev/null 2>&1", blkslice, tmpmnt);
6299eb2bd662Svikram 
6300eb2bd662Svikram 	if (exec_cmd(cmd, NULL) != 0) {
6301eb2bd662Svikram 		if (bam_verbose)
6302eb2bd662Svikram 			bam_print(MOUNT_FAILED, blkslice, "ufs");
6303eb2bd662Svikram 		return (0);
6304eb2bd662Svikram 	}
6305eb2bd662Svikram 
6306eb2bd662Svikram 	ret = check_add_ufs_sign_to_list(tfp, tmpmnt);
6307eb2bd662Svikram 
6308eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6309eb2bd662Svikram 	    "/usr/sbin/umount -f %s > /dev/null 2>&1",
6310eb2bd662Svikram 	    tmpmnt);
6311eb2bd662Svikram 
6312eb2bd662Svikram 	if (exec_cmd(cmd, NULL) != 0) {
6313eb2bd662Svikram 		bam_print(UMOUNT_FAILED, slice);
6314eb2bd662Svikram 		return (0);
6315eb2bd662Svikram 	}
6316eb2bd662Svikram 
6317eb2bd662Svikram 	return (ret);
6318eb2bd662Svikram }
6319eb2bd662Svikram 
6320eb2bd662Svikram static int
6321eb2bd662Svikram process_vtoc_slices(
6322eb2bd662Svikram 	char *s0,
6323eb2bd662Svikram 	struct vtoc *vtoc,
6324eb2bd662Svikram 	FILE *tfp,
6325eb2bd662Svikram 	mhash_t *mhp,
6326eb2bd662Svikram 	char *tmpmnt)
6327eb2bd662Svikram {
6328eb2bd662Svikram 	int		idx;
6329eb2bd662Svikram 	char		slice[PATH_MAX];
6330eb2bd662Svikram 	size_t		len;
6331eb2bd662Svikram 	char		*cp;
6332eb2bd662Svikram 	const char	*fcn = "process_vtoc_slices()";
6333eb2bd662Svikram 
6334eb2bd662Svikram 	len = strlen(s0);
6335eb2bd662Svikram 
6336eb2bd662Svikram 	assert(s0[len - 2] == 's' && s0[len - 1] == '0');
6337eb2bd662Svikram 
6338eb2bd662Svikram 	s0[len - 1] = '\0';
6339eb2bd662Svikram 
6340eb2bd662Svikram 	(void) strlcpy(slice, s0, sizeof (slice));
6341eb2bd662Svikram 
6342eb2bd662Svikram 	s0[len - 1] = '0';
6343eb2bd662Svikram 
6344eb2bd662Svikram 	cp = slice + len - 1;
6345eb2bd662Svikram 
6346eb2bd662Svikram 	for (idx = 0; idx < vtoc->v_nparts; idx++) {
6347eb2bd662Svikram 
6348eb2bd662Svikram 		(void) snprintf(cp, sizeof (slice) - (len - 1), "%u", idx);
6349eb2bd662Svikram 
6350eb2bd662Svikram 		if (vtoc->v_part[idx].p_size == 0) {
6351eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_SIZE_ZERO, fcn, slice));
6352eb2bd662Svikram 			continue;
6353eb2bd662Svikram 		}
6354eb2bd662Svikram 
6355eb2bd662Svikram 		/* Skip "SWAP", "USR", "BACKUP", "VAR", "HOME", "ALTSCTR" */
6356eb2bd662Svikram 		switch (vtoc->v_part[idx].p_tag) {
6357eb2bd662Svikram 		case V_SWAP:
6358eb2bd662Svikram 		case V_USR:
6359eb2bd662Svikram 		case V_BACKUP:
6360eb2bd662Svikram 		case V_VAR:
6361eb2bd662Svikram 		case V_HOME:
6362eb2bd662Svikram 		case V_ALTSCTR:
6363eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_NOT_ROOT_TAG, fcn, slice));
6364eb2bd662Svikram 			continue;
6365eb2bd662Svikram 		default:
6366eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_ROOT_TAG, fcn, slice));
6367eb2bd662Svikram 			break;
6368eb2bd662Svikram 		}
6369eb2bd662Svikram 
6370eb2bd662Svikram 		/* skip unmountable and readonly slices */
6371eb2bd662Svikram 		switch (vtoc->v_part[idx].p_flag) {
6372eb2bd662Svikram 		case V_UNMNT:
6373eb2bd662Svikram 		case V_RONLY:
6374eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_NOT_RDWR_FLAG, fcn, slice));
6375eb2bd662Svikram 			continue;
6376eb2bd662Svikram 		default:
6377eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_RDWR_FLAG, fcn, slice));
6378eb2bd662Svikram 			break;
6379eb2bd662Svikram 		}
6380eb2bd662Svikram 
6381eb2bd662Svikram 		if (process_slice_common(slice, tfp, mhp, tmpmnt) == -1) {
6382eb2bd662Svikram 			return (-1);
6383eb2bd662Svikram 		}
6384eb2bd662Svikram 	}
6385eb2bd662Svikram 
6386eb2bd662Svikram 	return (0);
6387eb2bd662Svikram }
6388eb2bd662Svikram 
6389eb2bd662Svikram static int
6390eb2bd662Svikram process_efi_slices(
6391eb2bd662Svikram 	char *s0,
6392eb2bd662Svikram 	struct dk_gpt *efi,
6393eb2bd662Svikram 	FILE *tfp,
6394eb2bd662Svikram 	mhash_t *mhp,
6395eb2bd662Svikram 	char *tmpmnt)
6396eb2bd662Svikram {
6397eb2bd662Svikram 	int		idx;
6398eb2bd662Svikram 	char		slice[PATH_MAX];
6399eb2bd662Svikram 	size_t		len;
6400eb2bd662Svikram 	char		*cp;
6401eb2bd662Svikram 	const char	*fcn = "process_efi_slices()";
6402eb2bd662Svikram 
6403eb2bd662Svikram 	len = strlen(s0);
6404eb2bd662Svikram 
6405eb2bd662Svikram 	assert(s0[len - 2] == 's' && s0[len - 1] == '0');
6406eb2bd662Svikram 
6407eb2bd662Svikram 	s0[len - 1] = '\0';
6408eb2bd662Svikram 
6409eb2bd662Svikram 	(void) strlcpy(slice, s0, sizeof (slice));
6410eb2bd662Svikram 
6411eb2bd662Svikram 	s0[len - 1] = '0';
6412eb2bd662Svikram 
6413eb2bd662Svikram 	cp = slice + len - 1;
6414eb2bd662Svikram 
6415eb2bd662Svikram 	for (idx = 0; idx < efi->efi_nparts; idx++) {
6416eb2bd662Svikram 
6417eb2bd662Svikram 		(void) snprintf(cp, sizeof (slice) - (len - 1), "%u", idx);
6418eb2bd662Svikram 
6419eb2bd662Svikram 		if (efi->efi_parts[idx].p_size == 0) {
6420eb2bd662Svikram 			BAM_DPRINTF((D_EFI_SIZE_ZERO, fcn, slice));
6421eb2bd662Svikram 			continue;
6422eb2bd662Svikram 		}
6423eb2bd662Svikram 
6424eb2bd662Svikram 		/* Skip "SWAP", "USR", "BACKUP", "VAR", "HOME", "ALTSCTR" */
6425eb2bd662Svikram 		switch (efi->efi_parts[idx].p_tag) {
6426eb2bd662Svikram 		case V_SWAP:
6427eb2bd662Svikram 		case V_USR:
6428eb2bd662Svikram 		case V_BACKUP:
6429eb2bd662Svikram 		case V_VAR:
6430eb2bd662Svikram 		case V_HOME:
6431eb2bd662Svikram 		case V_ALTSCTR:
6432eb2bd662Svikram 			BAM_DPRINTF((D_EFI_NOT_ROOT_TAG, fcn, slice));
6433eb2bd662Svikram 			continue;
6434eb2bd662Svikram 		default:
6435eb2bd662Svikram 			BAM_DPRINTF((D_EFI_ROOT_TAG, fcn, slice));
6436eb2bd662Svikram 			break;
6437eb2bd662Svikram 		}
6438eb2bd662Svikram 
6439eb2bd662Svikram 		/* skip unmountable and readonly slices */
6440eb2bd662Svikram 		switch (efi->efi_parts[idx].p_flag) {
6441eb2bd662Svikram 		case V_UNMNT:
6442eb2bd662Svikram 		case V_RONLY:
6443eb2bd662Svikram 			BAM_DPRINTF((D_EFI_NOT_RDWR_FLAG, fcn, slice));
6444eb2bd662Svikram 			continue;
6445eb2bd662Svikram 		default:
6446eb2bd662Svikram 			BAM_DPRINTF((D_EFI_RDWR_FLAG, fcn, slice));
6447eb2bd662Svikram 			break;
6448eb2bd662Svikram 		}
6449eb2bd662Svikram 
6450eb2bd662Svikram 		if (process_slice_common(slice, tfp, mhp, tmpmnt) == -1) {
6451eb2bd662Svikram 			return (-1);
6452eb2bd662Svikram 		}
6453eb2bd662Svikram 	}
6454eb2bd662Svikram 
6455eb2bd662Svikram 	return (0);
6456eb2bd662Svikram }
6457eb2bd662Svikram 
6458eb2bd662Svikram /*
6459eb2bd662Svikram  * s0 is a basename not a full path
6460eb2bd662Svikram  */
6461eb2bd662Svikram static int
6462eb2bd662Svikram process_slice0(char *s0, FILE *tfp, mhash_t *mhp, char *tmpmnt)
6463eb2bd662Svikram {
6464eb2bd662Svikram 	struct vtoc		vtoc;
6465eb2bd662Svikram 	struct dk_gpt		*efi;
6466eb2bd662Svikram 	char			s0path[PATH_MAX];
6467eb2bd662Svikram 	struct stat		sbuf;
6468eb2bd662Svikram 	int			e_flag;
6469eb2bd662Svikram 	int			v_flag;
6470eb2bd662Svikram 	int			retval;
6471eb2bd662Svikram 	int			err;
6472eb2bd662Svikram 	int			fd;
6473eb2bd662Svikram 	const char		*fcn = "process_slice0()";
6474eb2bd662Svikram 
6475eb2bd662Svikram 	(void) snprintf(s0path, sizeof (s0path), "/dev/rdsk/%s", s0);
6476eb2bd662Svikram 
6477eb2bd662Svikram 	if (stat(s0path, &sbuf) == -1) {
6478eb2bd662Svikram 		BAM_DPRINTF((D_SLICE0_ENOENT, fcn, s0path));
6479eb2bd662Svikram 		return (0);
6480eb2bd662Svikram 	}
6481eb2bd662Svikram 
6482eb2bd662Svikram 	fd = open(s0path, O_NONBLOCK|O_RDONLY);
6483eb2bd662Svikram 	if (fd == -1) {
6484eb2bd662Svikram 		bam_error(OPEN_FAIL, s0path, strerror(errno));
6485eb2bd662Svikram 		return (0);
6486eb2bd662Svikram 	}
6487eb2bd662Svikram 
6488eb2bd662Svikram 	e_flag = v_flag = 0;
6489eb2bd662Svikram 	retval = ((err = read_vtoc(fd, &vtoc)) >= 0) ? 0 : err;
6490eb2bd662Svikram 	switch (retval) {
6491eb2bd662Svikram 		case VT_EIO:
6492eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_READ_FAIL, fcn, s0path));
6493eb2bd662Svikram 			break;
6494eb2bd662Svikram 		case VT_EINVAL:
6495eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_INVALID, fcn, s0path));
6496eb2bd662Svikram 			break;
6497eb2bd662Svikram 		case VT_ERROR:
6498eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_UNKNOWN_ERR, fcn, s0path));
6499eb2bd662Svikram 			break;
6500eb2bd662Svikram 		case VT_ENOTSUP:
6501eb2bd662Svikram 			e_flag = 1;
6502eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_NOTSUP, fcn, s0path));
6503eb2bd662Svikram 			break;
6504eb2bd662Svikram 		case 0:
6505eb2bd662Svikram 			v_flag = 1;
6506eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_READ_SUCCESS, fcn, s0path));
6507eb2bd662Svikram 			break;
6508eb2bd662Svikram 		default:
6509eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_UNKNOWN_RETCODE, fcn, s0path));
6510eb2bd662Svikram 			break;
6511eb2bd662Svikram 	}
6512eb2bd662Svikram 
6513eb2bd662Svikram 
6514eb2bd662Svikram 	if (e_flag) {
6515eb2bd662Svikram 		e_flag = 0;
6516eb2bd662Svikram 		retval = ((err = efi_alloc_and_read(fd, &efi)) >= 0) ? 0 : err;
6517eb2bd662Svikram 		switch (retval) {
6518eb2bd662Svikram 		case VT_EIO:
6519eb2bd662Svikram 			BAM_DPRINTF((D_EFI_READ_FAIL, fcn, s0path));
6520eb2bd662Svikram 			break;
6521eb2bd662Svikram 		case VT_EINVAL:
6522eb2bd662Svikram 			BAM_DPRINTF((D_EFI_INVALID, fcn, s0path));
6523eb2bd662Svikram 			break;
6524eb2bd662Svikram 		case VT_ERROR:
6525eb2bd662Svikram 			BAM_DPRINTF((D_EFI_UNKNOWN_ERR, fcn, s0path));
6526eb2bd662Svikram 			break;
6527eb2bd662Svikram 		case VT_ENOTSUP:
6528eb2bd662Svikram 			BAM_DPRINTF((D_EFI_NOTSUP, fcn, s0path));
6529eb2bd662Svikram 			break;
6530eb2bd662Svikram 		case 0:
6531eb2bd662Svikram 			e_flag = 1;
6532eb2bd662Svikram 			BAM_DPRINTF((D_EFI_READ_SUCCESS, fcn, s0path));
6533eb2bd662Svikram 			break;
6534eb2bd662Svikram 		default:
6535eb2bd662Svikram 			BAM_DPRINTF((D_EFI_UNKNOWN_RETCODE, fcn, s0path));
6536eb2bd662Svikram 			break;
6537eb2bd662Svikram 		}
6538eb2bd662Svikram 	}
6539eb2bd662Svikram 
6540eb2bd662Svikram 	(void) close(fd);
6541eb2bd662Svikram 
6542eb2bd662Svikram 	if (v_flag) {
6543eb2bd662Svikram 		retval = process_vtoc_slices(s0,
6544eb2bd662Svikram 		    &vtoc, tfp, mhp, tmpmnt);
6545eb2bd662Svikram 	} else if (e_flag) {
6546eb2bd662Svikram 		retval = process_efi_slices(s0,
6547eb2bd662Svikram 		    efi, tfp, mhp, tmpmnt);
6548eb2bd662Svikram 	} else {
6549eb2bd662Svikram 		BAM_DPRINTF((D_NOT_VTOC_OR_EFI, fcn, s0path));
6550eb2bd662Svikram 		return (0);
6551eb2bd662Svikram 	}
6552eb2bd662Svikram 
6553eb2bd662Svikram 	return (retval);
6554eb2bd662Svikram }
6555eb2bd662Svikram 
6556eb2bd662Svikram /*
6557eb2bd662Svikram  * Find and create a list of all existing UFS boot signatures
6558eb2bd662Svikram  */
6559eb2bd662Svikram static int
6560eb2bd662Svikram FindAllUfsSignatures(void)
6561eb2bd662Svikram {
6562eb2bd662Svikram 	mhash_t		*mnttab_hash;
6563eb2bd662Svikram 	DIR		*dirp = NULL;
6564eb2bd662Svikram 	struct dirent	*dp;
6565eb2bd662Svikram 	char		tmpmnt[PATH_MAX];
6566eb2bd662Svikram 	char		cmd[PATH_MAX];
6567eb2bd662Svikram 	struct stat	sb;
6568eb2bd662Svikram 	int		fd;
6569eb2bd662Svikram 	FILE		*tfp;
6570eb2bd662Svikram 	size_t		len;
6571eb2bd662Svikram 	int		ret;
6572eb2bd662Svikram 	int		error;
6573eb2bd662Svikram 	const char	*fcn = "FindAllUfsSignatures()";
6574eb2bd662Svikram 
6575eb2bd662Svikram 	if (stat(UFS_SIGNATURE_LIST, &sb) != -1)  {
6576eb2bd662Svikram 		bam_print(SIGNATURE_LIST_EXISTS, UFS_SIGNATURE_LIST);
6577eb2bd662Svikram 		return (0);
6578eb2bd662Svikram 	}
6579eb2bd662Svikram 
6580eb2bd662Svikram 	fd = open(UFS_SIGNATURE_LIST".tmp",
6581eb2bd662Svikram 	    O_RDWR|O_CREAT|O_TRUNC, 0644);
6582eb2bd662Svikram 	error = errno;
6583eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_TMP_TRUNC", fd = -1);
6584eb2bd662Svikram 	if (fd == -1) {
6585eb2bd662Svikram 		bam_error(OPEN_FAIL, UFS_SIGNATURE_LIST".tmp", strerror(error));
6586eb2bd662Svikram 		return (-1);
6587eb2bd662Svikram 	}
6588eb2bd662Svikram 
6589eb2bd662Svikram 	ret = close(fd);
6590eb2bd662Svikram 	error = errno;
6591eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_TMP_CLOSE", ret = -1);
6592eb2bd662Svikram 	if (ret == -1) {
6593eb2bd662Svikram 		bam_error(CLOSE_FAIL, UFS_SIGNATURE_LIST".tmp",
6594eb2bd662Svikram 		    strerror(error));
6595eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6596eb2bd662Svikram 		return (-1);
6597eb2bd662Svikram 	}
6598eb2bd662Svikram 
6599eb2bd662Svikram 	tfp = fopen(UFS_SIGNATURE_LIST".tmp", "a");
6600eb2bd662Svikram 	error = errno;
6601eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_APPEND_FOPEN", tfp = NULL);
6602eb2bd662Svikram 	if (tfp == NULL) {
6603eb2bd662Svikram 		bam_error(OPEN_FAIL, UFS_SIGNATURE_LIST".tmp", strerror(error));
6604eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6605eb2bd662Svikram 		return (-1);
6606eb2bd662Svikram 	}
6607eb2bd662Svikram 
6608eb2bd662Svikram 	mnttab_hash = cache_mnttab();
6609eb2bd662Svikram 	INJECT_ERROR1("CACHE_MNTTAB_ERROR", mnttab_hash = NULL);
6610eb2bd662Svikram 	if (mnttab_hash == NULL) {
6611eb2bd662Svikram 		(void) fclose(tfp);
6612eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6613eb2bd662Svikram 		bam_error(CACHE_MNTTAB_FAIL, fcn);
6614eb2bd662Svikram 		return (-1);
6615eb2bd662Svikram 	}
6616eb2bd662Svikram 
6617eb2bd662Svikram 	(void) snprintf(tmpmnt, sizeof (tmpmnt),
6618eb2bd662Svikram 	    "/tmp/bootadm_ufs_sign_mnt.%d", getpid());
6619eb2bd662Svikram 	(void) unlink(tmpmnt);
6620eb2bd662Svikram 
662148847494SEnrico Perla - Sun Microsystems 	ret = mkdirp(tmpmnt, DIR_PERMS);
6622eb2bd662Svikram 	error = errno;
6623eb2bd662Svikram 	INJECT_ERROR1("MKDIRP_SIGN_MNT", ret = -1);
6624eb2bd662Svikram 	if (ret == -1) {
6625eb2bd662Svikram 		bam_error(MKDIR_FAILED, tmpmnt, strerror(error));
6626eb2bd662Svikram 		free_mnttab(mnttab_hash);
6627eb2bd662Svikram 		(void) fclose(tfp);
6628eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6629eb2bd662Svikram 		return (-1);
6630eb2bd662Svikram 	}
6631eb2bd662Svikram 
6632eb2bd662Svikram 	dirp = opendir("/dev/rdsk");
6633eb2bd662Svikram 	error = errno;
6634eb2bd662Svikram 	INJECT_ERROR1("OPENDIR_DEV_RDSK", dirp = NULL);
6635eb2bd662Svikram 	if (dirp == NULL) {
6636eb2bd662Svikram 		bam_error(OPENDIR_FAILED, "/dev/rdsk", strerror(error));
6637eb2bd662Svikram 		goto fail;
6638eb2bd662Svikram 	}
6639eb2bd662Svikram 
6640eb2bd662Svikram 	while (dp = readdir(dirp)) {
6641eb2bd662Svikram 		if (strcmp(dp->d_name, ".") == 0 ||
6642eb2bd662Svikram 		    strcmp(dp->d_name, "..") == 0)
6643eb2bd662Svikram 			continue;
6644eb2bd662Svikram 
6645eb2bd662Svikram 		/*
6646eb2bd662Svikram 		 * we only look for the s0 slice. This is guranteed to
6647eb2bd662Svikram 		 * have 's' at len - 2.
6648eb2bd662Svikram 		 */
6649eb2bd662Svikram 		len = strlen(dp->d_name);
6650eb2bd662Svikram 		if (dp->d_name[len - 2 ] != 's' || dp->d_name[len - 1] != '0') {
6651eb2bd662Svikram 			BAM_DPRINTF((D_SKIP_SLICE_NOTZERO, fcn, dp->d_name));
6652eb2bd662Svikram 			continue;
6653eb2bd662Svikram 		}
6654eb2bd662Svikram 
6655eb2bd662Svikram 		ret = process_slice0(dp->d_name, tfp, mnttab_hash, tmpmnt);
6656eb2bd662Svikram 		INJECT_ERROR1("PROCESS_S0_FAIL", ret = -1);
6657eb2bd662Svikram 		if (ret == -1)
6658eb2bd662Svikram 			goto fail;
6659eb2bd662Svikram 	}
6660eb2bd662Svikram 
6661eb2bd662Svikram 	(void) closedir(dirp);
6662eb2bd662Svikram 	free_mnttab(mnttab_hash);
6663eb2bd662Svikram 	(void) rmdir(tmpmnt);
6664eb2bd662Svikram 
6665eb2bd662Svikram 	ret = fclose(tfp);
6666eb2bd662Svikram 	error = errno;
6667eb2bd662Svikram 	INJECT_ERROR1("FCLOSE_SIGNLIST_TMP", ret = EOF);
6668eb2bd662Svikram 	if (ret == EOF) {
6669eb2bd662Svikram 		bam_error(CLOSE_FAIL, UFS_SIGNATURE_LIST".tmp",
6670eb2bd662Svikram 		    strerror(error));
6671eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6672eb2bd662Svikram 		return (-1);
6673eb2bd662Svikram 	}
6674eb2bd662Svikram 
6675eb2bd662Svikram 	/* We have a list of existing GRUB signatures. Sort it first */
6676eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6677eb2bd662Svikram 	    "/usr/bin/sort -u %s.tmp > %s.sorted",
6678eb2bd662Svikram 	    UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST);
6679eb2bd662Svikram 
6680eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
6681eb2bd662Svikram 	INJECT_ERROR1("SORT_SIGN_LIST", ret = 1);
6682eb2bd662Svikram 	if (ret != 0) {
6683eb2bd662Svikram 		bam_error(GRUBSIGN_SORT_FAILED);
6684eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
6685eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6686eb2bd662Svikram 		return (-1);
6687eb2bd662Svikram 	}
6688eb2bd662Svikram 
6689eb2bd662Svikram 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
6690eb2bd662Svikram 
6691eb2bd662Svikram 	ret = rename(UFS_SIGNATURE_LIST".sorted", UFS_SIGNATURE_LIST);
6692eb2bd662Svikram 	error = errno;
6693eb2bd662Svikram 	INJECT_ERROR1("RENAME_TMP_SIGNLIST", ret = -1);
6694eb2bd662Svikram 	if (ret == -1) {
6695eb2bd662Svikram 		bam_error(RENAME_FAIL, UFS_SIGNATURE_LIST, strerror(error));
6696eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
6697eb2bd662Svikram 		return (-1);
6698eb2bd662Svikram 	}
6699eb2bd662Svikram 
6700eb2bd662Svikram 	if (stat(UFS_SIGNATURE_LIST, &sb) == 0 && sb.st_size == 0) {
6701eb2bd662Svikram 		BAM_DPRINTF((D_ZERO_LEN_SIGNLIST, fcn, UFS_SIGNATURE_LIST));
6702eb2bd662Svikram 	}
6703eb2bd662Svikram 
6704eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6705eb2bd662Svikram 	return (0);
6706eb2bd662Svikram 
6707eb2bd662Svikram fail:
6708eb2bd662Svikram 	if (dirp)
6709eb2bd662Svikram 		(void) closedir(dirp);
6710eb2bd662Svikram 	free_mnttab(mnttab_hash);
6711eb2bd662Svikram 	(void) rmdir(tmpmnt);
6712eb2bd662Svikram 	(void) fclose(tfp);
6713eb2bd662Svikram 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
6714eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6715eb2bd662Svikram 	return (-1);
6716eb2bd662Svikram }
6717eb2bd662Svikram 
6718eb2bd662Svikram static char *
6719eb2bd662Svikram create_ufs_sign(void)
6720eb2bd662Svikram {
6721eb2bd662Svikram 	struct stat	sb;
6722eb2bd662Svikram 	int		signnum = -1;
6723eb2bd662Svikram 	char		tmpsign[MAXNAMELEN + 1];
6724eb2bd662Svikram 	char		*numstr;
6725eb2bd662Svikram 	int		i;
6726eb2bd662Svikram 	FILE		*tfp;
6727eb2bd662Svikram 	int		ret;
6728eb2bd662Svikram 	int		error;
6729eb2bd662Svikram 	const char	*fcn = "create_ufs_sign()";
6730eb2bd662Svikram 
6731eb2bd662Svikram 	bam_print(SEARCHING_UFS_SIGN);
6732eb2bd662Svikram 
6733eb2bd662Svikram 	ret = FindAllUfsSignatures();
6734eb2bd662Svikram 	INJECT_ERROR1("FIND_ALL_UFS", ret = -1);
6735eb2bd662Svikram 	if (ret == -1) {
6736eb2bd662Svikram 		bam_error(ERR_FIND_UFS_SIGN);
6737eb2bd662Svikram 		return (NULL);
6738eb2bd662Svikram 	}
6739eb2bd662Svikram 
6740eb2bd662Svikram 	/* Make sure the list exists and is owned by root */
6741eb2bd662Svikram 	INJECT_ERROR1("SIGNLIST_NOT_CREATED",
6742eb2bd662Svikram 	    (void) unlink(UFS_SIGNATURE_LIST));
6743eb2bd662Svikram 	if (stat(UFS_SIGNATURE_LIST, &sb) == -1 || sb.st_uid != 0) {
6744eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6745eb2bd662Svikram 		bam_error(UFS_SIGNATURE_LIST_MISS, UFS_SIGNATURE_LIST);
6746eb2bd662Svikram 		return (NULL);
6747eb2bd662Svikram 	}
6748eb2bd662Svikram 
6749eb2bd662Svikram 	if (sb.st_size == 0) {
6750eb2bd662Svikram 		bam_print(GRUBSIGN_UFS_NONE);
6751eb2bd662Svikram 		i = 0;
6752eb2bd662Svikram 		goto found;
6753eb2bd662Svikram 	}
6754eb2bd662Svikram 
6755eb2bd662Svikram 	/* The signature list was sorted when it was created */
6756eb2bd662Svikram 	tfp = fopen(UFS_SIGNATURE_LIST, "r");
6757eb2bd662Svikram 	error = errno;
6758eb2bd662Svikram 	INJECT_ERROR1("FOPEN_SIGN_LIST", tfp = NULL);
6759eb2bd662Svikram 	if (tfp == NULL) {
6760eb2bd662Svikram 		bam_error(UFS_SIGNATURE_LIST_OPENERR,
6761eb2bd662Svikram 		    UFS_SIGNATURE_LIST, strerror(error));
6762eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6763eb2bd662Svikram 		return (NULL);
6764eb2bd662Svikram 	}
6765eb2bd662Svikram 
6766eb2bd662Svikram 	for (i = 0; s_fgets(tmpsign, sizeof (tmpsign), tfp); i++) {
6767eb2bd662Svikram 
6768eb2bd662Svikram 		if (strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
6769eb2bd662Svikram 		    strlen(GRUBSIGN_UFS_PREFIX)) != 0) {
6770eb2bd662Svikram 			(void) fclose(tfp);
6771eb2bd662Svikram 			(void) unlink(UFS_SIGNATURE_LIST);
6772eb2bd662Svikram 			bam_error(UFS_BADSIGN, tmpsign);
6773eb2bd662Svikram 			return (NULL);
6774eb2bd662Svikram 		}
6775eb2bd662Svikram 		numstr = tmpsign + strlen(GRUBSIGN_UFS_PREFIX);
6776eb2bd662Svikram 
6777eb2bd662Svikram 		if (numstr[0] == '\0' || !isdigit(numstr[0])) {
6778eb2bd662Svikram 			(void) fclose(tfp);
6779eb2bd662Svikram 			(void) unlink(UFS_SIGNATURE_LIST);
6780eb2bd662Svikram 			bam_error(UFS_BADSIGN, tmpsign);
6781eb2bd662Svikram 			return (NULL);
6782eb2bd662Svikram 		}
6783eb2bd662Svikram 
6784eb2bd662Svikram 		signnum = atoi(numstr);
6785eb2bd662Svikram 		INJECT_ERROR1("NEGATIVE_SIGN", signnum = -1);
6786eb2bd662Svikram 		if (signnum < 0) {
6787eb2bd662Svikram 			(void) fclose(tfp);
6788eb2bd662Svikram 			(void) unlink(UFS_SIGNATURE_LIST);
6789eb2bd662Svikram 			bam_error(UFS_BADSIGN, tmpsign);
6790eb2bd662Svikram 			return (NULL);
6791eb2bd662Svikram 		}
6792eb2bd662Svikram 
6793eb2bd662Svikram 		if (i != signnum) {
6794eb2bd662Svikram 			BAM_DPRINTF((D_FOUND_HOLE_SIGNLIST, fcn, i));
6795eb2bd662Svikram 			break;
6796eb2bd662Svikram 		}
6797eb2bd662Svikram 	}
6798eb2bd662Svikram 
6799eb2bd662Svikram 	(void) fclose(tfp);
6800eb2bd662Svikram 
6801eb2bd662Svikram found:
6802eb2bd662Svikram 	(void) snprintf(tmpsign, sizeof (tmpsign), "rootfs%d", i);
6803eb2bd662Svikram 
6804eb2bd662Svikram 	/* add the ufs signature to the /var/run list of signatures */
6805eb2bd662Svikram 	ret = ufs_add_to_sign_list(tmpsign);
6806eb2bd662Svikram 	INJECT_ERROR1("UFS_ADD_TO_SIGN_LIST", ret = -1);
6807eb2bd662Svikram 	if (ret == -1) {
6808eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6809eb2bd662Svikram 		bam_error(FAILED_ADD_SIGNLIST, tmpsign);
6810eb2bd662Svikram 		return (NULL);
6811eb2bd662Svikram 	}
6812eb2bd662Svikram 
6813eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6814eb2bd662Svikram 
6815eb2bd662Svikram 	return (s_strdup(tmpsign));
6816eb2bd662Svikram }
6817eb2bd662Svikram 
6818eb2bd662Svikram static char *
6819eb2bd662Svikram get_fstype(char *osroot)
6820eb2bd662Svikram {
6821eb2bd662Svikram 	FILE		*mntfp;
6822eb2bd662Svikram 	struct mnttab	mp = {0};
6823eb2bd662Svikram 	struct mnttab	mpref = {0};
6824eb2bd662Svikram 	int		error;
6825eb2bd662Svikram 	int		ret;
6826eb2bd662Svikram 	const char	*fcn = "get_fstype()";
6827eb2bd662Svikram 
6828eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_OSROOT", osroot = NULL);
6829eb2bd662Svikram 	if (osroot == NULL) {
6830eb2bd662Svikram 		bam_error(GET_FSTYPE_ARGS);
6831eb2bd662Svikram 		return (NULL);
6832eb2bd662Svikram 	}
6833eb2bd662Svikram 
6834eb2bd662Svikram 	mntfp = fopen(MNTTAB, "r");
6835eb2bd662Svikram 	error = errno;
6836eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_FOPEN", mntfp = NULL);
6837eb2bd662Svikram 	if (mntfp == NULL) {
6838eb2bd662Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
6839eb2bd662Svikram 		return (NULL);
6840eb2bd662Svikram 	}
6841eb2bd662Svikram 
6842eb2bd662Svikram 	if (*osroot == '\0')
6843eb2bd662Svikram 		mpref.mnt_mountp = "/";
6844eb2bd662Svikram 	else
6845eb2bd662Svikram 		mpref.mnt_mountp = osroot;
6846eb2bd662Svikram 
6847eb2bd662Svikram 	ret = getmntany(mntfp, &mp, &mpref);
6848eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_GETMNTANY", ret = 1);
6849eb2bd662Svikram 	if (ret != 0) {
6850eb2bd662Svikram 		bam_error(MNTTAB_MNTPT_NOT_FOUND, osroot, MNTTAB);
6851eb2bd662Svikram 		(void) fclose(mntfp);
6852eb2bd662Svikram 		return (NULL);
6853eb2bd662Svikram 	}
6854eb2bd662Svikram 	(void) fclose(mntfp);
6855eb2bd662Svikram 
6856eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_NULL", mp.mnt_fstype = NULL);
6857eb2bd662Svikram 	if (mp.mnt_fstype == NULL) {
6858eb2bd662Svikram 		bam_error(MNTTAB_FSTYPE_NULL, osroot);
6859eb2bd662Svikram 		return (NULL);
6860eb2bd662Svikram 	}
6861eb2bd662Svikram 
6862eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6863eb2bd662Svikram 
6864eb2bd662Svikram 	return (s_strdup(mp.mnt_fstype));
6865eb2bd662Svikram }
6866eb2bd662Svikram 
6867eb2bd662Svikram static char *
6868eb2bd662Svikram create_zfs_sign(char *osdev)
6869eb2bd662Svikram {
6870eb2bd662Svikram 	char		tmpsign[PATH_MAX];
6871eb2bd662Svikram 	char		*pool;
6872eb2bd662Svikram 	const char	*fcn = "create_zfs_sign()";
6873eb2bd662Svikram 
6874eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, osdev));
6875eb2bd662Svikram 
6876eb2bd662Svikram 	/*
6877eb2bd662Svikram 	 * First find the pool name
6878eb2bd662Svikram 	 */
6879eb2bd662Svikram 	pool = get_pool(osdev);
6880eb2bd662Svikram 	INJECT_ERROR1("CREATE_ZFS_SIGN_GET_POOL", pool = NULL);
6881eb2bd662Svikram 	if (pool == NULL) {
6882eb2bd662Svikram 		bam_error(GET_POOL_FAILED, osdev);
6883eb2bd662Svikram 		return (NULL);
6884eb2bd662Svikram 	}
6885eb2bd662Svikram 
6886eb2bd662Svikram 	(void) snprintf(tmpsign, sizeof (tmpsign), "pool_%s", pool);
6887eb2bd662Svikram 
6888eb2bd662Svikram 	BAM_DPRINTF((D_CREATED_ZFS_SIGN, fcn, tmpsign));
6889eb2bd662Svikram 
6890eb2bd662Svikram 	free(pool);
6891eb2bd662Svikram 
6892eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6893eb2bd662Svikram 
6894eb2bd662Svikram 	return (s_strdup(tmpsign));
6895eb2bd662Svikram }
6896eb2bd662Svikram 
6897eb2bd662Svikram static char *
6898eb2bd662Svikram create_new_sign(char *osdev, char *fstype)
6899eb2bd662Svikram {
6900eb2bd662Svikram 	char		*sign;
6901eb2bd662Svikram 	const char	*fcn = "create_new_sign()";
6902eb2bd662Svikram 
6903eb2bd662Svikram 	INJECT_ERROR1("NEW_SIGN_FSTYPE", fstype = "foofs");
6904eb2bd662Svikram 
6905eb2bd662Svikram 	if (strcmp(fstype, "zfs") == 0) {
6906eb2bd662Svikram 		BAM_DPRINTF((D_CREATE_NEW_ZFS, fcn));
6907eb2bd662Svikram 		sign = create_zfs_sign(osdev);
6908eb2bd662Svikram 	} else if (strcmp(fstype, "ufs") == 0) {
6909eb2bd662Svikram 		BAM_DPRINTF((D_CREATE_NEW_UFS, fcn));
6910eb2bd662Svikram 		sign = create_ufs_sign();
6911eb2bd662Svikram 	} else {
6912eb2bd662Svikram 		bam_error(GRUBSIGN_NOTSUP, fstype);
6913eb2bd662Svikram 		sign = NULL;
6914eb2bd662Svikram 	}
6915eb2bd662Svikram 
6916eb2bd662Svikram 	BAM_DPRINTF((D_CREATED_NEW_SIGN, fcn, sign ? sign : "<NULL>"));
6917eb2bd662Svikram 	return (sign);
6918eb2bd662Svikram }
6919eb2bd662Svikram 
6920eb2bd662Svikram static int
6921eb2bd662Svikram set_backup_common(char *mntpt, char *sign)
6922eb2bd662Svikram {
6923eb2bd662Svikram 	FILE		*bfp;
6924eb2bd662Svikram 	char		backup[PATH_MAX];
6925eb2bd662Svikram 	char		tmpsign[PATH_MAX];
6926eb2bd662Svikram 	int		error;
6927eb2bd662Svikram 	char		*bdir;
6928eb2bd662Svikram 	char		*backup_dup;
6929eb2bd662Svikram 	struct stat	sb;
6930eb2bd662Svikram 	int		ret;
6931eb2bd662Svikram 	const char	*fcn = "set_backup_common()";
6932eb2bd662Svikram 
6933eb2bd662Svikram 	(void) snprintf(backup, sizeof (backup), "%s%s",
6934eb2bd662Svikram 	    mntpt, GRUBSIGN_BACKUP);
6935eb2bd662Svikram 
6936eb2bd662Svikram 	/* First read the backup */
6937eb2bd662Svikram 	bfp = fopen(backup, "r");
6938eb2bd662Svikram 	if (bfp != NULL) {
6939eb2bd662Svikram 		while (s_fgets(tmpsign, sizeof (tmpsign), bfp)) {
6940eb2bd662Svikram 			if (strcmp(tmpsign, sign) == 0) {
6941eb2bd662Svikram 				BAM_DPRINTF((D_FOUND_IN_BACKUP, fcn, sign));
6942eb2bd662Svikram 				(void) fclose(bfp);
6943eb2bd662Svikram 				return (0);
6944eb2bd662Svikram 			}
6945eb2bd662Svikram 		}
6946eb2bd662Svikram 		(void) fclose(bfp);
6947eb2bd662Svikram 		BAM_DPRINTF((D_NOT_FOUND_IN_EXIST_BACKUP, fcn, sign));
6948eb2bd662Svikram 	} else {
6949eb2bd662Svikram 		BAM_DPRINTF((D_BACKUP_NOT_EXIST, fcn, backup));
6950eb2bd662Svikram 	}
6951eb2bd662Svikram 
6952eb2bd662Svikram 	/*
6953eb2bd662Svikram 	 * Didn't find the correct signature. First create
6954eb2bd662Svikram 	 * the directory if necessary.
6955eb2bd662Svikram 	 */
6956eb2bd662Svikram 
6957eb2bd662Svikram 	/* dirname() modifies its argument so dup it */
6958eb2bd662Svikram 	backup_dup = s_strdup(backup);
6959eb2bd662Svikram 	bdir = dirname(backup_dup);
6960eb2bd662Svikram 	assert(bdir);
6961eb2bd662Svikram 
6962eb2bd662Svikram 	ret = stat(bdir, &sb);
6963eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_STAT", ret = -1);
6964eb2bd662Svikram 	if (ret == -1) {
6965eb2bd662Svikram 		BAM_DPRINTF((D_BACKUP_DIR_NOEXIST, fcn, bdir));
696648847494SEnrico Perla - Sun Microsystems 		ret = mkdirp(bdir, DIR_PERMS);
6967eb2bd662Svikram 		error = errno;
6968eb2bd662Svikram 		INJECT_ERROR1("SET_BACKUP_MKDIRP", ret = -1);
6969eb2bd662Svikram 		if (ret == -1) {
6970eb2bd662Svikram 			bam_error(GRUBSIGN_BACKUP_MKDIRERR,
6971eb2bd662Svikram 			    GRUBSIGN_BACKUP, strerror(error));
6972eb2bd662Svikram 			free(backup_dup);
6973eb2bd662Svikram 			return (-1);
6974eb2bd662Svikram 		}
6975eb2bd662Svikram 	}
6976eb2bd662Svikram 	free(backup_dup);
6977eb2bd662Svikram 
6978eb2bd662Svikram 	/*
6979eb2bd662Svikram 	 * Open the backup in append mode to add the correct
6980eb2bd662Svikram 	 * signature;
6981eb2bd662Svikram 	 */
6982eb2bd662Svikram 	bfp = fopen(backup, "a");
6983eb2bd662Svikram 	error = errno;
6984eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_FOPEN_A", bfp = NULL);
6985eb2bd662Svikram 	if (bfp == NULL) {
6986eb2bd662Svikram 		bam_error(GRUBSIGN_BACKUP_OPENERR,
6987eb2bd662Svikram 		    GRUBSIGN_BACKUP, strerror(error));
6988eb2bd662Svikram 		return (-1);
6989eb2bd662Svikram 	}
6990eb2bd662Svikram 
6991eb2bd662Svikram 	(void) snprintf(tmpsign, sizeof (tmpsign), "%s\n", sign);
6992eb2bd662Svikram 
6993eb2bd662Svikram 	ret = fputs(tmpsign, bfp);
6994eb2bd662Svikram 	error = errno;
6995eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_FPUTS", ret = 0);
6996eb2bd662Svikram 	if (ret != strlen(tmpsign)) {
6997eb2bd662Svikram 		bam_error(GRUBSIGN_BACKUP_WRITEERR,
6998eb2bd662Svikram 		    GRUBSIGN_BACKUP, strerror(error));
6999eb2bd662Svikram 		(void) fclose(bfp);
7000eb2bd662Svikram 		return (-1);
7001eb2bd662Svikram 	}
7002eb2bd662Svikram 
7003eb2bd662Svikram 	(void) fclose(bfp);
7004eb2bd662Svikram 
7005eb2bd662Svikram 	if (bam_verbose)
7006eb2bd662Svikram 		bam_print(GRUBSIGN_BACKUP_UPDATED, GRUBSIGN_BACKUP);
7007eb2bd662Svikram 
7008eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7009eb2bd662Svikram 
7010eb2bd662Svikram 	return (0);
7011eb2bd662Svikram }
7012eb2bd662Svikram 
7013eb2bd662Svikram static int
7014eb2bd662Svikram set_backup_ufs(char *osroot, char *sign)
7015eb2bd662Svikram {
7016eb2bd662Svikram 	const char	*fcn = "set_backup_ufs()";
7017eb2bd662Svikram 
7018eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, sign));
7019eb2bd662Svikram 	return (set_backup_common(osroot, sign));
7020eb2bd662Svikram }
7021eb2bd662Svikram 
7022eb2bd662Svikram static int
7023eb2bd662Svikram set_backup_zfs(char *osdev, char *sign)
7024eb2bd662Svikram {
7025eb2bd662Svikram 	char		*pool;
7026eb2bd662Svikram 	char		*mntpt;
7027eb2bd662Svikram 	zfs_mnted_t	mnted;
7028eb2bd662Svikram 	int		ret;
7029eb2bd662Svikram 	const char	*fcn = "set_backup_zfs()";
7030eb2bd662Svikram 
7031eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osdev, sign));
7032eb2bd662Svikram 
7033eb2bd662Svikram 	pool = get_pool(osdev);
7034eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_GET_POOL", pool = NULL);
7035eb2bd662Svikram 	if (pool == NULL) {
7036eb2bd662Svikram 		bam_error(GET_POOL_FAILED, osdev);
7037eb2bd662Svikram 		return (-1);
7038eb2bd662Svikram 	}
7039eb2bd662Svikram 
7040eb2bd662Svikram 	mntpt = mount_top_dataset(pool, &mnted);
7041eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_MOUNT_DATASET", mntpt = NULL);
7042eb2bd662Svikram 	if (mntpt == NULL) {
7043eb2bd662Svikram 		bam_error(FAIL_MNT_TOP_DATASET, pool);
7044eb2bd662Svikram 		free(pool);
7045eb2bd662Svikram 		return (-1);
7046eb2bd662Svikram 	}
7047eb2bd662Svikram 
7048eb2bd662Svikram 	ret = set_backup_common(mntpt, sign);
7049eb2bd662Svikram 
7050eb2bd662Svikram 	(void) umount_top_dataset(pool, mnted, mntpt);
7051eb2bd662Svikram 
7052eb2bd662Svikram 	free(pool);
7053eb2bd662Svikram 
7054eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_ZFS_FAIL", ret = 1);
7055eb2bd662Svikram 	if (ret == 0) {
7056eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7057eb2bd662Svikram 	} else {
7058eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7059eb2bd662Svikram 	}
7060eb2bd662Svikram 
7061eb2bd662Svikram 	return (ret);
7062eb2bd662Svikram }
7063eb2bd662Svikram 
7064eb2bd662Svikram static int
7065eb2bd662Svikram set_backup(char *osroot, char *osdev, char *sign, char *fstype)
7066eb2bd662Svikram {
7067eb2bd662Svikram 	const char	*fcn = "set_backup()";
7068eb2bd662Svikram 	int		ret;
7069eb2bd662Svikram 
7070eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_FSTYPE", fstype = "foofs");
7071eb2bd662Svikram 
7072eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0) {
7073eb2bd662Svikram 		BAM_DPRINTF((D_SET_BACKUP_UFS, fcn));
7074eb2bd662Svikram 		ret = set_backup_ufs(osroot, sign);
7075eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0) {
7076eb2bd662Svikram 		BAM_DPRINTF((D_SET_BACKUP_ZFS, fcn));
7077eb2bd662Svikram 		ret = set_backup_zfs(osdev, sign);
7078eb2bd662Svikram 	} else {
7079eb2bd662Svikram 		bam_error(GRUBSIGN_NOTSUP, fstype);
7080eb2bd662Svikram 		ret = -1;
7081eb2bd662Svikram 	}
7082eb2bd662Svikram 
7083eb2bd662Svikram 	if (ret == 0) {
7084eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7085eb2bd662Svikram 	} else {
7086eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7087eb2bd662Svikram 	}
7088eb2bd662Svikram 
7089eb2bd662Svikram 	return (ret);
7090eb2bd662Svikram }
7091eb2bd662Svikram 
7092eb2bd662Svikram static int
7093eb2bd662Svikram set_primary_common(char *mntpt, char *sign)
7094eb2bd662Svikram {
7095eb2bd662Svikram 	char		signfile[PATH_MAX];
7096eb2bd662Svikram 	char		signdir[PATH_MAX];
7097eb2bd662Svikram 	struct stat	sb;
7098eb2bd662Svikram 	int		fd;
7099eb2bd662Svikram 	int		error;
7100eb2bd662Svikram 	int		ret;
7101eb2bd662Svikram 	const char	*fcn = "set_primary_common()";
7102eb2bd662Svikram 
7103eb2bd662Svikram 	(void) snprintf(signfile, sizeof (signfile), "%s/%s/%s",
7104eb2bd662Svikram 	    mntpt, GRUBSIGN_DIR, sign);
7105eb2bd662Svikram 
7106eb2bd662Svikram 	if (stat(signfile, &sb) != -1) {
7107eb2bd662Svikram 		if (bam_verbose)
7108eb2bd662Svikram 			bam_print(PRIMARY_SIGN_EXISTS, sign);
7109eb2bd662Svikram 		return (0);
7110eb2bd662Svikram 	} else {
7111eb2bd662Svikram 		BAM_DPRINTF((D_PRIMARY_NOT_EXIST, fcn, signfile));
7112eb2bd662Svikram 	}
7113eb2bd662Svikram 
7114eb2bd662Svikram 	(void) snprintf(signdir, sizeof (signdir), "%s/%s",
7115eb2bd662Svikram 	    mntpt, GRUBSIGN_DIR);
7116eb2bd662Svikram 
7117eb2bd662Svikram 	if (stat(signdir, &sb) == -1) {
7118eb2bd662Svikram 		BAM_DPRINTF((D_PRIMARY_DIR_NOEXIST, fcn, signdir));
711948847494SEnrico Perla - Sun Microsystems 		ret = mkdirp(signdir, DIR_PERMS);
7120eb2bd662Svikram 		error = errno;
7121eb2bd662Svikram 		INJECT_ERROR1("SET_PRIMARY_MKDIRP", ret = -1);
7122eb2bd662Svikram 		if (ret == -1) {
7123eb2bd662Svikram 			bam_error(GRUBSIGN_MKDIR_ERR, signdir, strerror(errno));
7124eb2bd662Svikram 			return (-1);
7125eb2bd662Svikram 		}
7126eb2bd662Svikram 	}
7127eb2bd662Svikram 
7128eb2bd662Svikram 	fd = open(signfile, O_RDWR|O_CREAT|O_TRUNC, 0444);
7129eb2bd662Svikram 	error = errno;
7130eb2bd662Svikram 	INJECT_ERROR1("PRIMARY_SIGN_CREAT", fd = -1);
7131eb2bd662Svikram 	if (fd == -1) {
7132eb2bd662Svikram 		bam_error(GRUBSIGN_PRIMARY_CREATERR, signfile, strerror(error));
7133eb2bd662Svikram 		return (-1);
7134eb2bd662Svikram 	}
7135eb2bd662Svikram 
7136eb2bd662Svikram 	ret = fsync(fd);
7137eb2bd662Svikram 	error = errno;
7138eb2bd662Svikram 	INJECT_ERROR1("PRIMARY_FSYNC", ret = -1);
7139eb2bd662Svikram 	if (ret != 0) {
7140eb2bd662Svikram 		bam_error(GRUBSIGN_PRIMARY_SYNCERR, signfile, strerror(error));
7141eb2bd662Svikram 	}
7142eb2bd662Svikram 
7143eb2bd662Svikram 	(void) close(fd);
7144eb2bd662Svikram 
7145eb2bd662Svikram 	if (bam_verbose)
7146eb2bd662Svikram 		bam_print(GRUBSIGN_CREATED_PRIMARY, signfile);
7147eb2bd662Svikram 
7148eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7149eb2bd662Svikram 
7150eb2bd662Svikram 	return (0);
7151eb2bd662Svikram }
7152eb2bd662Svikram 
7153eb2bd662Svikram static int
7154eb2bd662Svikram set_primary_ufs(char *osroot, char *sign)
7155eb2bd662Svikram {
7156eb2bd662Svikram 	const char	*fcn = "set_primary_ufs()";
7157eb2bd662Svikram 
7158eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, sign));
7159eb2bd662Svikram 	return (set_primary_common(osroot, sign));
7160eb2bd662Svikram }
7161eb2bd662Svikram 
7162eb2bd662Svikram static int
7163eb2bd662Svikram set_primary_zfs(char *osdev, char *sign)
7164eb2bd662Svikram {
7165eb2bd662Svikram 	char		*pool;
7166eb2bd662Svikram 	char		*mntpt;
7167eb2bd662Svikram 	zfs_mnted_t	mnted;
7168eb2bd662Svikram 	int		ret;
7169eb2bd662Svikram 	const char	*fcn = "set_primary_zfs()";
7170eb2bd662Svikram 
7171eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osdev, sign));
7172eb2bd662Svikram 
7173eb2bd662Svikram 	pool = get_pool(osdev);
7174eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_GET_POOL", pool = NULL);
7175eb2bd662Svikram 	if (pool == NULL) {
7176eb2bd662Svikram 		bam_error(GET_POOL_FAILED, osdev);
7177eb2bd662Svikram 		return (-1);
7178eb2bd662Svikram 	}
7179eb2bd662Svikram 
7180eb2bd662Svikram 	/* Pool name must exist in the sign */
7181eb2bd662Svikram 	ret = (strstr(sign, pool) != NULL);
7182eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_POOL_SIGN_INCOMPAT", ret = 0);
7183eb2bd662Svikram 	if (ret == 0) {
7184eb2bd662Svikram 		bam_error(POOL_SIGN_INCOMPAT, pool, sign);
7185eb2bd662Svikram 		free(pool);
7186eb2bd662Svikram 		return (-1);
7187eb2bd662Svikram 	}
7188eb2bd662Svikram 
7189eb2bd662Svikram 	mntpt = mount_top_dataset(pool, &mnted);
7190eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_MOUNT_DATASET", mntpt = NULL);
7191eb2bd662Svikram 	if (mntpt == NULL) {
7192eb2bd662Svikram 		bam_error(FAIL_MNT_TOP_DATASET, pool);
7193eb2bd662Svikram 		free(pool);
7194eb2bd662Svikram 		return (-1);
7195eb2bd662Svikram 	}
7196eb2bd662Svikram 
7197eb2bd662Svikram 	ret = set_primary_common(mntpt, sign);
7198eb2bd662Svikram 
7199eb2bd662Svikram 	(void) umount_top_dataset(pool, mnted, mntpt);
7200eb2bd662Svikram 
7201eb2bd662Svikram 	free(pool);
7202eb2bd662Svikram 
7203eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_FAIL", ret = 1);
7204eb2bd662Svikram 	if (ret == 0) {
7205eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7206eb2bd662Svikram 	} else {
7207eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7208eb2bd662Svikram 	}
7209eb2bd662Svikram 
7210eb2bd662Svikram 	return (ret);
7211eb2bd662Svikram }
7212eb2bd662Svikram 
7213eb2bd662Svikram static int
7214eb2bd662Svikram set_primary(char *osroot, char *osdev, char *sign, char *fstype)
7215eb2bd662Svikram {
7216eb2bd662Svikram 	const char	*fcn = "set_primary()";
7217eb2bd662Svikram 	int		ret;
7218eb2bd662Svikram 
7219eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_FSTYPE", fstype = "foofs");
7220eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0) {
7221eb2bd662Svikram 		BAM_DPRINTF((D_SET_PRIMARY_UFS, fcn));
7222eb2bd662Svikram 		ret = set_primary_ufs(osroot, sign);
7223eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0) {
7224eb2bd662Svikram 		BAM_DPRINTF((D_SET_PRIMARY_ZFS, fcn));
7225eb2bd662Svikram 		ret = set_primary_zfs(osdev, sign);
7226eb2bd662Svikram 	} else {
7227eb2bd662Svikram 		bam_error(GRUBSIGN_NOTSUP, fstype);
7228eb2bd662Svikram 		ret = -1;
7229eb2bd662Svikram 	}
7230eb2bd662Svikram 
7231eb2bd662Svikram 	if (ret == 0) {
7232eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7233eb2bd662Svikram 	} else {
7234eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7235eb2bd662Svikram 	}
7236eb2bd662Svikram 
7237eb2bd662Svikram 	return (ret);
7238eb2bd662Svikram }
7239eb2bd662Svikram 
7240eb2bd662Svikram static int
7241eb2bd662Svikram ufs_add_to_sign_list(char *sign)
7242eb2bd662Svikram {
7243eb2bd662Svikram 	FILE		*tfp;
7244eb2bd662Svikram 	char		signline[MAXNAMELEN];
7245eb2bd662Svikram 	char		cmd[PATH_MAX];
7246eb2bd662Svikram 	int		ret;
7247eb2bd662Svikram 	int		error;
7248eb2bd662Svikram 	const char	*fcn = "ufs_add_to_sign_list()";
7249eb2bd662Svikram 
7250eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_NOT_UFS", sign = "pool_rpool5");
7251eb2bd662Svikram 	if (strncmp(sign, GRUBSIGN_UFS_PREFIX,
7252eb2bd662Svikram 	    strlen(GRUBSIGN_UFS_PREFIX)) != 0) {
7253eb2bd662Svikram 		bam_error(INVALID_UFS_SIGN, sign);
7254eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
7255eb2bd662Svikram 		return (-1);
7256eb2bd662Svikram 	}
7257eb2bd662Svikram 
7258eb2bd662Svikram 	/*
7259eb2bd662Svikram 	 * most failures in this routine are not a fatal error
7260eb2bd662Svikram 	 * We simply unlink the /var/run file and continue
7261eb2bd662Svikram 	 */
7262eb2bd662Svikram 
7263eb2bd662Svikram 	ret = rename(UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST".tmp");
7264eb2bd662Svikram 	error = errno;
7265eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_RENAME", ret = -1);
7266eb2bd662Svikram 	if (ret == -1) {
7267eb2bd662Svikram 		bam_error(RENAME_FAIL, UFS_SIGNATURE_LIST".tmp",
7268eb2bd662Svikram 		    strerror(error));
7269eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
7270eb2bd662Svikram 		return (0);
7271eb2bd662Svikram 	}
7272eb2bd662Svikram 
7273eb2bd662Svikram 	tfp = fopen(UFS_SIGNATURE_LIST".tmp", "a");
7274eb2bd662Svikram 	error = errno;
7275eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FOPEN", tfp = NULL);
7276eb2bd662Svikram 	if (tfp == NULL) {
7277eb2bd662Svikram 		bam_error(OPEN_FAIL, UFS_SIGNATURE_LIST".tmp", strerror(error));
7278eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
7279eb2bd662Svikram 		return (0);
7280eb2bd662Svikram 	}
7281eb2bd662Svikram 
7282eb2bd662Svikram 	(void) snprintf(signline, sizeof (signline), "%s\n", sign);
7283eb2bd662Svikram 
7284eb2bd662Svikram 	ret = fputs(signline, tfp);
7285eb2bd662Svikram 	error = errno;
7286eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FPUTS", ret = 0);
7287eb2bd662Svikram 	if (ret != strlen(signline)) {
7288eb2bd662Svikram 		bam_error(SIGN_LIST_FPUTS_ERR, sign, strerror(error));
7289eb2bd662Svikram 		(void) fclose(tfp);
7290eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
7291eb2bd662Svikram 		return (0);
7292eb2bd662Svikram 	}
7293eb2bd662Svikram 
7294eb2bd662Svikram 	ret = fclose(tfp);
7295eb2bd662Svikram 	error = errno;
7296eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FCLOSE", ret = EOF);
7297eb2bd662Svikram 	if (ret == EOF) {
7298eb2bd662Svikram 		bam_error(CLOSE_FAIL, UFS_SIGNATURE_LIST".tmp",
7299eb2bd662Svikram 		    strerror(error));
7300eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
7301eb2bd662Svikram 		return (0);
7302eb2bd662Svikram 	}
7303eb2bd662Svikram 
7304eb2bd662Svikram 	/* Sort the list again */
7305eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
7306eb2bd662Svikram 	    "/usr/bin/sort -u %s.tmp > %s.sorted",
7307eb2bd662Svikram 	    UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST);
7308eb2bd662Svikram 
7309eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
7310eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_SORT", ret = 1);
7311eb2bd662Svikram 	if (ret != 0) {
7312eb2bd662Svikram 		bam_error(GRUBSIGN_SORT_FAILED);
7313eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
7314eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
7315eb2bd662Svikram 		return (0);
7316eb2bd662Svikram 	}
7317eb2bd662Svikram 
7318eb2bd662Svikram 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
7319eb2bd662Svikram 
7320eb2bd662Svikram 	ret = rename(UFS_SIGNATURE_LIST".sorted", UFS_SIGNATURE_LIST);
7321eb2bd662Svikram 	error = errno;
7322eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_RENAME2", ret = -1);
7323eb2bd662Svikram 	if (ret == -1) {
7324eb2bd662Svikram 		bam_error(RENAME_FAIL, UFS_SIGNATURE_LIST, strerror(error));
7325eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
7326eb2bd662Svikram 		return (0);
7327eb2bd662Svikram 	}
7328eb2bd662Svikram 
7329eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7330eb2bd662Svikram 
7331eb2bd662Svikram 	return (0);
7332eb2bd662Svikram }
7333eb2bd662Svikram 
7334eb2bd662Svikram static int
7335eb2bd662Svikram set_signature(char *osroot, char *osdev, char *sign, char *fstype)
7336eb2bd662Svikram {
7337eb2bd662Svikram 	int		ret;
7338eb2bd662Svikram 	const char	*fcn = "set_signature()";
7339eb2bd662Svikram 
7340eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY4, fcn, osroot, osdev, sign, fstype));
7341eb2bd662Svikram 
7342eb2bd662Svikram 	ret = set_backup(osroot, osdev, sign, fstype);
7343eb2bd662Svikram 	INJECT_ERROR1("SET_SIGNATURE_BACKUP", ret = -1);
7344eb2bd662Svikram 	if (ret == -1) {
7345eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7346eb2bd662Svikram 		bam_error(SET_BACKUP_FAILED, sign, osroot, osdev);
7347eb2bd662Svikram 		return (-1);
7348eb2bd662Svikram 	}
7349eb2bd662Svikram 
7350eb2bd662Svikram 	ret = set_primary(osroot, osdev, sign, fstype);
7351eb2bd662Svikram 	INJECT_ERROR1("SET_SIGNATURE_PRIMARY", ret = -1);
7352eb2bd662Svikram 
7353eb2bd662Svikram 	if (ret == 0) {
7354eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7355eb2bd662Svikram 	} else {
7356eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7357eb2bd662Svikram 		bam_error(SET_PRIMARY_FAILED, sign, osroot, osdev);
7358eb2bd662Svikram 
7359eb2bd662Svikram 	}
7360eb2bd662Svikram 	return (ret);
7361eb2bd662Svikram }
7362eb2bd662Svikram 
7363eb2bd662Svikram char *
7364eb2bd662Svikram get_grubsign(char *osroot, char *osdev)
7365eb2bd662Svikram {
7366eb2bd662Svikram 	char		*grubsign;	/* (<sign>,#,#) */
7367eb2bd662Svikram 	char		*slice;
7368eb2bd662Svikram 	int		fdiskpart;
7369eb2bd662Svikram 	char		*sign;
7370eb2bd662Svikram 	char		*fstype;
7371eb2bd662Svikram 	int		ret;
7372eb2bd662Svikram 	const char	*fcn = "get_grubsign()";
7373eb2bd662Svikram 
7374eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, osdev));
7375eb2bd662Svikram 	fstype = get_fstype(osroot);
7376eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBSIGN_FSTYPE", fstype = NULL);
7377eb2bd662Svikram 	if (fstype == NULL) {
7378eb2bd662Svikram 		bam_error(GET_FSTYPE_FAILED, osroot);
7379eb2bd662Svikram 		return (NULL);
7380eb2bd662Svikram 	}
7381eb2bd662Svikram 
7382eb2bd662Svikram 	sign = find_existing_sign(osroot, osdev, fstype);
7383eb2bd662Svikram 	INJECT_ERROR1("FIND_EXISTING_SIGN", sign = NULL);
7384eb2bd662Svikram 	if (sign == NULL) {
7385eb2bd662Svikram 		BAM_DPRINTF((D_GET_GRUBSIGN_NO_EXISTING, fcn, osroot, osdev));
7386eb2bd662Svikram 		sign = create_new_sign(osdev, fstype);
7387eb2bd662Svikram 		INJECT_ERROR1("CREATE_NEW_SIGN", sign = NULL);
7388eb2bd662Svikram 		if (sign == NULL) {
7389eb2bd662Svikram 			bam_error(GRUBSIGN_CREATE_FAIL, osdev);
7390eb2bd662Svikram 			free(fstype);
7391eb2bd662Svikram 			return (NULL);
7392eb2bd662Svikram 		}
7393eb2bd662Svikram 	}
7394eb2bd662Svikram 
7395eb2bd662Svikram 	ret = set_signature(osroot, osdev, sign, fstype);
7396eb2bd662Svikram 	INJECT_ERROR1("SET_SIGNATURE_FAIL", ret = -1);
7397eb2bd662Svikram 	if (ret == -1) {
7398eb2bd662Svikram 		bam_error(GRUBSIGN_WRITE_FAIL, osdev);
7399eb2bd662Svikram 		free(sign);
7400eb2bd662Svikram 		free(fstype);
7401eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
7402eb2bd662Svikram 		return (NULL);
7403eb2bd662Svikram 	}
7404eb2bd662Svikram 
7405eb2bd662Svikram 	free(fstype);
7406eb2bd662Svikram 
7407eb2bd662Svikram 	if (bam_verbose)
7408eb2bd662Svikram 		bam_print(GRUBSIGN_FOUND_OR_CREATED, sign, osdev);
7409eb2bd662Svikram 
7410eb2bd662Svikram 	fdiskpart = get_partition(osdev);
74111a902ef8SHans Rosenfeld 	INJECT_ERROR1("GET_GRUBSIGN_FDISK", fdiskpart = PARTNO_NOTFOUND);
74121a902ef8SHans Rosenfeld 	if (fdiskpart == PARTNO_NOTFOUND) {
7413eb2bd662Svikram 		bam_error(FDISKPART_FAIL, osdev);
7414eb2bd662Svikram 		free(sign);
7415eb2bd662Svikram 		return (NULL);
7416eb2bd662Svikram 	}
7417eb2bd662Svikram 
7418eb2bd662Svikram 	slice = strrchr(osdev, 's');
7419eb2bd662Svikram 
74201a902ef8SHans Rosenfeld 	if (fdiskpart == PARTNO_EFI) {
74211a902ef8SHans Rosenfeld 		fdiskpart = atoi(&slice[1]);
74221a902ef8SHans Rosenfeld 		slice = NULL;
74231a902ef8SHans Rosenfeld 	}
74241a902ef8SHans Rosenfeld 
7425eb2bd662Svikram 	grubsign = s_calloc(1, MAXNAMELEN + 10);
7426eb2bd662Svikram 	if (slice) {
7427eb2bd662Svikram 		(void) snprintf(grubsign, MAXNAMELEN + 10, "(%s,%d,%c)",
7428eb2bd662Svikram 		    sign, fdiskpart, slice[1] + 'a' - '0');
7429eb2bd662Svikram 	} else
7430eb2bd662Svikram 		(void) snprintf(grubsign, MAXNAMELEN + 10, "(%s,%d)",
7431eb2bd662Svikram 		    sign, fdiskpart);
7432eb2bd662Svikram 
7433eb2bd662Svikram 	free(sign);
7434eb2bd662Svikram 
7435eb2bd662Svikram 	BAM_DPRINTF((D_GET_GRUBSIGN_SUCCESS, fcn, grubsign));
7436eb2bd662Svikram 
7437eb2bd662Svikram 	return (grubsign);
74387c478bd9Sstevel@tonic-gate }
74397c478bd9Sstevel@tonic-gate 
7440ee3b8144Sszhou static char *
7441ee3b8144Sszhou get_title(char *rootdir)
74427c478bd9Sstevel@tonic-gate {
7443eb2bd662Svikram 	static char	title[80];
7444eb2bd662Svikram 	char		*cp = NULL;
7445eb2bd662Svikram 	char		release[PATH_MAX];
74467c478bd9Sstevel@tonic-gate 	FILE		*fp;
7447eb2bd662Svikram 	const char	*fcn = "get_title()";
74487c478bd9Sstevel@tonic-gate 
74497c478bd9Sstevel@tonic-gate 	/* open the /etc/release file */
74507c478bd9Sstevel@tonic-gate 	(void) snprintf(release, sizeof (release), "%s/etc/release", rootdir);
74517c478bd9Sstevel@tonic-gate 
74527c478bd9Sstevel@tonic-gate 	fp = fopen(release, "r");
7453eb2bd662Svikram 	if (fp == NULL) {
7454eb2bd662Svikram 		bam_error(OPEN_FAIL, release, strerror(errno));
7455eb2bd662Svikram 		cp = NULL;
7456eb2bd662Svikram 		goto out;
7457eb2bd662Svikram 	}
74587c478bd9Sstevel@tonic-gate 
7459655ac28eSGeorge Asaad 	/* grab first line of /etc/release */
7460655ac28eSGeorge Asaad 	cp = s_fgets(title, sizeof (title), fp);
7461655ac28eSGeorge Asaad 	if (cp) {
7462655ac28eSGeorge Asaad 		while (isspace(*cp))    /* remove leading spaces */
7463655ac28eSGeorge Asaad 			cp++;
74647c478bd9Sstevel@tonic-gate 	}
7465655ac28eSGeorge Asaad 
74667c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
7467eb2bd662Svikram 
7468eb2bd662Svikram out:
7469655ac28eSGeorge Asaad 	cp = cp ? cp : "Oracle Solaris";
7470eb2bd662Svikram 
7471eb2bd662Svikram 	BAM_DPRINTF((D_GET_TITLE, fcn, cp));
7472eb2bd662Svikram 
7473eb2bd662Svikram 	return (cp);
74747c478bd9Sstevel@tonic-gate }
74757c478bd9Sstevel@tonic-gate 
7476ae115bc7Smrj char *
74777c478bd9Sstevel@tonic-gate get_special(char *mountp)
74787c478bd9Sstevel@tonic-gate {
74797c478bd9Sstevel@tonic-gate 	FILE		*mntfp;
7480eb2bd662Svikram 	struct mnttab	mp = {0};
7481eb2bd662Svikram 	struct mnttab	mpref = {0};
7482eb2bd662Svikram 	int		error;
7483eb2bd662Svikram 	int		ret;
7484eb2bd662Svikram 	const char 	*fcn = "get_special()";
7485eb2bd662Svikram 
7486eb2bd662Svikram 	INJECT_ERROR1("GET_SPECIAL_MNTPT", mountp = NULL);
7487eb2bd662Svikram 	if (mountp == NULL) {
7488eb2bd662Svikram 		bam_error(GET_SPECIAL_NULL_MNTPT);
7489eb2bd662Svikram 		return (NULL);
7490eb2bd662Svikram 	}
74917c478bd9Sstevel@tonic-gate 
74927c478bd9Sstevel@tonic-gate 	mntfp = fopen(MNTTAB, "r");
7493eb2bd662Svikram 	error = errno;
7494eb2bd662Svikram 	INJECT_ERROR1("GET_SPECIAL_MNTTAB_OPEN", mntfp = NULL);
74957c478bd9Sstevel@tonic-gate 	if (mntfp == NULL) {
7496eb2bd662Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
7497eb2bd662Svikram 		return (NULL);
74987c478bd9Sstevel@tonic-gate 	}
74997c478bd9Sstevel@tonic-gate 
75007c478bd9Sstevel@tonic-gate 	if (*mountp == '\0')
75017c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = "/";
75027c478bd9Sstevel@tonic-gate 	else
75037c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = mountp;
7504eb2bd662Svikram 
7505eb2bd662Svikram 	ret = getmntany(mntfp, &mp, &mpref);
7506eb2bd662Svikram 	INJECT_ERROR1("GET_SPECIAL_MNTTAB_SEARCH", ret = 1);
7507eb2bd662Svikram 	if (ret != 0) {
75087c478bd9Sstevel@tonic-gate 		(void) fclose(mntfp);
7509eb2bd662Svikram 		BAM_DPRINTF((D_GET_SPECIAL_NOT_IN_MNTTAB, fcn, mountp));
75107c478bd9Sstevel@tonic-gate 		return (NULL);
75117c478bd9Sstevel@tonic-gate 	}
75127c478bd9Sstevel@tonic-gate 	(void) fclose(mntfp);
75137c478bd9Sstevel@tonic-gate 
7514eb2bd662Svikram 	BAM_DPRINTF((D_GET_SPECIAL, fcn, mp.mnt_special));
7515eb2bd662Svikram 
75167c478bd9Sstevel@tonic-gate 	return (s_strdup(mp.mnt_special));
75177c478bd9Sstevel@tonic-gate }
75187c478bd9Sstevel@tonic-gate 
7519eb2bd662Svikram static void
7520eb2bd662Svikram free_physarray(char **physarray, int n)
75217c478bd9Sstevel@tonic-gate {
7522eb2bd662Svikram 	int			i;
7523eb2bd662Svikram 	const char		*fcn = "free_physarray()";
75247c478bd9Sstevel@tonic-gate 
7525eb2bd662Svikram 	assert(physarray);
7526eb2bd662Svikram 	assert(n);
7527eb2bd662Svikram 
7528eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY_N1, fcn, n));
7529eb2bd662Svikram 
7530eb2bd662Svikram 	for (i = 0; i < n; i++) {
7531eb2bd662Svikram 		free(physarray[i]);
75327c478bd9Sstevel@tonic-gate 	}
7533eb2bd662Svikram 	free(physarray);
7534eb2bd662Svikram 
7535eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7536eb2bd662Svikram }
7537eb2bd662Svikram 
7538eb2bd662Svikram static int
7539eb2bd662Svikram zfs_get_physical(char *special, char ***physarray, int *n)
7540eb2bd662Svikram {
7541eb2bd662Svikram 	char			sdup[PATH_MAX];
7542eb2bd662Svikram 	char			cmd[PATH_MAX];
7543eb2bd662Svikram 	char			dsk[PATH_MAX];
7544eb2bd662Svikram 	char			*pool;
7545eb2bd662Svikram 	filelist_t		flist = {0};
7546eb2bd662Svikram 	line_t			*lp;
7547eb2bd662Svikram 	line_t			*startlp;
7548eb2bd662Svikram 	char			*comp1;
7549eb2bd662Svikram 	int			i;
7550eb2bd662Svikram 	int			ret;
7551eb2bd662Svikram 	const char		*fcn = "zfs_get_physical()";
7552eb2bd662Svikram 
7553eb2bd662Svikram 	assert(special);
7554eb2bd662Svikram 
7555eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, special));
7556eb2bd662Svikram 
7557eb2bd662Svikram 	INJECT_ERROR1("INVALID_ZFS_SPECIAL", special = "/foo");
7558eb2bd662Svikram 	if (special[0] == '/') {
7559eb2bd662Svikram 		bam_error(INVALID_ZFS_SPECIAL, special);
7560eb2bd662Svikram 		return (-1);
7561eb2bd662Svikram 	}
7562eb2bd662Svikram 
7563eb2bd662Svikram 	(void) strlcpy(sdup, special, sizeof (sdup));
7564eb2bd662Svikram 
7565eb2bd662Svikram 	pool = strtok(sdup, "/");
7566eb2bd662Svikram 	INJECT_ERROR1("ZFS_GET_PHYS_POOL", pool = NULL);
7567eb2bd662Svikram 	if (pool == NULL) {
7568eb2bd662Svikram 		bam_error(CANT_FIND_POOL_FROM_SPECIAL, special);
7569eb2bd662Svikram 		return (-1);
7570eb2bd662Svikram 	}
7571eb2bd662Svikram 
7572eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/zpool status %s", pool);
7573eb2bd662Svikram 
7574eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
7575eb2bd662Svikram 	INJECT_ERROR1("ZFS_GET_PHYS_STATUS", ret = 1);
7576eb2bd662Svikram 	if (ret != 0) {
7577eb2bd662Svikram 		bam_error(ZFS_GET_POOL_STATUS, pool);
7578eb2bd662Svikram 		return (-1);
7579eb2bd662Svikram 	}
7580eb2bd662Svikram 
7581eb2bd662Svikram 	INJECT_ERROR1("ZFS_GET_PHYS_STATUS_OUT", flist.head = NULL);
7582eb2bd662Svikram 	if (flist.head == NULL) {
7583eb2bd662Svikram 		bam_error(BAD_ZPOOL_STATUS, pool);
7584eb2bd662Svikram 		filelist_free(&flist);
7585eb2bd662Svikram 		return (-1);
7586eb2bd662Svikram 	}
7587eb2bd662Svikram 
7588eb2bd662Svikram 	for (lp = flist.head; lp; lp = lp->next) {
7589eb2bd662Svikram 		BAM_DPRINTF((D_STRTOK_ZPOOL_STATUS, fcn, lp->line));
7590eb2bd662Svikram 		comp1 = strtok(lp->line, " \t");
7591eb2bd662Svikram 		if (comp1 == NULL) {
7592eb2bd662Svikram 			free(lp->line);
7593eb2bd662Svikram 			lp->line = NULL;
7594eb2bd662Svikram 		} else {
7595eb2bd662Svikram 			comp1 = s_strdup(comp1);
7596eb2bd662Svikram 			free(lp->line);
7597eb2bd662Svikram 			lp->line = comp1;
7598eb2bd662Svikram 		}
7599eb2bd662Svikram 	}
7600eb2bd662Svikram 
7601eb2bd662Svikram 	for (lp = flist.head; lp; lp = lp->next) {
7602eb2bd662Svikram 		if (lp->line == NULL)
7603eb2bd662Svikram 			continue;
7604eb2bd662Svikram 		if (strcmp(lp->line, pool) == 0) {
7605eb2bd662Svikram 			BAM_DPRINTF((D_FOUND_POOL_IN_ZPOOL_STATUS, fcn, pool));
7606eb2bd662Svikram 			break;
7607eb2bd662Svikram 		}
7608eb2bd662Svikram 	}
7609eb2bd662Svikram 
7610eb2bd662Svikram 	if (lp == NULL) {
7611eb2bd662Svikram 		bam_error(NO_POOL_IN_ZPOOL_STATUS, pool);
7612eb2bd662Svikram 		filelist_free(&flist);
7613eb2bd662Svikram 		return (-1);
7614eb2bd662Svikram 	}
7615eb2bd662Svikram 
7616eb2bd662Svikram 	startlp = lp->next;
7617eb2bd662Svikram 	for (i = 0, lp = startlp; lp; lp = lp->next) {
7618eb2bd662Svikram 		if (lp->line == NULL)
7619eb2bd662Svikram 			continue;
7620eb2bd662Svikram 		if (strcmp(lp->line, "mirror") == 0)
7621eb2bd662Svikram 			continue;
7622eb2bd662Svikram 		if (lp->line[0] == '\0' || strcmp(lp->line, "errors:") == 0)
7623eb2bd662Svikram 			break;
7624eb2bd662Svikram 		i++;
7625eb2bd662Svikram 		BAM_DPRINTF((D_COUNTING_ZFS_PHYS, fcn, i));
7626eb2bd662Svikram 	}
7627eb2bd662Svikram 
7628eb2bd662Svikram 	if (i == 0) {
7629eb2bd662Svikram 		bam_error(NO_PHYS_IN_ZPOOL_STATUS, pool);
7630eb2bd662Svikram 		filelist_free(&flist);
7631eb2bd662Svikram 		return (-1);
7632eb2bd662Svikram 	}
7633eb2bd662Svikram 
7634eb2bd662Svikram 	*n = i;
7635eb2bd662Svikram 	*physarray = s_calloc(*n, sizeof (char *));
7636eb2bd662Svikram 	for (i = 0, lp = startlp; lp; lp = lp->next) {
7637eb2bd662Svikram 		if (lp->line == NULL)
7638eb2bd662Svikram 			continue;
7639eb2bd662Svikram 		if (strcmp(lp->line, "mirror") == 0)
7640eb2bd662Svikram 			continue;
7641eb2bd662Svikram 		if (strcmp(lp->line, "errors:") == 0)
7642eb2bd662Svikram 			break;
7643eb2bd662Svikram 		if (strncmp(lp->line, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
7644eb2bd662Svikram 		    strncmp(lp->line, "/dev/rdsk/",
7645eb2bd662Svikram 		    strlen("/dev/rdsk/")) != 0)  {
7646e998e519SSheshadri Vasudevan 			(void) snprintf(dsk, sizeof (dsk), "/dev/rdsk/%s",
7647eb2bd662Svikram 			    lp->line);
7648eb2bd662Svikram 		} else {
7649eb2bd662Svikram 			(void) strlcpy(dsk, lp->line, sizeof (dsk));
7650eb2bd662Svikram 		}
7651eb2bd662Svikram 		BAM_DPRINTF((D_ADDING_ZFS_PHYS, fcn, dsk, pool));
7652eb2bd662Svikram 		(*physarray)[i++] = s_strdup(dsk);
7653eb2bd662Svikram 	}
7654eb2bd662Svikram 
7655eb2bd662Svikram 	assert(i == *n);
7656eb2bd662Svikram 
7657eb2bd662Svikram 	filelist_free(&flist);
7658eb2bd662Svikram 
7659eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7660eb2bd662Svikram 	return (0);
7661eb2bd662Svikram }
7662eb2bd662Svikram 
7663963390b4Svikram /*
7664963390b4Svikram  * Certain services needed to run metastat successfully may not
7665963390b4Svikram  * be enabled. Enable them now.
7666963390b4Svikram  */
7667963390b4Svikram /*
7668963390b4Svikram  * Checks if the specified service is online
7669963390b4Svikram  * Returns: 	1 if the service is online
7670963390b4Svikram  *		0 if the service is not online
7671963390b4Svikram  *		-1 on error
7672963390b4Svikram  */
7673963390b4Svikram static int
7674963390b4Svikram is_svc_online(char *svc)
7675963390b4Svikram {
7676963390b4Svikram 	char			*state;
7677963390b4Svikram 	const char		*fcn = "is_svc_online()";
7678963390b4Svikram 
7679963390b4Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, svc));
7680963390b4Svikram 
7681963390b4Svikram 	state = smf_get_state(svc);
7682963390b4Svikram 	INJECT_ERROR2("GET_SVC_STATE", free(state), state = NULL);
7683963390b4Svikram 	if (state == NULL) {
7684963390b4Svikram 		bam_error(GET_SVC_STATE_ERR, svc);
7685963390b4Svikram 		return (-1);
7686963390b4Svikram 	}
7687963390b4Svikram 	BAM_DPRINTF((D_GOT_SVC_STATUS, fcn, svc));
7688963390b4Svikram 
7689963390b4Svikram 	if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0) {
7690963390b4Svikram 		BAM_DPRINTF((D_SVC_ONLINE, fcn, svc));
7691963390b4Svikram 		free(state);
7692963390b4Svikram 		return (1);
7693963390b4Svikram 	}
7694963390b4Svikram 
7695963390b4Svikram 	BAM_DPRINTF((D_SVC_NOT_ONLINE, fcn, state, svc));
7696963390b4Svikram 
7697963390b4Svikram 	free(state);
7698963390b4Svikram 
7699963390b4Svikram 	return (0);
7700963390b4Svikram }
7701963390b4Svikram 
7702963390b4Svikram static int
7703963390b4Svikram enable_svc(char *svc)
7704963390b4Svikram {
7705963390b4Svikram 	int			ret;
7706963390b4Svikram 	int			sleeptime;
7707963390b4Svikram 	const char		*fcn = "enable_svc()";
7708963390b4Svikram 
7709963390b4Svikram 	ret = is_svc_online(svc);
7710963390b4Svikram 	if (ret == -1) {
7711963390b4Svikram 		bam_error(SVC_IS_ONLINE_FAILED, svc);
7712963390b4Svikram 		return (-1);
7713963390b4Svikram 	} else if (ret == 1) {
7714963390b4Svikram 		BAM_DPRINTF((D_SVC_ALREADY_ONLINE, fcn, svc));
7715963390b4Svikram 		return (0);
7716963390b4Svikram 	}
7717963390b4Svikram 
7718963390b4Svikram 	/* Service is not enabled. Enable it now. */
7719963390b4Svikram 	ret = smf_enable_instance(svc, 0);
7720963390b4Svikram 	INJECT_ERROR1("ENABLE_SVC_FAILED", ret = -1);
7721963390b4Svikram 	if (ret != 0) {
7722963390b4Svikram 		bam_error(ENABLE_SVC_FAILED, svc);
7723963390b4Svikram 		return (-1);
7724963390b4Svikram 	}
7725963390b4Svikram 
7726963390b4Svikram 	BAM_DPRINTF((D_SVC_ONLINE_INITIATED, fcn, svc));
7727963390b4Svikram 
7728963390b4Svikram 	sleeptime = 0;
7729963390b4Svikram 	do {
7730963390b4Svikram 		ret = is_svc_online(svc);
7731963390b4Svikram 		INJECT_ERROR1("SVC_ONLINE_SUCCESS", ret = 1);
7732963390b4Svikram 		INJECT_ERROR1("SVC_ONLINE_FAILURE", ret = -1);
7733963390b4Svikram 		INJECT_ERROR1("SVC_ONLINE_NOTYET", ret = 0);
7734963390b4Svikram 		if (ret == -1) {
7735963390b4Svikram 			bam_error(ERR_SVC_GET_ONLINE, svc);
7736963390b4Svikram 			return (-1);
7737963390b4Svikram 		} else if (ret == 1) {
7738963390b4Svikram 			BAM_DPRINTF((D_SVC_NOW_ONLINE, fcn, svc));
7739963390b4Svikram 			return (1);
7740963390b4Svikram 		}
7741963390b4Svikram 		(void) sleep(1);
7742589ac1f4SRobert Harris 	} while (++sleeptime < 60);
7743963390b4Svikram 
7744963390b4Svikram 	bam_error(TIMEOUT_ENABLE_SVC, svc);
7745963390b4Svikram 
7746963390b4Svikram 	return (-1);
7747963390b4Svikram }
7748963390b4Svikram 
7749eb2bd662Svikram static int
7750eb2bd662Svikram ufs_get_physical(char *special, char ***physarray, int *n)
7751eb2bd662Svikram {
7752eb2bd662Svikram 	char			cmd[PATH_MAX];
7753eb2bd662Svikram 	char			*shortname;
7754eb2bd662Svikram 	filelist_t		flist = {0};
7755eb2bd662Svikram 	char			*meta;
7756eb2bd662Svikram 	char			*type;
7757eb2bd662Svikram 	char			*comp1;
7758eb2bd662Svikram 	char			*comp2;
7759eb2bd662Svikram 	char			*comp3;
7760eb2bd662Svikram 	char			*comp4;
7761eb2bd662Svikram 	int			i;
7762eb2bd662Svikram 	line_t			*lp;
7763eb2bd662Svikram 	int			ret;
7764963390b4Svikram 	char			*svc;
7765eb2bd662Svikram 	const char		*fcn = "ufs_get_physical()";
7766eb2bd662Svikram 
7767eb2bd662Svikram 	assert(special);
7768eb2bd662Svikram 
7769eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, special));
7770eb2bd662Svikram 
7771eb2bd662Svikram 	if (strncmp(special, "/dev/md/", strlen("/dev/md/")) != 0) {
7772eb2bd662Svikram 		bam_error(UFS_GET_PHYS_NOT_SVM, special);
7773eb2bd662Svikram 		return (-1);
7774eb2bd662Svikram 	}
7775eb2bd662Svikram 
7776eb2bd662Svikram 	if (strncmp(special, "/dev/md/dsk/", strlen("/dev/md/dsk/")) == 0) {
7777eb2bd662Svikram 		shortname = special + strlen("/dev/md/dsk/");
7778eb2bd662Svikram 	} else if (strncmp(special, "/dev/md/rdsk/",
7779eb2bd662Svikram 	    strlen("/dev/md/rdsk/")) == 0) {
7780eb2bd662Svikram 		shortname = special + strlen("/dev/md/rdsk");
7781eb2bd662Svikram 	} else {
7782eb2bd662Svikram 		bam_error(UFS_GET_PHYS_INVALID_SVM, special);
7783eb2bd662Svikram 		return (-1);
7784eb2bd662Svikram 	}
7785eb2bd662Svikram 
7786eb2bd662Svikram 	BAM_DPRINTF((D_UFS_SVM_SHORT, fcn, special, shortname));
7787eb2bd662Svikram 
7788963390b4Svikram 	svc = "network/rpc/meta:default";
7789963390b4Svikram 	if (enable_svc(svc) == -1) {
7790963390b4Svikram 		bam_error(UFS_SVM_METASTAT_SVC_ERR, svc);
7791963390b4Svikram 	}
7792963390b4Svikram 
7793eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/metastat -p %s", shortname);
7794eb2bd662Svikram 
7795eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
7796eb2bd662Svikram 	INJECT_ERROR1("UFS_SVM_METASTAT", ret = 1);
7797eb2bd662Svikram 	if (ret != 0) {
7798eb2bd662Svikram 		bam_error(UFS_SVM_METASTAT_ERR, shortname);
7799eb2bd662Svikram 		return (-1);
7800eb2bd662Svikram 	}
7801eb2bd662Svikram 
7802eb2bd662Svikram 	INJECT_ERROR1("UFS_SVM_METASTAT_OUT", flist.head = NULL);
7803eb2bd662Svikram 	if (flist.head == NULL) {
7804eb2bd662Svikram 		bam_error(BAD_UFS_SVM_METASTAT, shortname);
7805eb2bd662Svikram 		filelist_free(&flist);
7806eb2bd662Svikram 		return (-1);
78077c478bd9Sstevel@tonic-gate 	}
78087c478bd9Sstevel@tonic-gate 
78097c478bd9Sstevel@tonic-gate 	/*
7810eb2bd662Svikram 	 * Check if not a mirror. We only parse a single metadevice
7811eb2bd662Svikram 	 * if not a mirror
7812eb2bd662Svikram 	 */
7813eb2bd662Svikram 	meta = strtok(flist.head->line, " \t");
7814eb2bd662Svikram 	type = strtok(NULL, " \t");
7815eb2bd662Svikram 	if (meta == NULL || type == NULL) {
7816eb2bd662Svikram 		bam_error(ERROR_PARSE_UFS_SVM_METASTAT, shortname);
7817eb2bd662Svikram 		filelist_free(&flist);
7818eb2bd662Svikram 		return (-1);
7819eb2bd662Svikram 	}
7820eb2bd662Svikram 	if (strcmp(type, "-m") != 0) {
7821eb2bd662Svikram 		comp1 = strtok(NULL, " \t");
7822eb2bd662Svikram 		comp2 = strtok(NULL, " \t");
7823eb2bd662Svikram 		if (comp1 == NULL || comp2 != NULL) {
7824eb2bd662Svikram 			bam_error(INVALID_UFS_SVM_METASTAT, shortname);
7825eb2bd662Svikram 			filelist_free(&flist);
7826eb2bd662Svikram 			return (-1);
7827eb2bd662Svikram 		}
7828eb2bd662Svikram 		BAM_DPRINTF((D_UFS_SVM_ONE_COMP, fcn, comp1, shortname));
7829eb2bd662Svikram 		*physarray = s_calloc(1, sizeof (char *));
7830eb2bd662Svikram 		(*physarray)[0] = s_strdup(comp1);
7831eb2bd662Svikram 		*n = 1;
7832eb2bd662Svikram 		filelist_free(&flist);
7833eb2bd662Svikram 		return (0);
7834eb2bd662Svikram 	}
7835eb2bd662Svikram 
7836eb2bd662Svikram 	/*
7837eb2bd662Svikram 	 * Okay we have a mirror. Everything after the first line
7838eb2bd662Svikram 	 * is a submirror
7839eb2bd662Svikram 	 */
7840eb2bd662Svikram 	for (i = 0, lp = flist.head->next; lp; lp = lp->next) {
7841eb2bd662Svikram 		if (strstr(lp->line, "/dev/dsk/") == NULL &&
7842eb2bd662Svikram 		    strstr(lp->line, "/dev/rdsk/") == NULL) {
7843eb2bd662Svikram 			bam_error(CANNOT_PARSE_UFS_SVM_METASTAT, shortname);
7844eb2bd662Svikram 			filelist_free(&flist);
7845eb2bd662Svikram 			return (-1);
7846eb2bd662Svikram 		}
7847eb2bd662Svikram 		i++;
7848eb2bd662Svikram 	}
7849eb2bd662Svikram 
7850eb2bd662Svikram 	*physarray = s_calloc(i, sizeof (char *));
7851eb2bd662Svikram 	*n = i;
7852eb2bd662Svikram 
7853eb2bd662Svikram 	for (i = 0, lp = flist.head->next; lp; lp = lp->next) {
7854eb2bd662Svikram 		comp1 = strtok(lp->line, " \t");
7855eb2bd662Svikram 		comp2 = strtok(NULL, " \t");
7856eb2bd662Svikram 		comp3 = strtok(NULL, " \t");
7857eb2bd662Svikram 		comp4 = strtok(NULL, " \t");
7858eb2bd662Svikram 
7859eb2bd662Svikram 		if (comp3 == NULL || comp4 == NULL ||
7860eb2bd662Svikram 		    (strncmp(comp4, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
7861eb2bd662Svikram 		    strncmp(comp4, "/dev/rdsk/", strlen("/dev/rdsk/")) != 0)) {
7862eb2bd662Svikram 			bam_error(CANNOT_PARSE_UFS_SVM_SUBMIRROR, shortname);
7863eb2bd662Svikram 			filelist_free(&flist);
7864eb2bd662Svikram 			free_physarray(*physarray, *n);
7865eb2bd662Svikram 			return (-1);
7866eb2bd662Svikram 		}
7867eb2bd662Svikram 
7868eb2bd662Svikram 		(*physarray)[i++] = s_strdup(comp4);
7869eb2bd662Svikram 	}
7870eb2bd662Svikram 
7871eb2bd662Svikram 	assert(i == *n);
7872eb2bd662Svikram 
7873eb2bd662Svikram 	filelist_free(&flist);
7874eb2bd662Svikram 
7875eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7876eb2bd662Svikram 	return (0);
7877eb2bd662Svikram }
7878eb2bd662Svikram 
7879eb2bd662Svikram static int
7880eb2bd662Svikram get_physical(char *menu_root, char ***physarray, int *n)
7881eb2bd662Svikram {
7882eb2bd662Svikram 	char			*special;
7883eb2bd662Svikram 	int			ret;
7884eb2bd662Svikram 	const char		*fcn = "get_physical()";
7885eb2bd662Svikram 
7886eb2bd662Svikram 	assert(menu_root);
7887eb2bd662Svikram 	assert(physarray);
7888eb2bd662Svikram 	assert(n);
7889eb2bd662Svikram 
7890eb2bd662Svikram 	*physarray = NULL;
7891eb2bd662Svikram 	*n = 0;
7892eb2bd662Svikram 
7893eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, menu_root));
7894eb2bd662Svikram 
7895eb2bd662Svikram 	/* First get the device special file from /etc/mnttab */
7896eb2bd662Svikram 	special = get_special(menu_root);
7897eb2bd662Svikram 	INJECT_ERROR1("GET_PHYSICAL_SPECIAL", special = NULL);
7898eb2bd662Svikram 	if (special == NULL) {
7899eb2bd662Svikram 		bam_error(GET_SPECIAL_NULL, menu_root);
7900eb2bd662Svikram 		return (-1);
7901eb2bd662Svikram 	}
7902eb2bd662Svikram 
7903eb2bd662Svikram 	/* If already a physical device nothing to do */
7904eb2bd662Svikram 	if (strncmp(special, "/dev/dsk/", strlen("/dev/dsk/")) == 0 ||
7905eb2bd662Svikram 	    strncmp(special, "/dev/rdsk/", strlen("/dev/rdsk/")) == 0) {
7906eb2bd662Svikram 		BAM_DPRINTF((D_GET_PHYSICAL_ALREADY, fcn, menu_root, special));
7907eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7908eb2bd662Svikram 		*physarray = s_calloc(1, sizeof (char *));
7909eb2bd662Svikram 		(*physarray)[0] = special;
7910eb2bd662Svikram 		*n = 1;
7911eb2bd662Svikram 		return (0);
7912eb2bd662Svikram 	}
7913eb2bd662Svikram 
7914eb2bd662Svikram 	if (is_zfs(menu_root)) {
7915eb2bd662Svikram 		ret = zfs_get_physical(special, physarray, n);
7916eb2bd662Svikram 	} else if (is_ufs(menu_root)) {
7917eb2bd662Svikram 		ret = ufs_get_physical(special, physarray, n);
7918eb2bd662Svikram 	} else {
7919eb2bd662Svikram 		bam_error(GET_PHYSICAL_NOTSUP_FSTYPE, menu_root, special);
7920eb2bd662Svikram 		ret = -1;
7921eb2bd662Svikram 	}
7922eb2bd662Svikram 
7923eb2bd662Svikram 	free(special);
7924eb2bd662Svikram 
7925eb2bd662Svikram 	INJECT_ERROR1("GET_PHYSICAL_RET", ret = -1);
7926eb2bd662Svikram 	if (ret == -1) {
7927eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7928eb2bd662Svikram 	} else {
7929eb2bd662Svikram 		int	i;
7930eb2bd662Svikram 		assert (*n > 0);
7931eb2bd662Svikram 		for (i = 0; i < *n; i++) {
7932eb2bd662Svikram 			BAM_DPRINTF((D_GET_PHYSICAL_RET, fcn, (*physarray)[i]));
7933eb2bd662Svikram 		}
7934eb2bd662Svikram 	}
7935eb2bd662Svikram 
7936eb2bd662Svikram 	return (ret);
7937eb2bd662Svikram }
7938eb2bd662Svikram 
7939eb2bd662Svikram static int
7940eb2bd662Svikram is_bootdisk(char *osroot, char *physical)
7941eb2bd662Svikram {
7942eb2bd662Svikram 	int			ret;
7943eb2bd662Svikram 	char			*grubroot;
7944eb2bd662Svikram 	char			*bootp;
7945eb2bd662Svikram 	const char		*fcn = "is_bootdisk()";
7946eb2bd662Svikram 
7947eb2bd662Svikram 	assert(osroot);
7948eb2bd662Svikram 	assert(physical);
7949eb2bd662Svikram 
7950eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, physical));
7951eb2bd662Svikram 
7952eb2bd662Svikram 	bootp = strstr(physical, "p0:boot");
7953eb2bd662Svikram 	if (bootp)
7954eb2bd662Svikram 		*bootp = '\0';
7955eb2bd662Svikram 	/*
7956eb2bd662Svikram 	 * We just want the BIOS mapping for menu disk.
7957eb2bd662Svikram 	 * Don't pass menu_root to get_grubroot() as the
7958eb2bd662Svikram 	 * check that it is used for is not relevant here.
7959eb2bd662Svikram 	 * The osroot is immaterial as well - it is only used to
7960eb2bd662Svikram 	 * to find create_diskmap script. Everything hinges on
7961eb2bd662Svikram 	 * "physical"
7962eb2bd662Svikram 	 */
7963eb2bd662Svikram 	grubroot = get_grubroot(osroot, physical, NULL);
7964eb2bd662Svikram 
7965eb2bd662Svikram 	INJECT_ERROR1("IS_BOOTDISK_GRUBROOT", grubroot = NULL);
7966eb2bd662Svikram 	if (grubroot == NULL) {
796737eb779cSVikram Hegde 		if (bam_verbose)
796837eb779cSVikram Hegde 			bam_error(NO_GRUBROOT_FOR_DISK, physical);
7969eb2bd662Svikram 		return (0);
7970eb2bd662Svikram 	}
7971eb2bd662Svikram 	ret = grubroot[3] == '0';
7972eb2bd662Svikram 	free(grubroot);
7973eb2bd662Svikram 
7974eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_RET, fcn, ret));
7975eb2bd662Svikram 
7976eb2bd662Svikram 	return (ret);
7977eb2bd662Svikram }
7978eb2bd662Svikram 
7979eb2bd662Svikram /*
7980eb2bd662Svikram  * Check if menu is on the boot device
79817c478bd9Sstevel@tonic-gate  * Return 0 (false) on error
79827c478bd9Sstevel@tonic-gate  */
79837c478bd9Sstevel@tonic-gate static int
7984eb2bd662Svikram menu_on_bootdisk(char *osroot, char *menu_root)
79857c478bd9Sstevel@tonic-gate {
7986eb2bd662Svikram 	char		**physarray;
79877c478bd9Sstevel@tonic-gate 	int		ret;
7988eb2bd662Svikram 	int		n;
7989eb2bd662Svikram 	int		i;
7990eb2bd662Svikram 	int		on_bootdisk;
7991eb2bd662Svikram 	const char	*fcn = "menu_on_bootdisk()";
79927c478bd9Sstevel@tonic-gate 
7993eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, menu_root));
79947c478bd9Sstevel@tonic-gate 
7995eb2bd662Svikram 	ret = get_physical(menu_root, &physarray, &n);
7996eb2bd662Svikram 	INJECT_ERROR1("MENU_ON_BOOTDISK_PHYSICAL", ret = -1);
7997eb2bd662Svikram 	if (ret != 0) {
7998eb2bd662Svikram 		bam_error(GET_PHYSICAL_MENU_NULL, menu_root);
79997c478bd9Sstevel@tonic-gate 		return (0);
8000eb2bd662Svikram 	}
8001eb2bd662Svikram 
8002eb2bd662Svikram 	assert(physarray);
8003eb2bd662Svikram 	assert(n > 0);
8004eb2bd662Svikram 
8005eb2bd662Svikram 	on_bootdisk = 0;
8006eb2bd662Svikram 	for (i = 0; i < n; i++) {
8007eb2bd662Svikram 		assert(strncmp(physarray[i], "/dev/dsk/",
8008eb2bd662Svikram 		    strlen("/dev/dsk/")) == 0 ||
8009eb2bd662Svikram 		    strncmp(physarray[i], "/dev/rdsk/",
8010eb2bd662Svikram 		    strlen("/dev/rdsk/")) == 0);
8011eb2bd662Svikram 
8012eb2bd662Svikram 		BAM_DPRINTF((D_CHECK_ON_BOOTDISK, fcn, physarray[i]));
8013eb2bd662Svikram 		if (is_bootdisk(osroot, physarray[i])) {
8014eb2bd662Svikram 			on_bootdisk = 1;
8015eb2bd662Svikram 			BAM_DPRINTF((D_IS_ON_BOOTDISK, fcn, physarray[i]));
8016eb2bd662Svikram 		}
8017eb2bd662Svikram 	}
8018eb2bd662Svikram 
8019eb2bd662Svikram 	free_physarray(physarray, n);
8020eb2bd662Svikram 
8021eb2bd662Svikram 	INJECT_ERROR1("ON_BOOTDISK_YES", on_bootdisk = 1);
8022eb2bd662Svikram 	INJECT_ERROR1("ON_BOOTDISK_NO", on_bootdisk = 0);
8023eb2bd662Svikram 	if (on_bootdisk) {
8024eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
8025eb2bd662Svikram 	} else {
8026eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
8027eb2bd662Svikram 	}
8028eb2bd662Svikram 
8029eb2bd662Svikram 	return (on_bootdisk);
8030eb2bd662Svikram }
8031eb2bd662Svikram 
8032eb2bd662Svikram void
8033eb2bd662Svikram bam_add_line(menu_t *mp, entry_t *entry, line_t *prev, line_t *lp)
8034eb2bd662Svikram {
8035eb2bd662Svikram 	const char	*fcn = "bam_add_line()";
8036eb2bd662Svikram 
8037eb2bd662Svikram 	assert(mp);
8038eb2bd662Svikram 	assert(entry);
8039eb2bd662Svikram 	assert(prev);
8040eb2bd662Svikram 	assert(lp);
8041eb2bd662Svikram 
8042eb2bd662Svikram 	lp->next = prev->next;
8043eb2bd662Svikram 	if (prev->next) {
8044eb2bd662Svikram 		BAM_DPRINTF((D_ADD_LINE_PREV_NEXT, fcn));
8045eb2bd662Svikram 		prev->next->prev = lp;
8046eb2bd662Svikram 	} else {
8047eb2bd662Svikram 		BAM_DPRINTF((D_ADD_LINE_NOT_PREV_NEXT, fcn));
8048eb2bd662Svikram 	}
8049eb2bd662Svikram 	prev->next = lp;
8050eb2bd662Svikram 	lp->prev = prev;
8051eb2bd662Svikram 
8052eb2bd662Svikram 	if (entry->end == prev) {
8053eb2bd662Svikram 		BAM_DPRINTF((D_ADD_LINE_LAST_LINE_IN_ENTRY, fcn));
8054eb2bd662Svikram 		entry->end = lp;
8055eb2bd662Svikram 	}
8056eb2bd662Svikram 	if (mp->end == prev) {
8057eb2bd662Svikram 		assert(lp->next == NULL);
8058eb2bd662Svikram 		mp->end = lp;
8059eb2bd662Svikram 		BAM_DPRINTF((D_ADD_LINE_LAST_LINE_IN_MENU, fcn));
8060eb2bd662Svikram 	}
80617c478bd9Sstevel@tonic-gate }
80627c478bd9Sstevel@tonic-gate 
80638c1b6884Sszhou /*
80648c1b6884Sszhou  * look for matching bootadm entry with specified parameters
80658c1b6884Sszhou  * Here are the rules (based on existing usage):
80668c1b6884Sszhou  * - If title is specified, match on title only
8067eb2bd662Svikram  * - Else, match on root/findroot, kernel, and module.
8068eb2bd662Svikram  *   Note that, if root_opt is non-zero, the absence of
8069eb2bd662Svikram  *   root line is considered a match.
80708c1b6884Sszhou  */
80718c1b6884Sszhou static entry_t *
8072eb2bd662Svikram find_boot_entry(
8073eb2bd662Svikram 	menu_t *mp,
8074eb2bd662Svikram 	char *title,
8075eb2bd662Svikram 	char *kernel,
8076eb2bd662Svikram 	char *findroot,
8077eb2bd662Svikram 	char *root,
8078eb2bd662Svikram 	char *module,
8079eb2bd662Svikram 	int root_opt,
8080eb2bd662Svikram 	int *entry_num)
80818c1b6884Sszhou {
80828c1b6884Sszhou 	int		i;
80838c1b6884Sszhou 	line_t		*lp;
80848c1b6884Sszhou 	entry_t		*ent;
8085eb2bd662Svikram 	const char	*fcn = "find_boot_entry()";
8086eb2bd662Svikram 
8087eb2bd662Svikram 	if (entry_num)
8088eb2bd662Svikram 		*entry_num = BAM_ERROR;
80898c1b6884Sszhou 
80908c1b6884Sszhou 	/* find matching entry */
80918c1b6884Sszhou 	for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) {
80928c1b6884Sszhou 		lp = ent->start;
80938c1b6884Sszhou 
80948c1b6884Sszhou 		/* first line of entry must be bootadm comment */
80958c1b6884Sszhou 		lp = ent->start;
8096ae115bc7Smrj 		if (lp->flags != BAM_COMMENT ||
8097ae115bc7Smrj 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0) {
80988c1b6884Sszhou 			continue;
80998c1b6884Sszhou 		}
81008c1b6884Sszhou 
81018c1b6884Sszhou 		/* advance to title line */
81028c1b6884Sszhou 		lp = lp->next;
81038c1b6884Sszhou 		if (title) {
81048c1b6884Sszhou 			if (lp->flags == BAM_TITLE && lp->arg &&
8105eb2bd662Svikram 			    strcmp(lp->arg, title) == 0) {
8106eb2bd662Svikram 				BAM_DPRINTF((D_MATCHED_TITLE, fcn, title));
81078c1b6884Sszhou 				break;
8108eb2bd662Svikram 			}
8109eb2bd662Svikram 			BAM_DPRINTF((D_NOMATCH_TITLE, fcn, title, lp->arg));
81108c1b6884Sszhou 			continue;	/* check title only */
81118c1b6884Sszhou 		}
81128c1b6884Sszhou 
81138c1b6884Sszhou 		lp = lp->next;	/* advance to root line */
8114843e1988Sjohnlev 		if (lp == NULL) {
8115843e1988Sjohnlev 			continue;
8116eac223ccSWilliam Kucharski 		} else if (lp->cmd != NULL &&
8117eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[FINDROOT_CMD]) == 0) {
8118eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_NULL_FINDROOT",
8119eb2bd662Svikram 			    findroot = NULL);
8120eb2bd662Svikram 			if (findroot == NULL) {
8121eb2bd662Svikram 				BAM_DPRINTF((D_NOMATCH_FINDROOT_NULL,
8122eb2bd662Svikram 				    fcn, lp->arg));
81238c1b6884Sszhou 				continue;
81248c1b6884Sszhou 			}
8125eb2bd662Svikram 			/* findroot command found, try match  */
8126eb2bd662Svikram 			if (strcmp(lp->arg, findroot) != 0) {
8127eb2bd662Svikram 				BAM_DPRINTF((D_NOMATCH_FINDROOT,
8128eb2bd662Svikram 				    fcn, findroot, lp->arg));
8129eb2bd662Svikram 				continue;
8130eb2bd662Svikram 			}
8131eb2bd662Svikram 			BAM_DPRINTF((D_MATCHED_FINDROOT, fcn, findroot));
8132eb2bd662Svikram 			lp = lp->next;	/* advance to kernel line */
8133eac223ccSWilliam Kucharski 		} else if (lp->cmd != NULL &&
8134eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
8135eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_NULL_ROOT", root = NULL);
8136eb2bd662Svikram 			if (root == NULL) {
8137eb2bd662Svikram 				BAM_DPRINTF((D_NOMATCH_ROOT_NULL,
8138eb2bd662Svikram 				    fcn, lp->arg));
8139eb2bd662Svikram 				continue;
8140eb2bd662Svikram 			}
8141eb2bd662Svikram 			/* root cmd found, try match */
8142eb2bd662Svikram 			if (strcmp(lp->arg, root) != 0) {
8143eb2bd662Svikram 				BAM_DPRINTF((D_NOMATCH_ROOT,
8144eb2bd662Svikram 				    fcn, root, lp->arg));
8145eb2bd662Svikram 				continue;
8146eb2bd662Svikram 			}
8147eb2bd662Svikram 			BAM_DPRINTF((D_MATCHED_ROOT, fcn, root));
81488c1b6884Sszhou 			lp = lp->next;	/* advance to kernel line */
81498c1b6884Sszhou 		} else {
8150eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_ROOT_OPT_NO",
8151eb2bd662Svikram 			    root_opt = 0);
8152eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_ROOT_OPT_YES",
8153eb2bd662Svikram 			    root_opt = 1);
81548c1b6884Sszhou 			/* no root command, see if root is optional */
81558c1b6884Sszhou 			if (root_opt == 0) {
8156eb2bd662Svikram 				BAM_DPRINTF((D_NO_ROOT_OPT, fcn));
81578c1b6884Sszhou 				continue;
81588c1b6884Sszhou 			}
8159eb2bd662Svikram 			BAM_DPRINTF((D_ROOT_OPT, fcn));
81608c1b6884Sszhou 		}
81618c1b6884Sszhou 
81628c1b6884Sszhou 		if (lp == NULL || lp->next == NULL) {
81638c1b6884Sszhou 			continue;
81648c1b6884Sszhou 		}
81658c1b6884Sszhou 
8166843e1988Sjohnlev 		if (kernel &&
8167843e1988Sjohnlev 		    (!check_cmd(lp->cmd, KERNEL_CMD, lp->arg, kernel))) {
8168bbcc54bdSEnrico Perla - Sun Microsystems 			if (!(ent->flags & BAM_ENTRY_FAILSAFE) ||
8169bbcc54bdSEnrico Perla - Sun Microsystems 			    !(ent->flags & BAM_ENTRY_DBOOT) ||
8170bbcc54bdSEnrico Perla - Sun Microsystems 			    strcmp(kernel, DIRECT_BOOT_FAILSAFE_LINE) != 0)
81718c1b6884Sszhou 				continue;
8172bbcc54bdSEnrico Perla - Sun Microsystems 
8173bbcc54bdSEnrico Perla - Sun Microsystems 			ent->flags |= BAM_ENTRY_UPGFSKERNEL;
8174bbcc54bdSEnrico Perla - Sun Microsystems 
81758c1b6884Sszhou 		}
8176eb2bd662Svikram 		BAM_DPRINTF((D_KERNEL_MATCH, fcn, kernel, lp->arg));
8177843e1988Sjohnlev 
8178843e1988Sjohnlev 		/*
8179843e1988Sjohnlev 		 * Check for matching module entry (failsafe or normal).
8180843e1988Sjohnlev 		 * If it fails to match, we go around the loop again.
8181843e1988Sjohnlev 		 * For xpv entries, there are two module lines, so we
8182843e1988Sjohnlev 		 * do the check twice.
8183843e1988Sjohnlev 		 */
8184843e1988Sjohnlev 		lp = lp->next;	/* advance to module line */
8185843e1988Sjohnlev 		if (check_cmd(lp->cmd, MODULE_CMD, lp->arg, module) ||
8186843e1988Sjohnlev 		    (((lp = lp->next) != NULL) &&
8187843e1988Sjohnlev 		    check_cmd(lp->cmd, MODULE_CMD, lp->arg, module))) {
8188843e1988Sjohnlev 			/* match found */
8189eb2bd662Svikram 			BAM_DPRINTF((D_MODULE_MATCH, fcn, module, lp->arg));
8190843e1988Sjohnlev 			break;
8191843e1988Sjohnlev 		}
8192bbcc54bdSEnrico Perla - Sun Microsystems 
8193bbcc54bdSEnrico Perla - Sun Microsystems 		if (strcmp(module, FAILSAFE_ARCHIVE) == 0 &&
8194bbcc54bdSEnrico Perla - Sun Microsystems 		    (strcmp(lp->prev->arg, FAILSAFE_ARCHIVE_32) == 0 ||
8195bbcc54bdSEnrico Perla - Sun Microsystems 		    strcmp(lp->prev->arg, FAILSAFE_ARCHIVE_64) == 0)) {
8196bbcc54bdSEnrico Perla - Sun Microsystems 			ent->flags |= BAM_ENTRY_UPGFSMODULE;
8197bbcc54bdSEnrico Perla - Sun Microsystems 			break;
8198bbcc54bdSEnrico Perla - Sun Microsystems 		}
8199bbcc54bdSEnrico Perla - Sun Microsystems 
82008c1b6884Sszhou 	}
82018c1b6884Sszhou 
8202eb2bd662Svikram 	if (ent && entry_num) {
82038c1b6884Sszhou 		*entry_num = i;
8204843e1988Sjohnlev 	}
8205eb2bd662Svikram 
8206eb2bd662Svikram 	if (ent) {
8207eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_RET, fcn, i));
8208eb2bd662Svikram 	} else {
8209eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_RET, fcn, BAM_ERROR));
8210eb2bd662Svikram 	}
82118c1b6884Sszhou 	return (ent);
82128c1b6884Sszhou }
82138c1b6884Sszhou 
82148c1b6884Sszhou static int
8215eb2bd662Svikram update_boot_entry(menu_t *mp, char *title, char *findroot, char *root,
8216eb2bd662Svikram     char *kernel, char *mod_kernel, char *module, int root_opt)
82178c1b6884Sszhou {
8218eb2bd662Svikram 	int		i;
8219eb2bd662Svikram 	int		change_kernel = 0;
82208c1b6884Sszhou 	entry_t		*ent;
82218c1b6884Sszhou 	line_t		*lp;
8222eb2bd662Svikram 	line_t		*tlp;
82238c1b6884Sszhou 	char		linebuf[BAM_MAXLINE];
8224eb2bd662Svikram 	const char	*fcn = "update_boot_entry()";
82258c1b6884Sszhou 
82268c1b6884Sszhou 	/* note: don't match on title, it's updated on upgrade */
8227eb2bd662Svikram 	ent = find_boot_entry(mp, NULL, kernel, findroot, root, module,
8228eb2bd662Svikram 	    root_opt, &i);
8229ae115bc7Smrj 	if ((ent == NULL) && (bam_direct == BAM_DIRECT_DBOOT)) {
8230ae115bc7Smrj 		/*
8231ae115bc7Smrj 		 * We may be upgrading a kernel from multiboot to
8232eb2bd662Svikram 		 * directboot.  Look for a multiboot entry. A multiboot
8233eb2bd662Svikram 		 * entry will not have a findroot line.
8234ae115bc7Smrj 		 */
8235eb2bd662Svikram 		ent = find_boot_entry(mp, NULL, "multiboot", NULL, root,
8236eb2bd662Svikram 		    MULTIBOOT_ARCHIVE, root_opt, &i);
8237ae115bc7Smrj 		if (ent != NULL) {
8238eb2bd662Svikram 			BAM_DPRINTF((D_UPGRADE_FROM_MULTIBOOT, fcn, root));
8239ae115bc7Smrj 			change_kernel = 1;
8240ae115bc7Smrj 		}
8241eb2bd662Svikram 	} else if (ent) {
8242eb2bd662Svikram 		BAM_DPRINTF((D_FOUND_FINDROOT, fcn, findroot));
8243ae115bc7Smrj 	}
82448c1b6884Sszhou 
8245eb2bd662Svikram 	if (ent == NULL) {
8246eb2bd662Svikram 		BAM_DPRINTF((D_ENTRY_NOT_FOUND_CREATING, fcn, findroot));
8247eb2bd662Svikram 		return (add_boot_entry(mp, title, findroot,
824844da779fSWilliam Kucharski 		    kernel, mod_kernel, module, NULL));
8249eb2bd662Svikram 	}
8250eb2bd662Svikram 
8251eb2bd662Svikram 	/* replace title of existing entry and update findroot line */
82528c1b6884Sszhou 	lp = ent->start;
82538c1b6884Sszhou 	lp = lp->next;	/* title line */
82548c1b6884Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
82558c1b6884Sszhou 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
82568c1b6884Sszhou 	free(lp->arg);
82578c1b6884Sszhou 	free(lp->line);
82588c1b6884Sszhou 	lp->arg = s_strdup(title);
82598c1b6884Sszhou 	lp->line = s_strdup(linebuf);
8260eb2bd662Svikram 	BAM_DPRINTF((D_CHANGING_TITLE, fcn, title));
82618c1b6884Sszhou 
8262eb2bd662Svikram 	tlp = lp;	/* title line */
82638c1b6884Sszhou 	lp = lp->next;	/* root line */
8264eb2bd662Svikram 
8265eb2bd662Svikram 	/* if no root or findroot command, create a new line_t */
8266eac223ccSWilliam Kucharski 	if ((lp->cmd != NULL) && (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) != 0 &&
8267eac223ccSWilliam Kucharski 	    strcmp(lp->cmd, menu_cmds[FINDROOT_CMD]) != 0)) {
8268eb2bd662Svikram 		lp = s_calloc(1, sizeof (line_t));
8269eb2bd662Svikram 		bam_add_line(mp, ent, tlp, lp);
8270eb2bd662Svikram 	} else {
8271eac223ccSWilliam Kucharski 		if (lp->cmd != NULL)
8272eb2bd662Svikram 			free(lp->cmd);
8273eac223ccSWilliam Kucharski 
8274eb2bd662Svikram 		free(lp->sep);
8275eb2bd662Svikram 		free(lp->arg);
8276eb2bd662Svikram 		free(lp->line);
82778c1b6884Sszhou 	}
8278ae115bc7Smrj 
8279eb2bd662Svikram 	lp->cmd = s_strdup(menu_cmds[FINDROOT_CMD]);
8280eb2bd662Svikram 	lp->sep = s_strdup(menu_cmds[SEP_CMD]);
8281eb2bd662Svikram 	lp->arg = s_strdup(findroot);
8282eb2bd662Svikram 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
8283eb2bd662Svikram 	    menu_cmds[FINDROOT_CMD], menu_cmds[SEP_CMD], findroot);
8284eb2bd662Svikram 	lp->line = s_strdup(linebuf);
8285eb2bd662Svikram 	BAM_DPRINTF((D_ADDING_FINDROOT_LINE, fcn, findroot));
8286eb2bd662Svikram 
8287eb2bd662Svikram 	/* kernel line */
8288eb2bd662Svikram 	lp = lp->next;
8289eb2bd662Svikram 
8290bbcc54bdSEnrico Perla - Sun Microsystems 	if (ent->flags & BAM_ENTRY_UPGFSKERNEL) {
8291bbcc54bdSEnrico Perla - Sun Microsystems 		char		*params = NULL;
8292bbcc54bdSEnrico Perla - Sun Microsystems 
8293bbcc54bdSEnrico Perla - Sun Microsystems 		params = strstr(lp->line, "-s");
8294bbcc54bdSEnrico Perla - Sun Microsystems 		if (params != NULL)
8295bbcc54bdSEnrico Perla - Sun Microsystems 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s%s",
8296bbcc54bdSEnrico Perla - Sun Microsystems 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
8297bbcc54bdSEnrico Perla - Sun Microsystems 			    kernel, params+2);
8298bbcc54bdSEnrico Perla - Sun Microsystems 		else
8299bbcc54bdSEnrico Perla - Sun Microsystems 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
8300bbcc54bdSEnrico Perla - Sun Microsystems 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
8301bbcc54bdSEnrico Perla - Sun Microsystems 			    kernel);
8302bbcc54bdSEnrico Perla - Sun Microsystems 
8303eac223ccSWilliam Kucharski 		if (lp->cmd != NULL)
8304bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->cmd);
8305eac223ccSWilliam Kucharski 
8306bbcc54bdSEnrico Perla - Sun Microsystems 		free(lp->arg);
8307bbcc54bdSEnrico Perla - Sun Microsystems 		free(lp->line);
8308bbcc54bdSEnrico Perla - Sun Microsystems 		lp->cmd = s_strdup(menu_cmds[KERNEL_DOLLAR_CMD]);
8309bbcc54bdSEnrico Perla - Sun Microsystems 		lp->arg = s_strdup(strstr(linebuf, "/"));
8310bbcc54bdSEnrico Perla - Sun Microsystems 		lp->line = s_strdup(linebuf);
8311bbcc54bdSEnrico Perla - Sun Microsystems 		ent->flags &= ~BAM_ENTRY_UPGFSKERNEL;
8312bbcc54bdSEnrico Perla - Sun Microsystems 		BAM_DPRINTF((D_ADDING_KERNEL_DOLLAR, fcn, lp->prev->cmd));
8313bbcc54bdSEnrico Perla - Sun Microsystems 	}
8314bbcc54bdSEnrico Perla - Sun Microsystems 
8315ae115bc7Smrj 	if (change_kernel) {
8316ae115bc7Smrj 		/*
8317ae115bc7Smrj 		 * We're upgrading from multiboot to directboot.
8318ae115bc7Smrj 		 */
8319eac223ccSWilliam Kucharski 		if (lp->cmd != NULL &&
8320eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[KERNEL_CMD]) == 0) {
8321ae115bc7Smrj 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
8322ae115bc7Smrj 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
8323ae115bc7Smrj 			    kernel);
8324eb2bd662Svikram 			free(lp->cmd);
8325ae115bc7Smrj 			free(lp->arg);
8326ae115bc7Smrj 			free(lp->line);
8327eb2bd662Svikram 			lp->cmd = s_strdup(menu_cmds[KERNEL_DOLLAR_CMD]);
8328ae115bc7Smrj 			lp->arg = s_strdup(kernel);
8329ae115bc7Smrj 			lp->line = s_strdup(linebuf);
8330ae115bc7Smrj 			lp = lp->next;
8331eb2bd662Svikram 			BAM_DPRINTF((D_ADDING_KERNEL_DOLLAR, fcn, kernel));
8332ae115bc7Smrj 		}
8333eac223ccSWilliam Kucharski 		if (lp->cmd != NULL &&
8334eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) {
8335ae115bc7Smrj 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
8336ae115bc7Smrj 			    menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD],
8337ae115bc7Smrj 			    module);
8338eb2bd662Svikram 			free(lp->cmd);
8339ae115bc7Smrj 			free(lp->arg);
8340ae115bc7Smrj 			free(lp->line);
8341eb2bd662Svikram 			lp->cmd = s_strdup(menu_cmds[MODULE_DOLLAR_CMD]);
8342ae115bc7Smrj 			lp->arg = s_strdup(module);
8343ae115bc7Smrj 			lp->line = s_strdup(linebuf);
8344ae115bc7Smrj 			lp = lp->next;
8345eb2bd662Svikram 			BAM_DPRINTF((D_ADDING_MODULE_DOLLAR, fcn, module));
8346ae115bc7Smrj 		}
8347ae115bc7Smrj 	}
8348bbcc54bdSEnrico Perla - Sun Microsystems 
8349bbcc54bdSEnrico Perla - Sun Microsystems 	/* module line */
8350bbcc54bdSEnrico Perla - Sun Microsystems 	lp = lp->next;
8351bbcc54bdSEnrico Perla - Sun Microsystems 
8352bbcc54bdSEnrico Perla - Sun Microsystems 	if (ent->flags & BAM_ENTRY_UPGFSMODULE) {
8353eac223ccSWilliam Kucharski 		if (lp->cmd != NULL &&
8354eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) {
8355bbcc54bdSEnrico Perla - Sun Microsystems 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
8356bbcc54bdSEnrico Perla - Sun Microsystems 			    menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD],
8357bbcc54bdSEnrico Perla - Sun Microsystems 			    module);
8358bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->cmd);
8359bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->arg);
8360bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->line);
8361bbcc54bdSEnrico Perla - Sun Microsystems 			lp->cmd = s_strdup(menu_cmds[MODULE_DOLLAR_CMD]);
8362bbcc54bdSEnrico Perla - Sun Microsystems 			lp->arg = s_strdup(module);
8363bbcc54bdSEnrico Perla - Sun Microsystems 			lp->line = s_strdup(linebuf);
8364bbcc54bdSEnrico Perla - Sun Microsystems 			lp = lp->next;
8365bbcc54bdSEnrico Perla - Sun Microsystems 			ent->flags &= ~BAM_ENTRY_UPGFSMODULE;
8366bbcc54bdSEnrico Perla - Sun Microsystems 			BAM_DPRINTF((D_ADDING_MODULE_DOLLAR, fcn, module));
8367bbcc54bdSEnrico Perla - Sun Microsystems 		}
8368bbcc54bdSEnrico Perla - Sun Microsystems 	}
8369bbcc54bdSEnrico Perla - Sun Microsystems 
8370eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_RET, fcn, i));
83718c1b6884Sszhou 	return (i);
83728c1b6884Sszhou }
83738c1b6884Sszhou 
8374eb2bd662Svikram int
8375eb2bd662Svikram root_optional(char *osroot, char *menu_root)
8376eb2bd662Svikram {
8377eb2bd662Svikram 	char			*ospecial;
8378eb2bd662Svikram 	char			*mspecial;
8379eb2bd662Svikram 	char			*slash;
8380eb2bd662Svikram 	int			root_opt;
8381eb2bd662Svikram 	int			ret1;
8382eb2bd662Svikram 	int			ret2;
8383eb2bd662Svikram 	const char		*fcn = "root_optional()";
8384eb2bd662Svikram 
8385eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, menu_root));
8386eb2bd662Svikram 
8387eb2bd662Svikram 	/*
8388eb2bd662Svikram 	 * For all filesystems except ZFS, a straight compare of osroot
8389eb2bd662Svikram 	 * and menu_root will tell us if root is optional.
8390eb2bd662Svikram 	 * For ZFS, the situation is complicated by the fact that
8391eb2bd662Svikram 	 * menu_root and osroot are always different
8392eb2bd662Svikram 	 */
8393eb2bd662Svikram 	ret1 = is_zfs(osroot);
8394eb2bd662Svikram 	ret2 = is_zfs(menu_root);
8395eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPT_NOT_ZFS", ret1 = 0);
8396eb2bd662Svikram 	if (!ret1 || !ret2) {
8397eb2bd662Svikram 		BAM_DPRINTF((D_ROOT_OPT_NOT_ZFS, fcn, osroot, menu_root));
8398eb2bd662Svikram 		root_opt = (strcmp(osroot, menu_root) == 0);
8399eb2bd662Svikram 		goto out;
8400eb2bd662Svikram 	}
8401eb2bd662Svikram 
8402eb2bd662Svikram 	ospecial = get_special(osroot);
8403eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_OSPECIAL", ospecial = NULL);
8404eb2bd662Svikram 	if (ospecial == NULL) {
8405eb2bd662Svikram 		bam_error(GET_OSROOT_SPECIAL_ERR, osroot);
8406eb2bd662Svikram 		return (0);
8407eb2bd662Svikram 	}
8408eb2bd662Svikram 	BAM_DPRINTF((D_ROOT_OPTIONAL_OSPECIAL, fcn, ospecial, osroot));
8409eb2bd662Svikram 
8410eb2bd662Svikram 	mspecial = get_special(menu_root);
8411eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_MSPECIAL", mspecial = NULL);
8412eb2bd662Svikram 	if (mspecial == NULL) {
8413eb2bd662Svikram 		bam_error(GET_MENU_ROOT_SPECIAL_ERR, menu_root);
8414eb2bd662Svikram 		free(ospecial);
8415eb2bd662Svikram 		return (0);
8416eb2bd662Svikram 	}
8417eb2bd662Svikram 	BAM_DPRINTF((D_ROOT_OPTIONAL_MSPECIAL, fcn, mspecial, menu_root));
8418eb2bd662Svikram 
8419eb2bd662Svikram 	slash = strchr(ospecial, '/');
8420eb2bd662Svikram 	if (slash)
8421eb2bd662Svikram 		*slash = '\0';
8422eb2bd662Svikram 	BAM_DPRINTF((D_ROOT_OPTIONAL_FIXED_OSPECIAL, fcn, ospecial, osroot));
8423eb2bd662Svikram 
8424eb2bd662Svikram 	root_opt = (strcmp(ospecial, mspecial) == 0);
8425eb2bd662Svikram 
8426eb2bd662Svikram 	free(ospecial);
8427eb2bd662Svikram 	free(mspecial);
8428eb2bd662Svikram 
8429eb2bd662Svikram out:
8430eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_NO", root_opt = 0);
8431eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_YES", root_opt = 1);
8432eb2bd662Svikram 	if (root_opt) {
8433eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
8434eb2bd662Svikram 	} else {
8435eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
8436eb2bd662Svikram 	}
8437eb2bd662Svikram 
8438eb2bd662Svikram 	return (root_opt);
8439eb2bd662Svikram }
8440eb2bd662Svikram 
84417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
84427c478bd9Sstevel@tonic-gate static error_t
8443eb2bd662Svikram update_entry(menu_t *mp, char *menu_root, char *osdev)
84447c478bd9Sstevel@tonic-gate {
84457c478bd9Sstevel@tonic-gate 	int		entry;
8446eb2bd662Svikram 	char		*grubsign;
8447eb2bd662Svikram 	char		*grubroot;
8448eb2bd662Svikram 	char		*title;
8449eb2bd662Svikram 	char		osroot[PATH_MAX];
8450eb2bd662Svikram 	char		*failsafe_kernel = NULL;
84518c1b6884Sszhou 	struct stat	sbuf;
84528c1b6884Sszhou 	char		failsafe[256];
8453bbcc54bdSEnrico Perla - Sun Microsystems 	char		failsafe_64[256];
8454eb2bd662Svikram 	int		ret;
8455eb2bd662Svikram 	const char	*fcn = "update_entry()";
84567c478bd9Sstevel@tonic-gate 
84577c478bd9Sstevel@tonic-gate 	assert(mp);
8458eb2bd662Svikram 	assert(menu_root);
8459eb2bd662Svikram 	assert(osdev);
8460eb2bd662Svikram 	assert(bam_root);
84617c478bd9Sstevel@tonic-gate 
8462eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY3, fcn, menu_root, osdev, bam_root));
8463eb2bd662Svikram 
8464eb2bd662Svikram 	(void) strlcpy(osroot, bam_root, sizeof (osroot));
8465eb2bd662Svikram 
84667c478bd9Sstevel@tonic-gate 	title = get_title(osroot);
8467eb2bd662Svikram 	assert(title);
84687c478bd9Sstevel@tonic-gate 
8469eb2bd662Svikram 	grubsign = get_grubsign(osroot, osdev);
8470eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBSIGN_FAIL", grubsign = NULL);
8471eb2bd662Svikram 	if (grubsign == NULL) {
8472eb2bd662Svikram 		bam_error(GET_GRUBSIGN_ERROR, osroot, osdev);
84737c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
84747c478bd9Sstevel@tonic-gate 	}
8475eb2bd662Svikram 
8476eb2bd662Svikram 	/*
8477eb2bd662Svikram 	 * It is not a fatal error if get_grubroot() fails
8478eb2bd662Svikram 	 * We no longer rely on biosdev to populate the
8479eb2bd662Svikram 	 * menu
8480eb2bd662Svikram 	 */
8481eb2bd662Svikram 	grubroot = get_grubroot(osroot, osdev, menu_root);
8482eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBROOT_FAIL", grubroot = NULL);
8483eb2bd662Svikram 	if (grubroot) {
8484eb2bd662Svikram 		BAM_DPRINTF((D_GET_GRUBROOT_SUCCESS,
8485eb2bd662Svikram 		    fcn, osroot, osdev, menu_root));
8486eb2bd662Svikram 	} else {
8487eb2bd662Svikram 		BAM_DPRINTF((D_GET_GRUBROOT_FAILURE,
8488eb2bd662Svikram 		    fcn, osroot, osdev, menu_root));
84897c478bd9Sstevel@tonic-gate 	}
84907c478bd9Sstevel@tonic-gate 
84917c478bd9Sstevel@tonic-gate 	/* add the entry for normal Solaris */
8492eb2bd662Svikram 	INJECT_ERROR1("UPDATE_ENTRY_MULTIBOOT",
8493eb2bd662Svikram 	    bam_direct = BAM_DIRECT_MULTIBOOT);
8494ae115bc7Smrj 	if (bam_direct == BAM_DIRECT_DBOOT) {
8495eb2bd662Svikram 		entry = update_boot_entry(mp, title, grubsign, grubroot,
8496e7cbe64fSgw25295 		    (bam_zfs ? DIRECT_BOOT_KERNEL_ZFS : DIRECT_BOOT_KERNEL),
8497eb2bd662Svikram 		    NULL, DIRECT_BOOT_ARCHIVE,
8498eb2bd662Svikram 		    root_optional(osroot, menu_root));
8499eb2bd662Svikram 		BAM_DPRINTF((D_UPDATED_BOOT_ENTRY, fcn, bam_zfs, grubsign));
8500843e1988Sjohnlev 		if ((entry != BAM_ERROR) && (bam_is_hv == BAM_HV_PRESENT)) {
8501eb2bd662Svikram 			(void) update_boot_entry(mp, NEW_HV_ENTRY, grubsign,
8502eb2bd662Svikram 			    grubroot, XEN_MENU, bam_zfs ?
8503eb2bd662Svikram 			    XEN_KERNEL_MODULE_LINE_ZFS : XEN_KERNEL_MODULE_LINE,
8504eb2bd662Svikram 			    DIRECT_BOOT_ARCHIVE,
8505eb2bd662Svikram 			    root_optional(osroot, menu_root));
8506eb2bd662Svikram 			BAM_DPRINTF((D_UPDATED_HV_ENTRY,
8507eb2bd662Svikram 			    fcn, bam_zfs, grubsign));
8508ae115bc7Smrj 		}
8509843e1988Sjohnlev 	} else {
8510eb2bd662Svikram 		entry = update_boot_entry(mp, title, grubsign, grubroot,
8511eb2bd662Svikram 		    MULTI_BOOT, NULL, MULTIBOOT_ARCHIVE,
8512eb2bd662Svikram 		    root_optional(osroot, menu_root));
8513eb2bd662Svikram 
8514eb2bd662Svikram 		BAM_DPRINTF((D_UPDATED_MULTIBOOT_ENTRY, fcn, grubsign));
8515843e1988Sjohnlev 	}
85167c478bd9Sstevel@tonic-gate 
8517843e1988Sjohnlev 	/*
8518843e1988Sjohnlev 	 * Add the entry for failsafe archive.  On a bfu'd system, the
8519843e1988Sjohnlev 	 * failsafe may be different than the installed kernel.
8520843e1988Sjohnlev 	 */
8521eb2bd662Svikram 	(void) snprintf(failsafe, sizeof (failsafe), "%s%s",
8522bbcc54bdSEnrico Perla - Sun Microsystems 	    osroot, FAILSAFE_ARCHIVE_32);
8523bbcc54bdSEnrico Perla - Sun Microsystems 	(void) snprintf(failsafe_64, sizeof (failsafe_64), "%s%s",
8524bbcc54bdSEnrico Perla - Sun Microsystems 	    osroot, FAILSAFE_ARCHIVE_64);
8525bbcc54bdSEnrico Perla - Sun Microsystems 
8526bbcc54bdSEnrico Perla - Sun Microsystems 	/*
8527bbcc54bdSEnrico Perla - Sun Microsystems 	 * Check if at least one of the two archives exists
8528bbcc54bdSEnrico Perla - Sun Microsystems 	 * Using $ISADIR as the default line, we have an entry which works
8529bbcc54bdSEnrico Perla - Sun Microsystems 	 * for both the cases.
8530bbcc54bdSEnrico Perla - Sun Microsystems 	 */
8531bbcc54bdSEnrico Perla - Sun Microsystems 
8532bbcc54bdSEnrico Perla - Sun Microsystems 	if (stat(failsafe, &sbuf) == 0 || stat(failsafe_64, &sbuf) == 0) {
853360d0a590Srscott 
853460d0a590Srscott 		/* Figure out where the kernel line should point */
853560d0a590Srscott 		(void) snprintf(failsafe, sizeof (failsafe), "%s%s", osroot,
8536bbcc54bdSEnrico Perla - Sun Microsystems 		    DIRECT_BOOT_FAILSAFE_32);
8537bbcc54bdSEnrico Perla - Sun Microsystems 		(void) snprintf(failsafe_64, sizeof (failsafe_64), "%s%s",
8538bbcc54bdSEnrico Perla - Sun Microsystems 		    osroot, DIRECT_BOOT_FAILSAFE_64);
8539bbcc54bdSEnrico Perla - Sun Microsystems 		if (stat(failsafe, &sbuf) == 0 ||
8540bbcc54bdSEnrico Perla - Sun Microsystems 		    stat(failsafe_64, &sbuf) == 0) {
8541963390b4Svikram 			failsafe_kernel = DIRECT_BOOT_FAILSAFE_LINE;
854260d0a590Srscott 		} else {
854360d0a590Srscott 			(void) snprintf(failsafe, sizeof (failsafe), "%s%s",
854460d0a590Srscott 			    osroot, MULTI_BOOT_FAILSAFE);
854560d0a590Srscott 			if (stat(failsafe, &sbuf) == 0) {
854660d0a590Srscott 				failsafe_kernel = MULTI_BOOT_FAILSAFE_LINE;
854760d0a590Srscott 			}
854860d0a590Srscott 		}
854960d0a590Srscott 		if (failsafe_kernel != NULL) {
8550eb2bd662Svikram 			(void) update_boot_entry(mp, FAILSAFE_TITLE, grubsign,
8551eb2bd662Svikram 			    grubroot, failsafe_kernel, NULL, FAILSAFE_ARCHIVE,
8552eb2bd662Svikram 			    root_optional(osroot, menu_root));
8553eb2bd662Svikram 			BAM_DPRINTF((D_UPDATED_FAILSAFE_ENTRY, fcn,
8554eb2bd662Svikram 			    failsafe_kernel));
855560d0a590Srscott 		}
8556ae115bc7Smrj 	}
8557eb2bd662Svikram 	free(grubroot);
85587c478bd9Sstevel@tonic-gate 
8559eb2bd662Svikram 	INJECT_ERROR1("UPDATE_ENTRY_ERROR", entry = BAM_ERROR);
85607c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
8561eb2bd662Svikram 		bam_error(FAILED_TO_ADD_BOOT_ENTRY, title, grubsign);
8562eb2bd662Svikram 		free(grubsign);
85637c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
85647c478bd9Sstevel@tonic-gate 	}
8565eb2bd662Svikram 	free(grubsign);
8566eb2bd662Svikram 
8567eb2bd662Svikram 	update_numbering(mp);
8568eb2bd662Svikram 	ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
8569eb2bd662Svikram 	INJECT_ERROR1("SET_DEFAULT_ERROR", ret = BAM_ERROR);
8570eb2bd662Svikram 	if (ret == BAM_ERROR) {
8571eb2bd662Svikram 		bam_error(SET_DEFAULT_FAILED, entry);
8572eb2bd662Svikram 	}
8573eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
85747c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
85757c478bd9Sstevel@tonic-gate }
85767c478bd9Sstevel@tonic-gate 
85778c1b6884Sszhou static void
8578ae115bc7Smrj save_default_entry(menu_t *mp, const char *which)
85798c1b6884Sszhou {
8580eb2bd662Svikram 	int		lineNum;
8581eb2bd662Svikram 	int		entryNum;
85828c1b6884Sszhou 	int		entry = 0;	/* default is 0 */
85838c1b6884Sszhou 	char		linebuf[BAM_MAXLINE];
85848c1b6884Sszhou 	line_t		*lp = mp->curdefault;
8585eb2bd662Svikram 	const char	*fcn = "save_default_entry()";
85868c1b6884Sszhou 
8587bff269d0Svikram 	if (mp->start) {
8588bff269d0Svikram 		lineNum = mp->end->lineNum;
8589bff269d0Svikram 		entryNum = mp->end->entryNum;
8590bff269d0Svikram 	} else {
8591bff269d0Svikram 		lineNum = LINE_INIT;
8592bff269d0Svikram 		entryNum = ENTRY_INIT;
8593bff269d0Svikram 	}
8594bff269d0Svikram 
85958c1b6884Sszhou 	if (lp)
85968c1b6884Sszhou 		entry = s_strtol(lp->arg);
85978c1b6884Sszhou 
8598ae115bc7Smrj 	(void) snprintf(linebuf, sizeof (linebuf), "#%s%d", which, entry);
8599eb2bd662Svikram 	BAM_DPRINTF((D_SAVING_DEFAULT_TO, fcn, linebuf));
86008c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
8601eb2bd662Svikram 	BAM_DPRINTF((D_SAVED_DEFAULT_TO, fcn, lineNum, entryNum));
86028c1b6884Sszhou }
86038c1b6884Sszhou 
86048c1b6884Sszhou static void
8605ae115bc7Smrj restore_default_entry(menu_t *mp, const char *which, line_t *lp)
86068c1b6884Sszhou {
86078c1b6884Sszhou 	int		entry;
86088c1b6884Sszhou 	char		*str;
8609eb2bd662Svikram 	const char	*fcn = "restore_default_entry()";
86108c1b6884Sszhou 
8611eb2bd662Svikram 	if (lp == NULL) {
8612eb2bd662Svikram 		BAM_DPRINTF((D_RESTORE_DEFAULT_NULL, fcn));
86138c1b6884Sszhou 		return;		/* nothing to restore */
8614eb2bd662Svikram 	}
8615eb2bd662Svikram 
8616eb2bd662Svikram 	BAM_DPRINTF((D_RESTORE_DEFAULT_STR, fcn, which));
86178c1b6884Sszhou 
8618ae115bc7Smrj 	str = lp->arg + strlen(which);
86198c1b6884Sszhou 	entry = s_strtol(str);
86208c1b6884Sszhou 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
86218c1b6884Sszhou 
8622eb2bd662Svikram 	BAM_DPRINTF((D_RESTORED_DEFAULT_TO, fcn, entry));
8623eb2bd662Svikram 
86248c1b6884Sszhou 	/* delete saved old default line */
86258c1b6884Sszhou 	unlink_line(mp, lp);
86268c1b6884Sszhou 	line_free(lp);
86278c1b6884Sszhou }
86288c1b6884Sszhou 
86297c478bd9Sstevel@tonic-gate /*
86307c478bd9Sstevel@tonic-gate  * This function is for supporting reboot with args.
86317c478bd9Sstevel@tonic-gate  * The opt value can be:
86327c478bd9Sstevel@tonic-gate  * NULL		delete temp entry, if present
8633eb2bd662Svikram  * entry=<n>	switches default entry to <n>
86347c478bd9Sstevel@tonic-gate  * else		treated as boot-args and setup a temperary menu entry
86357c478bd9Sstevel@tonic-gate  *		and make it the default
8636eb2bd662Svikram  * Note that we are always rebooting the current OS instance
8637eb2bd662Svikram  * so osroot == / always.
86387c478bd9Sstevel@tonic-gate  */
86397c478bd9Sstevel@tonic-gate #define	REBOOT_TITLE	"Solaris_reboot_transient"
86407c478bd9Sstevel@tonic-gate 
86418c1b6884Sszhou /*ARGSUSED*/
86427c478bd9Sstevel@tonic-gate static error_t
8643eb2bd662Svikram update_temp(menu_t *mp, char *dummy, char *opt)
86447c478bd9Sstevel@tonic-gate {
86457c478bd9Sstevel@tonic-gate 	int		entry;
8646eb2bd662Svikram 	char		*osdev;
8647eb2bd662Svikram 	char		*fstype;
8648eb2bd662Svikram 	char		*sign;
8649eb2bd662Svikram 	char		*opt_ptr;
8650eb2bd662Svikram 	char		*path;
8651ae115bc7Smrj 	char		kernbuf[BUFSIZ];
8652ae115bc7Smrj 	char		args_buf[BUFSIZ];
8653eb2bd662Svikram 	char		signbuf[PATH_MAX];
8654eb2bd662Svikram 	int		ret;
8655eb2bd662Svikram 	const char	*fcn = "update_temp()";
86567c478bd9Sstevel@tonic-gate 
86577c478bd9Sstevel@tonic-gate 	assert(mp);
8658eb2bd662Svikram 	assert(dummy == NULL);
8659eb2bd662Svikram 
8660eb2bd662Svikram 	/* opt can be NULL */
8661eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, opt ? opt : "<NULL>"));
8662eb2bd662Svikram 	BAM_DPRINTF((D_BAM_ROOT, fcn, bam_alt_root, bam_root));
8663eb2bd662Svikram 
8664eb2bd662Svikram 	if (bam_alt_root || bam_rootlen != 1 ||
8665eb2bd662Svikram 	    strcmp(bam_root, "/") != 0 ||
8666eb2bd662Svikram 	    strcmp(rootbuf, "/") != 0) {
8667eb2bd662Svikram 		bam_error(ALT_ROOT_INVALID, bam_root);
8668eb2bd662Svikram 		return (BAM_ERROR);
8669eb2bd662Svikram 	}
86707c478bd9Sstevel@tonic-gate 
86718c1b6884Sszhou 	/* If no option, delete exiting reboot menu entry */
86728c1b6884Sszhou 	if (opt == NULL) {
8673eb2bd662Svikram 		entry_t		*ent;
8674eb2bd662Svikram 		BAM_DPRINTF((D_OPT_NULL, fcn));
8675eb2bd662Svikram 		ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL,
8676eb2bd662Svikram 		    NULL, NULL, 0, &entry);
8677eb2bd662Svikram 		if (ent == NULL) {	/* not found is ok */
8678eb2bd662Svikram 			BAM_DPRINTF((D_TRANSIENT_NOTFOUND, fcn));
86798c1b6884Sszhou 			return (BAM_SUCCESS);
8680eb2bd662Svikram 		}
868144da779fSWilliam Kucharski 		(void) delete_boot_entry(mp, entry, DBE_PRINTERR);
8682ae115bc7Smrj 		restore_default_entry(mp, BAM_OLDDEF, mp->olddefault);
8683ae115bc7Smrj 		mp->olddefault = NULL;
8684eb2bd662Svikram 		BAM_DPRINTF((D_RESTORED_DEFAULT, fcn));
8685eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
86868c1b6884Sszhou 		return (BAM_WRITE);
86878c1b6884Sszhou 	}
86888c1b6884Sszhou 
86898c1b6884Sszhou 	/* if entry= is specified, set the default entry */
8690eb2bd662Svikram 	if (strncmp(opt, "entry=", strlen("entry=")) == 0) {
8691eb2bd662Svikram 		int entryNum = s_strtol(opt + strlen("entry="));
8692eb2bd662Svikram 		BAM_DPRINTF((D_ENTRY_EQUALS, fcn, opt));
8693eb2bd662Svikram 		if (selector(mp, opt, &entry, NULL) == BAM_SUCCESS) {
86947c478bd9Sstevel@tonic-gate 			/* this is entry=# option */
8695eb2bd662Svikram 			ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
8696eb2bd662Svikram 			BAM_DPRINTF((D_ENTRY_SET_IS, fcn, entry, ret));
8697eb2bd662Svikram 			return (ret);
8698eb2bd662Svikram 		} else {
8699eb2bd662Svikram 			bam_error(SET_DEFAULT_FAILED, entryNum);
8700eb2bd662Svikram 			return (BAM_ERROR);
8701eb2bd662Svikram 		}
87027c478bd9Sstevel@tonic-gate 	}
87037c478bd9Sstevel@tonic-gate 
87047c478bd9Sstevel@tonic-gate 	/*
8705eb2bd662Svikram 	 * add a new menu entry based on opt and make it the default
8706b610f78eSvikram 	 */
8707eb2bd662Svikram 
8708eb2bd662Svikram 	fstype = get_fstype("/");
8709eb2bd662Svikram 	INJECT_ERROR1("REBOOT_FSTYPE_NULL", fstype = NULL);
8710eb2bd662Svikram 	if (fstype == NULL) {
8711eb2bd662Svikram 		bam_error(REBOOT_FSTYPE_FAILED);
8712eb2bd662Svikram 		return (BAM_ERROR);
87137c478bd9Sstevel@tonic-gate 	}
8714eb2bd662Svikram 
8715eb2bd662Svikram 	osdev = get_special("/");
8716eb2bd662Svikram 	INJECT_ERROR1("REBOOT_SPECIAL_NULL", osdev = NULL);
8717eb2bd662Svikram 	if (osdev == NULL) {
8718eb2bd662Svikram 		free(fstype);
8719eb2bd662Svikram 		bam_error(REBOOT_SPECIAL_FAILED);
8720eb2bd662Svikram 		return (BAM_ERROR);
8721b610f78eSvikram 	}
8722eb2bd662Svikram 
8723eb2bd662Svikram 	sign = find_existing_sign("/", osdev, fstype);
8724eb2bd662Svikram 	INJECT_ERROR1("REBOOT_SIGN_NULL", sign = NULL);
8725eb2bd662Svikram 	if (sign == NULL) {
8726eb2bd662Svikram 		free(fstype);
8727eb2bd662Svikram 		free(osdev);
8728eb2bd662Svikram 		bam_error(REBOOT_SIGN_FAILED);
8729eb2bd662Svikram 		return (BAM_ERROR);
8730eb2bd662Svikram 	}
8731eb2bd662Svikram 
8732eb2bd662Svikram 	free(osdev);
8733eb2bd662Svikram 	(void) strlcpy(signbuf, sign, sizeof (signbuf));
8734eb2bd662Svikram 	free(sign);
8735eb2bd662Svikram 
8736eb2bd662Svikram 	assert(strchr(signbuf, '(') == NULL && strchr(signbuf, ',') == NULL &&
8737eb2bd662Svikram 	    strchr(signbuf, ')') == NULL);
8738eb2bd662Svikram 
8739eb2bd662Svikram 	/*
8740eb2bd662Svikram 	 * There is no alternate root while doing reboot with args
8741eb2bd662Svikram 	 * This version of bootadm is only delivered with a DBOOT
8742eb2bd662Svikram 	 * version of Solaris.
8743eb2bd662Svikram 	 */
8744eb2bd662Svikram 	INJECT_ERROR1("REBOOT_NOT_DBOOT", bam_direct = BAM_DIRECT_MULTIBOOT);
8745eb2bd662Svikram 	if (bam_direct != BAM_DIRECT_DBOOT) {
87469bd6d269SLin Ling 		free(fstype);
8747eb2bd662Svikram 		bam_error(REBOOT_DIRECT_FAILED);
87487c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
87497c478bd9Sstevel@tonic-gate 	}
87507c478bd9Sstevel@tonic-gate 
87517c478bd9Sstevel@tonic-gate 	/* add an entry for Solaris reboot */
8752ae115bc7Smrj 	if (opt[0] == '-') {
8753ae115bc7Smrj 		/* It's an option - first see if boot-file is set */
8754eb2bd662Svikram 		ret = get_kernel(mp, KERNEL_CMD, kernbuf, sizeof (kernbuf));
8755eb2bd662Svikram 		INJECT_ERROR1("REBOOT_GET_KERNEL", ret = BAM_ERROR);
8756eb2bd662Svikram 		if (ret != BAM_SUCCESS) {
87579bd6d269SLin Ling 			free(fstype);
8758eb2bd662Svikram 			bam_error(REBOOT_GET_KERNEL_FAILED);
8759ae115bc7Smrj 			return (BAM_ERROR);
8760eb2bd662Svikram 		}
8761ae115bc7Smrj 		if (kernbuf[0] == '\0')
8762eb2bd662Svikram 			(void) strlcpy(kernbuf, DIRECT_BOOT_KERNEL,
8763eb2bd662Svikram 			    sizeof (kernbuf));
87649bd6d269SLin Ling 		/*
87659bd6d269SLin Ling 		 * If this is a zfs file system and kernbuf does not
87669bd6d269SLin Ling 		 * have "-B $ZFS-BOOTFS" string yet, add it.
87679bd6d269SLin Ling 		 */
87689bd6d269SLin Ling 		if (strcmp(fstype, "zfs") == 0 && !strstr(kernbuf, ZFS_BOOT)) {
87699bd6d269SLin Ling 			(void) strlcat(kernbuf, " ", sizeof (kernbuf));
87709bd6d269SLin Ling 			(void) strlcat(kernbuf, ZFS_BOOT, sizeof (kernbuf));
87719bd6d269SLin Ling 		}
8772eb2bd662Svikram 		(void) strlcat(kernbuf, " ", sizeof (kernbuf));
8773eb2bd662Svikram 		(void) strlcat(kernbuf, opt, sizeof (kernbuf));
8774eb2bd662Svikram 		BAM_DPRINTF((D_REBOOT_OPTION, fcn, kernbuf));
8775ae115bc7Smrj 	} else if (opt[0] == '/') {
8776455710d3Srscott 		/* It's a full path, so write it out. */
8777eb2bd662Svikram 		(void) strlcpy(kernbuf, opt, sizeof (kernbuf));
8778455710d3Srscott 
8779455710d3Srscott 		/*
8780455710d3Srscott 		 * If someone runs:
8781455710d3Srscott 		 *
8782455710d3Srscott 		 *	# eeprom boot-args='-kd'
8783455710d3Srscott 		 *	# reboot /platform/i86pc/kernel/unix
8784455710d3Srscott 		 *
8785455710d3Srscott 		 * we want to use the boot-args as part of the boot
8786455710d3Srscott 		 * line.  On the other hand, if someone runs:
8787455710d3Srscott 		 *
8788455710d3Srscott 		 *	# reboot "/platform/i86pc/kernel/unix -kd"
8789455710d3Srscott 		 *
8790455710d3Srscott 		 * we don't need to mess with boot-args.  If there's
8791455710d3Srscott 		 * no space in the options string, assume we're in the
8792455710d3Srscott 		 * first case.
8793455710d3Srscott 		 */
8794455710d3Srscott 		if (strchr(opt, ' ') == NULL) {
8795eb2bd662Svikram 			ret = get_kernel(mp, ARGS_CMD, args_buf,
8796eb2bd662Svikram 			    sizeof (args_buf));
8797eb2bd662Svikram 			INJECT_ERROR1("REBOOT_GET_ARGS", ret = BAM_ERROR);
8798eb2bd662Svikram 			if (ret != BAM_SUCCESS) {
87999bd6d269SLin Ling 				free(fstype);
8800eb2bd662Svikram 				bam_error(REBOOT_GET_ARGS_FAILED);
8801455710d3Srscott 				return (BAM_ERROR);
8802eb2bd662Svikram 			}
8803455710d3Srscott 
8804455710d3Srscott 			if (args_buf[0] != '\0') {
8805eb2bd662Svikram 				(void) strlcat(kernbuf, " ", sizeof (kernbuf));
8806455710d3Srscott 				(void) strlcat(kernbuf, args_buf,
8807eb2bd662Svikram 				    sizeof (kernbuf));
8808455710d3Srscott 			}
8809455710d3Srscott 		}
8810eb2bd662Svikram 		BAM_DPRINTF((D_REBOOT_ABSPATH, fcn, kernbuf));
8811ae115bc7Smrj 	} else {
8812455710d3Srscott 		/*
8813455710d3Srscott 		 * It may be a partial path, or it may be a partial
8814455710d3Srscott 		 * path followed by options.  Assume that only options
8815455710d3Srscott 		 * follow a space.  If someone sends us a kernel path
8816455710d3Srscott 		 * that includes a space, they deserve to be broken.
8817455710d3Srscott 		 */
8818455710d3Srscott 		opt_ptr = strchr(opt, ' ');
8819455710d3Srscott 		if (opt_ptr != NULL) {
8820455710d3Srscott 			*opt_ptr = '\0';
8821455710d3Srscott 		}
8822455710d3Srscott 
8823ae115bc7Smrj 		path = expand_path(opt);
8824ae115bc7Smrj 		if (path != NULL) {
8825eb2bd662Svikram 			(void) strlcpy(kernbuf, path, sizeof (kernbuf));
8826ae115bc7Smrj 			free(path);
8827455710d3Srscott 
8828455710d3Srscott 			/*
8829455710d3Srscott 			 * If there were options given, use those.
8830455710d3Srscott 			 * Otherwise, copy over the default options.
8831455710d3Srscott 			 */
8832455710d3Srscott 			if (opt_ptr != NULL) {
8833455710d3Srscott 				/* Restore the space in opt string */
8834455710d3Srscott 				*opt_ptr = ' ';
8835455710d3Srscott 				(void) strlcat(kernbuf, opt_ptr,
8836eb2bd662Svikram 				    sizeof (kernbuf));
8837455710d3Srscott 			} else {
8838eb2bd662Svikram 				ret = get_kernel(mp, ARGS_CMD, args_buf,
8839eb2bd662Svikram 				    sizeof (args_buf));
8840eb2bd662Svikram 				INJECT_ERROR1("UPDATE_TEMP_PARTIAL_ARGS",
8841eb2bd662Svikram 				    ret = BAM_ERROR);
8842eb2bd662Svikram 				if (ret != BAM_SUCCESS) {
88439bd6d269SLin Ling 					free(fstype);
8844eb2bd662Svikram 					bam_error(REBOOT_GET_ARGS_FAILED);
8845ae115bc7Smrj 					return (BAM_ERROR);
8846eb2bd662Svikram 				}
8847ae115bc7Smrj 
8848ae115bc7Smrj 				if (args_buf[0] != '\0') {
8849ae115bc7Smrj 					(void) strlcat(kernbuf, " ",
8850eb2bd662Svikram 					    sizeof (kernbuf));
8851ae115bc7Smrj 					(void) strlcat(kernbuf,
8852eb2bd662Svikram 					    args_buf, sizeof (kernbuf));
8853ae115bc7Smrj 				}
8854ae115bc7Smrj 			}
8855eb2bd662Svikram 			BAM_DPRINTF((D_REBOOT_RESOLVED_PARTIAL, fcn, kernbuf));
8856455710d3Srscott 		} else {
88579bd6d269SLin Ling 			free(fstype);
8858455710d3Srscott 			bam_error(UNKNOWN_KERNEL, opt);
8859455710d3Srscott 			bam_print_stderr(UNKNOWN_KERNEL_REBOOT);
8860455710d3Srscott 			return (BAM_ERROR);
8861ae115bc7Smrj 		}
8862ae115bc7Smrj 	}
88639bd6d269SLin Ling 	free(fstype);
8864eb2bd662Svikram 	entry = add_boot_entry(mp, REBOOT_TITLE, signbuf, kernbuf,
886544da779fSWilliam Kucharski 	    NULL, NULL, NULL);
8866eb2bd662Svikram 	INJECT_ERROR1("REBOOT_ADD_BOOT_ENTRY", entry = BAM_ERROR);
88677c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
8868eb2bd662Svikram 		bam_error(REBOOT_WITH_ARGS_ADD_ENTRY_FAILED);
88697c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
88707c478bd9Sstevel@tonic-gate 	}
88718c1b6884Sszhou 
8872ae115bc7Smrj 	save_default_entry(mp, BAM_OLDDEF);
8873eb2bd662Svikram 	ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
8874eb2bd662Svikram 	INJECT_ERROR1("REBOOT_SET_GLOBAL", ret = BAM_ERROR);
8875eb2bd662Svikram 	if (ret == BAM_ERROR) {
8876eb2bd662Svikram 		bam_error(REBOOT_SET_DEFAULT_FAILED, entry);
8877eb2bd662Svikram 	}
8878eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
88797c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
88807c478bd9Sstevel@tonic-gate }
88817c478bd9Sstevel@tonic-gate 
888244da779fSWilliam Kucharski error_t
88837c478bd9Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val)
88847c478bd9Sstevel@tonic-gate {
8885eb2bd662Svikram 	line_t		*lp;
8886eb2bd662Svikram 	line_t		*found;
8887eb2bd662Svikram 	line_t		*last;
8888eb2bd662Svikram 	char		*cp;
8889eb2bd662Svikram 	char		*str;
88907c478bd9Sstevel@tonic-gate 	char		prefix[BAM_MAXLINE];
88917c478bd9Sstevel@tonic-gate 	size_t		len;
8892eb2bd662Svikram 	const char	*fcn = "set_global()";
88937c478bd9Sstevel@tonic-gate 
88947c478bd9Sstevel@tonic-gate 	assert(mp);
88957c478bd9Sstevel@tonic-gate 	assert(globalcmd);
88967c478bd9Sstevel@tonic-gate 
8897b610f78eSvikram 	if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) {
8898eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_VAL_NEG", val = -1);
8899eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_MENU_EMPTY", mp->end = NULL);
8900eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_VAL_TOO_BIG", val = 100);
8901b610f78eSvikram 		if (val < 0 || mp->end == NULL || val > mp->end->entryNum) {
8902b610f78eSvikram 			(void) snprintf(prefix, sizeof (prefix), "%d", val);
8903b610f78eSvikram 			bam_error(INVALID_ENTRY, prefix);
8904b610f78eSvikram 			return (BAM_ERROR);
8905b610f78eSvikram 		}
8906b610f78eSvikram 	}
8907b610f78eSvikram 
89087c478bd9Sstevel@tonic-gate 	found = last = NULL;
89097c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
89107c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
89117c478bd9Sstevel@tonic-gate 			continue;
89127c478bd9Sstevel@tonic-gate 
89137c478bd9Sstevel@tonic-gate 		last = lp; /* track the last global found */
89147c478bd9Sstevel@tonic-gate 
8915eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_NULL_CMD", lp->cmd = NULL);
89167c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
89177c478bd9Sstevel@tonic-gate 			bam_error(NO_CMD, lp->lineNum);
89187c478bd9Sstevel@tonic-gate 			continue;
89197c478bd9Sstevel@tonic-gate 		}
89207c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
89217c478bd9Sstevel@tonic-gate 			continue;
89227c478bd9Sstevel@tonic-gate 
8923eb2bd662Svikram 		BAM_DPRINTF((D_FOUND_GLOBAL, fcn, globalcmd));
8924eb2bd662Svikram 
89257c478bd9Sstevel@tonic-gate 		if (found) {
89267c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
89277c478bd9Sstevel@tonic-gate 		}
89287c478bd9Sstevel@tonic-gate 		found = lp;
89297c478bd9Sstevel@tonic-gate 	}
89307c478bd9Sstevel@tonic-gate 
89317c478bd9Sstevel@tonic-gate 	if (found == NULL) {
89327c478bd9Sstevel@tonic-gate 		lp = s_calloc(1, sizeof (line_t));
89337c478bd9Sstevel@tonic-gate 		if (last == NULL) {
89347c478bd9Sstevel@tonic-gate 			lp->next = mp->start;
89357c478bd9Sstevel@tonic-gate 			mp->start = lp;
89367c478bd9Sstevel@tonic-gate 			mp->end = (mp->end) ? mp->end : lp;
89377c478bd9Sstevel@tonic-gate 		} else {
89387c478bd9Sstevel@tonic-gate 			lp->next = last->next;
89397c478bd9Sstevel@tonic-gate 			last->next = lp;
89407c478bd9Sstevel@tonic-gate 			if (lp->next == NULL)
89417c478bd9Sstevel@tonic-gate 				mp->end = lp;
89427c478bd9Sstevel@tonic-gate 		}
89437c478bd9Sstevel@tonic-gate 		lp->flags = BAM_GLOBAL; /* other fields not needed for writes */
89447c478bd9Sstevel@tonic-gate 		len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]);
89457c478bd9Sstevel@tonic-gate 		len += 10;	/* val < 10 digits */
89467c478bd9Sstevel@tonic-gate 		lp->line = s_calloc(1, len);
89477c478bd9Sstevel@tonic-gate 		(void) snprintf(lp->line, len, "%s%s%d",
89487c478bd9Sstevel@tonic-gate 		    globalcmd, menu_cmds[SEP_CMD], val);
8949eb2bd662Svikram 		BAM_DPRINTF((D_SET_GLOBAL_WROTE_NEW, fcn, lp->line));
8950eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
89517c478bd9Sstevel@tonic-gate 		return (BAM_WRITE);
89527c478bd9Sstevel@tonic-gate 	}
89537c478bd9Sstevel@tonic-gate 
89547c478bd9Sstevel@tonic-gate 	/*
89557c478bd9Sstevel@tonic-gate 	 * We are changing an existing entry. Retain any prefix whitespace,
89567c478bd9Sstevel@tonic-gate 	 * but overwrite everything else. This preserves tabs added for
89577c478bd9Sstevel@tonic-gate 	 * readability.
89587c478bd9Sstevel@tonic-gate 	 */
89597c478bd9Sstevel@tonic-gate 	str = found->line;
89607c478bd9Sstevel@tonic-gate 	cp = prefix;
89617c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
89627c478bd9Sstevel@tonic-gate 		*(cp++) = *(str++);
89637c478bd9Sstevel@tonic-gate 	*cp = '\0'; /* Terminate prefix */
89647c478bd9Sstevel@tonic-gate 	len = strlen(prefix) + strlen(globalcmd);
89657c478bd9Sstevel@tonic-gate 	len += strlen(menu_cmds[SEP_CMD]) + 10;
89667c478bd9Sstevel@tonic-gate 
89677c478bd9Sstevel@tonic-gate 	free(found->line);
89687c478bd9Sstevel@tonic-gate 	found->line = s_calloc(1, len);
89697c478bd9Sstevel@tonic-gate 	(void) snprintf(found->line, len,
89707c478bd9Sstevel@tonic-gate 	    "%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val);
89717c478bd9Sstevel@tonic-gate 
8972eb2bd662Svikram 	BAM_DPRINTF((D_SET_GLOBAL_REPLACED, fcn, found->line));
8973eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
89747c478bd9Sstevel@tonic-gate 	return (BAM_WRITE); /* need a write to menu */
89757c478bd9Sstevel@tonic-gate }
89767c478bd9Sstevel@tonic-gate 
8977ae115bc7Smrj /*
8978ae115bc7Smrj  * partial_path may be anything like "kernel/unix" or "kmdb".  Try to
8979455710d3Srscott  * expand it to a full unix path.  The calling function is expected to
8980455710d3Srscott  * output a message if an error occurs and NULL is returned.
8981ae115bc7Smrj  */
8982ae115bc7Smrj static char *
8983ae115bc7Smrj expand_path(const char *partial_path)
8984ae115bc7Smrj {
8985ae115bc7Smrj 	int		new_path_len;
8986eb2bd662Svikram 	char		*new_path;
8987eb2bd662Svikram 	char		new_path2[PATH_MAX];
8988ae115bc7Smrj 	struct stat	sb;
8989eb2bd662Svikram 	const char	*fcn = "expand_path()";
8990ae115bc7Smrj 
8991ae115bc7Smrj 	new_path_len = strlen(partial_path) + 64;
8992ae115bc7Smrj 	new_path = s_calloc(1, new_path_len);
8993ae115bc7Smrj 
8994ae115bc7Smrj 	/* First, try the simplest case - something like "kernel/unix" */
8995ae115bc7Smrj 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s",
8996ae115bc7Smrj 	    partial_path);
8997ae115bc7Smrj 	if (stat(new_path, &sb) == 0) {
8998eb2bd662Svikram 		BAM_DPRINTF((D_EXPAND_PATH, fcn, new_path));
8999ae115bc7Smrj 		return (new_path);
9000ae115bc7Smrj 	}
9001ae115bc7Smrj 
9002ae115bc7Smrj 	if (strcmp(partial_path, "kmdb") == 0) {
9003ae115bc7Smrj 		(void) snprintf(new_path, new_path_len, "%s -k",
9004ae115bc7Smrj 		    DIRECT_BOOT_KERNEL);
9005eb2bd662Svikram 		BAM_DPRINTF((D_EXPAND_PATH, fcn, new_path));
9006ae115bc7Smrj 		return (new_path);
9007ae115bc7Smrj 	}
9008ae115bc7Smrj 
9009ae115bc7Smrj 	/*
9010ae115bc7Smrj 	 * We've quickly reached unsupported usage.  Try once more to
9011ae115bc7Smrj 	 * see if we were just given a glom name.
9012ae115bc7Smrj 	 */
9013ae115bc7Smrj 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s/unix",
9014ae115bc7Smrj 	    partial_path);
9015ae115bc7Smrj 	(void) snprintf(new_path2, PATH_MAX, "/platform/i86pc/%s/amd64/unix",
9016ae115bc7Smrj 	    partial_path);
9017ae115bc7Smrj 	if (stat(new_path, &sb) == 0) {
9018ae115bc7Smrj 		if (stat(new_path2, &sb) == 0) {
9019ae115bc7Smrj 			/*
9020ae115bc7Smrj 			 * We matched both, so we actually
9021ae115bc7Smrj 			 * want to write the $ISADIR version.
9022ae115bc7Smrj 			 */
9023ae115bc7Smrj 			(void) snprintf(new_path, new_path_len,
9024ae115bc7Smrj 			    "/platform/i86pc/kernel/%s/$ISADIR/unix",
9025ae115bc7Smrj 			    partial_path);
9026ae115bc7Smrj 		}
9027eb2bd662Svikram 		BAM_DPRINTF((D_EXPAND_PATH, fcn, new_path));
9028ae115bc7Smrj 		return (new_path);
9029ae115bc7Smrj 	}
9030ae115bc7Smrj 
9031ae115bc7Smrj 	free(new_path);
9032eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_FAILURE, fcn));
9033ae115bc7Smrj 	return (NULL);
9034ae115bc7Smrj }
9035ae115bc7Smrj 
9036ae115bc7Smrj /*
9037ae115bc7Smrj  * The kernel cmd and arg have been changed, so
9038ae115bc7Smrj  * check whether the archive line needs to change.
9039ae115bc7Smrj  */
9040ae115bc7Smrj static void
9041ae115bc7Smrj set_archive_line(entry_t *entryp, line_t *kernelp)
9042ae115bc7Smrj {
9043ae115bc7Smrj 	line_t		*lp = entryp->start;
9044ae115bc7Smrj 	char		*new_archive;
9045ae115bc7Smrj 	menu_cmd_t	m_cmd;
9046eb2bd662Svikram 	const char	*fcn = "set_archive_line()";
9047ae115bc7Smrj 
9048ae115bc7Smrj 	for (; lp != NULL; lp = lp->next) {
9049eac223ccSWilliam Kucharski 		if (lp->cmd != NULL && strncmp(lp->cmd, menu_cmds[MODULE_CMD],
9050ae115bc7Smrj 		    sizeof (menu_cmds[MODULE_CMD]) - 1) == 0) {
9051ae115bc7Smrj 			break;
9052ae115bc7Smrj 		}
9053eb2bd662Svikram 
9054eb2bd662Svikram 		INJECT_ERROR1("SET_ARCHIVE_LINE_END_ENTRY", lp = entryp->end);
9055eb2bd662Svikram 		if (lp == entryp->end) {
9056eb2bd662Svikram 			BAM_DPRINTF((D_ARCHIVE_LINE_NONE, fcn,
9057eb2bd662Svikram 			    entryp->entryNum));
9058ae115bc7Smrj 			return;
9059ae115bc7Smrj 		}
9060eb2bd662Svikram 	}
9061eb2bd662Svikram 	INJECT_ERROR1("SET_ARCHIVE_LINE_END_MENU", lp = NULL);
9062eb2bd662Svikram 	if (lp == NULL) {
9063eb2bd662Svikram 		BAM_DPRINTF((D_ARCHIVE_LINE_NONE, fcn, entryp->entryNum));
9064ae115bc7Smrj 		return;
9065eb2bd662Svikram 	}
9066ae115bc7Smrj 
9067ae115bc7Smrj 	if (strstr(kernelp->arg, "$ISADIR") != NULL) {
9068ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE;
9069ae115bc7Smrj 		m_cmd = MODULE_DOLLAR_CMD;
9070ae115bc7Smrj 	} else if (strstr(kernelp->arg, "amd64") != NULL) {
9071ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE_64;
9072ae115bc7Smrj 		m_cmd = MODULE_CMD;
9073ae115bc7Smrj 	} else {
9074ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE_32;
9075ae115bc7Smrj 		m_cmd = MODULE_CMD;
9076ae115bc7Smrj 	}
9077ae115bc7Smrj 
9078eb2bd662Svikram 	if (strcmp(lp->arg, new_archive) == 0) {
9079eb2bd662Svikram 		BAM_DPRINTF((D_ARCHIVE_LINE_NOCHANGE, fcn, lp->arg));
9080ae115bc7Smrj 		return;
9081eb2bd662Svikram 	}
9082ae115bc7Smrj 
9083eac223ccSWilliam Kucharski 	if (lp->cmd != NULL && strcmp(lp->cmd, menu_cmds[m_cmd]) != 0) {
9084ae115bc7Smrj 		free(lp->cmd);
9085ae115bc7Smrj 		lp->cmd = s_strdup(menu_cmds[m_cmd]);
9086ae115bc7Smrj 	}
9087ae115bc7Smrj 
9088ae115bc7Smrj 	free(lp->arg);
9089ae115bc7Smrj 	lp->arg = s_strdup(new_archive);
9090ae115bc7Smrj 	update_line(lp);
9091eb2bd662Svikram 	BAM_DPRINTF((D_ARCHIVE_LINE_REPLACED, fcn, lp->line));
9092ae115bc7Smrj }
9093ae115bc7Smrj 
9094ae115bc7Smrj /*
9095ae115bc7Smrj  * Title for an entry to set properties that once went in bootenv.rc.
9096ae115bc7Smrj  */
9097ae115bc7Smrj #define	BOOTENV_RC_TITLE	"Solaris bootenv rc"
9098ae115bc7Smrj 
9099ae115bc7Smrj /*
9100ae115bc7Smrj  * If path is NULL, return the kernel (optnum == KERNEL_CMD) or arguments
9101ae115bc7Smrj  * (optnum == ARGS_CMD) in the argument buf.  If path is a zero-length
9102ae115bc7Smrj  * string, reset the value to the default.  If path is a non-zero-length
9103ae115bc7Smrj  * string, set the kernel or arguments.
9104ae115bc7Smrj  */
9105ae115bc7Smrj static error_t
9106eb2bd662Svikram get_set_kernel(
9107eb2bd662Svikram 	menu_t *mp,
9108eb2bd662Svikram 	menu_cmd_t optnum,
9109eb2bd662Svikram 	char *path,
9110eb2bd662Svikram 	char *buf,
9111eb2bd662Svikram 	size_t bufsize)
9112ae115bc7Smrj {
9113eb2bd662Svikram 	int		entryNum;
9114eb2bd662Svikram 	int		rv = BAM_SUCCESS;
9115eb2bd662Svikram 	int		free_new_path = 0;
9116ae115bc7Smrj 	entry_t		*entryp;
9117eb2bd662Svikram 	line_t		*ptr;
9118eb2bd662Svikram 	line_t		*kernelp;
9119eb2bd662Svikram 	char		*new_arg;
9120eb2bd662Svikram 	char		*old_args;
9121eb2bd662Svikram 	char		*space;
9122eb2bd662Svikram 	char		*new_path;
9123ae115bc7Smrj 	char		old_space;
9124eb2bd662Svikram 	size_t		old_kernel_len;
9125eb2bd662Svikram 	size_t		new_str_len;
9126eb2bd662Svikram 	char		*fstype;
9127eb2bd662Svikram 	char		*osdev;
9128eb2bd662Svikram 	char		*sign;
9129eb2bd662Svikram 	char		signbuf[PATH_MAX];
9130eb2bd662Svikram 	int		ret;
9131eb2bd662Svikram 	const char	*fcn = "get_set_kernel()";
9132ae115bc7Smrj 
9133ae115bc7Smrj 	assert(bufsize > 0);
9134ae115bc7Smrj 
9135ae115bc7Smrj 	ptr = kernelp = NULL;
9136ae115bc7Smrj 	new_arg = old_args = space = NULL;
9137eb2bd662Svikram 	new_path = NULL;
9138ae115bc7Smrj 	buf[0] = '\0';
9139ae115bc7Smrj 
9140eb2bd662Svikram 	INJECT_ERROR1("GET_SET_KERNEL_NOT_DBOOT",
9141eb2bd662Svikram 	    bam_direct = BAM_DIRECT_MULTIBOOT);
9142ae115bc7Smrj 	if (bam_direct != BAM_DIRECT_DBOOT) {
9143ae115bc7Smrj 		bam_error(NOT_DBOOT, optnum == KERNEL_CMD ? "kernel" : "args");
9144ae115bc7Smrj 		return (BAM_ERROR);
9145ae115bc7Smrj 	}
9146ae115bc7Smrj 
9147ae115bc7Smrj 	/*
9148ae115bc7Smrj 	 * If a user changed the default entry to a non-bootadm controlled
9149ae115bc7Smrj 	 * one, we don't want to mess with it.  Just print an error and
9150ae115bc7Smrj 	 * return.
9151ae115bc7Smrj 	 */
9152ae115bc7Smrj 	if (mp->curdefault) {
9153ae115bc7Smrj 		entryNum = s_strtol(mp->curdefault->arg);
9154ae115bc7Smrj 		for (entryp = mp->entries; entryp; entryp = entryp->next) {
9155ae115bc7Smrj 			if (entryp->entryNum == entryNum)
9156ae115bc7Smrj 				break;
9157ae115bc7Smrj 		}
9158ae115bc7Smrj 		if ((entryp != NULL) &&
9159ae115bc7Smrj 		    ((entryp->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU)) == 0)) {
9160ae115bc7Smrj 			bam_error(DEFAULT_NOT_BAM);
9161ae115bc7Smrj 			return (BAM_ERROR);
9162ae115bc7Smrj 		}
9163ae115bc7Smrj 	}
9164ae115bc7Smrj 
9165eb2bd662Svikram 	entryp = find_boot_entry(mp, BOOTENV_RC_TITLE, NULL, NULL, NULL, NULL,
9166eb2bd662Svikram 	    0, &entryNum);
9167ae115bc7Smrj 
9168ae115bc7Smrj 	if (entryp != NULL) {
9169ae115bc7Smrj 		for (ptr = entryp->start; ptr && ptr != entryp->end;
9170ae115bc7Smrj 		    ptr = ptr->next) {
9171ae115bc7Smrj 			if (strncmp(ptr->cmd, menu_cmds[KERNEL_CMD],
9172ae115bc7Smrj 			    sizeof (menu_cmds[KERNEL_CMD]) - 1) == 0) {
9173ae115bc7Smrj 				kernelp = ptr;
9174ae115bc7Smrj 				break;
9175ae115bc7Smrj 			}
9176ae115bc7Smrj 		}
9177ae115bc7Smrj 		if (kernelp == NULL) {
9178ae115bc7Smrj 			bam_error(NO_KERNEL, entryNum);
9179ae115bc7Smrj 			return (BAM_ERROR);
9180ae115bc7Smrj 		}
9181ae115bc7Smrj 
9182ae115bc7Smrj 		old_kernel_len = strcspn(kernelp->arg, " \t");
9183ae115bc7Smrj 		space = old_args = kernelp->arg + old_kernel_len;
9184ae115bc7Smrj 		while ((*old_args == ' ') || (*old_args == '\t'))
9185ae115bc7Smrj 			old_args++;
9186ae115bc7Smrj 	}
9187ae115bc7Smrj 
9188ae115bc7Smrj 	if (path == NULL) {
9189eb2bd662Svikram 		if (entryp == NULL) {
9190eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_NO_RC, fcn));
9191eb2bd662Svikram 			BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
9192ae115bc7Smrj 			return (BAM_SUCCESS);
9193eb2bd662Svikram 		}
9194eb2bd662Svikram 		assert(kernelp);
9195ae115bc7Smrj 		if (optnum == ARGS_CMD) {
9196eb2bd662Svikram 			if (old_args[0] != '\0') {
9197ae115bc7Smrj 				(void) strlcpy(buf, old_args, bufsize);
9198eb2bd662Svikram 				BAM_DPRINTF((D_GET_SET_KERNEL_ARGS, fcn, buf));
9199eb2bd662Svikram 			}
9200ae115bc7Smrj 		} else {
9201ae115bc7Smrj 			/*
9202ae115bc7Smrj 			 * We need to print the kernel, so we just turn the
9203ae115bc7Smrj 			 * first space into a '\0' and print the beginning.
9204ae115bc7Smrj 			 * We don't print anything if it's the default kernel.
9205ae115bc7Smrj 			 */
9206ae115bc7Smrj 			old_space = *space;
9207ae115bc7Smrj 			*space = '\0';
9208eb2bd662Svikram 			if (strcmp(kernelp->arg, DIRECT_BOOT_KERNEL) != 0) {
9209ae115bc7Smrj 				(void) strlcpy(buf, kernelp->arg, bufsize);
9210eb2bd662Svikram 				BAM_DPRINTF((D_GET_SET_KERNEL_KERN, fcn, buf));
9211eb2bd662Svikram 			}
9212ae115bc7Smrj 			*space = old_space;
9213ae115bc7Smrj 		}
9214eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
9215ae115bc7Smrj 		return (BAM_SUCCESS);
9216ae115bc7Smrj 	}
9217ae115bc7Smrj 
9218ae115bc7Smrj 	/*
9219ae115bc7Smrj 	 * First, check if we're resetting an entry to the default.
9220ae115bc7Smrj 	 */
9221ae115bc7Smrj 	if ((path[0] == '\0') ||
9222ae115bc7Smrj 	    ((optnum == KERNEL_CMD) &&
9223ae115bc7Smrj 	    (strcmp(path, DIRECT_BOOT_KERNEL) == 0))) {
9224ae115bc7Smrj 		if ((entryp == NULL) || (kernelp == NULL)) {
9225ae115bc7Smrj 			/* No previous entry, it's already the default */
9226eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_ALREADY, fcn));
9227ae115bc7Smrj 			return (BAM_SUCCESS);
9228ae115bc7Smrj 		}
9229ae115bc7Smrj 
9230ae115bc7Smrj 		/*
9231ae115bc7Smrj 		 * Check if we can delete the entry.  If we're resetting the
9232ae115bc7Smrj 		 * kernel command, and the args is already empty, or if we're
9233ae115bc7Smrj 		 * resetting the args command, and the kernel is already the
9234ae115bc7Smrj 		 * default, we can restore the old default and delete the entry.
9235ae115bc7Smrj 		 */
9236ae115bc7Smrj 		if (((optnum == KERNEL_CMD) &&
9237ae115bc7Smrj 		    ((old_args == NULL) || (old_args[0] == '\0'))) ||
9238ae115bc7Smrj 		    ((optnum == ARGS_CMD) &&
9239ae115bc7Smrj 		    (strncmp(kernelp->arg, DIRECT_BOOT_KERNEL,
9240ae115bc7Smrj 		    sizeof (DIRECT_BOOT_KERNEL) - 1) == 0))) {
9241ae115bc7Smrj 			kernelp = NULL;
924244da779fSWilliam Kucharski 			(void) delete_boot_entry(mp, entryNum, DBE_PRINTERR);
9243ae115bc7Smrj 			restore_default_entry(mp, BAM_OLD_RC_DEF,
9244ae115bc7Smrj 			    mp->old_rc_default);
9245ae115bc7Smrj 			mp->old_rc_default = NULL;
9246ae115bc7Smrj 			rv = BAM_WRITE;
9247eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_RESTORE_DEFAULT, fcn));
9248ae115bc7Smrj 			goto done;
9249ae115bc7Smrj 		}
9250ae115bc7Smrj 
9251ae115bc7Smrj 		if (optnum == KERNEL_CMD) {
9252ae115bc7Smrj 			/*
9253ae115bc7Smrj 			 * At this point, we've already checked that old_args
9254ae115bc7Smrj 			 * and entryp are valid pointers.  The "+ 2" is for
9255ae115bc7Smrj 			 * a space a the string termination character.
9256ae115bc7Smrj 			 */
9257ae115bc7Smrj 			new_str_len = (sizeof (DIRECT_BOOT_KERNEL) - 1) +
9258ae115bc7Smrj 			    strlen(old_args) + 2;
9259ae115bc7Smrj 			new_arg = s_calloc(1, new_str_len);
9260ae115bc7Smrj 			(void) snprintf(new_arg, new_str_len, "%s %s",
9261ae115bc7Smrj 			    DIRECT_BOOT_KERNEL, old_args);
9262ae115bc7Smrj 			free(kernelp->arg);
9263ae115bc7Smrj 			kernelp->arg = new_arg;
9264ae115bc7Smrj 
9265ae115bc7Smrj 			/*
9266ae115bc7Smrj 			 * We have changed the kernel line, so we may need
9267ae115bc7Smrj 			 * to update the archive line as well.
9268ae115bc7Smrj 			 */
9269ae115bc7Smrj 			set_archive_line(entryp, kernelp);
9270eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_RESET_KERNEL_SET_ARG,
9271eb2bd662Svikram 			    fcn, kernelp->arg));
9272ae115bc7Smrj 		} else {
9273ae115bc7Smrj 			/*
9274ae115bc7Smrj 			 * We're resetting the boot args to nothing, so
9275ae115bc7Smrj 			 * we only need to copy the kernel.  We've already
9276ae115bc7Smrj 			 * checked that the kernel is not the default.
9277ae115bc7Smrj 			 */
9278ae115bc7Smrj 			new_arg = s_calloc(1, old_kernel_len + 1);
9279ae115bc7Smrj 			(void) snprintf(new_arg, old_kernel_len + 1, "%s",
9280ae115bc7Smrj 			    kernelp->arg);
9281ae115bc7Smrj 			free(kernelp->arg);
9282ae115bc7Smrj 			kernelp->arg = new_arg;
9283eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_RESET_ARG_SET_KERNEL,
9284eb2bd662Svikram 			    fcn, kernelp->arg));
9285ae115bc7Smrj 		}
9286ae115bc7Smrj 		rv = BAM_WRITE;
9287ae115bc7Smrj 		goto done;
9288ae115bc7Smrj 	}
9289ae115bc7Smrj 
9290ae115bc7Smrj 	/*
9291ae115bc7Smrj 	 * Expand the kernel file to a full path, if necessary
9292ae115bc7Smrj 	 */
9293ae115bc7Smrj 	if ((optnum == KERNEL_CMD) && (path[0] != '/')) {
9294ae115bc7Smrj 		new_path = expand_path(path);
9295ae115bc7Smrj 		if (new_path == NULL) {
9296455710d3Srscott 			bam_error(UNKNOWN_KERNEL, path);
9297eb2bd662Svikram 			BAM_DPRINTF((D_RETURN_FAILURE, fcn));
9298ae115bc7Smrj 			return (BAM_ERROR);
9299ae115bc7Smrj 		}
9300ae115bc7Smrj 		free_new_path = 1;
9301ae115bc7Smrj 	} else {
9302ae115bc7Smrj 		new_path = path;
9303ae115bc7Smrj 		free_new_path = 0;
9304ae115bc7Smrj 	}
9305ae115bc7Smrj 
9306ae115bc7Smrj 	/*
9307ae115bc7Smrj 	 * At this point, we know we're setting a new value.  First, take care
9308ae115bc7Smrj 	 * of the case where there was no previous entry.
9309ae115bc7Smrj 	 */
9310ae115bc7Smrj 	if (entryp == NULL) {
9311eb2bd662Svikram 
9312ae115bc7Smrj 		/* Similar to code in update_temp */
9313eb2bd662Svikram 		fstype = get_fstype("/");
9314eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_FSTYPE", fstype = NULL);
9315eb2bd662Svikram 		if (fstype == NULL) {
9316eb2bd662Svikram 			bam_error(BOOTENV_FSTYPE_FAILED);
9317ae115bc7Smrj 			rv = BAM_ERROR;
9318ae115bc7Smrj 			goto done;
9319ae115bc7Smrj 		}
9320eb2bd662Svikram 
9321eb2bd662Svikram 		osdev = get_special("/");
9322eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_SPECIAL", osdev = NULL);
9323eb2bd662Svikram 		if (osdev == NULL) {
9324eb2bd662Svikram 			free(fstype);
9325eb2bd662Svikram 			bam_error(BOOTENV_SPECIAL_FAILED);
9326eb2bd662Svikram 			rv = BAM_ERROR;
9327eb2bd662Svikram 			goto done;
9328eb2bd662Svikram 		}
9329eb2bd662Svikram 
9330eb2bd662Svikram 		sign = find_existing_sign("/", osdev, fstype);
9331eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_SIGN", sign = NULL);
9332eb2bd662Svikram 		if (sign == NULL) {
9333eb2bd662Svikram 			free(fstype);
9334eb2bd662Svikram 			free(osdev);
9335eb2bd662Svikram 			bam_error(BOOTENV_SIGN_FAILED);
9336eb2bd662Svikram 			rv = BAM_ERROR;
9337eb2bd662Svikram 			goto done;
9338eb2bd662Svikram 		}
9339eb2bd662Svikram 
9340eb2bd662Svikram 		free(osdev);
9341eb2bd662Svikram 		(void) strlcpy(signbuf, sign, sizeof (signbuf));
9342eb2bd662Svikram 		free(sign);
9343eb2bd662Svikram 		assert(strchr(signbuf, '(') == NULL &&
9344eb2bd662Svikram 		    strchr(signbuf, ',') == NULL &&
9345eb2bd662Svikram 		    strchr(signbuf, ')') == NULL);
9346eb2bd662Svikram 
9347ae115bc7Smrj 		if (optnum == KERNEL_CMD) {
93484e483a70SGangadhar Mylapuram 			if (strcmp(fstype, "zfs") == 0) {
93494e483a70SGangadhar Mylapuram 				new_str_len = strlen(new_path) +
93504e483a70SGangadhar Mylapuram 				    strlen(ZFS_BOOT) + 8;
93514e483a70SGangadhar Mylapuram 				new_arg = s_calloc(1, new_str_len);
93524e483a70SGangadhar Mylapuram 				(void) snprintf(new_arg, new_str_len, "%s %s",
93534e483a70SGangadhar Mylapuram 				    new_path, ZFS_BOOT);
93544e483a70SGangadhar Mylapuram 				BAM_DPRINTF((D_GET_SET_KERNEL_NEW_KERN, fcn,
93554e483a70SGangadhar Mylapuram 				    new_arg));
93564e483a70SGangadhar Mylapuram 				entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
93574e483a70SGangadhar Mylapuram 				    signbuf, new_arg, NULL, NULL, NULL);
93584e483a70SGangadhar Mylapuram 				free(new_arg);
93594e483a70SGangadhar Mylapuram 			} else {
93604e483a70SGangadhar Mylapuram 				BAM_DPRINTF((D_GET_SET_KERNEL_NEW_KERN, fcn,
93614e483a70SGangadhar Mylapuram 				    new_path));
9362ae115bc7Smrj 				entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
936344da779fSWilliam Kucharski 				    signbuf, new_path, NULL, NULL, NULL);
93644e483a70SGangadhar Mylapuram 			}
9365ae115bc7Smrj 		} else {
93664487ce1dSGangadhar Mylapuram 			new_str_len = strlen(path) + 8;
93674487ce1dSGangadhar Mylapuram 			if (strcmp(fstype, "zfs") == 0) {
93684487ce1dSGangadhar Mylapuram 				new_str_len += strlen(DIRECT_BOOT_KERNEL_ZFS);
9369ae115bc7Smrj 				new_arg = s_calloc(1, new_str_len);
93704487ce1dSGangadhar Mylapuram 				(void) snprintf(new_arg, new_str_len, "%s %s",
93714487ce1dSGangadhar Mylapuram 				    DIRECT_BOOT_KERNEL_ZFS, path);
93724487ce1dSGangadhar Mylapuram 			} else {
93734487ce1dSGangadhar Mylapuram 				new_str_len += strlen(DIRECT_BOOT_KERNEL);
93744487ce1dSGangadhar Mylapuram 				new_arg = s_calloc(1, new_str_len);
9375ae115bc7Smrj 				(void) snprintf(new_arg, new_str_len, "%s %s",
9376ae115bc7Smrj 				    DIRECT_BOOT_KERNEL, path);
93774487ce1dSGangadhar Mylapuram 			}
93784487ce1dSGangadhar Mylapuram 
9379eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_NEW_ARG, fcn, new_arg));
9380ae115bc7Smrj 			entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
938144da779fSWilliam Kucharski 			    signbuf, new_arg, NULL, DIRECT_BOOT_ARCHIVE, NULL);
9382eb2bd662Svikram 			free(new_arg);
9383eb2bd662Svikram 		}
93844487ce1dSGangadhar Mylapuram 		free(fstype);
9385eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_ADD_BOOT_ENTRY",
9386eb2bd662Svikram 		    entryNum = BAM_ERROR);
9387eb2bd662Svikram 		if (entryNum == BAM_ERROR) {
9388eb2bd662Svikram 			bam_error(GET_SET_KERNEL_ADD_BOOT_ENTRY,
9389eb2bd662Svikram 			    BOOTENV_RC_TITLE);
9390eb2bd662Svikram 			rv = BAM_ERROR;
9391eb2bd662Svikram 			goto done;
9392ae115bc7Smrj 		}
9393ae115bc7Smrj 		save_default_entry(mp, BAM_OLD_RC_DEF);
9394eb2bd662Svikram 		ret = set_global(mp, menu_cmds[DEFAULT_CMD], entryNum);
9395eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_SET_GLOBAL", ret = BAM_ERROR);
9396eb2bd662Svikram 		if (ret == BAM_ERROR) {
9397eb2bd662Svikram 			bam_error(GET_SET_KERNEL_SET_GLOBAL, entryNum);
9398eb2bd662Svikram 		}
9399ae115bc7Smrj 		rv = BAM_WRITE;
9400ae115bc7Smrj 		goto done;
9401ae115bc7Smrj 	}
9402ae115bc7Smrj 
9403ae115bc7Smrj 	/*
9404ae115bc7Smrj 	 * There was already an bootenv entry which we need to edit.
9405ae115bc7Smrj 	 */
9406ae115bc7Smrj 	if (optnum == KERNEL_CMD) {
9407ae115bc7Smrj 		new_str_len = strlen(new_path) + strlen(old_args) + 2;
9408ae115bc7Smrj 		new_arg = s_calloc(1, new_str_len);
9409ae115bc7Smrj 		(void) snprintf(new_arg, new_str_len, "%s %s", new_path,
9410ae115bc7Smrj 		    old_args);
9411ae115bc7Smrj 		free(kernelp->arg);
9412ae115bc7Smrj 		kernelp->arg = new_arg;
9413ae115bc7Smrj 
9414ae115bc7Smrj 		/*
9415ae115bc7Smrj 		 * If we have changed the kernel line, we may need to update
9416ae115bc7Smrj 		 * the archive line as well.
9417ae115bc7Smrj 		 */
9418ae115bc7Smrj 		set_archive_line(entryp, kernelp);
9419eb2bd662Svikram 		BAM_DPRINTF((D_GET_SET_KERNEL_REPLACED_KERNEL_SAME_ARG, fcn,
9420eb2bd662Svikram 		    kernelp->arg));
9421ae115bc7Smrj 	} else {
9422ae115bc7Smrj 		new_str_len = old_kernel_len + strlen(path) + 8;
9423ae115bc7Smrj 		new_arg = s_calloc(1, new_str_len);
9424ae115bc7Smrj 		(void) strncpy(new_arg, kernelp->arg, old_kernel_len);
9425ae115bc7Smrj 		(void) strlcat(new_arg, " ", new_str_len);
9426ae115bc7Smrj 		(void) strlcat(new_arg, path, new_str_len);
9427ae115bc7Smrj 		free(kernelp->arg);
9428ae115bc7Smrj 		kernelp->arg = new_arg;
9429eb2bd662Svikram 		BAM_DPRINTF((D_GET_SET_KERNEL_SAME_KERNEL_REPLACED_ARG, fcn,
9430eb2bd662Svikram 		    kernelp->arg));
9431ae115bc7Smrj 	}
9432ae115bc7Smrj 	rv = BAM_WRITE;
9433ae115bc7Smrj 
9434ae115bc7Smrj done:
9435ae115bc7Smrj 	if ((rv == BAM_WRITE) && kernelp)
9436ae115bc7Smrj 		update_line(kernelp);
9437ae115bc7Smrj 	if (free_new_path)
9438ae115bc7Smrj 		free(new_path);
9439eb2bd662Svikram 	if (rv == BAM_WRITE) {
9440eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
9441eb2bd662Svikram 	} else {
9442eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
9443eb2bd662Svikram 	}
9444ae115bc7Smrj 	return (rv);
9445ae115bc7Smrj }
9446ae115bc7Smrj 
9447eb2bd662Svikram static error_t
9448eb2bd662Svikram get_kernel(menu_t *mp, menu_cmd_t optnum, char *buf, size_t bufsize)
9449eb2bd662Svikram {
9450eb2bd662Svikram 	const char	*fcn = "get_kernel()";
9451eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, menu_cmds[optnum]));
9452eb2bd662Svikram 	return (get_set_kernel(mp, optnum, NULL, buf, bufsize));
9453eb2bd662Svikram }
9454eb2bd662Svikram 
9455eb2bd662Svikram static error_t
9456eb2bd662Svikram set_kernel(menu_t *mp, menu_cmd_t optnum, char *path, char *buf, size_t bufsize)
9457eb2bd662Svikram {
9458eb2bd662Svikram 	const char	*fcn = "set_kernel()";
9459eb2bd662Svikram 	assert(path != NULL);
9460eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, menu_cmds[optnum], path));
9461eb2bd662Svikram 	return (get_set_kernel(mp, optnum, path, buf, bufsize));
9462eb2bd662Svikram }
9463eb2bd662Svikram 
94647c478bd9Sstevel@tonic-gate /*ARGSUSED*/
94657c478bd9Sstevel@tonic-gate static error_t
9466eb2bd662Svikram set_option(menu_t *mp, char *dummy, char *opt)
94677c478bd9Sstevel@tonic-gate {
9468eb2bd662Svikram 	int		optnum;
9469eb2bd662Svikram 	int		optval;
94707c478bd9Sstevel@tonic-gate 	char		*val;
9471ae115bc7Smrj 	char		buf[BUFSIZ] = "";
9472ae115bc7Smrj 	error_t		rv;
9473eb2bd662Svikram 	const char	*fcn = "set_option()";
94747c478bd9Sstevel@tonic-gate 
94757c478bd9Sstevel@tonic-gate 	assert(mp);
94767c478bd9Sstevel@tonic-gate 	assert(opt);
9477eb2bd662Svikram 	assert(dummy == NULL);
9478eb2bd662Svikram 
9479eb2bd662Svikram 	/* opt is set from bam_argv[0] and is always non-NULL */
9480eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, opt));
94817c478bd9Sstevel@tonic-gate 
94827c478bd9Sstevel@tonic-gate 	val = strchr(opt, '=');
9483ae115bc7Smrj 	if (val != NULL) {
9484ae115bc7Smrj 		*val = '\0';
94857c478bd9Sstevel@tonic-gate 	}
94867c478bd9Sstevel@tonic-gate 
94877c478bd9Sstevel@tonic-gate 	if (strcmp(opt, "default") == 0) {
94887c478bd9Sstevel@tonic-gate 		optnum = DEFAULT_CMD;
94897c478bd9Sstevel@tonic-gate 	} else if (strcmp(opt, "timeout") == 0) {
94907c478bd9Sstevel@tonic-gate 		optnum = TIMEOUT_CMD;
9491ae115bc7Smrj 	} else if (strcmp(opt, menu_cmds[KERNEL_CMD]) == 0) {
9492ae115bc7Smrj 		optnum = KERNEL_CMD;
9493ae115bc7Smrj 	} else if (strcmp(opt, menu_cmds[ARGS_CMD]) == 0) {
9494ae115bc7Smrj 		optnum = ARGS_CMD;
94957c478bd9Sstevel@tonic-gate 	} else {
9496eb2bd662Svikram 		bam_error(INVALID_OPTION, opt);
94977c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
94987c478bd9Sstevel@tonic-gate 	}
94997c478bd9Sstevel@tonic-gate 
9500ae115bc7Smrj 	/*
9501ae115bc7Smrj 	 * kernel and args are allowed without "=new_value" strings.  All
9502ae115bc7Smrj 	 * others cause errors
9503ae115bc7Smrj 	 */
9504ae115bc7Smrj 	if ((val == NULL) && (optnum != KERNEL_CMD) && (optnum != ARGS_CMD)) {
9505eb2bd662Svikram 		bam_error(NO_OPTION_ARG, opt);
9506ae115bc7Smrj 		return (BAM_ERROR);
9507ae115bc7Smrj 	} else if (val != NULL) {
95087c478bd9Sstevel@tonic-gate 		*val = '=';
9509ae115bc7Smrj 	}
9510ae115bc7Smrj 
9511ae115bc7Smrj 	if ((optnum == KERNEL_CMD) || (optnum == ARGS_CMD)) {
9512eb2bd662Svikram 		BAM_DPRINTF((D_SET_OPTION, fcn, menu_cmds[optnum],
9513eb2bd662Svikram 		    val ? val + 1 : "NULL"));
9514eb2bd662Svikram 
9515eb2bd662Svikram 		if (val)
9516eb2bd662Svikram 			rv = set_kernel(mp, optnum, val + 1, buf, sizeof (buf));
9517eb2bd662Svikram 		else
9518eb2bd662Svikram 			rv = get_kernel(mp, optnum, buf, sizeof (buf));
9519ae115bc7Smrj 		if ((rv == BAM_SUCCESS) && (buf[0] != '\0'))
9520ae115bc7Smrj 			(void) printf("%s\n", buf);
9521ae115bc7Smrj 	} else {
9522ae115bc7Smrj 		optval = s_strtol(val + 1);
9523eb2bd662Svikram 		BAM_DPRINTF((D_SET_OPTION, fcn, menu_cmds[optnum], val + 1));
9524eb2bd662Svikram 		rv = set_global(mp, menu_cmds[optnum], optval);
95257c478bd9Sstevel@tonic-gate 	}
9526eb2bd662Svikram 
9527eb2bd662Svikram 	if (rv == BAM_WRITE || rv == BAM_SUCCESS) {
9528eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
9529eb2bd662Svikram 	} else {
9530eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
9531eb2bd662Svikram 	}
9532eb2bd662Svikram 
9533eb2bd662Svikram 	return (rv);
9534ae115bc7Smrj }
95357c478bd9Sstevel@tonic-gate 
95367c478bd9Sstevel@tonic-gate /*
95377c478bd9Sstevel@tonic-gate  * The quiet argument suppresses messages. This is used
95387c478bd9Sstevel@tonic-gate  * when invoked in the context of other commands (e.g. list_entry)
95397c478bd9Sstevel@tonic-gate  */
95407c478bd9Sstevel@tonic-gate static error_t
95417c478bd9Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet)
95427c478bd9Sstevel@tonic-gate {
95437c478bd9Sstevel@tonic-gate 	line_t *lp;
95447c478bd9Sstevel@tonic-gate 	char *arg;
95457c478bd9Sstevel@tonic-gate 	int done, ret = BAM_SUCCESS;
95467c478bd9Sstevel@tonic-gate 
95477c478bd9Sstevel@tonic-gate 	assert(mp);
95487c478bd9Sstevel@tonic-gate 	assert(menu_path);
95497c478bd9Sstevel@tonic-gate 	assert(globalcmd);
95507c478bd9Sstevel@tonic-gate 
95517c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
95527c478bd9Sstevel@tonic-gate 		if (!quiet)
95537c478bd9Sstevel@tonic-gate 			bam_error(NO_MENU, menu_path);
95547c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
95557c478bd9Sstevel@tonic-gate 	}
95567c478bd9Sstevel@tonic-gate 
95577c478bd9Sstevel@tonic-gate 	done = 0;
95587c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
95597c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
95607c478bd9Sstevel@tonic-gate 			continue;
95617c478bd9Sstevel@tonic-gate 
95627c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
95637c478bd9Sstevel@tonic-gate 			if (!quiet)
95647c478bd9Sstevel@tonic-gate 				bam_error(NO_CMD, lp->lineNum);
95657c478bd9Sstevel@tonic-gate 			continue;
95667c478bd9Sstevel@tonic-gate 		}
95677c478bd9Sstevel@tonic-gate 
95687c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
95697c478bd9Sstevel@tonic-gate 			continue;
95707c478bd9Sstevel@tonic-gate 
95717c478bd9Sstevel@tonic-gate 		/* Found global. Check for duplicates */
95727c478bd9Sstevel@tonic-gate 		if (done && !quiet) {
95737c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
95747c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
95757c478bd9Sstevel@tonic-gate 		}
95767c478bd9Sstevel@tonic-gate 
95777c478bd9Sstevel@tonic-gate 		arg = lp->arg ? lp->arg : "";
95787c478bd9Sstevel@tonic-gate 		bam_print(GLOBAL_CMD, globalcmd, arg);
95797c478bd9Sstevel@tonic-gate 		done = 1;
95807c478bd9Sstevel@tonic-gate 	}
95817c478bd9Sstevel@tonic-gate 
95827c478bd9Sstevel@tonic-gate 	if (!done && bam_verbose)
95837c478bd9Sstevel@tonic-gate 		bam_print(NO_ENTRY, globalcmd);
95847c478bd9Sstevel@tonic-gate 
95857c478bd9Sstevel@tonic-gate 	return (ret);
95867c478bd9Sstevel@tonic-gate }
95877c478bd9Sstevel@tonic-gate 
95887c478bd9Sstevel@tonic-gate static error_t
95897c478bd9Sstevel@tonic-gate menu_write(char *root, menu_t *mp)
95907c478bd9Sstevel@tonic-gate {
9591eb2bd662Svikram 	const char *fcn = "menu_write()";
9592eb2bd662Svikram 
9593eb2bd662Svikram 	BAM_DPRINTF((D_MENU_WRITE_ENTER, fcn, root));
95947c478bd9Sstevel@tonic-gate 	return (list2file(root, MENU_TMP, GRUB_MENU, mp->start));
95957c478bd9Sstevel@tonic-gate }
95967c478bd9Sstevel@tonic-gate 
9597eb2bd662Svikram void
95987c478bd9Sstevel@tonic-gate line_free(line_t *lp)
95997c478bd9Sstevel@tonic-gate {
96007c478bd9Sstevel@tonic-gate 	if (lp == NULL)
96017c478bd9Sstevel@tonic-gate 		return;
96027c478bd9Sstevel@tonic-gate 
9603eac223ccSWilliam Kucharski 	if (lp->cmd != NULL)
96047c478bd9Sstevel@tonic-gate 		free(lp->cmd);
96057c478bd9Sstevel@tonic-gate 	if (lp->sep)
96067c478bd9Sstevel@tonic-gate 		free(lp->sep);
96077c478bd9Sstevel@tonic-gate 	if (lp->arg)
96087c478bd9Sstevel@tonic-gate 		free(lp->arg);
96097c478bd9Sstevel@tonic-gate 	if (lp->line)
96107c478bd9Sstevel@tonic-gate 		free(lp->line);
96117c478bd9Sstevel@tonic-gate 	free(lp);
96127c478bd9Sstevel@tonic-gate }
96137c478bd9Sstevel@tonic-gate 
96147c478bd9Sstevel@tonic-gate static void
96157c478bd9Sstevel@tonic-gate linelist_free(line_t *start)
96167c478bd9Sstevel@tonic-gate {
96177c478bd9Sstevel@tonic-gate 	line_t *lp;
96187c478bd9Sstevel@tonic-gate 
96197c478bd9Sstevel@tonic-gate 	while (start) {
96207c478bd9Sstevel@tonic-gate 		lp = start;
96217c478bd9Sstevel@tonic-gate 		start = start->next;
96227c478bd9Sstevel@tonic-gate 		line_free(lp);
96237c478bd9Sstevel@tonic-gate 	}
96247c478bd9Sstevel@tonic-gate }
96257c478bd9Sstevel@tonic-gate 
96267c478bd9Sstevel@tonic-gate static void
96277c478bd9Sstevel@tonic-gate filelist_free(filelist_t *flistp)
96287c478bd9Sstevel@tonic-gate {
96297c478bd9Sstevel@tonic-gate 	linelist_free(flistp->head);
96307c478bd9Sstevel@tonic-gate 	flistp->head = NULL;
96317c478bd9Sstevel@tonic-gate 	flistp->tail = NULL;
96327c478bd9Sstevel@tonic-gate }
96337c478bd9Sstevel@tonic-gate 
96347c478bd9Sstevel@tonic-gate static void
96357c478bd9Sstevel@tonic-gate menu_free(menu_t *mp)
96367c478bd9Sstevel@tonic-gate {
96378c1b6884Sszhou 	entry_t *ent, *tmp;
96387c478bd9Sstevel@tonic-gate 	assert(mp);
96397c478bd9Sstevel@tonic-gate 
96407c478bd9Sstevel@tonic-gate 	if (mp->start)
96417c478bd9Sstevel@tonic-gate 		linelist_free(mp->start);
96428c1b6884Sszhou 	ent = mp->entries;
96438c1b6884Sszhou 	while (ent) {
96448c1b6884Sszhou 		tmp = ent;
96458c1b6884Sszhou 		ent = tmp->next;
96468c1b6884Sszhou 		free(tmp);
96478c1b6884Sszhou 	}
96487c478bd9Sstevel@tonic-gate 
96498c1b6884Sszhou 	free(mp);
96507c478bd9Sstevel@tonic-gate }
96517c478bd9Sstevel@tonic-gate 
96527c478bd9Sstevel@tonic-gate /*
96537c478bd9Sstevel@tonic-gate  * Utility routines
96547c478bd9Sstevel@tonic-gate  */
96557c478bd9Sstevel@tonic-gate 
96567c478bd9Sstevel@tonic-gate 
96577c478bd9Sstevel@tonic-gate /*
96587c478bd9Sstevel@tonic-gate  * Returns 0 on success
96597c478bd9Sstevel@tonic-gate  * Any other value indicates an error
96607c478bd9Sstevel@tonic-gate  */
96617c478bd9Sstevel@tonic-gate static int
9662986fd29aSsetje exec_cmd(char *cmdline, filelist_t *flistp)
96637c478bd9Sstevel@tonic-gate {
96647c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
96657c478bd9Sstevel@tonic-gate 	int ret;
96667c478bd9Sstevel@tonic-gate 	FILE *ptr;
96677c478bd9Sstevel@tonic-gate 	sigset_t set;
96687c478bd9Sstevel@tonic-gate 	void (*disp)(int);
96697c478bd9Sstevel@tonic-gate 
96707c478bd9Sstevel@tonic-gate 	/*
96717c478bd9Sstevel@tonic-gate 	 * For security
96727c478bd9Sstevel@tonic-gate 	 * - only absolute paths are allowed
96737c478bd9Sstevel@tonic-gate 	 * - set IFS to space and tab
96747c478bd9Sstevel@tonic-gate 	 */
96757c478bd9Sstevel@tonic-gate 	if (*cmdline != '/') {
96767c478bd9Sstevel@tonic-gate 		bam_error(ABS_PATH_REQ, cmdline);
96777c478bd9Sstevel@tonic-gate 		return (-1);
96787c478bd9Sstevel@tonic-gate 	}
96797c478bd9Sstevel@tonic-gate 	(void) putenv("IFS= \t");
96807c478bd9Sstevel@tonic-gate 
96817c478bd9Sstevel@tonic-gate 	/*
96827c478bd9Sstevel@tonic-gate 	 * We may have been exec'ed with SIGCHLD blocked
96837c478bd9Sstevel@tonic-gate 	 * unblock it here
96847c478bd9Sstevel@tonic-gate 	 */
96857c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
96867c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
96877c478bd9Sstevel@tonic-gate 	if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
96887c478bd9Sstevel@tonic-gate 		bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno));
96897c478bd9Sstevel@tonic-gate 		return (-1);
96907c478bd9Sstevel@tonic-gate 	}
96917c478bd9Sstevel@tonic-gate 
96927c478bd9Sstevel@tonic-gate 	/*
96937c478bd9Sstevel@tonic-gate 	 * Set SIGCHLD disposition to SIG_DFL for popen/pclose
96947c478bd9Sstevel@tonic-gate 	 */
96957c478bd9Sstevel@tonic-gate 	disp = sigset(SIGCHLD, SIG_DFL);
96967c478bd9Sstevel@tonic-gate 	if (disp == SIG_ERR) {
96977c478bd9Sstevel@tonic-gate 		bam_error(FAILED_SIG, strerror(errno));
96987c478bd9Sstevel@tonic-gate 		return (-1);
96997c478bd9Sstevel@tonic-gate 	}
97007c478bd9Sstevel@tonic-gate 	if (disp == SIG_HOLD) {
97017c478bd9Sstevel@tonic-gate 		bam_error(BLOCKED_SIG, cmdline);
97027c478bd9Sstevel@tonic-gate 		return (-1);
97037c478bd9Sstevel@tonic-gate 	}
97047c478bd9Sstevel@tonic-gate 
97057c478bd9Sstevel@tonic-gate 	ptr = popen(cmdline, "r");
97067c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
97077c478bd9Sstevel@tonic-gate 		bam_error(POPEN_FAIL, cmdline, strerror(errno));
97087c478bd9Sstevel@tonic-gate 		return (-1);
97097c478bd9Sstevel@tonic-gate 	}
97107c478bd9Sstevel@tonic-gate 
97117c478bd9Sstevel@tonic-gate 	/*
97127c478bd9Sstevel@tonic-gate 	 * If we simply do a pclose() following a popen(), pclose()
97137c478bd9Sstevel@tonic-gate 	 * will close the reader end of the pipe immediately even
97147c478bd9Sstevel@tonic-gate 	 * if the child process has not started/exited. pclose()
97157c478bd9Sstevel@tonic-gate 	 * does wait for cmd to terminate before returning though.
97167c478bd9Sstevel@tonic-gate 	 * When the executed command writes its output to the pipe
97177c478bd9Sstevel@tonic-gate 	 * there is no reader process and the command dies with
97187c478bd9Sstevel@tonic-gate 	 * SIGPIPE. To avoid this we read repeatedly until read
97197c478bd9Sstevel@tonic-gate 	 * terminates with EOF. This indicates that the command
97207c478bd9Sstevel@tonic-gate 	 * (writer) has closed the pipe and we can safely do a
97217c478bd9Sstevel@tonic-gate 	 * pclose().
97227c478bd9Sstevel@tonic-gate 	 *
97237c478bd9Sstevel@tonic-gate 	 * Since pclose() does wait for the command to exit,
97247c478bd9Sstevel@tonic-gate 	 * we can safely reap the exit status of the command
97257c478bd9Sstevel@tonic-gate 	 * from the value returned by pclose()
97267c478bd9Sstevel@tonic-gate 	 */
9727986fd29aSsetje 	while (s_fgets(buf, sizeof (buf), ptr) != NULL) {
9728986fd29aSsetje 		if (flistp == NULL) {
9729986fd29aSsetje 			/* s_fgets strips newlines, so insert them at the end */
9730986fd29aSsetje 			bam_print(PRINT, buf);
9731986fd29aSsetje 		} else {
9732986fd29aSsetje 			append_to_flist(flistp, buf);
97337c478bd9Sstevel@tonic-gate 		}
97347c478bd9Sstevel@tonic-gate 	}
97357c478bd9Sstevel@tonic-gate 
97367c478bd9Sstevel@tonic-gate 	ret = pclose(ptr);
97377c478bd9Sstevel@tonic-gate 	if (ret == -1) {
97387c478bd9Sstevel@tonic-gate 		bam_error(PCLOSE_FAIL, cmdline, strerror(errno));
97397c478bd9Sstevel@tonic-gate 		return (-1);
97407c478bd9Sstevel@tonic-gate 	}
97417c478bd9Sstevel@tonic-gate 
97427c478bd9Sstevel@tonic-gate 	if (WIFEXITED(ret)) {
97437c478bd9Sstevel@tonic-gate 		return (WEXITSTATUS(ret));
97447c478bd9Sstevel@tonic-gate 	} else {
97457c478bd9Sstevel@tonic-gate 		bam_error(EXEC_FAIL, cmdline, ret);
97467c478bd9Sstevel@tonic-gate 		return (-1);
97477c478bd9Sstevel@tonic-gate 	}
97487c478bd9Sstevel@tonic-gate }
97497c478bd9Sstevel@tonic-gate 
97507c478bd9Sstevel@tonic-gate /*
97517c478bd9Sstevel@tonic-gate  * Since this function returns -1 on error
97527c478bd9Sstevel@tonic-gate  * it cannot be used to convert -1. However,
97537c478bd9Sstevel@tonic-gate  * that is sufficient for what we need.
97547c478bd9Sstevel@tonic-gate  */
97557c478bd9Sstevel@tonic-gate static long
97567c478bd9Sstevel@tonic-gate s_strtol(char *str)
97577c478bd9Sstevel@tonic-gate {
97587c478bd9Sstevel@tonic-gate 	long l;
97597c478bd9Sstevel@tonic-gate 	char *res = NULL;
97607c478bd9Sstevel@tonic-gate 
97617c478bd9Sstevel@tonic-gate 	if (str == NULL) {
97627c478bd9Sstevel@tonic-gate 		return (-1);
97637c478bd9Sstevel@tonic-gate 	}
97647c478bd9Sstevel@tonic-gate 
97657c478bd9Sstevel@tonic-gate 	errno = 0;
97667c478bd9Sstevel@tonic-gate 	l = strtol(str, &res, 10);
97677c478bd9Sstevel@tonic-gate 	if (errno || *res != '\0') {
97687c478bd9Sstevel@tonic-gate 		return (-1);
97697c478bd9Sstevel@tonic-gate 	}
97707c478bd9Sstevel@tonic-gate 
97717c478bd9Sstevel@tonic-gate 	return (l);
97727c478bd9Sstevel@tonic-gate }
97737c478bd9Sstevel@tonic-gate 
97747c478bd9Sstevel@tonic-gate /*
97757c478bd9Sstevel@tonic-gate  * Wrapper around fputs, that adds a newline (since fputs doesn't)
97767c478bd9Sstevel@tonic-gate  */
97777c478bd9Sstevel@tonic-gate static int
97787c478bd9Sstevel@tonic-gate s_fputs(char *str, FILE *fp)
97797c478bd9Sstevel@tonic-gate {
97807c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
97817c478bd9Sstevel@tonic-gate 
97827c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s\n", str);
97837c478bd9Sstevel@tonic-gate 	return (fputs(linebuf, fp));
97847c478bd9Sstevel@tonic-gate }
97857c478bd9Sstevel@tonic-gate 
97867c478bd9Sstevel@tonic-gate /*
97877c478bd9Sstevel@tonic-gate  * Wrapper around fgets, that strips newlines returned by fgets
97887c478bd9Sstevel@tonic-gate  */
9789ae115bc7Smrj char *
97907c478bd9Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp)
97917c478bd9Sstevel@tonic-gate {
97927c478bd9Sstevel@tonic-gate 	int n;
97937c478bd9Sstevel@tonic-gate 
97947c478bd9Sstevel@tonic-gate 	buf = fgets(buf, buflen, fp);
97957c478bd9Sstevel@tonic-gate 	if (buf) {
97967c478bd9Sstevel@tonic-gate 		n = strlen(buf);
97977c478bd9Sstevel@tonic-gate 		if (n == buflen - 1 && buf[n-1] != '\n')
97987c478bd9Sstevel@tonic-gate 			bam_error(TOO_LONG, buflen - 1, buf);
97997c478bd9Sstevel@tonic-gate 		buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1];
98007c478bd9Sstevel@tonic-gate 	}
98017c478bd9Sstevel@tonic-gate 
98027c478bd9Sstevel@tonic-gate 	return (buf);
98037c478bd9Sstevel@tonic-gate }
98047c478bd9Sstevel@tonic-gate 
9805ae115bc7Smrj void *
98067c478bd9Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz)
98077c478bd9Sstevel@tonic-gate {
98087c478bd9Sstevel@tonic-gate 	void *ptr;
98097c478bd9Sstevel@tonic-gate 
98107c478bd9Sstevel@tonic-gate 	ptr = calloc(nelem, sz);
98117c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
98127c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, nelem*sz);
98137c478bd9Sstevel@tonic-gate 		bam_exit(1);
98147c478bd9Sstevel@tonic-gate 	}
98157c478bd9Sstevel@tonic-gate 	return (ptr);
98167c478bd9Sstevel@tonic-gate }
98177c478bd9Sstevel@tonic-gate 
9818ae115bc7Smrj void *
9819ae115bc7Smrj s_realloc(void *ptr, size_t sz)
9820ae115bc7Smrj {
9821ae115bc7Smrj 	ptr = realloc(ptr, sz);
9822ae115bc7Smrj 	if (ptr == NULL) {
9823ae115bc7Smrj 		bam_error(NO_MEM, sz);
9824ae115bc7Smrj 		bam_exit(1);
9825ae115bc7Smrj 	}
9826ae115bc7Smrj 	return (ptr);
9827ae115bc7Smrj }
9828ae115bc7Smrj 
9829eb2bd662Svikram char *
98307c478bd9Sstevel@tonic-gate s_strdup(char *str)
98317c478bd9Sstevel@tonic-gate {
98327c478bd9Sstevel@tonic-gate 	char *ptr;
98337c478bd9Sstevel@tonic-gate 
98347c478bd9Sstevel@tonic-gate 	if (str == NULL)
98357c478bd9Sstevel@tonic-gate 		return (NULL);
98367c478bd9Sstevel@tonic-gate 
98377c478bd9Sstevel@tonic-gate 	ptr = strdup(str);
98387c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
98397c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, strlen(str) + 1);
98407c478bd9Sstevel@tonic-gate 		bam_exit(1);
98417c478bd9Sstevel@tonic-gate 	}
98427c478bd9Sstevel@tonic-gate 	return (ptr);
98437c478bd9Sstevel@tonic-gate }
98447c478bd9Sstevel@tonic-gate 
98457c478bd9Sstevel@tonic-gate /*
98467c478bd9Sstevel@tonic-gate  * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients)
98477c478bd9Sstevel@tonic-gate  * Returns 0 otherwise
98487c478bd9Sstevel@tonic-gate  */
98497c478bd9Sstevel@tonic-gate static int
98507c478bd9Sstevel@tonic-gate is_amd64(void)
98517c478bd9Sstevel@tonic-gate {
98527c478bd9Sstevel@tonic-gate 	static int amd64 = -1;
98537c478bd9Sstevel@tonic-gate 	char isabuf[257];	/* from sysinfo(2) manpage */
98547c478bd9Sstevel@tonic-gate 
98557c478bd9Sstevel@tonic-gate 	if (amd64 != -1)
98567c478bd9Sstevel@tonic-gate 		return (amd64);
98577c478bd9Sstevel@tonic-gate 
9858d876c67dSjg 	if (bam_alt_platform) {
9859d876c67dSjg 		if (strcmp(bam_platform, "i86pc") == 0) {
98607c478bd9Sstevel@tonic-gate 			amd64 = 1;		/* diskless server */
9861d876c67dSjg 		}
9862d876c67dSjg 	} else {
9863d876c67dSjg 		if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 &&
9864d876c67dSjg 		    strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0) {
9865d876c67dSjg 			amd64 = 1;
9866d876c67dSjg 		} else if (strstr(isabuf, "i386") == NULL) {
9867d876c67dSjg 			amd64 = 1;		/* diskless server */
9868d876c67dSjg 		}
9869d876c67dSjg 	}
9870d876c67dSjg 	if (amd64 == -1)
98717c478bd9Sstevel@tonic-gate 		amd64 = 0;
98727c478bd9Sstevel@tonic-gate 
98737c478bd9Sstevel@tonic-gate 	return (amd64);
98747c478bd9Sstevel@tonic-gate }
98757c478bd9Sstevel@tonic-gate 
987679755401Ssetje static char *
987779755401Ssetje get_machine(void)
9878986fd29aSsetje {
987979755401Ssetje 	static int cached = -1;
988079755401Ssetje 	static char mbuf[257];	/* from sysinfo(2) manpage */
9881986fd29aSsetje 
988279755401Ssetje 	if (cached == 0)
988379755401Ssetje 		return (mbuf);
9884d876c67dSjg 
9885d876c67dSjg 	if (bam_alt_platform) {
988679755401Ssetje 		return (bam_platform);
9887d876c67dSjg 	} else {
988879755401Ssetje 		if (sysinfo(SI_MACHINE, mbuf, sizeof (mbuf)) > 0) {
988979755401Ssetje 			cached = 1;
9890d876c67dSjg 		}
9891d876c67dSjg 	}
989279755401Ssetje 	if (cached == -1) {
989379755401Ssetje 		mbuf[0] = '\0';
989479755401Ssetje 		cached = 0;
9895986fd29aSsetje 	}
9896986fd29aSsetje 
989779755401Ssetje 	return (mbuf);
9898986fd29aSsetje }
9899986fd29aSsetje 
9900eb2bd662Svikram int
9901eb2bd662Svikram is_sparc(void)
9902eb2bd662Svikram {
990379755401Ssetje 	static int issparc = -1;
990479755401Ssetje 	char mbuf[257];	/* from sysinfo(2) manpage */
990579755401Ssetje 
990679755401Ssetje 	if (issparc != -1)
990779755401Ssetje 		return (issparc);
990879755401Ssetje 
990979755401Ssetje 	if (bam_alt_platform) {
991079755401Ssetje 		if (strncmp(bam_platform, "sun4", 4) == 0) {
991179755401Ssetje 			issparc = 1;
991279755401Ssetje 		}
991379755401Ssetje 	} else {
991479755401Ssetje 		if (sysinfo(SI_ARCHITECTURE, mbuf, sizeof (mbuf)) > 0 &&
9915d7d0f93bSjg 		    strcmp(mbuf, "sparc") == 0) {
991679755401Ssetje 			issparc = 1;
991779755401Ssetje 		}
9918d7d0f93bSjg 	}
9919d7d0f93bSjg 	if (issparc == -1)
9920d7d0f93bSjg 		issparc = 0;
992179755401Ssetje 
992279755401Ssetje 	return (issparc);
9923eb2bd662Svikram }
9924986fd29aSsetje 
99257c478bd9Sstevel@tonic-gate static void
99267c478bd9Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s)
99277c478bd9Sstevel@tonic-gate {
99287c478bd9Sstevel@tonic-gate 	line_t *lp;
99297c478bd9Sstevel@tonic-gate 
99307c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
99317c478bd9Sstevel@tonic-gate 	lp->line = s_strdup(s);
99327c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL)
99337c478bd9Sstevel@tonic-gate 		flistp->head = lp;
99347c478bd9Sstevel@tonic-gate 	else
99357c478bd9Sstevel@tonic-gate 		flistp->tail->next = lp;
99367c478bd9Sstevel@tonic-gate 	flistp->tail = lp;
99377c478bd9Sstevel@tonic-gate }
99382449e17fSsherrym 
993979c28b70SToomas Soome #if !defined(_OBP)
99402449e17fSsherrym 
99412449e17fSsherrym UCODE_VENDORS;
99422449e17fSsherrym 
99432449e17fSsherrym /*ARGSUSED*/
99442449e17fSsherrym static void
99452449e17fSsherrym ucode_install(char *root)
99462449e17fSsherrym {
99472449e17fSsherrym 	int i;
99482449e17fSsherrym 
99492449e17fSsherrym 	for (i = 0; ucode_vendors[i].filestr != NULL; i++) {
99502449e17fSsherrym 		int cmd_len = PATH_MAX + 256;
99512449e17fSsherrym 		char cmd[PATH_MAX + 256];
99522449e17fSsherrym 		char file[PATH_MAX];
99532449e17fSsherrym 		char timestamp[PATH_MAX];
99542449e17fSsherrym 		struct stat fstatus, tstatus;
99552449e17fSsherrym 		struct utimbuf u_times;
99562449e17fSsherrym 
9957adc586deSMark Johnson 		(void) snprintf(file, PATH_MAX, "%s/%s/%s-ucode.%s",
9958adc586deSMark Johnson 		    bam_root, UCODE_INSTALL_PATH, ucode_vendors[i].filestr,
9959adc586deSMark Johnson 		    ucode_vendors[i].extstr);
99602449e17fSsherrym 
99612449e17fSsherrym 		if (stat(file, &fstatus) != 0 || !(S_ISREG(fstatus.st_mode)))
99622449e17fSsherrym 			continue;
99632449e17fSsherrym 
99642449e17fSsherrym 		(void) snprintf(timestamp, PATH_MAX, "%s.ts", file);
99652449e17fSsherrym 
99662449e17fSsherrym 		if (stat(timestamp, &tstatus) == 0 &&
99672449e17fSsherrym 		    fstatus.st_mtime <= tstatus.st_mtime)
99682449e17fSsherrym 			continue;
99692449e17fSsherrym 
99702449e17fSsherrym 		(void) snprintf(cmd, cmd_len, "/usr/sbin/ucodeadm -i -R "
99712449e17fSsherrym 		    "%s/%s/%s %s > /dev/null 2>&1", bam_root,
99722449e17fSsherrym 		    UCODE_INSTALL_PATH, ucode_vendors[i].vendorstr, file);
99732449e17fSsherrym 		if (system(cmd) != 0)
99742449e17fSsherrym 			return;
99752449e17fSsherrym 
99762449e17fSsherrym 		if (creat(timestamp, S_IRUSR | S_IWUSR) == -1)
99772449e17fSsherrym 			return;
99782449e17fSsherrym 
99792449e17fSsherrym 		u_times.actime = fstatus.st_atime;
99802449e17fSsherrym 		u_times.modtime = fstatus.st_mtime;
99812449e17fSsherrym 		(void) utime(timestamp, &u_times);
99822449e17fSsherrym 	}
99832449e17fSsherrym }
99842449e17fSsherrym #endif
9985