xref: /titanic_50/usr/src/cmd/boot/bootadm/bootadm.c (revision 98892a3058bed9457ab6a22659dcccecfaf05d2a)
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"
66ae115bc7Smrj #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 /*
124*98892a30Snadkarni  * The following two defines are used to detect and create the correct
125*98892a30Snadkarni  * boot archive  when safemode patching is underway.  LOFS_PATCH_FILE is a
126*98892a30Snadkarni  * contracted private interface between bootadm and the install
127*98892a30Snadkarni  * consolidation.  It is set by pdo.c when a patch with SUNW_PATCH_SAFEMODE
128*98892a30Snadkarni  * is applied.
129*98892a30Snadkarni  */
130*98892a30Snadkarni 
131*98892a30Snadkarni #define	LOFS_PATCH_FILE		"/var/run/.patch_loopback_mode"
132*98892a30Snadkarni #define	LOFS_PATCH_MNT		"/var/run/.patch_root_loopbackmnt"
133*98892a30Snadkarni 
134*98892a30Snadkarni /*
1357c478bd9Sstevel@tonic-gate  * Default file attributes
1367c478bd9Sstevel@tonic-gate  */
1377c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_MODE	0644	/* default permissions */
1387c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_UID		0	/* user root */
1397c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_GID		3	/* group sys */
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * Menu related
1437c478bd9Sstevel@tonic-gate  * menu_cmd_t and menu_cmds must be kept in sync
1447c478bd9Sstevel@tonic-gate  */
145ae115bc7Smrj char *menu_cmds[] = {
1467c478bd9Sstevel@tonic-gate 	"default",	/* DEFAULT_CMD */
1477c478bd9Sstevel@tonic-gate 	"timeout",	/* TIMEOUT_CMD */
1487c478bd9Sstevel@tonic-gate 	"title",	/* TITLE_CMD */
1497c478bd9Sstevel@tonic-gate 	"root",		/* ROOT_CMD */
1507c478bd9Sstevel@tonic-gate 	"kernel",	/* KERNEL_CMD */
151ae115bc7Smrj 	"kernel$",	/* KERNEL_DOLLAR_CMD */
1527c478bd9Sstevel@tonic-gate 	"module",	/* MODULE_CMD */
153ae115bc7Smrj 	"module$",	/* MODULE_DOLLAR_CMD */
1547c478bd9Sstevel@tonic-gate 	" ",		/* SEP_CMD */
1557c478bd9Sstevel@tonic-gate 	"#",		/* COMMENT_CMD */
156ae115bc7Smrj 	"chainloader",	/* CHAINLOADER_CMD */
157ae115bc7Smrj 	"args",		/* ARGS_CMD */
1587c478bd9Sstevel@tonic-gate 	NULL
1597c478bd9Sstevel@tonic-gate };
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate #define	OPT_ENTRY_NUM	"entry"
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate  * archive related
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate typedef struct {
1677c478bd9Sstevel@tonic-gate 	line_t *head;
1687c478bd9Sstevel@tonic-gate 	line_t *tail;
1697c478bd9Sstevel@tonic-gate } filelist_t;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate #define	BOOT_FILE_LIST	"boot/solaris/filelist.ramdisk"
1727c478bd9Sstevel@tonic-gate #define	ETC_FILE_LIST	"etc/boot/solaris/filelist.ramdisk"
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate #define	FILE_STAT	"boot/solaris/filestat.ramdisk"
1757c478bd9Sstevel@tonic-gate #define	FILE_STAT_TMP	"boot/solaris/filestat.ramdisk.tmp"
1767c478bd9Sstevel@tonic-gate #define	DIR_PERMS	(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
1777c478bd9Sstevel@tonic-gate #define	FILE_STAT_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /* Globals */
180ae115bc7Smrj int bam_verbose;
181ae115bc7Smrj int bam_force;
1827c478bd9Sstevel@tonic-gate static char *prog;
1837c478bd9Sstevel@tonic-gate static subcmd_t bam_cmd;
1847c478bd9Sstevel@tonic-gate static char *bam_root;
1857c478bd9Sstevel@tonic-gate static int bam_rootlen;
1867c478bd9Sstevel@tonic-gate static int bam_root_readonly;
18740541d5dSvikram static int bam_alt_root;
1887c478bd9Sstevel@tonic-gate static char *bam_subcmd;
1897c478bd9Sstevel@tonic-gate static char *bam_opt;
1907c478bd9Sstevel@tonic-gate static int bam_debug;
1917c478bd9Sstevel@tonic-gate static char **bam_argv;
1927c478bd9Sstevel@tonic-gate static int bam_argc;
1937c478bd9Sstevel@tonic-gate static int bam_check;
1947c478bd9Sstevel@tonic-gate static int bam_smf_check;
1957c478bd9Sstevel@tonic-gate static int bam_lock_fd = -1;
1967c478bd9Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/";
197b610f78eSvikram static int bam_update_all;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /* function prototypes */
2007c478bd9Sstevel@tonic-gate static void parse_args_internal(int argc, char *argv[]);
2017c478bd9Sstevel@tonic-gate static void parse_args(int argc, char *argv[]);
2027c478bd9Sstevel@tonic-gate static error_t bam_menu(char *subcmd, char *opt, int argc, char *argv[]);
2037c478bd9Sstevel@tonic-gate static error_t bam_archive(char *subcmd, char *opt);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate static void bam_print(char *format, ...);
2067c478bd9Sstevel@tonic-gate static void bam_exit(int excode);
2077c478bd9Sstevel@tonic-gate static void bam_lock(void);
2087c478bd9Sstevel@tonic-gate static void bam_unlock(void);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate static int exec_cmd(char *cmdline, char *output, int64_t osize);
2117c478bd9Sstevel@tonic-gate static error_t read_globals(menu_t *mp, char *menu_path,
2127c478bd9Sstevel@tonic-gate     char *globalcmd, int quiet);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate static menu_t *menu_read(char *menu_path);
2157c478bd9Sstevel@tonic-gate static error_t menu_write(char *root, menu_t *mp);
2167c478bd9Sstevel@tonic-gate static void linelist_free(line_t *start);
2177c478bd9Sstevel@tonic-gate static void menu_free(menu_t *mp);
2187c478bd9Sstevel@tonic-gate static void line_free(line_t *lp);
2197c478bd9Sstevel@tonic-gate static void filelist_free(filelist_t *flistp);
2207c478bd9Sstevel@tonic-gate static error_t list2file(char *root, char *tmp,
2217c478bd9Sstevel@tonic-gate     char *final, line_t *start);
2227c478bd9Sstevel@tonic-gate static error_t list_entry(menu_t *mp, char *menu_path, char *opt);
2237c478bd9Sstevel@tonic-gate static error_t delete_all_entries(menu_t *mp, char *menu_path, char *opt);
2247c478bd9Sstevel@tonic-gate static error_t update_entry(menu_t *mp, char *root, char *opt);
2257c478bd9Sstevel@tonic-gate static error_t update_temp(menu_t *mp, char *root, char *opt);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate static error_t update_archive(char *root, char *opt);
2287c478bd9Sstevel@tonic-gate static error_t list_archive(char *root, char *opt);
2297c478bd9Sstevel@tonic-gate static error_t update_all(char *root, char *opt);
2307c478bd9Sstevel@tonic-gate static error_t read_list(char *root, filelist_t  *flistp);
2317c478bd9Sstevel@tonic-gate static error_t set_global(menu_t *mp, char *globalcmd, int val);
2327c478bd9Sstevel@tonic-gate static error_t set_option(menu_t *mp, char *globalcmd, char *opt);
233ae115bc7Smrj static error_t set_kernel(menu_t *mp, menu_cmd_t optnum, char *path,
234ae115bc7Smrj     char *buf, size_t bufsize);
235ae115bc7Smrj static char *expand_path(const char *partial_path);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate static long s_strtol(char *str);
2387c478bd9Sstevel@tonic-gate static int s_fputs(char *str, FILE *fp);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate static char *s_strdup(char *str);
2417c478bd9Sstevel@tonic-gate static int is_readonly(char *);
2427c478bd9Sstevel@tonic-gate static int is_amd64(void);
2437c478bd9Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate #if defined(__sparc)
2467c478bd9Sstevel@tonic-gate static void sparc_abort(void);
2477c478bd9Sstevel@tonic-gate #endif
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /* Menu related sub commands */
2507c478bd9Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = {
2511a97e40eSvikram 	"set_option",		OPT_OPTIONAL,	set_option, 0,	/* PUB */
2521a97e40eSvikram 	"list_entry",		OPT_OPTIONAL,	list_entry, 1,	/* PUB */
2531a97e40eSvikram 	"delete_all_entries",	OPT_ABSENT,	delete_all_entries, 0, /* PVT */
2541a97e40eSvikram 	"update_entry",		OPT_REQ,	update_entry, 0, /* menu */
2551a97e40eSvikram 	"update_temp",		OPT_OPTIONAL,	update_temp, 0,	/* reboot */
256ae115bc7Smrj 	"upgrade",		OPT_ABSENT,	upgrade_menu, 0, /* menu */
2571a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
2587c478bd9Sstevel@tonic-gate };
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /* Archive related sub commands */
2617c478bd9Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = {
2621a97e40eSvikram 	"update",		OPT_ABSENT,	update_archive, 0, /* PUB */
2631a97e40eSvikram 	"update_all",		OPT_ABSENT,	update_all, 0,	/* PVT */
2641a97e40eSvikram 	"list",			OPT_OPTIONAL,	list_archive, 1, /* PUB */
2651a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
2667c478bd9Sstevel@tonic-gate };
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate static struct {
2697c478bd9Sstevel@tonic-gate 	nvlist_t *new_nvlp;
2707c478bd9Sstevel@tonic-gate 	nvlist_t *old_nvlp;
2717c478bd9Sstevel@tonic-gate 	int need_update;
2727c478bd9Sstevel@tonic-gate } walk_arg;
2737c478bd9Sstevel@tonic-gate 
27458091fd8Ssetje 
2759632cfadSsetje struct safefile {
27658091fd8Ssetje 	char *name;
27758091fd8Ssetje 	struct safefile *next;
27858091fd8Ssetje };
27958091fd8Ssetje 
280ae115bc7Smrj static struct safefile *safefiles = NULL;
28158091fd8Ssetje #define	NEED_UPDATE_FILE "/etc/svc/volatile/boot_archive_needs_update"
28258091fd8Ssetje 
2837c478bd9Sstevel@tonic-gate static void
2847c478bd9Sstevel@tonic-gate usage(void)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "USAGE:\n");
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/* archive usage */
2907c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s update-archive [-vn] [-R altroot]\n",
2917c478bd9Sstevel@tonic-gate 	    prog);
2927c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-archive [-R altroot]\n", prog);
2937c478bd9Sstevel@tonic-gate #ifndef __sparc
2947c478bd9Sstevel@tonic-gate 	/* x86 only */
2957c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog);
2967c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog);
2977c478bd9Sstevel@tonic-gate #endif
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate int
3017c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 	error_t ret;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3067c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
3097c478bd9Sstevel@tonic-gate 		prog = argv[0];
3107c478bd9Sstevel@tonic-gate 	} else {
3117c478bd9Sstevel@tonic-gate 		prog++;
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	/*
3167c478bd9Sstevel@tonic-gate 	 * Don't depend on caller's umask
3177c478bd9Sstevel@tonic-gate 	 */
3187c478bd9Sstevel@tonic-gate 	(void) umask(0022);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate #if defined(__sparc)
3237c478bd9Sstevel@tonic-gate 	/*
3247c478bd9Sstevel@tonic-gate 	 * There are only two valid invocations of bootadm
3257c478bd9Sstevel@tonic-gate 	 * on SPARC:
3267c478bd9Sstevel@tonic-gate 	 *
3277c478bd9Sstevel@tonic-gate 	 *	- SPARC diskless server creating boot_archive for i386 clients
3287c478bd9Sstevel@tonic-gate 	 *	- archive creation call during reboot of a SPARC system
3297c478bd9Sstevel@tonic-gate 	 *
3307c478bd9Sstevel@tonic-gate 	 *	The latter should be a NOP
3317c478bd9Sstevel@tonic-gate 	 */
3327c478bd9Sstevel@tonic-gate 	if (bam_cmd != BAM_ARCHIVE) {
3337c478bd9Sstevel@tonic-gate 		sparc_abort();
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate #endif
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	switch (bam_cmd) {
3387c478bd9Sstevel@tonic-gate 		case BAM_MENU:
3397c478bd9Sstevel@tonic-gate 			ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv);
3407c478bd9Sstevel@tonic-gate 			break;
3417c478bd9Sstevel@tonic-gate 		case BAM_ARCHIVE:
3427c478bd9Sstevel@tonic-gate 			ret = bam_archive(bam_subcmd, bam_opt);
3437c478bd9Sstevel@tonic-gate 			break;
3447c478bd9Sstevel@tonic-gate 		default:
3457c478bd9Sstevel@tonic-gate 			usage();
3467c478bd9Sstevel@tonic-gate 			bam_exit(1);
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS)
3507c478bd9Sstevel@tonic-gate 		bam_exit(1);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	bam_unlock();
3537c478bd9Sstevel@tonic-gate 	return (0);
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate #if defined(__sparc)
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate static void
3597c478bd9Sstevel@tonic-gate sparc_abort(void)
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate 	bam_error(NOT_ON_SPARC);
3627c478bd9Sstevel@tonic-gate 	bam_exit(1);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate #endif
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate /*
3687c478bd9Sstevel@tonic-gate  * Equivalence of public and internal commands:
3697c478bd9Sstevel@tonic-gate  *	update-archive  -- -a update
3707c478bd9Sstevel@tonic-gate  *	list-archive	-- -a list
3717c478bd9Sstevel@tonic-gate  *	set-menu	-- -m set_option
3727c478bd9Sstevel@tonic-gate  *	list-menu	-- -m list_entry
3737c478bd9Sstevel@tonic-gate  *	update-menu	-- -m update_entry
3747c478bd9Sstevel@tonic-gate  */
3757c478bd9Sstevel@tonic-gate static struct cmd_map {
3767c478bd9Sstevel@tonic-gate 	char *bam_cmdname;
3777c478bd9Sstevel@tonic-gate 	int bam_cmd;
3787c478bd9Sstevel@tonic-gate 	char *bam_subcmd;
3797c478bd9Sstevel@tonic-gate } cmd_map[] = {
3807c478bd9Sstevel@tonic-gate 	{ "update-archive",	BAM_ARCHIVE,	"update"},
3817c478bd9Sstevel@tonic-gate 	{ "list-archive",	BAM_ARCHIVE,	"list"},
3827c478bd9Sstevel@tonic-gate 	{ "set-menu",		BAM_MENU,	"set_option"},
3837c478bd9Sstevel@tonic-gate 	{ "list-menu",		BAM_MENU,	"list_entry"},
3847c478bd9Sstevel@tonic-gate 	{ "update-menu",	BAM_MENU,	"update_entry"},
3857c478bd9Sstevel@tonic-gate 	{ NULL,			0,		NULL}
3867c478bd9Sstevel@tonic-gate };
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate  * Commands syntax published in bootadm(1M) are parsed here
3907c478bd9Sstevel@tonic-gate  */
3917c478bd9Sstevel@tonic-gate static void
3927c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[])
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	struct cmd_map *cmp = cmd_map;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/* command conforming to the final spec */
3977c478bd9Sstevel@tonic-gate 	if (argc > 1 && argv[1][0] != '-') {
3987c478bd9Sstevel@tonic-gate 		/*
3997c478bd9Sstevel@tonic-gate 		 * Map commands to internal table.
4007c478bd9Sstevel@tonic-gate 		 */
4017c478bd9Sstevel@tonic-gate 		while (cmp->bam_cmdname) {
4027c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], cmp->bam_cmdname) == 0) {
4037c478bd9Sstevel@tonic-gate 				bam_cmd = cmp->bam_cmd;
4047c478bd9Sstevel@tonic-gate 				bam_subcmd = cmp->bam_subcmd;
4057c478bd9Sstevel@tonic-gate 				break;
4067c478bd9Sstevel@tonic-gate 			}
4077c478bd9Sstevel@tonic-gate 			cmp++;
4087c478bd9Sstevel@tonic-gate 		}
4097c478bd9Sstevel@tonic-gate 		if (cmp->bam_cmdname == NULL) {
4107c478bd9Sstevel@tonic-gate 			usage();
4117c478bd9Sstevel@tonic-gate 			bam_exit(1);
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 		argc--;
4147c478bd9Sstevel@tonic-gate 		argv++;
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	parse_args_internal(argc, argv);
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate /*
4217c478bd9Sstevel@tonic-gate  * A combination of public and private commands are parsed here.
4227c478bd9Sstevel@tonic-gate  * The internal syntax and the corresponding functionality are:
4237c478bd9Sstevel@tonic-gate  *	-a update	-- update-archive
4247c478bd9Sstevel@tonic-gate  *	-a list		-- list-archive
4257c478bd9Sstevel@tonic-gate  *	-a update-all	-- (reboot to sync all mounted OS archive)
4267c478bd9Sstevel@tonic-gate  *	-m update_entry	-- update-menu
4277c478bd9Sstevel@tonic-gate  *	-m list_entry	-- list-menu
4287c478bd9Sstevel@tonic-gate  *	-m update_temp	-- (reboot -- [boot-args])
4297c478bd9Sstevel@tonic-gate  *	-m delete_all_entries -- (called from install)
4307c478bd9Sstevel@tonic-gate  */
4317c478bd9Sstevel@tonic-gate static void
4327c478bd9Sstevel@tonic-gate parse_args_internal(int argc, char *argv[])
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 	int c, error;
4357c478bd9Sstevel@tonic-gate 	extern char *optarg;
4367c478bd9Sstevel@tonic-gate 	extern int optind, opterr;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	/* Suppress error message from getopt */
4397c478bd9Sstevel@tonic-gate 	opterr = 0;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	error = 0;
4420d69385cSrscott 	while ((c = getopt(argc, argv, "a:d:fm:no:vCR:")) != -1) {
4437c478bd9Sstevel@tonic-gate 		switch (c) {
4447c478bd9Sstevel@tonic-gate 		case 'a':
4457c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
4467c478bd9Sstevel@tonic-gate 				error = 1;
4477c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
4487c478bd9Sstevel@tonic-gate 			}
4497c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_ARCHIVE;
4507c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
4517c478bd9Sstevel@tonic-gate 			break;
4527c478bd9Sstevel@tonic-gate 		case 'd':
4537c478bd9Sstevel@tonic-gate 			if (bam_debug) {
4547c478bd9Sstevel@tonic-gate 				error = 1;
4557c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4567c478bd9Sstevel@tonic-gate 			}
4577c478bd9Sstevel@tonic-gate 			bam_debug = s_strtol(optarg);
4587c478bd9Sstevel@tonic-gate 			break;
4597c478bd9Sstevel@tonic-gate 		case 'f':
4607c478bd9Sstevel@tonic-gate 			if (bam_force) {
4617c478bd9Sstevel@tonic-gate 				error = 1;
4627c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4637c478bd9Sstevel@tonic-gate 			}
4647c478bd9Sstevel@tonic-gate 			bam_force = 1;
4657c478bd9Sstevel@tonic-gate 			break;
4667c478bd9Sstevel@tonic-gate 		case 'm':
4677c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
4687c478bd9Sstevel@tonic-gate 				error = 1;
4697c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
4707c478bd9Sstevel@tonic-gate 			}
4717c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_MENU;
4727c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
4737c478bd9Sstevel@tonic-gate 			break;
4747c478bd9Sstevel@tonic-gate 		case 'n':
4757c478bd9Sstevel@tonic-gate 			if (bam_check) {
4767c478bd9Sstevel@tonic-gate 				error = 1;
4777c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4787c478bd9Sstevel@tonic-gate 			}
4797c478bd9Sstevel@tonic-gate 			bam_check = 1;
4807c478bd9Sstevel@tonic-gate 			break;
4817c478bd9Sstevel@tonic-gate 		case 'o':
4827c478bd9Sstevel@tonic-gate 			if (bam_opt) {
4837c478bd9Sstevel@tonic-gate 				error = 1;
4847c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4857c478bd9Sstevel@tonic-gate 			}
4867c478bd9Sstevel@tonic-gate 			bam_opt = optarg;
4877c478bd9Sstevel@tonic-gate 			break;
4887c478bd9Sstevel@tonic-gate 		case 'v':
4897c478bd9Sstevel@tonic-gate 			if (bam_verbose) {
4907c478bd9Sstevel@tonic-gate 				error = 1;
4917c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4927c478bd9Sstevel@tonic-gate 			}
4937c478bd9Sstevel@tonic-gate 			bam_verbose = 1;
4947c478bd9Sstevel@tonic-gate 			break;
4958c1b6884Sszhou 		case 'C':
4968c1b6884Sszhou 			bam_smf_check = 1;
4978c1b6884Sszhou 			break;
4987c478bd9Sstevel@tonic-gate 		case 'R':
4997c478bd9Sstevel@tonic-gate 			if (bam_root) {
5007c478bd9Sstevel@tonic-gate 				error = 1;
5017c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5027c478bd9Sstevel@tonic-gate 				break;
5037c478bd9Sstevel@tonic-gate 			} else if (realpath(optarg, rootbuf) == NULL) {
5047c478bd9Sstevel@tonic-gate 				error = 1;
5057c478bd9Sstevel@tonic-gate 				bam_error(CANT_RESOLVE, optarg,
5067c478bd9Sstevel@tonic-gate 				    strerror(errno));
5077c478bd9Sstevel@tonic-gate 				break;
5087c478bd9Sstevel@tonic-gate 			}
50940541d5dSvikram 			bam_alt_root = 1;
5107c478bd9Sstevel@tonic-gate 			bam_root = rootbuf;
5117c478bd9Sstevel@tonic-gate 			bam_rootlen = strlen(rootbuf);
5127c478bd9Sstevel@tonic-gate 			break;
5137c478bd9Sstevel@tonic-gate 		case '?':
5147c478bd9Sstevel@tonic-gate 			error = 1;
5157c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, optopt);
5167c478bd9Sstevel@tonic-gate 			break;
5177c478bd9Sstevel@tonic-gate 		default :
5187c478bd9Sstevel@tonic-gate 			error = 1;
5197c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, c);
5207c478bd9Sstevel@tonic-gate 			break;
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/*
5257c478bd9Sstevel@tonic-gate 	 * A command option must be specfied
5267c478bd9Sstevel@tonic-gate 	 */
5277c478bd9Sstevel@tonic-gate 	if (!bam_cmd) {
5287c478bd9Sstevel@tonic-gate 		if (bam_opt && strcmp(bam_opt, "all") == 0) {
5297c478bd9Sstevel@tonic-gate 			usage();
5307c478bd9Sstevel@tonic-gate 			bam_exit(0);
5317c478bd9Sstevel@tonic-gate 		}
5327c478bd9Sstevel@tonic-gate 		bam_error(NEED_CMD);
5337c478bd9Sstevel@tonic-gate 		error = 1;
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	if (error) {
5377c478bd9Sstevel@tonic-gate 		usage();
5387c478bd9Sstevel@tonic-gate 		bam_exit(1);
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if (optind > argc) {
5427c478bd9Sstevel@tonic-gate 		bam_error(INT_ERROR, "parse_args");
5437c478bd9Sstevel@tonic-gate 		bam_exit(1);
5447c478bd9Sstevel@tonic-gate 	} else if (optind < argc) {
5457c478bd9Sstevel@tonic-gate 		bam_argv = &argv[optind];
5467c478bd9Sstevel@tonic-gate 		bam_argc = argc - optind;
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	/*
5507c478bd9Sstevel@tonic-gate 	 * -n implies verbose mode
5517c478bd9Sstevel@tonic-gate 	 */
5527c478bd9Sstevel@tonic-gate 	if (bam_check)
5537c478bd9Sstevel@tonic-gate 		bam_verbose = 1;
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate static error_t
5577c478bd9Sstevel@tonic-gate check_subcmd_and_options(
5587c478bd9Sstevel@tonic-gate 	char *subcmd,
5597c478bd9Sstevel@tonic-gate 	char *opt,
5607c478bd9Sstevel@tonic-gate 	subcmd_defn_t *table,
5617c478bd9Sstevel@tonic-gate 	error_t (**fp)())
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate 	int i;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	if (subcmd == NULL) {
5667c478bd9Sstevel@tonic-gate 		bam_error(NEED_SUBCMD);
5677c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate 
5701a97e40eSvikram 	if (bam_argc != 0 || bam_argv) {
5711a97e40eSvikram 		if (strcmp(subcmd, "set_option") != 0 || bam_argc != 1) {
5721a97e40eSvikram 			bam_error(TRAILING_ARGS);
5731a97e40eSvikram 			usage();
5741a97e40eSvikram 			return (BAM_ERROR);
5751a97e40eSvikram 		}
5761a97e40eSvikram 	}
5771a97e40eSvikram 
5787c478bd9Sstevel@tonic-gate 	if (bam_root == NULL) {
5797c478bd9Sstevel@tonic-gate 		bam_root = rootbuf;
5807c478bd9Sstevel@tonic-gate 		bam_rootlen = 1;
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	/* verify that subcmd is valid */
5847c478bd9Sstevel@tonic-gate 	for (i = 0; table[i].subcmd != NULL; i++) {
5857c478bd9Sstevel@tonic-gate 		if (strcmp(table[i].subcmd, subcmd) == 0)
5867c478bd9Sstevel@tonic-gate 			break;
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	if (table[i].subcmd == NULL) {
5907c478bd9Sstevel@tonic-gate 		bam_error(INVALID_SUBCMD, subcmd);
5917c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5941a97e40eSvikram 	if (table[i].unpriv == 0 && geteuid() != 0) {
5951a97e40eSvikram 		bam_error(MUST_BE_ROOT);
5961a97e40eSvikram 		return (BAM_ERROR);
5971a97e40eSvikram 	}
5981a97e40eSvikram 
5991a97e40eSvikram 	/*
6001a97e40eSvikram 	 * Currently only privileged commands need a lock
6011a97e40eSvikram 	 */
6021a97e40eSvikram 	if (table[i].unpriv == 0)
6031a97e40eSvikram 		bam_lock();
6041a97e40eSvikram 
6057c478bd9Sstevel@tonic-gate 	/* subcmd verifies that opt is appropriate */
6067c478bd9Sstevel@tonic-gate 	if (table[i].option != OPT_OPTIONAL) {
6077c478bd9Sstevel@tonic-gate 		if ((table[i].option == OPT_REQ) ^ (opt != NULL)) {
6087c478bd9Sstevel@tonic-gate 			if (opt)
6097c478bd9Sstevel@tonic-gate 				bam_error(NO_OPT_REQ, subcmd);
6107c478bd9Sstevel@tonic-gate 			else
6117c478bd9Sstevel@tonic-gate 				bam_error(MISS_OPT, subcmd);
6127c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
6137c478bd9Sstevel@tonic-gate 		}
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	*fp = table[i].handler;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate 
621b610f78eSvikram 
622b610f78eSvikram static char *
62340541d5dSvikram mount_grub_slice(int *mnted, char **physlice, char **logslice, char **fs_type)
624b610f78eSvikram {
625b610f78eSvikram 	struct extmnttab mnt;
626b610f78eSvikram 	struct stat sb;
627b610f78eSvikram 	char buf[BAM_MAXLINE], dev[PATH_MAX], phys[PATH_MAX], fstype[32];
628f0f2c544Svikram 	char cmd[PATH_MAX];
629b610f78eSvikram 	char *mntpt;
630b610f78eSvikram 	int p, l, f;
631b610f78eSvikram 	FILE *fp;
632b610f78eSvikram 
633b610f78eSvikram 	assert(mnted);
634b610f78eSvikram 	*mnted = 0;
635b610f78eSvikram 
636b610f78eSvikram 	/*
63740541d5dSvikram 	 * physlice, logslice, fs_type  args may be NULL
638b610f78eSvikram 	 */
639b610f78eSvikram 	if (physlice)
640b610f78eSvikram 		*physlice = NULL;
64140541d5dSvikram 	if (logslice)
64240541d5dSvikram 		*logslice = NULL;
64340541d5dSvikram 	if (fs_type)
64440541d5dSvikram 		*fs_type = NULL;
645b610f78eSvikram 
646b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
647b610f78eSvikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
648b610f78eSvikram 		return (NULL);
649b610f78eSvikram 	}
650b610f78eSvikram 
651b610f78eSvikram 	fp = fopen(GRUB_slice, "r");
652b610f78eSvikram 	if (fp == NULL) {
653b610f78eSvikram 		bam_error(OPEN_FAIL, GRUB_slice, strerror(errno));
654b610f78eSvikram 		return (NULL);
655b610f78eSvikram 	}
656b610f78eSvikram 
657b610f78eSvikram 	dev[0] = fstype[0] = phys[0] = '\0';
658b610f78eSvikram 	p = sizeof ("PHYS_SLICE=") - 1;
659b610f78eSvikram 	l = sizeof ("LOG_SLICE=") - 1;
660b610f78eSvikram 	f = sizeof ("LOG_FSTYP=") - 1;
661b610f78eSvikram 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
662b610f78eSvikram 		if (strncmp(buf, "PHYS_SLICE=", p) == 0) {
663b610f78eSvikram 			(void) strlcpy(phys, buf + p, sizeof (phys));
664b610f78eSvikram 			continue;
665b610f78eSvikram 		}
666b610f78eSvikram 		if (strncmp(buf, "LOG_SLICE=", l) == 0) {
667b610f78eSvikram 			(void) strlcpy(dev, buf + l, sizeof (dev));
668b610f78eSvikram 			continue;
669b610f78eSvikram 		}
670b610f78eSvikram 		if (strncmp(buf, "LOG_FSTYP=", f) == 0) {
671b610f78eSvikram 			(void) strlcpy(fstype, buf + f, sizeof (fstype));
672b610f78eSvikram 			continue;
673b610f78eSvikram 		}
674b610f78eSvikram 	}
675b610f78eSvikram 	(void) fclose(fp);
676b610f78eSvikram 
677b610f78eSvikram 	if (dev[0] == '\0' || fstype[0] == '\0' || phys[0] == '\0') {
678b610f78eSvikram 		bam_error(BAD_SLICE_FILE, GRUB_slice);
679b610f78eSvikram 		return (NULL);
680b610f78eSvikram 	}
681b610f78eSvikram 
682b610f78eSvikram 	if (physlice) {
683b610f78eSvikram 		*physlice = s_strdup(phys);
684b610f78eSvikram 	}
68540541d5dSvikram 	if (logslice) {
68640541d5dSvikram 		*logslice = s_strdup(dev);
68740541d5dSvikram 	}
68840541d5dSvikram 	if (fs_type) {
68940541d5dSvikram 		*fs_type = s_strdup(fstype);
69040541d5dSvikram 	}
691b610f78eSvikram 
692b610f78eSvikram 	/*
693b610f78eSvikram 	 * Check if the slice is already mounted
694b610f78eSvikram 	 */
695b610f78eSvikram 	fp = fopen(MNTTAB, "r");
696b610f78eSvikram 	if (fp == NULL) {
697b610f78eSvikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
69840541d5dSvikram 		goto error;
699b610f78eSvikram 	}
700b610f78eSvikram 
701b610f78eSvikram 	resetmnttab(fp);
702b610f78eSvikram 
703b610f78eSvikram 	mntpt = NULL;
704b610f78eSvikram 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
705b610f78eSvikram 		if (strcmp(mnt.mnt_special, dev) == 0) {
706b610f78eSvikram 			mntpt = s_strdup(mnt.mnt_mountp);
707b610f78eSvikram 			break;
708b610f78eSvikram 		}
709b610f78eSvikram 	}
710b610f78eSvikram 
711b610f78eSvikram 	(void) fclose(fp);
712b610f78eSvikram 
713b610f78eSvikram 	if (mntpt) {
714b610f78eSvikram 		return (mntpt);
715b610f78eSvikram 	}
716b610f78eSvikram 
717b610f78eSvikram 
718b610f78eSvikram 	/*
719b610f78eSvikram 	 * GRUB slice is not mounted, we need to mount it now.
720b610f78eSvikram 	 * First create the mountpoint
721b610f78eSvikram 	 */
722b610f78eSvikram 	mntpt = s_calloc(1, PATH_MAX);
723b610f78eSvikram 	(void) snprintf(mntpt, PATH_MAX, "%s.%d", GRUB_slice_mntpt, getpid());
724b610f78eSvikram 	if (mkdir(mntpt, 0755) == -1 && errno != EEXIST) {
725b610f78eSvikram 		bam_error(MKDIR_FAILED, mntpt, strerror(errno));
726b610f78eSvikram 		free(mntpt);
72740541d5dSvikram 		goto error;
728b610f78eSvikram 	}
729b610f78eSvikram 
730f0f2c544Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/mount -F %s %s %s",
731f0f2c544Svikram 	    fstype, dev, mntpt);
732f0f2c544Svikram 
733f0f2c544Svikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
73440541d5dSvikram 		bam_error(MOUNT_FAILED, dev, fstype);
735b610f78eSvikram 		if (rmdir(mntpt) != 0) {
736b610f78eSvikram 			bam_error(RMDIR_FAILED, mntpt, strerror(errno));
737b610f78eSvikram 		}
738b610f78eSvikram 		free(mntpt);
73940541d5dSvikram 		goto error;
740b610f78eSvikram 	}
741b610f78eSvikram 
742b610f78eSvikram 	*mnted = 1;
743b610f78eSvikram 	return (mntpt);
74440541d5dSvikram 
74540541d5dSvikram error:
74640541d5dSvikram 	if (physlice) {
74740541d5dSvikram 		free(*physlice);
74840541d5dSvikram 		*physlice = NULL;
74940541d5dSvikram 	}
75040541d5dSvikram 	if (logslice) {
75140541d5dSvikram 		free(*logslice);
75240541d5dSvikram 		*logslice = NULL;
75340541d5dSvikram 	}
75440541d5dSvikram 	if (fs_type) {
75540541d5dSvikram 		free(*fs_type);
75640541d5dSvikram 		*fs_type = NULL;
75740541d5dSvikram 	}
75840541d5dSvikram 	return (NULL);
759b610f78eSvikram }
760b610f78eSvikram 
761b610f78eSvikram static void
76240541d5dSvikram umount_grub_slice(
76340541d5dSvikram 	int mnted,
76440541d5dSvikram 	char *mntpt,
76540541d5dSvikram 	char *physlice,
76640541d5dSvikram 	char *logslice,
76740541d5dSvikram 	char *fs_type)
768b610f78eSvikram {
769f0f2c544Svikram 	char cmd[PATH_MAX];
770f0f2c544Svikram 
771b610f78eSvikram 	/*
772b610f78eSvikram 	 * If we have not dealt with GRUB slice
773b610f78eSvikram 	 * we have nothing to do - just return.
774b610f78eSvikram 	 */
775b610f78eSvikram 	if (mntpt == NULL)
776b610f78eSvikram 		return;
777b610f78eSvikram 
778b610f78eSvikram 
779b610f78eSvikram 	/*
780b610f78eSvikram 	 * If we mounted the filesystem earlier in mount_grub_slice()
781b610f78eSvikram 	 * unmount it now.
782b610f78eSvikram 	 */
783b610f78eSvikram 	if (mnted) {
784f0f2c544Svikram 		(void) snprintf(cmd, sizeof (cmd), "/sbin/umount %s",
785f0f2c544Svikram 		    mntpt);
786f0f2c544Svikram 		if (exec_cmd(cmd, NULL, 0) != 0) {
787f0f2c544Svikram 			bam_error(UMOUNT_FAILED, mntpt);
788b610f78eSvikram 		}
789b610f78eSvikram 		if (rmdir(mntpt) != 0) {
790b610f78eSvikram 			bam_error(RMDIR_FAILED, mntpt, strerror(errno));
791b610f78eSvikram 		}
792b610f78eSvikram 	}
79340541d5dSvikram 
794b610f78eSvikram 	if (physlice)
795b610f78eSvikram 		free(physlice);
79640541d5dSvikram 	if (logslice)
79740541d5dSvikram 		free(logslice);
79840541d5dSvikram 	if (fs_type)
79940541d5dSvikram 		free(fs_type);
80040541d5dSvikram 
801b610f78eSvikram 	free(mntpt);
802b610f78eSvikram }
803b610f78eSvikram 
80440541d5dSvikram static char *
80540541d5dSvikram use_stubboot(void)
80640541d5dSvikram {
80740541d5dSvikram 	int mnted;
80840541d5dSvikram 	struct stat sb;
80940541d5dSvikram 	struct extmnttab mnt;
81040541d5dSvikram 	FILE *fp;
81140541d5dSvikram 	char cmd[PATH_MAX];
81240541d5dSvikram 
81340541d5dSvikram 	if (stat(STUBBOOT, &sb) != 0) {
81440541d5dSvikram 		bam_error(STUBBOOT_DIR_NOT_FOUND);
81540541d5dSvikram 		return (NULL);
81640541d5dSvikram 	}
81740541d5dSvikram 
81840541d5dSvikram 	/*
81940541d5dSvikram 	 * Check if stubboot is mounted. If not, mount it
82040541d5dSvikram 	 */
82140541d5dSvikram 	fp = fopen(MNTTAB, "r");
82240541d5dSvikram 	if (fp == NULL) {
82340541d5dSvikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
82440541d5dSvikram 		return (NULL);
82540541d5dSvikram 	}
82640541d5dSvikram 
82740541d5dSvikram 	resetmnttab(fp);
82840541d5dSvikram 
82940541d5dSvikram 	mnted = 0;
83040541d5dSvikram 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
83140541d5dSvikram 		if (strcmp(mnt.mnt_mountp, STUBBOOT) == 0) {
83240541d5dSvikram 			mnted = 1;
83340541d5dSvikram 			break;
83440541d5dSvikram 		}
83540541d5dSvikram 	}
83640541d5dSvikram 
83740541d5dSvikram 	(void) fclose(fp);
83840541d5dSvikram 
83940541d5dSvikram 	if (mnted)
84040541d5dSvikram 		return (STUBBOOT);
84140541d5dSvikram 
84240541d5dSvikram 	/*
84340541d5dSvikram 	 * Stubboot is not mounted, mount it now.
84440541d5dSvikram 	 * It should exist in /etc/vfstab
84540541d5dSvikram 	 */
84640541d5dSvikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/mount %s",
84740541d5dSvikram 	    STUBBOOT);
84840541d5dSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
84940541d5dSvikram 		bam_error(MOUNT_MNTPT_FAILED, STUBBOOT);
85040541d5dSvikram 		return (NULL);
85140541d5dSvikram 	}
85240541d5dSvikram 
85340541d5dSvikram 	return (STUBBOOT);
85440541d5dSvikram }
85540541d5dSvikram 
85640541d5dSvikram static void
85740541d5dSvikram disp_active_menu_locn(char *menu_path, char *logslice, char *fstype, int mnted)
85840541d5dSvikram {
85940541d5dSvikram 	/*
86040541d5dSvikram 	 * Check if we did a temp mount of an unmounted device.
86140541d5dSvikram 	 * If yes, print the block device and fstype for that device
86240541d5dSvikram 	 * else it is already mounted, so we print the path to the GRUB menu.
86340541d5dSvikram 	 */
86440541d5dSvikram 	if (mnted) {
86540541d5dSvikram 		bam_print(GRUB_MENU_DEVICE, logslice);
86640541d5dSvikram 		bam_print(GRUB_MENU_FSTYPE, fstype);
86740541d5dSvikram 	} else {
86840541d5dSvikram 		bam_print(GRUB_MENU_PATH, menu_path);
86940541d5dSvikram 	}
87040541d5dSvikram }
87140541d5dSvikram 
87240541d5dSvikram /*
87340541d5dSvikram  * NOTE: A single "/" is also considered a trailing slash and will
87440541d5dSvikram  * be deleted.
87540541d5dSvikram  */
87640541d5dSvikram static void
87740541d5dSvikram elide_trailing_slash(const char *src, char *dst, size_t dstsize)
87840541d5dSvikram {
87940541d5dSvikram 	size_t dstlen;
88040541d5dSvikram 
88140541d5dSvikram 	assert(src);
88240541d5dSvikram 	assert(dst);
88340541d5dSvikram 
88440541d5dSvikram 	(void) strlcpy(dst, src, dstsize);
88540541d5dSvikram 
88640541d5dSvikram 	dstlen = strlen(dst);
88740541d5dSvikram 	if (dst[dstlen - 1] == '/') {
88840541d5dSvikram 		dst[dstlen - 1] = '\0';
88940541d5dSvikram 	}
89040541d5dSvikram }
89140541d5dSvikram 
8927c478bd9Sstevel@tonic-gate static error_t
8937c478bd9Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[])
8947c478bd9Sstevel@tonic-gate {
8957c478bd9Sstevel@tonic-gate 	error_t ret;
8967c478bd9Sstevel@tonic-gate 	char menu_path[PATH_MAX];
897ae115bc7Smrj 	char path[PATH_MAX];
8987c478bd9Sstevel@tonic-gate 	menu_t *menu;
89940541d5dSvikram 	char *mntpt, *menu_root, *logslice, *fstype;
900b610f78eSvikram 	struct stat sb;
901b610f78eSvikram 	int mnted;	/* set if we did a mount */
9027c478bd9Sstevel@tonic-gate 	error_t (*f)(menu_t *mp, char *menu_path, char *opt);
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	/*
9057c478bd9Sstevel@tonic-gate 	 * Check arguments
9067c478bd9Sstevel@tonic-gate 	 */
9077c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
9087c478bd9Sstevel@tonic-gate 	if (ret == BAM_ERROR) {
9097c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 
912b610f78eSvikram 	mntpt = NULL;
913b610f78eSvikram 	mnted = 0;
91440541d5dSvikram 	logslice = fstype = NULL;
91540541d5dSvikram 
91640541d5dSvikram 	/*
917ae115bc7Smrj 	 * Check for the menu.list file:
918ae115bc7Smrj 	 *
919ae115bc7Smrj 	 * 1. Check for a GRUB_slice file, be it on / or
920ae115bc7Smrj 	 *    on the user-provided alternate root.
921ae115bc7Smrj 	 * 2. Use the alternate root, if given.
922ae115bc7Smrj 	 * 3. Check /stubboot
923ae115bc7Smrj 	 * 4. Use /
92440541d5dSvikram 	 */
92540541d5dSvikram 	if (bam_alt_root) {
926ae115bc7Smrj 		(void) snprintf(path, sizeof (path), "%s%s", bam_root,
927ae115bc7Smrj 		    GRUB_slice);
928ae115bc7Smrj 	} else {
929ae115bc7Smrj 		(void) snprintf(path, sizeof (path), "%s", GRUB_slice);
930ae115bc7Smrj 	}
931ae115bc7Smrj 
932ae115bc7Smrj 	if (stat(path, &sb) == 0) {
93340541d5dSvikram 		mntpt = mount_grub_slice(&mnted, NULL, &logslice, &fstype);
934b610f78eSvikram 		menu_root = mntpt;
935ae115bc7Smrj 	} else if (bam_alt_root) {
936ae115bc7Smrj 		menu_root = bam_root;
93740541d5dSvikram 	} else if (stat(STUBBOOT, &sb) == 0) {
93840541d5dSvikram 		menu_root = use_stubboot();
939b610f78eSvikram 	} else {
940b610f78eSvikram 		menu_root = bam_root;
941b610f78eSvikram 	}
942b610f78eSvikram 
94340541d5dSvikram 	if (menu_root == NULL) {
94440541d5dSvikram 		bam_error(CANNOT_LOCATE_GRUB_MENU);
94540541d5dSvikram 		return (BAM_ERROR);
94640541d5dSvikram 	}
94740541d5dSvikram 
94840541d5dSvikram 	elide_trailing_slash(menu_root, menu_path, sizeof (menu_path));
94940541d5dSvikram 	(void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path));
95040541d5dSvikram 
95140541d5dSvikram 	/*
95240541d5dSvikram 	 * If listing the menu, display the active menu
95340541d5dSvikram 	 * location
95440541d5dSvikram 	 */
95540541d5dSvikram 	if (strcmp(subcmd, "list_entry") == 0) {
95640541d5dSvikram 		disp_active_menu_locn(menu_path, logslice, fstype, mnted);
95740541d5dSvikram 	}
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	menu = menu_read(menu_path);
9607c478bd9Sstevel@tonic-gate 	assert(menu);
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	/*
9637c478bd9Sstevel@tonic-gate 	 * Special handling for setting timeout and default
9647c478bd9Sstevel@tonic-gate 	 */
9657c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "set_option") == 0) {
9667c478bd9Sstevel@tonic-gate 		if (largc != 1 || largv[0] == NULL) {
9677c478bd9Sstevel@tonic-gate 			usage();
96840541d5dSvikram 			menu_free(menu);
96940541d5dSvikram 			umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
9707c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
9717c478bd9Sstevel@tonic-gate 		}
9727c478bd9Sstevel@tonic-gate 		opt = largv[0];
9737c478bd9Sstevel@tonic-gate 	} else if (largc != 0) {
9747c478bd9Sstevel@tonic-gate 		usage();
97540541d5dSvikram 		menu_free(menu);
97640541d5dSvikram 		umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
9777c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
9787c478bd9Sstevel@tonic-gate 	}
9797c478bd9Sstevel@tonic-gate 
980ae115bc7Smrj 	ret = dboot_or_multiboot(bam_root);
981ae115bc7Smrj 	if (ret != BAM_SUCCESS)
982ae115bc7Smrj 		return (ret);
983ae115bc7Smrj 
9847c478bd9Sstevel@tonic-gate 	/*
9857c478bd9Sstevel@tonic-gate 	 * Once the sub-cmd handler has run
9867c478bd9Sstevel@tonic-gate 	 * only the line field is guaranteed to have valid values
9877c478bd9Sstevel@tonic-gate 	 */
988ae115bc7Smrj 	if ((strcmp(subcmd, "update_entry") == 0) ||
989ae115bc7Smrj 	    (strcmp(subcmd, "upgrade") == 0))
9907c478bd9Sstevel@tonic-gate 		ret = f(menu, bam_root, opt);
9917c478bd9Sstevel@tonic-gate 	else
9927c478bd9Sstevel@tonic-gate 		ret = f(menu, menu_path, opt);
9937c478bd9Sstevel@tonic-gate 	if (ret == BAM_WRITE) {
994b610f78eSvikram 		ret = menu_write(menu_root, menu);
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	menu_free(menu);
9987c478bd9Sstevel@tonic-gate 
99940541d5dSvikram 	umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
1000b610f78eSvikram 
10017c478bd9Sstevel@tonic-gate 	return (ret);
10027c478bd9Sstevel@tonic-gate }
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate static error_t
10067c478bd9Sstevel@tonic-gate bam_archive(
10077c478bd9Sstevel@tonic-gate 	char *subcmd,
10087c478bd9Sstevel@tonic-gate 	char *opt)
10097c478bd9Sstevel@tonic-gate {
10107c478bd9Sstevel@tonic-gate 	error_t ret;
10117c478bd9Sstevel@tonic-gate 	error_t (*f)(char *root, char *opt);
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	/*
10148c1b6884Sszhou 	 * Add trailing / for archive subcommands
10158c1b6884Sszhou 	 */
10168c1b6884Sszhou 	if (rootbuf[strlen(rootbuf) - 1] != '/')
10178c1b6884Sszhou 		(void) strcat(rootbuf, "/");
10188c1b6884Sszhou 	bam_rootlen = strlen(rootbuf);
10198c1b6884Sszhou 
10208c1b6884Sszhou 	/*
10217c478bd9Sstevel@tonic-gate 	 * Check arguments
10227c478bd9Sstevel@tonic-gate 	 */
10237c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f);
10247c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS) {
10257c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
10267c478bd9Sstevel@tonic-gate 	}
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate #if defined(__sparc)
10297c478bd9Sstevel@tonic-gate 	/*
10307c478bd9Sstevel@tonic-gate 	 * A NOP if called on SPARC during reboot
10317c478bd9Sstevel@tonic-gate 	 */
10327c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "update_all") == 0)
10337c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
10347c478bd9Sstevel@tonic-gate 	else if (strcmp(subcmd, "update") != 0)
10357c478bd9Sstevel@tonic-gate 		sparc_abort();
10367c478bd9Sstevel@tonic-gate #endif
10377c478bd9Sstevel@tonic-gate 
1038ae115bc7Smrj 	ret = dboot_or_multiboot(rootbuf);
1039ae115bc7Smrj 	if (ret != BAM_SUCCESS)
1040ae115bc7Smrj 		return (ret);
1041ae115bc7Smrj 
10427c478bd9Sstevel@tonic-gate 	/*
10437c478bd9Sstevel@tonic-gate 	 * Check archive not supported with update_all
10447c478bd9Sstevel@tonic-gate 	 * since it is awkward to display out-of-sync
10457c478bd9Sstevel@tonic-gate 	 * information for each BE.
10467c478bd9Sstevel@tonic-gate 	 */
10477c478bd9Sstevel@tonic-gate 	if (bam_check && strcmp(subcmd, "update_all") == 0) {
10487c478bd9Sstevel@tonic-gate 		bam_error(CHECK_NOT_SUPPORTED, subcmd);
10497c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
10507c478bd9Sstevel@tonic-gate 	}
10517c478bd9Sstevel@tonic-gate 
1052b610f78eSvikram 	if (strcmp(subcmd, "update_all") == 0)
1053b610f78eSvikram 		bam_update_all = 1;
1054b610f78eSvikram 
1055b610f78eSvikram 	ret = f(bam_root, opt);
1056b610f78eSvikram 
1057b610f78eSvikram 	bam_update_all = 0;
1058b610f78eSvikram 
1059b610f78eSvikram 	return (ret);
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1063ae115bc7Smrj void
10647c478bd9Sstevel@tonic-gate bam_error(char *format, ...)
10657c478bd9Sstevel@tonic-gate {
10667c478bd9Sstevel@tonic-gate 	va_list ap;
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	va_start(ap, format);
10697c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", prog);
10707c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
10717c478bd9Sstevel@tonic-gate 	va_end(ap);
10727c478bd9Sstevel@tonic-gate }
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
10757c478bd9Sstevel@tonic-gate static void
10767c478bd9Sstevel@tonic-gate bam_print(char *format, ...)
10777c478bd9Sstevel@tonic-gate {
10787c478bd9Sstevel@tonic-gate 	va_list ap;
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	va_start(ap, format);
10817c478bd9Sstevel@tonic-gate 	(void) vfprintf(stdout, format, ap);
10827c478bd9Sstevel@tonic-gate 	va_end(ap);
10837c478bd9Sstevel@tonic-gate }
10847c478bd9Sstevel@tonic-gate 
1085ae115bc7Smrj /*PRINTFLIKE1*/
1086ae115bc7Smrj void
1087ae115bc7Smrj bam_print_stderr(char *format, ...)
1088ae115bc7Smrj {
1089ae115bc7Smrj 	va_list ap;
1090ae115bc7Smrj 
1091ae115bc7Smrj 	va_start(ap, format);
1092ae115bc7Smrj 	(void) vfprintf(stderr, format, ap);
1093ae115bc7Smrj 	va_end(ap);
1094ae115bc7Smrj }
1095ae115bc7Smrj 
10967c478bd9Sstevel@tonic-gate static void
10977c478bd9Sstevel@tonic-gate bam_exit(int excode)
10987c478bd9Sstevel@tonic-gate {
10997c478bd9Sstevel@tonic-gate 	bam_unlock();
11007c478bd9Sstevel@tonic-gate 	exit(excode);
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate static void
11047c478bd9Sstevel@tonic-gate bam_lock(void)
11057c478bd9Sstevel@tonic-gate {
11067c478bd9Sstevel@tonic-gate 	struct flock lock;
11077c478bd9Sstevel@tonic-gate 	pid_t pid;
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS);
11107c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
11117c478bd9Sstevel@tonic-gate 		/*
11127c478bd9Sstevel@tonic-gate 		 * We may be invoked early in boot for archive verification.
11137c478bd9Sstevel@tonic-gate 		 * In this case, root is readonly and /var/run may not exist.
11147c478bd9Sstevel@tonic-gate 		 * Proceed without the lock
11157c478bd9Sstevel@tonic-gate 		 */
11167c478bd9Sstevel@tonic-gate 		if (errno == EROFS || errno == ENOENT) {
11177c478bd9Sstevel@tonic-gate 			bam_root_readonly = 1;
11187c478bd9Sstevel@tonic-gate 			return;
11197c478bd9Sstevel@tonic-gate 		}
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno));
11227c478bd9Sstevel@tonic-gate 		bam_exit(1);
11237c478bd9Sstevel@tonic-gate 	}
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
11267c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
11277c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
11287c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) {
11317c478bd9Sstevel@tonic-gate 		if (errno != EACCES && errno != EAGAIN) {
11327c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11337c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
11347c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
11357c478bd9Sstevel@tonic-gate 			bam_exit(1);
11367c478bd9Sstevel@tonic-gate 		}
11377c478bd9Sstevel@tonic-gate 		pid = 0;
11387c478bd9Sstevel@tonic-gate 		(void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0);
11397c478bd9Sstevel@tonic-gate 		bam_print(FILE_LOCKED, pid);
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 		lock.l_type = F_WRLCK;
11427c478bd9Sstevel@tonic-gate 		lock.l_whence = SEEK_SET;
11437c478bd9Sstevel@tonic-gate 		lock.l_start = 0;
11447c478bd9Sstevel@tonic-gate 		lock.l_len = 0;
11457c478bd9Sstevel@tonic-gate 		if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) {
11467c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11477c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
11487c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
11497c478bd9Sstevel@tonic-gate 			bam_exit(1);
11507c478bd9Sstevel@tonic-gate 		}
11517c478bd9Sstevel@tonic-gate 	}
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	/* We own the lock now */
11547c478bd9Sstevel@tonic-gate 	pid = getpid();
11557c478bd9Sstevel@tonic-gate 	(void) write(bam_lock_fd, &pid, sizeof (pid));
11567c478bd9Sstevel@tonic-gate }
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate static void
11597c478bd9Sstevel@tonic-gate bam_unlock(void)
11607c478bd9Sstevel@tonic-gate {
11617c478bd9Sstevel@tonic-gate 	struct flock unlock;
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	/*
11647c478bd9Sstevel@tonic-gate 	 * NOP if we don't hold the lock
11657c478bd9Sstevel@tonic-gate 	 */
11667c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
11677c478bd9Sstevel@tonic-gate 		return;
11687c478bd9Sstevel@tonic-gate 	}
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
11717c478bd9Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
11727c478bd9Sstevel@tonic-gate 	unlock.l_start = 0;
11737c478bd9Sstevel@tonic-gate 	unlock.l_len = 0;
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) {
11767c478bd9Sstevel@tonic-gate 		bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	if (close(bam_lock_fd) == -1) {
11807c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno));
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 	bam_lock_fd = -1;
11837c478bd9Sstevel@tonic-gate }
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate static error_t
11867c478bd9Sstevel@tonic-gate list_archive(char *root, char *opt)
11877c478bd9Sstevel@tonic-gate {
11887c478bd9Sstevel@tonic-gate 	filelist_t flist;
11897c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
11907c478bd9Sstevel@tonic-gate 	line_t *lp;
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	assert(root);
11937c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
11967c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
11977c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
11987c478bd9Sstevel@tonic-gate 	}
11997c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
12027c478bd9Sstevel@tonic-gate 		bam_print(PRINT, lp->line);
12037c478bd9Sstevel@tonic-gate 	}
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
12087c478bd9Sstevel@tonic-gate }
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate /*
12117c478bd9Sstevel@tonic-gate  * This routine writes a list of lines to a file.
12127c478bd9Sstevel@tonic-gate  * The list is *not* freed
12137c478bd9Sstevel@tonic-gate  */
12147c478bd9Sstevel@tonic-gate static error_t
12157c478bd9Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start)
12167c478bd9Sstevel@tonic-gate {
12177c478bd9Sstevel@tonic-gate 	char tmpfile[PATH_MAX];
12187c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
12197c478bd9Sstevel@tonic-gate 	FILE *fp;
12207c478bd9Sstevel@tonic-gate 	int ret;
12217c478bd9Sstevel@tonic-gate 	struct stat sb;
12227c478bd9Sstevel@tonic-gate 	mode_t mode;
12237c478bd9Sstevel@tonic-gate 	uid_t root_uid;
12247c478bd9Sstevel@tonic-gate 	gid_t sys_gid;
12257c478bd9Sstevel@tonic-gate 	struct passwd *pw;
12267c478bd9Sstevel@tonic-gate 	struct group *gp;
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, final);
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	if (start == NULL) {
12327c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != -1) {
12337c478bd9Sstevel@tonic-gate 			bam_print(UNLINK_EMPTY, path);
12347c478bd9Sstevel@tonic-gate 			if (unlink(path) != 0) {
12357c478bd9Sstevel@tonic-gate 				bam_error(UNLINK_FAIL, path, strerror(errno));
12367c478bd9Sstevel@tonic-gate 				return (BAM_ERROR);
12377c478bd9Sstevel@tonic-gate 			} else {
12387c478bd9Sstevel@tonic-gate 				return (BAM_SUCCESS);
12397c478bd9Sstevel@tonic-gate 			}
12407c478bd9Sstevel@tonic-gate 		}
12417c478bd9Sstevel@tonic-gate 	}
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	/*
12447c478bd9Sstevel@tonic-gate 	 * Preserve attributes of existing file if possible,
12457c478bd9Sstevel@tonic-gate 	 * otherwise ask the system for uid/gid of root/sys.
12467c478bd9Sstevel@tonic-gate 	 * If all fails, fall back on hard-coded defaults.
12477c478bd9Sstevel@tonic-gate 	 */
12487c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != -1) {
12497c478bd9Sstevel@tonic-gate 		mode = sb.st_mode;
12507c478bd9Sstevel@tonic-gate 		root_uid = sb.st_uid;
12517c478bd9Sstevel@tonic-gate 		sys_gid = sb.st_gid;
12527c478bd9Sstevel@tonic-gate 	} else {
12537c478bd9Sstevel@tonic-gate 		mode = DEFAULT_DEV_MODE;
12547c478bd9Sstevel@tonic-gate 		if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
12557c478bd9Sstevel@tonic-gate 			root_uid = pw->pw_uid;
12567c478bd9Sstevel@tonic-gate 		} else {
12577c478bd9Sstevel@tonic-gate 			if (bam_verbose)
12587c478bd9Sstevel@tonic-gate 				bam_error(CANT_FIND_USER,
12597c478bd9Sstevel@tonic-gate 				    DEFAULT_DEV_USER, DEFAULT_DEV_UID);
12607c478bd9Sstevel@tonic-gate 			root_uid = (uid_t)DEFAULT_DEV_UID;
12617c478bd9Sstevel@tonic-gate 		}
12627c478bd9Sstevel@tonic-gate 		if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
12637c478bd9Sstevel@tonic-gate 			sys_gid = gp->gr_gid;
12647c478bd9Sstevel@tonic-gate 		} else {
12657c478bd9Sstevel@tonic-gate 			if (bam_verbose)
12667c478bd9Sstevel@tonic-gate 				bam_error(CANT_FIND_GROUP,
12677c478bd9Sstevel@tonic-gate 				    DEFAULT_DEV_GROUP, DEFAULT_DEV_GID);
12687c478bd9Sstevel@tonic-gate 			sys_gid = (gid_t)DEFAULT_DEV_GID;
12697c478bd9Sstevel@tonic-gate 		}
12707c478bd9Sstevel@tonic-gate 	}
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	(void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp);
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	/* Truncate tmpfile first */
12757c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "w");
12767c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
12777c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
12787c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12797c478bd9Sstevel@tonic-gate 	}
12807c478bd9Sstevel@tonic-gate 	ret = fclose(fp);
12817c478bd9Sstevel@tonic-gate 	if (ret == EOF) {
12827c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
12837c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12847c478bd9Sstevel@tonic-gate 	}
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	/* Now open it in append mode */
12877c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "a");
12887c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
12897c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
12907c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12917c478bd9Sstevel@tonic-gate 	}
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	for (; start; start = start->next) {
12947c478bd9Sstevel@tonic-gate 		ret = s_fputs(start->line, fp);
12957c478bd9Sstevel@tonic-gate 		if (ret == EOF) {
12967c478bd9Sstevel@tonic-gate 			bam_error(WRITE_FAIL, tmpfile, strerror(errno));
12977c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
12987c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
12997c478bd9Sstevel@tonic-gate 		}
13007c478bd9Sstevel@tonic-gate 	}
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 	ret = fclose(fp);
13037c478bd9Sstevel@tonic-gate 	if (ret == EOF) {
13047c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
13057c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13067c478bd9Sstevel@tonic-gate 	}
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	/*
1309de531be4Sjg 	 * Set up desired attributes.  Ignore failures on filesystems
1310de531be4Sjg 	 * not supporting these operations - pcfs reports unsupported
1311de531be4Sjg 	 * operations as EINVAL.
13127c478bd9Sstevel@tonic-gate 	 */
13137c478bd9Sstevel@tonic-gate 	ret = chmod(tmpfile, mode);
1314de531be4Sjg 	if (ret == -1 &&
1315de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
13167c478bd9Sstevel@tonic-gate 		bam_error(CHMOD_FAIL, tmpfile, strerror(errno));
13177c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13187c478bd9Sstevel@tonic-gate 	}
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	ret = chown(tmpfile, root_uid, sys_gid);
1321de531be4Sjg 	if (ret == -1 &&
1322de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
13237c478bd9Sstevel@tonic-gate 		bam_error(CHOWN_FAIL, tmpfile, strerror(errno));
13247c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13257c478bd9Sstevel@tonic-gate 	}
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 	/*
13297c478bd9Sstevel@tonic-gate 	 * Do an atomic rename
13307c478bd9Sstevel@tonic-gate 	 */
13317c478bd9Sstevel@tonic-gate 	ret = rename(tmpfile, path);
13327c478bd9Sstevel@tonic-gate 	if (ret != 0) {
13337c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path, strerror(errno));
13347c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13357c478bd9Sstevel@tonic-gate 	}
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
13387c478bd9Sstevel@tonic-gate }
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate /*
13417c478bd9Sstevel@tonic-gate  * This function should always return 0 - since we want
13427c478bd9Sstevel@tonic-gate  * to create stat data for *all* files in the list.
13437c478bd9Sstevel@tonic-gate  */
13447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13457c478bd9Sstevel@tonic-gate static int
13467c478bd9Sstevel@tonic-gate cmpstat(
13477c478bd9Sstevel@tonic-gate 	const char *file,
13487c478bd9Sstevel@tonic-gate 	const struct stat *stat,
13497c478bd9Sstevel@tonic-gate 	int flags,
13507c478bd9Sstevel@tonic-gate 	struct FTW *ftw)
13517c478bd9Sstevel@tonic-gate {
13527c478bd9Sstevel@tonic-gate 	uint_t sz;
13537c478bd9Sstevel@tonic-gate 	uint64_t *value;
13547c478bd9Sstevel@tonic-gate 	uint64_t filestat[2];
13557c478bd9Sstevel@tonic-gate 	int error;
13567c478bd9Sstevel@tonic-gate 
135758091fd8Ssetje 	struct safefile *safefilep;
135858091fd8Ssetje 	FILE *fp;
135958091fd8Ssetje 
13607c478bd9Sstevel@tonic-gate 	/*
13617c478bd9Sstevel@tonic-gate 	 * We only want regular files
13627c478bd9Sstevel@tonic-gate 	 */
13637c478bd9Sstevel@tonic-gate 	if (!S_ISREG(stat->st_mode))
13647c478bd9Sstevel@tonic-gate 		return (0);
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 	/*
13677c478bd9Sstevel@tonic-gate 	 * new_nvlp may be NULL if there were errors earlier
13687c478bd9Sstevel@tonic-gate 	 * but this is not fatal to update determination.
13697c478bd9Sstevel@tonic-gate 	 */
13707c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp) {
13717c478bd9Sstevel@tonic-gate 		filestat[0] = stat->st_size;
13727c478bd9Sstevel@tonic-gate 		filestat[1] = stat->st_mtime;
13737c478bd9Sstevel@tonic-gate 		error = nvlist_add_uint64_array(walk_arg.new_nvlp,
13747c478bd9Sstevel@tonic-gate 		    file + bam_rootlen, filestat, 2);
13757c478bd9Sstevel@tonic-gate 		if (error)
13767c478bd9Sstevel@tonic-gate 			bam_error(NVADD_FAIL, file, strerror(error));
13777c478bd9Sstevel@tonic-gate 	}
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 	/*
13807c478bd9Sstevel@tonic-gate 	 * The remaining steps are only required if we haven't made a
13817c478bd9Sstevel@tonic-gate 	 * decision about update or if we are checking (-n)
13827c478bd9Sstevel@tonic-gate 	 */
13837c478bd9Sstevel@tonic-gate 	if (walk_arg.need_update && !bam_check)
13847c478bd9Sstevel@tonic-gate 		return (0);
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	/*
138758091fd8Ssetje 	 * If we are invoked as part of system/filesyste/boot-archive, then
138858091fd8Ssetje 	 * there are a number of things we should not worry about
13897c478bd9Sstevel@tonic-gate 	 */
139058091fd8Ssetje 	if (bam_smf_check) {
139158091fd8Ssetje 		/* ignore amd64 modules unless we are booted amd64. */
139258091fd8Ssetje 		if (!is_amd64() && strstr(file, "/amd64/") != 0)
13937c478bd9Sstevel@tonic-gate 			return (0);
13947c478bd9Sstevel@tonic-gate 
139558091fd8Ssetje 		/* read in list of safe files */
139658091fd8Ssetje 		if (safefiles == NULL)
139758091fd8Ssetje 			if (fp = fopen("/boot/solaris/filelist.safe", "r")) {
139858091fd8Ssetje 				safefiles = s_calloc(1,
139958091fd8Ssetje 				    sizeof (struct safefile));
140058091fd8Ssetje 				safefilep = safefiles;
140158091fd8Ssetje 				safefilep->name = s_calloc(1, MAXPATHLEN +
140258091fd8Ssetje 				    MAXNAMELEN);
140358091fd8Ssetje 				safefilep->next = NULL;
140458091fd8Ssetje 				while (s_fgets(safefilep->name, MAXPATHLEN +
140558091fd8Ssetje 				    MAXNAMELEN, fp) != NULL) {
140658091fd8Ssetje 					safefilep->next = s_calloc(1,
140758091fd8Ssetje 					    sizeof (struct safefile));
140858091fd8Ssetje 					safefilep = safefilep->next;
140958091fd8Ssetje 					safefilep->name = s_calloc(1,
141058091fd8Ssetje 					    MAXPATHLEN + MAXNAMELEN);
141158091fd8Ssetje 					safefilep->next = NULL;
141258091fd8Ssetje 				}
141358091fd8Ssetje 				(void) fclose(fp);
141458091fd8Ssetje 			}
141558091fd8Ssetje 	}
141658091fd8Ssetje 
14177c478bd9Sstevel@tonic-gate 	/*
14187c478bd9Sstevel@tonic-gate 	 * We need an update if file doesn't exist in old archive
14197c478bd9Sstevel@tonic-gate 	 */
14207c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp == NULL ||
14217c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint64_array(walk_arg.old_nvlp,
14227c478bd9Sstevel@tonic-gate 	    file + bam_rootlen, &value, &sz) != 0) {
14237c478bd9Sstevel@tonic-gate 		if (bam_smf_check)	/* ignore new during smf check */
14247c478bd9Sstevel@tonic-gate 			return (0);
14257c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
14267c478bd9Sstevel@tonic-gate 		if (bam_verbose)
14277c478bd9Sstevel@tonic-gate 			bam_print(PARSEABLE_NEW_FILE, file);
14287c478bd9Sstevel@tonic-gate 		return (0);
14297c478bd9Sstevel@tonic-gate 	}
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	/*
14327c478bd9Sstevel@tonic-gate 	 * File exists in old archive. Check if file has changed
14337c478bd9Sstevel@tonic-gate 	 */
14347c478bd9Sstevel@tonic-gate 	assert(sz == 2);
14357c478bd9Sstevel@tonic-gate 	bcopy(value, filestat, sizeof (filestat));
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	if (filestat[0] != stat->st_size ||
14387c478bd9Sstevel@tonic-gate 	    filestat[1] != stat->st_mtime) {
14397c609327Ssetje 		if (bam_smf_check) {
14407c609327Ssetje 			safefilep = safefiles;
14417c609327Ssetje 			while (safefilep != NULL) {
14427c609327Ssetje 				if (strcmp(file + bam_rootlen,
14437c609327Ssetje 				    safefilep->name) == 0) {
14447c609327Ssetje 					(void) creat(NEED_UPDATE_FILE, 0644);
14457c609327Ssetje 					return (0);
14467c609327Ssetje 				}
14477c609327Ssetje 				safefilep = safefilep->next;
14487c609327Ssetje 			}
14497c609327Ssetje 		}
14507c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
14517c478bd9Sstevel@tonic-gate 		if (bam_verbose)
14527c478bd9Sstevel@tonic-gate 			if (bam_smf_check)
14537c478bd9Sstevel@tonic-gate 				bam_print("    %s\n", file);
14547c478bd9Sstevel@tonic-gate 			else
14557c478bd9Sstevel@tonic-gate 				bam_print(PARSEABLE_OUT_DATE, file);
14567c478bd9Sstevel@tonic-gate 	}
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	return (0);
14597c478bd9Sstevel@tonic-gate }
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate /*
14627c478bd9Sstevel@tonic-gate  * Check flags and presence of required files.
14637c478bd9Sstevel@tonic-gate  * The force flag and/or absence of files should
14647c478bd9Sstevel@tonic-gate  * trigger an update.
14657c478bd9Sstevel@tonic-gate  * Suppress stdout output if check (-n) option is set
14667c478bd9Sstevel@tonic-gate  * (as -n should only produce parseable output.)
14677c478bd9Sstevel@tonic-gate  */
14687c478bd9Sstevel@tonic-gate static void
14697c478bd9Sstevel@tonic-gate check_flags_and_files(char *root)
14707c478bd9Sstevel@tonic-gate {
14717c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
14727c478bd9Sstevel@tonic-gate 	struct stat sb;
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	/*
14757c478bd9Sstevel@tonic-gate 	 * if force, create archive unconditionally
14767c478bd9Sstevel@tonic-gate 	 */
14777c478bd9Sstevel@tonic-gate 	if (bam_force) {
14787c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
14797c478bd9Sstevel@tonic-gate 		if (bam_verbose && !bam_check)
14807c478bd9Sstevel@tonic-gate 			bam_print(UPDATE_FORCE);
14817c478bd9Sstevel@tonic-gate 		return;
14827c478bd9Sstevel@tonic-gate 	}
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	/*
14857c478bd9Sstevel@tonic-gate 	 * If archive is missing, create archive
14867c478bd9Sstevel@tonic-gate 	 */
1487ae115bc7Smrj 	(void) snprintf(path, sizeof (path), "%s%s", root,
1488ae115bc7Smrj 	    DIRECT_BOOT_ARCHIVE_32);
14897c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
14907c478bd9Sstevel@tonic-gate 		if (bam_verbose && !bam_check)
14917c478bd9Sstevel@tonic-gate 			bam_print(UPDATE_ARCH_MISS, path);
14927c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
14937c478bd9Sstevel@tonic-gate 		return;
14947c478bd9Sstevel@tonic-gate 	}
1495ae115bc7Smrj 	if (bam_direct == BAM_DIRECT_DBOOT) {
1496ae115bc7Smrj 		(void) snprintf(path, sizeof (path), "%s%s", root,
1497ae115bc7Smrj 		    DIRECT_BOOT_ARCHIVE_64);
1498ae115bc7Smrj 		if (stat(path, &sb) != 0) {
1499ae115bc7Smrj 			if (bam_verbose && !bam_check)
1500ae115bc7Smrj 				bam_print(UPDATE_ARCH_MISS, path);
1501ae115bc7Smrj 			walk_arg.need_update = 1;
1502ae115bc7Smrj 			return;
1503ae115bc7Smrj 		}
1504ae115bc7Smrj 	}
15057c478bd9Sstevel@tonic-gate }
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate static error_t
15087c478bd9Sstevel@tonic-gate read_one_list(char *root, filelist_t  *flistp, char *filelist)
15097c478bd9Sstevel@tonic-gate {
15107c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
15117c478bd9Sstevel@tonic-gate 	FILE *fp;
15127c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE];
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, filelist);
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	fp = fopen(path, "r");
15177c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
15187c478bd9Sstevel@tonic-gate 		if (bam_debug)
15197c478bd9Sstevel@tonic-gate 			bam_error(FLIST_FAIL, path, strerror(errno));
15207c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15217c478bd9Sstevel@tonic-gate 	}
15227c478bd9Sstevel@tonic-gate 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
1523b610f78eSvikram 		/* skip blank lines */
1524b610f78eSvikram 		if (strspn(buf, " \t") == strlen(buf))
1525b610f78eSvikram 			continue;
15267c478bd9Sstevel@tonic-gate 		append_to_flist(flistp, buf);
15277c478bd9Sstevel@tonic-gate 	}
15287c478bd9Sstevel@tonic-gate 	if (fclose(fp) != 0) {
15297c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, path, strerror(errno));
15307c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15317c478bd9Sstevel@tonic-gate 	}
15327c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
15337c478bd9Sstevel@tonic-gate }
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate static error_t
15367c478bd9Sstevel@tonic-gate read_list(char *root, filelist_t  *flistp)
15377c478bd9Sstevel@tonic-gate {
15387c478bd9Sstevel@tonic-gate 	int rval;
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 	/*
15437c478bd9Sstevel@tonic-gate 	 * Read current lists of files - only the first is mandatory
15447c478bd9Sstevel@tonic-gate 	 */
15457c478bd9Sstevel@tonic-gate 	rval = read_one_list(root, flistp, BOOT_FILE_LIST);
15467c478bd9Sstevel@tonic-gate 	if (rval != BAM_SUCCESS)
15477c478bd9Sstevel@tonic-gate 		return (rval);
15487c478bd9Sstevel@tonic-gate 	(void) read_one_list(root, flistp, ETC_FILE_LIST);
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL) {
15517c478bd9Sstevel@tonic-gate 		bam_error(NO_FLIST);
15527c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15537c478bd9Sstevel@tonic-gate 	}
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
15567c478bd9Sstevel@tonic-gate }
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate static void
15597c478bd9Sstevel@tonic-gate getoldstat(char *root)
15607c478bd9Sstevel@tonic-gate {
15617c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
15627c478bd9Sstevel@tonic-gate 	int fd, error;
15637c478bd9Sstevel@tonic-gate 	struct stat sb;
15647c478bd9Sstevel@tonic-gate 	char *ostat;
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
15677c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDONLY);
15687c478bd9Sstevel@tonic-gate 	if (fd == -1) {
15697c478bd9Sstevel@tonic-gate 		if (bam_verbose)
15707c478bd9Sstevel@tonic-gate 			bam_print(OPEN_FAIL, path, strerror(errno));
15717c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
15727c478bd9Sstevel@tonic-gate 		return;
15737c478bd9Sstevel@tonic-gate 	}
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	if (fstat(fd, &sb) != 0) {
15767c478bd9Sstevel@tonic-gate 		bam_error(STAT_FAIL, path, strerror(errno));
15777c478bd9Sstevel@tonic-gate 		(void) close(fd);
15787c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
15797c478bd9Sstevel@tonic-gate 		return;
15807c478bd9Sstevel@tonic-gate 	}
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 	ostat = s_calloc(1, sb.st_size);
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	if (read(fd, ostat, sb.st_size) != sb.st_size) {
15857c478bd9Sstevel@tonic-gate 		bam_error(READ_FAIL, path, strerror(errno));
15867c478bd9Sstevel@tonic-gate 		(void) close(fd);
15877c478bd9Sstevel@tonic-gate 		free(ostat);
15887c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
15897c478bd9Sstevel@tonic-gate 		return;
15907c478bd9Sstevel@tonic-gate 	}
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	(void) close(fd);
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
15957c478bd9Sstevel@tonic-gate 	error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0);
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	free(ostat);
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	if (error) {
16007c478bd9Sstevel@tonic-gate 		bam_error(UNPACK_FAIL, path, strerror(error));
16017c478bd9Sstevel@tonic-gate 		walk_arg.old_nvlp = NULL;
16027c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
16037c478bd9Sstevel@tonic-gate 		return;
16047c478bd9Sstevel@tonic-gate 	}
16057c478bd9Sstevel@tonic-gate }
16067c478bd9Sstevel@tonic-gate 
1607a28d77b8Svikram /*
1608a28d77b8Svikram  * Checks if a file in the current (old) archive has
1609a28d77b8Svikram  * been deleted from the root filesystem. This is needed for
1610a28d77b8Svikram  * software like Trusted Extensions (TX) that switch early
1611a28d77b8Svikram  * in boot based on presence/absence of a kernel module.
1612a28d77b8Svikram  */
1613a28d77b8Svikram static void
1614a28d77b8Svikram check4stale(char *root)
1615a28d77b8Svikram {
1616a28d77b8Svikram 	nvpair_t	*nvp;
1617a28d77b8Svikram 	nvlist_t	*nvlp;
1618a28d77b8Svikram 	char 		*file;
1619a28d77b8Svikram 	char		path[PATH_MAX];
1620a28d77b8Svikram 	struct stat	sb;
1621a28d77b8Svikram 
1622a28d77b8Svikram 	/*
1623a28d77b8Svikram 	 * Skip stale file check during smf check
1624a28d77b8Svikram 	 */
1625a28d77b8Svikram 	if (bam_smf_check)
1626a28d77b8Svikram 		return;
1627a28d77b8Svikram 
1628a28d77b8Svikram 	/* Nothing to do if no old stats */
1629a28d77b8Svikram 	if ((nvlp = walk_arg.old_nvlp) == NULL)
1630a28d77b8Svikram 		return;
1631a28d77b8Svikram 
1632a28d77b8Svikram 	for (nvp = nvlist_next_nvpair(nvlp, NULL); nvp;
1633a28d77b8Svikram 	    nvp = nvlist_next_nvpair(nvlp, nvp)) {
1634a28d77b8Svikram 		file = nvpair_name(nvp);
1635a28d77b8Svikram 		if (file == NULL)
1636a28d77b8Svikram 			continue;
1637a28d77b8Svikram 		(void) snprintf(path, sizeof (path), "%s/%s",
1638a28d77b8Svikram 		    root, file);
1639a28d77b8Svikram 		if (stat(path, &sb) == -1) {
1640a28d77b8Svikram 			walk_arg.need_update = 1;
1641a28d77b8Svikram 			if (bam_verbose)
1642a28d77b8Svikram 				bam_print(PARSEABLE_STALE_FILE, path);
1643a28d77b8Svikram 		}
1644a28d77b8Svikram 	}
1645a28d77b8Svikram }
1646a28d77b8Svikram 
16477c478bd9Sstevel@tonic-gate static void
16487c478bd9Sstevel@tonic-gate create_newstat(void)
16497c478bd9Sstevel@tonic-gate {
16507c478bd9Sstevel@tonic-gate 	int error;
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0);
16537c478bd9Sstevel@tonic-gate 	if (error) {
16547c478bd9Sstevel@tonic-gate 		/*
16557c478bd9Sstevel@tonic-gate 		 * Not fatal - we can still create archive
16567c478bd9Sstevel@tonic-gate 		 */
16577c478bd9Sstevel@tonic-gate 		walk_arg.new_nvlp = NULL;
16587c478bd9Sstevel@tonic-gate 		bam_error(NVALLOC_FAIL, strerror(error));
16597c478bd9Sstevel@tonic-gate 	}
16607c478bd9Sstevel@tonic-gate }
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate static void
16637c478bd9Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp)
16647c478bd9Sstevel@tonic-gate {
16657c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
16667c478bd9Sstevel@tonic-gate 	line_t *lp;
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
16697c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, lp->line);
16707c478bd9Sstevel@tonic-gate 		/* XXX shouldn't we use FTW_MOUNT ? */
16717c478bd9Sstevel@tonic-gate 		if (nftw(path, cmpstat, 20, 0) == -1) {
16727c478bd9Sstevel@tonic-gate 			/*
16737c478bd9Sstevel@tonic-gate 			 * Some files may not exist.
16747c478bd9Sstevel@tonic-gate 			 * For example: etc/rtc_config on a x86 diskless system
16757c478bd9Sstevel@tonic-gate 			 * Emit verbose message only
16767c478bd9Sstevel@tonic-gate 			 */
16777c478bd9Sstevel@tonic-gate 			if (bam_verbose)
16787c478bd9Sstevel@tonic-gate 				bam_print(NFTW_FAIL, path, strerror(errno));
16797c478bd9Sstevel@tonic-gate 		}
16807c478bd9Sstevel@tonic-gate 	}
16817c478bd9Sstevel@tonic-gate }
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate static void
16847c478bd9Sstevel@tonic-gate savenew(char *root)
16857c478bd9Sstevel@tonic-gate {
16867c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
16877c478bd9Sstevel@tonic-gate 	char path2[PATH_MAX];
16887c478bd9Sstevel@tonic-gate 	size_t sz;
16897c478bd9Sstevel@tonic-gate 	char *nstat;
16907c478bd9Sstevel@tonic-gate 	int fd, wrote, error;
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 	nstat = NULL;
16937c478bd9Sstevel@tonic-gate 	sz = 0;
16947c478bd9Sstevel@tonic-gate 	error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz,
16957c478bd9Sstevel@tonic-gate 	    NV_ENCODE_XDR, 0);
16967c478bd9Sstevel@tonic-gate 	if (error) {
16977c478bd9Sstevel@tonic-gate 		bam_error(PACK_FAIL, strerror(error));
16987c478bd9Sstevel@tonic-gate 		return;
16997c478bd9Sstevel@tonic-gate 	}
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP);
17027c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE);
17037c478bd9Sstevel@tonic-gate 	if (fd == -1) {
17047c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, path, strerror(errno));
17057c478bd9Sstevel@tonic-gate 		free(nstat);
17067c478bd9Sstevel@tonic-gate 		return;
17077c478bd9Sstevel@tonic-gate 	}
17087c478bd9Sstevel@tonic-gate 	wrote = write(fd, nstat, sz);
17097c478bd9Sstevel@tonic-gate 	if (wrote != sz) {
17107c478bd9Sstevel@tonic-gate 		bam_error(WRITE_FAIL, path, strerror(errno));
17117c478bd9Sstevel@tonic-gate 		(void) close(fd);
17127c478bd9Sstevel@tonic-gate 		free(nstat);
17137c478bd9Sstevel@tonic-gate 		return;
17147c478bd9Sstevel@tonic-gate 	}
17157c478bd9Sstevel@tonic-gate 	(void) close(fd);
17167c478bd9Sstevel@tonic-gate 	free(nstat);
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 	(void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT);
17197c478bd9Sstevel@tonic-gate 	if (rename(path, path2) != 0) {
17207c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path2, strerror(errno));
17217c478bd9Sstevel@tonic-gate 	}
17227c478bd9Sstevel@tonic-gate }
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate static void
17257c478bd9Sstevel@tonic-gate clear_walk_args(void)
17267c478bd9Sstevel@tonic-gate {
17277c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp)
17287c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.old_nvlp);
17297c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp)
17307c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.new_nvlp);
17317c478bd9Sstevel@tonic-gate 	walk_arg.need_update = 0;
17327c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
17337c478bd9Sstevel@tonic-gate 	walk_arg.new_nvlp = NULL;
17347c478bd9Sstevel@tonic-gate }
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate /*
17377c478bd9Sstevel@tonic-gate  * Returns:
17387c478bd9Sstevel@tonic-gate  *	0 - no update necessary
17397c478bd9Sstevel@tonic-gate  *	1 - update required.
17407c478bd9Sstevel@tonic-gate  *	BAM_ERROR (-1) - An error occurred
17417c478bd9Sstevel@tonic-gate  *
17427c478bd9Sstevel@tonic-gate  * Special handling for check (-n):
17437c478bd9Sstevel@tonic-gate  * ================================
17447c478bd9Sstevel@tonic-gate  * The check (-n) option produces parseable output.
17457c478bd9Sstevel@tonic-gate  * To do this, we suppress all stdout messages unrelated
17467c478bd9Sstevel@tonic-gate  * to out of sync files.
17477c478bd9Sstevel@tonic-gate  * All stderr messages are still printed though.
17487c478bd9Sstevel@tonic-gate  *
17497c478bd9Sstevel@tonic-gate  */
17507c478bd9Sstevel@tonic-gate static int
17517c478bd9Sstevel@tonic-gate update_required(char *root)
17527c478bd9Sstevel@tonic-gate {
17537c478bd9Sstevel@tonic-gate 	struct stat sb;
17547c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
17557c478bd9Sstevel@tonic-gate 	filelist_t flist;
17567c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
17577c478bd9Sstevel@tonic-gate 	int need_update;
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	walk_arg.need_update = 0;
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	/*
17647c478bd9Sstevel@tonic-gate 	 * Without consulting stat data, check if we need update
17657c478bd9Sstevel@tonic-gate 	 */
17667c478bd9Sstevel@tonic-gate 	check_flags_and_files(root);
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 	/*
17697c478bd9Sstevel@tonic-gate 	 * In certain deployment scenarios, filestat may not
17707c478bd9Sstevel@tonic-gate 	 * exist. Ignore it during boot-archive SMF check.
17717c478bd9Sstevel@tonic-gate 	 */
17727c478bd9Sstevel@tonic-gate 	if (bam_smf_check) {
17737c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
17747c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != 0)
17757c478bd9Sstevel@tonic-gate 			return (0);
17767c478bd9Sstevel@tonic-gate 	}
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	/*
17797c478bd9Sstevel@tonic-gate 	 * consult stat data only if we haven't made a decision
17807c478bd9Sstevel@tonic-gate 	 * about update. If checking (-n) however, we always
17817c478bd9Sstevel@tonic-gate 	 * need stat data (since we want to compare old and new)
17827c478bd9Sstevel@tonic-gate 	 */
17837c478bd9Sstevel@tonic-gate 	if (!walk_arg.need_update || bam_check)
17847c478bd9Sstevel@tonic-gate 		getoldstat(root);
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	/*
1787a28d77b8Svikram 	 * Check if the archive contains files that are no longer
1788a28d77b8Svikram 	 * present on the root filesystem.
1789a28d77b8Svikram 	 */
1790a28d77b8Svikram 	if (!walk_arg.need_update || bam_check)
1791a28d77b8Svikram 		check4stale(root);
1792a28d77b8Svikram 
1793a28d77b8Svikram 	/*
17947c478bd9Sstevel@tonic-gate 	 * read list of files
17957c478bd9Sstevel@tonic-gate 	 */
17967c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
17977c478bd9Sstevel@tonic-gate 		clear_walk_args();
17987c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
17997c478bd9Sstevel@tonic-gate 	}
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 	/*
18047c478bd9Sstevel@tonic-gate 	 * At this point either the update is required
18057c478bd9Sstevel@tonic-gate 	 * or the decision is pending. In either case
18067c478bd9Sstevel@tonic-gate 	 * we need to create new stat nvlist
18077c478bd9Sstevel@tonic-gate 	 */
18087c478bd9Sstevel@tonic-gate 	create_newstat();
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	/*
18117c478bd9Sstevel@tonic-gate 	 * This walk does 2 things:
18127c478bd9Sstevel@tonic-gate 	 *  	- gets new stat data for every file
18137c478bd9Sstevel@tonic-gate 	 *	- (optional) compare old and new stat data
18147c478bd9Sstevel@tonic-gate 	 */
18157c478bd9Sstevel@tonic-gate 	walk_list(root, &flist);
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	/* done with the file list */
18187c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	/*
18217c478bd9Sstevel@tonic-gate 	 * if we didn't succeed in  creating new stat data above
18227c478bd9Sstevel@tonic-gate 	 * just return result of update check so that archive is built.
18237c478bd9Sstevel@tonic-gate 	 */
18247c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp == NULL) {
18257c478bd9Sstevel@tonic-gate 		bam_error(NO_NEW_STAT);
18267c478bd9Sstevel@tonic-gate 		need_update = walk_arg.need_update;
18277c478bd9Sstevel@tonic-gate 		clear_walk_args();
18287c478bd9Sstevel@tonic-gate 		return (need_update ? 1 : 0);
18297c478bd9Sstevel@tonic-gate 	}
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 	/*
18337c478bd9Sstevel@tonic-gate 	 * If no update required, discard newstat
18347c478bd9Sstevel@tonic-gate 	 */
18357c478bd9Sstevel@tonic-gate 	if (!walk_arg.need_update) {
18367c478bd9Sstevel@tonic-gate 		clear_walk_args();
18377c478bd9Sstevel@tonic-gate 		return (0);
18387c478bd9Sstevel@tonic-gate 	}
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	/*
18417c478bd9Sstevel@tonic-gate 	 * At this point we need an update - so save new stat data
18427c478bd9Sstevel@tonic-gate 	 * However, if only checking (-n), don't save new stat data.
18437c478bd9Sstevel@tonic-gate 	 */
18447c478bd9Sstevel@tonic-gate 	if (!bam_check)
18457c478bd9Sstevel@tonic-gate 		savenew(root);
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	clear_walk_args();
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	return (1);
18507c478bd9Sstevel@tonic-gate }
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate static error_t
18537c478bd9Sstevel@tonic-gate create_ramdisk(char *root)
18547c478bd9Sstevel@tonic-gate {
18557c478bd9Sstevel@tonic-gate 	char *cmdline, path[PATH_MAX];
18567c478bd9Sstevel@tonic-gate 	size_t len;
18577c478bd9Sstevel@tonic-gate 	struct stat sb;
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 	/*
18607c478bd9Sstevel@tonic-gate 	 * Setup command args for create_ramdisk.ksh
18617c478bd9Sstevel@tonic-gate 	 */
18627c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, CREATE_RAMDISK);
18637c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
18647c478bd9Sstevel@tonic-gate 		bam_error(ARCH_EXEC_MISS, path, strerror(errno));
18657c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18667c478bd9Sstevel@tonic-gate 	}
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	len = strlen(path) + strlen(root) + 10;	/* room for space + -R */
18697c478bd9Sstevel@tonic-gate 	cmdline = s_calloc(1, len);
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	if (strlen(root) > 1) {
18727c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s -R %s", path, root);
18737c478bd9Sstevel@tonic-gate 		/* chop off / at the end */
18747c478bd9Sstevel@tonic-gate 		cmdline[strlen(cmdline) - 1] = '\0';
18757c478bd9Sstevel@tonic-gate 	} else
18767c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s", path);
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	if (exec_cmd(cmdline, NULL, 0) != 0) {
18797c478bd9Sstevel@tonic-gate 		bam_error(ARCHIVE_FAIL, cmdline);
18807c478bd9Sstevel@tonic-gate 		free(cmdline);
18817c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18827c478bd9Sstevel@tonic-gate 	}
18837c478bd9Sstevel@tonic-gate 	free(cmdline);
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	/*
18867c478bd9Sstevel@tonic-gate 	 * Verify that the archive has been created
18877c478bd9Sstevel@tonic-gate 	 */
1888ae115bc7Smrj 	(void) snprintf(path, sizeof (path), "%s%s", root,
1889ae115bc7Smrj 	    DIRECT_BOOT_ARCHIVE_32);
18907c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
18917c478bd9Sstevel@tonic-gate 		bam_error(ARCHIVE_NOT_CREATED, path);
18927c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18937c478bd9Sstevel@tonic-gate 	}
1894ae115bc7Smrj 	if (bam_direct == BAM_DIRECT_DBOOT) {
1895ae115bc7Smrj 		(void) snprintf(path, sizeof (path), "%s%s", root,
1896ae115bc7Smrj 		    DIRECT_BOOT_ARCHIVE_64);
1897ae115bc7Smrj 		if (stat(path, &sb) != 0) {
1898ae115bc7Smrj 			bam_error(ARCHIVE_NOT_CREATED, path);
1899ae115bc7Smrj 			return (BAM_ERROR);
1900ae115bc7Smrj 		}
1901ae115bc7Smrj 	}
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
19047c478bd9Sstevel@tonic-gate }
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate /*
19077c478bd9Sstevel@tonic-gate  * Checks if target filesystem is on a ramdisk
19087c478bd9Sstevel@tonic-gate  * 1 - is miniroot
19097c478bd9Sstevel@tonic-gate  * 0 - is not
19107c478bd9Sstevel@tonic-gate  * When in doubt assume it is not a ramdisk.
19117c478bd9Sstevel@tonic-gate  */
19127c478bd9Sstevel@tonic-gate static int
19137c478bd9Sstevel@tonic-gate is_ramdisk(char *root)
19147c478bd9Sstevel@tonic-gate {
19157c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
19167c478bd9Sstevel@tonic-gate 	FILE *fp;
19177c478bd9Sstevel@tonic-gate 	int found;
1918b610f78eSvikram 	char mntpt[PATH_MAX];
1919b610f78eSvikram 	char *cp;
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	/*
19227c478bd9Sstevel@tonic-gate 	 * There are 3 situations where creating archive is
19237c478bd9Sstevel@tonic-gate 	 * of dubious value:
1924b610f78eSvikram 	 *	- create boot_archive on a lofi-mounted boot_archive
19257c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk which is the root filesystem
19267c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk mounted somewhere else
19277c478bd9Sstevel@tonic-gate 	 * The first is not easy to detect and checking for it is not
19287c478bd9Sstevel@tonic-gate 	 * worth it.
19297c478bd9Sstevel@tonic-gate 	 * The other two conditions are handled here
19307c478bd9Sstevel@tonic-gate 	 */
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
19337c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
19347c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
19357c478bd9Sstevel@tonic-gate 		return (0);
19367c478bd9Sstevel@tonic-gate 	}
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
19397c478bd9Sstevel@tonic-gate 
1940b610f78eSvikram 	/*
1941b610f78eSvikram 	 * Remove any trailing / from the mount point
1942b610f78eSvikram 	 */
1943b610f78eSvikram 	(void) strlcpy(mntpt, root, sizeof (mntpt));
1944b610f78eSvikram 	if (strcmp(root, "/") != 0) {
1945b610f78eSvikram 		cp = mntpt + strlen(mntpt) - 1;
1946b610f78eSvikram 		if (*cp == '/')
1947b610f78eSvikram 			*cp = '\0';
1948b610f78eSvikram 	}
19497c478bd9Sstevel@tonic-gate 	found = 0;
19507c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
1951b610f78eSvikram 		if (strcmp(mnt.mnt_mountp, mntpt) == 0) {
19527c478bd9Sstevel@tonic-gate 			found = 1;
19537c478bd9Sstevel@tonic-gate 			break;
19547c478bd9Sstevel@tonic-gate 		}
19557c478bd9Sstevel@tonic-gate 	}
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 	if (!found) {
19587c478bd9Sstevel@tonic-gate 		if (bam_verbose)
1959b610f78eSvikram 			bam_error(NOT_IN_MNTTAB, mntpt);
19607c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
19617c478bd9Sstevel@tonic-gate 		return (0);
19627c478bd9Sstevel@tonic-gate 	}
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 	if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) {
19657c478bd9Sstevel@tonic-gate 		if (bam_verbose)
19667c478bd9Sstevel@tonic-gate 			bam_error(IS_RAMDISK, bam_root);
19677c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
19687c478bd9Sstevel@tonic-gate 		return (1);
19697c478bd9Sstevel@tonic-gate 	}
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	return (0);
19747c478bd9Sstevel@tonic-gate }
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate static int
19777c478bd9Sstevel@tonic-gate is_newboot(char *root)
19787c478bd9Sstevel@tonic-gate {
19797c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
19807c478bd9Sstevel@tonic-gate 	struct stat sb;
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 	/*
19837c478bd9Sstevel@tonic-gate 	 * We can't boot without MULTI_BOOT
19847c478bd9Sstevel@tonic-gate 	 */
19857c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, MULTI_BOOT);
19867c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
19877c478bd9Sstevel@tonic-gate 		if (bam_verbose)
19887c478bd9Sstevel@tonic-gate 			bam_print(FILE_MISS, path);
19897c478bd9Sstevel@tonic-gate 		return (0);
19907c478bd9Sstevel@tonic-gate 	}
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 	/*
19937c478bd9Sstevel@tonic-gate 	 * We can't generate archive without GRUB_DIR
19947c478bd9Sstevel@tonic-gate 	 */
19957c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, GRUB_DIR);
19967c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
19977c478bd9Sstevel@tonic-gate 		if (bam_verbose)
19987c478bd9Sstevel@tonic-gate 			bam_print(DIR_MISS, path);
19997c478bd9Sstevel@tonic-gate 		return (0);
20007c478bd9Sstevel@tonic-gate 	}
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 	return (1);
20037c478bd9Sstevel@tonic-gate }
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate static int
20067c478bd9Sstevel@tonic-gate is_readonly(char *root)
20077c478bd9Sstevel@tonic-gate {
20087c478bd9Sstevel@tonic-gate 	struct statvfs vfs;
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 	/*
20117c478bd9Sstevel@tonic-gate 	 * Check for RDONLY filesystem
20127c478bd9Sstevel@tonic-gate 	 * When in doubt assume it is not readonly
20137c478bd9Sstevel@tonic-gate 	 */
20147c478bd9Sstevel@tonic-gate 	if (statvfs(root, &vfs) != 0) {
20157c478bd9Sstevel@tonic-gate 		if (bam_verbose)
20167c478bd9Sstevel@tonic-gate 			bam_error(STATVFS_FAIL, root, strerror(errno));
20177c478bd9Sstevel@tonic-gate 		return (0);
20187c478bd9Sstevel@tonic-gate 	}
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 	if (vfs.f_flag & ST_RDONLY) {
20217c478bd9Sstevel@tonic-gate 		return (1);
20227c478bd9Sstevel@tonic-gate 	}
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 	return (0);
20257c478bd9Sstevel@tonic-gate }
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate static error_t
20287c478bd9Sstevel@tonic-gate update_archive(char *root, char *opt)
20297c478bd9Sstevel@tonic-gate {
20307c478bd9Sstevel@tonic-gate 	error_t ret;
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	assert(root);
20337c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 	/*
2036b610f78eSvikram 	 * root must belong to a GRUB boot OS,
20377c478bd9Sstevel@tonic-gate 	 * don't care on sparc except for diskless clients
20387c478bd9Sstevel@tonic-gate 	 */
20397c478bd9Sstevel@tonic-gate 	if (!is_newboot(root)) {
2040b610f78eSvikram 		/*
2041b610f78eSvikram 		 * Emit message only if not in context of update_all.
2042b610f78eSvikram 		 * If in update_all, emit only if verbose flag is set.
2043b610f78eSvikram 		 */
2044b610f78eSvikram 		if (!bam_update_all || bam_verbose)
2045b610f78eSvikram 			bam_print(NOT_GRUB_BOOT, root);
20467c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
20477c478bd9Sstevel@tonic-gate 	}
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 	/*
20508c1b6884Sszhou 	 * If smf check is requested when / is writable (can happen
20518c1b6884Sszhou 	 * on first reboot following an upgrade because service
20528c1b6884Sszhou 	 * dependency is messed up), skip the check.
20538c1b6884Sszhou 	 */
20548c1b6884Sszhou 	if (bam_smf_check && !bam_root_readonly)
20558c1b6884Sszhou 		return (BAM_SUCCESS);
20568c1b6884Sszhou 
20578c1b6884Sszhou 	/*
20588c1b6884Sszhou 	 * root must be writable. This check applies to alternate
20598c1b6884Sszhou 	 * root (-R option); bam_root_readonly applies to '/' only.
20607c478bd9Sstevel@tonic-gate 	 * Note: statvfs() does not always report the truth
20617c478bd9Sstevel@tonic-gate 	 */
206261cb17bdSsetje 	if (!bam_smf_check && !bam_check && is_readonly(root)) {
20638c1b6884Sszhou 		if (bam_verbose)
20647c478bd9Sstevel@tonic-gate 			bam_print(RDONLY_FS, root);
20657c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
20667c478bd9Sstevel@tonic-gate 	}
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 	/*
20697c478bd9Sstevel@tonic-gate 	 * Don't generate archive on ramdisk
20707c478bd9Sstevel@tonic-gate 	 */
20717c478bd9Sstevel@tonic-gate 	if (is_ramdisk(root)) {
20727c478bd9Sstevel@tonic-gate 		if (bam_verbose)
20737c478bd9Sstevel@tonic-gate 			bam_print(SKIP_RAMDISK);
20747c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
20757c478bd9Sstevel@tonic-gate 	}
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 	/*
20787c478bd9Sstevel@tonic-gate 	 * Now check if updated is really needed
20797c478bd9Sstevel@tonic-gate 	 */
20807c478bd9Sstevel@tonic-gate 	ret = update_required(root);
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 	/*
20837c478bd9Sstevel@tonic-gate 	 * The check command (-n) is *not* a dry run
20847c478bd9Sstevel@tonic-gate 	 * It only checks if the archive is in sync.
20857c478bd9Sstevel@tonic-gate 	 */
20867c478bd9Sstevel@tonic-gate 	if (bam_check) {
20877c478bd9Sstevel@tonic-gate 		bam_exit((ret != 0) ? 1 : 0);
20887c478bd9Sstevel@tonic-gate 	}
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 	if (ret == 1) {
20917c478bd9Sstevel@tonic-gate 		/* create the ramdisk */
20927c478bd9Sstevel@tonic-gate 		ret = create_ramdisk(root);
20937c478bd9Sstevel@tonic-gate 	}
20947c478bd9Sstevel@tonic-gate 	return (ret);
20957c478bd9Sstevel@tonic-gate }
20967c478bd9Sstevel@tonic-gate 
2097b610f78eSvikram static void
2098fbac2b2bSvikram update_fdisk(void)
2099fbac2b2bSvikram {
2100fbac2b2bSvikram 	struct stat sb;
2101fbac2b2bSvikram 	char cmd[PATH_MAX];
2102fbac2b2bSvikram 	int ret1, ret2;
2103fbac2b2bSvikram 
2104fbac2b2bSvikram 	assert(stat(GRUB_fdisk, &sb) == 0);
2105fbac2b2bSvikram 	assert(stat(GRUB_fdisk_target, &sb) == 0);
2106fbac2b2bSvikram 
2107fbac2b2bSvikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/fdisk -F %s `/bin/cat %s`",
2108fbac2b2bSvikram 	    GRUB_fdisk, GRUB_fdisk_target);
2109fbac2b2bSvikram 
2110fbac2b2bSvikram 	bam_print(UPDATING_FDISK);
2111fbac2b2bSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2112fbac2b2bSvikram 		bam_error(FDISK_UPDATE_FAILED);
2113fbac2b2bSvikram 	}
2114fbac2b2bSvikram 
2115fbac2b2bSvikram 	/*
2116fbac2b2bSvikram 	 * We are done, remove the files.
2117fbac2b2bSvikram 	 */
2118fbac2b2bSvikram 	ret1 = unlink(GRUB_fdisk);
2119fbac2b2bSvikram 	ret2 = unlink(GRUB_fdisk_target);
2120fbac2b2bSvikram 	if (ret1 != 0 || ret2 != 0) {
2121fbac2b2bSvikram 		bam_error(FILE_REMOVE_FAILED, GRUB_fdisk, GRUB_fdisk_target);
2122fbac2b2bSvikram 	}
2123fbac2b2bSvikram }
2124fbac2b2bSvikram 
2125fbac2b2bSvikram static void
2126b610f78eSvikram restore_grub_slice(void)
2127b610f78eSvikram {
2128b610f78eSvikram 	struct stat sb;
2129b610f78eSvikram 	char *mntpt, *physlice;
2130b610f78eSvikram 	int mnted;	/* set if we did a mount */
2131b610f78eSvikram 	char menupath[PATH_MAX], cmd[PATH_MAX];
2132b610f78eSvikram 
2133b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
2134b610f78eSvikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
2135b610f78eSvikram 		return;
2136b610f78eSvikram 	}
2137b610f78eSvikram 
2138b610f78eSvikram 	/*
2139b610f78eSvikram 	 * If we are doing an luactivate, don't attempt to restore GRUB or else
2140b610f78eSvikram 	 * we may not be able to get to DCA boot environments. Let luactivate
2141b610f78eSvikram 	 * handle GRUB/DCA installation
2142b610f78eSvikram 	 */
2143b610f78eSvikram 	if (stat(LU_ACTIVATE_FILE, &sb) == 0) {
2144b610f78eSvikram 		return;
2145b610f78eSvikram 	}
2146b610f78eSvikram 
2147b610f78eSvikram 	mnted = 0;
2148b610f78eSvikram 	physlice = NULL;
214940541d5dSvikram 	mntpt = mount_grub_slice(&mnted, &physlice, NULL, NULL);
2150b610f78eSvikram 	if (mntpt == NULL) {
2151b610f78eSvikram 		bam_error(CANNOT_RESTORE_GRUB_SLICE);
2152b610f78eSvikram 		return;
2153b610f78eSvikram 	}
2154b610f78eSvikram 
2155b610f78eSvikram 	(void) snprintf(menupath, sizeof (menupath), "%s%s", mntpt, GRUB_MENU);
2156b610f78eSvikram 	if (stat(menupath, &sb) == 0) {
215740541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2158b610f78eSvikram 		return;
2159b610f78eSvikram 	}
2160b610f78eSvikram 
2161b610f78eSvikram 	/*
2162b610f78eSvikram 	 * The menu is missing - we need to do a restore
2163b610f78eSvikram 	 */
2164b610f78eSvikram 	bam_print(RESTORING_GRUB);
2165b610f78eSvikram 
2166b610f78eSvikram 	(void) snprintf(cmd, sizeof (cmd), "%s %s %s %s",
2167b610f78eSvikram 	    INSTALLGRUB, STAGE1, STAGE2, physlice);
2168b610f78eSvikram 
2169b610f78eSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2170b610f78eSvikram 		bam_error(RESTORE_GRUB_FAILED);
217140541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2172b610f78eSvikram 		return;
2173b610f78eSvikram 	}
2174b610f78eSvikram 
2175b610f78eSvikram 	if (stat(GRUB_backup_menu, &sb) != 0) {
2176b610f78eSvikram 		bam_error(MISSING_BACKUP_MENU,
2177b610f78eSvikram 		    GRUB_backup_menu, strerror(errno));
217840541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2179b610f78eSvikram 		return;
2180b610f78eSvikram 	}
2181b610f78eSvikram 
2182b610f78eSvikram 	(void) snprintf(cmd, sizeof (cmd), "/bin/cp %s %s",
2183b610f78eSvikram 	    GRUB_backup_menu, menupath);
2184b610f78eSvikram 
2185b610f78eSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2186b610f78eSvikram 		bam_error(RESTORE_MENU_FAILED, menupath);
218740541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2188b610f78eSvikram 		return;
2189b610f78eSvikram 	}
2190b610f78eSvikram 
2191b610f78eSvikram 	/* Success */
219240541d5dSvikram 	umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2193b610f78eSvikram }
2194b610f78eSvikram 
21957c478bd9Sstevel@tonic-gate static error_t
21967c478bd9Sstevel@tonic-gate update_all(char *root, char *opt)
21977c478bd9Sstevel@tonic-gate {
21987c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
21997c478bd9Sstevel@tonic-gate 	struct stat sb;
22007c478bd9Sstevel@tonic-gate 	FILE *fp;
22017c478bd9Sstevel@tonic-gate 	char multibt[PATH_MAX];
22027c478bd9Sstevel@tonic-gate 	error_t ret = BAM_SUCCESS;
2203fbac2b2bSvikram 	int ret1, ret2;
22047c478bd9Sstevel@tonic-gate 
220540541d5dSvikram 	assert(root);
22067c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
22077c478bd9Sstevel@tonic-gate 
220840541d5dSvikram 	if (bam_rootlen != 1 || *root != '/') {
220940541d5dSvikram 		elide_trailing_slash(root, multibt, sizeof (multibt));
221040541d5dSvikram 		bam_error(ALT_ROOT_INVALID, multibt);
221140541d5dSvikram 		return (BAM_ERROR);
221240541d5dSvikram 	}
221340541d5dSvikram 
22147c478bd9Sstevel@tonic-gate 	/*
2215*98892a30Snadkarni 	 * Check to see if we are in the midst of safemode patching
2216*98892a30Snadkarni 	 * If so skip building the archive for /. Instead build it
2217*98892a30Snadkarni 	 * against the latest bits obtained by creating a fresh lofs
2218*98892a30Snadkarni 	 * mount of root.
2219*98892a30Snadkarni 	 */
2220*98892a30Snadkarni 	if (stat(LOFS_PATCH_FILE, &sb) == 0)  {
2221*98892a30Snadkarni 		if (mkdir(LOFS_PATCH_MNT, 0755) == -1 &&
2222*98892a30Snadkarni 		    errno != EEXIST) {
2223*98892a30Snadkarni 			bam_error(MKDIR_FAILED, "%s", LOFS_PATCH_MNT,
2224*98892a30Snadkarni 			    strerror(errno));
2225*98892a30Snadkarni 			ret = BAM_ERROR;
2226*98892a30Snadkarni 			goto out;
2227*98892a30Snadkarni 		}
2228*98892a30Snadkarni 		(void) snprintf(multibt, sizeof (multibt),
2229*98892a30Snadkarni 		    "/sbin/mount -F lofs -o nosub /  %s", LOFS_PATCH_MNT);
2230*98892a30Snadkarni 		if (exec_cmd(multibt, NULL, 0) != 0) {
2231*98892a30Snadkarni 			bam_error(MOUNT_FAILED, LOFS_PATCH_MNT, "lofs");
2232*98892a30Snadkarni 			ret = BAM_ERROR;
2233*98892a30Snadkarni 		}
2234*98892a30Snadkarni 		if (ret != BAM_ERROR) {
2235*98892a30Snadkarni 			(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
2236*98892a30Snadkarni 			    LOFS_PATCH_MNT);
2237*98892a30Snadkarni 			bam_rootlen = strlen(rootbuf);
2238*98892a30Snadkarni 			if (update_archive(rootbuf, opt) != BAM_SUCCESS)
2239*98892a30Snadkarni 				ret = BAM_ERROR;
2240*98892a30Snadkarni 		}
2241*98892a30Snadkarni 	} else {
2242*98892a30Snadkarni 		/*
22437c478bd9Sstevel@tonic-gate 		 * First update archive for current root
22447c478bd9Sstevel@tonic-gate 		 */
22457c478bd9Sstevel@tonic-gate 		if (update_archive(root, opt) != BAM_SUCCESS)
22467c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
2247*98892a30Snadkarni 	}
2248*98892a30Snadkarni 
2249*98892a30Snadkarni 	if (ret == BAM_ERROR)
2250*98892a30Snadkarni 		goto out;
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 	/*
22537c478bd9Sstevel@tonic-gate 	 * Now walk the mount table, performing archive update
22547c478bd9Sstevel@tonic-gate 	 * for all mounted Newboot root filesystems
22557c478bd9Sstevel@tonic-gate 	 */
22567c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
22577c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
22587c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
2259b610f78eSvikram 		ret = BAM_ERROR;
2260b610f78eSvikram 		goto out;
22617c478bd9Sstevel@tonic-gate 	}
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
22667c478bd9Sstevel@tonic-gate 		if (mnt.mnt_special == NULL)
22677c478bd9Sstevel@tonic-gate 			continue;
22687c478bd9Sstevel@tonic-gate 		if (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0)
22697c478bd9Sstevel@tonic-gate 			continue;
22707c478bd9Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, "/") == 0)
22717c478bd9Sstevel@tonic-gate 			continue;
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate 		(void) snprintf(multibt, sizeof (multibt), "%s%s",
22747c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp, MULTI_BOOT);
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate 		if (stat(multibt, &sb) == -1)
22777c478bd9Sstevel@tonic-gate 			continue;
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 		/*
22807c478bd9Sstevel@tonic-gate 		 * We put a trailing slash to be consistent with root = "/"
22817c478bd9Sstevel@tonic-gate 		 * case, such that we don't have to print // in some cases.
22827c478bd9Sstevel@tonic-gate 		 */
22837c478bd9Sstevel@tonic-gate 		(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
22847c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp);
22857c478bd9Sstevel@tonic-gate 		bam_rootlen = strlen(rootbuf);
2286ae115bc7Smrj 
2287ae115bc7Smrj 		/*
2288ae115bc7Smrj 		 * It's possible that other mounts may be an alternate boot
2289ae115bc7Smrj 		 * architecture, so check it again.
2290ae115bc7Smrj 		 */
2291ae115bc7Smrj 		if ((dboot_or_multiboot(rootbuf) != BAM_SUCCESS) ||
2292ae115bc7Smrj 		    (update_archive(rootbuf, opt) != BAM_SUCCESS))
22937c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
22947c478bd9Sstevel@tonic-gate 	}
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
22977c478bd9Sstevel@tonic-gate 
2298b610f78eSvikram out:
2299b610f78eSvikram 	if (stat(GRUB_slice, &sb) == 0) {
2300b610f78eSvikram 		restore_grub_slice();
2301b610f78eSvikram 	}
2302b610f78eSvikram 
2303fbac2b2bSvikram 	/*
2304fbac2b2bSvikram 	 * Update fdisk table as we go down. Updating it when
2305fbac2b2bSvikram 	 * the system is running will confuse biosdev.
2306fbac2b2bSvikram 	 */
2307fbac2b2bSvikram 	ret1 = stat(GRUB_fdisk, &sb);
2308fbac2b2bSvikram 	ret2 = stat(GRUB_fdisk_target, &sb);
2309fbac2b2bSvikram 	if ((ret1 == 0) && (ret2 == 0)) {
2310fbac2b2bSvikram 		update_fdisk();
2311fbac2b2bSvikram 	} else if ((ret1 == 0) ^ (ret2 == 0)) {
2312fbac2b2bSvikram 		/*
2313fbac2b2bSvikram 		 * It is an error for one file to be
2314fbac2b2bSvikram 		 * present and the other absent.
2315fbac2b2bSvikram 		 * It is normal for both files to be
2316fbac2b2bSvikram 		 * absent - it indicates that no fdisk
2317fbac2b2bSvikram 		 * update is required.
2318fbac2b2bSvikram 		 */
2319fbac2b2bSvikram 		bam_error(MISSING_FDISK_FILE,
2320fbac2b2bSvikram 		    ret1 ? GRUB_fdisk : GRUB_fdisk_target);
2321fbac2b2bSvikram 		ret = BAM_ERROR;
2322fbac2b2bSvikram 	}
2323fbac2b2bSvikram 
23247c478bd9Sstevel@tonic-gate 	return (ret);
23257c478bd9Sstevel@tonic-gate }
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate static void
23287c478bd9Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp)
23297c478bd9Sstevel@tonic-gate {
23307c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
23317c478bd9Sstevel@tonic-gate 		mp->start = lp;
23327c478bd9Sstevel@tonic-gate 	} else {
23337c478bd9Sstevel@tonic-gate 		mp->end->next = lp;
23348c1b6884Sszhou 		lp->prev = mp->end;
23357c478bd9Sstevel@tonic-gate 	}
23367c478bd9Sstevel@tonic-gate 	mp->end = lp;
23377c478bd9Sstevel@tonic-gate }
23387c478bd9Sstevel@tonic-gate 
23398c1b6884Sszhou static void
23408c1b6884Sszhou unlink_line(menu_t *mp, line_t *lp)
23418c1b6884Sszhou {
23428c1b6884Sszhou 	/* unlink from list */
23438c1b6884Sszhou 	if (lp->prev)
23448c1b6884Sszhou 		lp->prev->next = lp->next;
23458c1b6884Sszhou 	else
23468c1b6884Sszhou 		mp->start = lp->next;
23478c1b6884Sszhou 	if (lp->next)
23488c1b6884Sszhou 		lp->next->prev = lp->prev;
23498c1b6884Sszhou 	else
23508c1b6884Sszhou 		mp->end = lp->prev;
23518c1b6884Sszhou }
23528c1b6884Sszhou 
23538c1b6884Sszhou static entry_t *
23548c1b6884Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end)
23558c1b6884Sszhou {
23568c1b6884Sszhou 	entry_t *ent, *prev;
23578c1b6884Sszhou 
23588c1b6884Sszhou 	ent = s_calloc(1, sizeof (entry_t));
23598c1b6884Sszhou 	ent->start = start;
23608c1b6884Sszhou 	ent->end = end;
23618c1b6884Sszhou 
23628c1b6884Sszhou 	if (mp->entries == NULL) {
23638c1b6884Sszhou 		mp->entries = ent;
23648c1b6884Sszhou 		return (ent);
23658c1b6884Sszhou 	}
23668c1b6884Sszhou 
23678c1b6884Sszhou 	prev = mp->entries;
23688c1b6884Sszhou 	while (prev->next)
23698c1b6884Sszhou 		prev = prev-> next;
23708c1b6884Sszhou 	prev->next = ent;
23718c1b6884Sszhou 	ent->prev = prev;
23728c1b6884Sszhou 	return (ent);
23738c1b6884Sszhou }
23748c1b6884Sszhou 
23758c1b6884Sszhou static void
23768c1b6884Sszhou boot_entry_addline(entry_t *ent, line_t *lp)
23778c1b6884Sszhou {
23788c1b6884Sszhou 	if (ent)
23798c1b6884Sszhou 		ent->end = lp;
23808c1b6884Sszhou }
23818c1b6884Sszhou 
23827c478bd9Sstevel@tonic-gate /*
23837c478bd9Sstevel@tonic-gate  * A line in menu.lst looks like
23847c478bd9Sstevel@tonic-gate  * [ ]*<cmd>[ \t=]*<arg>*
23857c478bd9Sstevel@tonic-gate  */
23867c478bd9Sstevel@tonic-gate static void
23877c478bd9Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum)
23887c478bd9Sstevel@tonic-gate {
23897c478bd9Sstevel@tonic-gate 	/*
23907c478bd9Sstevel@tonic-gate 	 * save state across calls. This is so that
23917c478bd9Sstevel@tonic-gate 	 * header gets the right entry# after title has
23927c478bd9Sstevel@tonic-gate 	 * been processed
23937c478bd9Sstevel@tonic-gate 	 */
23948c1b6884Sszhou 	static line_t *prev = NULL;
23958c1b6884Sszhou 	static entry_t *curr_ent = NULL;
2396ae115bc7Smrj 	static int in_liveupgrade = 0;
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 	line_t	*lp;
23997c478bd9Sstevel@tonic-gate 	char *cmd, *sep, *arg;
24007c478bd9Sstevel@tonic-gate 	char save, *cp, *line;
24017c478bd9Sstevel@tonic-gate 	menu_flag_t flag = BAM_INVALID;
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate 	if (str == NULL) {
24047c478bd9Sstevel@tonic-gate 		return;
24057c478bd9Sstevel@tonic-gate 	}
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 	/*
24087c478bd9Sstevel@tonic-gate 	 * First save a copy of the entire line.
24097c478bd9Sstevel@tonic-gate 	 * We use this later to set the line field.
24107c478bd9Sstevel@tonic-gate 	 */
24117c478bd9Sstevel@tonic-gate 	line = s_strdup(str);
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate 	/* Eat up leading whitespace */
24147c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
24157c478bd9Sstevel@tonic-gate 		str++;
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 	if (*str == '#') {		/* comment */
24187c478bd9Sstevel@tonic-gate 		cmd = s_strdup("#");
24197c478bd9Sstevel@tonic-gate 		sep = NULL;
24207c478bd9Sstevel@tonic-gate 		arg = s_strdup(str + 1);
24217c478bd9Sstevel@tonic-gate 		flag = BAM_COMMENT;
2422ae115bc7Smrj 		if (strstr(arg, BAM_LU_HDR) != NULL) {
2423ae115bc7Smrj 			in_liveupgrade = 1;
2424ae115bc7Smrj 		} else if (strstr(arg, BAM_LU_FTR) != NULL) {
2425ae115bc7Smrj 			in_liveupgrade = 0;
2426ae115bc7Smrj 		}
24277c478bd9Sstevel@tonic-gate 	} else if (*str == '\0') {	/* blank line */
24287c478bd9Sstevel@tonic-gate 		cmd = sep = arg = NULL;
24297c478bd9Sstevel@tonic-gate 		flag = BAM_EMPTY;
24307c478bd9Sstevel@tonic-gate 	} else {
24317c478bd9Sstevel@tonic-gate 		/*
24327c478bd9Sstevel@tonic-gate 		 * '=' is not a documented separator in grub syntax.
24337c478bd9Sstevel@tonic-gate 		 * However various development bits use '=' as a
24347c478bd9Sstevel@tonic-gate 		 * separator. In addition, external users also
24357c478bd9Sstevel@tonic-gate 		 * use = as a separator. So we will allow that usage.
24367c478bd9Sstevel@tonic-gate 		 */
24377c478bd9Sstevel@tonic-gate 		cp = str;
24387c478bd9Sstevel@tonic-gate 		while (*str != ' ' && *str != '\t' && *str != '=') {
24397c478bd9Sstevel@tonic-gate 			if (*str == '\0') {
24407c478bd9Sstevel@tonic-gate 				cmd = s_strdup(cp);
24417c478bd9Sstevel@tonic-gate 				sep = arg = NULL;
24427c478bd9Sstevel@tonic-gate 				break;
24437c478bd9Sstevel@tonic-gate 			}
24447c478bd9Sstevel@tonic-gate 			str++;
24457c478bd9Sstevel@tonic-gate 		}
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 		if (*str != '\0') {
24487c478bd9Sstevel@tonic-gate 			save = *str;
24497c478bd9Sstevel@tonic-gate 			*str = '\0';
24507c478bd9Sstevel@tonic-gate 			cmd = s_strdup(cp);
24517c478bd9Sstevel@tonic-gate 			*str = save;
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate 			str++;
24547c478bd9Sstevel@tonic-gate 			save = *str;
24557c478bd9Sstevel@tonic-gate 			*str = '\0';
24567c478bd9Sstevel@tonic-gate 			sep = s_strdup(str - 1);
24577c478bd9Sstevel@tonic-gate 			*str = save;
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 			while (*str == ' ' || *str == '\t')
24607c478bd9Sstevel@tonic-gate 				str++;
24617c478bd9Sstevel@tonic-gate 			if (*str == '\0')
24627c478bd9Sstevel@tonic-gate 				arg = NULL;
24637c478bd9Sstevel@tonic-gate 			else
24647c478bd9Sstevel@tonic-gate 				arg = s_strdup(str);
24657c478bd9Sstevel@tonic-gate 		}
24667c478bd9Sstevel@tonic-gate 	}
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
24697c478bd9Sstevel@tonic-gate 
24707c478bd9Sstevel@tonic-gate 	lp->cmd = cmd;
24717c478bd9Sstevel@tonic-gate 	lp->sep = sep;
24727c478bd9Sstevel@tonic-gate 	lp->arg = arg;
24737c478bd9Sstevel@tonic-gate 	lp->line = line;
24747c478bd9Sstevel@tonic-gate 	lp->lineNum = ++(*lineNum);
24757c478bd9Sstevel@tonic-gate 	if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) {
24767c478bd9Sstevel@tonic-gate 		lp->entryNum = ++(*entryNum);
24777c478bd9Sstevel@tonic-gate 		lp->flags = BAM_TITLE;
24787c478bd9Sstevel@tonic-gate 		if (prev && prev->flags == BAM_COMMENT &&
2479ae115bc7Smrj 		    prev->arg && strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
24807c478bd9Sstevel@tonic-gate 			prev->entryNum = lp->entryNum;
24818c1b6884Sszhou 			curr_ent = boot_entry_new(mp, prev, lp);
2482ae115bc7Smrj 			curr_ent->flags = BAM_ENTRY_BOOTADM;
24838c1b6884Sszhou 		} else {
24848c1b6884Sszhou 			curr_ent = boot_entry_new(mp, lp, lp);
2485ae115bc7Smrj 			if (in_liveupgrade) {
2486ae115bc7Smrj 				curr_ent->flags = BAM_ENTRY_LU;
24878c1b6884Sszhou 			}
2488ae115bc7Smrj 		}
2489ae115bc7Smrj 		curr_ent->entryNum = *entryNum;
24907c478bd9Sstevel@tonic-gate 	} else if (flag != BAM_INVALID) {
24917c478bd9Sstevel@tonic-gate 		/*
24927c478bd9Sstevel@tonic-gate 		 * For header comments, the entry# is "fixed up"
24937c478bd9Sstevel@tonic-gate 		 * by the subsequent title
24947c478bd9Sstevel@tonic-gate 		 */
24957c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
24967c478bd9Sstevel@tonic-gate 		lp->flags = flag;
24977c478bd9Sstevel@tonic-gate 	} else {
24987c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
2499ae115bc7Smrj 
2500ae115bc7Smrj 		if (*entryNum == ENTRY_INIT) {
2501ae115bc7Smrj 			lp->flags = BAM_GLOBAL;
2502ae115bc7Smrj 		} else {
2503ae115bc7Smrj 			lp->flags = BAM_ENTRY;
2504ae115bc7Smrj 
2505ae115bc7Smrj 			if (cmd && arg) {
2506ae115bc7Smrj 				/*
2507ae115bc7Smrj 				 * We only compare for the length of "module"
2508ae115bc7Smrj 				 * so that "module$" will also match.
2509ae115bc7Smrj 				 */
2510ae115bc7Smrj 				if ((strncmp(cmd, menu_cmds[MODULE_CMD],
2511ae115bc7Smrj 				    strlen(menu_cmds[MODULE_CMD])) == 0) &&
2512ae115bc7Smrj 				    (strcmp(arg, MINIROOT) == 0))
2513ae115bc7Smrj 					curr_ent->flags |= BAM_ENTRY_MINIROOT;
2514ae115bc7Smrj 				else if (strcmp(cmd, menu_cmds[ROOT_CMD]) == 0)
2515ae115bc7Smrj 					curr_ent->flags |= BAM_ENTRY_ROOT;
2516ae115bc7Smrj 				else if (strcmp(cmd,
2517ae115bc7Smrj 				    menu_cmds[CHAINLOADER_CMD]) == 0)
2518ae115bc7Smrj 					curr_ent->flags |=
2519ae115bc7Smrj 					    BAM_ENTRY_CHAINLOADER;
2520ae115bc7Smrj 			}
2521ae115bc7Smrj 		}
25227c478bd9Sstevel@tonic-gate 	}
25237c478bd9Sstevel@tonic-gate 
25248c1b6884Sszhou 	/* record default, old default, and entry line ranges */
25258c1b6884Sszhou 	if (lp->flags == BAM_GLOBAL &&
25268c1b6884Sszhou 	    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) {
25278c1b6884Sszhou 		mp->curdefault = lp;
25288c1b6884Sszhou 	} else if (lp->flags == BAM_COMMENT &&
25298c1b6884Sszhou 	    strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) {
25308c1b6884Sszhou 		mp->olddefault = lp;
2531ae115bc7Smrj 	} else if (lp->flags == BAM_COMMENT &&
2532ae115bc7Smrj 	    strncmp(lp->arg, BAM_OLD_RC_DEF, strlen(BAM_OLD_RC_DEF)) == 0) {
2533ae115bc7Smrj 		mp->old_rc_default = lp;
25348c1b6884Sszhou 	} else if (lp->flags == BAM_ENTRY ||
2535ae115bc7Smrj 	    (lp->flags == BAM_COMMENT &&
2536ae115bc7Smrj 	    strcmp(lp->arg, BAM_BOOTADM_FTR) == 0)) {
25378c1b6884Sszhou 		boot_entry_addline(curr_ent, lp);
25388c1b6884Sszhou 	}
25397c478bd9Sstevel@tonic-gate 	append_line(mp, lp);
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate 	prev = lp;
25427c478bd9Sstevel@tonic-gate }
25437c478bd9Sstevel@tonic-gate 
254440541d5dSvikram static void
254540541d5dSvikram update_numbering(menu_t *mp)
254640541d5dSvikram {
254740541d5dSvikram 	int lineNum;
254840541d5dSvikram 	int entryNum;
254940541d5dSvikram 	int old_default_value;
255040541d5dSvikram 	line_t *lp, *prev, *default_lp, *default_entry;
255140541d5dSvikram 	char buf[PATH_MAX];
255240541d5dSvikram 
255340541d5dSvikram 	if (mp->start == NULL) {
255440541d5dSvikram 		return;
255540541d5dSvikram 	}
255640541d5dSvikram 
255740541d5dSvikram 	lineNum = LINE_INIT;
255840541d5dSvikram 	entryNum = ENTRY_INIT;
255940541d5dSvikram 	old_default_value = ENTRY_INIT;
256040541d5dSvikram 	lp = default_lp = default_entry = NULL;
256140541d5dSvikram 
256240541d5dSvikram 	prev = NULL;
256340541d5dSvikram 	for (lp = mp->start; lp; prev = lp, lp = lp->next) {
256440541d5dSvikram 		lp->lineNum = ++lineNum;
256540541d5dSvikram 
256640541d5dSvikram 		/*
256740541d5dSvikram 		 * Get the value of the default command
256840541d5dSvikram 		 */
256940541d5dSvikram 		if (lp->entryNum == ENTRY_INIT && lp->cmd &&
257040541d5dSvikram 		    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 &&
257140541d5dSvikram 		    lp->arg) {
257240541d5dSvikram 			old_default_value = atoi(lp->arg);
257340541d5dSvikram 			default_lp = lp;
257440541d5dSvikram 		}
257540541d5dSvikram 
257640541d5dSvikram 		/*
257740541d5dSvikram 		 * If not boot entry, nothing else to fix for this
257840541d5dSvikram 		 * entry
257940541d5dSvikram 		 */
258040541d5dSvikram 		if (lp->entryNum == ENTRY_INIT)
258140541d5dSvikram 			continue;
258240541d5dSvikram 
258340541d5dSvikram 		/*
258440541d5dSvikram 		 * Record the position of the default entry.
258540541d5dSvikram 		 * The following works because global
258640541d5dSvikram 		 * commands like default and timeout should precede
258740541d5dSvikram 		 * actual boot entries, so old_default_value
258840541d5dSvikram 		 * is already known (or default cmd is missing).
258940541d5dSvikram 		 */
259040541d5dSvikram 		if (default_entry == NULL &&
259140541d5dSvikram 		    old_default_value != ENTRY_INIT &&
259240541d5dSvikram 		    lp->entryNum == old_default_value) {
259340541d5dSvikram 			default_entry = lp;
259440541d5dSvikram 		}
259540541d5dSvikram 
259640541d5dSvikram 		/*
259740541d5dSvikram 		 * Now fixup the entry number
259840541d5dSvikram 		 */
259940541d5dSvikram 		if (lp->cmd && strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) {
260040541d5dSvikram 			lp->entryNum = ++entryNum;
260140541d5dSvikram 			/* fixup the bootadm header */
260240541d5dSvikram 			if (prev && prev->flags == BAM_COMMENT &&
2603ae115bc7Smrj 			    prev->arg &&
2604ae115bc7Smrj 			    strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
260540541d5dSvikram 				prev->entryNum = lp->entryNum;
260640541d5dSvikram 			}
260740541d5dSvikram 		} else {
260840541d5dSvikram 			lp->entryNum = entryNum;
260940541d5dSvikram 		}
261040541d5dSvikram 	}
261140541d5dSvikram 
261240541d5dSvikram 	/*
261340541d5dSvikram 	 * No default command in menu, simply return
261440541d5dSvikram 	 */
261540541d5dSvikram 	if (default_lp == NULL) {
261640541d5dSvikram 		return;
261740541d5dSvikram 	}
261840541d5dSvikram 
261940541d5dSvikram 	free(default_lp->arg);
262040541d5dSvikram 	free(default_lp->line);
262140541d5dSvikram 
262240541d5dSvikram 	if (default_entry == NULL) {
262340541d5dSvikram 		default_lp->arg = s_strdup("0");
262440541d5dSvikram 	} else {
262540541d5dSvikram 		(void) snprintf(buf, sizeof (buf), "%d",
262640541d5dSvikram 		    default_entry->entryNum);
262740541d5dSvikram 		default_lp->arg = s_strdup(buf);
262840541d5dSvikram 	}
262940541d5dSvikram 
263040541d5dSvikram 	/*
263140541d5dSvikram 	 * The following is required since only the line field gets
263240541d5dSvikram 	 * written back to menu.lst
263340541d5dSvikram 	 */
263440541d5dSvikram 	(void) snprintf(buf, sizeof (buf), "%s%s%s",
263540541d5dSvikram 	    menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg);
263640541d5dSvikram 	default_lp->line = s_strdup(buf);
263740541d5dSvikram }
263840541d5dSvikram 
263940541d5dSvikram 
26407c478bd9Sstevel@tonic-gate static menu_t *
26417c478bd9Sstevel@tonic-gate menu_read(char *menu_path)
26427c478bd9Sstevel@tonic-gate {
26437c478bd9Sstevel@tonic-gate 	FILE *fp;
26447c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE], *cp;
26457c478bd9Sstevel@tonic-gate 	menu_t *mp;
26467c478bd9Sstevel@tonic-gate 	int line, entry, len, n;
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate 	mp = s_calloc(1, sizeof (menu_t));
26497c478bd9Sstevel@tonic-gate 
26507c478bd9Sstevel@tonic-gate 	fp = fopen(menu_path, "r");
26517c478bd9Sstevel@tonic-gate 	if (fp == NULL) { /* Let the caller handle this error */
26527c478bd9Sstevel@tonic-gate 		return (mp);
26537c478bd9Sstevel@tonic-gate 	}
26547c478bd9Sstevel@tonic-gate 
26557c478bd9Sstevel@tonic-gate 
26567c478bd9Sstevel@tonic-gate 	/* Note: GRUB boot entry number starts with 0 */
26577c478bd9Sstevel@tonic-gate 	line = LINE_INIT;
26587c478bd9Sstevel@tonic-gate 	entry = ENTRY_INIT;
26597c478bd9Sstevel@tonic-gate 	cp = buf;
26607c478bd9Sstevel@tonic-gate 	len = sizeof (buf);
26617c478bd9Sstevel@tonic-gate 	while (s_fgets(cp, len, fp) != NULL) {
26627c478bd9Sstevel@tonic-gate 		n = strlen(cp);
26637c478bd9Sstevel@tonic-gate 		if (cp[n - 1] == '\\') {
26647c478bd9Sstevel@tonic-gate 			len -= n - 1;
26657c478bd9Sstevel@tonic-gate 			assert(len >= 2);
26667c478bd9Sstevel@tonic-gate 			cp += n - 1;
26677c478bd9Sstevel@tonic-gate 			continue;
26687c478bd9Sstevel@tonic-gate 		}
26697c478bd9Sstevel@tonic-gate 		line_parser(mp, buf, &line, &entry);
26707c478bd9Sstevel@tonic-gate 		cp = buf;
26717c478bd9Sstevel@tonic-gate 		len = sizeof (buf);
26727c478bd9Sstevel@tonic-gate 	}
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
26757c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, menu_path, strerror(errno));
26767c478bd9Sstevel@tonic-gate 	}
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate 	return (mp);
26797c478bd9Sstevel@tonic-gate }
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate static error_t
26827c478bd9Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title)
26837c478bd9Sstevel@tonic-gate {
26847c478bd9Sstevel@tonic-gate 	char *eq;
26857c478bd9Sstevel@tonic-gate 	char *opt_dup;
26867c478bd9Sstevel@tonic-gate 	int entryNum;
26877c478bd9Sstevel@tonic-gate 
26887c478bd9Sstevel@tonic-gate 	assert(mp);
26897c478bd9Sstevel@tonic-gate 	assert(mp->start);
26907c478bd9Sstevel@tonic-gate 	assert(opt);
26917c478bd9Sstevel@tonic-gate 
26927c478bd9Sstevel@tonic-gate 	opt_dup = s_strdup(opt);
26937c478bd9Sstevel@tonic-gate 
26947c478bd9Sstevel@tonic-gate 	if (entry)
26957c478bd9Sstevel@tonic-gate 		*entry = ENTRY_INIT;
26967c478bd9Sstevel@tonic-gate 	if (title)
26977c478bd9Sstevel@tonic-gate 		*title = NULL;
26987c478bd9Sstevel@tonic-gate 
26997c478bd9Sstevel@tonic-gate 	eq = strchr(opt_dup, '=');
27007c478bd9Sstevel@tonic-gate 	if (eq == NULL) {
27017c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
27027c478bd9Sstevel@tonic-gate 		free(opt_dup);
27037c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27047c478bd9Sstevel@tonic-gate 	}
27057c478bd9Sstevel@tonic-gate 
27067c478bd9Sstevel@tonic-gate 	*eq = '\0';
27077c478bd9Sstevel@tonic-gate 	if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) {
27087c478bd9Sstevel@tonic-gate 		assert(mp->end);
27097c478bd9Sstevel@tonic-gate 		entryNum = s_strtol(eq + 1);
27107c478bd9Sstevel@tonic-gate 		if (entryNum < 0 || entryNum > mp->end->entryNum) {
27117c478bd9Sstevel@tonic-gate 			bam_error(INVALID_ENTRY, eq + 1);
27127c478bd9Sstevel@tonic-gate 			free(opt_dup);
27137c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
27147c478bd9Sstevel@tonic-gate 		}
27157c478bd9Sstevel@tonic-gate 		*entry = entryNum;
27167c478bd9Sstevel@tonic-gate 	} else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) {
27177c478bd9Sstevel@tonic-gate 		*title = opt + (eq - opt_dup) + 1;
27187c478bd9Sstevel@tonic-gate 	} else {
27197c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
27207c478bd9Sstevel@tonic-gate 		free(opt_dup);
27217c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27227c478bd9Sstevel@tonic-gate 	}
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate 	free(opt_dup);
27257c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
27267c478bd9Sstevel@tonic-gate }
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate /*
27297c478bd9Sstevel@tonic-gate  * If invoked with no titles/entries (opt == NULL)
27307c478bd9Sstevel@tonic-gate  * only title lines in file are printed.
27317c478bd9Sstevel@tonic-gate  *
27327c478bd9Sstevel@tonic-gate  * If invoked with a title or entry #, all
27337c478bd9Sstevel@tonic-gate  * lines in *every* matching entry are listed
27347c478bd9Sstevel@tonic-gate  */
27357c478bd9Sstevel@tonic-gate static error_t
27367c478bd9Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt)
27377c478bd9Sstevel@tonic-gate {
27387c478bd9Sstevel@tonic-gate 	line_t *lp;
27397c478bd9Sstevel@tonic-gate 	int entry = ENTRY_INIT;
27407c478bd9Sstevel@tonic-gate 	int found;
27417c478bd9Sstevel@tonic-gate 	char *title = NULL;
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate 	assert(mp);
27447c478bd9Sstevel@tonic-gate 	assert(menu_path);
27457c478bd9Sstevel@tonic-gate 
27467c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
27477c478bd9Sstevel@tonic-gate 		bam_error(NO_MENU, menu_path);
27487c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27497c478bd9Sstevel@tonic-gate 	}
27507c478bd9Sstevel@tonic-gate 
27517c478bd9Sstevel@tonic-gate 	if (opt != NULL) {
27527c478bd9Sstevel@tonic-gate 		if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
27537c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
27547c478bd9Sstevel@tonic-gate 		}
27557c478bd9Sstevel@tonic-gate 		assert((entry != ENTRY_INIT) ^ (title != NULL));
27567c478bd9Sstevel@tonic-gate 	} else {
27577c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0);
27587c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0);
27597c478bd9Sstevel@tonic-gate 	}
27607c478bd9Sstevel@tonic-gate 
27617c478bd9Sstevel@tonic-gate 	found = 0;
27627c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
27637c478bd9Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY)
27647c478bd9Sstevel@tonic-gate 			continue;
27657c478bd9Sstevel@tonic-gate 		if (opt == NULL && lp->flags == BAM_TITLE) {
27667c478bd9Sstevel@tonic-gate 			bam_print(PRINT_TITLE, lp->entryNum,
27677c478bd9Sstevel@tonic-gate 			    lp->arg);
27687c478bd9Sstevel@tonic-gate 			found = 1;
27697c478bd9Sstevel@tonic-gate 			continue;
27707c478bd9Sstevel@tonic-gate 		}
27717c478bd9Sstevel@tonic-gate 		if (entry != ENTRY_INIT && lp->entryNum == entry) {
27727c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
27737c478bd9Sstevel@tonic-gate 			found = 1;
27747c478bd9Sstevel@tonic-gate 			continue;
27757c478bd9Sstevel@tonic-gate 		}
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 		/*
27787c478bd9Sstevel@tonic-gate 		 * We set the entry value here so that all lines
27797c478bd9Sstevel@tonic-gate 		 * in entry get printed. If we subsequently match
27807c478bd9Sstevel@tonic-gate 		 * title in other entries, all lines in those
27817c478bd9Sstevel@tonic-gate 		 * entries get printed as well.
27827c478bd9Sstevel@tonic-gate 		 */
27837c478bd9Sstevel@tonic-gate 		if (title && lp->flags == BAM_TITLE && lp->arg &&
27847c478bd9Sstevel@tonic-gate 		    strncmp(title, lp->arg, strlen(title)) == 0) {
27857c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
27867c478bd9Sstevel@tonic-gate 			entry = lp->entryNum;
27877c478bd9Sstevel@tonic-gate 			found = 1;
27887c478bd9Sstevel@tonic-gate 			continue;
27897c478bd9Sstevel@tonic-gate 		}
27907c478bd9Sstevel@tonic-gate 	}
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate 	if (!found) {
27937c478bd9Sstevel@tonic-gate 		bam_error(NO_MATCH_ENTRY);
27947c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27957c478bd9Sstevel@tonic-gate 	}
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
27987c478bd9Sstevel@tonic-gate }
27997c478bd9Sstevel@tonic-gate 
28007c478bd9Sstevel@tonic-gate static int
28017c478bd9Sstevel@tonic-gate add_boot_entry(menu_t *mp,
28027c478bd9Sstevel@tonic-gate 	char *title,
28037c478bd9Sstevel@tonic-gate 	char *root,
28047c478bd9Sstevel@tonic-gate 	char *kernel,
28057c478bd9Sstevel@tonic-gate 	char *module)
28067c478bd9Sstevel@tonic-gate {
28077c478bd9Sstevel@tonic-gate 	int lineNum, entryNum;
28087c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
2809ae115bc7Smrj 	menu_cmd_t k_cmd, m_cmd;
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate 	assert(mp);
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate 	if (title == NULL) {
2814ee3b8144Sszhou 		title = "Solaris";	/* default to Solaris */
28157c478bd9Sstevel@tonic-gate 	}
28167c478bd9Sstevel@tonic-gate 	if (kernel == NULL) {
28177c478bd9Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]);
28187c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
28197c478bd9Sstevel@tonic-gate 	}
28207c478bd9Sstevel@tonic-gate 	if (module == NULL) {
2821ae115bc7Smrj 		if (bam_direct != BAM_DIRECT_DBOOT) {
28227c478bd9Sstevel@tonic-gate 			bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]);
28237c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
28247c478bd9Sstevel@tonic-gate 		}
28257c478bd9Sstevel@tonic-gate 
2826ae115bc7Smrj 		/* Figure the commands out from the kernel line */
2827ae115bc7Smrj 		if (strstr(kernel, "$ISADIR") != NULL) {
2828ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE;
2829ae115bc7Smrj 			k_cmd = KERNEL_DOLLAR_CMD;
2830ae115bc7Smrj 			m_cmd = MODULE_DOLLAR_CMD;
2831ae115bc7Smrj 		} else if (strstr(kernel, "amd64") != NULL) {
2832ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE_64;
2833ae115bc7Smrj 			k_cmd = KERNEL_CMD;
2834ae115bc7Smrj 			m_cmd = MODULE_CMD;
2835ae115bc7Smrj 		} else {
2836ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE_32;
2837ae115bc7Smrj 			k_cmd = KERNEL_CMD;
2838ae115bc7Smrj 			m_cmd = MODULE_CMD;
2839ae115bc7Smrj 		}
2840ae115bc7Smrj 	} else if ((bam_direct == BAM_DIRECT_DBOOT) &&
2841ae115bc7Smrj 	    (strstr(kernel, "$ISADIR") != NULL)) {
2842ae115bc7Smrj 		/*
2843ae115bc7Smrj 		 * If it's a non-failsafe dboot kernel, use the "kernel$"
2844ae115bc7Smrj 		 * command.  Otherwise, use "kernel".
2845ae115bc7Smrj 		 */
2846ae115bc7Smrj 		k_cmd = KERNEL_DOLLAR_CMD;
2847ae115bc7Smrj 		m_cmd = MODULE_DOLLAR_CMD;
2848ae115bc7Smrj 	} else {
2849ae115bc7Smrj 		k_cmd = KERNEL_CMD;
2850ae115bc7Smrj 		m_cmd = MODULE_CMD;
2851ae115bc7Smrj 	}
2852ae115bc7Smrj 
28537c478bd9Sstevel@tonic-gate 	if (mp->start) {
28547c478bd9Sstevel@tonic-gate 		lineNum = mp->end->lineNum;
28557c478bd9Sstevel@tonic-gate 		entryNum = mp->end->entryNum;
28567c478bd9Sstevel@tonic-gate 	} else {
28577c478bd9Sstevel@tonic-gate 		lineNum = LINE_INIT;
28587c478bd9Sstevel@tonic-gate 		entryNum = ENTRY_INIT;
28597c478bd9Sstevel@tonic-gate 	}
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate 	/*
28627c478bd9Sstevel@tonic-gate 	 * No separator for comment (HDR/FTR) commands
28637c478bd9Sstevel@tonic-gate 	 * The syntax for comments is #<comment>
28647c478bd9Sstevel@tonic-gate 	 */
28657c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
2866ae115bc7Smrj 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_HDR);
28678c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
28687c478bd9Sstevel@tonic-gate 
28697c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
28707c478bd9Sstevel@tonic-gate 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
28718c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
28727c478bd9Sstevel@tonic-gate 
28738c1b6884Sszhou 	if (root) {
28747c478bd9Sstevel@tonic-gate 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
28757c478bd9Sstevel@tonic-gate 		    menu_cmds[ROOT_CMD], menu_cmds[SEP_CMD], root);
28768c1b6884Sszhou 		line_parser(mp, linebuf, &lineNum, &entryNum);
28777c478bd9Sstevel@tonic-gate 	}
28787c478bd9Sstevel@tonic-gate 
28797c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
2880ae115bc7Smrj 	    menu_cmds[k_cmd], menu_cmds[SEP_CMD], kernel);
28818c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
28827c478bd9Sstevel@tonic-gate 
28837c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
2884ae115bc7Smrj 	    menu_cmds[m_cmd], menu_cmds[SEP_CMD], module);
28858c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
28867c478bd9Sstevel@tonic-gate 
28877c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
2888ae115bc7Smrj 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_FTR);
28898c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
28907c478bd9Sstevel@tonic-gate 
28917c478bd9Sstevel@tonic-gate 	return (entryNum);
28927c478bd9Sstevel@tonic-gate }
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate static error_t
28957c478bd9Sstevel@tonic-gate do_delete(menu_t *mp, int entryNum)
28967c478bd9Sstevel@tonic-gate {
28978c1b6884Sszhou 	line_t *lp, *freed;
28988c1b6884Sszhou 	entry_t *ent, *tmp;
28997c478bd9Sstevel@tonic-gate 	int deleted;
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate 	assert(entryNum != ENTRY_INIT);
29027c478bd9Sstevel@tonic-gate 
29038c1b6884Sszhou 	ent = mp->entries;
29048c1b6884Sszhou 	while (ent) {
29058c1b6884Sszhou 		lp = ent->start;
29068c1b6884Sszhou 		/* check entry number and make sure it's a bootadm entry */
29078c1b6884Sszhou 		if (lp->flags != BAM_COMMENT ||
2908ae115bc7Smrj 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0 ||
29098c1b6884Sszhou 		    (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) {
29108c1b6884Sszhou 			ent = ent->next;
29117c478bd9Sstevel@tonic-gate 			continue;
29127c478bd9Sstevel@tonic-gate 		}
29137c478bd9Sstevel@tonic-gate 
29148c1b6884Sszhou 		/* free the entry content */
29158c1b6884Sszhou 		do {
29168c1b6884Sszhou 			freed = lp;
29178c1b6884Sszhou 			lp = lp->next;	/* prev stays the same */
29188c1b6884Sszhou 			unlink_line(mp, freed);
29198c1b6884Sszhou 			line_free(freed);
29208c1b6884Sszhou 		} while (freed != ent->end);
29217c478bd9Sstevel@tonic-gate 
29228c1b6884Sszhou 		/* free the entry_t structure */
29238c1b6884Sszhou 		tmp = ent;
29248c1b6884Sszhou 		ent = ent->next;
29258c1b6884Sszhou 		if (tmp->prev)
29268c1b6884Sszhou 			tmp->prev->next = ent;
29277c478bd9Sstevel@tonic-gate 		else
29288c1b6884Sszhou 			mp->entries = ent;
29298c1b6884Sszhou 		if (ent)
29308c1b6884Sszhou 			ent->prev = tmp->prev;
29317c478bd9Sstevel@tonic-gate 		deleted = 1;
29327c478bd9Sstevel@tonic-gate 	}
29337c478bd9Sstevel@tonic-gate 
29347c478bd9Sstevel@tonic-gate 	if (!deleted && entryNum != ALL_ENTRIES) {
29357c478bd9Sstevel@tonic-gate 		bam_error(NO_BOOTADM_MATCH);
29367c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
29377c478bd9Sstevel@tonic-gate 	}
29387c478bd9Sstevel@tonic-gate 
293940541d5dSvikram 	/*
294040541d5dSvikram 	 * Now that we have deleted an entry, update
294140541d5dSvikram 	 * the entry numbering and the default cmd.
294240541d5dSvikram 	 */
294340541d5dSvikram 	update_numbering(mp);
294440541d5dSvikram 
29457c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
29467c478bd9Sstevel@tonic-gate }
29477c478bd9Sstevel@tonic-gate 
29487c478bd9Sstevel@tonic-gate static error_t
29497c478bd9Sstevel@tonic-gate delete_all_entries(menu_t *mp, char *menu_path, char *opt)
29507c478bd9Sstevel@tonic-gate {
29517c478bd9Sstevel@tonic-gate 	assert(mp);
29527c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
29537c478bd9Sstevel@tonic-gate 
29547c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
29557c478bd9Sstevel@tonic-gate 		bam_print(EMPTY_FILE, menu_path);
29567c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
29577c478bd9Sstevel@tonic-gate 	}
29587c478bd9Sstevel@tonic-gate 
29597c478bd9Sstevel@tonic-gate 	if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) {
29607c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
29617c478bd9Sstevel@tonic-gate 	}
29627c478bd9Sstevel@tonic-gate 
29637c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
29647c478bd9Sstevel@tonic-gate }
29657c478bd9Sstevel@tonic-gate 
29667c478bd9Sstevel@tonic-gate static FILE *
29678c1b6884Sszhou open_diskmap(char *root)
29687c478bd9Sstevel@tonic-gate {
29697c478bd9Sstevel@tonic-gate 	FILE *fp;
29707c478bd9Sstevel@tonic-gate 	char cmd[PATH_MAX];
29717c478bd9Sstevel@tonic-gate 
29727c478bd9Sstevel@tonic-gate 	/* make sure we have a map file */
29737c478bd9Sstevel@tonic-gate 	fp = fopen(GRUBDISK_MAP, "r");
29747c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
29757c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd),
29768c1b6884Sszhou 		    "%s%s > /dev/null", root, CREATE_DISKMAP);
29777c478bd9Sstevel@tonic-gate 		(void) system(cmd);
29787c478bd9Sstevel@tonic-gate 		fp = fopen(GRUBDISK_MAP, "r");
29797c478bd9Sstevel@tonic-gate 	}
29807c478bd9Sstevel@tonic-gate 	return (fp);
29817c478bd9Sstevel@tonic-gate }
29827c478bd9Sstevel@tonic-gate 
29837c478bd9Sstevel@tonic-gate #define	SECTOR_SIZE	512
29847c478bd9Sstevel@tonic-gate 
29857c478bd9Sstevel@tonic-gate static int
29867c478bd9Sstevel@tonic-gate get_partition(char *device)
29877c478bd9Sstevel@tonic-gate {
29887c478bd9Sstevel@tonic-gate 	int i, fd, is_pcfs, partno = -1;
29897c478bd9Sstevel@tonic-gate 	struct mboot *mboot;
29907c478bd9Sstevel@tonic-gate 	char boot_sect[SECTOR_SIZE];
29917c478bd9Sstevel@tonic-gate 	char *wholedisk, *slice;
29927c478bd9Sstevel@tonic-gate 
29937c478bd9Sstevel@tonic-gate 	/* form whole disk (p0) */
29947c478bd9Sstevel@tonic-gate 	slice = device + strlen(device) - 2;
29957c478bd9Sstevel@tonic-gate 	is_pcfs = (*slice != 's');
29967c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
29977c478bd9Sstevel@tonic-gate 		*slice = '\0';
29987c478bd9Sstevel@tonic-gate 	wholedisk = s_calloc(1, strlen(device) + 3);
29997c478bd9Sstevel@tonic-gate 	(void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device);
30007c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
30017c478bd9Sstevel@tonic-gate 		*slice = 's';
30027c478bd9Sstevel@tonic-gate 
30037c478bd9Sstevel@tonic-gate 	/* read boot sector */
30047c478bd9Sstevel@tonic-gate 	fd = open(wholedisk, O_RDONLY);
30057c478bd9Sstevel@tonic-gate 	free(wholedisk);
30067c478bd9Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
30077c478bd9Sstevel@tonic-gate 		return (partno);
30087c478bd9Sstevel@tonic-gate 	}
30097c478bd9Sstevel@tonic-gate 	(void) close(fd);
30107c478bd9Sstevel@tonic-gate 
30117c478bd9Sstevel@tonic-gate 	/* parse fdisk table */
30127c478bd9Sstevel@tonic-gate 	mboot = (struct mboot *)((void *)boot_sect);
30137c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
30147c478bd9Sstevel@tonic-gate 		struct ipart *part =
30157c478bd9Sstevel@tonic-gate 		    (struct ipart *)(uintptr_t)mboot->parts + i;
30167c478bd9Sstevel@tonic-gate 		if (is_pcfs) {	/* looking for solaris boot part */
30177c478bd9Sstevel@tonic-gate 			if (part->systid == 0xbe) {
30187c478bd9Sstevel@tonic-gate 				partno = i;
30197c478bd9Sstevel@tonic-gate 				break;
30207c478bd9Sstevel@tonic-gate 			}
30217c478bd9Sstevel@tonic-gate 		} else {	/* look for solaris partition, old and new */
30227c478bd9Sstevel@tonic-gate 			if (part->systid == SUNIXOS ||
30237c478bd9Sstevel@tonic-gate 			    part->systid == SUNIXOS2) {
30247c478bd9Sstevel@tonic-gate 				partno = i;
30257c478bd9Sstevel@tonic-gate 				break;
30267c478bd9Sstevel@tonic-gate 			}
30277c478bd9Sstevel@tonic-gate 		}
30287c478bd9Sstevel@tonic-gate 	}
30297c478bd9Sstevel@tonic-gate 	return (partno);
30307c478bd9Sstevel@tonic-gate }
30317c478bd9Sstevel@tonic-gate 
30327c478bd9Sstevel@tonic-gate static char *
30337c478bd9Sstevel@tonic-gate get_grubdisk(char *rootdev, FILE *fp, int on_bootdev)
30347c478bd9Sstevel@tonic-gate {
30357c478bd9Sstevel@tonic-gate 	char *grubdisk;	/* (hd#,#,#) */
30367c478bd9Sstevel@tonic-gate 	char *slice;
30377c478bd9Sstevel@tonic-gate 	char *grubhd;
30387c478bd9Sstevel@tonic-gate 	int fdiskpart;
30397c478bd9Sstevel@tonic-gate 	int found = 0;
30407c478bd9Sstevel@tonic-gate 	char *devname, *ctdname = strstr(rootdev, "dsk/");
30417c478bd9Sstevel@tonic-gate 	char linebuf[PATH_MAX];
30427c478bd9Sstevel@tonic-gate 
30437c478bd9Sstevel@tonic-gate 	if (ctdname == NULL)
30447c478bd9Sstevel@tonic-gate 		return (NULL);
30457c478bd9Sstevel@tonic-gate 
30467c478bd9Sstevel@tonic-gate 	ctdname += strlen("dsk/");
30477c478bd9Sstevel@tonic-gate 	slice = strrchr(ctdname, 's');
30487c478bd9Sstevel@tonic-gate 	if (slice)
30497c478bd9Sstevel@tonic-gate 		*slice = '\0';
30507c478bd9Sstevel@tonic-gate 
30517c478bd9Sstevel@tonic-gate 	rewind(fp);
30527c478bd9Sstevel@tonic-gate 	while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) {
30537c478bd9Sstevel@tonic-gate 		grubhd = strtok(linebuf, " \t\n");
30547c478bd9Sstevel@tonic-gate 		if (grubhd)
30557c478bd9Sstevel@tonic-gate 			devname = strtok(NULL, " \t\n");
30567c478bd9Sstevel@tonic-gate 		else
30577c478bd9Sstevel@tonic-gate 			devname = NULL;
30587c478bd9Sstevel@tonic-gate 		if (devname && strcmp(devname, ctdname) == 0) {
30597c478bd9Sstevel@tonic-gate 			found = 1;
30607c478bd9Sstevel@tonic-gate 			break;
30617c478bd9Sstevel@tonic-gate 		}
30627c478bd9Sstevel@tonic-gate 	}
30637c478bd9Sstevel@tonic-gate 
30647c478bd9Sstevel@tonic-gate 	if (slice)
30657c478bd9Sstevel@tonic-gate 		*slice = 's';
30667c478bd9Sstevel@tonic-gate 
30677c478bd9Sstevel@tonic-gate 	if (found == 0) {
30687c478bd9Sstevel@tonic-gate 		if (bam_verbose)
30697c478bd9Sstevel@tonic-gate 			bam_print(DISKMAP_FAIL_NONFATAL, rootdev);
30707c478bd9Sstevel@tonic-gate 		grubhd = "0";	/* assume disk 0 if can't match */
30717c478bd9Sstevel@tonic-gate 	}
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate 	fdiskpart = get_partition(rootdev);
30747c478bd9Sstevel@tonic-gate 	if (fdiskpart == -1)
30757c478bd9Sstevel@tonic-gate 		return (NULL);
30767c478bd9Sstevel@tonic-gate 
30777c478bd9Sstevel@tonic-gate 	grubdisk = s_calloc(1, 10);
30787c478bd9Sstevel@tonic-gate 	if (slice) {
30797c478bd9Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d,%c)",
30807c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart, slice[1] + 'a' - '0');
30817c478bd9Sstevel@tonic-gate 	} else
30827c478bd9Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d)",
30837c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart);
30847c478bd9Sstevel@tonic-gate 
30857c478bd9Sstevel@tonic-gate 	/* if root not on bootdev, change GRUB disk to 0 */
30867c478bd9Sstevel@tonic-gate 	if (!on_bootdev)
30877c478bd9Sstevel@tonic-gate 		grubdisk[3] = '0';
30887c478bd9Sstevel@tonic-gate 	return (grubdisk);
30897c478bd9Sstevel@tonic-gate }
30907c478bd9Sstevel@tonic-gate 
3091ee3b8144Sszhou static char *
3092ee3b8144Sszhou get_title(char *rootdir)
30937c478bd9Sstevel@tonic-gate {
30947c478bd9Sstevel@tonic-gate 	static char title[80];	/* from /etc/release */
30958c1b6884Sszhou 	char *cp = NULL, release[PATH_MAX];
30967c478bd9Sstevel@tonic-gate 	FILE *fp;
30977c478bd9Sstevel@tonic-gate 
30987c478bd9Sstevel@tonic-gate 	/* open the /etc/release file */
30997c478bd9Sstevel@tonic-gate 	(void) snprintf(release, sizeof (release), "%s/etc/release", rootdir);
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 	fp = fopen(release, "r");
31027c478bd9Sstevel@tonic-gate 	if (fp == NULL)
3103ee3b8144Sszhou 		return (NULL);
31047c478bd9Sstevel@tonic-gate 
31057c478bd9Sstevel@tonic-gate 	while (s_fgets(title, sizeof (title), fp) != NULL) {
31067c478bd9Sstevel@tonic-gate 		cp = strstr(title, "Solaris");
31077c478bd9Sstevel@tonic-gate 		if (cp)
31087c478bd9Sstevel@tonic-gate 			break;
31097c478bd9Sstevel@tonic-gate 	}
31107c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
31118c1b6884Sszhou 	return (cp == NULL ? "Solaris" : cp);
31127c478bd9Sstevel@tonic-gate }
31137c478bd9Sstevel@tonic-gate 
3114ae115bc7Smrj char *
31157c478bd9Sstevel@tonic-gate get_special(char *mountp)
31167c478bd9Sstevel@tonic-gate {
31177c478bd9Sstevel@tonic-gate 	FILE *mntfp;
31187c478bd9Sstevel@tonic-gate 	struct mnttab mp = {0}, mpref = {0};
31197c478bd9Sstevel@tonic-gate 
31207c478bd9Sstevel@tonic-gate 	mntfp = fopen(MNTTAB, "r");
31217c478bd9Sstevel@tonic-gate 	if (mntfp == NULL) {
31227c478bd9Sstevel@tonic-gate 		return (0);
31237c478bd9Sstevel@tonic-gate 	}
31247c478bd9Sstevel@tonic-gate 
31257c478bd9Sstevel@tonic-gate 	if (*mountp == '\0')
31267c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = "/";
31277c478bd9Sstevel@tonic-gate 	else
31287c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = mountp;
31297c478bd9Sstevel@tonic-gate 	if (getmntany(mntfp, &mp, &mpref) != 0) {
31307c478bd9Sstevel@tonic-gate 		(void) fclose(mntfp);
31317c478bd9Sstevel@tonic-gate 		return (NULL);
31327c478bd9Sstevel@tonic-gate 	}
31337c478bd9Sstevel@tonic-gate 	(void) fclose(mntfp);
31347c478bd9Sstevel@tonic-gate 
31357c478bd9Sstevel@tonic-gate 	return (s_strdup(mp.mnt_special));
31367c478bd9Sstevel@tonic-gate }
31377c478bd9Sstevel@tonic-gate 
3138ae115bc7Smrj char *
31397c478bd9Sstevel@tonic-gate os_to_grubdisk(char *osdisk, int on_bootdev)
31407c478bd9Sstevel@tonic-gate {
31417c478bd9Sstevel@tonic-gate 	FILE *fp;
31427c478bd9Sstevel@tonic-gate 	char *grubdisk;
31437c478bd9Sstevel@tonic-gate 
31447c478bd9Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
31458c1b6884Sszhou 	fp = open_diskmap("");
31467c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
31477c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdisk);
31487c478bd9Sstevel@tonic-gate 		return (NULL);
31497c478bd9Sstevel@tonic-gate 	}
31507c478bd9Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdisk, fp, on_bootdev);
31517c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
31527c478bd9Sstevel@tonic-gate 	return (grubdisk);
31537c478bd9Sstevel@tonic-gate }
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate /*
31567c478bd9Sstevel@tonic-gate  * Check if root is on the boot device
31577c478bd9Sstevel@tonic-gate  * Return 0 (false) on error
31587c478bd9Sstevel@tonic-gate  */
31597c478bd9Sstevel@tonic-gate static int
31607c478bd9Sstevel@tonic-gate menu_on_bootdev(char *menu_root, FILE *fp)
31617c478bd9Sstevel@tonic-gate {
31627c478bd9Sstevel@tonic-gate 	int ret;
31637c478bd9Sstevel@tonic-gate 	char *grubhd, *bootp, *special;
31647c478bd9Sstevel@tonic-gate 
31657c478bd9Sstevel@tonic-gate 	special = get_special(menu_root);
31667c478bd9Sstevel@tonic-gate 	if (special == NULL)
31677c478bd9Sstevel@tonic-gate 		return (0);
31687c478bd9Sstevel@tonic-gate 	bootp = strstr(special, "p0:boot");
31697c478bd9Sstevel@tonic-gate 	if (bootp)
31707c478bd9Sstevel@tonic-gate 		*bootp = '\0';
31717c478bd9Sstevel@tonic-gate 	grubhd = get_grubdisk(special, fp, 1);
31727c478bd9Sstevel@tonic-gate 	free(special);
31737c478bd9Sstevel@tonic-gate 
31747c478bd9Sstevel@tonic-gate 	if (grubhd == NULL)
31757c478bd9Sstevel@tonic-gate 		return (0);
31767c478bd9Sstevel@tonic-gate 	ret = grubhd[3] == '0';
31777c478bd9Sstevel@tonic-gate 	free(grubhd);
31787c478bd9Sstevel@tonic-gate 	return (ret);
31797c478bd9Sstevel@tonic-gate }
31807c478bd9Sstevel@tonic-gate 
31818c1b6884Sszhou /*
31828c1b6884Sszhou  * look for matching bootadm entry with specified parameters
31838c1b6884Sszhou  * Here are the rules (based on existing usage):
31848c1b6884Sszhou  * - If title is specified, match on title only
31858c1b6884Sszhou  * - Else, match on grubdisk and module (don't care about kernel line).
31868c1b6884Sszhou  *   note that, if root_opt is non-zero, the absence of root line is
31878c1b6884Sszhou  *   considered a match.
31888c1b6884Sszhou  */
31898c1b6884Sszhou static entry_t *
31908c1b6884Sszhou find_boot_entry(menu_t *mp, char *title, char *root, char *module,
31918c1b6884Sszhou     int root_opt, int *entry_num)
31928c1b6884Sszhou {
31938c1b6884Sszhou 	int i;
31948c1b6884Sszhou 	line_t *lp;
31958c1b6884Sszhou 	entry_t *ent;
31968c1b6884Sszhou 
31978c1b6884Sszhou 	/* find matching entry */
31988c1b6884Sszhou 	for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) {
31998c1b6884Sszhou 		lp = ent->start;
32008c1b6884Sszhou 
32018c1b6884Sszhou 		/* first line of entry must be bootadm comment */
32028c1b6884Sszhou 		lp = ent->start;
3203ae115bc7Smrj 		if (lp->flags != BAM_COMMENT ||
3204ae115bc7Smrj 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0) {
32058c1b6884Sszhou 			continue;
32068c1b6884Sszhou 		}
32078c1b6884Sszhou 
32088c1b6884Sszhou 		/* advance to title line */
32098c1b6884Sszhou 		lp = lp->next;
32108c1b6884Sszhou 		if (title) {
32118c1b6884Sszhou 			if (lp->flags == BAM_TITLE && lp->arg &&
32128c1b6884Sszhou 			    strcmp(lp->arg, title) == 0)
32138c1b6884Sszhou 				break;
32148c1b6884Sszhou 			continue;	/* check title only */
32158c1b6884Sszhou 		}
32168c1b6884Sszhou 
32178c1b6884Sszhou 		lp = lp->next;	/* advance to root line */
32188c1b6884Sszhou 		if (lp == NULL || strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
32198c1b6884Sszhou 			/* root command found, match grub disk */
32208c1b6884Sszhou 			if (strcmp(lp->arg, root) != 0) {
32218c1b6884Sszhou 				continue;
32228c1b6884Sszhou 			}
32238c1b6884Sszhou 			lp = lp->next;	/* advance to kernel line */
32248c1b6884Sszhou 		} else {
32258c1b6884Sszhou 			/* no root command, see if root is optional */
32268c1b6884Sszhou 			if (root_opt == 0) {
32278c1b6884Sszhou 				continue;
32288c1b6884Sszhou 			}
32298c1b6884Sszhou 		}
32308c1b6884Sszhou 
32318c1b6884Sszhou 		if (lp == NULL || lp->next == NULL) {
32328c1b6884Sszhou 			continue;
32338c1b6884Sszhou 		}
32348c1b6884Sszhou 
32350d69385cSrscott 		/*
32360d69385cSrscott 		 * Check for matching module entry (failsafe or normal).  We
32370d69385cSrscott 		 * use a strncmp to match "module" or "module$", since we
32380d69385cSrscott 		 * don't know which one it should be.  If it fails to match,
32390d69385cSrscott 		 * we go around the loop again.
32400d69385cSrscott 		 */
32418c1b6884Sszhou 		lp = lp->next;	/* advance to module line */
3242ae115bc7Smrj 		if ((strncmp(lp->cmd, menu_cmds[MODULE_CMD],
3243ae115bc7Smrj 		    strlen(menu_cmds[MODULE_CMD])) != 0) ||
3244ae115bc7Smrj 		    (strcmp(lp->arg, module) != 0)) {
32458c1b6884Sszhou 			continue;
32468c1b6884Sszhou 		}
32478c1b6884Sszhou 		break;	/* match found */
32488c1b6884Sszhou 	}
32498c1b6884Sszhou 
32508c1b6884Sszhou 	*entry_num = i;
32518c1b6884Sszhou 	return (ent);
32528c1b6884Sszhou }
32538c1b6884Sszhou 
32548c1b6884Sszhou static int
32558c1b6884Sszhou update_boot_entry(menu_t *mp, char *title, char *root, char *kernel,
32568c1b6884Sszhou     char *module, int root_opt)
32578c1b6884Sszhou {
3258ae115bc7Smrj 	int i, change_kernel = 0;
32598c1b6884Sszhou 	entry_t *ent;
32608c1b6884Sszhou 	line_t *lp;
32618c1b6884Sszhou 	char linebuf[BAM_MAXLINE];
32628c1b6884Sszhou 
32638c1b6884Sszhou 	/* note: don't match on title, it's updated on upgrade */
32648c1b6884Sszhou 	ent = find_boot_entry(mp, NULL, root, module, root_opt, &i);
3265ae115bc7Smrj 	if ((ent == NULL) && (bam_direct == BAM_DIRECT_DBOOT)) {
3266ae115bc7Smrj 		/*
3267ae115bc7Smrj 		 * We may be upgrading a kernel from multiboot to
3268ae115bc7Smrj 		 * directboot.  Look for a multiboot entry.
3269ae115bc7Smrj 		 */
3270ae115bc7Smrj 		ent = find_boot_entry(mp, NULL, root, MULTI_BOOT_ARCHIVE,
3271ae115bc7Smrj 		    root_opt, &i);
3272ae115bc7Smrj 		if (ent != NULL) {
3273ae115bc7Smrj 			change_kernel = 1;
3274ae115bc7Smrj 		}
3275ae115bc7Smrj 	}
32768c1b6884Sszhou 	if (ent == NULL)
32778c1b6884Sszhou 		return (add_boot_entry(mp, title, root_opt ? NULL : root,
32788c1b6884Sszhou 		    kernel, module));
32798c1b6884Sszhou 
32808c1b6884Sszhou 	/* replace title of exiting entry and delete root line */
32818c1b6884Sszhou 	lp = ent->start;
32828c1b6884Sszhou 	lp = lp->next;	/* title line */
32838c1b6884Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
32848c1b6884Sszhou 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
32858c1b6884Sszhou 	free(lp->arg);
32868c1b6884Sszhou 	free(lp->line);
32878c1b6884Sszhou 	lp->arg = s_strdup(title);
32888c1b6884Sszhou 	lp->line = s_strdup(linebuf);
32898c1b6884Sszhou 
32908c1b6884Sszhou 	lp = lp->next;	/* root line */
32918c1b6884Sszhou 	if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
32928c1b6884Sszhou 		if (root_opt) {		/* root line not needed */
32938c1b6884Sszhou 			line_t *tmp = lp;
32948c1b6884Sszhou 			lp = lp->next;
32958c1b6884Sszhou 			unlink_line(mp, tmp);
32968c1b6884Sszhou 			line_free(tmp);
32978c1b6884Sszhou 		} else
32988c1b6884Sszhou 			lp = lp->next;
32998c1b6884Sszhou 	}
3300ae115bc7Smrj 
3301ae115bc7Smrj 	if (change_kernel) {
3302ae115bc7Smrj 		/*
3303ae115bc7Smrj 		 * We're upgrading from multiboot to directboot.
3304ae115bc7Smrj 		 */
3305ae115bc7Smrj 		if (strcmp(lp->cmd, menu_cmds[KERNEL_CMD]) == 0) {
3306ae115bc7Smrj 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
3307ae115bc7Smrj 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
3308ae115bc7Smrj 			    kernel);
3309ae115bc7Smrj 			free(lp->arg);
3310ae115bc7Smrj 			free(lp->line);
3311ae115bc7Smrj 			lp->arg = s_strdup(kernel);
3312ae115bc7Smrj 			lp->line = s_strdup(linebuf);
3313ae115bc7Smrj 			lp = lp->next;
3314ae115bc7Smrj 		}
3315ae115bc7Smrj 		if (strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) {
3316ae115bc7Smrj 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
3317ae115bc7Smrj 			    menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD],
3318ae115bc7Smrj 			    module);
3319ae115bc7Smrj 			free(lp->arg);
3320ae115bc7Smrj 			free(lp->line);
3321ae115bc7Smrj 			lp->arg = s_strdup(module);
3322ae115bc7Smrj 			lp->line = s_strdup(linebuf);
3323ae115bc7Smrj 			lp = lp->next;
3324ae115bc7Smrj 		}
3325ae115bc7Smrj 	}
33268c1b6884Sszhou 	return (i);
33278c1b6884Sszhou }
33288c1b6884Sszhou 
33297c478bd9Sstevel@tonic-gate /*ARGSUSED*/
33307c478bd9Sstevel@tonic-gate static error_t
33317c478bd9Sstevel@tonic-gate update_entry(menu_t *mp, char *menu_root, char *opt)
33327c478bd9Sstevel@tonic-gate {
33337c478bd9Sstevel@tonic-gate 	FILE *fp;
33347c478bd9Sstevel@tonic-gate 	int entry;
333560d0a590Srscott 	char *grubdisk, *title, *osdev, *osroot, *failsafe_kernel = NULL;
33368c1b6884Sszhou 	struct stat sbuf;
33378c1b6884Sszhou 	char failsafe[256];
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate 	assert(mp);
33407c478bd9Sstevel@tonic-gate 	assert(opt);
33417c478bd9Sstevel@tonic-gate 
33427c478bd9Sstevel@tonic-gate 	osdev = strtok(opt, ",");
33437c478bd9Sstevel@tonic-gate 	osroot = strtok(NULL, ",");
33447c478bd9Sstevel@tonic-gate 	if (osroot == NULL)
33457c478bd9Sstevel@tonic-gate 		osroot = menu_root;
33467c478bd9Sstevel@tonic-gate 	title = get_title(osroot);
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
33498c1b6884Sszhou 	fp = open_diskmap(osroot);
33507c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
33517c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
33527c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
33537c478bd9Sstevel@tonic-gate 	}
33547c478bd9Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdev, fp, menu_on_bootdev(menu_root, fp));
33557c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
33567c478bd9Sstevel@tonic-gate 	if (grubdisk == NULL) {
33577c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
33587c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
33597c478bd9Sstevel@tonic-gate 	}
33607c478bd9Sstevel@tonic-gate 
33617c478bd9Sstevel@tonic-gate 	/* add the entry for normal Solaris */
3362ae115bc7Smrj 	if (bam_direct == BAM_DIRECT_DBOOT) {
33638c1b6884Sszhou 		entry = update_boot_entry(mp, title, grubdisk,
3364ae115bc7Smrj 		    DIRECT_BOOT_KERNEL, DIRECT_BOOT_ARCHIVE,
33658c1b6884Sszhou 		    osroot == menu_root);
3366ae115bc7Smrj 	} else {
3367ae115bc7Smrj 		entry = update_boot_entry(mp, title, grubdisk,
3368ae115bc7Smrj 		    MULTI_BOOT, MULTI_BOOT_ARCHIVE,
3369ae115bc7Smrj 		    osroot == menu_root);
3370ae115bc7Smrj 	}
33717c478bd9Sstevel@tonic-gate 
33727c478bd9Sstevel@tonic-gate 	/* add the entry for failsafe archive */
3373ae115bc7Smrj 	(void) snprintf(failsafe, sizeof (failsafe), "%s%s", osroot, MINIROOT);
3374ae115bc7Smrj 	if (stat(failsafe, &sbuf) == 0) {
337560d0a590Srscott 
337660d0a590Srscott 		/* Figure out where the kernel line should point */
337760d0a590Srscott 		(void) snprintf(failsafe, sizeof (failsafe), "%s%s", osroot,
337860d0a590Srscott 		    DIRECT_BOOT_FAILSAFE_KERNEL);
337960d0a590Srscott 		if (stat(failsafe, &sbuf) == 0) {
338060d0a590Srscott 			failsafe_kernel = DIRECT_BOOT_FAILSAFE_LINE;
338160d0a590Srscott 		} else {
338260d0a590Srscott 			(void) snprintf(failsafe, sizeof (failsafe), "%s%s",
338360d0a590Srscott 			    osroot, MULTI_BOOT_FAILSAFE);
338460d0a590Srscott 			if (stat(failsafe, &sbuf) == 0) {
338560d0a590Srscott 				failsafe_kernel = MULTI_BOOT_FAILSAFE_LINE;
338660d0a590Srscott 			}
338760d0a590Srscott 		}
338860d0a590Srscott 		if (failsafe_kernel != NULL) {
3389ae115bc7Smrj 			(void) update_boot_entry(mp, FAILSAFE_TITLE, grubdisk,
339060d0a590Srscott 			    failsafe_kernel, MINIROOT, osroot == menu_root);
339160d0a590Srscott 		} else {
339260d0a590Srscott 			bam_error(NO_FAILSAFE_KERNEL);
339360d0a590Srscott 		}
3394ae115bc7Smrj 	}
33957c478bd9Sstevel@tonic-gate 	free(grubdisk);
33967c478bd9Sstevel@tonic-gate 
33977c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
33987c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
33997c478bd9Sstevel@tonic-gate 	}
34007c478bd9Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
34017c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
34027c478bd9Sstevel@tonic-gate }
34037c478bd9Sstevel@tonic-gate 
3404b610f78eSvikram static char *
3405b610f78eSvikram read_grub_root(void)
3406b610f78eSvikram {
3407b610f78eSvikram 	FILE *fp;
3408b610f78eSvikram 	struct stat sb;
3409b610f78eSvikram 	char buf[BAM_MAXLINE];
3410b610f78eSvikram 	char *rootstr;
3411b610f78eSvikram 
3412b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
3413b610f78eSvikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
3414b610f78eSvikram 		return (NULL);
3415b610f78eSvikram 	}
3416b610f78eSvikram 
3417b610f78eSvikram 	if (stat(GRUB_root, &sb) != 0) {
3418b610f78eSvikram 		bam_error(MISSING_ROOT_FILE, GRUB_root, strerror(errno));
3419b610f78eSvikram 		return (NULL);
3420b610f78eSvikram 	}
3421b610f78eSvikram 
3422b610f78eSvikram 	fp = fopen(GRUB_root, "r");
3423b610f78eSvikram 	if (fp == NULL) {
3424b610f78eSvikram 		bam_error(OPEN_FAIL, GRUB_root, strerror(errno));
3425b610f78eSvikram 		return (NULL);
3426b610f78eSvikram 	}
3427b610f78eSvikram 
3428b610f78eSvikram 	if (s_fgets(buf, sizeof (buf), fp) == NULL) {
3429b610f78eSvikram 		bam_error(EMPTY_FILE, GRUB_root, strerror(errno));
3430b610f78eSvikram 		(void) fclose(fp);
3431b610f78eSvikram 		return (NULL);
3432b610f78eSvikram 	}
3433b610f78eSvikram 
3434b610f78eSvikram 	/*
3435b610f78eSvikram 	 * Copy buf here as check below may trash the buffer
3436b610f78eSvikram 	 */
3437b610f78eSvikram 	rootstr = s_strdup(buf);
3438b610f78eSvikram 
3439b610f78eSvikram 	if (s_fgets(buf, sizeof (buf), fp) != NULL) {
3440b610f78eSvikram 		bam_error(BAD_ROOT_FILE, GRUB_root);
3441b610f78eSvikram 		free(rootstr);
3442b610f78eSvikram 		rootstr = NULL;
3443b610f78eSvikram 	}
3444b610f78eSvikram 
3445b610f78eSvikram 	(void) fclose(fp);
3446b610f78eSvikram 
3447b610f78eSvikram 	return (rootstr);
3448b610f78eSvikram }
3449b610f78eSvikram 
34508c1b6884Sszhou static void
3451ae115bc7Smrj save_default_entry(menu_t *mp, const char *which)
34528c1b6884Sszhou {
34538c1b6884Sszhou 	int lineNum, entryNum;
34548c1b6884Sszhou 	int entry = 0;	/* default is 0 */
34558c1b6884Sszhou 	char linebuf[BAM_MAXLINE];
34568c1b6884Sszhou 	line_t *lp = mp->curdefault;
34578c1b6884Sszhou 
3458bff269d0Svikram 	if (mp->start) {
3459bff269d0Svikram 		lineNum = mp->end->lineNum;
3460bff269d0Svikram 		entryNum = mp->end->entryNum;
3461bff269d0Svikram 	} else {
3462bff269d0Svikram 		lineNum = LINE_INIT;
3463bff269d0Svikram 		entryNum = ENTRY_INIT;
3464bff269d0Svikram 	}
3465bff269d0Svikram 
34668c1b6884Sszhou 	if (lp)
34678c1b6884Sszhou 		entry = s_strtol(lp->arg);
34688c1b6884Sszhou 
3469ae115bc7Smrj 	(void) snprintf(linebuf, sizeof (linebuf), "#%s%d", which, entry);
34708c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
34718c1b6884Sszhou }
34728c1b6884Sszhou 
34738c1b6884Sszhou static void
3474ae115bc7Smrj restore_default_entry(menu_t *mp, const char *which, line_t *lp)
34758c1b6884Sszhou {
34768c1b6884Sszhou 	int entry;
34778c1b6884Sszhou 	char *str;
34788c1b6884Sszhou 
34798c1b6884Sszhou 	if (lp == NULL)
34808c1b6884Sszhou 		return;		/* nothing to restore */
34818c1b6884Sszhou 
3482ae115bc7Smrj 	str = lp->arg + strlen(which);
34838c1b6884Sszhou 	entry = s_strtol(str);
34848c1b6884Sszhou 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
34858c1b6884Sszhou 
34868c1b6884Sszhou 	/* delete saved old default line */
34878c1b6884Sszhou 	unlink_line(mp, lp);
34888c1b6884Sszhou 	line_free(lp);
34898c1b6884Sszhou }
34908c1b6884Sszhou 
34917c478bd9Sstevel@tonic-gate /*
34927c478bd9Sstevel@tonic-gate  * This function is for supporting reboot with args.
34937c478bd9Sstevel@tonic-gate  * The opt value can be:
34947c478bd9Sstevel@tonic-gate  * NULL		delete temp entry, if present
34957c478bd9Sstevel@tonic-gate  * entry=#	switches default entry to 1
34967c478bd9Sstevel@tonic-gate  * else		treated as boot-args and setup a temperary menu entry
34977c478bd9Sstevel@tonic-gate  *		and make it the default
34987c478bd9Sstevel@tonic-gate  */
34997c478bd9Sstevel@tonic-gate #define	REBOOT_TITLE	"Solaris_reboot_transient"
35007c478bd9Sstevel@tonic-gate 
35018c1b6884Sszhou /*ARGSUSED*/
35027c478bd9Sstevel@tonic-gate static error_t
35037c478bd9Sstevel@tonic-gate update_temp(menu_t *mp, char *menupath, char *opt)
35047c478bd9Sstevel@tonic-gate {
35057c478bd9Sstevel@tonic-gate 	int entry;
3506455710d3Srscott 	char *grubdisk, *rootdev, *path, *opt_ptr;
3507ae115bc7Smrj 	char kernbuf[BUFSIZ];
3508ae115bc7Smrj 	char args_buf[BUFSIZ];
3509b610f78eSvikram 	struct stat sb;
35107c478bd9Sstevel@tonic-gate 
35117c478bd9Sstevel@tonic-gate 	assert(mp);
35127c478bd9Sstevel@tonic-gate 
35138c1b6884Sszhou 	/* If no option, delete exiting reboot menu entry */
35148c1b6884Sszhou 	if (opt == NULL) {
35158c1b6884Sszhou 		entry_t *ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL,
35168c1b6884Sszhou 		    0, &entry);
35178c1b6884Sszhou 		if (ent == NULL)	/* not found is ok */
35188c1b6884Sszhou 			return (BAM_SUCCESS);
35198c1b6884Sszhou 		(void) do_delete(mp, entry);
3520ae115bc7Smrj 		restore_default_entry(mp, BAM_OLDDEF, mp->olddefault);
3521ae115bc7Smrj 		mp->olddefault = NULL;
35228c1b6884Sszhou 		return (BAM_WRITE);
35238c1b6884Sszhou 	}
35248c1b6884Sszhou 
35258c1b6884Sszhou 	/* if entry= is specified, set the default entry */
35268c1b6884Sszhou 	if (strncmp(opt, "entry=", strlen("entry=")) == 0 &&
35277c478bd9Sstevel@tonic-gate 	    selector(mp, opt, &entry, NULL) == BAM_SUCCESS) {
35287c478bd9Sstevel@tonic-gate 		/* this is entry=# option */
35297c478bd9Sstevel@tonic-gate 		return (set_global(mp, menu_cmds[DEFAULT_CMD], entry));
35307c478bd9Sstevel@tonic-gate 	}
35317c478bd9Sstevel@tonic-gate 
35327c478bd9Sstevel@tonic-gate 	/*
35337c478bd9Sstevel@tonic-gate 	 * add a new menu entry base on opt and make it the default
3534b610f78eSvikram 	 */
3535b610f78eSvikram 	grubdisk = NULL;
3536b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
3537b610f78eSvikram 		/*
35387c478bd9Sstevel@tonic-gate 		 * 1. First get root disk name from mnttab
35397c478bd9Sstevel@tonic-gate 		 * 2. Translate disk name to grub name
35407c478bd9Sstevel@tonic-gate 		 * 3. Add the new menu entry
35417c478bd9Sstevel@tonic-gate 		 */
35427c478bd9Sstevel@tonic-gate 		rootdev = get_special("/");
35437c478bd9Sstevel@tonic-gate 		if (rootdev) {
35447c478bd9Sstevel@tonic-gate 			grubdisk = os_to_grubdisk(rootdev, 1);
35457c478bd9Sstevel@tonic-gate 			free(rootdev);
35467c478bd9Sstevel@tonic-gate 		}
3547b610f78eSvikram 	} else {
3548b610f78eSvikram 		/*
3549b610f78eSvikram 		 * This is an LU BE. The GRUB_root file
3550b610f78eSvikram 		 * contains entry for GRUB's "root" cmd.
3551b610f78eSvikram 		 */
3552b610f78eSvikram 		grubdisk = read_grub_root();
3553b610f78eSvikram 	}
35547c478bd9Sstevel@tonic-gate 	if (grubdisk == NULL) {
3555b610f78eSvikram 		bam_error(REBOOT_WITH_ARGS_FAILED);
35567c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
35577c478bd9Sstevel@tonic-gate 	}
35587c478bd9Sstevel@tonic-gate 
35597c478bd9Sstevel@tonic-gate 	/* add an entry for Solaris reboot */
3560ae115bc7Smrj 	if (bam_direct == BAM_DIRECT_DBOOT) {
3561ae115bc7Smrj 		if (opt[0] == '-') {
3562ae115bc7Smrj 			/* It's an option - first see if boot-file is set */
3563ae115bc7Smrj 			if (set_kernel(mp, KERNEL_CMD, NULL, kernbuf, BUFSIZ)
3564ae115bc7Smrj 			    != BAM_SUCCESS)
3565ae115bc7Smrj 				return (BAM_ERROR);
3566ae115bc7Smrj 			if (kernbuf[0] == '\0')
3567ae115bc7Smrj 				(void) strncpy(kernbuf, DIRECT_BOOT_KERNEL,
3568ae115bc7Smrj 				    BUFSIZ);
3569ae115bc7Smrj 			(void) strlcat(kernbuf, " ", BUFSIZ);
3570ae115bc7Smrj 			(void) strlcat(kernbuf, opt, BUFSIZ);
3571ae115bc7Smrj 		} else if (opt[0] == '/') {
3572455710d3Srscott 			/* It's a full path, so write it out. */
3573ae115bc7Smrj 			(void) strlcpy(kernbuf, opt, BUFSIZ);
3574455710d3Srscott 
3575455710d3Srscott 			/*
3576455710d3Srscott 			 * If someone runs:
3577455710d3Srscott 			 *
3578455710d3Srscott 			 *	# eeprom boot-args='-kd'
3579455710d3Srscott 			 *	# reboot /platform/i86pc/kernel/unix
3580455710d3Srscott 			 *
3581455710d3Srscott 			 * we want to use the boot-args as part of the boot
3582455710d3Srscott 			 * line.  On the other hand, if someone runs:
3583455710d3Srscott 			 *
3584455710d3Srscott 			 *	# reboot "/platform/i86pc/kernel/unix -kd"
3585455710d3Srscott 			 *
3586455710d3Srscott 			 * we don't need to mess with boot-args.  If there's
3587455710d3Srscott 			 * no space in the options string, assume we're in the
3588455710d3Srscott 			 * first case.
3589455710d3Srscott 			 */
3590455710d3Srscott 			if (strchr(opt, ' ') == NULL) {
3591455710d3Srscott 				if (set_kernel(mp, ARGS_CMD, NULL, args_buf,
3592455710d3Srscott 				    BUFSIZ) != BAM_SUCCESS)
3593455710d3Srscott 					return (BAM_ERROR);
3594455710d3Srscott 
3595455710d3Srscott 				if (args_buf[0] != '\0') {
3596455710d3Srscott 					(void) strlcat(kernbuf, " ", BUFSIZ);
3597455710d3Srscott 					(void) strlcat(kernbuf, args_buf,
3598455710d3Srscott 					    BUFSIZ);
3599455710d3Srscott 				}
3600455710d3Srscott 			}
3601ae115bc7Smrj 		} else {
3602455710d3Srscott 			/*
3603455710d3Srscott 			 * It may be a partial path, or it may be a partial
3604455710d3Srscott 			 * path followed by options.  Assume that only options
3605455710d3Srscott 			 * follow a space.  If someone sends us a kernel path
3606455710d3Srscott 			 * that includes a space, they deserve to be broken.
3607455710d3Srscott 			 */
3608455710d3Srscott 			opt_ptr = strchr(opt, ' ');
3609455710d3Srscott 			if (opt_ptr != NULL) {
3610455710d3Srscott 				*opt_ptr = '\0';
3611455710d3Srscott 			}
3612455710d3Srscott 
3613ae115bc7Smrj 			path = expand_path(opt);
3614ae115bc7Smrj 			if (path != NULL) {
3615ae115bc7Smrj 				(void) strlcpy(kernbuf, path, BUFSIZ);
3616ae115bc7Smrj 				free(path);
3617455710d3Srscott 
3618455710d3Srscott 				/*
3619455710d3Srscott 				 * If there were options given, use those.
3620455710d3Srscott 				 * Otherwise, copy over the default options.
3621455710d3Srscott 				 */
3622455710d3Srscott 				if (opt_ptr != NULL) {
3623455710d3Srscott 					/* Restore the space in opt string */
3624455710d3Srscott 					*opt_ptr = ' ';
3625455710d3Srscott 					(void) strlcat(kernbuf, opt_ptr,
3626455710d3Srscott 					    BUFSIZ);
3627455710d3Srscott 				} else {
3628ae115bc7Smrj 					if (set_kernel(mp, ARGS_CMD, NULL,
3629ae115bc7Smrj 					    args_buf, BUFSIZ) != BAM_SUCCESS)
3630ae115bc7Smrj 						return (BAM_ERROR);
3631ae115bc7Smrj 
3632ae115bc7Smrj 					if (args_buf[0] != '\0') {
3633ae115bc7Smrj 						(void) strlcat(kernbuf, " ",
3634ae115bc7Smrj 						    BUFSIZ);
3635ae115bc7Smrj 						(void) strlcat(kernbuf,
3636ae115bc7Smrj 						    args_buf, BUFSIZ);
3637ae115bc7Smrj 					}
3638ae115bc7Smrj 				}
3639455710d3Srscott 			} else {
3640455710d3Srscott 				bam_error(UNKNOWN_KERNEL, opt);
3641455710d3Srscott 				bam_print_stderr(UNKNOWN_KERNEL_REBOOT);
3642455710d3Srscott 				return (BAM_ERROR);
3643ae115bc7Smrj 			}
3644ae115bc7Smrj 		}
36457c478bd9Sstevel@tonic-gate 		entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf,
3646ae115bc7Smrj 		    NULL);
3647ae115bc7Smrj 	} else {
3648ae115bc7Smrj 		(void) snprintf(kernbuf, sizeof (kernbuf), "%s %s",
3649ae115bc7Smrj 		    MULTI_BOOT, opt);
3650ae115bc7Smrj 		entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf,
3651ae115bc7Smrj 		    MULTI_BOOT_ARCHIVE);
3652ae115bc7Smrj 	}
36537c478bd9Sstevel@tonic-gate 	free(grubdisk);
36547c478bd9Sstevel@tonic-gate 
36557c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
3656b610f78eSvikram 		bam_error(REBOOT_WITH_ARGS_FAILED);
36577c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
36587c478bd9Sstevel@tonic-gate 	}
36598c1b6884Sszhou 
3660ae115bc7Smrj 	save_default_entry(mp, BAM_OLDDEF);
36617c478bd9Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
36627c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
36637c478bd9Sstevel@tonic-gate }
36647c478bd9Sstevel@tonic-gate 
36657c478bd9Sstevel@tonic-gate static error_t
36667c478bd9Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val)
36677c478bd9Sstevel@tonic-gate {
36687c478bd9Sstevel@tonic-gate 	line_t *lp, *found, *last;
36697c478bd9Sstevel@tonic-gate 	char *cp, *str;
36707c478bd9Sstevel@tonic-gate 	char prefix[BAM_MAXLINE];
36717c478bd9Sstevel@tonic-gate 	size_t len;
36727c478bd9Sstevel@tonic-gate 
36737c478bd9Sstevel@tonic-gate 	assert(mp);
36747c478bd9Sstevel@tonic-gate 	assert(globalcmd);
36757c478bd9Sstevel@tonic-gate 
3676b610f78eSvikram 	if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) {
3677b610f78eSvikram 		if (val < 0 || mp->end == NULL || val > mp->end->entryNum) {
3678b610f78eSvikram 			(void) snprintf(prefix, sizeof (prefix), "%d", val);
3679b610f78eSvikram 			bam_error(INVALID_ENTRY, prefix);
3680b610f78eSvikram 			return (BAM_ERROR);
3681b610f78eSvikram 		}
3682b610f78eSvikram 	}
3683b610f78eSvikram 
36847c478bd9Sstevel@tonic-gate 	found = last = NULL;
36857c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
36867c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
36877c478bd9Sstevel@tonic-gate 			continue;
36887c478bd9Sstevel@tonic-gate 
36897c478bd9Sstevel@tonic-gate 		last = lp; /* track the last global found */
36907c478bd9Sstevel@tonic-gate 
36917c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
36927c478bd9Sstevel@tonic-gate 			bam_error(NO_CMD, lp->lineNum);
36937c478bd9Sstevel@tonic-gate 			continue;
36947c478bd9Sstevel@tonic-gate 		}
36957c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
36967c478bd9Sstevel@tonic-gate 			continue;
36977c478bd9Sstevel@tonic-gate 
36987c478bd9Sstevel@tonic-gate 		if (found) {
36997c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
37007c478bd9Sstevel@tonic-gate 		}
37017c478bd9Sstevel@tonic-gate 		found = lp;
37027c478bd9Sstevel@tonic-gate 	}
37037c478bd9Sstevel@tonic-gate 
37047c478bd9Sstevel@tonic-gate 	if (found == NULL) {
37057c478bd9Sstevel@tonic-gate 		lp = s_calloc(1, sizeof (line_t));
37067c478bd9Sstevel@tonic-gate 		if (last == NULL) {
37077c478bd9Sstevel@tonic-gate 			lp->next = mp->start;
37087c478bd9Sstevel@tonic-gate 			mp->start = lp;
37097c478bd9Sstevel@tonic-gate 			mp->end = (mp->end) ? mp->end : lp;
37107c478bd9Sstevel@tonic-gate 		} else {
37117c478bd9Sstevel@tonic-gate 			lp->next = last->next;
37127c478bd9Sstevel@tonic-gate 			last->next = lp;
37137c478bd9Sstevel@tonic-gate 			if (lp->next == NULL)
37147c478bd9Sstevel@tonic-gate 				mp->end = lp;
37157c478bd9Sstevel@tonic-gate 		}
37167c478bd9Sstevel@tonic-gate 		lp->flags = BAM_GLOBAL; /* other fields not needed for writes */
37177c478bd9Sstevel@tonic-gate 		len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]);
37187c478bd9Sstevel@tonic-gate 		len += 10;	/* val < 10 digits */
37197c478bd9Sstevel@tonic-gate 		lp->line = s_calloc(1, len);
37207c478bd9Sstevel@tonic-gate 		(void) snprintf(lp->line, len, "%s%s%d",
37217c478bd9Sstevel@tonic-gate 		    globalcmd, menu_cmds[SEP_CMD], val);
37227c478bd9Sstevel@tonic-gate 		return (BAM_WRITE);
37237c478bd9Sstevel@tonic-gate 	}
37247c478bd9Sstevel@tonic-gate 
37257c478bd9Sstevel@tonic-gate 	/*
37267c478bd9Sstevel@tonic-gate 	 * We are changing an existing entry. Retain any prefix whitespace,
37277c478bd9Sstevel@tonic-gate 	 * but overwrite everything else. This preserves tabs added for
37287c478bd9Sstevel@tonic-gate 	 * readability.
37297c478bd9Sstevel@tonic-gate 	 */
37307c478bd9Sstevel@tonic-gate 	str = found->line;
37317c478bd9Sstevel@tonic-gate 	cp = prefix;
37327c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
37337c478bd9Sstevel@tonic-gate 		*(cp++) = *(str++);
37347c478bd9Sstevel@tonic-gate 	*cp = '\0'; /* Terminate prefix */
37357c478bd9Sstevel@tonic-gate 	len = strlen(prefix) + strlen(globalcmd);
37367c478bd9Sstevel@tonic-gate 	len += strlen(menu_cmds[SEP_CMD]) + 10;
37377c478bd9Sstevel@tonic-gate 
37387c478bd9Sstevel@tonic-gate 	free(found->line);
37397c478bd9Sstevel@tonic-gate 	found->line = s_calloc(1, len);
37407c478bd9Sstevel@tonic-gate 	(void) snprintf(found->line, len,
37417c478bd9Sstevel@tonic-gate 	    "%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val);
37427c478bd9Sstevel@tonic-gate 
37437c478bd9Sstevel@tonic-gate 	return (BAM_WRITE); /* need a write to menu */
37447c478bd9Sstevel@tonic-gate }
37457c478bd9Sstevel@tonic-gate 
3746ae115bc7Smrj /*
3747ae115bc7Smrj  * partial_path may be anything like "kernel/unix" or "kmdb".  Try to
3748455710d3Srscott  * expand it to a full unix path.  The calling function is expected to
3749455710d3Srscott  * output a message if an error occurs and NULL is returned.
3750ae115bc7Smrj  */
3751ae115bc7Smrj static char *
3752ae115bc7Smrj expand_path(const char *partial_path)
3753ae115bc7Smrj {
3754ae115bc7Smrj 	int new_path_len;
3755ae115bc7Smrj 	char *new_path, new_path2[PATH_MAX];
3756ae115bc7Smrj 	struct stat sb;
3757ae115bc7Smrj 
3758ae115bc7Smrj 	new_path_len = strlen(partial_path) + 64;
3759ae115bc7Smrj 	new_path = s_calloc(1, new_path_len);
3760ae115bc7Smrj 
3761ae115bc7Smrj 	/* First, try the simplest case - something like "kernel/unix" */
3762ae115bc7Smrj 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s",
3763ae115bc7Smrj 	    partial_path);
3764ae115bc7Smrj 	if (stat(new_path, &sb) == 0) {
3765ae115bc7Smrj 		return (new_path);
3766ae115bc7Smrj 	}
3767ae115bc7Smrj 
3768ae115bc7Smrj 	if (strcmp(partial_path, "kmdb") == 0) {
3769ae115bc7Smrj 		(void) snprintf(new_path, new_path_len, "%s -k",
3770ae115bc7Smrj 		    DIRECT_BOOT_KERNEL);
3771ae115bc7Smrj 		return (new_path);
3772ae115bc7Smrj 	}
3773ae115bc7Smrj 
3774ae115bc7Smrj 	/*
3775ae115bc7Smrj 	 * We've quickly reached unsupported usage.  Try once more to
3776ae115bc7Smrj 	 * see if we were just given a glom name.
3777ae115bc7Smrj 	 */
3778ae115bc7Smrj 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s/unix",
3779ae115bc7Smrj 	    partial_path);
3780ae115bc7Smrj 	(void) snprintf(new_path2, PATH_MAX, "/platform/i86pc/%s/amd64/unix",
3781ae115bc7Smrj 	    partial_path);
3782ae115bc7Smrj 	if (stat(new_path, &sb) == 0) {
3783ae115bc7Smrj 		if (stat(new_path2, &sb) == 0) {
3784ae115bc7Smrj 			/*
3785ae115bc7Smrj 			 * We matched both, so we actually
3786ae115bc7Smrj 			 * want to write the $ISADIR version.
3787ae115bc7Smrj 			 */
3788ae115bc7Smrj 			(void) snprintf(new_path, new_path_len,
3789ae115bc7Smrj 			    "/platform/i86pc/kernel/%s/$ISADIR/unix",
3790ae115bc7Smrj 			    partial_path);
3791ae115bc7Smrj 		}
3792ae115bc7Smrj 		return (new_path);
3793ae115bc7Smrj 	}
3794ae115bc7Smrj 
3795ae115bc7Smrj 	free(new_path);
3796ae115bc7Smrj 	return (NULL);
3797ae115bc7Smrj }
3798ae115bc7Smrj 
3799ae115bc7Smrj /*
3800ae115bc7Smrj  * The kernel cmd and arg have been changed, so
3801ae115bc7Smrj  * check whether the archive line needs to change.
3802ae115bc7Smrj  */
3803ae115bc7Smrj static void
3804ae115bc7Smrj set_archive_line(entry_t *entryp, line_t *kernelp)
3805ae115bc7Smrj {
3806ae115bc7Smrj 	line_t *lp = entryp->start;
3807ae115bc7Smrj 	char *new_archive;
3808ae115bc7Smrj 	menu_cmd_t m_cmd;
3809ae115bc7Smrj 
3810ae115bc7Smrj 	for (; lp != NULL; lp = lp->next) {
3811ae115bc7Smrj 		if (strncmp(lp->cmd, menu_cmds[MODULE_CMD],
3812ae115bc7Smrj 		    sizeof (menu_cmds[MODULE_CMD]) - 1) == 0) {
3813ae115bc7Smrj 			break;
3814ae115bc7Smrj 		}
3815ae115bc7Smrj 		if (lp == entryp->end)
3816ae115bc7Smrj 			return;
3817ae115bc7Smrj 	}
3818ae115bc7Smrj 	if (lp == NULL)
3819ae115bc7Smrj 		return;
3820ae115bc7Smrj 
3821ae115bc7Smrj 	if (strstr(kernelp->arg, "$ISADIR") != NULL) {
3822ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE;
3823ae115bc7Smrj 		m_cmd = MODULE_DOLLAR_CMD;
3824ae115bc7Smrj 	} else if (strstr(kernelp->arg, "amd64") != NULL) {
3825ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE_64;
3826ae115bc7Smrj 		m_cmd = MODULE_CMD;
3827ae115bc7Smrj 	} else {
3828ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE_32;
3829ae115bc7Smrj 		m_cmd = MODULE_CMD;
3830ae115bc7Smrj 	}
3831ae115bc7Smrj 
3832ae115bc7Smrj 	if (strcmp(lp->arg, new_archive) == 0)
3833ae115bc7Smrj 		return;
3834ae115bc7Smrj 
3835ae115bc7Smrj 	if (strcmp(lp->cmd, menu_cmds[m_cmd]) != 0) {
3836ae115bc7Smrj 		free(lp->cmd);
3837ae115bc7Smrj 		lp->cmd = s_strdup(menu_cmds[m_cmd]);
3838ae115bc7Smrj 	}
3839ae115bc7Smrj 
3840ae115bc7Smrj 	free(lp->arg);
3841ae115bc7Smrj 	lp->arg = s_strdup(new_archive);
3842ae115bc7Smrj 	update_line(lp);
3843ae115bc7Smrj }
3844ae115bc7Smrj 
3845ae115bc7Smrj /*
3846ae115bc7Smrj  * Title for an entry to set properties that once went in bootenv.rc.
3847ae115bc7Smrj  */
3848ae115bc7Smrj #define	BOOTENV_RC_TITLE	"Solaris bootenv rc"
3849ae115bc7Smrj 
3850ae115bc7Smrj /*
3851ae115bc7Smrj  * If path is NULL, return the kernel (optnum == KERNEL_CMD) or arguments
3852ae115bc7Smrj  * (optnum == ARGS_CMD) in the argument buf.  If path is a zero-length
3853ae115bc7Smrj  * string, reset the value to the default.  If path is a non-zero-length
3854ae115bc7Smrj  * string, set the kernel or arguments.
3855ae115bc7Smrj  */
3856ae115bc7Smrj static error_t
3857ae115bc7Smrj set_kernel(menu_t *mp, menu_cmd_t optnum, char *path, char *buf, size_t bufsize)
3858ae115bc7Smrj {
3859ae115bc7Smrj 	int entryNum, rv = BAM_SUCCESS, free_new_path = 0;
3860ae115bc7Smrj 	entry_t *entryp;
3861ae115bc7Smrj 	line_t *ptr, *kernelp;
3862ae115bc7Smrj 	char *new_arg, *old_args, *space;
3863ae115bc7Smrj 	char *grubdisk, *rootdev, *new_path;
3864ae115bc7Smrj 	char old_space;
3865ae115bc7Smrj 	size_t old_kernel_len, new_str_len;
3866ae115bc7Smrj 	struct stat sb;
3867ae115bc7Smrj 
3868ae115bc7Smrj 	assert(bufsize > 0);
3869ae115bc7Smrj 
3870ae115bc7Smrj 	ptr = kernelp = NULL;
3871ae115bc7Smrj 	new_arg = old_args = space = NULL;
3872ae115bc7Smrj 	grubdisk = rootdev = new_path = NULL;
3873ae115bc7Smrj 	buf[0] = '\0';
3874ae115bc7Smrj 
3875ae115bc7Smrj 	if (bam_direct != BAM_DIRECT_DBOOT) {
3876ae115bc7Smrj 		bam_error(NOT_DBOOT, optnum == KERNEL_CMD ? "kernel" : "args");
3877ae115bc7Smrj 		return (BAM_ERROR);
3878ae115bc7Smrj 	}
3879ae115bc7Smrj 
3880ae115bc7Smrj 	/*
3881ae115bc7Smrj 	 * If a user changed the default entry to a non-bootadm controlled
3882ae115bc7Smrj 	 * one, we don't want to mess with it.  Just print an error and
3883ae115bc7Smrj 	 * return.
3884ae115bc7Smrj 	 */
3885ae115bc7Smrj 	if (mp->curdefault) {
3886ae115bc7Smrj 		entryNum = s_strtol(mp->curdefault->arg);
3887ae115bc7Smrj 		for (entryp = mp->entries; entryp; entryp = entryp->next) {
3888ae115bc7Smrj 			if (entryp->entryNum == entryNum)
3889ae115bc7Smrj 				break;
3890ae115bc7Smrj 		}
3891ae115bc7Smrj 		if ((entryp != NULL) &&
3892ae115bc7Smrj 		    ((entryp->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU)) == 0)) {
3893ae115bc7Smrj 			bam_error(DEFAULT_NOT_BAM);
3894ae115bc7Smrj 			return (BAM_ERROR);
3895ae115bc7Smrj 		}
3896ae115bc7Smrj 	}
3897ae115bc7Smrj 
3898ae115bc7Smrj 	entryNum = -1;
3899ae115bc7Smrj 	entryp = find_boot_entry(mp, BOOTENV_RC_TITLE, NULL, NULL, 0,
3900ae115bc7Smrj 	    &entryNum);
3901ae115bc7Smrj 
3902ae115bc7Smrj 	if (entryp != NULL) {
3903ae115bc7Smrj 		for (ptr = entryp->start; ptr && ptr != entryp->end;
3904ae115bc7Smrj 		    ptr = ptr->next) {
3905ae115bc7Smrj 			if (strncmp(ptr->cmd, menu_cmds[KERNEL_CMD],
3906ae115bc7Smrj 			    sizeof (menu_cmds[KERNEL_CMD]) - 1) == 0) {
3907ae115bc7Smrj 				kernelp = ptr;
3908ae115bc7Smrj 				break;
3909ae115bc7Smrj 			}
3910ae115bc7Smrj 		}
3911ae115bc7Smrj 		if (kernelp == NULL) {
3912ae115bc7Smrj 			bam_error(NO_KERNEL, entryNum);
3913ae115bc7Smrj 			return (BAM_ERROR);
3914ae115bc7Smrj 		}
3915ae115bc7Smrj 
3916ae115bc7Smrj 		old_kernel_len = strcspn(kernelp->arg, " \t");
3917ae115bc7Smrj 		space = old_args = kernelp->arg + old_kernel_len;
3918ae115bc7Smrj 		while ((*old_args == ' ') || (*old_args == '\t'))
3919ae115bc7Smrj 			old_args++;
3920ae115bc7Smrj 	}
3921ae115bc7Smrj 
3922ae115bc7Smrj 	if (path == NULL) {
3923ae115bc7Smrj 		/* Simply report what was found */
3924ae115bc7Smrj 		if (kernelp == NULL)
3925ae115bc7Smrj 			return (BAM_SUCCESS);
3926ae115bc7Smrj 
3927ae115bc7Smrj 		if (optnum == ARGS_CMD) {
3928ae115bc7Smrj 			if (old_args[0] != '\0')
3929ae115bc7Smrj 				(void) strlcpy(buf, old_args, bufsize);
3930ae115bc7Smrj 		} else {
3931ae115bc7Smrj 			/*
3932ae115bc7Smrj 			 * We need to print the kernel, so we just turn the
3933ae115bc7Smrj 			 * first space into a '\0' and print the beginning.
3934ae115bc7Smrj 			 * We don't print anything if it's the default kernel.
3935ae115bc7Smrj 			 */
3936ae115bc7Smrj 			old_space = *space;
3937ae115bc7Smrj 			*space = '\0';
3938ae115bc7Smrj 			if (strcmp(kernelp->arg, DIRECT_BOOT_KERNEL) != 0)
3939ae115bc7Smrj 				(void) strlcpy(buf, kernelp->arg, bufsize);
3940ae115bc7Smrj 			*space = old_space;
3941ae115bc7Smrj 		}
3942ae115bc7Smrj 		return (BAM_SUCCESS);
3943ae115bc7Smrj 	}
3944ae115bc7Smrj 
3945ae115bc7Smrj 	/*
3946ae115bc7Smrj 	 * First, check if we're resetting an entry to the default.
3947ae115bc7Smrj 	 */
3948ae115bc7Smrj 	if ((path[0] == '\0') ||
3949ae115bc7Smrj 	    ((optnum == KERNEL_CMD) &&
3950ae115bc7Smrj 	    (strcmp(path, DIRECT_BOOT_KERNEL) == 0))) {
3951ae115bc7Smrj 		if ((entryp == NULL) || (kernelp == NULL)) {
3952ae115bc7Smrj 			/* No previous entry, it's already the default */
3953ae115bc7Smrj 			return (BAM_SUCCESS);
3954ae115bc7Smrj 		}
3955ae115bc7Smrj 
3956ae115bc7Smrj 		/*
3957ae115bc7Smrj 		 * Check if we can delete the entry.  If we're resetting the
3958ae115bc7Smrj 		 * kernel command, and the args is already empty, or if we're
3959ae115bc7Smrj 		 * resetting the args command, and the kernel is already the
3960ae115bc7Smrj 		 * default, we can restore the old default and delete the entry.
3961ae115bc7Smrj 		 */
3962ae115bc7Smrj 		if (((optnum == KERNEL_CMD) &&
3963ae115bc7Smrj 		    ((old_args == NULL) || (old_args[0] == '\0'))) ||
3964ae115bc7Smrj 		    ((optnum == ARGS_CMD) &&
3965ae115bc7Smrj 		    (strncmp(kernelp->arg, DIRECT_BOOT_KERNEL,
3966ae115bc7Smrj 		    sizeof (DIRECT_BOOT_KERNEL) - 1) == 0))) {
3967ae115bc7Smrj 			kernelp = NULL;
3968ae115bc7Smrj 			(void) do_delete(mp, entryNum);
3969ae115bc7Smrj 			restore_default_entry(mp, BAM_OLD_RC_DEF,
3970ae115bc7Smrj 			    mp->old_rc_default);
3971ae115bc7Smrj 			mp->old_rc_default = NULL;
3972ae115bc7Smrj 			rv = BAM_WRITE;
3973ae115bc7Smrj 			goto done;
3974ae115bc7Smrj 		}
3975ae115bc7Smrj 
3976ae115bc7Smrj 		if (optnum == KERNEL_CMD) {
3977ae115bc7Smrj 			/*
3978ae115bc7Smrj 			 * At this point, we've already checked that old_args
3979ae115bc7Smrj 			 * and entryp are valid pointers.  The "+ 2" is for
3980ae115bc7Smrj 			 * a space a the string termination character.
3981ae115bc7Smrj 			 */
3982ae115bc7Smrj 			new_str_len = (sizeof (DIRECT_BOOT_KERNEL) - 1) +
3983ae115bc7Smrj 			    strlen(old_args) + 2;
3984ae115bc7Smrj 			new_arg = s_calloc(1, new_str_len);
3985ae115bc7Smrj 			(void) snprintf(new_arg, new_str_len, "%s %s",
3986ae115bc7Smrj 			    DIRECT_BOOT_KERNEL, old_args);
3987ae115bc7Smrj 			free(kernelp->arg);
3988ae115bc7Smrj 			kernelp->arg = new_arg;
3989ae115bc7Smrj 
3990ae115bc7Smrj 			/*
3991ae115bc7Smrj 			 * We have changed the kernel line, so we may need
3992ae115bc7Smrj 			 * to update the archive line as well.
3993ae115bc7Smrj 			 */
3994ae115bc7Smrj 			set_archive_line(entryp, kernelp);
3995ae115bc7Smrj 		} else {
3996ae115bc7Smrj 			/*
3997ae115bc7Smrj 			 * We're resetting the boot args to nothing, so
3998ae115bc7Smrj 			 * we only need to copy the kernel.  We've already
3999ae115bc7Smrj 			 * checked that the kernel is not the default.
4000ae115bc7Smrj 			 */
4001ae115bc7Smrj 			new_arg = s_calloc(1, old_kernel_len + 1);
4002ae115bc7Smrj 			(void) snprintf(new_arg, old_kernel_len + 1, "%s",
4003ae115bc7Smrj 			    kernelp->arg);
4004ae115bc7Smrj 			free(kernelp->arg);
4005ae115bc7Smrj 			kernelp->arg = new_arg;
4006ae115bc7Smrj 		}
4007ae115bc7Smrj 		rv = BAM_WRITE;
4008ae115bc7Smrj 		goto done;
4009ae115bc7Smrj 	}
4010ae115bc7Smrj 
4011ae115bc7Smrj 	/*
4012ae115bc7Smrj 	 * Expand the kernel file to a full path, if necessary
4013ae115bc7Smrj 	 */
4014ae115bc7Smrj 	if ((optnum == KERNEL_CMD) && (path[0] != '/')) {
4015ae115bc7Smrj 		new_path = expand_path(path);
4016ae115bc7Smrj 		if (new_path == NULL) {
4017455710d3Srscott 			bam_error(UNKNOWN_KERNEL, path);
4018ae115bc7Smrj 			return (BAM_ERROR);
4019ae115bc7Smrj 		}
4020ae115bc7Smrj 		free_new_path = 1;
4021ae115bc7Smrj 	} else {
4022ae115bc7Smrj 		new_path = path;
4023ae115bc7Smrj 		free_new_path = 0;
4024ae115bc7Smrj 	}
4025ae115bc7Smrj 
4026ae115bc7Smrj 	/*
4027ae115bc7Smrj 	 * At this point, we know we're setting a new value.  First, take care
4028ae115bc7Smrj 	 * of the case where there was no previous entry.
4029ae115bc7Smrj 	 */
4030ae115bc7Smrj 	if (entryp == NULL) {
4031ae115bc7Smrj 		/* Similar to code in update_temp */
4032ae115bc7Smrj 		if (stat(GRUB_slice, &sb) != 0) {
4033ae115bc7Smrj 			/*
4034ae115bc7Smrj 			 * 1. First get root disk name from mnttab
4035ae115bc7Smrj 			 * 2. Translate disk name to grub name
4036ae115bc7Smrj 			 * 3. Add the new menu entry
4037ae115bc7Smrj 			 */
4038ae115bc7Smrj 			rootdev = get_special("/");
4039ae115bc7Smrj 			if (rootdev) {
4040ae115bc7Smrj 				grubdisk = os_to_grubdisk(rootdev, 1);
4041ae115bc7Smrj 				free(rootdev);
4042ae115bc7Smrj 			}
4043ae115bc7Smrj 		} else {
4044ae115bc7Smrj 			/*
4045ae115bc7Smrj 			 * This is an LU BE. The GRUB_root file
4046ae115bc7Smrj 			 * contains entry for GRUB's "root" cmd.
4047ae115bc7Smrj 			 */
4048ae115bc7Smrj 			grubdisk = read_grub_root();
4049ae115bc7Smrj 		}
4050ae115bc7Smrj 		if (grubdisk == NULL) {
4051ae115bc7Smrj 			bam_error(REBOOT_WITH_ARGS_FAILED);
4052ae115bc7Smrj 			rv = BAM_ERROR;
4053ae115bc7Smrj 			goto done;
4054ae115bc7Smrj 		}
4055ae115bc7Smrj 		if (optnum == KERNEL_CMD) {
4056ae115bc7Smrj 			entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
4057ae115bc7Smrj 			    grubdisk, new_path, NULL);
4058ae115bc7Smrj 		} else {
4059ae115bc7Smrj 			new_str_len = strlen(DIRECT_BOOT_KERNEL) +
4060ae115bc7Smrj 			    strlen(path) + 8;
4061ae115bc7Smrj 			new_arg = s_calloc(1, new_str_len);
4062ae115bc7Smrj 
4063ae115bc7Smrj 			(void) snprintf(new_arg, new_str_len, "%s %s",
4064ae115bc7Smrj 			    DIRECT_BOOT_KERNEL, path);
4065ae115bc7Smrj 			entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
4066ae115bc7Smrj 			    grubdisk, new_arg, DIRECT_BOOT_ARCHIVE);
4067ae115bc7Smrj 		}
4068ae115bc7Smrj 		save_default_entry(mp, BAM_OLD_RC_DEF);
4069ae115bc7Smrj 		(void) set_global(mp, menu_cmds[DEFAULT_CMD], entryNum);
4070ae115bc7Smrj 		rv = BAM_WRITE;
4071ae115bc7Smrj 		goto done;
4072ae115bc7Smrj 	}
4073ae115bc7Smrj 
4074ae115bc7Smrj 	/*
4075ae115bc7Smrj 	 * There was already an bootenv entry which we need to edit.
4076ae115bc7Smrj 	 */
4077ae115bc7Smrj 	if (optnum == KERNEL_CMD) {
4078ae115bc7Smrj 		new_str_len = strlen(new_path) + strlen(old_args) + 2;
4079ae115bc7Smrj 		new_arg = s_calloc(1, new_str_len);
4080ae115bc7Smrj 		(void) snprintf(new_arg, new_str_len, "%s %s", new_path,
4081ae115bc7Smrj 		    old_args);
4082ae115bc7Smrj 		free(kernelp->arg);
4083ae115bc7Smrj 		kernelp->arg = new_arg;
4084ae115bc7Smrj 
4085ae115bc7Smrj 		/*
4086ae115bc7Smrj 		 * If we have changed the kernel line, we may need to update
4087ae115bc7Smrj 		 * the archive line as well.
4088ae115bc7Smrj 		 */
4089ae115bc7Smrj 		set_archive_line(entryp, kernelp);
4090ae115bc7Smrj 	} else {
4091ae115bc7Smrj 		new_str_len = old_kernel_len + strlen(path) + 8;
4092ae115bc7Smrj 		new_arg = s_calloc(1, new_str_len);
4093ae115bc7Smrj 		(void) strncpy(new_arg, kernelp->arg, old_kernel_len);
4094ae115bc7Smrj 		(void) strlcat(new_arg, " ", new_str_len);
4095ae115bc7Smrj 		(void) strlcat(new_arg, path, new_str_len);
4096ae115bc7Smrj 		free(kernelp->arg);
4097ae115bc7Smrj 		kernelp->arg = new_arg;
4098ae115bc7Smrj 	}
4099ae115bc7Smrj 	rv = BAM_WRITE;
4100ae115bc7Smrj 
4101ae115bc7Smrj done:
4102ae115bc7Smrj 	if ((rv == BAM_WRITE) && kernelp)
4103ae115bc7Smrj 		update_line(kernelp);
4104ae115bc7Smrj 	if (free_new_path)
4105ae115bc7Smrj 		free(new_path);
4106ae115bc7Smrj 	return (rv);
4107ae115bc7Smrj }
4108ae115bc7Smrj 
41097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
41107c478bd9Sstevel@tonic-gate static error_t
41117c478bd9Sstevel@tonic-gate set_option(menu_t *mp, char *menu_path, char *opt)
41127c478bd9Sstevel@tonic-gate {
41137c478bd9Sstevel@tonic-gate 	int optnum, optval;
41147c478bd9Sstevel@tonic-gate 	char *val;
4115ae115bc7Smrj 	char buf[BUFSIZ] = "";
4116ae115bc7Smrj 	error_t rv;
41177c478bd9Sstevel@tonic-gate 
41187c478bd9Sstevel@tonic-gate 	assert(mp);
41197c478bd9Sstevel@tonic-gate 	assert(opt);
41207c478bd9Sstevel@tonic-gate 
41217c478bd9Sstevel@tonic-gate 	val = strchr(opt, '=');
4122ae115bc7Smrj 	if (val != NULL) {
4123ae115bc7Smrj 		*val = '\0';
41247c478bd9Sstevel@tonic-gate 	}
41257c478bd9Sstevel@tonic-gate 
41267c478bd9Sstevel@tonic-gate 	if (strcmp(opt, "default") == 0) {
41277c478bd9Sstevel@tonic-gate 		optnum = DEFAULT_CMD;
41287c478bd9Sstevel@tonic-gate 	} else if (strcmp(opt, "timeout") == 0) {
41297c478bd9Sstevel@tonic-gate 		optnum = TIMEOUT_CMD;
4130ae115bc7Smrj 	} else if (strcmp(opt, menu_cmds[KERNEL_CMD]) == 0) {
4131ae115bc7Smrj 		optnum = KERNEL_CMD;
4132ae115bc7Smrj 	} else if (strcmp(opt, menu_cmds[ARGS_CMD]) == 0) {
4133ae115bc7Smrj 		optnum = ARGS_CMD;
41347c478bd9Sstevel@tonic-gate 	} else {
41357c478bd9Sstevel@tonic-gate 		bam_error(INVALID_ENTRY, opt);
41367c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
41377c478bd9Sstevel@tonic-gate 	}
41387c478bd9Sstevel@tonic-gate 
4139ae115bc7Smrj 	/*
4140ae115bc7Smrj 	 * kernel and args are allowed without "=new_value" strings.  All
4141ae115bc7Smrj 	 * others cause errors
4142ae115bc7Smrj 	 */
4143ae115bc7Smrj 	if ((val == NULL) && (optnum != KERNEL_CMD) && (optnum != ARGS_CMD)) {
4144ae115bc7Smrj 		bam_error(INVALID_ENTRY, opt);
4145ae115bc7Smrj 		return (BAM_ERROR);
4146ae115bc7Smrj 	} else if (val != NULL) {
41477c478bd9Sstevel@tonic-gate 		*val = '=';
4148ae115bc7Smrj 	}
4149ae115bc7Smrj 
4150ae115bc7Smrj 	if ((optnum == KERNEL_CMD) || (optnum == ARGS_CMD)) {
4151ae115bc7Smrj 		rv = set_kernel(mp, optnum, val ? val + 1 : NULL, buf, BUFSIZ);
4152ae115bc7Smrj 		if ((rv == BAM_SUCCESS) && (buf[0] != '\0'))
4153ae115bc7Smrj 			(void) printf("%s\n", buf);
4154ae115bc7Smrj 		return (rv);
4155ae115bc7Smrj 	} else {
4156ae115bc7Smrj 		optval = s_strtol(val + 1);
41577c478bd9Sstevel@tonic-gate 		return (set_global(mp, menu_cmds[optnum], optval));
41587c478bd9Sstevel@tonic-gate 	}
4159ae115bc7Smrj }
41607c478bd9Sstevel@tonic-gate 
41617c478bd9Sstevel@tonic-gate /*
41627c478bd9Sstevel@tonic-gate  * The quiet argument suppresses messages. This is used
41637c478bd9Sstevel@tonic-gate  * when invoked in the context of other commands (e.g. list_entry)
41647c478bd9Sstevel@tonic-gate  */
41657c478bd9Sstevel@tonic-gate static error_t
41667c478bd9Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet)
41677c478bd9Sstevel@tonic-gate {
41687c478bd9Sstevel@tonic-gate 	line_t *lp;
41697c478bd9Sstevel@tonic-gate 	char *arg;
41707c478bd9Sstevel@tonic-gate 	int done, ret = BAM_SUCCESS;
41717c478bd9Sstevel@tonic-gate 
41727c478bd9Sstevel@tonic-gate 	assert(mp);
41737c478bd9Sstevel@tonic-gate 	assert(menu_path);
41747c478bd9Sstevel@tonic-gate 	assert(globalcmd);
41757c478bd9Sstevel@tonic-gate 
41767c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
41777c478bd9Sstevel@tonic-gate 		if (!quiet)
41787c478bd9Sstevel@tonic-gate 			bam_error(NO_MENU, menu_path);
41797c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
41807c478bd9Sstevel@tonic-gate 	}
41817c478bd9Sstevel@tonic-gate 
41827c478bd9Sstevel@tonic-gate 	done = 0;
41837c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
41847c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
41857c478bd9Sstevel@tonic-gate 			continue;
41867c478bd9Sstevel@tonic-gate 
41877c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
41887c478bd9Sstevel@tonic-gate 			if (!quiet)
41897c478bd9Sstevel@tonic-gate 				bam_error(NO_CMD, lp->lineNum);
41907c478bd9Sstevel@tonic-gate 			continue;
41917c478bd9Sstevel@tonic-gate 		}
41927c478bd9Sstevel@tonic-gate 
41937c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
41947c478bd9Sstevel@tonic-gate 			continue;
41957c478bd9Sstevel@tonic-gate 
41967c478bd9Sstevel@tonic-gate 		/* Found global. Check for duplicates */
41977c478bd9Sstevel@tonic-gate 		if (done && !quiet) {
41987c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
41997c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
42007c478bd9Sstevel@tonic-gate 		}
42017c478bd9Sstevel@tonic-gate 
42027c478bd9Sstevel@tonic-gate 		arg = lp->arg ? lp->arg : "";
42037c478bd9Sstevel@tonic-gate 		bam_print(GLOBAL_CMD, globalcmd, arg);
42047c478bd9Sstevel@tonic-gate 		done = 1;
42057c478bd9Sstevel@tonic-gate 	}
42067c478bd9Sstevel@tonic-gate 
42077c478bd9Sstevel@tonic-gate 	if (!done && bam_verbose)
42087c478bd9Sstevel@tonic-gate 		bam_print(NO_ENTRY, globalcmd);
42097c478bd9Sstevel@tonic-gate 
42107c478bd9Sstevel@tonic-gate 	return (ret);
42117c478bd9Sstevel@tonic-gate }
42127c478bd9Sstevel@tonic-gate 
42137c478bd9Sstevel@tonic-gate static error_t
42147c478bd9Sstevel@tonic-gate menu_write(char *root, menu_t *mp)
42157c478bd9Sstevel@tonic-gate {
42167c478bd9Sstevel@tonic-gate 	return (list2file(root, MENU_TMP, GRUB_MENU, mp->start));
42177c478bd9Sstevel@tonic-gate }
42187c478bd9Sstevel@tonic-gate 
42197c478bd9Sstevel@tonic-gate static void
42207c478bd9Sstevel@tonic-gate line_free(line_t *lp)
42217c478bd9Sstevel@tonic-gate {
42227c478bd9Sstevel@tonic-gate 	if (lp == NULL)
42237c478bd9Sstevel@tonic-gate 		return;
42247c478bd9Sstevel@tonic-gate 
42257c478bd9Sstevel@tonic-gate 	if (lp->cmd)
42267c478bd9Sstevel@tonic-gate 		free(lp->cmd);
42277c478bd9Sstevel@tonic-gate 	if (lp->sep)
42287c478bd9Sstevel@tonic-gate 		free(lp->sep);
42297c478bd9Sstevel@tonic-gate 	if (lp->arg)
42307c478bd9Sstevel@tonic-gate 		free(lp->arg);
42317c478bd9Sstevel@tonic-gate 	if (lp->line)
42327c478bd9Sstevel@tonic-gate 		free(lp->line);
42337c478bd9Sstevel@tonic-gate 	free(lp);
42347c478bd9Sstevel@tonic-gate }
42357c478bd9Sstevel@tonic-gate 
42367c478bd9Sstevel@tonic-gate static void
42377c478bd9Sstevel@tonic-gate linelist_free(line_t *start)
42387c478bd9Sstevel@tonic-gate {
42397c478bd9Sstevel@tonic-gate 	line_t *lp;
42407c478bd9Sstevel@tonic-gate 
42417c478bd9Sstevel@tonic-gate 	while (start) {
42427c478bd9Sstevel@tonic-gate 		lp = start;
42437c478bd9Sstevel@tonic-gate 		start = start->next;
42447c478bd9Sstevel@tonic-gate 		line_free(lp);
42457c478bd9Sstevel@tonic-gate 	}
42467c478bd9Sstevel@tonic-gate }
42477c478bd9Sstevel@tonic-gate 
42487c478bd9Sstevel@tonic-gate static void
42497c478bd9Sstevel@tonic-gate filelist_free(filelist_t *flistp)
42507c478bd9Sstevel@tonic-gate {
42517c478bd9Sstevel@tonic-gate 	linelist_free(flistp->head);
42527c478bd9Sstevel@tonic-gate 	flistp->head = NULL;
42537c478bd9Sstevel@tonic-gate 	flistp->tail = NULL;
42547c478bd9Sstevel@tonic-gate }
42557c478bd9Sstevel@tonic-gate 
42567c478bd9Sstevel@tonic-gate static void
42577c478bd9Sstevel@tonic-gate menu_free(menu_t *mp)
42587c478bd9Sstevel@tonic-gate {
42598c1b6884Sszhou 	entry_t *ent, *tmp;
42607c478bd9Sstevel@tonic-gate 	assert(mp);
42617c478bd9Sstevel@tonic-gate 
42627c478bd9Sstevel@tonic-gate 	if (mp->start)
42637c478bd9Sstevel@tonic-gate 		linelist_free(mp->start);
42648c1b6884Sszhou 	ent = mp->entries;
42658c1b6884Sszhou 	while (ent) {
42668c1b6884Sszhou 		tmp = ent;
42678c1b6884Sszhou 		ent = tmp->next;
42688c1b6884Sszhou 		free(tmp);
42698c1b6884Sszhou 	}
42707c478bd9Sstevel@tonic-gate 
42718c1b6884Sszhou 	free(mp);
42727c478bd9Sstevel@tonic-gate }
42737c478bd9Sstevel@tonic-gate 
42747c478bd9Sstevel@tonic-gate /*
42757c478bd9Sstevel@tonic-gate  * Utility routines
42767c478bd9Sstevel@tonic-gate  */
42777c478bd9Sstevel@tonic-gate 
42787c478bd9Sstevel@tonic-gate 
42797c478bd9Sstevel@tonic-gate /*
42807c478bd9Sstevel@tonic-gate  * Returns 0 on success
42817c478bd9Sstevel@tonic-gate  * Any other value indicates an error
42827c478bd9Sstevel@tonic-gate  */
42837c478bd9Sstevel@tonic-gate static int
42847c478bd9Sstevel@tonic-gate exec_cmd(char *cmdline, char *output, int64_t osize)
42857c478bd9Sstevel@tonic-gate {
42867c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
42877c478bd9Sstevel@tonic-gate 	int ret;
42887c478bd9Sstevel@tonic-gate 	FILE *ptr;
42897c478bd9Sstevel@tonic-gate 	size_t len;
42907c478bd9Sstevel@tonic-gate 	sigset_t set;
42917c478bd9Sstevel@tonic-gate 	void (*disp)(int);
42927c478bd9Sstevel@tonic-gate 
42937c478bd9Sstevel@tonic-gate 	/*
42947c478bd9Sstevel@tonic-gate 	 * For security
42957c478bd9Sstevel@tonic-gate 	 * - only absolute paths are allowed
42967c478bd9Sstevel@tonic-gate 	 * - set IFS to space and tab
42977c478bd9Sstevel@tonic-gate 	 */
42987c478bd9Sstevel@tonic-gate 	if (*cmdline != '/') {
42997c478bd9Sstevel@tonic-gate 		bam_error(ABS_PATH_REQ, cmdline);
43007c478bd9Sstevel@tonic-gate 		return (-1);
43017c478bd9Sstevel@tonic-gate 	}
43027c478bd9Sstevel@tonic-gate 	(void) putenv("IFS= \t");
43037c478bd9Sstevel@tonic-gate 
43047c478bd9Sstevel@tonic-gate 	/*
43057c478bd9Sstevel@tonic-gate 	 * We may have been exec'ed with SIGCHLD blocked
43067c478bd9Sstevel@tonic-gate 	 * unblock it here
43077c478bd9Sstevel@tonic-gate 	 */
43087c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
43097c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
43107c478bd9Sstevel@tonic-gate 	if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
43117c478bd9Sstevel@tonic-gate 		bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno));
43127c478bd9Sstevel@tonic-gate 		return (-1);
43137c478bd9Sstevel@tonic-gate 	}
43147c478bd9Sstevel@tonic-gate 
43157c478bd9Sstevel@tonic-gate 	/*
43167c478bd9Sstevel@tonic-gate 	 * Set SIGCHLD disposition to SIG_DFL for popen/pclose
43177c478bd9Sstevel@tonic-gate 	 */
43187c478bd9Sstevel@tonic-gate 	disp = sigset(SIGCHLD, SIG_DFL);
43197c478bd9Sstevel@tonic-gate 	if (disp == SIG_ERR) {
43207c478bd9Sstevel@tonic-gate 		bam_error(FAILED_SIG, strerror(errno));
43217c478bd9Sstevel@tonic-gate 		return (-1);
43227c478bd9Sstevel@tonic-gate 	}
43237c478bd9Sstevel@tonic-gate 	if (disp == SIG_HOLD) {
43247c478bd9Sstevel@tonic-gate 		bam_error(BLOCKED_SIG, cmdline);
43257c478bd9Sstevel@tonic-gate 		return (-1);
43267c478bd9Sstevel@tonic-gate 	}
43277c478bd9Sstevel@tonic-gate 
43287c478bd9Sstevel@tonic-gate 	ptr = popen(cmdline, "r");
43297c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
43307c478bd9Sstevel@tonic-gate 		bam_error(POPEN_FAIL, cmdline, strerror(errno));
43317c478bd9Sstevel@tonic-gate 		return (-1);
43327c478bd9Sstevel@tonic-gate 	}
43337c478bd9Sstevel@tonic-gate 
43347c478bd9Sstevel@tonic-gate 	/*
43357c478bd9Sstevel@tonic-gate 	 * If we simply do a pclose() following a popen(), pclose()
43367c478bd9Sstevel@tonic-gate 	 * will close the reader end of the pipe immediately even
43377c478bd9Sstevel@tonic-gate 	 * if the child process has not started/exited. pclose()
43387c478bd9Sstevel@tonic-gate 	 * does wait for cmd to terminate before returning though.
43397c478bd9Sstevel@tonic-gate 	 * When the executed command writes its output to the pipe
43407c478bd9Sstevel@tonic-gate 	 * there is no reader process and the command dies with
43417c478bd9Sstevel@tonic-gate 	 * SIGPIPE. To avoid this we read repeatedly until read
43427c478bd9Sstevel@tonic-gate 	 * terminates with EOF. This indicates that the command
43437c478bd9Sstevel@tonic-gate 	 * (writer) has closed the pipe and we can safely do a
43447c478bd9Sstevel@tonic-gate 	 * pclose().
43457c478bd9Sstevel@tonic-gate 	 *
43467c478bd9Sstevel@tonic-gate 	 * Since pclose() does wait for the command to exit,
43477c478bd9Sstevel@tonic-gate 	 * we can safely reap the exit status of the command
43487c478bd9Sstevel@tonic-gate 	 * from the value returned by pclose()
43497c478bd9Sstevel@tonic-gate 	 */
43507c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), ptr) != NULL) {
43517c478bd9Sstevel@tonic-gate 		/* if (bam_verbose)  XXX */
43527c478bd9Sstevel@tonic-gate 			bam_print(PRINT_NO_NEWLINE, buf);
43537c478bd9Sstevel@tonic-gate 		if (output && osize > 0) {
43547c478bd9Sstevel@tonic-gate 			(void) snprintf(output, osize, "%s", buf);
43557c478bd9Sstevel@tonic-gate 			len = strlen(buf);
43567c478bd9Sstevel@tonic-gate 			output += len;
43577c478bd9Sstevel@tonic-gate 			osize -= len;
43587c478bd9Sstevel@tonic-gate 		}
43597c478bd9Sstevel@tonic-gate 	}
43607c478bd9Sstevel@tonic-gate 
43617c478bd9Sstevel@tonic-gate 	ret = pclose(ptr);
43627c478bd9Sstevel@tonic-gate 	if (ret == -1) {
43637c478bd9Sstevel@tonic-gate 		bam_error(PCLOSE_FAIL, cmdline, strerror(errno));
43647c478bd9Sstevel@tonic-gate 		return (-1);
43657c478bd9Sstevel@tonic-gate 	}
43667c478bd9Sstevel@tonic-gate 
43677c478bd9Sstevel@tonic-gate 	if (WIFEXITED(ret)) {
43687c478bd9Sstevel@tonic-gate 		return (WEXITSTATUS(ret));
43697c478bd9Sstevel@tonic-gate 	} else {
43707c478bd9Sstevel@tonic-gate 		bam_error(EXEC_FAIL, cmdline, ret);
43717c478bd9Sstevel@tonic-gate 		return (-1);
43727c478bd9Sstevel@tonic-gate 	}
43737c478bd9Sstevel@tonic-gate }
43747c478bd9Sstevel@tonic-gate 
43757c478bd9Sstevel@tonic-gate /*
43767c478bd9Sstevel@tonic-gate  * Since this function returns -1 on error
43777c478bd9Sstevel@tonic-gate  * it cannot be used to convert -1. However,
43787c478bd9Sstevel@tonic-gate  * that is sufficient for what we need.
43797c478bd9Sstevel@tonic-gate  */
43807c478bd9Sstevel@tonic-gate static long
43817c478bd9Sstevel@tonic-gate s_strtol(char *str)
43827c478bd9Sstevel@tonic-gate {
43837c478bd9Sstevel@tonic-gate 	long l;
43847c478bd9Sstevel@tonic-gate 	char *res = NULL;
43857c478bd9Sstevel@tonic-gate 
43867c478bd9Sstevel@tonic-gate 	if (str == NULL) {
43877c478bd9Sstevel@tonic-gate 		return (-1);
43887c478bd9Sstevel@tonic-gate 	}
43897c478bd9Sstevel@tonic-gate 
43907c478bd9Sstevel@tonic-gate 	errno = 0;
43917c478bd9Sstevel@tonic-gate 	l = strtol(str, &res, 10);
43927c478bd9Sstevel@tonic-gate 	if (errno || *res != '\0') {
43937c478bd9Sstevel@tonic-gate 		return (-1);
43947c478bd9Sstevel@tonic-gate 	}
43957c478bd9Sstevel@tonic-gate 
43967c478bd9Sstevel@tonic-gate 	return (l);
43977c478bd9Sstevel@tonic-gate }
43987c478bd9Sstevel@tonic-gate 
43997c478bd9Sstevel@tonic-gate /*
44007c478bd9Sstevel@tonic-gate  * Wrapper around fputs, that adds a newline (since fputs doesn't)
44017c478bd9Sstevel@tonic-gate  */
44027c478bd9Sstevel@tonic-gate static int
44037c478bd9Sstevel@tonic-gate s_fputs(char *str, FILE *fp)
44047c478bd9Sstevel@tonic-gate {
44057c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
44067c478bd9Sstevel@tonic-gate 
44077c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s\n", str);
44087c478bd9Sstevel@tonic-gate 	return (fputs(linebuf, fp));
44097c478bd9Sstevel@tonic-gate }
44107c478bd9Sstevel@tonic-gate 
44117c478bd9Sstevel@tonic-gate /*
44127c478bd9Sstevel@tonic-gate  * Wrapper around fgets, that strips newlines returned by fgets
44137c478bd9Sstevel@tonic-gate  */
4414ae115bc7Smrj char *
44157c478bd9Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp)
44167c478bd9Sstevel@tonic-gate {
44177c478bd9Sstevel@tonic-gate 	int n;
44187c478bd9Sstevel@tonic-gate 
44197c478bd9Sstevel@tonic-gate 	buf = fgets(buf, buflen, fp);
44207c478bd9Sstevel@tonic-gate 	if (buf) {
44217c478bd9Sstevel@tonic-gate 		n = strlen(buf);
44227c478bd9Sstevel@tonic-gate 		if (n == buflen - 1 && buf[n-1] != '\n')
44237c478bd9Sstevel@tonic-gate 			bam_error(TOO_LONG, buflen - 1, buf);
44247c478bd9Sstevel@tonic-gate 		buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1];
44257c478bd9Sstevel@tonic-gate 	}
44267c478bd9Sstevel@tonic-gate 
44277c478bd9Sstevel@tonic-gate 	return (buf);
44287c478bd9Sstevel@tonic-gate }
44297c478bd9Sstevel@tonic-gate 
4430ae115bc7Smrj void *
44317c478bd9Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz)
44327c478bd9Sstevel@tonic-gate {
44337c478bd9Sstevel@tonic-gate 	void *ptr;
44347c478bd9Sstevel@tonic-gate 
44357c478bd9Sstevel@tonic-gate 	ptr = calloc(nelem, sz);
44367c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
44377c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, nelem*sz);
44387c478bd9Sstevel@tonic-gate 		bam_exit(1);
44397c478bd9Sstevel@tonic-gate 	}
44407c478bd9Sstevel@tonic-gate 	return (ptr);
44417c478bd9Sstevel@tonic-gate }
44427c478bd9Sstevel@tonic-gate 
4443ae115bc7Smrj void *
4444ae115bc7Smrj s_realloc(void *ptr, size_t sz)
4445ae115bc7Smrj {
4446ae115bc7Smrj 	ptr = realloc(ptr, sz);
4447ae115bc7Smrj 	if (ptr == NULL) {
4448ae115bc7Smrj 		bam_error(NO_MEM, sz);
4449ae115bc7Smrj 		bam_exit(1);
4450ae115bc7Smrj 	}
4451ae115bc7Smrj 	return (ptr);
4452ae115bc7Smrj }
4453ae115bc7Smrj 
44547c478bd9Sstevel@tonic-gate static char *
44557c478bd9Sstevel@tonic-gate s_strdup(char *str)
44567c478bd9Sstevel@tonic-gate {
44577c478bd9Sstevel@tonic-gate 	char *ptr;
44587c478bd9Sstevel@tonic-gate 
44597c478bd9Sstevel@tonic-gate 	if (str == NULL)
44607c478bd9Sstevel@tonic-gate 		return (NULL);
44617c478bd9Sstevel@tonic-gate 
44627c478bd9Sstevel@tonic-gate 	ptr = strdup(str);
44637c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
44647c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, strlen(str) + 1);
44657c478bd9Sstevel@tonic-gate 		bam_exit(1);
44667c478bd9Sstevel@tonic-gate 	}
44677c478bd9Sstevel@tonic-gate 	return (ptr);
44687c478bd9Sstevel@tonic-gate }
44697c478bd9Sstevel@tonic-gate 
44707c478bd9Sstevel@tonic-gate /*
44717c478bd9Sstevel@tonic-gate  * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients)
44727c478bd9Sstevel@tonic-gate  * Returns 0 otherwise
44737c478bd9Sstevel@tonic-gate  */
44747c478bd9Sstevel@tonic-gate static int
44757c478bd9Sstevel@tonic-gate is_amd64(void)
44767c478bd9Sstevel@tonic-gate {
44777c478bd9Sstevel@tonic-gate 	static int amd64 = -1;
44787c478bd9Sstevel@tonic-gate 	char isabuf[257];	/* from sysinfo(2) manpage */
44797c478bd9Sstevel@tonic-gate 
44807c478bd9Sstevel@tonic-gate 	if (amd64 != -1)
44817c478bd9Sstevel@tonic-gate 		return (amd64);
44827c478bd9Sstevel@tonic-gate 
44837c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 &&
44847c478bd9Sstevel@tonic-gate 	    strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0)
44857c478bd9Sstevel@tonic-gate 		amd64 = 1;
44867c478bd9Sstevel@tonic-gate 	else if (strstr(isabuf, "i386") == NULL)
44877c478bd9Sstevel@tonic-gate 		amd64 = 1;		/* diskless server */
44887c478bd9Sstevel@tonic-gate 	else
44897c478bd9Sstevel@tonic-gate 		amd64 = 0;
44907c478bd9Sstevel@tonic-gate 
44917c478bd9Sstevel@tonic-gate 	return (amd64);
44927c478bd9Sstevel@tonic-gate }
44937c478bd9Sstevel@tonic-gate 
44947c478bd9Sstevel@tonic-gate static void
44957c478bd9Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s)
44967c478bd9Sstevel@tonic-gate {
44977c478bd9Sstevel@tonic-gate 	line_t *lp;
44987c478bd9Sstevel@tonic-gate 
44997c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
45007c478bd9Sstevel@tonic-gate 	lp->line = s_strdup(s);
45017c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL)
45027c478bd9Sstevel@tonic-gate 		flistp->head = lp;
45037c478bd9Sstevel@tonic-gate 	else
45047c478bd9Sstevel@tonic-gate 		flistp->tail->next = lp;
45057c478bd9Sstevel@tonic-gate 	flistp->tail = lp;
45067c478bd9Sstevel@tonic-gate }
4507