xref: /titanic_50/usr/src/cmd/boot/bootadm/bootadm.c (revision ae115bc77f6fcde83175c75b4206dc2e50747966)
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 /*
22bff269d0Svikram  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * bootadm(1M) is a new utility for managing bootability of
307c478bd9Sstevel@tonic-gate  * Solaris *Newboot* environments. It has two primary tasks:
317c478bd9Sstevel@tonic-gate  * 	- Allow end users to manage bootability of Newboot Solaris instances
327c478bd9Sstevel@tonic-gate  *	- Provide services to other subsystems in Solaris (primarily Install)
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /* Headers */
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <stdarg.h>
447c478bd9Sstevel@tonic-gate #include <limits.h>
457c478bd9Sstevel@tonic-gate #include <signal.h>
467c478bd9Sstevel@tonic-gate #include <sys/wait.h>
477c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
487c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
497c478bd9Sstevel@tonic-gate #include <libnvpair.h>
507c478bd9Sstevel@tonic-gate #include <ftw.h>
517c478bd9Sstevel@tonic-gate #include <fcntl.h>
527c478bd9Sstevel@tonic-gate #include <strings.h>
537c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
547c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
5558091fd8Ssetje #include <sys/param.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include <pwd.h>
587c478bd9Sstevel@tonic-gate #include <grp.h>
597c478bd9Sstevel@tonic-gate #include <device_info.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #include <locale.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include <assert.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #include "message.h"
66*ae115bc7Smrj #include "bootadm.h"
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
697c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
707c478bd9Sstevel@tonic-gate #endif	/* TEXT_DOMAIN */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /* Type definitions */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /* Primary subcmds */
757c478bd9Sstevel@tonic-gate typedef enum {
767c478bd9Sstevel@tonic-gate 	BAM_MENU = 3,
777c478bd9Sstevel@tonic-gate 	BAM_ARCHIVE
787c478bd9Sstevel@tonic-gate } subcmd_t;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate typedef enum {
817c478bd9Sstevel@tonic-gate     OPT_ABSENT = 0,	/* No option */
827c478bd9Sstevel@tonic-gate     OPT_REQ,		/* option required */
837c478bd9Sstevel@tonic-gate     OPT_OPTIONAL	/* option may or may not be present */
847c478bd9Sstevel@tonic-gate } option_t;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate typedef struct {
877c478bd9Sstevel@tonic-gate 	char	*subcmd;
887c478bd9Sstevel@tonic-gate 	option_t option;
897c478bd9Sstevel@tonic-gate 	error_t (*handler)();
901a97e40eSvikram 	int	unpriv;			/* is this an unprivileged command */
917c478bd9Sstevel@tonic-gate } subcmd_defn_t;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #define	LINE_INIT	0	/* lineNum initial value */
947c478bd9Sstevel@tonic-gate #define	ENTRY_INIT	-1	/* entryNum initial value */
957c478bd9Sstevel@tonic-gate #define	ALL_ENTRIES	-2	/* selects all boot entries */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #define	GRUB_DIR		"/boot/grub"
987c478bd9Sstevel@tonic-gate #define	GRUB_MENU		"/boot/grub/menu.lst"
997c478bd9Sstevel@tonic-gate #define	MENU_TMP		"/boot/grub/menu.lst.tmp"
1007c478bd9Sstevel@tonic-gate #define	RAMDISK_SPECIAL		"/ramdisk"
10140541d5dSvikram #define	STUBBOOT		"/stubboot"
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /* lock related */
1047c478bd9Sstevel@tonic-gate #define	BAM_LOCK_FILE		"/var/run/bootadm.lock"
1057c478bd9Sstevel@tonic-gate #define	LOCK_FILE_PERMS		(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #define	CREATE_RAMDISK		"/boot/solaris/bin/create_ramdisk"
1087c478bd9Sstevel@tonic-gate #define	CREATE_DISKMAP		"/boot/solaris/bin/create_diskmap"
1097c478bd9Sstevel@tonic-gate #define	GRUBDISK_MAP		"/var/run/solaris_grubdisk.map"
1107c478bd9Sstevel@tonic-gate 
111b610f78eSvikram #define	GRUB_slice		"/etc/lu/GRUB_slice"
112b610f78eSvikram #define	GRUB_root		"/etc/lu/GRUB_root"
113b610f78eSvikram #define	GRUB_backup_menu	"/etc/lu/GRUB_backup_menu"
114b610f78eSvikram #define	GRUB_slice_mntpt	"/tmp/GRUB_slice_mntpt"
115b610f78eSvikram #define	LU_ACTIVATE_FILE	"/etc/lu/DelayUpdate/activate.sh"
116fbac2b2bSvikram #define	GRUB_fdisk		"/etc/lu/GRUB_fdisk"
117fbac2b2bSvikram #define	GRUB_fdisk_target	"/etc/lu/GRUB_fdisk_target"
118b610f78eSvikram 
119b610f78eSvikram #define	INSTALLGRUB		"/sbin/installgrub"
120b610f78eSvikram #define	STAGE1			"/boot/grub/stage1"
121b610f78eSvikram #define	STAGE2			"/boot/grub/stage2"
122b610f78eSvikram 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * Default file attributes
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_MODE	0644	/* default permissions */
1277c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_UID		0	/* user root */
1287c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_GID		3	/* group sys */
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate  * Menu related
1327c478bd9Sstevel@tonic-gate  * menu_cmd_t and menu_cmds must be kept in sync
1337c478bd9Sstevel@tonic-gate  */
134*ae115bc7Smrj char *menu_cmds[] = {
1357c478bd9Sstevel@tonic-gate 	"default",	/* DEFAULT_CMD */
1367c478bd9Sstevel@tonic-gate 	"timeout",	/* TIMEOUT_CMD */
1377c478bd9Sstevel@tonic-gate 	"title",	/* TITLE_CMD */
1387c478bd9Sstevel@tonic-gate 	"root",		/* ROOT_CMD */
1397c478bd9Sstevel@tonic-gate 	"kernel",	/* KERNEL_CMD */
140*ae115bc7Smrj 	"kernel$",	/* KERNEL_DOLLAR_CMD */
1417c478bd9Sstevel@tonic-gate 	"module",	/* MODULE_CMD */
142*ae115bc7Smrj 	"module$",	/* MODULE_DOLLAR_CMD */
1437c478bd9Sstevel@tonic-gate 	" ",		/* SEP_CMD */
1447c478bd9Sstevel@tonic-gate 	"#",		/* COMMENT_CMD */
145*ae115bc7Smrj 	"chainloader",	/* CHAINLOADER_CMD */
146*ae115bc7Smrj 	"args",		/* ARGS_CMD */
1477c478bd9Sstevel@tonic-gate 	NULL
1487c478bd9Sstevel@tonic-gate };
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate #define	OPT_ENTRY_NUM	"entry"
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * archive related
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate typedef struct {
1567c478bd9Sstevel@tonic-gate 	line_t *head;
1577c478bd9Sstevel@tonic-gate 	line_t *tail;
1587c478bd9Sstevel@tonic-gate } filelist_t;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate #define	BOOT_FILE_LIST	"boot/solaris/filelist.ramdisk"
1617c478bd9Sstevel@tonic-gate #define	ETC_FILE_LIST	"etc/boot/solaris/filelist.ramdisk"
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate #define	FILE_STAT	"boot/solaris/filestat.ramdisk"
1647c478bd9Sstevel@tonic-gate #define	FILE_STAT_TMP	"boot/solaris/filestat.ramdisk.tmp"
1657c478bd9Sstevel@tonic-gate #define	DIR_PERMS	(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
1667c478bd9Sstevel@tonic-gate #define	FILE_STAT_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /* Globals */
169*ae115bc7Smrj int bam_verbose;
170*ae115bc7Smrj int bam_force;
1717c478bd9Sstevel@tonic-gate static char *prog;
1727c478bd9Sstevel@tonic-gate static subcmd_t bam_cmd;
1737c478bd9Sstevel@tonic-gate static char *bam_root;
1747c478bd9Sstevel@tonic-gate static int bam_rootlen;
1757c478bd9Sstevel@tonic-gate static int bam_root_readonly;
17640541d5dSvikram static int bam_alt_root;
1777c478bd9Sstevel@tonic-gate static char *bam_subcmd;
1787c478bd9Sstevel@tonic-gate static char *bam_opt;
1797c478bd9Sstevel@tonic-gate static int bam_debug;
1807c478bd9Sstevel@tonic-gate static char **bam_argv;
1817c478bd9Sstevel@tonic-gate static int bam_argc;
1827c478bd9Sstevel@tonic-gate static int bam_check;
1837c478bd9Sstevel@tonic-gate static int bam_smf_check;
1847c478bd9Sstevel@tonic-gate static int bam_lock_fd = -1;
1857c478bd9Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/";
186b610f78eSvikram static int bam_update_all;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /* function prototypes */
1897c478bd9Sstevel@tonic-gate static void parse_args_internal(int argc, char *argv[]);
1907c478bd9Sstevel@tonic-gate static void parse_args(int argc, char *argv[]);
1917c478bd9Sstevel@tonic-gate static error_t bam_menu(char *subcmd, char *opt, int argc, char *argv[]);
1927c478bd9Sstevel@tonic-gate static error_t bam_archive(char *subcmd, char *opt);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate static void bam_print(char *format, ...);
1957c478bd9Sstevel@tonic-gate static void bam_exit(int excode);
1967c478bd9Sstevel@tonic-gate static void bam_lock(void);
1977c478bd9Sstevel@tonic-gate static void bam_unlock(void);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate static int exec_cmd(char *cmdline, char *output, int64_t osize);
2007c478bd9Sstevel@tonic-gate static error_t read_globals(menu_t *mp, char *menu_path,
2017c478bd9Sstevel@tonic-gate     char *globalcmd, int quiet);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate static menu_t *menu_read(char *menu_path);
2047c478bd9Sstevel@tonic-gate static error_t menu_write(char *root, menu_t *mp);
2057c478bd9Sstevel@tonic-gate static void linelist_free(line_t *start);
2067c478bd9Sstevel@tonic-gate static void menu_free(menu_t *mp);
2077c478bd9Sstevel@tonic-gate static void line_free(line_t *lp);
2087c478bd9Sstevel@tonic-gate static void filelist_free(filelist_t *flistp);
2097c478bd9Sstevel@tonic-gate static error_t list2file(char *root, char *tmp,
2107c478bd9Sstevel@tonic-gate     char *final, line_t *start);
2117c478bd9Sstevel@tonic-gate static error_t list_entry(menu_t *mp, char *menu_path, char *opt);
2127c478bd9Sstevel@tonic-gate static error_t delete_all_entries(menu_t *mp, char *menu_path, char *opt);
2137c478bd9Sstevel@tonic-gate static error_t update_entry(menu_t *mp, char *root, char *opt);
2147c478bd9Sstevel@tonic-gate static error_t update_temp(menu_t *mp, char *root, char *opt);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate static error_t update_archive(char *root, char *opt);
2177c478bd9Sstevel@tonic-gate static error_t list_archive(char *root, char *opt);
2187c478bd9Sstevel@tonic-gate static error_t update_all(char *root, char *opt);
2197c478bd9Sstevel@tonic-gate static error_t read_list(char *root, filelist_t  *flistp);
2207c478bd9Sstevel@tonic-gate static error_t set_global(menu_t *mp, char *globalcmd, int val);
2217c478bd9Sstevel@tonic-gate static error_t set_option(menu_t *mp, char *globalcmd, char *opt);
222*ae115bc7Smrj static error_t set_kernel(menu_t *mp, menu_cmd_t optnum, char *path,
223*ae115bc7Smrj     char *buf, size_t bufsize);
224*ae115bc7Smrj static char *expand_path(const char *partial_path);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate static long s_strtol(char *str);
2277c478bd9Sstevel@tonic-gate static int s_fputs(char *str, FILE *fp);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate static char *s_strdup(char *str);
2307c478bd9Sstevel@tonic-gate static int is_readonly(char *);
2317c478bd9Sstevel@tonic-gate static int is_amd64(void);
2327c478bd9Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate #if defined(__sparc)
2357c478bd9Sstevel@tonic-gate static void sparc_abort(void);
2367c478bd9Sstevel@tonic-gate #endif
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /* Menu related sub commands */
2397c478bd9Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = {
2401a97e40eSvikram 	"set_option",		OPT_OPTIONAL,	set_option, 0,	/* PUB */
2411a97e40eSvikram 	"list_entry",		OPT_OPTIONAL,	list_entry, 1,	/* PUB */
2421a97e40eSvikram 	"delete_all_entries",	OPT_ABSENT,	delete_all_entries, 0, /* PVT */
2431a97e40eSvikram 	"update_entry",		OPT_REQ,	update_entry, 0, /* menu */
2441a97e40eSvikram 	"update_temp",		OPT_OPTIONAL,	update_temp, 0,	/* reboot */
245*ae115bc7Smrj 	"upgrade",		OPT_ABSENT,	upgrade_menu, 0, /* menu */
2461a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
2477c478bd9Sstevel@tonic-gate };
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /* Archive related sub commands */
2507c478bd9Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = {
2511a97e40eSvikram 	"update",		OPT_ABSENT,	update_archive, 0, /* PUB */
2521a97e40eSvikram 	"update_all",		OPT_ABSENT,	update_all, 0,	/* PVT */
2531a97e40eSvikram 	"list",			OPT_OPTIONAL,	list_archive, 1, /* PUB */
2541a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
2557c478bd9Sstevel@tonic-gate };
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate static struct {
2587c478bd9Sstevel@tonic-gate 	nvlist_t *new_nvlp;
2597c478bd9Sstevel@tonic-gate 	nvlist_t *old_nvlp;
2607c478bd9Sstevel@tonic-gate 	int need_update;
2617c478bd9Sstevel@tonic-gate } walk_arg;
2627c478bd9Sstevel@tonic-gate 
26358091fd8Ssetje 
2649632cfadSsetje struct safefile {
26558091fd8Ssetje 	char *name;
26658091fd8Ssetje 	struct safefile *next;
26758091fd8Ssetje };
26858091fd8Ssetje 
269*ae115bc7Smrj static struct safefile *safefiles = NULL;
27058091fd8Ssetje #define	NEED_UPDATE_FILE "/etc/svc/volatile/boot_archive_needs_update"
27158091fd8Ssetje 
2727c478bd9Sstevel@tonic-gate static void
2737c478bd9Sstevel@tonic-gate usage(void)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "USAGE:\n");
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/* archive usage */
2797c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s update-archive [-vn] [-R altroot]\n",
2807c478bd9Sstevel@tonic-gate 	    prog);
2817c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-archive [-R altroot]\n", prog);
2827c478bd9Sstevel@tonic-gate #ifndef __sparc
2837c478bd9Sstevel@tonic-gate 	/* x86 only */
2847c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog);
2857c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog);
2867c478bd9Sstevel@tonic-gate #endif
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate int
2907c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
2917c478bd9Sstevel@tonic-gate {
2927c478bd9Sstevel@tonic-gate 	error_t ret;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2957c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
2987c478bd9Sstevel@tonic-gate 		prog = argv[0];
2997c478bd9Sstevel@tonic-gate 	} else {
3007c478bd9Sstevel@tonic-gate 		prog++;
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/*
3057c478bd9Sstevel@tonic-gate 	 * Don't depend on caller's umask
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 	(void) umask(0022);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate #if defined(__sparc)
3127c478bd9Sstevel@tonic-gate 	/*
3137c478bd9Sstevel@tonic-gate 	 * There are only two valid invocations of bootadm
3147c478bd9Sstevel@tonic-gate 	 * on SPARC:
3157c478bd9Sstevel@tonic-gate 	 *
3167c478bd9Sstevel@tonic-gate 	 *	- SPARC diskless server creating boot_archive for i386 clients
3177c478bd9Sstevel@tonic-gate 	 *	- archive creation call during reboot of a SPARC system
3187c478bd9Sstevel@tonic-gate 	 *
3197c478bd9Sstevel@tonic-gate 	 *	The latter should be a NOP
3207c478bd9Sstevel@tonic-gate 	 */
3217c478bd9Sstevel@tonic-gate 	if (bam_cmd != BAM_ARCHIVE) {
3227c478bd9Sstevel@tonic-gate 		sparc_abort();
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate #endif
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	switch (bam_cmd) {
3277c478bd9Sstevel@tonic-gate 		case BAM_MENU:
3287c478bd9Sstevel@tonic-gate 			ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv);
3297c478bd9Sstevel@tonic-gate 			break;
3307c478bd9Sstevel@tonic-gate 		case BAM_ARCHIVE:
3317c478bd9Sstevel@tonic-gate 			ret = bam_archive(bam_subcmd, bam_opt);
3327c478bd9Sstevel@tonic-gate 			break;
3337c478bd9Sstevel@tonic-gate 		default:
3347c478bd9Sstevel@tonic-gate 			usage();
3357c478bd9Sstevel@tonic-gate 			bam_exit(1);
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS)
3397c478bd9Sstevel@tonic-gate 		bam_exit(1);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	bam_unlock();
3427c478bd9Sstevel@tonic-gate 	return (0);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate #if defined(__sparc)
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate static void
3487c478bd9Sstevel@tonic-gate sparc_abort(void)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	bam_error(NOT_ON_SPARC);
3517c478bd9Sstevel@tonic-gate 	bam_exit(1);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate #endif
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate /*
3577c478bd9Sstevel@tonic-gate  * Equivalence of public and internal commands:
3587c478bd9Sstevel@tonic-gate  *	update-archive  -- -a update
3597c478bd9Sstevel@tonic-gate  *	list-archive	-- -a list
3607c478bd9Sstevel@tonic-gate  *	set-menu	-- -m set_option
3617c478bd9Sstevel@tonic-gate  *	list-menu	-- -m list_entry
3627c478bd9Sstevel@tonic-gate  *	update-menu	-- -m update_entry
3637c478bd9Sstevel@tonic-gate  */
3647c478bd9Sstevel@tonic-gate static struct cmd_map {
3657c478bd9Sstevel@tonic-gate 	char *bam_cmdname;
3667c478bd9Sstevel@tonic-gate 	int bam_cmd;
3677c478bd9Sstevel@tonic-gate 	char *bam_subcmd;
3687c478bd9Sstevel@tonic-gate } cmd_map[] = {
3697c478bd9Sstevel@tonic-gate 	{ "update-archive",	BAM_ARCHIVE,	"update"},
3707c478bd9Sstevel@tonic-gate 	{ "list-archive",	BAM_ARCHIVE,	"list"},
3717c478bd9Sstevel@tonic-gate 	{ "set-menu",		BAM_MENU,	"set_option"},
3727c478bd9Sstevel@tonic-gate 	{ "list-menu",		BAM_MENU,	"list_entry"},
3737c478bd9Sstevel@tonic-gate 	{ "update-menu",	BAM_MENU,	"update_entry"},
3747c478bd9Sstevel@tonic-gate 	{ NULL,			0,		NULL}
3757c478bd9Sstevel@tonic-gate };
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate  * Commands syntax published in bootadm(1M) are parsed here
3797c478bd9Sstevel@tonic-gate  */
3807c478bd9Sstevel@tonic-gate static void
3817c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[])
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	struct cmd_map *cmp = cmd_map;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	/* command conforming to the final spec */
3867c478bd9Sstevel@tonic-gate 	if (argc > 1 && argv[1][0] != '-') {
3877c478bd9Sstevel@tonic-gate 		/*
3887c478bd9Sstevel@tonic-gate 		 * Map commands to internal table.
3897c478bd9Sstevel@tonic-gate 		 */
3907c478bd9Sstevel@tonic-gate 		while (cmp->bam_cmdname) {
3917c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], cmp->bam_cmdname) == 0) {
3927c478bd9Sstevel@tonic-gate 				bam_cmd = cmp->bam_cmd;
3937c478bd9Sstevel@tonic-gate 				bam_subcmd = cmp->bam_subcmd;
3947c478bd9Sstevel@tonic-gate 				break;
3957c478bd9Sstevel@tonic-gate 			}
3967c478bd9Sstevel@tonic-gate 			cmp++;
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 		if (cmp->bam_cmdname == NULL) {
3997c478bd9Sstevel@tonic-gate 			usage();
4007c478bd9Sstevel@tonic-gate 			bam_exit(1);
4017c478bd9Sstevel@tonic-gate 		}
4027c478bd9Sstevel@tonic-gate 		argc--;
4037c478bd9Sstevel@tonic-gate 		argv++;
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	parse_args_internal(argc, argv);
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate  * A combination of public and private commands are parsed here.
4117c478bd9Sstevel@tonic-gate  * The internal syntax and the corresponding functionality are:
4127c478bd9Sstevel@tonic-gate  *	-a update	-- update-archive
4137c478bd9Sstevel@tonic-gate  *	-a list		-- list-archive
4147c478bd9Sstevel@tonic-gate  *	-a update-all	-- (reboot to sync all mounted OS archive)
4157c478bd9Sstevel@tonic-gate  *	-m update_entry	-- update-menu
4167c478bd9Sstevel@tonic-gate  *	-m list_entry	-- list-menu
4177c478bd9Sstevel@tonic-gate  *	-m update_temp	-- (reboot -- [boot-args])
4187c478bd9Sstevel@tonic-gate  *	-m delete_all_entries -- (called from install)
4197c478bd9Sstevel@tonic-gate  */
4207c478bd9Sstevel@tonic-gate static void
4217c478bd9Sstevel@tonic-gate parse_args_internal(int argc, char *argv[])
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 	int c, error;
4247c478bd9Sstevel@tonic-gate 	extern char *optarg;
4257c478bd9Sstevel@tonic-gate 	extern int optind, opterr;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	/* Suppress error message from getopt */
4287c478bd9Sstevel@tonic-gate 	opterr = 0;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	error = 0;
431*ae115bc7Smrj 	while ((c = getopt(argc, argv, "a:d:fm:no:vCR:xX")) != -1) {
4327c478bd9Sstevel@tonic-gate 		switch (c) {
4337c478bd9Sstevel@tonic-gate 		case 'a':
4347c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
4357c478bd9Sstevel@tonic-gate 				error = 1;
4367c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
4377c478bd9Sstevel@tonic-gate 			}
4387c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_ARCHIVE;
4397c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
4407c478bd9Sstevel@tonic-gate 			break;
4417c478bd9Sstevel@tonic-gate 		case 'd':
4427c478bd9Sstevel@tonic-gate 			if (bam_debug) {
4437c478bd9Sstevel@tonic-gate 				error = 1;
4447c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4457c478bd9Sstevel@tonic-gate 			}
4467c478bd9Sstevel@tonic-gate 			bam_debug = s_strtol(optarg);
4477c478bd9Sstevel@tonic-gate 			break;
4487c478bd9Sstevel@tonic-gate 		case 'f':
4497c478bd9Sstevel@tonic-gate 			if (bam_force) {
4507c478bd9Sstevel@tonic-gate 				error = 1;
4517c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4527c478bd9Sstevel@tonic-gate 			}
4537c478bd9Sstevel@tonic-gate 			bam_force = 1;
4547c478bd9Sstevel@tonic-gate 			break;
4557c478bd9Sstevel@tonic-gate 		case 'm':
4567c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
4577c478bd9Sstevel@tonic-gate 				error = 1;
4587c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
4597c478bd9Sstevel@tonic-gate 			}
4607c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_MENU;
4617c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
4627c478bd9Sstevel@tonic-gate 			break;
4637c478bd9Sstevel@tonic-gate 		case 'n':
4647c478bd9Sstevel@tonic-gate 			if (bam_check) {
4657c478bd9Sstevel@tonic-gate 				error = 1;
4667c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4677c478bd9Sstevel@tonic-gate 			}
4687c478bd9Sstevel@tonic-gate 			bam_check = 1;
4697c478bd9Sstevel@tonic-gate 			break;
4707c478bd9Sstevel@tonic-gate 		case 'o':
4717c478bd9Sstevel@tonic-gate 			if (bam_opt) {
4727c478bd9Sstevel@tonic-gate 				error = 1;
4737c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4747c478bd9Sstevel@tonic-gate 			}
4757c478bd9Sstevel@tonic-gate 			bam_opt = optarg;
4767c478bd9Sstevel@tonic-gate 			break;
4777c478bd9Sstevel@tonic-gate 		case 'v':
4787c478bd9Sstevel@tonic-gate 			if (bam_verbose) {
4797c478bd9Sstevel@tonic-gate 				error = 1;
4807c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4817c478bd9Sstevel@tonic-gate 			}
4827c478bd9Sstevel@tonic-gate 			bam_verbose = 1;
4837c478bd9Sstevel@tonic-gate 			break;
4848c1b6884Sszhou 		case 'C':
4858c1b6884Sszhou 			bam_smf_check = 1;
4868c1b6884Sszhou 			break;
4877c478bd9Sstevel@tonic-gate 		case 'R':
4887c478bd9Sstevel@tonic-gate 			if (bam_root) {
4897c478bd9Sstevel@tonic-gate 				error = 1;
4907c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4917c478bd9Sstevel@tonic-gate 				break;
4927c478bd9Sstevel@tonic-gate 			} else if (realpath(optarg, rootbuf) == NULL) {
4937c478bd9Sstevel@tonic-gate 				error = 1;
4947c478bd9Sstevel@tonic-gate 				bam_error(CANT_RESOLVE, optarg,
4957c478bd9Sstevel@tonic-gate 				    strerror(errno));
4967c478bd9Sstevel@tonic-gate 				break;
4977c478bd9Sstevel@tonic-gate 			}
49840541d5dSvikram 			bam_alt_root = 1;
4997c478bd9Sstevel@tonic-gate 			bam_root = rootbuf;
5007c478bd9Sstevel@tonic-gate 			bam_rootlen = strlen(rootbuf);
5017c478bd9Sstevel@tonic-gate 			break;
5027c478bd9Sstevel@tonic-gate 		case '?':
5037c478bd9Sstevel@tonic-gate 			error = 1;
5047c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, optopt);
5057c478bd9Sstevel@tonic-gate 			break;
5067c478bd9Sstevel@tonic-gate 		default :
5077c478bd9Sstevel@tonic-gate 			error = 1;
5087c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, c);
5097c478bd9Sstevel@tonic-gate 			break;
5107c478bd9Sstevel@tonic-gate 		}
5117c478bd9Sstevel@tonic-gate 	}
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	/*
5147c478bd9Sstevel@tonic-gate 	 * A command option must be specfied
5157c478bd9Sstevel@tonic-gate 	 */
5167c478bd9Sstevel@tonic-gate 	if (!bam_cmd) {
5177c478bd9Sstevel@tonic-gate 		if (bam_opt && strcmp(bam_opt, "all") == 0) {
5187c478bd9Sstevel@tonic-gate 			usage();
5197c478bd9Sstevel@tonic-gate 			bam_exit(0);
5207c478bd9Sstevel@tonic-gate 		}
5217c478bd9Sstevel@tonic-gate 		bam_error(NEED_CMD);
5227c478bd9Sstevel@tonic-gate 		error = 1;
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	if (error) {
5267c478bd9Sstevel@tonic-gate 		usage();
5277c478bd9Sstevel@tonic-gate 		bam_exit(1);
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	if (optind > argc) {
5317c478bd9Sstevel@tonic-gate 		bam_error(INT_ERROR, "parse_args");
5327c478bd9Sstevel@tonic-gate 		bam_exit(1);
5337c478bd9Sstevel@tonic-gate 	} else if (optind < argc) {
5347c478bd9Sstevel@tonic-gate 		bam_argv = &argv[optind];
5357c478bd9Sstevel@tonic-gate 		bam_argc = argc - optind;
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	/*
5397c478bd9Sstevel@tonic-gate 	 * -n implies verbose mode
5407c478bd9Sstevel@tonic-gate 	 */
5417c478bd9Sstevel@tonic-gate 	if (bam_check)
5427c478bd9Sstevel@tonic-gate 		bam_verbose = 1;
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate static error_t
5467c478bd9Sstevel@tonic-gate check_subcmd_and_options(
5477c478bd9Sstevel@tonic-gate 	char *subcmd,
5487c478bd9Sstevel@tonic-gate 	char *opt,
5497c478bd9Sstevel@tonic-gate 	subcmd_defn_t *table,
5507c478bd9Sstevel@tonic-gate 	error_t (**fp)())
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate 	int i;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (subcmd == NULL) {
5557c478bd9Sstevel@tonic-gate 		bam_error(NEED_SUBCMD);
5567c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 
5591a97e40eSvikram 	if (bam_argc != 0 || bam_argv) {
5601a97e40eSvikram 		if (strcmp(subcmd, "set_option") != 0 || bam_argc != 1) {
5611a97e40eSvikram 			bam_error(TRAILING_ARGS);
5621a97e40eSvikram 			usage();
5631a97e40eSvikram 			return (BAM_ERROR);
5641a97e40eSvikram 		}
5651a97e40eSvikram 	}
5661a97e40eSvikram 
5677c478bd9Sstevel@tonic-gate 	if (bam_root == NULL) {
5687c478bd9Sstevel@tonic-gate 		bam_root = rootbuf;
5697c478bd9Sstevel@tonic-gate 		bam_rootlen = 1;
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	/* verify that subcmd is valid */
5737c478bd9Sstevel@tonic-gate 	for (i = 0; table[i].subcmd != NULL; i++) {
5747c478bd9Sstevel@tonic-gate 		if (strcmp(table[i].subcmd, subcmd) == 0)
5757c478bd9Sstevel@tonic-gate 			break;
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	if (table[i].subcmd == NULL) {
5797c478bd9Sstevel@tonic-gate 		bam_error(INVALID_SUBCMD, subcmd);
5807c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 
5831a97e40eSvikram 	if (table[i].unpriv == 0 && geteuid() != 0) {
5841a97e40eSvikram 		bam_error(MUST_BE_ROOT);
5851a97e40eSvikram 		return (BAM_ERROR);
5861a97e40eSvikram 	}
5871a97e40eSvikram 
5881a97e40eSvikram 	/*
5891a97e40eSvikram 	 * Currently only privileged commands need a lock
5901a97e40eSvikram 	 */
5911a97e40eSvikram 	if (table[i].unpriv == 0)
5921a97e40eSvikram 		bam_lock();
5931a97e40eSvikram 
5947c478bd9Sstevel@tonic-gate 	/* subcmd verifies that opt is appropriate */
5957c478bd9Sstevel@tonic-gate 	if (table[i].option != OPT_OPTIONAL) {
5967c478bd9Sstevel@tonic-gate 		if ((table[i].option == OPT_REQ) ^ (opt != NULL)) {
5977c478bd9Sstevel@tonic-gate 			if (opt)
5987c478bd9Sstevel@tonic-gate 				bam_error(NO_OPT_REQ, subcmd);
5997c478bd9Sstevel@tonic-gate 			else
6007c478bd9Sstevel@tonic-gate 				bam_error(MISS_OPT, subcmd);
6017c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
6027c478bd9Sstevel@tonic-gate 		}
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	*fp = table[i].handler;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
610b610f78eSvikram 
611b610f78eSvikram static char *
61240541d5dSvikram mount_grub_slice(int *mnted, char **physlice, char **logslice, char **fs_type)
613b610f78eSvikram {
614b610f78eSvikram 	struct extmnttab mnt;
615b610f78eSvikram 	struct stat sb;
616b610f78eSvikram 	char buf[BAM_MAXLINE], dev[PATH_MAX], phys[PATH_MAX], fstype[32];
617f0f2c544Svikram 	char cmd[PATH_MAX];
618b610f78eSvikram 	char *mntpt;
619b610f78eSvikram 	int p, l, f;
620b610f78eSvikram 	FILE *fp;
621b610f78eSvikram 
622b610f78eSvikram 	assert(mnted);
623b610f78eSvikram 	*mnted = 0;
624b610f78eSvikram 
625b610f78eSvikram 	/*
62640541d5dSvikram 	 * physlice, logslice, fs_type  args may be NULL
627b610f78eSvikram 	 */
628b610f78eSvikram 	if (physlice)
629b610f78eSvikram 		*physlice = NULL;
63040541d5dSvikram 	if (logslice)
63140541d5dSvikram 		*logslice = NULL;
63240541d5dSvikram 	if (fs_type)
63340541d5dSvikram 		*fs_type = NULL;
634b610f78eSvikram 
635b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
636b610f78eSvikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
637b610f78eSvikram 		return (NULL);
638b610f78eSvikram 	}
639b610f78eSvikram 
640b610f78eSvikram 	fp = fopen(GRUB_slice, "r");
641b610f78eSvikram 	if (fp == NULL) {
642b610f78eSvikram 		bam_error(OPEN_FAIL, GRUB_slice, strerror(errno));
643b610f78eSvikram 		return (NULL);
644b610f78eSvikram 	}
645b610f78eSvikram 
646b610f78eSvikram 	dev[0] = fstype[0] = phys[0] = '\0';
647b610f78eSvikram 	p = sizeof ("PHYS_SLICE=") - 1;
648b610f78eSvikram 	l = sizeof ("LOG_SLICE=") - 1;
649b610f78eSvikram 	f = sizeof ("LOG_FSTYP=") - 1;
650b610f78eSvikram 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
651b610f78eSvikram 		if (strncmp(buf, "PHYS_SLICE=", p) == 0) {
652b610f78eSvikram 			(void) strlcpy(phys, buf + p, sizeof (phys));
653b610f78eSvikram 			continue;
654b610f78eSvikram 		}
655b610f78eSvikram 		if (strncmp(buf, "LOG_SLICE=", l) == 0) {
656b610f78eSvikram 			(void) strlcpy(dev, buf + l, sizeof (dev));
657b610f78eSvikram 			continue;
658b610f78eSvikram 		}
659b610f78eSvikram 		if (strncmp(buf, "LOG_FSTYP=", f) == 0) {
660b610f78eSvikram 			(void) strlcpy(fstype, buf + f, sizeof (fstype));
661b610f78eSvikram 			continue;
662b610f78eSvikram 		}
663b610f78eSvikram 	}
664b610f78eSvikram 	(void) fclose(fp);
665b610f78eSvikram 
666b610f78eSvikram 	if (dev[0] == '\0' || fstype[0] == '\0' || phys[0] == '\0') {
667b610f78eSvikram 		bam_error(BAD_SLICE_FILE, GRUB_slice);
668b610f78eSvikram 		return (NULL);
669b610f78eSvikram 	}
670b610f78eSvikram 
671b610f78eSvikram 	if (physlice) {
672b610f78eSvikram 		*physlice = s_strdup(phys);
673b610f78eSvikram 	}
67440541d5dSvikram 	if (logslice) {
67540541d5dSvikram 		*logslice = s_strdup(dev);
67640541d5dSvikram 	}
67740541d5dSvikram 	if (fs_type) {
67840541d5dSvikram 		*fs_type = s_strdup(fstype);
67940541d5dSvikram 	}
680b610f78eSvikram 
681b610f78eSvikram 	/*
682b610f78eSvikram 	 * Check if the slice is already mounted
683b610f78eSvikram 	 */
684b610f78eSvikram 	fp = fopen(MNTTAB, "r");
685b610f78eSvikram 	if (fp == NULL) {
686b610f78eSvikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
68740541d5dSvikram 		goto error;
688b610f78eSvikram 	}
689b610f78eSvikram 
690b610f78eSvikram 	resetmnttab(fp);
691b610f78eSvikram 
692b610f78eSvikram 	mntpt = NULL;
693b610f78eSvikram 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
694b610f78eSvikram 		if (strcmp(mnt.mnt_special, dev) == 0) {
695b610f78eSvikram 			mntpt = s_strdup(mnt.mnt_mountp);
696b610f78eSvikram 			break;
697b610f78eSvikram 		}
698b610f78eSvikram 	}
699b610f78eSvikram 
700b610f78eSvikram 	(void) fclose(fp);
701b610f78eSvikram 
702b610f78eSvikram 	if (mntpt) {
703b610f78eSvikram 		return (mntpt);
704b610f78eSvikram 	}
705b610f78eSvikram 
706b610f78eSvikram 
707b610f78eSvikram 	/*
708b610f78eSvikram 	 * GRUB slice is not mounted, we need to mount it now.
709b610f78eSvikram 	 * First create the mountpoint
710b610f78eSvikram 	 */
711b610f78eSvikram 	mntpt = s_calloc(1, PATH_MAX);
712b610f78eSvikram 	(void) snprintf(mntpt, PATH_MAX, "%s.%d", GRUB_slice_mntpt, getpid());
713b610f78eSvikram 	if (mkdir(mntpt, 0755) == -1 && errno != EEXIST) {
714b610f78eSvikram 		bam_error(MKDIR_FAILED, mntpt, strerror(errno));
715b610f78eSvikram 		free(mntpt);
71640541d5dSvikram 		goto error;
717b610f78eSvikram 	}
718b610f78eSvikram 
719f0f2c544Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/mount -F %s %s %s",
720f0f2c544Svikram 	    fstype, dev, mntpt);
721f0f2c544Svikram 
722f0f2c544Svikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
72340541d5dSvikram 		bam_error(MOUNT_FAILED, dev, fstype);
724b610f78eSvikram 		if (rmdir(mntpt) != 0) {
725b610f78eSvikram 			bam_error(RMDIR_FAILED, mntpt, strerror(errno));
726b610f78eSvikram 		}
727b610f78eSvikram 		free(mntpt);
72840541d5dSvikram 		goto error;
729b610f78eSvikram 	}
730b610f78eSvikram 
731b610f78eSvikram 	*mnted = 1;
732b610f78eSvikram 	return (mntpt);
73340541d5dSvikram 
73440541d5dSvikram error:
73540541d5dSvikram 	if (physlice) {
73640541d5dSvikram 		free(*physlice);
73740541d5dSvikram 		*physlice = NULL;
73840541d5dSvikram 	}
73940541d5dSvikram 	if (logslice) {
74040541d5dSvikram 		free(*logslice);
74140541d5dSvikram 		*logslice = NULL;
74240541d5dSvikram 	}
74340541d5dSvikram 	if (fs_type) {
74440541d5dSvikram 		free(*fs_type);
74540541d5dSvikram 		*fs_type = NULL;
74640541d5dSvikram 	}
74740541d5dSvikram 	return (NULL);
748b610f78eSvikram }
749b610f78eSvikram 
750b610f78eSvikram static void
75140541d5dSvikram umount_grub_slice(
75240541d5dSvikram 	int mnted,
75340541d5dSvikram 	char *mntpt,
75440541d5dSvikram 	char *physlice,
75540541d5dSvikram 	char *logslice,
75640541d5dSvikram 	char *fs_type)
757b610f78eSvikram {
758f0f2c544Svikram 	char cmd[PATH_MAX];
759f0f2c544Svikram 
760b610f78eSvikram 	/*
761b610f78eSvikram 	 * If we have not dealt with GRUB slice
762b610f78eSvikram 	 * we have nothing to do - just return.
763b610f78eSvikram 	 */
764b610f78eSvikram 	if (mntpt == NULL)
765b610f78eSvikram 		return;
766b610f78eSvikram 
767b610f78eSvikram 
768b610f78eSvikram 	/*
769b610f78eSvikram 	 * If we mounted the filesystem earlier in mount_grub_slice()
770b610f78eSvikram 	 * unmount it now.
771b610f78eSvikram 	 */
772b610f78eSvikram 	if (mnted) {
773f0f2c544Svikram 		(void) snprintf(cmd, sizeof (cmd), "/sbin/umount %s",
774f0f2c544Svikram 		    mntpt);
775f0f2c544Svikram 		if (exec_cmd(cmd, NULL, 0) != 0) {
776f0f2c544Svikram 			bam_error(UMOUNT_FAILED, mntpt);
777b610f78eSvikram 		}
778b610f78eSvikram 		if (rmdir(mntpt) != 0) {
779b610f78eSvikram 			bam_error(RMDIR_FAILED, mntpt, strerror(errno));
780b610f78eSvikram 		}
781b610f78eSvikram 	}
78240541d5dSvikram 
783b610f78eSvikram 	if (physlice)
784b610f78eSvikram 		free(physlice);
78540541d5dSvikram 	if (logslice)
78640541d5dSvikram 		free(logslice);
78740541d5dSvikram 	if (fs_type)
78840541d5dSvikram 		free(fs_type);
78940541d5dSvikram 
790b610f78eSvikram 	free(mntpt);
791b610f78eSvikram }
792b610f78eSvikram 
79340541d5dSvikram static char *
79440541d5dSvikram use_stubboot(void)
79540541d5dSvikram {
79640541d5dSvikram 	int mnted;
79740541d5dSvikram 	struct stat sb;
79840541d5dSvikram 	struct extmnttab mnt;
79940541d5dSvikram 	FILE *fp;
80040541d5dSvikram 	char cmd[PATH_MAX];
80140541d5dSvikram 
80240541d5dSvikram 	if (stat(STUBBOOT, &sb) != 0) {
80340541d5dSvikram 		bam_error(STUBBOOT_DIR_NOT_FOUND);
80440541d5dSvikram 		return (NULL);
80540541d5dSvikram 	}
80640541d5dSvikram 
80740541d5dSvikram 	/*
80840541d5dSvikram 	 * Check if stubboot is mounted. If not, mount it
80940541d5dSvikram 	 */
81040541d5dSvikram 	fp = fopen(MNTTAB, "r");
81140541d5dSvikram 	if (fp == NULL) {
81240541d5dSvikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
81340541d5dSvikram 		return (NULL);
81440541d5dSvikram 	}
81540541d5dSvikram 
81640541d5dSvikram 	resetmnttab(fp);
81740541d5dSvikram 
81840541d5dSvikram 	mnted = 0;
81940541d5dSvikram 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
82040541d5dSvikram 		if (strcmp(mnt.mnt_mountp, STUBBOOT) == 0) {
82140541d5dSvikram 			mnted = 1;
82240541d5dSvikram 			break;
82340541d5dSvikram 		}
82440541d5dSvikram 	}
82540541d5dSvikram 
82640541d5dSvikram 	(void) fclose(fp);
82740541d5dSvikram 
82840541d5dSvikram 	if (mnted)
82940541d5dSvikram 		return (STUBBOOT);
83040541d5dSvikram 
83140541d5dSvikram 	/*
83240541d5dSvikram 	 * Stubboot is not mounted, mount it now.
83340541d5dSvikram 	 * It should exist in /etc/vfstab
83440541d5dSvikram 	 */
83540541d5dSvikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/mount %s",
83640541d5dSvikram 	    STUBBOOT);
83740541d5dSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
83840541d5dSvikram 		bam_error(MOUNT_MNTPT_FAILED, STUBBOOT);
83940541d5dSvikram 		return (NULL);
84040541d5dSvikram 	}
84140541d5dSvikram 
84240541d5dSvikram 	return (STUBBOOT);
84340541d5dSvikram }
84440541d5dSvikram 
84540541d5dSvikram static void
84640541d5dSvikram disp_active_menu_locn(char *menu_path, char *logslice, char *fstype, int mnted)
84740541d5dSvikram {
84840541d5dSvikram 	/*
84940541d5dSvikram 	 * Check if we did a temp mount of an unmounted device.
85040541d5dSvikram 	 * If yes, print the block device and fstype for that device
85140541d5dSvikram 	 * else it is already mounted, so we print the path to the GRUB menu.
85240541d5dSvikram 	 */
85340541d5dSvikram 	if (mnted) {
85440541d5dSvikram 		bam_print(GRUB_MENU_DEVICE, logslice);
85540541d5dSvikram 		bam_print(GRUB_MENU_FSTYPE, fstype);
85640541d5dSvikram 	} else {
85740541d5dSvikram 		bam_print(GRUB_MENU_PATH, menu_path);
85840541d5dSvikram 	}
85940541d5dSvikram }
86040541d5dSvikram 
86140541d5dSvikram /*
86240541d5dSvikram  * NOTE: A single "/" is also considered a trailing slash and will
86340541d5dSvikram  * be deleted.
86440541d5dSvikram  */
86540541d5dSvikram static void
86640541d5dSvikram elide_trailing_slash(const char *src, char *dst, size_t dstsize)
86740541d5dSvikram {
86840541d5dSvikram 	size_t dstlen;
86940541d5dSvikram 
87040541d5dSvikram 	assert(src);
87140541d5dSvikram 	assert(dst);
87240541d5dSvikram 
87340541d5dSvikram 	(void) strlcpy(dst, src, dstsize);
87440541d5dSvikram 
87540541d5dSvikram 	dstlen = strlen(dst);
87640541d5dSvikram 	if (dst[dstlen - 1] == '/') {
87740541d5dSvikram 		dst[dstlen - 1] = '\0';
87840541d5dSvikram 	}
87940541d5dSvikram }
88040541d5dSvikram 
8817c478bd9Sstevel@tonic-gate static error_t
8827c478bd9Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[])
8837c478bd9Sstevel@tonic-gate {
8847c478bd9Sstevel@tonic-gate 	error_t ret;
8857c478bd9Sstevel@tonic-gate 	char menu_path[PATH_MAX];
886*ae115bc7Smrj 	char path[PATH_MAX];
8877c478bd9Sstevel@tonic-gate 	menu_t *menu;
88840541d5dSvikram 	char *mntpt, *menu_root, *logslice, *fstype;
889b610f78eSvikram 	struct stat sb;
890b610f78eSvikram 	int mnted;	/* set if we did a mount */
8917c478bd9Sstevel@tonic-gate 	error_t (*f)(menu_t *mp, char *menu_path, char *opt);
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	/*
8947c478bd9Sstevel@tonic-gate 	 * Check arguments
8957c478bd9Sstevel@tonic-gate 	 */
8967c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
8977c478bd9Sstevel@tonic-gate 	if (ret == BAM_ERROR) {
8987c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
8997c478bd9Sstevel@tonic-gate 	}
9007c478bd9Sstevel@tonic-gate 
901b610f78eSvikram 	mntpt = NULL;
902b610f78eSvikram 	mnted = 0;
90340541d5dSvikram 	logslice = fstype = NULL;
90440541d5dSvikram 
90540541d5dSvikram 	/*
906*ae115bc7Smrj 	 * Check for the menu.list file:
907*ae115bc7Smrj 	 *
908*ae115bc7Smrj 	 * 1. Check for a GRUB_slice file, be it on / or
909*ae115bc7Smrj 	 *    on the user-provided alternate root.
910*ae115bc7Smrj 	 * 2. Use the alternate root, if given.
911*ae115bc7Smrj 	 * 3. Check /stubboot
912*ae115bc7Smrj 	 * 4. Use /
91340541d5dSvikram 	 */
91440541d5dSvikram 	if (bam_alt_root) {
915*ae115bc7Smrj 		(void) snprintf(path, sizeof (path), "%s%s", bam_root,
916*ae115bc7Smrj 		    GRUB_slice);
917*ae115bc7Smrj 	} else {
918*ae115bc7Smrj 		(void) snprintf(path, sizeof (path), "%s", GRUB_slice);
919*ae115bc7Smrj 	}
920*ae115bc7Smrj 
921*ae115bc7Smrj 	if (stat(path, &sb) == 0) {
92240541d5dSvikram 		mntpt = mount_grub_slice(&mnted, NULL, &logslice, &fstype);
923b610f78eSvikram 		menu_root = mntpt;
924*ae115bc7Smrj 	} else if (bam_alt_root) {
925*ae115bc7Smrj 		menu_root = bam_root;
92640541d5dSvikram 	} else if (stat(STUBBOOT, &sb) == 0) {
92740541d5dSvikram 		menu_root = use_stubboot();
928b610f78eSvikram 	} else {
929b610f78eSvikram 		menu_root = bam_root;
930b610f78eSvikram 	}
931b610f78eSvikram 
93240541d5dSvikram 	if (menu_root == NULL) {
93340541d5dSvikram 		bam_error(CANNOT_LOCATE_GRUB_MENU);
93440541d5dSvikram 		return (BAM_ERROR);
93540541d5dSvikram 	}
93640541d5dSvikram 
93740541d5dSvikram 	elide_trailing_slash(menu_root, menu_path, sizeof (menu_path));
93840541d5dSvikram 	(void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path));
93940541d5dSvikram 
94040541d5dSvikram 	/*
94140541d5dSvikram 	 * If listing the menu, display the active menu
94240541d5dSvikram 	 * location
94340541d5dSvikram 	 */
94440541d5dSvikram 	if (strcmp(subcmd, "list_entry") == 0) {
94540541d5dSvikram 		disp_active_menu_locn(menu_path, logslice, fstype, mnted);
94640541d5dSvikram 	}
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	menu = menu_read(menu_path);
9497c478bd9Sstevel@tonic-gate 	assert(menu);
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	/*
9527c478bd9Sstevel@tonic-gate 	 * Special handling for setting timeout and default
9537c478bd9Sstevel@tonic-gate 	 */
9547c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "set_option") == 0) {
9557c478bd9Sstevel@tonic-gate 		if (largc != 1 || largv[0] == NULL) {
9567c478bd9Sstevel@tonic-gate 			usage();
95740541d5dSvikram 			menu_free(menu);
95840541d5dSvikram 			umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
9597c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
9607c478bd9Sstevel@tonic-gate 		}
9617c478bd9Sstevel@tonic-gate 		opt = largv[0];
9627c478bd9Sstevel@tonic-gate 	} else if (largc != 0) {
9637c478bd9Sstevel@tonic-gate 		usage();
96440541d5dSvikram 		menu_free(menu);
96540541d5dSvikram 		umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
9667c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
9677c478bd9Sstevel@tonic-gate 	}
9687c478bd9Sstevel@tonic-gate 
969*ae115bc7Smrj 	ret = dboot_or_multiboot(bam_root);
970*ae115bc7Smrj 	if (ret != BAM_SUCCESS)
971*ae115bc7Smrj 		return (ret);
972*ae115bc7Smrj 
9737c478bd9Sstevel@tonic-gate 	/*
9747c478bd9Sstevel@tonic-gate 	 * Once the sub-cmd handler has run
9757c478bd9Sstevel@tonic-gate 	 * only the line field is guaranteed to have valid values
9767c478bd9Sstevel@tonic-gate 	 */
977*ae115bc7Smrj 	if ((strcmp(subcmd, "update_entry") == 0) ||
978*ae115bc7Smrj 	    (strcmp(subcmd, "upgrade") == 0))
9797c478bd9Sstevel@tonic-gate 		ret = f(menu, bam_root, opt);
9807c478bd9Sstevel@tonic-gate 	else
9817c478bd9Sstevel@tonic-gate 		ret = f(menu, menu_path, opt);
9827c478bd9Sstevel@tonic-gate 	if (ret == BAM_WRITE) {
983b610f78eSvikram 		ret = menu_write(menu_root, menu);
9847c478bd9Sstevel@tonic-gate 	}
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	menu_free(menu);
9877c478bd9Sstevel@tonic-gate 
98840541d5dSvikram 	umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
989b610f78eSvikram 
9907c478bd9Sstevel@tonic-gate 	return (ret);
9917c478bd9Sstevel@tonic-gate }
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate static error_t
9957c478bd9Sstevel@tonic-gate bam_archive(
9967c478bd9Sstevel@tonic-gate 	char *subcmd,
9977c478bd9Sstevel@tonic-gate 	char *opt)
9987c478bd9Sstevel@tonic-gate {
9997c478bd9Sstevel@tonic-gate 	error_t ret;
10007c478bd9Sstevel@tonic-gate 	error_t (*f)(char *root, char *opt);
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	/*
10038c1b6884Sszhou 	 * Add trailing / for archive subcommands
10048c1b6884Sszhou 	 */
10058c1b6884Sszhou 	if (rootbuf[strlen(rootbuf) - 1] != '/')
10068c1b6884Sszhou 		(void) strcat(rootbuf, "/");
10078c1b6884Sszhou 	bam_rootlen = strlen(rootbuf);
10088c1b6884Sszhou 
10098c1b6884Sszhou 	/*
10107c478bd9Sstevel@tonic-gate 	 * Check arguments
10117c478bd9Sstevel@tonic-gate 	 */
10127c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f);
10137c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS) {
10147c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate #if defined(__sparc)
10187c478bd9Sstevel@tonic-gate 	/*
10197c478bd9Sstevel@tonic-gate 	 * A NOP if called on SPARC during reboot
10207c478bd9Sstevel@tonic-gate 	 */
10217c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "update_all") == 0)
10227c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
10237c478bd9Sstevel@tonic-gate 	else if (strcmp(subcmd, "update") != 0)
10247c478bd9Sstevel@tonic-gate 		sparc_abort();
10257c478bd9Sstevel@tonic-gate #endif
10267c478bd9Sstevel@tonic-gate 
1027*ae115bc7Smrj 	ret = dboot_or_multiboot(rootbuf);
1028*ae115bc7Smrj 	if (ret != BAM_SUCCESS)
1029*ae115bc7Smrj 		return (ret);
1030*ae115bc7Smrj 
10317c478bd9Sstevel@tonic-gate 	/*
10327c478bd9Sstevel@tonic-gate 	 * Check archive not supported with update_all
10337c478bd9Sstevel@tonic-gate 	 * since it is awkward to display out-of-sync
10347c478bd9Sstevel@tonic-gate 	 * information for each BE.
10357c478bd9Sstevel@tonic-gate 	 */
10367c478bd9Sstevel@tonic-gate 	if (bam_check && strcmp(subcmd, "update_all") == 0) {
10377c478bd9Sstevel@tonic-gate 		bam_error(CHECK_NOT_SUPPORTED, subcmd);
10387c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
10397c478bd9Sstevel@tonic-gate 	}
10407c478bd9Sstevel@tonic-gate 
1041b610f78eSvikram 	if (strcmp(subcmd, "update_all") == 0)
1042b610f78eSvikram 		bam_update_all = 1;
1043b610f78eSvikram 
1044b610f78eSvikram 	ret = f(bam_root, opt);
1045b610f78eSvikram 
1046b610f78eSvikram 	bam_update_all = 0;
1047b610f78eSvikram 
1048b610f78eSvikram 	return (ret);
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1052*ae115bc7Smrj void
10537c478bd9Sstevel@tonic-gate bam_error(char *format, ...)
10547c478bd9Sstevel@tonic-gate {
10557c478bd9Sstevel@tonic-gate 	va_list ap;
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	va_start(ap, format);
10587c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", prog);
10597c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
10607c478bd9Sstevel@tonic-gate 	va_end(ap);
10617c478bd9Sstevel@tonic-gate }
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
10647c478bd9Sstevel@tonic-gate static void
10657c478bd9Sstevel@tonic-gate bam_print(char *format, ...)
10667c478bd9Sstevel@tonic-gate {
10677c478bd9Sstevel@tonic-gate 	va_list ap;
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	va_start(ap, format);
10707c478bd9Sstevel@tonic-gate 	(void) vfprintf(stdout, format, ap);
10717c478bd9Sstevel@tonic-gate 	va_end(ap);
10727c478bd9Sstevel@tonic-gate }
10737c478bd9Sstevel@tonic-gate 
1074*ae115bc7Smrj /*PRINTFLIKE1*/
1075*ae115bc7Smrj void
1076*ae115bc7Smrj bam_print_stderr(char *format, ...)
1077*ae115bc7Smrj {
1078*ae115bc7Smrj 	va_list ap;
1079*ae115bc7Smrj 
1080*ae115bc7Smrj 	va_start(ap, format);
1081*ae115bc7Smrj 	(void) vfprintf(stderr, format, ap);
1082*ae115bc7Smrj 	va_end(ap);
1083*ae115bc7Smrj }
1084*ae115bc7Smrj 
10857c478bd9Sstevel@tonic-gate static void
10867c478bd9Sstevel@tonic-gate bam_exit(int excode)
10877c478bd9Sstevel@tonic-gate {
10887c478bd9Sstevel@tonic-gate 	bam_unlock();
10897c478bd9Sstevel@tonic-gate 	exit(excode);
10907c478bd9Sstevel@tonic-gate }
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate static void
10937c478bd9Sstevel@tonic-gate bam_lock(void)
10947c478bd9Sstevel@tonic-gate {
10957c478bd9Sstevel@tonic-gate 	struct flock lock;
10967c478bd9Sstevel@tonic-gate 	pid_t pid;
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS);
10997c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
11007c478bd9Sstevel@tonic-gate 		/*
11017c478bd9Sstevel@tonic-gate 		 * We may be invoked early in boot for archive verification.
11027c478bd9Sstevel@tonic-gate 		 * In this case, root is readonly and /var/run may not exist.
11037c478bd9Sstevel@tonic-gate 		 * Proceed without the lock
11047c478bd9Sstevel@tonic-gate 		 */
11057c478bd9Sstevel@tonic-gate 		if (errno == EROFS || errno == ENOENT) {
11067c478bd9Sstevel@tonic-gate 			bam_root_readonly = 1;
11077c478bd9Sstevel@tonic-gate 			return;
11087c478bd9Sstevel@tonic-gate 		}
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno));
11117c478bd9Sstevel@tonic-gate 		bam_exit(1);
11127c478bd9Sstevel@tonic-gate 	}
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
11157c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
11167c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
11177c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) {
11207c478bd9Sstevel@tonic-gate 		if (errno != EACCES && errno != EAGAIN) {
11217c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11227c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
11237c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
11247c478bd9Sstevel@tonic-gate 			bam_exit(1);
11257c478bd9Sstevel@tonic-gate 		}
11267c478bd9Sstevel@tonic-gate 		pid = 0;
11277c478bd9Sstevel@tonic-gate 		(void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0);
11287c478bd9Sstevel@tonic-gate 		bam_print(FILE_LOCKED, pid);
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 		lock.l_type = F_WRLCK;
11317c478bd9Sstevel@tonic-gate 		lock.l_whence = SEEK_SET;
11327c478bd9Sstevel@tonic-gate 		lock.l_start = 0;
11337c478bd9Sstevel@tonic-gate 		lock.l_len = 0;
11347c478bd9Sstevel@tonic-gate 		if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) {
11357c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11367c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
11377c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
11387c478bd9Sstevel@tonic-gate 			bam_exit(1);
11397c478bd9Sstevel@tonic-gate 		}
11407c478bd9Sstevel@tonic-gate 	}
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	/* We own the lock now */
11437c478bd9Sstevel@tonic-gate 	pid = getpid();
11447c478bd9Sstevel@tonic-gate 	(void) write(bam_lock_fd, &pid, sizeof (pid));
11457c478bd9Sstevel@tonic-gate }
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate static void
11487c478bd9Sstevel@tonic-gate bam_unlock(void)
11497c478bd9Sstevel@tonic-gate {
11507c478bd9Sstevel@tonic-gate 	struct flock unlock;
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	/*
11537c478bd9Sstevel@tonic-gate 	 * NOP if we don't hold the lock
11547c478bd9Sstevel@tonic-gate 	 */
11557c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
11567c478bd9Sstevel@tonic-gate 		return;
11577c478bd9Sstevel@tonic-gate 	}
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
11607c478bd9Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
11617c478bd9Sstevel@tonic-gate 	unlock.l_start = 0;
11627c478bd9Sstevel@tonic-gate 	unlock.l_len = 0;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) {
11657c478bd9Sstevel@tonic-gate 		bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11667c478bd9Sstevel@tonic-gate 	}
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	if (close(bam_lock_fd) == -1) {
11697c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno));
11707c478bd9Sstevel@tonic-gate 	}
11717c478bd9Sstevel@tonic-gate 	bam_lock_fd = -1;
11727c478bd9Sstevel@tonic-gate }
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate static error_t
11757c478bd9Sstevel@tonic-gate list_archive(char *root, char *opt)
11767c478bd9Sstevel@tonic-gate {
11777c478bd9Sstevel@tonic-gate 	filelist_t flist;
11787c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
11797c478bd9Sstevel@tonic-gate 	line_t *lp;
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	assert(root);
11827c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
11857c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
11867c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
11877c478bd9Sstevel@tonic-gate 	}
11887c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
11917c478bd9Sstevel@tonic-gate 		bam_print(PRINT, lp->line);
11927c478bd9Sstevel@tonic-gate 	}
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
11977c478bd9Sstevel@tonic-gate }
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate /*
12007c478bd9Sstevel@tonic-gate  * This routine writes a list of lines to a file.
12017c478bd9Sstevel@tonic-gate  * The list is *not* freed
12027c478bd9Sstevel@tonic-gate  */
12037c478bd9Sstevel@tonic-gate static error_t
12047c478bd9Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start)
12057c478bd9Sstevel@tonic-gate {
12067c478bd9Sstevel@tonic-gate 	char tmpfile[PATH_MAX];
12077c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
12087c478bd9Sstevel@tonic-gate 	FILE *fp;
12097c478bd9Sstevel@tonic-gate 	int ret;
12107c478bd9Sstevel@tonic-gate 	struct stat sb;
12117c478bd9Sstevel@tonic-gate 	mode_t mode;
12127c478bd9Sstevel@tonic-gate 	uid_t root_uid;
12137c478bd9Sstevel@tonic-gate 	gid_t sys_gid;
12147c478bd9Sstevel@tonic-gate 	struct passwd *pw;
12157c478bd9Sstevel@tonic-gate 	struct group *gp;
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, final);
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	if (start == NULL) {
12217c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != -1) {
12227c478bd9Sstevel@tonic-gate 			bam_print(UNLINK_EMPTY, path);
12237c478bd9Sstevel@tonic-gate 			if (unlink(path) != 0) {
12247c478bd9Sstevel@tonic-gate 				bam_error(UNLINK_FAIL, path, strerror(errno));
12257c478bd9Sstevel@tonic-gate 				return (BAM_ERROR);
12267c478bd9Sstevel@tonic-gate 			} else {
12277c478bd9Sstevel@tonic-gate 				return (BAM_SUCCESS);
12287c478bd9Sstevel@tonic-gate 			}
12297c478bd9Sstevel@tonic-gate 		}
12307c478bd9Sstevel@tonic-gate 	}
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	/*
12337c478bd9Sstevel@tonic-gate 	 * Preserve attributes of existing file if possible,
12347c478bd9Sstevel@tonic-gate 	 * otherwise ask the system for uid/gid of root/sys.
12357c478bd9Sstevel@tonic-gate 	 * If all fails, fall back on hard-coded defaults.
12367c478bd9Sstevel@tonic-gate 	 */
12377c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != -1) {
12387c478bd9Sstevel@tonic-gate 		mode = sb.st_mode;
12397c478bd9Sstevel@tonic-gate 		root_uid = sb.st_uid;
12407c478bd9Sstevel@tonic-gate 		sys_gid = sb.st_gid;
12417c478bd9Sstevel@tonic-gate 	} else {
12427c478bd9Sstevel@tonic-gate 		mode = DEFAULT_DEV_MODE;
12437c478bd9Sstevel@tonic-gate 		if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
12447c478bd9Sstevel@tonic-gate 			root_uid = pw->pw_uid;
12457c478bd9Sstevel@tonic-gate 		} else {
12467c478bd9Sstevel@tonic-gate 			if (bam_verbose)
12477c478bd9Sstevel@tonic-gate 				bam_error(CANT_FIND_USER,
12487c478bd9Sstevel@tonic-gate 				    DEFAULT_DEV_USER, DEFAULT_DEV_UID);
12497c478bd9Sstevel@tonic-gate 			root_uid = (uid_t)DEFAULT_DEV_UID;
12507c478bd9Sstevel@tonic-gate 		}
12517c478bd9Sstevel@tonic-gate 		if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
12527c478bd9Sstevel@tonic-gate 			sys_gid = gp->gr_gid;
12537c478bd9Sstevel@tonic-gate 		} else {
12547c478bd9Sstevel@tonic-gate 			if (bam_verbose)
12557c478bd9Sstevel@tonic-gate 				bam_error(CANT_FIND_GROUP,
12567c478bd9Sstevel@tonic-gate 				    DEFAULT_DEV_GROUP, DEFAULT_DEV_GID);
12577c478bd9Sstevel@tonic-gate 			sys_gid = (gid_t)DEFAULT_DEV_GID;
12587c478bd9Sstevel@tonic-gate 		}
12597c478bd9Sstevel@tonic-gate 	}
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	(void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp);
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	/* Truncate tmpfile first */
12647c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "w");
12657c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
12667c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
12677c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12687c478bd9Sstevel@tonic-gate 	}
12697c478bd9Sstevel@tonic-gate 	ret = fclose(fp);
12707c478bd9Sstevel@tonic-gate 	if (ret == EOF) {
12717c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
12727c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12737c478bd9Sstevel@tonic-gate 	}
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	/* Now open it in append mode */
12767c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "a");
12777c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
12787c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
12797c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12807c478bd9Sstevel@tonic-gate 	}
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	for (; start; start = start->next) {
12837c478bd9Sstevel@tonic-gate 		ret = s_fputs(start->line, fp);
12847c478bd9Sstevel@tonic-gate 		if (ret == EOF) {
12857c478bd9Sstevel@tonic-gate 			bam_error(WRITE_FAIL, tmpfile, strerror(errno));
12867c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
12877c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
12887c478bd9Sstevel@tonic-gate 		}
12897c478bd9Sstevel@tonic-gate 	}
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	ret = fclose(fp);
12927c478bd9Sstevel@tonic-gate 	if (ret == EOF) {
12937c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
12947c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12957c478bd9Sstevel@tonic-gate 	}
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	/*
1298de531be4Sjg 	 * Set up desired attributes.  Ignore failures on filesystems
1299de531be4Sjg 	 * not supporting these operations - pcfs reports unsupported
1300de531be4Sjg 	 * operations as EINVAL.
13017c478bd9Sstevel@tonic-gate 	 */
13027c478bd9Sstevel@tonic-gate 	ret = chmod(tmpfile, mode);
1303de531be4Sjg 	if (ret == -1 &&
1304de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
13057c478bd9Sstevel@tonic-gate 		bam_error(CHMOD_FAIL, tmpfile, strerror(errno));
13067c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13077c478bd9Sstevel@tonic-gate 	}
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 	ret = chown(tmpfile, root_uid, sys_gid);
1310de531be4Sjg 	if (ret == -1 &&
1311de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
13127c478bd9Sstevel@tonic-gate 		bam_error(CHOWN_FAIL, tmpfile, strerror(errno));
13137c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13147c478bd9Sstevel@tonic-gate 	}
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	/*
13187c478bd9Sstevel@tonic-gate 	 * Do an atomic rename
13197c478bd9Sstevel@tonic-gate 	 */
13207c478bd9Sstevel@tonic-gate 	ret = rename(tmpfile, path);
13217c478bd9Sstevel@tonic-gate 	if (ret != 0) {
13227c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path, strerror(errno));
13237c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13247c478bd9Sstevel@tonic-gate 	}
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
13277c478bd9Sstevel@tonic-gate }
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate /*
13307c478bd9Sstevel@tonic-gate  * This function should always return 0 - since we want
13317c478bd9Sstevel@tonic-gate  * to create stat data for *all* files in the list.
13327c478bd9Sstevel@tonic-gate  */
13337c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13347c478bd9Sstevel@tonic-gate static int
13357c478bd9Sstevel@tonic-gate cmpstat(
13367c478bd9Sstevel@tonic-gate 	const char *file,
13377c478bd9Sstevel@tonic-gate 	const struct stat *stat,
13387c478bd9Sstevel@tonic-gate 	int flags,
13397c478bd9Sstevel@tonic-gate 	struct FTW *ftw)
13407c478bd9Sstevel@tonic-gate {
13417c478bd9Sstevel@tonic-gate 	uint_t sz;
13427c478bd9Sstevel@tonic-gate 	uint64_t *value;
13437c478bd9Sstevel@tonic-gate 	uint64_t filestat[2];
13447c478bd9Sstevel@tonic-gate 	int error;
13457c478bd9Sstevel@tonic-gate 
134658091fd8Ssetje 	struct safefile *safefilep;
134758091fd8Ssetje 	FILE *fp;
134858091fd8Ssetje 
13497c478bd9Sstevel@tonic-gate 	/*
13507c478bd9Sstevel@tonic-gate 	 * We only want regular files
13517c478bd9Sstevel@tonic-gate 	 */
13527c478bd9Sstevel@tonic-gate 	if (!S_ISREG(stat->st_mode))
13537c478bd9Sstevel@tonic-gate 		return (0);
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	/*
13567c478bd9Sstevel@tonic-gate 	 * new_nvlp may be NULL if there were errors earlier
13577c478bd9Sstevel@tonic-gate 	 * but this is not fatal to update determination.
13587c478bd9Sstevel@tonic-gate 	 */
13597c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp) {
13607c478bd9Sstevel@tonic-gate 		filestat[0] = stat->st_size;
13617c478bd9Sstevel@tonic-gate 		filestat[1] = stat->st_mtime;
13627c478bd9Sstevel@tonic-gate 		error = nvlist_add_uint64_array(walk_arg.new_nvlp,
13637c478bd9Sstevel@tonic-gate 		    file + bam_rootlen, filestat, 2);
13647c478bd9Sstevel@tonic-gate 		if (error)
13657c478bd9Sstevel@tonic-gate 			bam_error(NVADD_FAIL, file, strerror(error));
13667c478bd9Sstevel@tonic-gate 	}
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	/*
13697c478bd9Sstevel@tonic-gate 	 * The remaining steps are only required if we haven't made a
13707c478bd9Sstevel@tonic-gate 	 * decision about update or if we are checking (-n)
13717c478bd9Sstevel@tonic-gate 	 */
13727c478bd9Sstevel@tonic-gate 	if (walk_arg.need_update && !bam_check)
13737c478bd9Sstevel@tonic-gate 		return (0);
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	/*
137658091fd8Ssetje 	 * If we are invoked as part of system/filesyste/boot-archive, then
137758091fd8Ssetje 	 * there are a number of things we should not worry about
13787c478bd9Sstevel@tonic-gate 	 */
137958091fd8Ssetje 	if (bam_smf_check) {
138058091fd8Ssetje 		/* ignore amd64 modules unless we are booted amd64. */
138158091fd8Ssetje 		if (!is_amd64() && strstr(file, "/amd64/") != 0)
13827c478bd9Sstevel@tonic-gate 			return (0);
13837c478bd9Sstevel@tonic-gate 
138458091fd8Ssetje 		/* read in list of safe files */
138558091fd8Ssetje 		if (safefiles == NULL)
138658091fd8Ssetje 			if (fp = fopen("/boot/solaris/filelist.safe", "r")) {
138758091fd8Ssetje 				safefiles = s_calloc(1,
138858091fd8Ssetje 				    sizeof (struct safefile));
138958091fd8Ssetje 				safefilep = safefiles;
139058091fd8Ssetje 				safefilep->name = s_calloc(1, MAXPATHLEN +
139158091fd8Ssetje 				    MAXNAMELEN);
139258091fd8Ssetje 				safefilep->next = NULL;
139358091fd8Ssetje 				while (s_fgets(safefilep->name, MAXPATHLEN +
139458091fd8Ssetje 				    MAXNAMELEN, fp) != NULL) {
139558091fd8Ssetje 					safefilep->next = s_calloc(1,
139658091fd8Ssetje 					    sizeof (struct safefile));
139758091fd8Ssetje 					safefilep = safefilep->next;
139858091fd8Ssetje 					safefilep->name = s_calloc(1,
139958091fd8Ssetje 					    MAXPATHLEN + MAXNAMELEN);
140058091fd8Ssetje 					safefilep->next = NULL;
140158091fd8Ssetje 				}
140258091fd8Ssetje 				(void) fclose(fp);
140358091fd8Ssetje 			}
140458091fd8Ssetje 
140558091fd8Ssetje 		safefilep = safefiles;
140658091fd8Ssetje 		while (safefilep->next != NULL)
140758091fd8Ssetje 			if (strcmp(file, safefilep->name) != 0) {
140858091fd8Ssetje 				fp = fopen(NEED_UPDATE_FILE, "w");
140958091fd8Ssetje 				if (fclose(fp) != 0)
141058091fd8Ssetje 					bam_error(CLOSE_FAIL, NEED_UPDATE_FILE,
141158091fd8Ssetje 						strerror(errno));
141258091fd8Ssetje 				return (0);
141358091fd8Ssetje 			}
141458091fd8Ssetje 	}
141558091fd8Ssetje 
14167c478bd9Sstevel@tonic-gate 	/*
14177c478bd9Sstevel@tonic-gate 	 * We need an update if file doesn't exist in old archive
14187c478bd9Sstevel@tonic-gate 	 */
14197c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp == NULL ||
14207c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint64_array(walk_arg.old_nvlp,
14217c478bd9Sstevel@tonic-gate 	    file + bam_rootlen, &value, &sz) != 0) {
14227c478bd9Sstevel@tonic-gate 		if (bam_smf_check)	/* ignore new during smf check */
14237c478bd9Sstevel@tonic-gate 			return (0);
14247c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
14257c478bd9Sstevel@tonic-gate 		if (bam_verbose)
14267c478bd9Sstevel@tonic-gate 			bam_print(PARSEABLE_NEW_FILE, file);
14277c478bd9Sstevel@tonic-gate 		return (0);
14287c478bd9Sstevel@tonic-gate 	}
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 	/*
14317c478bd9Sstevel@tonic-gate 	 * File exists in old archive. Check if file has changed
14327c478bd9Sstevel@tonic-gate 	 */
14337c478bd9Sstevel@tonic-gate 	assert(sz == 2);
14347c478bd9Sstevel@tonic-gate 	bcopy(value, filestat, sizeof (filestat));
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	if (filestat[0] != stat->st_size ||
14377c478bd9Sstevel@tonic-gate 	    filestat[1] != stat->st_mtime) {
14387c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
14397c478bd9Sstevel@tonic-gate 		if (bam_verbose)
14407c478bd9Sstevel@tonic-gate 			if (bam_smf_check)
14417c478bd9Sstevel@tonic-gate 				bam_print("    %s\n", file);
14427c478bd9Sstevel@tonic-gate 			else
14437c478bd9Sstevel@tonic-gate 				bam_print(PARSEABLE_OUT_DATE, file);
14447c478bd9Sstevel@tonic-gate 	}
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	return (0);
14477c478bd9Sstevel@tonic-gate }
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate /*
14507c478bd9Sstevel@tonic-gate  * Check flags and presence of required files.
14517c478bd9Sstevel@tonic-gate  * The force flag and/or absence of files should
14527c478bd9Sstevel@tonic-gate  * trigger an update.
14537c478bd9Sstevel@tonic-gate  * Suppress stdout output if check (-n) option is set
14547c478bd9Sstevel@tonic-gate  * (as -n should only produce parseable output.)
14557c478bd9Sstevel@tonic-gate  */
14567c478bd9Sstevel@tonic-gate static void
14577c478bd9Sstevel@tonic-gate check_flags_and_files(char *root)
14587c478bd9Sstevel@tonic-gate {
14597c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
14607c478bd9Sstevel@tonic-gate 	struct stat sb;
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 	/*
14637c478bd9Sstevel@tonic-gate 	 * if force, create archive unconditionally
14647c478bd9Sstevel@tonic-gate 	 */
14657c478bd9Sstevel@tonic-gate 	if (bam_force) {
14667c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
14677c478bd9Sstevel@tonic-gate 		if (bam_verbose && !bam_check)
14687c478bd9Sstevel@tonic-gate 			bam_print(UPDATE_FORCE);
14697c478bd9Sstevel@tonic-gate 		return;
14707c478bd9Sstevel@tonic-gate 	}
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 	/*
14737c478bd9Sstevel@tonic-gate 	 * If archive is missing, create archive
14747c478bd9Sstevel@tonic-gate 	 */
1475*ae115bc7Smrj 	(void) snprintf(path, sizeof (path), "%s%s", root,
1476*ae115bc7Smrj 	    DIRECT_BOOT_ARCHIVE_32);
14777c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
14787c478bd9Sstevel@tonic-gate 		if (bam_verbose && !bam_check)
14797c478bd9Sstevel@tonic-gate 			bam_print(UPDATE_ARCH_MISS, path);
14807c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
14817c478bd9Sstevel@tonic-gate 		return;
14827c478bd9Sstevel@tonic-gate 	}
1483*ae115bc7Smrj 	if (bam_direct == BAM_DIRECT_DBOOT) {
1484*ae115bc7Smrj 		(void) snprintf(path, sizeof (path), "%s%s", root,
1485*ae115bc7Smrj 		    DIRECT_BOOT_ARCHIVE_64);
1486*ae115bc7Smrj 		if (stat(path, &sb) != 0) {
1487*ae115bc7Smrj 			if (bam_verbose && !bam_check)
1488*ae115bc7Smrj 				bam_print(UPDATE_ARCH_MISS, path);
1489*ae115bc7Smrj 			walk_arg.need_update = 1;
1490*ae115bc7Smrj 			return;
1491*ae115bc7Smrj 		}
1492*ae115bc7Smrj 	}
14937c478bd9Sstevel@tonic-gate }
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate static error_t
14967c478bd9Sstevel@tonic-gate read_one_list(char *root, filelist_t  *flistp, char *filelist)
14977c478bd9Sstevel@tonic-gate {
14987c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
14997c478bd9Sstevel@tonic-gate 	FILE *fp;
15007c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE];
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, filelist);
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 	fp = fopen(path, "r");
15057c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
15067c478bd9Sstevel@tonic-gate 		if (bam_debug)
15077c478bd9Sstevel@tonic-gate 			bam_error(FLIST_FAIL, path, strerror(errno));
15087c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15097c478bd9Sstevel@tonic-gate 	}
15107c478bd9Sstevel@tonic-gate 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
1511b610f78eSvikram 		/* skip blank lines */
1512b610f78eSvikram 		if (strspn(buf, " \t") == strlen(buf))
1513b610f78eSvikram 			continue;
15147c478bd9Sstevel@tonic-gate 		append_to_flist(flistp, buf);
15157c478bd9Sstevel@tonic-gate 	}
15167c478bd9Sstevel@tonic-gate 	if (fclose(fp) != 0) {
15177c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, path, strerror(errno));
15187c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15197c478bd9Sstevel@tonic-gate 	}
15207c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
15217c478bd9Sstevel@tonic-gate }
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate static error_t
15247c478bd9Sstevel@tonic-gate read_list(char *root, filelist_t  *flistp)
15257c478bd9Sstevel@tonic-gate {
15267c478bd9Sstevel@tonic-gate 	int rval;
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	/*
15317c478bd9Sstevel@tonic-gate 	 * Read current lists of files - only the first is mandatory
15327c478bd9Sstevel@tonic-gate 	 */
15337c478bd9Sstevel@tonic-gate 	rval = read_one_list(root, flistp, BOOT_FILE_LIST);
15347c478bd9Sstevel@tonic-gate 	if (rval != BAM_SUCCESS)
15357c478bd9Sstevel@tonic-gate 		return (rval);
15367c478bd9Sstevel@tonic-gate 	(void) read_one_list(root, flistp, ETC_FILE_LIST);
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL) {
15397c478bd9Sstevel@tonic-gate 		bam_error(NO_FLIST);
15407c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15417c478bd9Sstevel@tonic-gate 	}
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
15447c478bd9Sstevel@tonic-gate }
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate static void
15477c478bd9Sstevel@tonic-gate getoldstat(char *root)
15487c478bd9Sstevel@tonic-gate {
15497c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
15507c478bd9Sstevel@tonic-gate 	int fd, error;
15517c478bd9Sstevel@tonic-gate 	struct stat sb;
15527c478bd9Sstevel@tonic-gate 	char *ostat;
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
15557c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDONLY);
15567c478bd9Sstevel@tonic-gate 	if (fd == -1) {
15577c478bd9Sstevel@tonic-gate 		if (bam_verbose)
15587c478bd9Sstevel@tonic-gate 			bam_print(OPEN_FAIL, path, strerror(errno));
15597c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
15607c478bd9Sstevel@tonic-gate 		return;
15617c478bd9Sstevel@tonic-gate 	}
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 	if (fstat(fd, &sb) != 0) {
15647c478bd9Sstevel@tonic-gate 		bam_error(STAT_FAIL, path, strerror(errno));
15657c478bd9Sstevel@tonic-gate 		(void) close(fd);
15667c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
15677c478bd9Sstevel@tonic-gate 		return;
15687c478bd9Sstevel@tonic-gate 	}
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate 	ostat = s_calloc(1, sb.st_size);
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	if (read(fd, ostat, sb.st_size) != sb.st_size) {
15737c478bd9Sstevel@tonic-gate 		bam_error(READ_FAIL, path, strerror(errno));
15747c478bd9Sstevel@tonic-gate 		(void) close(fd);
15757c478bd9Sstevel@tonic-gate 		free(ostat);
15767c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
15777c478bd9Sstevel@tonic-gate 		return;
15787c478bd9Sstevel@tonic-gate 	}
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	(void) close(fd);
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
15837c478bd9Sstevel@tonic-gate 	error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0);
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 	free(ostat);
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	if (error) {
15887c478bd9Sstevel@tonic-gate 		bam_error(UNPACK_FAIL, path, strerror(error));
15897c478bd9Sstevel@tonic-gate 		walk_arg.old_nvlp = NULL;
15907c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
15917c478bd9Sstevel@tonic-gate 		return;
15927c478bd9Sstevel@tonic-gate 	}
15937c478bd9Sstevel@tonic-gate }
15947c478bd9Sstevel@tonic-gate 
1595a28d77b8Svikram /*
1596a28d77b8Svikram  * Checks if a file in the current (old) archive has
1597a28d77b8Svikram  * been deleted from the root filesystem. This is needed for
1598a28d77b8Svikram  * software like Trusted Extensions (TX) that switch early
1599a28d77b8Svikram  * in boot based on presence/absence of a kernel module.
1600a28d77b8Svikram  */
1601a28d77b8Svikram static void
1602a28d77b8Svikram check4stale(char *root)
1603a28d77b8Svikram {
1604a28d77b8Svikram 	nvpair_t	*nvp;
1605a28d77b8Svikram 	nvlist_t	*nvlp;
1606a28d77b8Svikram 	char 		*file;
1607a28d77b8Svikram 	char		path[PATH_MAX];
1608a28d77b8Svikram 	struct stat	sb;
1609a28d77b8Svikram 
1610a28d77b8Svikram 	/*
1611a28d77b8Svikram 	 * Skip stale file check during smf check
1612a28d77b8Svikram 	 */
1613a28d77b8Svikram 	if (bam_smf_check)
1614a28d77b8Svikram 		return;
1615a28d77b8Svikram 
1616a28d77b8Svikram 	/* Nothing to do if no old stats */
1617a28d77b8Svikram 	if ((nvlp = walk_arg.old_nvlp) == NULL)
1618a28d77b8Svikram 		return;
1619a28d77b8Svikram 
1620a28d77b8Svikram 	for (nvp = nvlist_next_nvpair(nvlp, NULL); nvp;
1621a28d77b8Svikram 	    nvp = nvlist_next_nvpair(nvlp, nvp)) {
1622a28d77b8Svikram 		file = nvpair_name(nvp);
1623a28d77b8Svikram 		if (file == NULL)
1624a28d77b8Svikram 			continue;
1625a28d77b8Svikram 		(void) snprintf(path, sizeof (path), "%s/%s",
1626a28d77b8Svikram 		    root, file);
1627a28d77b8Svikram 		if (stat(path, &sb) == -1) {
1628a28d77b8Svikram 			walk_arg.need_update = 1;
1629a28d77b8Svikram 			if (bam_verbose)
1630a28d77b8Svikram 				bam_print(PARSEABLE_STALE_FILE, path);
1631a28d77b8Svikram 		}
1632a28d77b8Svikram 	}
1633a28d77b8Svikram }
1634a28d77b8Svikram 
16357c478bd9Sstevel@tonic-gate static void
16367c478bd9Sstevel@tonic-gate create_newstat(void)
16377c478bd9Sstevel@tonic-gate {
16387c478bd9Sstevel@tonic-gate 	int error;
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0);
16417c478bd9Sstevel@tonic-gate 	if (error) {
16427c478bd9Sstevel@tonic-gate 		/*
16437c478bd9Sstevel@tonic-gate 		 * Not fatal - we can still create archive
16447c478bd9Sstevel@tonic-gate 		 */
16457c478bd9Sstevel@tonic-gate 		walk_arg.new_nvlp = NULL;
16467c478bd9Sstevel@tonic-gate 		bam_error(NVALLOC_FAIL, strerror(error));
16477c478bd9Sstevel@tonic-gate 	}
16487c478bd9Sstevel@tonic-gate }
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate static void
16517c478bd9Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp)
16527c478bd9Sstevel@tonic-gate {
16537c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
16547c478bd9Sstevel@tonic-gate 	line_t *lp;
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
16577c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, lp->line);
16587c478bd9Sstevel@tonic-gate 		/* XXX shouldn't we use FTW_MOUNT ? */
16597c478bd9Sstevel@tonic-gate 		if (nftw(path, cmpstat, 20, 0) == -1) {
16607c478bd9Sstevel@tonic-gate 			/*
16617c478bd9Sstevel@tonic-gate 			 * Some files may not exist.
16627c478bd9Sstevel@tonic-gate 			 * For example: etc/rtc_config on a x86 diskless system
16637c478bd9Sstevel@tonic-gate 			 * Emit verbose message only
16647c478bd9Sstevel@tonic-gate 			 */
16657c478bd9Sstevel@tonic-gate 			if (bam_verbose)
16667c478bd9Sstevel@tonic-gate 				bam_print(NFTW_FAIL, path, strerror(errno));
16677c478bd9Sstevel@tonic-gate 		}
16687c478bd9Sstevel@tonic-gate 	}
16697c478bd9Sstevel@tonic-gate }
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate static void
16727c478bd9Sstevel@tonic-gate savenew(char *root)
16737c478bd9Sstevel@tonic-gate {
16747c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
16757c478bd9Sstevel@tonic-gate 	char path2[PATH_MAX];
16767c478bd9Sstevel@tonic-gate 	size_t sz;
16777c478bd9Sstevel@tonic-gate 	char *nstat;
16787c478bd9Sstevel@tonic-gate 	int fd, wrote, error;
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	nstat = NULL;
16817c478bd9Sstevel@tonic-gate 	sz = 0;
16827c478bd9Sstevel@tonic-gate 	error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz,
16837c478bd9Sstevel@tonic-gate 	    NV_ENCODE_XDR, 0);
16847c478bd9Sstevel@tonic-gate 	if (error) {
16857c478bd9Sstevel@tonic-gate 		bam_error(PACK_FAIL, strerror(error));
16867c478bd9Sstevel@tonic-gate 		return;
16877c478bd9Sstevel@tonic-gate 	}
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP);
16907c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE);
16917c478bd9Sstevel@tonic-gate 	if (fd == -1) {
16927c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, path, strerror(errno));
16937c478bd9Sstevel@tonic-gate 		free(nstat);
16947c478bd9Sstevel@tonic-gate 		return;
16957c478bd9Sstevel@tonic-gate 	}
16967c478bd9Sstevel@tonic-gate 	wrote = write(fd, nstat, sz);
16977c478bd9Sstevel@tonic-gate 	if (wrote != sz) {
16987c478bd9Sstevel@tonic-gate 		bam_error(WRITE_FAIL, path, strerror(errno));
16997c478bd9Sstevel@tonic-gate 		(void) close(fd);
17007c478bd9Sstevel@tonic-gate 		free(nstat);
17017c478bd9Sstevel@tonic-gate 		return;
17027c478bd9Sstevel@tonic-gate 	}
17037c478bd9Sstevel@tonic-gate 	(void) close(fd);
17047c478bd9Sstevel@tonic-gate 	free(nstat);
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	(void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT);
17077c478bd9Sstevel@tonic-gate 	if (rename(path, path2) != 0) {
17087c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path2, strerror(errno));
17097c478bd9Sstevel@tonic-gate 	}
17107c478bd9Sstevel@tonic-gate }
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate static void
17137c478bd9Sstevel@tonic-gate clear_walk_args(void)
17147c478bd9Sstevel@tonic-gate {
17157c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp)
17167c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.old_nvlp);
17177c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp)
17187c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.new_nvlp);
17197c478bd9Sstevel@tonic-gate 	walk_arg.need_update = 0;
17207c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
17217c478bd9Sstevel@tonic-gate 	walk_arg.new_nvlp = NULL;
17227c478bd9Sstevel@tonic-gate }
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate /*
17257c478bd9Sstevel@tonic-gate  * Returns:
17267c478bd9Sstevel@tonic-gate  *	0 - no update necessary
17277c478bd9Sstevel@tonic-gate  *	1 - update required.
17287c478bd9Sstevel@tonic-gate  *	BAM_ERROR (-1) - An error occurred
17297c478bd9Sstevel@tonic-gate  *
17307c478bd9Sstevel@tonic-gate  * Special handling for check (-n):
17317c478bd9Sstevel@tonic-gate  * ================================
17327c478bd9Sstevel@tonic-gate  * The check (-n) option produces parseable output.
17337c478bd9Sstevel@tonic-gate  * To do this, we suppress all stdout messages unrelated
17347c478bd9Sstevel@tonic-gate  * to out of sync files.
17357c478bd9Sstevel@tonic-gate  * All stderr messages are still printed though.
17367c478bd9Sstevel@tonic-gate  *
17377c478bd9Sstevel@tonic-gate  */
17387c478bd9Sstevel@tonic-gate static int
17397c478bd9Sstevel@tonic-gate update_required(char *root)
17407c478bd9Sstevel@tonic-gate {
17417c478bd9Sstevel@tonic-gate 	struct stat sb;
17427c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
17437c478bd9Sstevel@tonic-gate 	filelist_t flist;
17447c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
17457c478bd9Sstevel@tonic-gate 	int need_update;
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 	walk_arg.need_update = 0;
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	/*
17527c478bd9Sstevel@tonic-gate 	 * Without consulting stat data, check if we need update
17537c478bd9Sstevel@tonic-gate 	 */
17547c478bd9Sstevel@tonic-gate 	check_flags_and_files(root);
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	/*
17577c478bd9Sstevel@tonic-gate 	 * In certain deployment scenarios, filestat may not
17587c478bd9Sstevel@tonic-gate 	 * exist. Ignore it during boot-archive SMF check.
17597c478bd9Sstevel@tonic-gate 	 */
17607c478bd9Sstevel@tonic-gate 	if (bam_smf_check) {
17617c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
17627c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != 0)
17637c478bd9Sstevel@tonic-gate 			return (0);
17647c478bd9Sstevel@tonic-gate 	}
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	/*
17677c478bd9Sstevel@tonic-gate 	 * consult stat data only if we haven't made a decision
17687c478bd9Sstevel@tonic-gate 	 * about update. If checking (-n) however, we always
17697c478bd9Sstevel@tonic-gate 	 * need stat data (since we want to compare old and new)
17707c478bd9Sstevel@tonic-gate 	 */
17717c478bd9Sstevel@tonic-gate 	if (!walk_arg.need_update || bam_check)
17727c478bd9Sstevel@tonic-gate 		getoldstat(root);
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 	/*
1775a28d77b8Svikram 	 * Check if the archive contains files that are no longer
1776a28d77b8Svikram 	 * present on the root filesystem.
1777a28d77b8Svikram 	 */
1778a28d77b8Svikram 	if (!walk_arg.need_update || bam_check)
1779a28d77b8Svikram 		check4stale(root);
1780a28d77b8Svikram 
1781a28d77b8Svikram 	/*
17827c478bd9Sstevel@tonic-gate 	 * read list of files
17837c478bd9Sstevel@tonic-gate 	 */
17847c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
17857c478bd9Sstevel@tonic-gate 		clear_walk_args();
17867c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
17877c478bd9Sstevel@tonic-gate 	}
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 	/*
17927c478bd9Sstevel@tonic-gate 	 * At this point either the update is required
17937c478bd9Sstevel@tonic-gate 	 * or the decision is pending. In either case
17947c478bd9Sstevel@tonic-gate 	 * we need to create new stat nvlist
17957c478bd9Sstevel@tonic-gate 	 */
17967c478bd9Sstevel@tonic-gate 	create_newstat();
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 	/*
17997c478bd9Sstevel@tonic-gate 	 * This walk does 2 things:
18007c478bd9Sstevel@tonic-gate 	 *  	- gets new stat data for every file
18017c478bd9Sstevel@tonic-gate 	 *	- (optional) compare old and new stat data
18027c478bd9Sstevel@tonic-gate 	 */
18037c478bd9Sstevel@tonic-gate 	walk_list(root, &flist);
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	/* done with the file list */
18067c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	/*
18097c478bd9Sstevel@tonic-gate 	 * if we didn't succeed in  creating new stat data above
18107c478bd9Sstevel@tonic-gate 	 * just return result of update check so that archive is built.
18117c478bd9Sstevel@tonic-gate 	 */
18127c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp == NULL) {
18137c478bd9Sstevel@tonic-gate 		bam_error(NO_NEW_STAT);
18147c478bd9Sstevel@tonic-gate 		need_update = walk_arg.need_update;
18157c478bd9Sstevel@tonic-gate 		clear_walk_args();
18167c478bd9Sstevel@tonic-gate 		return (need_update ? 1 : 0);
18177c478bd9Sstevel@tonic-gate 	}
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	/*
18217c478bd9Sstevel@tonic-gate 	 * If no update required, discard newstat
18227c478bd9Sstevel@tonic-gate 	 */
18237c478bd9Sstevel@tonic-gate 	if (!walk_arg.need_update) {
18247c478bd9Sstevel@tonic-gate 		clear_walk_args();
18257c478bd9Sstevel@tonic-gate 		return (0);
18267c478bd9Sstevel@tonic-gate 	}
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 	/*
18297c478bd9Sstevel@tonic-gate 	 * At this point we need an update - so save new stat data
18307c478bd9Sstevel@tonic-gate 	 * However, if only checking (-n), don't save new stat data.
18317c478bd9Sstevel@tonic-gate 	 */
18327c478bd9Sstevel@tonic-gate 	if (!bam_check)
18337c478bd9Sstevel@tonic-gate 		savenew(root);
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 	clear_walk_args();
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	return (1);
18387c478bd9Sstevel@tonic-gate }
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate static error_t
18417c478bd9Sstevel@tonic-gate create_ramdisk(char *root)
18427c478bd9Sstevel@tonic-gate {
18437c478bd9Sstevel@tonic-gate 	char *cmdline, path[PATH_MAX];
18447c478bd9Sstevel@tonic-gate 	size_t len;
18457c478bd9Sstevel@tonic-gate 	struct stat sb;
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	/*
18487c478bd9Sstevel@tonic-gate 	 * Setup command args for create_ramdisk.ksh
18497c478bd9Sstevel@tonic-gate 	 */
18507c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, CREATE_RAMDISK);
18517c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
18527c478bd9Sstevel@tonic-gate 		bam_error(ARCH_EXEC_MISS, path, strerror(errno));
18537c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18547c478bd9Sstevel@tonic-gate 	}
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 	len = strlen(path) + strlen(root) + 10;	/* room for space + -R */
18577c478bd9Sstevel@tonic-gate 	cmdline = s_calloc(1, len);
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 	if (strlen(root) > 1) {
18607c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s -R %s", path, root);
18617c478bd9Sstevel@tonic-gate 		/* chop off / at the end */
18627c478bd9Sstevel@tonic-gate 		cmdline[strlen(cmdline) - 1] = '\0';
18637c478bd9Sstevel@tonic-gate 	} else
18647c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s", path);
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 	if (exec_cmd(cmdline, NULL, 0) != 0) {
18677c478bd9Sstevel@tonic-gate 		bam_error(ARCHIVE_FAIL, cmdline);
18687c478bd9Sstevel@tonic-gate 		free(cmdline);
18697c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18707c478bd9Sstevel@tonic-gate 	}
18717c478bd9Sstevel@tonic-gate 	free(cmdline);
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	/*
18747c478bd9Sstevel@tonic-gate 	 * Verify that the archive has been created
18757c478bd9Sstevel@tonic-gate 	 */
1876*ae115bc7Smrj 	(void) snprintf(path, sizeof (path), "%s%s", root,
1877*ae115bc7Smrj 	    DIRECT_BOOT_ARCHIVE_32);
18787c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
18797c478bd9Sstevel@tonic-gate 		bam_error(ARCHIVE_NOT_CREATED, path);
18807c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18817c478bd9Sstevel@tonic-gate 	}
1882*ae115bc7Smrj 	if (bam_direct == BAM_DIRECT_DBOOT) {
1883*ae115bc7Smrj 		(void) snprintf(path, sizeof (path), "%s%s", root,
1884*ae115bc7Smrj 		    DIRECT_BOOT_ARCHIVE_64);
1885*ae115bc7Smrj 		if (stat(path, &sb) != 0) {
1886*ae115bc7Smrj 			bam_error(ARCHIVE_NOT_CREATED, path);
1887*ae115bc7Smrj 			return (BAM_ERROR);
1888*ae115bc7Smrj 		}
1889*ae115bc7Smrj 	}
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
18927c478bd9Sstevel@tonic-gate }
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate /*
18957c478bd9Sstevel@tonic-gate  * Checks if target filesystem is on a ramdisk
18967c478bd9Sstevel@tonic-gate  * 1 - is miniroot
18977c478bd9Sstevel@tonic-gate  * 0 - is not
18987c478bd9Sstevel@tonic-gate  * When in doubt assume it is not a ramdisk.
18997c478bd9Sstevel@tonic-gate  */
19007c478bd9Sstevel@tonic-gate static int
19017c478bd9Sstevel@tonic-gate is_ramdisk(char *root)
19027c478bd9Sstevel@tonic-gate {
19037c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
19047c478bd9Sstevel@tonic-gate 	FILE *fp;
19057c478bd9Sstevel@tonic-gate 	int found;
1906b610f78eSvikram 	char mntpt[PATH_MAX];
1907b610f78eSvikram 	char *cp;
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	/*
19107c478bd9Sstevel@tonic-gate 	 * There are 3 situations where creating archive is
19117c478bd9Sstevel@tonic-gate 	 * of dubious value:
1912b610f78eSvikram 	 *	- create boot_archive on a lofi-mounted boot_archive
19137c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk which is the root filesystem
19147c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk mounted somewhere else
19157c478bd9Sstevel@tonic-gate 	 * The first is not easy to detect and checking for it is not
19167c478bd9Sstevel@tonic-gate 	 * worth it.
19177c478bd9Sstevel@tonic-gate 	 * The other two conditions are handled here
19187c478bd9Sstevel@tonic-gate 	 */
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
19217c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
19227c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
19237c478bd9Sstevel@tonic-gate 		return (0);
19247c478bd9Sstevel@tonic-gate 	}
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
19277c478bd9Sstevel@tonic-gate 
1928b610f78eSvikram 	/*
1929b610f78eSvikram 	 * Remove any trailing / from the mount point
1930b610f78eSvikram 	 */
1931b610f78eSvikram 	(void) strlcpy(mntpt, root, sizeof (mntpt));
1932b610f78eSvikram 	if (strcmp(root, "/") != 0) {
1933b610f78eSvikram 		cp = mntpt + strlen(mntpt) - 1;
1934b610f78eSvikram 		if (*cp == '/')
1935b610f78eSvikram 			*cp = '\0';
1936b610f78eSvikram 	}
19377c478bd9Sstevel@tonic-gate 	found = 0;
19387c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
1939b610f78eSvikram 		if (strcmp(mnt.mnt_mountp, mntpt) == 0) {
19407c478bd9Sstevel@tonic-gate 			found = 1;
19417c478bd9Sstevel@tonic-gate 			break;
19427c478bd9Sstevel@tonic-gate 		}
19437c478bd9Sstevel@tonic-gate 	}
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 	if (!found) {
19467c478bd9Sstevel@tonic-gate 		if (bam_verbose)
1947b610f78eSvikram 			bam_error(NOT_IN_MNTTAB, mntpt);
19487c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
19497c478bd9Sstevel@tonic-gate 		return (0);
19507c478bd9Sstevel@tonic-gate 	}
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 	if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) {
19537c478bd9Sstevel@tonic-gate 		if (bam_verbose)
19547c478bd9Sstevel@tonic-gate 			bam_error(IS_RAMDISK, bam_root);
19557c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
19567c478bd9Sstevel@tonic-gate 		return (1);
19577c478bd9Sstevel@tonic-gate 	}
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	return (0);
19627c478bd9Sstevel@tonic-gate }
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate static int
19657c478bd9Sstevel@tonic-gate is_newboot(char *root)
19667c478bd9Sstevel@tonic-gate {
19677c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
19687c478bd9Sstevel@tonic-gate 	struct stat sb;
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 	/*
19717c478bd9Sstevel@tonic-gate 	 * We can't boot without MULTI_BOOT
19727c478bd9Sstevel@tonic-gate 	 */
19737c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, MULTI_BOOT);
19747c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
19757c478bd9Sstevel@tonic-gate 		if (bam_verbose)
19767c478bd9Sstevel@tonic-gate 			bam_print(FILE_MISS, path);
19777c478bd9Sstevel@tonic-gate 		return (0);
19787c478bd9Sstevel@tonic-gate 	}
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 	/*
19817c478bd9Sstevel@tonic-gate 	 * We can't generate archive without GRUB_DIR
19827c478bd9Sstevel@tonic-gate 	 */
19837c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, GRUB_DIR);
19847c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
19857c478bd9Sstevel@tonic-gate 		if (bam_verbose)
19867c478bd9Sstevel@tonic-gate 			bam_print(DIR_MISS, path);
19877c478bd9Sstevel@tonic-gate 		return (0);
19887c478bd9Sstevel@tonic-gate 	}
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	return (1);
19917c478bd9Sstevel@tonic-gate }
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate static int
19947c478bd9Sstevel@tonic-gate is_readonly(char *root)
19957c478bd9Sstevel@tonic-gate {
19967c478bd9Sstevel@tonic-gate 	struct statvfs vfs;
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 	/*
19997c478bd9Sstevel@tonic-gate 	 * Check for RDONLY filesystem
20007c478bd9Sstevel@tonic-gate 	 * When in doubt assume it is not readonly
20017c478bd9Sstevel@tonic-gate 	 */
20027c478bd9Sstevel@tonic-gate 	if (statvfs(root, &vfs) != 0) {
20037c478bd9Sstevel@tonic-gate 		if (bam_verbose)
20047c478bd9Sstevel@tonic-gate 			bam_error(STATVFS_FAIL, root, strerror(errno));
20057c478bd9Sstevel@tonic-gate 		return (0);
20067c478bd9Sstevel@tonic-gate 	}
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 	if (vfs.f_flag & ST_RDONLY) {
20097c478bd9Sstevel@tonic-gate 		return (1);
20107c478bd9Sstevel@tonic-gate 	}
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	return (0);
20137c478bd9Sstevel@tonic-gate }
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate static error_t
20167c478bd9Sstevel@tonic-gate update_archive(char *root, char *opt)
20177c478bd9Sstevel@tonic-gate {
20187c478bd9Sstevel@tonic-gate 	error_t ret;
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 	assert(root);
20217c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 	/*
2024b610f78eSvikram 	 * root must belong to a GRUB boot OS,
20257c478bd9Sstevel@tonic-gate 	 * don't care on sparc except for diskless clients
20267c478bd9Sstevel@tonic-gate 	 */
20277c478bd9Sstevel@tonic-gate 	if (!is_newboot(root)) {
2028b610f78eSvikram 		/*
2029b610f78eSvikram 		 * Emit message only if not in context of update_all.
2030b610f78eSvikram 		 * If in update_all, emit only if verbose flag is set.
2031b610f78eSvikram 		 */
2032b610f78eSvikram 		if (!bam_update_all || bam_verbose)
2033b610f78eSvikram 			bam_print(NOT_GRUB_BOOT, root);
20347c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
20357c478bd9Sstevel@tonic-gate 	}
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	/*
20388c1b6884Sszhou 	 * If smf check is requested when / is writable (can happen
20398c1b6884Sszhou 	 * on first reboot following an upgrade because service
20408c1b6884Sszhou 	 * dependency is messed up), skip the check.
20418c1b6884Sszhou 	 */
20428c1b6884Sszhou 	if (bam_smf_check && !bam_root_readonly)
20438c1b6884Sszhou 		return (BAM_SUCCESS);
20448c1b6884Sszhou 
20458c1b6884Sszhou 	/*
20468c1b6884Sszhou 	 * root must be writable. This check applies to alternate
20478c1b6884Sszhou 	 * root (-R option); bam_root_readonly applies to '/' only.
20487c478bd9Sstevel@tonic-gate 	 * Note: statvfs() does not always report the truth
20497c478bd9Sstevel@tonic-gate 	 */
205061cb17bdSsetje 	if (!bam_smf_check && !bam_check && is_readonly(root)) {
20518c1b6884Sszhou 		if (bam_verbose)
20527c478bd9Sstevel@tonic-gate 			bam_print(RDONLY_FS, root);
20537c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
20547c478bd9Sstevel@tonic-gate 	}
20557c478bd9Sstevel@tonic-gate 
20567c478bd9Sstevel@tonic-gate 	/*
20577c478bd9Sstevel@tonic-gate 	 * Don't generate archive on ramdisk
20587c478bd9Sstevel@tonic-gate 	 */
20597c478bd9Sstevel@tonic-gate 	if (is_ramdisk(root)) {
20607c478bd9Sstevel@tonic-gate 		if (bam_verbose)
20617c478bd9Sstevel@tonic-gate 			bam_print(SKIP_RAMDISK);
20627c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
20637c478bd9Sstevel@tonic-gate 	}
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 	/*
20667c478bd9Sstevel@tonic-gate 	 * Now check if updated is really needed
20677c478bd9Sstevel@tonic-gate 	 */
20687c478bd9Sstevel@tonic-gate 	ret = update_required(root);
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 	/*
20717c478bd9Sstevel@tonic-gate 	 * The check command (-n) is *not* a dry run
20727c478bd9Sstevel@tonic-gate 	 * It only checks if the archive is in sync.
20737c478bd9Sstevel@tonic-gate 	 */
20747c478bd9Sstevel@tonic-gate 	if (bam_check) {
20757c478bd9Sstevel@tonic-gate 		bam_exit((ret != 0) ? 1 : 0);
20767c478bd9Sstevel@tonic-gate 	}
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate 	if (ret == 1) {
20797c478bd9Sstevel@tonic-gate 		/* create the ramdisk */
20807c478bd9Sstevel@tonic-gate 		ret = create_ramdisk(root);
20817c478bd9Sstevel@tonic-gate 	}
20827c478bd9Sstevel@tonic-gate 	return (ret);
20837c478bd9Sstevel@tonic-gate }
20847c478bd9Sstevel@tonic-gate 
2085b610f78eSvikram static void
2086fbac2b2bSvikram update_fdisk(void)
2087fbac2b2bSvikram {
2088fbac2b2bSvikram 	struct stat sb;
2089fbac2b2bSvikram 	char cmd[PATH_MAX];
2090fbac2b2bSvikram 	int ret1, ret2;
2091fbac2b2bSvikram 
2092fbac2b2bSvikram 	assert(stat(GRUB_fdisk, &sb) == 0);
2093fbac2b2bSvikram 	assert(stat(GRUB_fdisk_target, &sb) == 0);
2094fbac2b2bSvikram 
2095fbac2b2bSvikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/fdisk -F %s `/bin/cat %s`",
2096fbac2b2bSvikram 	    GRUB_fdisk, GRUB_fdisk_target);
2097fbac2b2bSvikram 
2098fbac2b2bSvikram 	bam_print(UPDATING_FDISK);
2099fbac2b2bSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2100fbac2b2bSvikram 		bam_error(FDISK_UPDATE_FAILED);
2101fbac2b2bSvikram 	}
2102fbac2b2bSvikram 
2103fbac2b2bSvikram 	/*
2104fbac2b2bSvikram 	 * We are done, remove the files.
2105fbac2b2bSvikram 	 */
2106fbac2b2bSvikram 	ret1 = unlink(GRUB_fdisk);
2107fbac2b2bSvikram 	ret2 = unlink(GRUB_fdisk_target);
2108fbac2b2bSvikram 	if (ret1 != 0 || ret2 != 0) {
2109fbac2b2bSvikram 		bam_error(FILE_REMOVE_FAILED, GRUB_fdisk, GRUB_fdisk_target);
2110fbac2b2bSvikram 	}
2111fbac2b2bSvikram }
2112fbac2b2bSvikram 
2113fbac2b2bSvikram static void
2114b610f78eSvikram restore_grub_slice(void)
2115b610f78eSvikram {
2116b610f78eSvikram 	struct stat sb;
2117b610f78eSvikram 	char *mntpt, *physlice;
2118b610f78eSvikram 	int mnted;	/* set if we did a mount */
2119b610f78eSvikram 	char menupath[PATH_MAX], cmd[PATH_MAX];
2120b610f78eSvikram 
2121b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
2122b610f78eSvikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
2123b610f78eSvikram 		return;
2124b610f78eSvikram 	}
2125b610f78eSvikram 
2126b610f78eSvikram 	/*
2127b610f78eSvikram 	 * If we are doing an luactivate, don't attempt to restore GRUB or else
2128b610f78eSvikram 	 * we may not be able to get to DCA boot environments. Let luactivate
2129b610f78eSvikram 	 * handle GRUB/DCA installation
2130b610f78eSvikram 	 */
2131b610f78eSvikram 	if (stat(LU_ACTIVATE_FILE, &sb) == 0) {
2132b610f78eSvikram 		return;
2133b610f78eSvikram 	}
2134b610f78eSvikram 
2135b610f78eSvikram 	mnted = 0;
2136b610f78eSvikram 	physlice = NULL;
213740541d5dSvikram 	mntpt = mount_grub_slice(&mnted, &physlice, NULL, NULL);
2138b610f78eSvikram 	if (mntpt == NULL) {
2139b610f78eSvikram 		bam_error(CANNOT_RESTORE_GRUB_SLICE);
2140b610f78eSvikram 		return;
2141b610f78eSvikram 	}
2142b610f78eSvikram 
2143b610f78eSvikram 	(void) snprintf(menupath, sizeof (menupath), "%s%s", mntpt, GRUB_MENU);
2144b610f78eSvikram 	if (stat(menupath, &sb) == 0) {
214540541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2146b610f78eSvikram 		return;
2147b610f78eSvikram 	}
2148b610f78eSvikram 
2149b610f78eSvikram 	/*
2150b610f78eSvikram 	 * The menu is missing - we need to do a restore
2151b610f78eSvikram 	 */
2152b610f78eSvikram 	bam_print(RESTORING_GRUB);
2153b610f78eSvikram 
2154b610f78eSvikram 	(void) snprintf(cmd, sizeof (cmd), "%s %s %s %s",
2155b610f78eSvikram 	    INSTALLGRUB, STAGE1, STAGE2, physlice);
2156b610f78eSvikram 
2157b610f78eSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2158b610f78eSvikram 		bam_error(RESTORE_GRUB_FAILED);
215940541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2160b610f78eSvikram 		return;
2161b610f78eSvikram 	}
2162b610f78eSvikram 
2163b610f78eSvikram 	if (stat(GRUB_backup_menu, &sb) != 0) {
2164b610f78eSvikram 		bam_error(MISSING_BACKUP_MENU,
2165b610f78eSvikram 		    GRUB_backup_menu, strerror(errno));
216640541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2167b610f78eSvikram 		return;
2168b610f78eSvikram 	}
2169b610f78eSvikram 
2170b610f78eSvikram 	(void) snprintf(cmd, sizeof (cmd), "/bin/cp %s %s",
2171b610f78eSvikram 	    GRUB_backup_menu, menupath);
2172b610f78eSvikram 
2173b610f78eSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2174b610f78eSvikram 		bam_error(RESTORE_MENU_FAILED, menupath);
217540541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2176b610f78eSvikram 		return;
2177b610f78eSvikram 	}
2178b610f78eSvikram 
2179b610f78eSvikram 	/* Success */
218040541d5dSvikram 	umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2181b610f78eSvikram }
2182b610f78eSvikram 
21837c478bd9Sstevel@tonic-gate static error_t
21847c478bd9Sstevel@tonic-gate update_all(char *root, char *opt)
21857c478bd9Sstevel@tonic-gate {
21867c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
21877c478bd9Sstevel@tonic-gate 	struct stat sb;
21887c478bd9Sstevel@tonic-gate 	FILE *fp;
21897c478bd9Sstevel@tonic-gate 	char multibt[PATH_MAX];
21907c478bd9Sstevel@tonic-gate 	error_t ret = BAM_SUCCESS;
2191fbac2b2bSvikram 	int ret1, ret2;
21927c478bd9Sstevel@tonic-gate 
219340541d5dSvikram 	assert(root);
21947c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
21957c478bd9Sstevel@tonic-gate 
219640541d5dSvikram 	if (bam_rootlen != 1 || *root != '/') {
219740541d5dSvikram 		elide_trailing_slash(root, multibt, sizeof (multibt));
219840541d5dSvikram 		bam_error(ALT_ROOT_INVALID, multibt);
219940541d5dSvikram 		return (BAM_ERROR);
220040541d5dSvikram 	}
220140541d5dSvikram 
22027c478bd9Sstevel@tonic-gate 	/*
22037c478bd9Sstevel@tonic-gate 	 * First update archive for current root
22047c478bd9Sstevel@tonic-gate 	 */
22057c478bd9Sstevel@tonic-gate 	if (update_archive(root, opt) != BAM_SUCCESS)
22067c478bd9Sstevel@tonic-gate 		ret = BAM_ERROR;
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	/*
22097c478bd9Sstevel@tonic-gate 	 * Now walk the mount table, performing archive update
22107c478bd9Sstevel@tonic-gate 	 * for all mounted Newboot root filesystems
22117c478bd9Sstevel@tonic-gate 	 */
22127c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
22137c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
22147c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
2215b610f78eSvikram 		ret = BAM_ERROR;
2216b610f78eSvikram 		goto out;
22177c478bd9Sstevel@tonic-gate 	}
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
22227c478bd9Sstevel@tonic-gate 		if (mnt.mnt_special == NULL)
22237c478bd9Sstevel@tonic-gate 			continue;
22247c478bd9Sstevel@tonic-gate 		if (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0)
22257c478bd9Sstevel@tonic-gate 			continue;
22267c478bd9Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, "/") == 0)
22277c478bd9Sstevel@tonic-gate 			continue;
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 		(void) snprintf(multibt, sizeof (multibt), "%s%s",
22307c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp, MULTI_BOOT);
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 		if (stat(multibt, &sb) == -1)
22337c478bd9Sstevel@tonic-gate 			continue;
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 		/*
22367c478bd9Sstevel@tonic-gate 		 * We put a trailing slash to be consistent with root = "/"
22377c478bd9Sstevel@tonic-gate 		 * case, such that we don't have to print // in some cases.
22387c478bd9Sstevel@tonic-gate 		 */
22397c478bd9Sstevel@tonic-gate 		(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
22407c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp);
22417c478bd9Sstevel@tonic-gate 		bam_rootlen = strlen(rootbuf);
2242*ae115bc7Smrj 
2243*ae115bc7Smrj 		/*
2244*ae115bc7Smrj 		 * It's possible that other mounts may be an alternate boot
2245*ae115bc7Smrj 		 * architecture, so check it again.
2246*ae115bc7Smrj 		 */
2247*ae115bc7Smrj 		if ((dboot_or_multiboot(rootbuf) != BAM_SUCCESS) ||
2248*ae115bc7Smrj 		    (update_archive(rootbuf, opt) != BAM_SUCCESS))
22497c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
22507c478bd9Sstevel@tonic-gate 	}
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
22537c478bd9Sstevel@tonic-gate 
2254b610f78eSvikram out:
2255b610f78eSvikram 	if (stat(GRUB_slice, &sb) == 0) {
2256b610f78eSvikram 		restore_grub_slice();
2257b610f78eSvikram 	}
2258b610f78eSvikram 
2259fbac2b2bSvikram 	/*
2260fbac2b2bSvikram 	 * Update fdisk table as we go down. Updating it when
2261fbac2b2bSvikram 	 * the system is running will confuse biosdev.
2262fbac2b2bSvikram 	 */
2263fbac2b2bSvikram 	ret1 = stat(GRUB_fdisk, &sb);
2264fbac2b2bSvikram 	ret2 = stat(GRUB_fdisk_target, &sb);
2265fbac2b2bSvikram 	if ((ret1 == 0) && (ret2 == 0)) {
2266fbac2b2bSvikram 		update_fdisk();
2267fbac2b2bSvikram 	} else if ((ret1 == 0) ^ (ret2 == 0)) {
2268fbac2b2bSvikram 		/*
2269fbac2b2bSvikram 		 * It is an error for one file to be
2270fbac2b2bSvikram 		 * present and the other absent.
2271fbac2b2bSvikram 		 * It is normal for both files to be
2272fbac2b2bSvikram 		 * absent - it indicates that no fdisk
2273fbac2b2bSvikram 		 * update is required.
2274fbac2b2bSvikram 		 */
2275fbac2b2bSvikram 		bam_error(MISSING_FDISK_FILE,
2276fbac2b2bSvikram 		    ret1 ? GRUB_fdisk : GRUB_fdisk_target);
2277fbac2b2bSvikram 		ret = BAM_ERROR;
2278fbac2b2bSvikram 	}
2279fbac2b2bSvikram 
22807c478bd9Sstevel@tonic-gate 	return (ret);
22817c478bd9Sstevel@tonic-gate }
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate static void
22847c478bd9Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp)
22857c478bd9Sstevel@tonic-gate {
22867c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
22877c478bd9Sstevel@tonic-gate 		mp->start = lp;
22887c478bd9Sstevel@tonic-gate 	} else {
22897c478bd9Sstevel@tonic-gate 		mp->end->next = lp;
22908c1b6884Sszhou 		lp->prev = mp->end;
22917c478bd9Sstevel@tonic-gate 	}
22927c478bd9Sstevel@tonic-gate 	mp->end = lp;
22937c478bd9Sstevel@tonic-gate }
22947c478bd9Sstevel@tonic-gate 
22958c1b6884Sszhou static void
22968c1b6884Sszhou unlink_line(menu_t *mp, line_t *lp)
22978c1b6884Sszhou {
22988c1b6884Sszhou 	/* unlink from list */
22998c1b6884Sszhou 	if (lp->prev)
23008c1b6884Sszhou 		lp->prev->next = lp->next;
23018c1b6884Sszhou 	else
23028c1b6884Sszhou 		mp->start = lp->next;
23038c1b6884Sszhou 	if (lp->next)
23048c1b6884Sszhou 		lp->next->prev = lp->prev;
23058c1b6884Sszhou 	else
23068c1b6884Sszhou 		mp->end = lp->prev;
23078c1b6884Sszhou }
23088c1b6884Sszhou 
23098c1b6884Sszhou static entry_t *
23108c1b6884Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end)
23118c1b6884Sszhou {
23128c1b6884Sszhou 	entry_t *ent, *prev;
23138c1b6884Sszhou 
23148c1b6884Sszhou 	ent = s_calloc(1, sizeof (entry_t));
23158c1b6884Sszhou 	ent->start = start;
23168c1b6884Sszhou 	ent->end = end;
23178c1b6884Sszhou 
23188c1b6884Sszhou 	if (mp->entries == NULL) {
23198c1b6884Sszhou 		mp->entries = ent;
23208c1b6884Sszhou 		return (ent);
23218c1b6884Sszhou 	}
23228c1b6884Sszhou 
23238c1b6884Sszhou 	prev = mp->entries;
23248c1b6884Sszhou 	while (prev->next)
23258c1b6884Sszhou 		prev = prev-> next;
23268c1b6884Sszhou 	prev->next = ent;
23278c1b6884Sszhou 	ent->prev = prev;
23288c1b6884Sszhou 	return (ent);
23298c1b6884Sszhou }
23308c1b6884Sszhou 
23318c1b6884Sszhou static void
23328c1b6884Sszhou boot_entry_addline(entry_t *ent, line_t *lp)
23338c1b6884Sszhou {
23348c1b6884Sszhou 	if (ent)
23358c1b6884Sszhou 		ent->end = lp;
23368c1b6884Sszhou }
23378c1b6884Sszhou 
23387c478bd9Sstevel@tonic-gate /*
23397c478bd9Sstevel@tonic-gate  * A line in menu.lst looks like
23407c478bd9Sstevel@tonic-gate  * [ ]*<cmd>[ \t=]*<arg>*
23417c478bd9Sstevel@tonic-gate  */
23427c478bd9Sstevel@tonic-gate static void
23437c478bd9Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum)
23447c478bd9Sstevel@tonic-gate {
23457c478bd9Sstevel@tonic-gate 	/*
23467c478bd9Sstevel@tonic-gate 	 * save state across calls. This is so that
23477c478bd9Sstevel@tonic-gate 	 * header gets the right entry# after title has
23487c478bd9Sstevel@tonic-gate 	 * been processed
23497c478bd9Sstevel@tonic-gate 	 */
23508c1b6884Sszhou 	static line_t *prev = NULL;
23518c1b6884Sszhou 	static entry_t *curr_ent = NULL;
2352*ae115bc7Smrj 	static int in_liveupgrade = 0;
23537c478bd9Sstevel@tonic-gate 
23547c478bd9Sstevel@tonic-gate 	line_t	*lp;
23557c478bd9Sstevel@tonic-gate 	char *cmd, *sep, *arg;
23567c478bd9Sstevel@tonic-gate 	char save, *cp, *line;
23577c478bd9Sstevel@tonic-gate 	menu_flag_t flag = BAM_INVALID;
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 	if (str == NULL) {
23607c478bd9Sstevel@tonic-gate 		return;
23617c478bd9Sstevel@tonic-gate 	}
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 	/*
23647c478bd9Sstevel@tonic-gate 	 * First save a copy of the entire line.
23657c478bd9Sstevel@tonic-gate 	 * We use this later to set the line field.
23667c478bd9Sstevel@tonic-gate 	 */
23677c478bd9Sstevel@tonic-gate 	line = s_strdup(str);
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate 	/* Eat up leading whitespace */
23707c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
23717c478bd9Sstevel@tonic-gate 		str++;
23727c478bd9Sstevel@tonic-gate 
23737c478bd9Sstevel@tonic-gate 	if (*str == '#') {		/* comment */
23747c478bd9Sstevel@tonic-gate 		cmd = s_strdup("#");
23757c478bd9Sstevel@tonic-gate 		sep = NULL;
23767c478bd9Sstevel@tonic-gate 		arg = s_strdup(str + 1);
23777c478bd9Sstevel@tonic-gate 		flag = BAM_COMMENT;
2378*ae115bc7Smrj 		if (strstr(arg, BAM_LU_HDR) != NULL) {
2379*ae115bc7Smrj 			in_liveupgrade = 1;
2380*ae115bc7Smrj 		} else if (strstr(arg, BAM_LU_FTR) != NULL) {
2381*ae115bc7Smrj 			in_liveupgrade = 0;
2382*ae115bc7Smrj 		}
23837c478bd9Sstevel@tonic-gate 	} else if (*str == '\0') {	/* blank line */
23847c478bd9Sstevel@tonic-gate 		cmd = sep = arg = NULL;
23857c478bd9Sstevel@tonic-gate 		flag = BAM_EMPTY;
23867c478bd9Sstevel@tonic-gate 	} else {
23877c478bd9Sstevel@tonic-gate 		/*
23887c478bd9Sstevel@tonic-gate 		 * '=' is not a documented separator in grub syntax.
23897c478bd9Sstevel@tonic-gate 		 * However various development bits use '=' as a
23907c478bd9Sstevel@tonic-gate 		 * separator. In addition, external users also
23917c478bd9Sstevel@tonic-gate 		 * use = as a separator. So we will allow that usage.
23927c478bd9Sstevel@tonic-gate 		 */
23937c478bd9Sstevel@tonic-gate 		cp = str;
23947c478bd9Sstevel@tonic-gate 		while (*str != ' ' && *str != '\t' && *str != '=') {
23957c478bd9Sstevel@tonic-gate 			if (*str == '\0') {
23967c478bd9Sstevel@tonic-gate 				cmd = s_strdup(cp);
23977c478bd9Sstevel@tonic-gate 				sep = arg = NULL;
23987c478bd9Sstevel@tonic-gate 				break;
23997c478bd9Sstevel@tonic-gate 			}
24007c478bd9Sstevel@tonic-gate 			str++;
24017c478bd9Sstevel@tonic-gate 		}
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate 		if (*str != '\0') {
24047c478bd9Sstevel@tonic-gate 			save = *str;
24057c478bd9Sstevel@tonic-gate 			*str = '\0';
24067c478bd9Sstevel@tonic-gate 			cmd = s_strdup(cp);
24077c478bd9Sstevel@tonic-gate 			*str = save;
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 			str++;
24107c478bd9Sstevel@tonic-gate 			save = *str;
24117c478bd9Sstevel@tonic-gate 			*str = '\0';
24127c478bd9Sstevel@tonic-gate 			sep = s_strdup(str - 1);
24137c478bd9Sstevel@tonic-gate 			*str = save;
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 			while (*str == ' ' || *str == '\t')
24167c478bd9Sstevel@tonic-gate 				str++;
24177c478bd9Sstevel@tonic-gate 			if (*str == '\0')
24187c478bd9Sstevel@tonic-gate 				arg = NULL;
24197c478bd9Sstevel@tonic-gate 			else
24207c478bd9Sstevel@tonic-gate 				arg = s_strdup(str);
24217c478bd9Sstevel@tonic-gate 		}
24227c478bd9Sstevel@tonic-gate 	}
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate 	lp->cmd = cmd;
24277c478bd9Sstevel@tonic-gate 	lp->sep = sep;
24287c478bd9Sstevel@tonic-gate 	lp->arg = arg;
24297c478bd9Sstevel@tonic-gate 	lp->line = line;
24307c478bd9Sstevel@tonic-gate 	lp->lineNum = ++(*lineNum);
24317c478bd9Sstevel@tonic-gate 	if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) {
24327c478bd9Sstevel@tonic-gate 		lp->entryNum = ++(*entryNum);
24337c478bd9Sstevel@tonic-gate 		lp->flags = BAM_TITLE;
24347c478bd9Sstevel@tonic-gate 		if (prev && prev->flags == BAM_COMMENT &&
2435*ae115bc7Smrj 		    prev->arg && strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
24367c478bd9Sstevel@tonic-gate 			prev->entryNum = lp->entryNum;
24378c1b6884Sszhou 			curr_ent = boot_entry_new(mp, prev, lp);
2438*ae115bc7Smrj 			curr_ent->flags = BAM_ENTRY_BOOTADM;
24398c1b6884Sszhou 		} else {
24408c1b6884Sszhou 			curr_ent = boot_entry_new(mp, lp, lp);
2441*ae115bc7Smrj 			if (in_liveupgrade) {
2442*ae115bc7Smrj 				curr_ent->flags = BAM_ENTRY_LU;
24438c1b6884Sszhou 			}
2444*ae115bc7Smrj 		}
2445*ae115bc7Smrj 		curr_ent->entryNum = *entryNum;
24467c478bd9Sstevel@tonic-gate 	} else if (flag != BAM_INVALID) {
24477c478bd9Sstevel@tonic-gate 		/*
24487c478bd9Sstevel@tonic-gate 		 * For header comments, the entry# is "fixed up"
24497c478bd9Sstevel@tonic-gate 		 * by the subsequent title
24507c478bd9Sstevel@tonic-gate 		 */
24517c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
24527c478bd9Sstevel@tonic-gate 		lp->flags = flag;
24537c478bd9Sstevel@tonic-gate 	} else {
24547c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
2455*ae115bc7Smrj 
2456*ae115bc7Smrj 		if (*entryNum == ENTRY_INIT) {
2457*ae115bc7Smrj 			lp->flags = BAM_GLOBAL;
2458*ae115bc7Smrj 		} else {
2459*ae115bc7Smrj 			lp->flags = BAM_ENTRY;
2460*ae115bc7Smrj 
2461*ae115bc7Smrj 			if (cmd && arg) {
2462*ae115bc7Smrj 				/*
2463*ae115bc7Smrj 				 * We only compare for the length of "module"
2464*ae115bc7Smrj 				 * so that "module$" will also match.
2465*ae115bc7Smrj 				 */
2466*ae115bc7Smrj 				if ((strncmp(cmd, menu_cmds[MODULE_CMD],
2467*ae115bc7Smrj 				    strlen(menu_cmds[MODULE_CMD])) == 0) &&
2468*ae115bc7Smrj 				    (strcmp(arg, MINIROOT) == 0))
2469*ae115bc7Smrj 					curr_ent->flags |= BAM_ENTRY_MINIROOT;
2470*ae115bc7Smrj 				else if (strcmp(cmd, menu_cmds[ROOT_CMD]) == 0)
2471*ae115bc7Smrj 					curr_ent->flags |= BAM_ENTRY_ROOT;
2472*ae115bc7Smrj 				else if (strcmp(cmd,
2473*ae115bc7Smrj 				    menu_cmds[CHAINLOADER_CMD]) == 0)
2474*ae115bc7Smrj 					curr_ent->flags |=
2475*ae115bc7Smrj 					    BAM_ENTRY_CHAINLOADER;
2476*ae115bc7Smrj 			}
2477*ae115bc7Smrj 		}
24787c478bd9Sstevel@tonic-gate 	}
24797c478bd9Sstevel@tonic-gate 
24808c1b6884Sszhou 	/* record default, old default, and entry line ranges */
24818c1b6884Sszhou 	if (lp->flags == BAM_GLOBAL &&
24828c1b6884Sszhou 	    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) {
24838c1b6884Sszhou 		mp->curdefault = lp;
24848c1b6884Sszhou 	} else if (lp->flags == BAM_COMMENT &&
24858c1b6884Sszhou 	    strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) {
24868c1b6884Sszhou 		mp->olddefault = lp;
2487*ae115bc7Smrj 	} else if (lp->flags == BAM_COMMENT &&
2488*ae115bc7Smrj 	    strncmp(lp->arg, BAM_OLD_RC_DEF, strlen(BAM_OLD_RC_DEF)) == 0) {
2489*ae115bc7Smrj 		mp->old_rc_default = lp;
24908c1b6884Sszhou 	} else if (lp->flags == BAM_ENTRY ||
2491*ae115bc7Smrj 	    (lp->flags == BAM_COMMENT &&
2492*ae115bc7Smrj 	    strcmp(lp->arg, BAM_BOOTADM_FTR) == 0)) {
24938c1b6884Sszhou 		boot_entry_addline(curr_ent, lp);
24948c1b6884Sszhou 	}
24957c478bd9Sstevel@tonic-gate 	append_line(mp, lp);
24967c478bd9Sstevel@tonic-gate 
24977c478bd9Sstevel@tonic-gate 	prev = lp;
24987c478bd9Sstevel@tonic-gate }
24997c478bd9Sstevel@tonic-gate 
250040541d5dSvikram static void
250140541d5dSvikram update_numbering(menu_t *mp)
250240541d5dSvikram {
250340541d5dSvikram 	int lineNum;
250440541d5dSvikram 	int entryNum;
250540541d5dSvikram 	int old_default_value;
250640541d5dSvikram 	line_t *lp, *prev, *default_lp, *default_entry;
250740541d5dSvikram 	char buf[PATH_MAX];
250840541d5dSvikram 
250940541d5dSvikram 	if (mp->start == NULL) {
251040541d5dSvikram 		return;
251140541d5dSvikram 	}
251240541d5dSvikram 
251340541d5dSvikram 	lineNum = LINE_INIT;
251440541d5dSvikram 	entryNum = ENTRY_INIT;
251540541d5dSvikram 	old_default_value = ENTRY_INIT;
251640541d5dSvikram 	lp = default_lp = default_entry = NULL;
251740541d5dSvikram 
251840541d5dSvikram 	prev = NULL;
251940541d5dSvikram 	for (lp = mp->start; lp; prev = lp, lp = lp->next) {
252040541d5dSvikram 		lp->lineNum = ++lineNum;
252140541d5dSvikram 
252240541d5dSvikram 		/*
252340541d5dSvikram 		 * Get the value of the default command
252440541d5dSvikram 		 */
252540541d5dSvikram 		if (lp->entryNum == ENTRY_INIT && lp->cmd &&
252640541d5dSvikram 		    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 &&
252740541d5dSvikram 		    lp->arg) {
252840541d5dSvikram 			old_default_value = atoi(lp->arg);
252940541d5dSvikram 			default_lp = lp;
253040541d5dSvikram 		}
253140541d5dSvikram 
253240541d5dSvikram 		/*
253340541d5dSvikram 		 * If not boot entry, nothing else to fix for this
253440541d5dSvikram 		 * entry
253540541d5dSvikram 		 */
253640541d5dSvikram 		if (lp->entryNum == ENTRY_INIT)
253740541d5dSvikram 			continue;
253840541d5dSvikram 
253940541d5dSvikram 		/*
254040541d5dSvikram 		 * Record the position of the default entry.
254140541d5dSvikram 		 * The following works because global
254240541d5dSvikram 		 * commands like default and timeout should precede
254340541d5dSvikram 		 * actual boot entries, so old_default_value
254440541d5dSvikram 		 * is already known (or default cmd is missing).
254540541d5dSvikram 		 */
254640541d5dSvikram 		if (default_entry == NULL &&
254740541d5dSvikram 		    old_default_value != ENTRY_INIT &&
254840541d5dSvikram 		    lp->entryNum == old_default_value) {
254940541d5dSvikram 			default_entry = lp;
255040541d5dSvikram 		}
255140541d5dSvikram 
255240541d5dSvikram 		/*
255340541d5dSvikram 		 * Now fixup the entry number
255440541d5dSvikram 		 */
255540541d5dSvikram 		if (lp->cmd && strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) {
255640541d5dSvikram 			lp->entryNum = ++entryNum;
255740541d5dSvikram 			/* fixup the bootadm header */
255840541d5dSvikram 			if (prev && prev->flags == BAM_COMMENT &&
2559*ae115bc7Smrj 			    prev->arg &&
2560*ae115bc7Smrj 			    strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
256140541d5dSvikram 				prev->entryNum = lp->entryNum;
256240541d5dSvikram 			}
256340541d5dSvikram 		} else {
256440541d5dSvikram 			lp->entryNum = entryNum;
256540541d5dSvikram 		}
256640541d5dSvikram 	}
256740541d5dSvikram 
256840541d5dSvikram 	/*
256940541d5dSvikram 	 * No default command in menu, simply return
257040541d5dSvikram 	 */
257140541d5dSvikram 	if (default_lp == NULL) {
257240541d5dSvikram 		return;
257340541d5dSvikram 	}
257440541d5dSvikram 
257540541d5dSvikram 	free(default_lp->arg);
257640541d5dSvikram 	free(default_lp->line);
257740541d5dSvikram 
257840541d5dSvikram 	if (default_entry == NULL) {
257940541d5dSvikram 		default_lp->arg = s_strdup("0");
258040541d5dSvikram 	} else {
258140541d5dSvikram 		(void) snprintf(buf, sizeof (buf), "%d",
258240541d5dSvikram 		    default_entry->entryNum);
258340541d5dSvikram 		default_lp->arg = s_strdup(buf);
258440541d5dSvikram 	}
258540541d5dSvikram 
258640541d5dSvikram 	/*
258740541d5dSvikram 	 * The following is required since only the line field gets
258840541d5dSvikram 	 * written back to menu.lst
258940541d5dSvikram 	 */
259040541d5dSvikram 	(void) snprintf(buf, sizeof (buf), "%s%s%s",
259140541d5dSvikram 	    menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg);
259240541d5dSvikram 	default_lp->line = s_strdup(buf);
259340541d5dSvikram }
259440541d5dSvikram 
259540541d5dSvikram 
25967c478bd9Sstevel@tonic-gate static menu_t *
25977c478bd9Sstevel@tonic-gate menu_read(char *menu_path)
25987c478bd9Sstevel@tonic-gate {
25997c478bd9Sstevel@tonic-gate 	FILE *fp;
26007c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE], *cp;
26017c478bd9Sstevel@tonic-gate 	menu_t *mp;
26027c478bd9Sstevel@tonic-gate 	int line, entry, len, n;
26037c478bd9Sstevel@tonic-gate 
26047c478bd9Sstevel@tonic-gate 	mp = s_calloc(1, sizeof (menu_t));
26057c478bd9Sstevel@tonic-gate 
26067c478bd9Sstevel@tonic-gate 	fp = fopen(menu_path, "r");
26077c478bd9Sstevel@tonic-gate 	if (fp == NULL) { /* Let the caller handle this error */
26087c478bd9Sstevel@tonic-gate 		return (mp);
26097c478bd9Sstevel@tonic-gate 	}
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate 	/* Note: GRUB boot entry number starts with 0 */
26137c478bd9Sstevel@tonic-gate 	line = LINE_INIT;
26147c478bd9Sstevel@tonic-gate 	entry = ENTRY_INIT;
26157c478bd9Sstevel@tonic-gate 	cp = buf;
26167c478bd9Sstevel@tonic-gate 	len = sizeof (buf);
26177c478bd9Sstevel@tonic-gate 	while (s_fgets(cp, len, fp) != NULL) {
26187c478bd9Sstevel@tonic-gate 		n = strlen(cp);
26197c478bd9Sstevel@tonic-gate 		if (cp[n - 1] == '\\') {
26207c478bd9Sstevel@tonic-gate 			len -= n - 1;
26217c478bd9Sstevel@tonic-gate 			assert(len >= 2);
26227c478bd9Sstevel@tonic-gate 			cp += n - 1;
26237c478bd9Sstevel@tonic-gate 			continue;
26247c478bd9Sstevel@tonic-gate 		}
26257c478bd9Sstevel@tonic-gate 		line_parser(mp, buf, &line, &entry);
26267c478bd9Sstevel@tonic-gate 		cp = buf;
26277c478bd9Sstevel@tonic-gate 		len = sizeof (buf);
26287c478bd9Sstevel@tonic-gate 	}
26297c478bd9Sstevel@tonic-gate 
26307c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
26317c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, menu_path, strerror(errno));
26327c478bd9Sstevel@tonic-gate 	}
26337c478bd9Sstevel@tonic-gate 
26347c478bd9Sstevel@tonic-gate 	return (mp);
26357c478bd9Sstevel@tonic-gate }
26367c478bd9Sstevel@tonic-gate 
26377c478bd9Sstevel@tonic-gate static error_t
26387c478bd9Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title)
26397c478bd9Sstevel@tonic-gate {
26407c478bd9Sstevel@tonic-gate 	char *eq;
26417c478bd9Sstevel@tonic-gate 	char *opt_dup;
26427c478bd9Sstevel@tonic-gate 	int entryNum;
26437c478bd9Sstevel@tonic-gate 
26447c478bd9Sstevel@tonic-gate 	assert(mp);
26457c478bd9Sstevel@tonic-gate 	assert(mp->start);
26467c478bd9Sstevel@tonic-gate 	assert(opt);
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate 	opt_dup = s_strdup(opt);
26497c478bd9Sstevel@tonic-gate 
26507c478bd9Sstevel@tonic-gate 	if (entry)
26517c478bd9Sstevel@tonic-gate 		*entry = ENTRY_INIT;
26527c478bd9Sstevel@tonic-gate 	if (title)
26537c478bd9Sstevel@tonic-gate 		*title = NULL;
26547c478bd9Sstevel@tonic-gate 
26557c478bd9Sstevel@tonic-gate 	eq = strchr(opt_dup, '=');
26567c478bd9Sstevel@tonic-gate 	if (eq == NULL) {
26577c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
26587c478bd9Sstevel@tonic-gate 		free(opt_dup);
26597c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
26607c478bd9Sstevel@tonic-gate 	}
26617c478bd9Sstevel@tonic-gate 
26627c478bd9Sstevel@tonic-gate 	*eq = '\0';
26637c478bd9Sstevel@tonic-gate 	if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) {
26647c478bd9Sstevel@tonic-gate 		assert(mp->end);
26657c478bd9Sstevel@tonic-gate 		entryNum = s_strtol(eq + 1);
26667c478bd9Sstevel@tonic-gate 		if (entryNum < 0 || entryNum > mp->end->entryNum) {
26677c478bd9Sstevel@tonic-gate 			bam_error(INVALID_ENTRY, eq + 1);
26687c478bd9Sstevel@tonic-gate 			free(opt_dup);
26697c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
26707c478bd9Sstevel@tonic-gate 		}
26717c478bd9Sstevel@tonic-gate 		*entry = entryNum;
26727c478bd9Sstevel@tonic-gate 	} else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) {
26737c478bd9Sstevel@tonic-gate 		*title = opt + (eq - opt_dup) + 1;
26747c478bd9Sstevel@tonic-gate 	} else {
26757c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
26767c478bd9Sstevel@tonic-gate 		free(opt_dup);
26777c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
26787c478bd9Sstevel@tonic-gate 	}
26797c478bd9Sstevel@tonic-gate 
26807c478bd9Sstevel@tonic-gate 	free(opt_dup);
26817c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
26827c478bd9Sstevel@tonic-gate }
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate /*
26857c478bd9Sstevel@tonic-gate  * If invoked with no titles/entries (opt == NULL)
26867c478bd9Sstevel@tonic-gate  * only title lines in file are printed.
26877c478bd9Sstevel@tonic-gate  *
26887c478bd9Sstevel@tonic-gate  * If invoked with a title or entry #, all
26897c478bd9Sstevel@tonic-gate  * lines in *every* matching entry are listed
26907c478bd9Sstevel@tonic-gate  */
26917c478bd9Sstevel@tonic-gate static error_t
26927c478bd9Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt)
26937c478bd9Sstevel@tonic-gate {
26947c478bd9Sstevel@tonic-gate 	line_t *lp;
26957c478bd9Sstevel@tonic-gate 	int entry = ENTRY_INIT;
26967c478bd9Sstevel@tonic-gate 	int found;
26977c478bd9Sstevel@tonic-gate 	char *title = NULL;
26987c478bd9Sstevel@tonic-gate 
26997c478bd9Sstevel@tonic-gate 	assert(mp);
27007c478bd9Sstevel@tonic-gate 	assert(menu_path);
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
27037c478bd9Sstevel@tonic-gate 		bam_error(NO_MENU, menu_path);
27047c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27057c478bd9Sstevel@tonic-gate 	}
27067c478bd9Sstevel@tonic-gate 
27077c478bd9Sstevel@tonic-gate 	if (opt != NULL) {
27087c478bd9Sstevel@tonic-gate 		if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
27097c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
27107c478bd9Sstevel@tonic-gate 		}
27117c478bd9Sstevel@tonic-gate 		assert((entry != ENTRY_INIT) ^ (title != NULL));
27127c478bd9Sstevel@tonic-gate 	} else {
27137c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0);
27147c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0);
27157c478bd9Sstevel@tonic-gate 	}
27167c478bd9Sstevel@tonic-gate 
27177c478bd9Sstevel@tonic-gate 	found = 0;
27187c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
27197c478bd9Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY)
27207c478bd9Sstevel@tonic-gate 			continue;
27217c478bd9Sstevel@tonic-gate 		if (opt == NULL && lp->flags == BAM_TITLE) {
27227c478bd9Sstevel@tonic-gate 			bam_print(PRINT_TITLE, lp->entryNum,
27237c478bd9Sstevel@tonic-gate 			    lp->arg);
27247c478bd9Sstevel@tonic-gate 			found = 1;
27257c478bd9Sstevel@tonic-gate 			continue;
27267c478bd9Sstevel@tonic-gate 		}
27277c478bd9Sstevel@tonic-gate 		if (entry != ENTRY_INIT && lp->entryNum == entry) {
27287c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
27297c478bd9Sstevel@tonic-gate 			found = 1;
27307c478bd9Sstevel@tonic-gate 			continue;
27317c478bd9Sstevel@tonic-gate 		}
27327c478bd9Sstevel@tonic-gate 
27337c478bd9Sstevel@tonic-gate 		/*
27347c478bd9Sstevel@tonic-gate 		 * We set the entry value here so that all lines
27357c478bd9Sstevel@tonic-gate 		 * in entry get printed. If we subsequently match
27367c478bd9Sstevel@tonic-gate 		 * title in other entries, all lines in those
27377c478bd9Sstevel@tonic-gate 		 * entries get printed as well.
27387c478bd9Sstevel@tonic-gate 		 */
27397c478bd9Sstevel@tonic-gate 		if (title && lp->flags == BAM_TITLE && lp->arg &&
27407c478bd9Sstevel@tonic-gate 		    strncmp(title, lp->arg, strlen(title)) == 0) {
27417c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
27427c478bd9Sstevel@tonic-gate 			entry = lp->entryNum;
27437c478bd9Sstevel@tonic-gate 			found = 1;
27447c478bd9Sstevel@tonic-gate 			continue;
27457c478bd9Sstevel@tonic-gate 		}
27467c478bd9Sstevel@tonic-gate 	}
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 	if (!found) {
27497c478bd9Sstevel@tonic-gate 		bam_error(NO_MATCH_ENTRY);
27507c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27517c478bd9Sstevel@tonic-gate 	}
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
27547c478bd9Sstevel@tonic-gate }
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate static int
27577c478bd9Sstevel@tonic-gate add_boot_entry(menu_t *mp,
27587c478bd9Sstevel@tonic-gate 	char *title,
27597c478bd9Sstevel@tonic-gate 	char *root,
27607c478bd9Sstevel@tonic-gate 	char *kernel,
27617c478bd9Sstevel@tonic-gate 	char *module)
27627c478bd9Sstevel@tonic-gate {
27637c478bd9Sstevel@tonic-gate 	int lineNum, entryNum;
27647c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
2765*ae115bc7Smrj 	menu_cmd_t k_cmd, m_cmd;
27667c478bd9Sstevel@tonic-gate 
27677c478bd9Sstevel@tonic-gate 	assert(mp);
27687c478bd9Sstevel@tonic-gate 
27697c478bd9Sstevel@tonic-gate 	if (title == NULL) {
2770ee3b8144Sszhou 		title = "Solaris";	/* default to Solaris */
27717c478bd9Sstevel@tonic-gate 	}
27727c478bd9Sstevel@tonic-gate 	if (kernel == NULL) {
27737c478bd9Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]);
27747c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27757c478bd9Sstevel@tonic-gate 	}
27767c478bd9Sstevel@tonic-gate 	if (module == NULL) {
2777*ae115bc7Smrj 		if (bam_direct != BAM_DIRECT_DBOOT) {
27787c478bd9Sstevel@tonic-gate 			bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]);
27797c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
27807c478bd9Sstevel@tonic-gate 		}
27817c478bd9Sstevel@tonic-gate 
2782*ae115bc7Smrj 		/* Figure the commands out from the kernel line */
2783*ae115bc7Smrj 		if (strstr(kernel, "$ISADIR") != NULL) {
2784*ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE;
2785*ae115bc7Smrj 			k_cmd = KERNEL_DOLLAR_CMD;
2786*ae115bc7Smrj 			m_cmd = MODULE_DOLLAR_CMD;
2787*ae115bc7Smrj 		} else if (strstr(kernel, "amd64") != NULL) {
2788*ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE_64;
2789*ae115bc7Smrj 			k_cmd = KERNEL_CMD;
2790*ae115bc7Smrj 			m_cmd = MODULE_CMD;
2791*ae115bc7Smrj 		} else {
2792*ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE_32;
2793*ae115bc7Smrj 			k_cmd = KERNEL_CMD;
2794*ae115bc7Smrj 			m_cmd = MODULE_CMD;
2795*ae115bc7Smrj 		}
2796*ae115bc7Smrj 	} else if ((bam_direct == BAM_DIRECT_DBOOT) &&
2797*ae115bc7Smrj 	    (strstr(kernel, "$ISADIR") != NULL)) {
2798*ae115bc7Smrj 		/*
2799*ae115bc7Smrj 		 * If it's a non-failsafe dboot kernel, use the "kernel$"
2800*ae115bc7Smrj 		 * command.  Otherwise, use "kernel".
2801*ae115bc7Smrj 		 */
2802*ae115bc7Smrj 		k_cmd = KERNEL_DOLLAR_CMD;
2803*ae115bc7Smrj 		m_cmd = MODULE_DOLLAR_CMD;
2804*ae115bc7Smrj 	} else {
2805*ae115bc7Smrj 		k_cmd = KERNEL_CMD;
2806*ae115bc7Smrj 		m_cmd = MODULE_CMD;
2807*ae115bc7Smrj 	}
2808*ae115bc7Smrj 
28097c478bd9Sstevel@tonic-gate 	if (mp->start) {
28107c478bd9Sstevel@tonic-gate 		lineNum = mp->end->lineNum;
28117c478bd9Sstevel@tonic-gate 		entryNum = mp->end->entryNum;
28127c478bd9Sstevel@tonic-gate 	} else {
28137c478bd9Sstevel@tonic-gate 		lineNum = LINE_INIT;
28147c478bd9Sstevel@tonic-gate 		entryNum = ENTRY_INIT;
28157c478bd9Sstevel@tonic-gate 	}
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate 	/*
28187c478bd9Sstevel@tonic-gate 	 * No separator for comment (HDR/FTR) commands
28197c478bd9Sstevel@tonic-gate 	 * The syntax for comments is #<comment>
28207c478bd9Sstevel@tonic-gate 	 */
28217c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
2822*ae115bc7Smrj 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_HDR);
28238c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
28247c478bd9Sstevel@tonic-gate 
28257c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
28267c478bd9Sstevel@tonic-gate 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
28278c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
28287c478bd9Sstevel@tonic-gate 
28298c1b6884Sszhou 	if (root) {
28307c478bd9Sstevel@tonic-gate 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
28317c478bd9Sstevel@tonic-gate 		    menu_cmds[ROOT_CMD], menu_cmds[SEP_CMD], root);
28328c1b6884Sszhou 		line_parser(mp, linebuf, &lineNum, &entryNum);
28337c478bd9Sstevel@tonic-gate 	}
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
2836*ae115bc7Smrj 	    menu_cmds[k_cmd], menu_cmds[SEP_CMD], kernel);
28378c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
28387c478bd9Sstevel@tonic-gate 
28397c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
2840*ae115bc7Smrj 	    menu_cmds[m_cmd], menu_cmds[SEP_CMD], module);
28418c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
2844*ae115bc7Smrj 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_FTR);
28458c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 	return (entryNum);
28487c478bd9Sstevel@tonic-gate }
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate static error_t
28517c478bd9Sstevel@tonic-gate do_delete(menu_t *mp, int entryNum)
28527c478bd9Sstevel@tonic-gate {
28538c1b6884Sszhou 	line_t *lp, *freed;
28548c1b6884Sszhou 	entry_t *ent, *tmp;
28557c478bd9Sstevel@tonic-gate 	int deleted;
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate 	assert(entryNum != ENTRY_INIT);
28587c478bd9Sstevel@tonic-gate 
28598c1b6884Sszhou 	ent = mp->entries;
28608c1b6884Sszhou 	while (ent) {
28618c1b6884Sszhou 		lp = ent->start;
28628c1b6884Sszhou 		/* check entry number and make sure it's a bootadm entry */
28638c1b6884Sszhou 		if (lp->flags != BAM_COMMENT ||
2864*ae115bc7Smrj 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0 ||
28658c1b6884Sszhou 		    (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) {
28668c1b6884Sszhou 			ent = ent->next;
28677c478bd9Sstevel@tonic-gate 			continue;
28687c478bd9Sstevel@tonic-gate 		}
28697c478bd9Sstevel@tonic-gate 
28708c1b6884Sszhou 		/* free the entry content */
28718c1b6884Sszhou 		do {
28728c1b6884Sszhou 			freed = lp;
28738c1b6884Sszhou 			lp = lp->next;	/* prev stays the same */
28748c1b6884Sszhou 			unlink_line(mp, freed);
28758c1b6884Sszhou 			line_free(freed);
28768c1b6884Sszhou 		} while (freed != ent->end);
28777c478bd9Sstevel@tonic-gate 
28788c1b6884Sszhou 		/* free the entry_t structure */
28798c1b6884Sszhou 		tmp = ent;
28808c1b6884Sszhou 		ent = ent->next;
28818c1b6884Sszhou 		if (tmp->prev)
28828c1b6884Sszhou 			tmp->prev->next = ent;
28837c478bd9Sstevel@tonic-gate 		else
28848c1b6884Sszhou 			mp->entries = ent;
28858c1b6884Sszhou 		if (ent)
28868c1b6884Sszhou 			ent->prev = tmp->prev;
28877c478bd9Sstevel@tonic-gate 		deleted = 1;
28887c478bd9Sstevel@tonic-gate 	}
28897c478bd9Sstevel@tonic-gate 
28907c478bd9Sstevel@tonic-gate 	if (!deleted && entryNum != ALL_ENTRIES) {
28917c478bd9Sstevel@tonic-gate 		bam_error(NO_BOOTADM_MATCH);
28927c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
28937c478bd9Sstevel@tonic-gate 	}
28947c478bd9Sstevel@tonic-gate 
289540541d5dSvikram 	/*
289640541d5dSvikram 	 * Now that we have deleted an entry, update
289740541d5dSvikram 	 * the entry numbering and the default cmd.
289840541d5dSvikram 	 */
289940541d5dSvikram 	update_numbering(mp);
290040541d5dSvikram 
29017c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
29027c478bd9Sstevel@tonic-gate }
29037c478bd9Sstevel@tonic-gate 
29047c478bd9Sstevel@tonic-gate static error_t
29057c478bd9Sstevel@tonic-gate delete_all_entries(menu_t *mp, char *menu_path, char *opt)
29067c478bd9Sstevel@tonic-gate {
29077c478bd9Sstevel@tonic-gate 	assert(mp);
29087c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
29097c478bd9Sstevel@tonic-gate 
29107c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
29117c478bd9Sstevel@tonic-gate 		bam_print(EMPTY_FILE, menu_path);
29127c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
29137c478bd9Sstevel@tonic-gate 	}
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate 	if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) {
29167c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
29177c478bd9Sstevel@tonic-gate 	}
29187c478bd9Sstevel@tonic-gate 
29197c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
29207c478bd9Sstevel@tonic-gate }
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate static FILE *
29238c1b6884Sszhou open_diskmap(char *root)
29247c478bd9Sstevel@tonic-gate {
29257c478bd9Sstevel@tonic-gate 	FILE *fp;
29267c478bd9Sstevel@tonic-gate 	char cmd[PATH_MAX];
29277c478bd9Sstevel@tonic-gate 
29287c478bd9Sstevel@tonic-gate 	/* make sure we have a map file */
29297c478bd9Sstevel@tonic-gate 	fp = fopen(GRUBDISK_MAP, "r");
29307c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
29317c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd),
29328c1b6884Sszhou 		    "%s%s > /dev/null", root, CREATE_DISKMAP);
29337c478bd9Sstevel@tonic-gate 		(void) system(cmd);
29347c478bd9Sstevel@tonic-gate 		fp = fopen(GRUBDISK_MAP, "r");
29357c478bd9Sstevel@tonic-gate 	}
29367c478bd9Sstevel@tonic-gate 	return (fp);
29377c478bd9Sstevel@tonic-gate }
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate #define	SECTOR_SIZE	512
29407c478bd9Sstevel@tonic-gate 
29417c478bd9Sstevel@tonic-gate static int
29427c478bd9Sstevel@tonic-gate get_partition(char *device)
29437c478bd9Sstevel@tonic-gate {
29447c478bd9Sstevel@tonic-gate 	int i, fd, is_pcfs, partno = -1;
29457c478bd9Sstevel@tonic-gate 	struct mboot *mboot;
29467c478bd9Sstevel@tonic-gate 	char boot_sect[SECTOR_SIZE];
29477c478bd9Sstevel@tonic-gate 	char *wholedisk, *slice;
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate 	/* form whole disk (p0) */
29507c478bd9Sstevel@tonic-gate 	slice = device + strlen(device) - 2;
29517c478bd9Sstevel@tonic-gate 	is_pcfs = (*slice != 's');
29527c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
29537c478bd9Sstevel@tonic-gate 		*slice = '\0';
29547c478bd9Sstevel@tonic-gate 	wholedisk = s_calloc(1, strlen(device) + 3);
29557c478bd9Sstevel@tonic-gate 	(void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device);
29567c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
29577c478bd9Sstevel@tonic-gate 		*slice = 's';
29587c478bd9Sstevel@tonic-gate 
29597c478bd9Sstevel@tonic-gate 	/* read boot sector */
29607c478bd9Sstevel@tonic-gate 	fd = open(wholedisk, O_RDONLY);
29617c478bd9Sstevel@tonic-gate 	free(wholedisk);
29627c478bd9Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
29637c478bd9Sstevel@tonic-gate 		return (partno);
29647c478bd9Sstevel@tonic-gate 	}
29657c478bd9Sstevel@tonic-gate 	(void) close(fd);
29667c478bd9Sstevel@tonic-gate 
29677c478bd9Sstevel@tonic-gate 	/* parse fdisk table */
29687c478bd9Sstevel@tonic-gate 	mboot = (struct mboot *)((void *)boot_sect);
29697c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
29707c478bd9Sstevel@tonic-gate 		struct ipart *part =
29717c478bd9Sstevel@tonic-gate 		    (struct ipart *)(uintptr_t)mboot->parts + i;
29727c478bd9Sstevel@tonic-gate 		if (is_pcfs) {	/* looking for solaris boot part */
29737c478bd9Sstevel@tonic-gate 			if (part->systid == 0xbe) {
29747c478bd9Sstevel@tonic-gate 				partno = i;
29757c478bd9Sstevel@tonic-gate 				break;
29767c478bd9Sstevel@tonic-gate 			}
29777c478bd9Sstevel@tonic-gate 		} else {	/* look for solaris partition, old and new */
29787c478bd9Sstevel@tonic-gate 			if (part->systid == SUNIXOS ||
29797c478bd9Sstevel@tonic-gate 			    part->systid == SUNIXOS2) {
29807c478bd9Sstevel@tonic-gate 				partno = i;
29817c478bd9Sstevel@tonic-gate 				break;
29827c478bd9Sstevel@tonic-gate 			}
29837c478bd9Sstevel@tonic-gate 		}
29847c478bd9Sstevel@tonic-gate 	}
29857c478bd9Sstevel@tonic-gate 	return (partno);
29867c478bd9Sstevel@tonic-gate }
29877c478bd9Sstevel@tonic-gate 
29887c478bd9Sstevel@tonic-gate static char *
29897c478bd9Sstevel@tonic-gate get_grubdisk(char *rootdev, FILE *fp, int on_bootdev)
29907c478bd9Sstevel@tonic-gate {
29917c478bd9Sstevel@tonic-gate 	char *grubdisk;	/* (hd#,#,#) */
29927c478bd9Sstevel@tonic-gate 	char *slice;
29937c478bd9Sstevel@tonic-gate 	char *grubhd;
29947c478bd9Sstevel@tonic-gate 	int fdiskpart;
29957c478bd9Sstevel@tonic-gate 	int found = 0;
29967c478bd9Sstevel@tonic-gate 	char *devname, *ctdname = strstr(rootdev, "dsk/");
29977c478bd9Sstevel@tonic-gate 	char linebuf[PATH_MAX];
29987c478bd9Sstevel@tonic-gate 
29997c478bd9Sstevel@tonic-gate 	if (ctdname == NULL)
30007c478bd9Sstevel@tonic-gate 		return (NULL);
30017c478bd9Sstevel@tonic-gate 
30027c478bd9Sstevel@tonic-gate 	ctdname += strlen("dsk/");
30037c478bd9Sstevel@tonic-gate 	slice = strrchr(ctdname, 's');
30047c478bd9Sstevel@tonic-gate 	if (slice)
30057c478bd9Sstevel@tonic-gate 		*slice = '\0';
30067c478bd9Sstevel@tonic-gate 
30077c478bd9Sstevel@tonic-gate 	rewind(fp);
30087c478bd9Sstevel@tonic-gate 	while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) {
30097c478bd9Sstevel@tonic-gate 		grubhd = strtok(linebuf, " \t\n");
30107c478bd9Sstevel@tonic-gate 		if (grubhd)
30117c478bd9Sstevel@tonic-gate 			devname = strtok(NULL, " \t\n");
30127c478bd9Sstevel@tonic-gate 		else
30137c478bd9Sstevel@tonic-gate 			devname = NULL;
30147c478bd9Sstevel@tonic-gate 		if (devname && strcmp(devname, ctdname) == 0) {
30157c478bd9Sstevel@tonic-gate 			found = 1;
30167c478bd9Sstevel@tonic-gate 			break;
30177c478bd9Sstevel@tonic-gate 		}
30187c478bd9Sstevel@tonic-gate 	}
30197c478bd9Sstevel@tonic-gate 
30207c478bd9Sstevel@tonic-gate 	if (slice)
30217c478bd9Sstevel@tonic-gate 		*slice = 's';
30227c478bd9Sstevel@tonic-gate 
30237c478bd9Sstevel@tonic-gate 	if (found == 0) {
30247c478bd9Sstevel@tonic-gate 		if (bam_verbose)
30257c478bd9Sstevel@tonic-gate 			bam_print(DISKMAP_FAIL_NONFATAL, rootdev);
30267c478bd9Sstevel@tonic-gate 		grubhd = "0";	/* assume disk 0 if can't match */
30277c478bd9Sstevel@tonic-gate 	}
30287c478bd9Sstevel@tonic-gate 
30297c478bd9Sstevel@tonic-gate 	fdiskpart = get_partition(rootdev);
30307c478bd9Sstevel@tonic-gate 	if (fdiskpart == -1)
30317c478bd9Sstevel@tonic-gate 		return (NULL);
30327c478bd9Sstevel@tonic-gate 
30337c478bd9Sstevel@tonic-gate 	grubdisk = s_calloc(1, 10);
30347c478bd9Sstevel@tonic-gate 	if (slice) {
30357c478bd9Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d,%c)",
30367c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart, slice[1] + 'a' - '0');
30377c478bd9Sstevel@tonic-gate 	} else
30387c478bd9Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d)",
30397c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart);
30407c478bd9Sstevel@tonic-gate 
30417c478bd9Sstevel@tonic-gate 	/* if root not on bootdev, change GRUB disk to 0 */
30427c478bd9Sstevel@tonic-gate 	if (!on_bootdev)
30437c478bd9Sstevel@tonic-gate 		grubdisk[3] = '0';
30447c478bd9Sstevel@tonic-gate 	return (grubdisk);
30457c478bd9Sstevel@tonic-gate }
30467c478bd9Sstevel@tonic-gate 
3047ee3b8144Sszhou static char *
3048ee3b8144Sszhou get_title(char *rootdir)
30497c478bd9Sstevel@tonic-gate {
30507c478bd9Sstevel@tonic-gate 	static char title[80];	/* from /etc/release */
30518c1b6884Sszhou 	char *cp = NULL, release[PATH_MAX];
30527c478bd9Sstevel@tonic-gate 	FILE *fp;
30537c478bd9Sstevel@tonic-gate 
30547c478bd9Sstevel@tonic-gate 	/* open the /etc/release file */
30557c478bd9Sstevel@tonic-gate 	(void) snprintf(release, sizeof (release), "%s/etc/release", rootdir);
30567c478bd9Sstevel@tonic-gate 
30577c478bd9Sstevel@tonic-gate 	fp = fopen(release, "r");
30587c478bd9Sstevel@tonic-gate 	if (fp == NULL)
3059ee3b8144Sszhou 		return (NULL);
30607c478bd9Sstevel@tonic-gate 
30617c478bd9Sstevel@tonic-gate 	while (s_fgets(title, sizeof (title), fp) != NULL) {
30627c478bd9Sstevel@tonic-gate 		cp = strstr(title, "Solaris");
30637c478bd9Sstevel@tonic-gate 		if (cp)
30647c478bd9Sstevel@tonic-gate 			break;
30657c478bd9Sstevel@tonic-gate 	}
30667c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
30678c1b6884Sszhou 	return (cp == NULL ? "Solaris" : cp);
30687c478bd9Sstevel@tonic-gate }
30697c478bd9Sstevel@tonic-gate 
3070*ae115bc7Smrj char *
30717c478bd9Sstevel@tonic-gate get_special(char *mountp)
30727c478bd9Sstevel@tonic-gate {
30737c478bd9Sstevel@tonic-gate 	FILE *mntfp;
30747c478bd9Sstevel@tonic-gate 	struct mnttab mp = {0}, mpref = {0};
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 	mntfp = fopen(MNTTAB, "r");
30777c478bd9Sstevel@tonic-gate 	if (mntfp == NULL) {
30787c478bd9Sstevel@tonic-gate 		return (0);
30797c478bd9Sstevel@tonic-gate 	}
30807c478bd9Sstevel@tonic-gate 
30817c478bd9Sstevel@tonic-gate 	if (*mountp == '\0')
30827c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = "/";
30837c478bd9Sstevel@tonic-gate 	else
30847c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = mountp;
30857c478bd9Sstevel@tonic-gate 	if (getmntany(mntfp, &mp, &mpref) != 0) {
30867c478bd9Sstevel@tonic-gate 		(void) fclose(mntfp);
30877c478bd9Sstevel@tonic-gate 		return (NULL);
30887c478bd9Sstevel@tonic-gate 	}
30897c478bd9Sstevel@tonic-gate 	(void) fclose(mntfp);
30907c478bd9Sstevel@tonic-gate 
30917c478bd9Sstevel@tonic-gate 	return (s_strdup(mp.mnt_special));
30927c478bd9Sstevel@tonic-gate }
30937c478bd9Sstevel@tonic-gate 
3094*ae115bc7Smrj char *
30957c478bd9Sstevel@tonic-gate os_to_grubdisk(char *osdisk, int on_bootdev)
30967c478bd9Sstevel@tonic-gate {
30977c478bd9Sstevel@tonic-gate 	FILE *fp;
30987c478bd9Sstevel@tonic-gate 	char *grubdisk;
30997c478bd9Sstevel@tonic-gate 
31007c478bd9Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
31018c1b6884Sszhou 	fp = open_diskmap("");
31027c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
31037c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdisk);
31047c478bd9Sstevel@tonic-gate 		return (NULL);
31057c478bd9Sstevel@tonic-gate 	}
31067c478bd9Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdisk, fp, on_bootdev);
31077c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
31087c478bd9Sstevel@tonic-gate 	return (grubdisk);
31097c478bd9Sstevel@tonic-gate }
31107c478bd9Sstevel@tonic-gate 
31117c478bd9Sstevel@tonic-gate /*
31127c478bd9Sstevel@tonic-gate  * Check if root is on the boot device
31137c478bd9Sstevel@tonic-gate  * Return 0 (false) on error
31147c478bd9Sstevel@tonic-gate  */
31157c478bd9Sstevel@tonic-gate static int
31167c478bd9Sstevel@tonic-gate menu_on_bootdev(char *menu_root, FILE *fp)
31177c478bd9Sstevel@tonic-gate {
31187c478bd9Sstevel@tonic-gate 	int ret;
31197c478bd9Sstevel@tonic-gate 	char *grubhd, *bootp, *special;
31207c478bd9Sstevel@tonic-gate 
31217c478bd9Sstevel@tonic-gate 	special = get_special(menu_root);
31227c478bd9Sstevel@tonic-gate 	if (special == NULL)
31237c478bd9Sstevel@tonic-gate 		return (0);
31247c478bd9Sstevel@tonic-gate 	bootp = strstr(special, "p0:boot");
31257c478bd9Sstevel@tonic-gate 	if (bootp)
31267c478bd9Sstevel@tonic-gate 		*bootp = '\0';
31277c478bd9Sstevel@tonic-gate 	grubhd = get_grubdisk(special, fp, 1);
31287c478bd9Sstevel@tonic-gate 	free(special);
31297c478bd9Sstevel@tonic-gate 
31307c478bd9Sstevel@tonic-gate 	if (grubhd == NULL)
31317c478bd9Sstevel@tonic-gate 		return (0);
31327c478bd9Sstevel@tonic-gate 	ret = grubhd[3] == '0';
31337c478bd9Sstevel@tonic-gate 	free(grubhd);
31347c478bd9Sstevel@tonic-gate 	return (ret);
31357c478bd9Sstevel@tonic-gate }
31367c478bd9Sstevel@tonic-gate 
31378c1b6884Sszhou /*
31388c1b6884Sszhou  * look for matching bootadm entry with specified parameters
31398c1b6884Sszhou  * Here are the rules (based on existing usage):
31408c1b6884Sszhou  * - If title is specified, match on title only
31418c1b6884Sszhou  * - Else, match on grubdisk and module (don't care about kernel line).
31428c1b6884Sszhou  *   note that, if root_opt is non-zero, the absence of root line is
31438c1b6884Sszhou  *   considered a match.
31448c1b6884Sszhou  */
31458c1b6884Sszhou static entry_t *
31468c1b6884Sszhou find_boot_entry(menu_t *mp, char *title, char *root, char *module,
31478c1b6884Sszhou     int root_opt, int *entry_num)
31488c1b6884Sszhou {
31498c1b6884Sszhou 	int i;
31508c1b6884Sszhou 	line_t *lp;
31518c1b6884Sszhou 	entry_t *ent;
31528c1b6884Sszhou 
31538c1b6884Sszhou 	/* find matching entry */
31548c1b6884Sszhou 	for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) {
31558c1b6884Sszhou 		lp = ent->start;
31568c1b6884Sszhou 
31578c1b6884Sszhou 		/* first line of entry must be bootadm comment */
31588c1b6884Sszhou 		lp = ent->start;
3159*ae115bc7Smrj 		if (lp->flags != BAM_COMMENT ||
3160*ae115bc7Smrj 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0) {
31618c1b6884Sszhou 			continue;
31628c1b6884Sszhou 		}
31638c1b6884Sszhou 
31648c1b6884Sszhou 		/* advance to title line */
31658c1b6884Sszhou 		lp = lp->next;
31668c1b6884Sszhou 		if (title) {
31678c1b6884Sszhou 			if (lp->flags == BAM_TITLE && lp->arg &&
31688c1b6884Sszhou 			    strcmp(lp->arg, title) == 0)
31698c1b6884Sszhou 				break;
31708c1b6884Sszhou 			continue;	/* check title only */
31718c1b6884Sszhou 		}
31728c1b6884Sszhou 
31738c1b6884Sszhou 		lp = lp->next;	/* advance to root line */
31748c1b6884Sszhou 		if (lp == NULL || strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
31758c1b6884Sszhou 			/* root command found, match grub disk */
31768c1b6884Sszhou 			if (strcmp(lp->arg, root) != 0) {
31778c1b6884Sszhou 				continue;
31788c1b6884Sszhou 			}
31798c1b6884Sszhou 			lp = lp->next;	/* advance to kernel line */
31808c1b6884Sszhou 		} else {
31818c1b6884Sszhou 			/* no root command, see if root is optional */
31828c1b6884Sszhou 			if (root_opt == 0) {
31838c1b6884Sszhou 				continue;
31848c1b6884Sszhou 			}
31858c1b6884Sszhou 		}
31868c1b6884Sszhou 
31878c1b6884Sszhou 		if (lp == NULL || lp->next == NULL) {
31888c1b6884Sszhou 			continue;
31898c1b6884Sszhou 		}
31908c1b6884Sszhou 
31918c1b6884Sszhou 		/* check for matching module entry (failsafe or normal) */
31928c1b6884Sszhou 		lp = lp->next;	/* advance to module line */
3193*ae115bc7Smrj 		if ((strncmp(lp->cmd, menu_cmds[MODULE_CMD],
3194*ae115bc7Smrj 		    strlen(menu_cmds[MODULE_CMD])) != 0) ||
3195*ae115bc7Smrj 		    (strcmp(lp->arg, module) != 0)) {
31968c1b6884Sszhou 			continue;
31978c1b6884Sszhou 		}
31988c1b6884Sszhou 		break;	/* match found */
31998c1b6884Sszhou 	}
32008c1b6884Sszhou 
32018c1b6884Sszhou 	*entry_num = i;
32028c1b6884Sszhou 	return (ent);
32038c1b6884Sszhou }
32048c1b6884Sszhou 
32058c1b6884Sszhou static int
32068c1b6884Sszhou update_boot_entry(menu_t *mp, char *title, char *root, char *kernel,
32078c1b6884Sszhou     char *module, int root_opt)
32088c1b6884Sszhou {
3209*ae115bc7Smrj 	int i, change_kernel = 0;
32108c1b6884Sszhou 	entry_t *ent;
32118c1b6884Sszhou 	line_t *lp;
32128c1b6884Sszhou 	char linebuf[BAM_MAXLINE];
32138c1b6884Sszhou 
32148c1b6884Sszhou 	/* note: don't match on title, it's updated on upgrade */
32158c1b6884Sszhou 	ent = find_boot_entry(mp, NULL, root, module, root_opt, &i);
3216*ae115bc7Smrj 	if ((ent == NULL) && (bam_direct == BAM_DIRECT_DBOOT)) {
3217*ae115bc7Smrj 		/*
3218*ae115bc7Smrj 		 * We may be upgrading a kernel from multiboot to
3219*ae115bc7Smrj 		 * directboot.  Look for a multiboot entry.
3220*ae115bc7Smrj 		 */
3221*ae115bc7Smrj 		ent = find_boot_entry(mp, NULL, root, MULTI_BOOT_ARCHIVE,
3222*ae115bc7Smrj 		    root_opt, &i);
3223*ae115bc7Smrj 		if (ent != NULL) {
3224*ae115bc7Smrj 			change_kernel = 1;
3225*ae115bc7Smrj 		}
3226*ae115bc7Smrj 	}
32278c1b6884Sszhou 	if (ent == NULL)
32288c1b6884Sszhou 		return (add_boot_entry(mp, title, root_opt ? NULL : root,
32298c1b6884Sszhou 		    kernel, module));
32308c1b6884Sszhou 
32318c1b6884Sszhou 	/* replace title of exiting entry and delete root line */
32328c1b6884Sszhou 	lp = ent->start;
32338c1b6884Sszhou 	lp = lp->next;	/* title line */
32348c1b6884Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
32358c1b6884Sszhou 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
32368c1b6884Sszhou 	free(lp->arg);
32378c1b6884Sszhou 	free(lp->line);
32388c1b6884Sszhou 	lp->arg = s_strdup(title);
32398c1b6884Sszhou 	lp->line = s_strdup(linebuf);
32408c1b6884Sszhou 
32418c1b6884Sszhou 	lp = lp->next;	/* root line */
32428c1b6884Sszhou 	if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
32438c1b6884Sszhou 		if (root_opt) {		/* root line not needed */
32448c1b6884Sszhou 			line_t *tmp = lp;
32458c1b6884Sszhou 			lp = lp->next;
32468c1b6884Sszhou 			unlink_line(mp, tmp);
32478c1b6884Sszhou 			line_free(tmp);
32488c1b6884Sszhou 		} else
32498c1b6884Sszhou 			lp = lp->next;
32508c1b6884Sszhou 	}
3251*ae115bc7Smrj 
3252*ae115bc7Smrj 	if (change_kernel) {
3253*ae115bc7Smrj 		/*
3254*ae115bc7Smrj 		 * We're upgrading from multiboot to directboot.
3255*ae115bc7Smrj 		 */
3256*ae115bc7Smrj 		if (strcmp(lp->cmd, menu_cmds[KERNEL_CMD]) == 0) {
3257*ae115bc7Smrj 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
3258*ae115bc7Smrj 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
3259*ae115bc7Smrj 			    kernel);
3260*ae115bc7Smrj 			free(lp->arg);
3261*ae115bc7Smrj 			free(lp->line);
3262*ae115bc7Smrj 			lp->arg = s_strdup(kernel);
3263*ae115bc7Smrj 			lp->line = s_strdup(linebuf);
3264*ae115bc7Smrj 			lp = lp->next;
3265*ae115bc7Smrj 		}
3266*ae115bc7Smrj 		if (strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) {
3267*ae115bc7Smrj 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
3268*ae115bc7Smrj 			    menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD],
3269*ae115bc7Smrj 			    module);
3270*ae115bc7Smrj 			free(lp->arg);
3271*ae115bc7Smrj 			free(lp->line);
3272*ae115bc7Smrj 			lp->arg = s_strdup(module);
3273*ae115bc7Smrj 			lp->line = s_strdup(linebuf);
3274*ae115bc7Smrj 			lp = lp->next;
3275*ae115bc7Smrj 		}
3276*ae115bc7Smrj 	}
32778c1b6884Sszhou 	return (i);
32788c1b6884Sszhou }
32798c1b6884Sszhou 
32807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
32817c478bd9Sstevel@tonic-gate static error_t
32827c478bd9Sstevel@tonic-gate update_entry(menu_t *mp, char *menu_root, char *opt)
32837c478bd9Sstevel@tonic-gate {
32847c478bd9Sstevel@tonic-gate 	FILE *fp;
32857c478bd9Sstevel@tonic-gate 	int entry;
32867c478bd9Sstevel@tonic-gate 	char *grubdisk, *title, *osdev, *osroot;
32878c1b6884Sszhou 	struct stat sbuf;
32888c1b6884Sszhou 	char failsafe[256];
32897c478bd9Sstevel@tonic-gate 
32907c478bd9Sstevel@tonic-gate 	assert(mp);
32917c478bd9Sstevel@tonic-gate 	assert(opt);
32927c478bd9Sstevel@tonic-gate 
32937c478bd9Sstevel@tonic-gate 	osdev = strtok(opt, ",");
32947c478bd9Sstevel@tonic-gate 	osroot = strtok(NULL, ",");
32957c478bd9Sstevel@tonic-gate 	if (osroot == NULL)
32967c478bd9Sstevel@tonic-gate 		osroot = menu_root;
32977c478bd9Sstevel@tonic-gate 	title = get_title(osroot);
32987c478bd9Sstevel@tonic-gate 
32997c478bd9Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
33008c1b6884Sszhou 	fp = open_diskmap(osroot);
33017c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
33027c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
33037c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
33047c478bd9Sstevel@tonic-gate 	}
33057c478bd9Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdev, fp, menu_on_bootdev(menu_root, fp));
33067c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
33077c478bd9Sstevel@tonic-gate 	if (grubdisk == NULL) {
33087c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
33097c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
33107c478bd9Sstevel@tonic-gate 	}
33117c478bd9Sstevel@tonic-gate 
33127c478bd9Sstevel@tonic-gate 	/* add the entry for normal Solaris */
3313*ae115bc7Smrj 	if (bam_direct == BAM_DIRECT_DBOOT) {
33148c1b6884Sszhou 		entry = update_boot_entry(mp, title, grubdisk,
3315*ae115bc7Smrj 		    DIRECT_BOOT_KERNEL, DIRECT_BOOT_ARCHIVE,
33168c1b6884Sszhou 		    osroot == menu_root);
3317*ae115bc7Smrj 	} else {
3318*ae115bc7Smrj 		entry = update_boot_entry(mp, title, grubdisk,
3319*ae115bc7Smrj 		    MULTI_BOOT, MULTI_BOOT_ARCHIVE,
3320*ae115bc7Smrj 		    osroot == menu_root);
3321*ae115bc7Smrj 	}
33227c478bd9Sstevel@tonic-gate 
33237c478bd9Sstevel@tonic-gate 	/* add the entry for failsafe archive */
3324*ae115bc7Smrj 	(void) snprintf(failsafe, sizeof (failsafe), "%s%s", osroot, MINIROOT);
3325*ae115bc7Smrj 	if (stat(failsafe, &sbuf) == 0) {
3326*ae115bc7Smrj 		(void) update_boot_entry(mp, FAILSAFE_TITLE, grubdisk,
3327*ae115bc7Smrj 		    (bam_direct == BAM_DIRECT_DBOOT) ?
3328*ae115bc7Smrj 		    DIRECT_BOOT_FAILSAFE_LINE : MULTI_BOOT_FAILSAFE_LINE,
3329*ae115bc7Smrj 		    MINIROOT, osroot == menu_root);
3330*ae115bc7Smrj 	}
33317c478bd9Sstevel@tonic-gate 	free(grubdisk);
33327c478bd9Sstevel@tonic-gate 
33337c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
33347c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
33357c478bd9Sstevel@tonic-gate 	}
33367c478bd9Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
33377c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
33387c478bd9Sstevel@tonic-gate }
33397c478bd9Sstevel@tonic-gate 
3340b610f78eSvikram static char *
3341b610f78eSvikram read_grub_root(void)
3342b610f78eSvikram {
3343b610f78eSvikram 	FILE *fp;
3344b610f78eSvikram 	struct stat sb;
3345b610f78eSvikram 	char buf[BAM_MAXLINE];
3346b610f78eSvikram 	char *rootstr;
3347b610f78eSvikram 
3348b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
3349b610f78eSvikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
3350b610f78eSvikram 		return (NULL);
3351b610f78eSvikram 	}
3352b610f78eSvikram 
3353b610f78eSvikram 	if (stat(GRUB_root, &sb) != 0) {
3354b610f78eSvikram 		bam_error(MISSING_ROOT_FILE, GRUB_root, strerror(errno));
3355b610f78eSvikram 		return (NULL);
3356b610f78eSvikram 	}
3357b610f78eSvikram 
3358b610f78eSvikram 	fp = fopen(GRUB_root, "r");
3359b610f78eSvikram 	if (fp == NULL) {
3360b610f78eSvikram 		bam_error(OPEN_FAIL, GRUB_root, strerror(errno));
3361b610f78eSvikram 		return (NULL);
3362b610f78eSvikram 	}
3363b610f78eSvikram 
3364b610f78eSvikram 	if (s_fgets(buf, sizeof (buf), fp) == NULL) {
3365b610f78eSvikram 		bam_error(EMPTY_FILE, GRUB_root, strerror(errno));
3366b610f78eSvikram 		(void) fclose(fp);
3367b610f78eSvikram 		return (NULL);
3368b610f78eSvikram 	}
3369b610f78eSvikram 
3370b610f78eSvikram 	/*
3371b610f78eSvikram 	 * Copy buf here as check below may trash the buffer
3372b610f78eSvikram 	 */
3373b610f78eSvikram 	rootstr = s_strdup(buf);
3374b610f78eSvikram 
3375b610f78eSvikram 	if (s_fgets(buf, sizeof (buf), fp) != NULL) {
3376b610f78eSvikram 		bam_error(BAD_ROOT_FILE, GRUB_root);
3377b610f78eSvikram 		free(rootstr);
3378b610f78eSvikram 		rootstr = NULL;
3379b610f78eSvikram 	}
3380b610f78eSvikram 
3381b610f78eSvikram 	(void) fclose(fp);
3382b610f78eSvikram 
3383b610f78eSvikram 	return (rootstr);
3384b610f78eSvikram }
3385b610f78eSvikram 
33868c1b6884Sszhou static void
3387*ae115bc7Smrj save_default_entry(menu_t *mp, const char *which)
33888c1b6884Sszhou {
33898c1b6884Sszhou 	int lineNum, entryNum;
33908c1b6884Sszhou 	int entry = 0;	/* default is 0 */
33918c1b6884Sszhou 	char linebuf[BAM_MAXLINE];
33928c1b6884Sszhou 	line_t *lp = mp->curdefault;
33938c1b6884Sszhou 
3394bff269d0Svikram 	if (mp->start) {
3395bff269d0Svikram 		lineNum = mp->end->lineNum;
3396bff269d0Svikram 		entryNum = mp->end->entryNum;
3397bff269d0Svikram 	} else {
3398bff269d0Svikram 		lineNum = LINE_INIT;
3399bff269d0Svikram 		entryNum = ENTRY_INIT;
3400bff269d0Svikram 	}
3401bff269d0Svikram 
34028c1b6884Sszhou 	if (lp)
34038c1b6884Sszhou 		entry = s_strtol(lp->arg);
34048c1b6884Sszhou 
3405*ae115bc7Smrj 	(void) snprintf(linebuf, sizeof (linebuf), "#%s%d", which, entry);
34068c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
34078c1b6884Sszhou }
34088c1b6884Sszhou 
34098c1b6884Sszhou static void
3410*ae115bc7Smrj restore_default_entry(menu_t *mp, const char *which, line_t *lp)
34118c1b6884Sszhou {
34128c1b6884Sszhou 	int entry;
34138c1b6884Sszhou 	char *str;
34148c1b6884Sszhou 
34158c1b6884Sszhou 	if (lp == NULL)
34168c1b6884Sszhou 		return;		/* nothing to restore */
34178c1b6884Sszhou 
3418*ae115bc7Smrj 	str = lp->arg + strlen(which);
34198c1b6884Sszhou 	entry = s_strtol(str);
34208c1b6884Sszhou 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
34218c1b6884Sszhou 
34228c1b6884Sszhou 	/* delete saved old default line */
34238c1b6884Sszhou 	unlink_line(mp, lp);
34248c1b6884Sszhou 	line_free(lp);
34258c1b6884Sszhou }
34268c1b6884Sszhou 
34277c478bd9Sstevel@tonic-gate /*
34287c478bd9Sstevel@tonic-gate  * This function is for supporting reboot with args.
34297c478bd9Sstevel@tonic-gate  * The opt value can be:
34307c478bd9Sstevel@tonic-gate  * NULL		delete temp entry, if present
34317c478bd9Sstevel@tonic-gate  * entry=#	switches default entry to 1
34327c478bd9Sstevel@tonic-gate  * else		treated as boot-args and setup a temperary menu entry
34337c478bd9Sstevel@tonic-gate  *		and make it the default
34347c478bd9Sstevel@tonic-gate  */
34357c478bd9Sstevel@tonic-gate #define	REBOOT_TITLE	"Solaris_reboot_transient"
34367c478bd9Sstevel@tonic-gate 
34378c1b6884Sszhou /*ARGSUSED*/
34387c478bd9Sstevel@tonic-gate static error_t
34397c478bd9Sstevel@tonic-gate update_temp(menu_t *mp, char *menupath, char *opt)
34407c478bd9Sstevel@tonic-gate {
34417c478bd9Sstevel@tonic-gate 	int entry;
3442*ae115bc7Smrj 	char *grubdisk, *rootdev, *path;
3443*ae115bc7Smrj 	char kernbuf[BUFSIZ];
3444*ae115bc7Smrj 	char args_buf[BUFSIZ];
3445b610f78eSvikram 	struct stat sb;
34467c478bd9Sstevel@tonic-gate 
34477c478bd9Sstevel@tonic-gate 	assert(mp);
34487c478bd9Sstevel@tonic-gate 
34498c1b6884Sszhou 	/* If no option, delete exiting reboot menu entry */
34508c1b6884Sszhou 	if (opt == NULL) {
34518c1b6884Sszhou 		entry_t *ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL,
34528c1b6884Sszhou 		    0, &entry);
34538c1b6884Sszhou 		if (ent == NULL)	/* not found is ok */
34548c1b6884Sszhou 			return (BAM_SUCCESS);
34558c1b6884Sszhou 		(void) do_delete(mp, entry);
3456*ae115bc7Smrj 		restore_default_entry(mp, BAM_OLDDEF, mp->olddefault);
3457*ae115bc7Smrj 		mp->olddefault = NULL;
34588c1b6884Sszhou 		return (BAM_WRITE);
34598c1b6884Sszhou 	}
34608c1b6884Sszhou 
34618c1b6884Sszhou 	/* if entry= is specified, set the default entry */
34628c1b6884Sszhou 	if (strncmp(opt, "entry=", strlen("entry=")) == 0 &&
34637c478bd9Sstevel@tonic-gate 	    selector(mp, opt, &entry, NULL) == BAM_SUCCESS) {
34647c478bd9Sstevel@tonic-gate 		/* this is entry=# option */
34657c478bd9Sstevel@tonic-gate 		return (set_global(mp, menu_cmds[DEFAULT_CMD], entry));
34667c478bd9Sstevel@tonic-gate 	}
34677c478bd9Sstevel@tonic-gate 
34687c478bd9Sstevel@tonic-gate 	/*
34697c478bd9Sstevel@tonic-gate 	 * add a new menu entry base on opt and make it the default
3470b610f78eSvikram 	 */
3471b610f78eSvikram 	grubdisk = NULL;
3472b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
3473b610f78eSvikram 		/*
34747c478bd9Sstevel@tonic-gate 		 * 1. First get root disk name from mnttab
34757c478bd9Sstevel@tonic-gate 		 * 2. Translate disk name to grub name
34767c478bd9Sstevel@tonic-gate 		 * 3. Add the new menu entry
34777c478bd9Sstevel@tonic-gate 		 */
34787c478bd9Sstevel@tonic-gate 		rootdev = get_special("/");
34797c478bd9Sstevel@tonic-gate 		if (rootdev) {
34807c478bd9Sstevel@tonic-gate 			grubdisk = os_to_grubdisk(rootdev, 1);
34817c478bd9Sstevel@tonic-gate 			free(rootdev);
34827c478bd9Sstevel@tonic-gate 		}
3483b610f78eSvikram 	} else {
3484b610f78eSvikram 		/*
3485b610f78eSvikram 		 * This is an LU BE. The GRUB_root file
3486b610f78eSvikram 		 * contains entry for GRUB's "root" cmd.
3487b610f78eSvikram 		 */
3488b610f78eSvikram 		grubdisk = read_grub_root();
3489b610f78eSvikram 	}
34907c478bd9Sstevel@tonic-gate 	if (grubdisk == NULL) {
3491b610f78eSvikram 		bam_error(REBOOT_WITH_ARGS_FAILED);
34927c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
34937c478bd9Sstevel@tonic-gate 	}
34947c478bd9Sstevel@tonic-gate 
34957c478bd9Sstevel@tonic-gate 	/* add an entry for Solaris reboot */
3496*ae115bc7Smrj 	if (bam_direct == BAM_DIRECT_DBOOT) {
3497*ae115bc7Smrj 		if (opt[0] == '-') {
3498*ae115bc7Smrj 			/* It's an option - first see if boot-file is set */
3499*ae115bc7Smrj 			if (set_kernel(mp, KERNEL_CMD, NULL, kernbuf, BUFSIZ)
3500*ae115bc7Smrj 			    != BAM_SUCCESS)
3501*ae115bc7Smrj 				return (BAM_ERROR);
3502*ae115bc7Smrj 			if (kernbuf[0] == '\0')
3503*ae115bc7Smrj 				(void) strncpy(kernbuf, DIRECT_BOOT_KERNEL,
3504*ae115bc7Smrj 				    BUFSIZ);
3505*ae115bc7Smrj 			(void) strlcat(kernbuf, " ", BUFSIZ);
3506*ae115bc7Smrj 			(void) strlcat(kernbuf, opt, BUFSIZ);
3507*ae115bc7Smrj 		} else if (opt[0] == '/') {
3508*ae115bc7Smrj 			/* It's a full path - write it out and go home */
3509*ae115bc7Smrj 			(void) strlcpy(kernbuf, opt, BUFSIZ);
3510*ae115bc7Smrj 		} else {
3511*ae115bc7Smrj 			path = expand_path(opt);
3512*ae115bc7Smrj 			if (path != NULL) {
3513*ae115bc7Smrj 				(void) strlcpy(kernbuf, path, BUFSIZ);
3514*ae115bc7Smrj 				free(path);
3515*ae115bc7Smrj 				if (strcmp(opt, "kmdb") == 0) {
3516*ae115bc7Smrj 					if (set_kernel(mp, ARGS_CMD, NULL,
3517*ae115bc7Smrj 					    args_buf, BUFSIZ) != BAM_SUCCESS)
3518*ae115bc7Smrj 						return (BAM_ERROR);
3519*ae115bc7Smrj 
3520*ae115bc7Smrj 					if (args_buf[0] != '\0') {
3521*ae115bc7Smrj 						(void) strlcat(kernbuf, " ",
3522*ae115bc7Smrj 						    BUFSIZ);
3523*ae115bc7Smrj 						(void) strlcat(kernbuf,
3524*ae115bc7Smrj 						    args_buf, BUFSIZ);
3525*ae115bc7Smrj 					}
3526*ae115bc7Smrj 				}
3527*ae115bc7Smrj 			}
3528*ae115bc7Smrj 		}
35297c478bd9Sstevel@tonic-gate 		entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf,
3530*ae115bc7Smrj 		    NULL);
3531*ae115bc7Smrj 	} else {
3532*ae115bc7Smrj 		(void) snprintf(kernbuf, sizeof (kernbuf), "%s %s",
3533*ae115bc7Smrj 		    MULTI_BOOT, opt);
3534*ae115bc7Smrj 		entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf,
3535*ae115bc7Smrj 		    MULTI_BOOT_ARCHIVE);
3536*ae115bc7Smrj 	}
35377c478bd9Sstevel@tonic-gate 	free(grubdisk);
35387c478bd9Sstevel@tonic-gate 
35397c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
3540b610f78eSvikram 		bam_error(REBOOT_WITH_ARGS_FAILED);
35417c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
35427c478bd9Sstevel@tonic-gate 	}
35438c1b6884Sszhou 
3544*ae115bc7Smrj 	save_default_entry(mp, BAM_OLDDEF);
35457c478bd9Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
35467c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
35477c478bd9Sstevel@tonic-gate }
35487c478bd9Sstevel@tonic-gate 
35497c478bd9Sstevel@tonic-gate static error_t
35507c478bd9Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val)
35517c478bd9Sstevel@tonic-gate {
35527c478bd9Sstevel@tonic-gate 	line_t *lp, *found, *last;
35537c478bd9Sstevel@tonic-gate 	char *cp, *str;
35547c478bd9Sstevel@tonic-gate 	char prefix[BAM_MAXLINE];
35557c478bd9Sstevel@tonic-gate 	size_t len;
35567c478bd9Sstevel@tonic-gate 
35577c478bd9Sstevel@tonic-gate 	assert(mp);
35587c478bd9Sstevel@tonic-gate 	assert(globalcmd);
35597c478bd9Sstevel@tonic-gate 
3560b610f78eSvikram 	if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) {
3561b610f78eSvikram 		if (val < 0 || mp->end == NULL || val > mp->end->entryNum) {
3562b610f78eSvikram 			(void) snprintf(prefix, sizeof (prefix), "%d", val);
3563b610f78eSvikram 			bam_error(INVALID_ENTRY, prefix);
3564b610f78eSvikram 			return (BAM_ERROR);
3565b610f78eSvikram 		}
3566b610f78eSvikram 	}
3567b610f78eSvikram 
35687c478bd9Sstevel@tonic-gate 	found = last = NULL;
35697c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
35707c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
35717c478bd9Sstevel@tonic-gate 			continue;
35727c478bd9Sstevel@tonic-gate 
35737c478bd9Sstevel@tonic-gate 		last = lp; /* track the last global found */
35747c478bd9Sstevel@tonic-gate 
35757c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
35767c478bd9Sstevel@tonic-gate 			bam_error(NO_CMD, lp->lineNum);
35777c478bd9Sstevel@tonic-gate 			continue;
35787c478bd9Sstevel@tonic-gate 		}
35797c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
35807c478bd9Sstevel@tonic-gate 			continue;
35817c478bd9Sstevel@tonic-gate 
35827c478bd9Sstevel@tonic-gate 		if (found) {
35837c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
35847c478bd9Sstevel@tonic-gate 		}
35857c478bd9Sstevel@tonic-gate 		found = lp;
35867c478bd9Sstevel@tonic-gate 	}
35877c478bd9Sstevel@tonic-gate 
35887c478bd9Sstevel@tonic-gate 	if (found == NULL) {
35897c478bd9Sstevel@tonic-gate 		lp = s_calloc(1, sizeof (line_t));
35907c478bd9Sstevel@tonic-gate 		if (last == NULL) {
35917c478bd9Sstevel@tonic-gate 			lp->next = mp->start;
35927c478bd9Sstevel@tonic-gate 			mp->start = lp;
35937c478bd9Sstevel@tonic-gate 			mp->end = (mp->end) ? mp->end : lp;
35947c478bd9Sstevel@tonic-gate 		} else {
35957c478bd9Sstevel@tonic-gate 			lp->next = last->next;
35967c478bd9Sstevel@tonic-gate 			last->next = lp;
35977c478bd9Sstevel@tonic-gate 			if (lp->next == NULL)
35987c478bd9Sstevel@tonic-gate 				mp->end = lp;
35997c478bd9Sstevel@tonic-gate 		}
36007c478bd9Sstevel@tonic-gate 		lp->flags = BAM_GLOBAL; /* other fields not needed for writes */
36017c478bd9Sstevel@tonic-gate 		len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]);
36027c478bd9Sstevel@tonic-gate 		len += 10;	/* val < 10 digits */
36037c478bd9Sstevel@tonic-gate 		lp->line = s_calloc(1, len);
36047c478bd9Sstevel@tonic-gate 		(void) snprintf(lp->line, len, "%s%s%d",
36057c478bd9Sstevel@tonic-gate 		    globalcmd, menu_cmds[SEP_CMD], val);
36067c478bd9Sstevel@tonic-gate 		return (BAM_WRITE);
36077c478bd9Sstevel@tonic-gate 	}
36087c478bd9Sstevel@tonic-gate 
36097c478bd9Sstevel@tonic-gate 	/*
36107c478bd9Sstevel@tonic-gate 	 * We are changing an existing entry. Retain any prefix whitespace,
36117c478bd9Sstevel@tonic-gate 	 * but overwrite everything else. This preserves tabs added for
36127c478bd9Sstevel@tonic-gate 	 * readability.
36137c478bd9Sstevel@tonic-gate 	 */
36147c478bd9Sstevel@tonic-gate 	str = found->line;
36157c478bd9Sstevel@tonic-gate 	cp = prefix;
36167c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
36177c478bd9Sstevel@tonic-gate 		*(cp++) = *(str++);
36187c478bd9Sstevel@tonic-gate 	*cp = '\0'; /* Terminate prefix */
36197c478bd9Sstevel@tonic-gate 	len = strlen(prefix) + strlen(globalcmd);
36207c478bd9Sstevel@tonic-gate 	len += strlen(menu_cmds[SEP_CMD]) + 10;
36217c478bd9Sstevel@tonic-gate 
36227c478bd9Sstevel@tonic-gate 	free(found->line);
36237c478bd9Sstevel@tonic-gate 	found->line = s_calloc(1, len);
36247c478bd9Sstevel@tonic-gate 	(void) snprintf(found->line, len,
36257c478bd9Sstevel@tonic-gate 		"%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val);
36267c478bd9Sstevel@tonic-gate 
36277c478bd9Sstevel@tonic-gate 	return (BAM_WRITE); /* need a write to menu */
36287c478bd9Sstevel@tonic-gate }
36297c478bd9Sstevel@tonic-gate 
3630*ae115bc7Smrj /*
3631*ae115bc7Smrj  * partial_path may be anything like "kernel/unix" or "kmdb".  Try to
3632*ae115bc7Smrj  * expand it to a full unix path.
3633*ae115bc7Smrj  */
3634*ae115bc7Smrj static char *
3635*ae115bc7Smrj expand_path(const char *partial_path)
3636*ae115bc7Smrj {
3637*ae115bc7Smrj 	int new_path_len;
3638*ae115bc7Smrj 	char *new_path, new_path2[PATH_MAX];
3639*ae115bc7Smrj 	struct stat sb;
3640*ae115bc7Smrj 
3641*ae115bc7Smrj 	new_path_len = strlen(partial_path) + 64;
3642*ae115bc7Smrj 	new_path = s_calloc(1, new_path_len);
3643*ae115bc7Smrj 
3644*ae115bc7Smrj 	/* First, try the simplest case - something like "kernel/unix" */
3645*ae115bc7Smrj 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s",
3646*ae115bc7Smrj 	    partial_path);
3647*ae115bc7Smrj 	if (stat(new_path, &sb) == 0) {
3648*ae115bc7Smrj 		return (new_path);
3649*ae115bc7Smrj 	}
3650*ae115bc7Smrj 
3651*ae115bc7Smrj 	if (strcmp(partial_path, "kmdb") == 0) {
3652*ae115bc7Smrj 		(void) snprintf(new_path, new_path_len, "%s -k",
3653*ae115bc7Smrj 		    DIRECT_BOOT_KERNEL);
3654*ae115bc7Smrj 		return (new_path);
3655*ae115bc7Smrj 	}
3656*ae115bc7Smrj 
3657*ae115bc7Smrj 	/*
3658*ae115bc7Smrj 	 * We've quickly reached unsupported usage.  Try once more to
3659*ae115bc7Smrj 	 * see if we were just given a glom name.
3660*ae115bc7Smrj 	 */
3661*ae115bc7Smrj 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s/unix",
3662*ae115bc7Smrj 	    partial_path);
3663*ae115bc7Smrj 	(void) snprintf(new_path2, PATH_MAX, "/platform/i86pc/%s/amd64/unix",
3664*ae115bc7Smrj 	    partial_path);
3665*ae115bc7Smrj 	if (stat(new_path, &sb) == 0) {
3666*ae115bc7Smrj 		if (stat(new_path2, &sb) == 0) {
3667*ae115bc7Smrj 			/*
3668*ae115bc7Smrj 			 * We matched both, so we actually
3669*ae115bc7Smrj 			 * want to write the $ISADIR version.
3670*ae115bc7Smrj 			 */
3671*ae115bc7Smrj 			(void) snprintf(new_path, new_path_len,
3672*ae115bc7Smrj 			    "/platform/i86pc/kernel/%s/$ISADIR/unix",
3673*ae115bc7Smrj 			    partial_path);
3674*ae115bc7Smrj 		}
3675*ae115bc7Smrj 		return (new_path);
3676*ae115bc7Smrj 	}
3677*ae115bc7Smrj 
3678*ae115bc7Smrj 	bam_error(UNKNOWN_KERNEL, partial_path);
3679*ae115bc7Smrj 	free(new_path);
3680*ae115bc7Smrj 	return (NULL);
3681*ae115bc7Smrj }
3682*ae115bc7Smrj 
3683*ae115bc7Smrj /*
3684*ae115bc7Smrj  * The kernel cmd and arg have been changed, so
3685*ae115bc7Smrj  * check whether the archive line needs to change.
3686*ae115bc7Smrj  */
3687*ae115bc7Smrj static void
3688*ae115bc7Smrj set_archive_line(entry_t *entryp, line_t *kernelp)
3689*ae115bc7Smrj {
3690*ae115bc7Smrj 	line_t *lp = entryp->start;
3691*ae115bc7Smrj 	char *new_archive;
3692*ae115bc7Smrj 	menu_cmd_t m_cmd;
3693*ae115bc7Smrj 
3694*ae115bc7Smrj 	for (; lp != NULL; lp = lp->next) {
3695*ae115bc7Smrj 		if (strncmp(lp->cmd, menu_cmds[MODULE_CMD],
3696*ae115bc7Smrj 		    sizeof (menu_cmds[MODULE_CMD]) - 1) == 0) {
3697*ae115bc7Smrj 			break;
3698*ae115bc7Smrj 		}
3699*ae115bc7Smrj 		if (lp == entryp->end)
3700*ae115bc7Smrj 			return;
3701*ae115bc7Smrj 	}
3702*ae115bc7Smrj 	if (lp == NULL)
3703*ae115bc7Smrj 		return;
3704*ae115bc7Smrj 
3705*ae115bc7Smrj 	if (strstr(kernelp->arg, "$ISADIR") != NULL) {
3706*ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE;
3707*ae115bc7Smrj 		m_cmd = MODULE_DOLLAR_CMD;
3708*ae115bc7Smrj 	} else if (strstr(kernelp->arg, "amd64") != NULL) {
3709*ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE_64;
3710*ae115bc7Smrj 		m_cmd = MODULE_CMD;
3711*ae115bc7Smrj 	} else {
3712*ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE_32;
3713*ae115bc7Smrj 		m_cmd = MODULE_CMD;
3714*ae115bc7Smrj 	}
3715*ae115bc7Smrj 
3716*ae115bc7Smrj 	if (strcmp(lp->arg, new_archive) == 0)
3717*ae115bc7Smrj 		return;
3718*ae115bc7Smrj 
3719*ae115bc7Smrj 	if (strcmp(lp->cmd, menu_cmds[m_cmd]) != 0) {
3720*ae115bc7Smrj 		free(lp->cmd);
3721*ae115bc7Smrj 		lp->cmd = s_strdup(menu_cmds[m_cmd]);
3722*ae115bc7Smrj 	}
3723*ae115bc7Smrj 
3724*ae115bc7Smrj 	free(lp->arg);
3725*ae115bc7Smrj 	lp->arg = s_strdup(new_archive);
3726*ae115bc7Smrj 	update_line(lp);
3727*ae115bc7Smrj }
3728*ae115bc7Smrj 
3729*ae115bc7Smrj /*
3730*ae115bc7Smrj  * Title for an entry to set properties that once went in bootenv.rc.
3731*ae115bc7Smrj  */
3732*ae115bc7Smrj #define	BOOTENV_RC_TITLE	"Solaris bootenv rc"
3733*ae115bc7Smrj 
3734*ae115bc7Smrj /*
3735*ae115bc7Smrj  * If path is NULL, return the kernel (optnum == KERNEL_CMD) or arguments
3736*ae115bc7Smrj  * (optnum == ARGS_CMD) in the argument buf.  If path is a zero-length
3737*ae115bc7Smrj  * string, reset the value to the default.  If path is a non-zero-length
3738*ae115bc7Smrj  * string, set the kernel or arguments.
3739*ae115bc7Smrj  */
3740*ae115bc7Smrj static error_t
3741*ae115bc7Smrj set_kernel(menu_t *mp, menu_cmd_t optnum, char *path, char *buf, size_t bufsize)
3742*ae115bc7Smrj {
3743*ae115bc7Smrj 	int entryNum, rv = BAM_SUCCESS, free_new_path = 0;
3744*ae115bc7Smrj 	entry_t *entryp;
3745*ae115bc7Smrj 	line_t *ptr, *kernelp;
3746*ae115bc7Smrj 	char *new_arg, *old_args, *space;
3747*ae115bc7Smrj 	char *grubdisk, *rootdev, *new_path;
3748*ae115bc7Smrj 	char old_space;
3749*ae115bc7Smrj 	size_t old_kernel_len, new_str_len;
3750*ae115bc7Smrj 	struct stat sb;
3751*ae115bc7Smrj 
3752*ae115bc7Smrj 	assert(bufsize > 0);
3753*ae115bc7Smrj 
3754*ae115bc7Smrj 	ptr = kernelp = NULL;
3755*ae115bc7Smrj 	new_arg = old_args = space = NULL;
3756*ae115bc7Smrj 	grubdisk = rootdev = new_path = NULL;
3757*ae115bc7Smrj 	buf[0] = '\0';
3758*ae115bc7Smrj 
3759*ae115bc7Smrj 	if (bam_direct != BAM_DIRECT_DBOOT) {
3760*ae115bc7Smrj 		bam_error(NOT_DBOOT, optnum == KERNEL_CMD ? "kernel" : "args");
3761*ae115bc7Smrj 		return (BAM_ERROR);
3762*ae115bc7Smrj 	}
3763*ae115bc7Smrj 
3764*ae115bc7Smrj 	/*
3765*ae115bc7Smrj 	 * If a user changed the default entry to a non-bootadm controlled
3766*ae115bc7Smrj 	 * one, we don't want to mess with it.  Just print an error and
3767*ae115bc7Smrj 	 * return.
3768*ae115bc7Smrj 	 */
3769*ae115bc7Smrj 	if (mp->curdefault) {
3770*ae115bc7Smrj 		entryNum = s_strtol(mp->curdefault->arg);
3771*ae115bc7Smrj 		for (entryp = mp->entries; entryp; entryp = entryp->next) {
3772*ae115bc7Smrj 			if (entryp->entryNum == entryNum)
3773*ae115bc7Smrj 				break;
3774*ae115bc7Smrj 		}
3775*ae115bc7Smrj 		if ((entryp != NULL) &&
3776*ae115bc7Smrj 		    ((entryp->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU)) == 0)) {
3777*ae115bc7Smrj 			bam_error(DEFAULT_NOT_BAM);
3778*ae115bc7Smrj 			return (BAM_ERROR);
3779*ae115bc7Smrj 		}
3780*ae115bc7Smrj 	}
3781*ae115bc7Smrj 
3782*ae115bc7Smrj 	entryNum = -1;
3783*ae115bc7Smrj 	entryp = find_boot_entry(mp, BOOTENV_RC_TITLE, NULL, NULL, 0,
3784*ae115bc7Smrj 	    &entryNum);
3785*ae115bc7Smrj 
3786*ae115bc7Smrj 	if (entryp != NULL) {
3787*ae115bc7Smrj 		for (ptr = entryp->start; ptr && ptr != entryp->end;
3788*ae115bc7Smrj 		    ptr = ptr->next) {
3789*ae115bc7Smrj 			if (strncmp(ptr->cmd, menu_cmds[KERNEL_CMD],
3790*ae115bc7Smrj 			    sizeof (menu_cmds[KERNEL_CMD]) - 1) == 0) {
3791*ae115bc7Smrj 				kernelp = ptr;
3792*ae115bc7Smrj 				break;
3793*ae115bc7Smrj 			}
3794*ae115bc7Smrj 		}
3795*ae115bc7Smrj 		if (kernelp == NULL) {
3796*ae115bc7Smrj 			bam_error(NO_KERNEL, entryNum);
3797*ae115bc7Smrj 			return (BAM_ERROR);
3798*ae115bc7Smrj 		}
3799*ae115bc7Smrj 
3800*ae115bc7Smrj 		old_kernel_len = strcspn(kernelp->arg, " \t");
3801*ae115bc7Smrj 		space = old_args = kernelp->arg + old_kernel_len;
3802*ae115bc7Smrj 		while ((*old_args == ' ') || (*old_args == '\t'))
3803*ae115bc7Smrj 			old_args++;
3804*ae115bc7Smrj 	}
3805*ae115bc7Smrj 
3806*ae115bc7Smrj 	if (path == NULL) {
3807*ae115bc7Smrj 		/* Simply report what was found */
3808*ae115bc7Smrj 		if (kernelp == NULL)
3809*ae115bc7Smrj 			return (BAM_SUCCESS);
3810*ae115bc7Smrj 
3811*ae115bc7Smrj 		if (optnum == ARGS_CMD) {
3812*ae115bc7Smrj 			if (old_args[0] != '\0')
3813*ae115bc7Smrj 				(void) strlcpy(buf, old_args, bufsize);
3814*ae115bc7Smrj 		} else {
3815*ae115bc7Smrj 			/*
3816*ae115bc7Smrj 			 * We need to print the kernel, so we just turn the
3817*ae115bc7Smrj 			 * first space into a '\0' and print the beginning.
3818*ae115bc7Smrj 			 * We don't print anything if it's the default kernel.
3819*ae115bc7Smrj 			 */
3820*ae115bc7Smrj 			old_space = *space;
3821*ae115bc7Smrj 			*space = '\0';
3822*ae115bc7Smrj 			if (strcmp(kernelp->arg, DIRECT_BOOT_KERNEL) != 0)
3823*ae115bc7Smrj 				(void) strlcpy(buf, kernelp->arg, bufsize);
3824*ae115bc7Smrj 			*space = old_space;
3825*ae115bc7Smrj 		}
3826*ae115bc7Smrj 		return (BAM_SUCCESS);
3827*ae115bc7Smrj 	}
3828*ae115bc7Smrj 
3829*ae115bc7Smrj 	/*
3830*ae115bc7Smrj 	 * First, check if we're resetting an entry to the default.
3831*ae115bc7Smrj 	 */
3832*ae115bc7Smrj 	if ((path[0] == '\0') ||
3833*ae115bc7Smrj 	    ((optnum == KERNEL_CMD) &&
3834*ae115bc7Smrj 	    (strcmp(path, DIRECT_BOOT_KERNEL) == 0))) {
3835*ae115bc7Smrj 		if ((entryp == NULL) || (kernelp == NULL)) {
3836*ae115bc7Smrj 			/* No previous entry, it's already the default */
3837*ae115bc7Smrj 			return (BAM_SUCCESS);
3838*ae115bc7Smrj 		}
3839*ae115bc7Smrj 
3840*ae115bc7Smrj 		/*
3841*ae115bc7Smrj 		 * Check if we can delete the entry.  If we're resetting the
3842*ae115bc7Smrj 		 * kernel command, and the args is already empty, or if we're
3843*ae115bc7Smrj 		 * resetting the args command, and the kernel is already the
3844*ae115bc7Smrj 		 * default, we can restore the old default and delete the entry.
3845*ae115bc7Smrj 		 */
3846*ae115bc7Smrj 		if (((optnum == KERNEL_CMD) &&
3847*ae115bc7Smrj 		    ((old_args == NULL) || (old_args[0] == '\0'))) ||
3848*ae115bc7Smrj 		    ((optnum == ARGS_CMD) &&
3849*ae115bc7Smrj 		    (strncmp(kernelp->arg, DIRECT_BOOT_KERNEL,
3850*ae115bc7Smrj 		    sizeof (DIRECT_BOOT_KERNEL) - 1) == 0))) {
3851*ae115bc7Smrj 			kernelp = NULL;
3852*ae115bc7Smrj 			(void) do_delete(mp, entryNum);
3853*ae115bc7Smrj 			restore_default_entry(mp, BAM_OLD_RC_DEF,
3854*ae115bc7Smrj 			    mp->old_rc_default);
3855*ae115bc7Smrj 			mp->old_rc_default = NULL;
3856*ae115bc7Smrj 			rv = BAM_WRITE;
3857*ae115bc7Smrj 			goto done;
3858*ae115bc7Smrj 		}
3859*ae115bc7Smrj 
3860*ae115bc7Smrj 		if (optnum == KERNEL_CMD) {
3861*ae115bc7Smrj 			/*
3862*ae115bc7Smrj 			 * At this point, we've already checked that old_args
3863*ae115bc7Smrj 			 * and entryp are valid pointers.  The "+ 2" is for
3864*ae115bc7Smrj 			 * a space a the string termination character.
3865*ae115bc7Smrj 			 */
3866*ae115bc7Smrj 			new_str_len = (sizeof (DIRECT_BOOT_KERNEL) - 1) +
3867*ae115bc7Smrj 			    strlen(old_args) + 2;
3868*ae115bc7Smrj 			new_arg = s_calloc(1, new_str_len);
3869*ae115bc7Smrj 			(void) snprintf(new_arg, new_str_len, "%s %s",
3870*ae115bc7Smrj 			    DIRECT_BOOT_KERNEL, old_args);
3871*ae115bc7Smrj 			free(kernelp->arg);
3872*ae115bc7Smrj 			kernelp->arg = new_arg;
3873*ae115bc7Smrj 
3874*ae115bc7Smrj 			/*
3875*ae115bc7Smrj 			 * We have changed the kernel line, so we may need
3876*ae115bc7Smrj 			 * to update the archive line as well.
3877*ae115bc7Smrj 			 */
3878*ae115bc7Smrj 			set_archive_line(entryp, kernelp);
3879*ae115bc7Smrj 		} else {
3880*ae115bc7Smrj 			/*
3881*ae115bc7Smrj 			 * We're resetting the boot args to nothing, so
3882*ae115bc7Smrj 			 * we only need to copy the kernel.  We've already
3883*ae115bc7Smrj 			 * checked that the kernel is not the default.
3884*ae115bc7Smrj 			 */
3885*ae115bc7Smrj 			new_arg = s_calloc(1, old_kernel_len + 1);
3886*ae115bc7Smrj 			(void) snprintf(new_arg, old_kernel_len + 1, "%s",
3887*ae115bc7Smrj 			    kernelp->arg);
3888*ae115bc7Smrj 			free(kernelp->arg);
3889*ae115bc7Smrj 			kernelp->arg = new_arg;
3890*ae115bc7Smrj 		}
3891*ae115bc7Smrj 		rv = BAM_WRITE;
3892*ae115bc7Smrj 		goto done;
3893*ae115bc7Smrj 	}
3894*ae115bc7Smrj 
3895*ae115bc7Smrj 	/*
3896*ae115bc7Smrj 	 * Expand the kernel file to a full path, if necessary
3897*ae115bc7Smrj 	 */
3898*ae115bc7Smrj 	if ((optnum == KERNEL_CMD) && (path[0] != '/')) {
3899*ae115bc7Smrj 		new_path = expand_path(path);
3900*ae115bc7Smrj 		if (new_path == NULL) {
3901*ae115bc7Smrj 			return (BAM_ERROR);
3902*ae115bc7Smrj 		}
3903*ae115bc7Smrj 		free_new_path = 1;
3904*ae115bc7Smrj 	} else {
3905*ae115bc7Smrj 		new_path = path;
3906*ae115bc7Smrj 		free_new_path = 0;
3907*ae115bc7Smrj 	}
3908*ae115bc7Smrj 
3909*ae115bc7Smrj 	/*
3910*ae115bc7Smrj 	 * At this point, we know we're setting a new value.  First, take care
3911*ae115bc7Smrj 	 * of the case where there was no previous entry.
3912*ae115bc7Smrj 	 */
3913*ae115bc7Smrj 	if (entryp == NULL) {
3914*ae115bc7Smrj 		/* Similar to code in update_temp */
3915*ae115bc7Smrj 		if (stat(GRUB_slice, &sb) != 0) {
3916*ae115bc7Smrj 			/*
3917*ae115bc7Smrj 			 * 1. First get root disk name from mnttab
3918*ae115bc7Smrj 			 * 2. Translate disk name to grub name
3919*ae115bc7Smrj 			 * 3. Add the new menu entry
3920*ae115bc7Smrj 			 */
3921*ae115bc7Smrj 			rootdev = get_special("/");
3922*ae115bc7Smrj 			if (rootdev) {
3923*ae115bc7Smrj 				grubdisk = os_to_grubdisk(rootdev, 1);
3924*ae115bc7Smrj 				free(rootdev);
3925*ae115bc7Smrj 			}
3926*ae115bc7Smrj 		} else {
3927*ae115bc7Smrj 			/*
3928*ae115bc7Smrj 			 * This is an LU BE. The GRUB_root file
3929*ae115bc7Smrj 			 * contains entry for GRUB's "root" cmd.
3930*ae115bc7Smrj 			 */
3931*ae115bc7Smrj 			grubdisk = read_grub_root();
3932*ae115bc7Smrj 		}
3933*ae115bc7Smrj 		if (grubdisk == NULL) {
3934*ae115bc7Smrj 			bam_error(REBOOT_WITH_ARGS_FAILED);
3935*ae115bc7Smrj 			rv = BAM_ERROR;
3936*ae115bc7Smrj 			goto done;
3937*ae115bc7Smrj 		}
3938*ae115bc7Smrj 		if (optnum == KERNEL_CMD) {
3939*ae115bc7Smrj 			entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
3940*ae115bc7Smrj 			    grubdisk, new_path, NULL);
3941*ae115bc7Smrj 		} else {
3942*ae115bc7Smrj 			new_str_len = strlen(DIRECT_BOOT_KERNEL) +
3943*ae115bc7Smrj 			    strlen(path) + 8;
3944*ae115bc7Smrj 			new_arg = s_calloc(1, new_str_len);
3945*ae115bc7Smrj 
3946*ae115bc7Smrj 			(void) snprintf(new_arg, new_str_len, "%s %s",
3947*ae115bc7Smrj 			    DIRECT_BOOT_KERNEL, path);
3948*ae115bc7Smrj 			entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
3949*ae115bc7Smrj 			    grubdisk, new_arg, DIRECT_BOOT_ARCHIVE);
3950*ae115bc7Smrj 		}
3951*ae115bc7Smrj 		save_default_entry(mp, BAM_OLD_RC_DEF);
3952*ae115bc7Smrj 		(void) set_global(mp, menu_cmds[DEFAULT_CMD], entryNum);
3953*ae115bc7Smrj 		rv = BAM_WRITE;
3954*ae115bc7Smrj 		goto done;
3955*ae115bc7Smrj 	}
3956*ae115bc7Smrj 
3957*ae115bc7Smrj 	/*
3958*ae115bc7Smrj 	 * There was already an bootenv entry which we need to edit.
3959*ae115bc7Smrj 	 */
3960*ae115bc7Smrj 	if (optnum == KERNEL_CMD) {
3961*ae115bc7Smrj 		new_str_len = strlen(new_path) + strlen(old_args) + 2;
3962*ae115bc7Smrj 		new_arg = s_calloc(1, new_str_len);
3963*ae115bc7Smrj 		(void) snprintf(new_arg, new_str_len, "%s %s", new_path,
3964*ae115bc7Smrj 		    old_args);
3965*ae115bc7Smrj 		free(kernelp->arg);
3966*ae115bc7Smrj 		kernelp->arg = new_arg;
3967*ae115bc7Smrj 
3968*ae115bc7Smrj 		/*
3969*ae115bc7Smrj 		 * If we have changed the kernel line, we may need to update
3970*ae115bc7Smrj 		 * the archive line as well.
3971*ae115bc7Smrj 		 */
3972*ae115bc7Smrj 		set_archive_line(entryp, kernelp);
3973*ae115bc7Smrj 	} else {
3974*ae115bc7Smrj 		new_str_len = old_kernel_len + strlen(path) + 8;
3975*ae115bc7Smrj 		new_arg = s_calloc(1, new_str_len);
3976*ae115bc7Smrj 		(void) strncpy(new_arg, kernelp->arg, old_kernel_len);
3977*ae115bc7Smrj 		(void) strlcat(new_arg, " ", new_str_len);
3978*ae115bc7Smrj 		(void) strlcat(new_arg, path, new_str_len);
3979*ae115bc7Smrj 		free(kernelp->arg);
3980*ae115bc7Smrj 		kernelp->arg = new_arg;
3981*ae115bc7Smrj 	}
3982*ae115bc7Smrj 	rv = BAM_WRITE;
3983*ae115bc7Smrj 
3984*ae115bc7Smrj done:
3985*ae115bc7Smrj 	if ((rv == BAM_WRITE) && kernelp)
3986*ae115bc7Smrj 		update_line(kernelp);
3987*ae115bc7Smrj 	if (free_new_path)
3988*ae115bc7Smrj 		free(new_path);
3989*ae115bc7Smrj 	return (rv);
3990*ae115bc7Smrj }
3991*ae115bc7Smrj 
39927c478bd9Sstevel@tonic-gate /*ARGSUSED*/
39937c478bd9Sstevel@tonic-gate static error_t
39947c478bd9Sstevel@tonic-gate set_option(menu_t *mp, char *menu_path, char *opt)
39957c478bd9Sstevel@tonic-gate {
39967c478bd9Sstevel@tonic-gate 	int optnum, optval;
39977c478bd9Sstevel@tonic-gate 	char *val;
3998*ae115bc7Smrj 	char buf[BUFSIZ] = "";
3999*ae115bc7Smrj 	error_t rv;
40007c478bd9Sstevel@tonic-gate 
40017c478bd9Sstevel@tonic-gate 	assert(mp);
40027c478bd9Sstevel@tonic-gate 	assert(opt);
40037c478bd9Sstevel@tonic-gate 
40047c478bd9Sstevel@tonic-gate 	val = strchr(opt, '=');
4005*ae115bc7Smrj 	if (val != NULL) {
4006*ae115bc7Smrj 		*val = '\0';
40077c478bd9Sstevel@tonic-gate 	}
40087c478bd9Sstevel@tonic-gate 
40097c478bd9Sstevel@tonic-gate 	if (strcmp(opt, "default") == 0) {
40107c478bd9Sstevel@tonic-gate 		optnum = DEFAULT_CMD;
40117c478bd9Sstevel@tonic-gate 	} else if (strcmp(opt, "timeout") == 0) {
40127c478bd9Sstevel@tonic-gate 		optnum = TIMEOUT_CMD;
4013*ae115bc7Smrj 	} else if (strcmp(opt, menu_cmds[KERNEL_CMD]) == 0) {
4014*ae115bc7Smrj 		optnum = KERNEL_CMD;
4015*ae115bc7Smrj 	} else if (strcmp(opt, menu_cmds[ARGS_CMD]) == 0) {
4016*ae115bc7Smrj 		optnum = ARGS_CMD;
40177c478bd9Sstevel@tonic-gate 	} else {
40187c478bd9Sstevel@tonic-gate 		bam_error(INVALID_ENTRY, opt);
40197c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
40207c478bd9Sstevel@tonic-gate 	}
40217c478bd9Sstevel@tonic-gate 
4022*ae115bc7Smrj 	/*
4023*ae115bc7Smrj 	 * kernel and args are allowed without "=new_value" strings.  All
4024*ae115bc7Smrj 	 * others cause errors
4025*ae115bc7Smrj 	 */
4026*ae115bc7Smrj 	if ((val == NULL) && (optnum != KERNEL_CMD) && (optnum != ARGS_CMD)) {
4027*ae115bc7Smrj 		bam_error(INVALID_ENTRY, opt);
4028*ae115bc7Smrj 		return (BAM_ERROR);
4029*ae115bc7Smrj 	} else if (val != NULL) {
40307c478bd9Sstevel@tonic-gate 		*val = '=';
4031*ae115bc7Smrj 	}
4032*ae115bc7Smrj 
4033*ae115bc7Smrj 	if ((optnum == KERNEL_CMD) || (optnum == ARGS_CMD)) {
4034*ae115bc7Smrj 		rv = set_kernel(mp, optnum, val ? val + 1 : NULL, buf, BUFSIZ);
4035*ae115bc7Smrj 		if ((rv == BAM_SUCCESS) && (buf[0] != '\0'))
4036*ae115bc7Smrj 			(void) printf("%s\n", buf);
4037*ae115bc7Smrj 		return (rv);
4038*ae115bc7Smrj 	} else {
4039*ae115bc7Smrj 		optval = s_strtol(val + 1);
40407c478bd9Sstevel@tonic-gate 		return (set_global(mp, menu_cmds[optnum], optval));
40417c478bd9Sstevel@tonic-gate 	}
4042*ae115bc7Smrj }
40437c478bd9Sstevel@tonic-gate 
40447c478bd9Sstevel@tonic-gate /*
40457c478bd9Sstevel@tonic-gate  * The quiet argument suppresses messages. This is used
40467c478bd9Sstevel@tonic-gate  * when invoked in the context of other commands (e.g. list_entry)
40477c478bd9Sstevel@tonic-gate  */
40487c478bd9Sstevel@tonic-gate static error_t
40497c478bd9Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet)
40507c478bd9Sstevel@tonic-gate {
40517c478bd9Sstevel@tonic-gate 	line_t *lp;
40527c478bd9Sstevel@tonic-gate 	char *arg;
40537c478bd9Sstevel@tonic-gate 	int done, ret = BAM_SUCCESS;
40547c478bd9Sstevel@tonic-gate 
40557c478bd9Sstevel@tonic-gate 	assert(mp);
40567c478bd9Sstevel@tonic-gate 	assert(menu_path);
40577c478bd9Sstevel@tonic-gate 	assert(globalcmd);
40587c478bd9Sstevel@tonic-gate 
40597c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
40607c478bd9Sstevel@tonic-gate 		if (!quiet)
40617c478bd9Sstevel@tonic-gate 			bam_error(NO_MENU, menu_path);
40627c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
40637c478bd9Sstevel@tonic-gate 	}
40647c478bd9Sstevel@tonic-gate 
40657c478bd9Sstevel@tonic-gate 	done = 0;
40667c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
40677c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
40687c478bd9Sstevel@tonic-gate 			continue;
40697c478bd9Sstevel@tonic-gate 
40707c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
40717c478bd9Sstevel@tonic-gate 			if (!quiet)
40727c478bd9Sstevel@tonic-gate 				bam_error(NO_CMD, lp->lineNum);
40737c478bd9Sstevel@tonic-gate 			continue;
40747c478bd9Sstevel@tonic-gate 		}
40757c478bd9Sstevel@tonic-gate 
40767c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
40777c478bd9Sstevel@tonic-gate 			continue;
40787c478bd9Sstevel@tonic-gate 
40797c478bd9Sstevel@tonic-gate 		/* Found global. Check for duplicates */
40807c478bd9Sstevel@tonic-gate 		if (done && !quiet) {
40817c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
40827c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
40837c478bd9Sstevel@tonic-gate 		}
40847c478bd9Sstevel@tonic-gate 
40857c478bd9Sstevel@tonic-gate 		arg = lp->arg ? lp->arg : "";
40867c478bd9Sstevel@tonic-gate 		bam_print(GLOBAL_CMD, globalcmd, arg);
40877c478bd9Sstevel@tonic-gate 		done = 1;
40887c478bd9Sstevel@tonic-gate 	}
40897c478bd9Sstevel@tonic-gate 
40907c478bd9Sstevel@tonic-gate 	if (!done && bam_verbose)
40917c478bd9Sstevel@tonic-gate 		bam_print(NO_ENTRY, globalcmd);
40927c478bd9Sstevel@tonic-gate 
40937c478bd9Sstevel@tonic-gate 	return (ret);
40947c478bd9Sstevel@tonic-gate }
40957c478bd9Sstevel@tonic-gate 
40967c478bd9Sstevel@tonic-gate static error_t
40977c478bd9Sstevel@tonic-gate menu_write(char *root, menu_t *mp)
40987c478bd9Sstevel@tonic-gate {
40997c478bd9Sstevel@tonic-gate 	return (list2file(root, MENU_TMP, GRUB_MENU, mp->start));
41007c478bd9Sstevel@tonic-gate }
41017c478bd9Sstevel@tonic-gate 
41027c478bd9Sstevel@tonic-gate static void
41037c478bd9Sstevel@tonic-gate line_free(line_t *lp)
41047c478bd9Sstevel@tonic-gate {
41057c478bd9Sstevel@tonic-gate 	if (lp == NULL)
41067c478bd9Sstevel@tonic-gate 		return;
41077c478bd9Sstevel@tonic-gate 
41087c478bd9Sstevel@tonic-gate 	if (lp->cmd)
41097c478bd9Sstevel@tonic-gate 		free(lp->cmd);
41107c478bd9Sstevel@tonic-gate 	if (lp->sep)
41117c478bd9Sstevel@tonic-gate 		free(lp->sep);
41127c478bd9Sstevel@tonic-gate 	if (lp->arg)
41137c478bd9Sstevel@tonic-gate 		free(lp->arg);
41147c478bd9Sstevel@tonic-gate 	if (lp->line)
41157c478bd9Sstevel@tonic-gate 		free(lp->line);
41167c478bd9Sstevel@tonic-gate 	free(lp);
41177c478bd9Sstevel@tonic-gate }
41187c478bd9Sstevel@tonic-gate 
41197c478bd9Sstevel@tonic-gate static void
41207c478bd9Sstevel@tonic-gate linelist_free(line_t *start)
41217c478bd9Sstevel@tonic-gate {
41227c478bd9Sstevel@tonic-gate 	line_t *lp;
41237c478bd9Sstevel@tonic-gate 
41247c478bd9Sstevel@tonic-gate 	while (start) {
41257c478bd9Sstevel@tonic-gate 		lp = start;
41267c478bd9Sstevel@tonic-gate 		start = start->next;
41277c478bd9Sstevel@tonic-gate 		line_free(lp);
41287c478bd9Sstevel@tonic-gate 	}
41297c478bd9Sstevel@tonic-gate }
41307c478bd9Sstevel@tonic-gate 
41317c478bd9Sstevel@tonic-gate static void
41327c478bd9Sstevel@tonic-gate filelist_free(filelist_t *flistp)
41337c478bd9Sstevel@tonic-gate {
41347c478bd9Sstevel@tonic-gate 	linelist_free(flistp->head);
41357c478bd9Sstevel@tonic-gate 	flistp->head = NULL;
41367c478bd9Sstevel@tonic-gate 	flistp->tail = NULL;
41377c478bd9Sstevel@tonic-gate }
41387c478bd9Sstevel@tonic-gate 
41397c478bd9Sstevel@tonic-gate static void
41407c478bd9Sstevel@tonic-gate menu_free(menu_t *mp)
41417c478bd9Sstevel@tonic-gate {
41428c1b6884Sszhou 	entry_t *ent, *tmp;
41437c478bd9Sstevel@tonic-gate 	assert(mp);
41447c478bd9Sstevel@tonic-gate 
41457c478bd9Sstevel@tonic-gate 	if (mp->start)
41467c478bd9Sstevel@tonic-gate 		linelist_free(mp->start);
41478c1b6884Sszhou 	ent = mp->entries;
41488c1b6884Sszhou 	while (ent) {
41498c1b6884Sszhou 		tmp = ent;
41508c1b6884Sszhou 		ent = tmp->next;
41518c1b6884Sszhou 		free(tmp);
41528c1b6884Sszhou 	}
41537c478bd9Sstevel@tonic-gate 
41548c1b6884Sszhou 	free(mp);
41557c478bd9Sstevel@tonic-gate }
41567c478bd9Sstevel@tonic-gate 
41577c478bd9Sstevel@tonic-gate /*
41587c478bd9Sstevel@tonic-gate  * Utility routines
41597c478bd9Sstevel@tonic-gate  */
41607c478bd9Sstevel@tonic-gate 
41617c478bd9Sstevel@tonic-gate 
41627c478bd9Sstevel@tonic-gate /*
41637c478bd9Sstevel@tonic-gate  * Returns 0 on success
41647c478bd9Sstevel@tonic-gate  * Any other value indicates an error
41657c478bd9Sstevel@tonic-gate  */
41667c478bd9Sstevel@tonic-gate static int
41677c478bd9Sstevel@tonic-gate exec_cmd(char *cmdline, char *output, int64_t osize)
41687c478bd9Sstevel@tonic-gate {
41697c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
41707c478bd9Sstevel@tonic-gate 	int ret;
41717c478bd9Sstevel@tonic-gate 	FILE *ptr;
41727c478bd9Sstevel@tonic-gate 	size_t len;
41737c478bd9Sstevel@tonic-gate 	sigset_t set;
41747c478bd9Sstevel@tonic-gate 	void (*disp)(int);
41757c478bd9Sstevel@tonic-gate 
41767c478bd9Sstevel@tonic-gate 	/*
41777c478bd9Sstevel@tonic-gate 	 * For security
41787c478bd9Sstevel@tonic-gate 	 * - only absolute paths are allowed
41797c478bd9Sstevel@tonic-gate 	 * - set IFS to space and tab
41807c478bd9Sstevel@tonic-gate 	 */
41817c478bd9Sstevel@tonic-gate 	if (*cmdline != '/') {
41827c478bd9Sstevel@tonic-gate 		bam_error(ABS_PATH_REQ, cmdline);
41837c478bd9Sstevel@tonic-gate 		return (-1);
41847c478bd9Sstevel@tonic-gate 	}
41857c478bd9Sstevel@tonic-gate 	(void) putenv("IFS= \t");
41867c478bd9Sstevel@tonic-gate 
41877c478bd9Sstevel@tonic-gate 	/*
41887c478bd9Sstevel@tonic-gate 	 * We may have been exec'ed with SIGCHLD blocked
41897c478bd9Sstevel@tonic-gate 	 * unblock it here
41907c478bd9Sstevel@tonic-gate 	 */
41917c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
41927c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
41937c478bd9Sstevel@tonic-gate 	if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
41947c478bd9Sstevel@tonic-gate 		bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno));
41957c478bd9Sstevel@tonic-gate 		return (-1);
41967c478bd9Sstevel@tonic-gate 	}
41977c478bd9Sstevel@tonic-gate 
41987c478bd9Sstevel@tonic-gate 	/*
41997c478bd9Sstevel@tonic-gate 	 * Set SIGCHLD disposition to SIG_DFL for popen/pclose
42007c478bd9Sstevel@tonic-gate 	 */
42017c478bd9Sstevel@tonic-gate 	disp = sigset(SIGCHLD, SIG_DFL);
42027c478bd9Sstevel@tonic-gate 	if (disp == SIG_ERR) {
42037c478bd9Sstevel@tonic-gate 		bam_error(FAILED_SIG, strerror(errno));
42047c478bd9Sstevel@tonic-gate 		return (-1);
42057c478bd9Sstevel@tonic-gate 	}
42067c478bd9Sstevel@tonic-gate 	if (disp == SIG_HOLD) {
42077c478bd9Sstevel@tonic-gate 		bam_error(BLOCKED_SIG, cmdline);
42087c478bd9Sstevel@tonic-gate 		return (-1);
42097c478bd9Sstevel@tonic-gate 	}
42107c478bd9Sstevel@tonic-gate 
42117c478bd9Sstevel@tonic-gate 	ptr = popen(cmdline, "r");
42127c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
42137c478bd9Sstevel@tonic-gate 		bam_error(POPEN_FAIL, cmdline, strerror(errno));
42147c478bd9Sstevel@tonic-gate 		return (-1);
42157c478bd9Sstevel@tonic-gate 	}
42167c478bd9Sstevel@tonic-gate 
42177c478bd9Sstevel@tonic-gate 	/*
42187c478bd9Sstevel@tonic-gate 	 * If we simply do a pclose() following a popen(), pclose()
42197c478bd9Sstevel@tonic-gate 	 * will close the reader end of the pipe immediately even
42207c478bd9Sstevel@tonic-gate 	 * if the child process has not started/exited. pclose()
42217c478bd9Sstevel@tonic-gate 	 * does wait for cmd to terminate before returning though.
42227c478bd9Sstevel@tonic-gate 	 * When the executed command writes its output to the pipe
42237c478bd9Sstevel@tonic-gate 	 * there is no reader process and the command dies with
42247c478bd9Sstevel@tonic-gate 	 * SIGPIPE. To avoid this we read repeatedly until read
42257c478bd9Sstevel@tonic-gate 	 * terminates with EOF. This indicates that the command
42267c478bd9Sstevel@tonic-gate 	 * (writer) has closed the pipe and we can safely do a
42277c478bd9Sstevel@tonic-gate 	 * pclose().
42287c478bd9Sstevel@tonic-gate 	 *
42297c478bd9Sstevel@tonic-gate 	 * Since pclose() does wait for the command to exit,
42307c478bd9Sstevel@tonic-gate 	 * we can safely reap the exit status of the command
42317c478bd9Sstevel@tonic-gate 	 * from the value returned by pclose()
42327c478bd9Sstevel@tonic-gate 	 */
42337c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), ptr) != NULL) {
42347c478bd9Sstevel@tonic-gate 		/* if (bam_verbose)  XXX */
42357c478bd9Sstevel@tonic-gate 			bam_print(PRINT_NO_NEWLINE, buf);
42367c478bd9Sstevel@tonic-gate 		if (output && osize > 0) {
42377c478bd9Sstevel@tonic-gate 			(void) snprintf(output, osize, "%s", buf);
42387c478bd9Sstevel@tonic-gate 			len = strlen(buf);
42397c478bd9Sstevel@tonic-gate 			output += len;
42407c478bd9Sstevel@tonic-gate 			osize -= len;
42417c478bd9Sstevel@tonic-gate 		}
42427c478bd9Sstevel@tonic-gate 	}
42437c478bd9Sstevel@tonic-gate 
42447c478bd9Sstevel@tonic-gate 	ret = pclose(ptr);
42457c478bd9Sstevel@tonic-gate 	if (ret == -1) {
42467c478bd9Sstevel@tonic-gate 		bam_error(PCLOSE_FAIL, cmdline, strerror(errno));
42477c478bd9Sstevel@tonic-gate 		return (-1);
42487c478bd9Sstevel@tonic-gate 	}
42497c478bd9Sstevel@tonic-gate 
42507c478bd9Sstevel@tonic-gate 	if (WIFEXITED(ret)) {
42517c478bd9Sstevel@tonic-gate 		return (WEXITSTATUS(ret));
42527c478bd9Sstevel@tonic-gate 	} else {
42537c478bd9Sstevel@tonic-gate 		bam_error(EXEC_FAIL, cmdline, ret);
42547c478bd9Sstevel@tonic-gate 		return (-1);
42557c478bd9Sstevel@tonic-gate 	}
42567c478bd9Sstevel@tonic-gate }
42577c478bd9Sstevel@tonic-gate 
42587c478bd9Sstevel@tonic-gate /*
42597c478bd9Sstevel@tonic-gate  * Since this function returns -1 on error
42607c478bd9Sstevel@tonic-gate  * it cannot be used to convert -1. However,
42617c478bd9Sstevel@tonic-gate  * that is sufficient for what we need.
42627c478bd9Sstevel@tonic-gate  */
42637c478bd9Sstevel@tonic-gate static long
42647c478bd9Sstevel@tonic-gate s_strtol(char *str)
42657c478bd9Sstevel@tonic-gate {
42667c478bd9Sstevel@tonic-gate 	long l;
42677c478bd9Sstevel@tonic-gate 	char *res = NULL;
42687c478bd9Sstevel@tonic-gate 
42697c478bd9Sstevel@tonic-gate 	if (str == NULL) {
42707c478bd9Sstevel@tonic-gate 		return (-1);
42717c478bd9Sstevel@tonic-gate 	}
42727c478bd9Sstevel@tonic-gate 
42737c478bd9Sstevel@tonic-gate 	errno = 0;
42747c478bd9Sstevel@tonic-gate 	l = strtol(str, &res, 10);
42757c478bd9Sstevel@tonic-gate 	if (errno || *res != '\0') {
42767c478bd9Sstevel@tonic-gate 		return (-1);
42777c478bd9Sstevel@tonic-gate 	}
42787c478bd9Sstevel@tonic-gate 
42797c478bd9Sstevel@tonic-gate 	return (l);
42807c478bd9Sstevel@tonic-gate }
42817c478bd9Sstevel@tonic-gate 
42827c478bd9Sstevel@tonic-gate /*
42837c478bd9Sstevel@tonic-gate  * Wrapper around fputs, that adds a newline (since fputs doesn't)
42847c478bd9Sstevel@tonic-gate  */
42857c478bd9Sstevel@tonic-gate static int
42867c478bd9Sstevel@tonic-gate s_fputs(char *str, FILE *fp)
42877c478bd9Sstevel@tonic-gate {
42887c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
42897c478bd9Sstevel@tonic-gate 
42907c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s\n", str);
42917c478bd9Sstevel@tonic-gate 	return (fputs(linebuf, fp));
42927c478bd9Sstevel@tonic-gate }
42937c478bd9Sstevel@tonic-gate 
42947c478bd9Sstevel@tonic-gate /*
42957c478bd9Sstevel@tonic-gate  * Wrapper around fgets, that strips newlines returned by fgets
42967c478bd9Sstevel@tonic-gate  */
4297*ae115bc7Smrj char *
42987c478bd9Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp)
42997c478bd9Sstevel@tonic-gate {
43007c478bd9Sstevel@tonic-gate 	int n;
43017c478bd9Sstevel@tonic-gate 
43027c478bd9Sstevel@tonic-gate 	buf = fgets(buf, buflen, fp);
43037c478bd9Sstevel@tonic-gate 	if (buf) {
43047c478bd9Sstevel@tonic-gate 		n = strlen(buf);
43057c478bd9Sstevel@tonic-gate 		if (n == buflen - 1 && buf[n-1] != '\n')
43067c478bd9Sstevel@tonic-gate 			bam_error(TOO_LONG, buflen - 1, buf);
43077c478bd9Sstevel@tonic-gate 		buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1];
43087c478bd9Sstevel@tonic-gate 	}
43097c478bd9Sstevel@tonic-gate 
43107c478bd9Sstevel@tonic-gate 	return (buf);
43117c478bd9Sstevel@tonic-gate }
43127c478bd9Sstevel@tonic-gate 
4313*ae115bc7Smrj void *
43147c478bd9Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz)
43157c478bd9Sstevel@tonic-gate {
43167c478bd9Sstevel@tonic-gate 	void *ptr;
43177c478bd9Sstevel@tonic-gate 
43187c478bd9Sstevel@tonic-gate 	ptr = calloc(nelem, sz);
43197c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
43207c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, nelem*sz);
43217c478bd9Sstevel@tonic-gate 		bam_exit(1);
43227c478bd9Sstevel@tonic-gate 	}
43237c478bd9Sstevel@tonic-gate 	return (ptr);
43247c478bd9Sstevel@tonic-gate }
43257c478bd9Sstevel@tonic-gate 
4326*ae115bc7Smrj void *
4327*ae115bc7Smrj s_realloc(void *ptr, size_t sz)
4328*ae115bc7Smrj {
4329*ae115bc7Smrj 	ptr = realloc(ptr, sz);
4330*ae115bc7Smrj 	if (ptr == NULL) {
4331*ae115bc7Smrj 		bam_error(NO_MEM, sz);
4332*ae115bc7Smrj 		bam_exit(1);
4333*ae115bc7Smrj 	}
4334*ae115bc7Smrj 	return (ptr);
4335*ae115bc7Smrj }
4336*ae115bc7Smrj 
43377c478bd9Sstevel@tonic-gate static char *
43387c478bd9Sstevel@tonic-gate s_strdup(char *str)
43397c478bd9Sstevel@tonic-gate {
43407c478bd9Sstevel@tonic-gate 	char *ptr;
43417c478bd9Sstevel@tonic-gate 
43427c478bd9Sstevel@tonic-gate 	if (str == NULL)
43437c478bd9Sstevel@tonic-gate 		return (NULL);
43447c478bd9Sstevel@tonic-gate 
43457c478bd9Sstevel@tonic-gate 	ptr = strdup(str);
43467c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
43477c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, strlen(str) + 1);
43487c478bd9Sstevel@tonic-gate 		bam_exit(1);
43497c478bd9Sstevel@tonic-gate 	}
43507c478bd9Sstevel@tonic-gate 	return (ptr);
43517c478bd9Sstevel@tonic-gate }
43527c478bd9Sstevel@tonic-gate 
43537c478bd9Sstevel@tonic-gate /*
43547c478bd9Sstevel@tonic-gate  * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients)
43557c478bd9Sstevel@tonic-gate  * Returns 0 otherwise
43567c478bd9Sstevel@tonic-gate  */
43577c478bd9Sstevel@tonic-gate static int
43587c478bd9Sstevel@tonic-gate is_amd64(void)
43597c478bd9Sstevel@tonic-gate {
43607c478bd9Sstevel@tonic-gate 	static int amd64 = -1;
43617c478bd9Sstevel@tonic-gate 	char isabuf[257];	/* from sysinfo(2) manpage */
43627c478bd9Sstevel@tonic-gate 
43637c478bd9Sstevel@tonic-gate 	if (amd64 != -1)
43647c478bd9Sstevel@tonic-gate 		return (amd64);
43657c478bd9Sstevel@tonic-gate 
43667c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 &&
43677c478bd9Sstevel@tonic-gate 	    strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0)
43687c478bd9Sstevel@tonic-gate 		amd64 = 1;
43697c478bd9Sstevel@tonic-gate 	else if (strstr(isabuf, "i386") == NULL)
43707c478bd9Sstevel@tonic-gate 		amd64 = 1;		/* diskless server */
43717c478bd9Sstevel@tonic-gate 	else
43727c478bd9Sstevel@tonic-gate 		amd64 = 0;
43737c478bd9Sstevel@tonic-gate 
43747c478bd9Sstevel@tonic-gate 	return (amd64);
43757c478bd9Sstevel@tonic-gate }
43767c478bd9Sstevel@tonic-gate 
43777c478bd9Sstevel@tonic-gate static void
43787c478bd9Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s)
43797c478bd9Sstevel@tonic-gate {
43807c478bd9Sstevel@tonic-gate 	line_t *lp;
43817c478bd9Sstevel@tonic-gate 
43827c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
43837c478bd9Sstevel@tonic-gate 	lp->line = s_strdup(s);
43847c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL)
43857c478bd9Sstevel@tonic-gate 		flistp->head = lp;
43867c478bd9Sstevel@tonic-gate 	else
43877c478bd9Sstevel@tonic-gate 		flistp->tail->next = lp;
43887c478bd9Sstevel@tonic-gate 	flistp->tail = lp;
43897c478bd9Sstevel@tonic-gate }
4390