xref: /onnv-gate/usr/src/cmd/boot/bootadm/bootadm.c (revision 2115:a40e8f141552)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51746Svikram  * Common Development and Distribution License (the "License").
61746Svikram  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221746Svikram  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * bootadm(1M) is a new utility for managing bootability of
300Sstevel@tonic-gate  * Solaris *Newboot* environments. It has two primary tasks:
310Sstevel@tonic-gate  * 	- Allow end users to manage bootability of Newboot Solaris instances
320Sstevel@tonic-gate  *	- Provide services to other subsystems in Solaris (primarily Install)
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate /* Headers */
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate #include <stdlib.h>
390Sstevel@tonic-gate #include <string.h>
400Sstevel@tonic-gate #include <unistd.h>
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <sys/stat.h>
430Sstevel@tonic-gate #include <stdarg.h>
440Sstevel@tonic-gate #include <limits.h>
450Sstevel@tonic-gate #include <signal.h>
460Sstevel@tonic-gate #include <sys/wait.h>
470Sstevel@tonic-gate #include <sys/mnttab.h>
480Sstevel@tonic-gate #include <sys/statvfs.h>
490Sstevel@tonic-gate #include <libnvpair.h>
500Sstevel@tonic-gate #include <ftw.h>
510Sstevel@tonic-gate #include <fcntl.h>
520Sstevel@tonic-gate #include <strings.h>
530Sstevel@tonic-gate #include <sys/systeminfo.h>
540Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #include <pwd.h>
570Sstevel@tonic-gate #include <grp.h>
580Sstevel@tonic-gate #include <device_info.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #include <libintl.h>
610Sstevel@tonic-gate #include <locale.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #include <assert.h>
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #include "message.h"
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #ifndef TEXT_DOMAIN
680Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
690Sstevel@tonic-gate #endif	/* TEXT_DOMAIN */
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /* Type definitions */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /* Primary subcmds */
740Sstevel@tonic-gate typedef enum {
750Sstevel@tonic-gate 	BAM_MENU = 3,
760Sstevel@tonic-gate 	BAM_ARCHIVE
770Sstevel@tonic-gate } subcmd_t;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /* GRUB menu per-line classification */
800Sstevel@tonic-gate typedef enum {
810Sstevel@tonic-gate 	BAM_INVALID = 0,
820Sstevel@tonic-gate 	BAM_EMPTY,
830Sstevel@tonic-gate 	BAM_COMMENT,
840Sstevel@tonic-gate 	BAM_GLOBAL,
850Sstevel@tonic-gate 	BAM_ENTRY,
860Sstevel@tonic-gate 	BAM_TITLE
870Sstevel@tonic-gate } menu_flag_t;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /* struct for menu.lst contents */
900Sstevel@tonic-gate typedef struct line {
910Sstevel@tonic-gate 	int  lineNum;	/* Line number in menu.lst */
920Sstevel@tonic-gate 	int  entryNum;	/* menu boot entry #. ENTRY_INIT if not applicable */
930Sstevel@tonic-gate 	char *cmd;
940Sstevel@tonic-gate 	char *sep;
950Sstevel@tonic-gate 	char *arg;
960Sstevel@tonic-gate 	char *line;
970Sstevel@tonic-gate 	menu_flag_t flags;
980Sstevel@tonic-gate 	struct line *next;
99662Sszhou 	struct line *prev;
1000Sstevel@tonic-gate } line_t;
1010Sstevel@tonic-gate 
102662Sszhou typedef struct entry {
103662Sszhou 	struct entry *next;
104662Sszhou 	struct entry *prev;
105662Sszhou 	line_t *start;
106662Sszhou 	line_t *end;
107662Sszhou } entry_t;
108662Sszhou 
1090Sstevel@tonic-gate typedef struct {
1100Sstevel@tonic-gate 	line_t *start;
1110Sstevel@tonic-gate 	line_t *end;
112662Sszhou 	line_t *curdefault;	/* line containing default */
113662Sszhou 	line_t *olddefault;	/* old default line (commented) */
114662Sszhou 	entry_t *entries;	/* os entries */
1150Sstevel@tonic-gate } menu_t;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate typedef enum {
1180Sstevel@tonic-gate     OPT_ABSENT = 0,	/* No option */
1190Sstevel@tonic-gate     OPT_REQ,		/* option required */
1200Sstevel@tonic-gate     OPT_OPTIONAL	/* option may or may not be present */
1210Sstevel@tonic-gate } option_t;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate typedef enum {
124316Svikram     BAM_ERROR = -1,	/* Must be negative. add_boot_entry() depends on it */
1250Sstevel@tonic-gate     BAM_SUCCESS = 0,
1260Sstevel@tonic-gate     BAM_WRITE = 2
1270Sstevel@tonic-gate } error_t;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate typedef struct {
1300Sstevel@tonic-gate 	char	*subcmd;
1310Sstevel@tonic-gate 	option_t option;
1320Sstevel@tonic-gate 	error_t (*handler)();
133*2115Svikram 	int	unpriv;			/* is this an unprivileged command */
1340Sstevel@tonic-gate } subcmd_defn_t;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate #define	BAM_MAXLINE	8192
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate #define	LINE_INIT	0	/* lineNum initial value */
1400Sstevel@tonic-gate #define	ENTRY_INIT	-1	/* entryNum initial value */
1410Sstevel@tonic-gate #define	ALL_ENTRIES	-2	/* selects all boot entries */
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate #define	GRUB_DIR		"/boot/grub"
1440Sstevel@tonic-gate #define	MULTI_BOOT		"/platform/i86pc/multiboot"
1450Sstevel@tonic-gate #define	BOOT_ARCHIVE		"/platform/i86pc/boot_archive"
1460Sstevel@tonic-gate #define	GRUB_MENU		"/boot/grub/menu.lst"
1470Sstevel@tonic-gate #define	MENU_TMP		"/boot/grub/menu.lst.tmp"
1480Sstevel@tonic-gate #define	RAMDISK_SPECIAL		"/ramdisk"
149621Svikram #define	STUBBOOT		"/stubboot"
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate /* lock related */
1520Sstevel@tonic-gate #define	BAM_LOCK_FILE		"/var/run/bootadm.lock"
1530Sstevel@tonic-gate #define	LOCK_FILE_PERMS		(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate #define	CREATE_RAMDISK		"/boot/solaris/bin/create_ramdisk"
1560Sstevel@tonic-gate #define	CREATE_DISKMAP		"/boot/solaris/bin/create_diskmap"
1570Sstevel@tonic-gate #define	GRUBDISK_MAP		"/var/run/solaris_grubdisk.map"
1580Sstevel@tonic-gate 
159316Svikram #define	GRUB_slice		"/etc/lu/GRUB_slice"
160316Svikram #define	GRUB_root		"/etc/lu/GRUB_root"
161316Svikram #define	GRUB_backup_menu	"/etc/lu/GRUB_backup_menu"
162316Svikram #define	GRUB_slice_mntpt	"/tmp/GRUB_slice_mntpt"
163316Svikram #define	LU_ACTIVATE_FILE	"/etc/lu/DelayUpdate/activate.sh"
1641746Svikram #define	GRUB_fdisk		"/etc/lu/GRUB_fdisk"
1651746Svikram #define	GRUB_fdisk_target	"/etc/lu/GRUB_fdisk_target"
166316Svikram 
167316Svikram #define	INSTALLGRUB		"/sbin/installgrub"
168316Svikram #define	STAGE1			"/boot/grub/stage1"
169316Svikram #define	STAGE2			"/boot/grub/stage2"
170316Svikram 
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  * Default file attributes
1730Sstevel@tonic-gate  */
1740Sstevel@tonic-gate #define	DEFAULT_DEV_MODE	0644	/* default permissions */
1750Sstevel@tonic-gate #define	DEFAULT_DEV_UID		0	/* user root */
1760Sstevel@tonic-gate #define	DEFAULT_DEV_GID		3	/* group sys */
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate /*
1790Sstevel@tonic-gate  * Menu related
1800Sstevel@tonic-gate  * menu_cmd_t and menu_cmds must be kept in sync
1810Sstevel@tonic-gate  */
1820Sstevel@tonic-gate typedef enum {
1830Sstevel@tonic-gate 	DEFAULT_CMD = 0,
1840Sstevel@tonic-gate 	TIMEOUT_CMD,
1850Sstevel@tonic-gate 	TITLE_CMD,
1860Sstevel@tonic-gate 	ROOT_CMD,
1870Sstevel@tonic-gate 	KERNEL_CMD,
1880Sstevel@tonic-gate 	MODULE_CMD,
1890Sstevel@tonic-gate 	SEP_CMD,
1900Sstevel@tonic-gate 	COMMENT_CMD
1910Sstevel@tonic-gate } menu_cmd_t;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate static char *menu_cmds[] = {
1940Sstevel@tonic-gate 	"default",	/* DEFAULT_CMD */
1950Sstevel@tonic-gate 	"timeout",	/* TIMEOUT_CMD */
1960Sstevel@tonic-gate 	"title",	/* TITLE_CMD */
1970Sstevel@tonic-gate 	"root",		/* ROOT_CMD */
1980Sstevel@tonic-gate 	"kernel",	/* KERNEL_CMD */
1990Sstevel@tonic-gate 	"module",	/* MODULE_CMD */
2000Sstevel@tonic-gate 	" ",		/* SEP_CMD */
2010Sstevel@tonic-gate 	"#",		/* COMMENT_CMD */
2020Sstevel@tonic-gate 	NULL
2030Sstevel@tonic-gate };
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate #define	OPT_ENTRY_NUM	"entry"
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate  * archive related
2090Sstevel@tonic-gate  */
2100Sstevel@tonic-gate typedef struct {
2110Sstevel@tonic-gate 	line_t *head;
2120Sstevel@tonic-gate 	line_t *tail;
2130Sstevel@tonic-gate } filelist_t;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate #define	BOOT_FILE_LIST	"boot/solaris/filelist.ramdisk"
2160Sstevel@tonic-gate #define	ETC_FILE_LIST	"etc/boot/solaris/filelist.ramdisk"
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate #define	FILE_STAT	"boot/solaris/filestat.ramdisk"
2190Sstevel@tonic-gate #define	FILE_STAT_TMP	"boot/solaris/filestat.ramdisk.tmp"
2200Sstevel@tonic-gate #define	DIR_PERMS	(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
2210Sstevel@tonic-gate #define	FILE_STAT_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate #define	BAM_HDR		"---------- ADDED BY BOOTADM - DO NOT EDIT ----------"
2240Sstevel@tonic-gate #define	BAM_FTR		"---------------------END BOOTADM--------------------"
225662Sszhou #define	BAM_OLDDEF	"BOOTADM SAVED DEFAULT: "
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate /* Globals */
2280Sstevel@tonic-gate static char *prog;
2290Sstevel@tonic-gate static subcmd_t bam_cmd;
2300Sstevel@tonic-gate static char *bam_root;
2310Sstevel@tonic-gate static int bam_rootlen;
2320Sstevel@tonic-gate static int bam_root_readonly;
233621Svikram static int bam_alt_root;
2340Sstevel@tonic-gate static char *bam_subcmd;
2350Sstevel@tonic-gate static char *bam_opt;
2360Sstevel@tonic-gate static int bam_debug;
2370Sstevel@tonic-gate static char **bam_argv;
2380Sstevel@tonic-gate static int bam_argc;
2390Sstevel@tonic-gate static int bam_force;
2400Sstevel@tonic-gate static int bam_verbose;
2410Sstevel@tonic-gate static int bam_check;
2420Sstevel@tonic-gate static int bam_smf_check;
2430Sstevel@tonic-gate static int bam_lock_fd = -1;
2440Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/";
245316Svikram static int bam_update_all;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate /* function prototypes */
2480Sstevel@tonic-gate static void parse_args_internal(int argc, char *argv[]);
2490Sstevel@tonic-gate static void parse_args(int argc, char *argv[]);
2500Sstevel@tonic-gate static error_t bam_menu(char *subcmd, char *opt, int argc, char *argv[]);
2510Sstevel@tonic-gate static error_t bam_archive(char *subcmd, char *opt);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate static void bam_error(char *format, ...);
2540Sstevel@tonic-gate static void bam_print(char *format, ...);
2550Sstevel@tonic-gate static void bam_exit(int excode);
2560Sstevel@tonic-gate static void bam_lock(void);
2570Sstevel@tonic-gate static void bam_unlock(void);
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate static int exec_cmd(char *cmdline, char *output, int64_t osize);
2600Sstevel@tonic-gate static error_t read_globals(menu_t *mp, char *menu_path,
2610Sstevel@tonic-gate     char *globalcmd, int quiet);
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate static menu_t *menu_read(char *menu_path);
2640Sstevel@tonic-gate static error_t menu_write(char *root, menu_t *mp);
2650Sstevel@tonic-gate static void linelist_free(line_t *start);
2660Sstevel@tonic-gate static void menu_free(menu_t *mp);
2670Sstevel@tonic-gate static void line_free(line_t *lp);
2680Sstevel@tonic-gate static void filelist_free(filelist_t *flistp);
2690Sstevel@tonic-gate static error_t list2file(char *root, char *tmp,
2700Sstevel@tonic-gate     char *final, line_t *start);
2710Sstevel@tonic-gate static error_t list_entry(menu_t *mp, char *menu_path, char *opt);
2720Sstevel@tonic-gate static error_t delete_all_entries(menu_t *mp, char *menu_path, char *opt);
2730Sstevel@tonic-gate static error_t update_entry(menu_t *mp, char *root, char *opt);
2740Sstevel@tonic-gate static error_t update_temp(menu_t *mp, char *root, char *opt);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate static error_t update_archive(char *root, char *opt);
2770Sstevel@tonic-gate static error_t list_archive(char *root, char *opt);
2780Sstevel@tonic-gate static error_t update_all(char *root, char *opt);
2790Sstevel@tonic-gate static error_t read_list(char *root, filelist_t  *flistp);
2800Sstevel@tonic-gate static error_t set_global(menu_t *mp, char *globalcmd, int val);
2810Sstevel@tonic-gate static error_t set_option(menu_t *mp, char *globalcmd, char *opt);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate static long s_strtol(char *str);
2840Sstevel@tonic-gate static char *s_fgets(char *buf, int n, FILE *fp);
2850Sstevel@tonic-gate static int s_fputs(char *str, FILE *fp);
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate static void *s_calloc(size_t nelem, size_t sz);
2880Sstevel@tonic-gate static char *s_strdup(char *str);
2890Sstevel@tonic-gate static int is_readonly(char *);
2900Sstevel@tonic-gate static int is_amd64(void);
2910Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate #if defined(__sparc)
2940Sstevel@tonic-gate static void sparc_abort(void);
2950Sstevel@tonic-gate #endif
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /* Menu related sub commands */
2980Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = {
299*2115Svikram 	"set_option",		OPT_OPTIONAL,	set_option, 0,	/* PUB */
300*2115Svikram 	"list_entry",		OPT_OPTIONAL,	list_entry, 1,	/* PUB */
301*2115Svikram 	"delete_all_entries",	OPT_ABSENT,	delete_all_entries, 0, /* PVT */
302*2115Svikram 	"update_entry",		OPT_REQ,	update_entry, 0, /* menu */
303*2115Svikram 	"update_temp",		OPT_OPTIONAL,	update_temp, 0,	/* reboot */
304*2115Svikram 	NULL,			0,		NULL, 0	/* must be last */
3050Sstevel@tonic-gate };
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate /* Archive related sub commands */
3080Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = {
309*2115Svikram 	"update",		OPT_ABSENT,	update_archive, 0, /* PUB */
310*2115Svikram 	"update_all",		OPT_ABSENT,	update_all, 0,	/* PVT */
311*2115Svikram 	"list",			OPT_OPTIONAL,	list_archive, 1, /* PUB */
312*2115Svikram 	NULL,			0,		NULL, 0	/* must be last */
3130Sstevel@tonic-gate };
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate static struct {
3160Sstevel@tonic-gate 	nvlist_t *new_nvlp;
3170Sstevel@tonic-gate 	nvlist_t *old_nvlp;
3180Sstevel@tonic-gate 	int need_update;
3190Sstevel@tonic-gate } walk_arg;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate static void
3220Sstevel@tonic-gate usage(void)
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate 	(void) fprintf(stderr, "USAGE:\n");
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	/* archive usage */
3280Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s update-archive [-vn] [-R altroot]\n",
3290Sstevel@tonic-gate 	    prog);
3300Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-archive [-R altroot]\n", prog);
3310Sstevel@tonic-gate #ifndef __sparc
3320Sstevel@tonic-gate 	/* x86 only */
3330Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog);
3340Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog);
3350Sstevel@tonic-gate #endif
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate int
3390Sstevel@tonic-gate main(int argc, char *argv[])
3400Sstevel@tonic-gate {
3410Sstevel@tonic-gate 	error_t ret;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3440Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
3470Sstevel@tonic-gate 		prog = argv[0];
3480Sstevel@tonic-gate 	} else {
3490Sstevel@tonic-gate 		prog++;
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	/*
3540Sstevel@tonic-gate 	 * Don't depend on caller's umask
3550Sstevel@tonic-gate 	 */
3560Sstevel@tonic-gate 	(void) umask(0022);
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	parse_args(argc, argv);
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate #if defined(__sparc)
3610Sstevel@tonic-gate 	/*
3620Sstevel@tonic-gate 	 * There are only two valid invocations of bootadm
3630Sstevel@tonic-gate 	 * on SPARC:
3640Sstevel@tonic-gate 	 *
3650Sstevel@tonic-gate 	 *	- SPARC diskless server creating boot_archive for i386 clients
3660Sstevel@tonic-gate 	 *	- archive creation call during reboot of a SPARC system
3670Sstevel@tonic-gate 	 *
3680Sstevel@tonic-gate 	 *	The latter should be a NOP
3690Sstevel@tonic-gate 	 */
3700Sstevel@tonic-gate 	if (bam_cmd != BAM_ARCHIVE) {
3710Sstevel@tonic-gate 		sparc_abort();
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate #endif
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	switch (bam_cmd) {
3760Sstevel@tonic-gate 		case BAM_MENU:
3770Sstevel@tonic-gate 			ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv);
3780Sstevel@tonic-gate 			break;
3790Sstevel@tonic-gate 		case BAM_ARCHIVE:
3800Sstevel@tonic-gate 			ret = bam_archive(bam_subcmd, bam_opt);
3810Sstevel@tonic-gate 			break;
3820Sstevel@tonic-gate 		default:
3830Sstevel@tonic-gate 			usage();
3840Sstevel@tonic-gate 			bam_exit(1);
3850Sstevel@tonic-gate 	}
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	if (ret != BAM_SUCCESS)
3880Sstevel@tonic-gate 		bam_exit(1);
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	bam_unlock();
3910Sstevel@tonic-gate 	return (0);
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate #if defined(__sparc)
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate static void
3970Sstevel@tonic-gate sparc_abort(void)
3980Sstevel@tonic-gate {
3990Sstevel@tonic-gate 	bam_error(NOT_ON_SPARC);
4000Sstevel@tonic-gate 	bam_exit(1);
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate #endif
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate /*
4060Sstevel@tonic-gate  * Equivalence of public and internal commands:
4070Sstevel@tonic-gate  *	update-archive  -- -a update
4080Sstevel@tonic-gate  *	list-archive	-- -a list
4090Sstevel@tonic-gate  *	set-menu	-- -m set_option
4100Sstevel@tonic-gate  *	list-menu	-- -m list_entry
4110Sstevel@tonic-gate  *	update-menu	-- -m update_entry
4120Sstevel@tonic-gate  */
4130Sstevel@tonic-gate static struct cmd_map {
4140Sstevel@tonic-gate 	char *bam_cmdname;
4150Sstevel@tonic-gate 	int bam_cmd;
4160Sstevel@tonic-gate 	char *bam_subcmd;
4170Sstevel@tonic-gate } cmd_map[] = {
4180Sstevel@tonic-gate 	{ "update-archive",	BAM_ARCHIVE,	"update"},
4190Sstevel@tonic-gate 	{ "list-archive",	BAM_ARCHIVE,	"list"},
4200Sstevel@tonic-gate 	{ "set-menu",		BAM_MENU,	"set_option"},
4210Sstevel@tonic-gate 	{ "list-menu",		BAM_MENU,	"list_entry"},
4220Sstevel@tonic-gate 	{ "update-menu",	BAM_MENU,	"update_entry"},
4230Sstevel@tonic-gate 	{ NULL,			0,		NULL}
4240Sstevel@tonic-gate };
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate /*
4270Sstevel@tonic-gate  * Commands syntax published in bootadm(1M) are parsed here
4280Sstevel@tonic-gate  */
4290Sstevel@tonic-gate static void
4300Sstevel@tonic-gate parse_args(int argc, char *argv[])
4310Sstevel@tonic-gate {
4320Sstevel@tonic-gate 	struct cmd_map *cmp = cmd_map;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	/* command conforming to the final spec */
4350Sstevel@tonic-gate 	if (argc > 1 && argv[1][0] != '-') {
4360Sstevel@tonic-gate 		/*
4370Sstevel@tonic-gate 		 * Map commands to internal table.
4380Sstevel@tonic-gate 		 */
4390Sstevel@tonic-gate 		while (cmp->bam_cmdname) {
4400Sstevel@tonic-gate 			if (strcmp(argv[1], cmp->bam_cmdname) == 0) {
4410Sstevel@tonic-gate 				bam_cmd = cmp->bam_cmd;
4420Sstevel@tonic-gate 				bam_subcmd = cmp->bam_subcmd;
4430Sstevel@tonic-gate 				break;
4440Sstevel@tonic-gate 			}
4450Sstevel@tonic-gate 			cmp++;
4460Sstevel@tonic-gate 		}
4470Sstevel@tonic-gate 		if (cmp->bam_cmdname == NULL) {
4480Sstevel@tonic-gate 			usage();
4490Sstevel@tonic-gate 			bam_exit(1);
4500Sstevel@tonic-gate 		}
4510Sstevel@tonic-gate 		argc--;
4520Sstevel@tonic-gate 		argv++;
4530Sstevel@tonic-gate 	}
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	parse_args_internal(argc, argv);
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate /*
4590Sstevel@tonic-gate  * A combination of public and private commands are parsed here.
4600Sstevel@tonic-gate  * The internal syntax and the corresponding functionality are:
4610Sstevel@tonic-gate  *	-a update	-- update-archive
4620Sstevel@tonic-gate  *	-a list		-- list-archive
4630Sstevel@tonic-gate  *	-a update-all	-- (reboot to sync all mounted OS archive)
4640Sstevel@tonic-gate  *	-m update_entry	-- update-menu
4650Sstevel@tonic-gate  *	-m list_entry	-- list-menu
4660Sstevel@tonic-gate  *	-m update_temp	-- (reboot -- [boot-args])
4670Sstevel@tonic-gate  *	-m delete_all_entries -- (called from install)
4680Sstevel@tonic-gate  */
4690Sstevel@tonic-gate static void
4700Sstevel@tonic-gate parse_args_internal(int argc, char *argv[])
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate 	int c, error;
4730Sstevel@tonic-gate 	extern char *optarg;
4740Sstevel@tonic-gate 	extern int optind, opterr;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	/* Suppress error message from getopt */
4770Sstevel@tonic-gate 	opterr = 0;
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	error = 0;
480662Sszhou 	while ((c = getopt(argc, argv, "a:d:fm:no:vCR:")) != -1) {
4810Sstevel@tonic-gate 		switch (c) {
4820Sstevel@tonic-gate 		case 'a':
4830Sstevel@tonic-gate 			if (bam_cmd) {
4840Sstevel@tonic-gate 				error = 1;
4850Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
4860Sstevel@tonic-gate 			}
4870Sstevel@tonic-gate 			bam_cmd = BAM_ARCHIVE;
4880Sstevel@tonic-gate 			bam_subcmd = optarg;
4890Sstevel@tonic-gate 			break;
4900Sstevel@tonic-gate 		case 'd':
4910Sstevel@tonic-gate 			if (bam_debug) {
4920Sstevel@tonic-gate 				error = 1;
4930Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4940Sstevel@tonic-gate 			}
4950Sstevel@tonic-gate 			bam_debug = s_strtol(optarg);
4960Sstevel@tonic-gate 			break;
4970Sstevel@tonic-gate 		case 'f':
4980Sstevel@tonic-gate 			if (bam_force) {
4990Sstevel@tonic-gate 				error = 1;
5000Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5010Sstevel@tonic-gate 			}
5020Sstevel@tonic-gate 			bam_force = 1;
5030Sstevel@tonic-gate 			break;
5040Sstevel@tonic-gate 		case 'm':
5050Sstevel@tonic-gate 			if (bam_cmd) {
5060Sstevel@tonic-gate 				error = 1;
5070Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
5080Sstevel@tonic-gate 			}
5090Sstevel@tonic-gate 			bam_cmd = BAM_MENU;
5100Sstevel@tonic-gate 			bam_subcmd = optarg;
5110Sstevel@tonic-gate 			break;
5120Sstevel@tonic-gate 		case 'n':
5130Sstevel@tonic-gate 			if (bam_check) {
5140Sstevel@tonic-gate 				error = 1;
5150Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5160Sstevel@tonic-gate 			}
5170Sstevel@tonic-gate 			bam_check = 1;
5180Sstevel@tonic-gate 			break;
5190Sstevel@tonic-gate 		case 'o':
5200Sstevel@tonic-gate 			if (bam_opt) {
5210Sstevel@tonic-gate 				error = 1;
5220Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5230Sstevel@tonic-gate 			}
5240Sstevel@tonic-gate 			bam_opt = optarg;
5250Sstevel@tonic-gate 			break;
5260Sstevel@tonic-gate 		case 'v':
5270Sstevel@tonic-gate 			if (bam_verbose) {
5280Sstevel@tonic-gate 				error = 1;
5290Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5300Sstevel@tonic-gate 			}
5310Sstevel@tonic-gate 			bam_verbose = 1;
5320Sstevel@tonic-gate 			break;
533662Sszhou 		case 'C':
534662Sszhou 			bam_smf_check = 1;
535662Sszhou 			break;
5360Sstevel@tonic-gate 		case 'R':
5370Sstevel@tonic-gate 			if (bam_root) {
5380Sstevel@tonic-gate 				error = 1;
5390Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5400Sstevel@tonic-gate 				break;
5410Sstevel@tonic-gate 			} else if (realpath(optarg, rootbuf) == NULL) {
5420Sstevel@tonic-gate 				error = 1;
5430Sstevel@tonic-gate 				bam_error(CANT_RESOLVE, optarg,
5440Sstevel@tonic-gate 				    strerror(errno));
5450Sstevel@tonic-gate 				break;
5460Sstevel@tonic-gate 			}
547621Svikram 			bam_alt_root = 1;
5480Sstevel@tonic-gate 			bam_root = rootbuf;
5490Sstevel@tonic-gate 			bam_rootlen = strlen(rootbuf);
5500Sstevel@tonic-gate 			break;
5510Sstevel@tonic-gate 		case '?':
5520Sstevel@tonic-gate 			error = 1;
5530Sstevel@tonic-gate 			bam_error(BAD_OPT, optopt);
5540Sstevel@tonic-gate 			break;
5550Sstevel@tonic-gate 		default :
5560Sstevel@tonic-gate 			error = 1;
5570Sstevel@tonic-gate 			bam_error(BAD_OPT, c);
5580Sstevel@tonic-gate 			break;
5590Sstevel@tonic-gate 		}
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	/*
5630Sstevel@tonic-gate 	 * A command option must be specfied
5640Sstevel@tonic-gate 	 */
5650Sstevel@tonic-gate 	if (!bam_cmd) {
5660Sstevel@tonic-gate 		if (bam_opt && strcmp(bam_opt, "all") == 0) {
5670Sstevel@tonic-gate 			usage();
5680Sstevel@tonic-gate 			bam_exit(0);
5690Sstevel@tonic-gate 		}
5700Sstevel@tonic-gate 		bam_error(NEED_CMD);
5710Sstevel@tonic-gate 		error = 1;
5720Sstevel@tonic-gate 	}
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	if (error) {
5750Sstevel@tonic-gate 		usage();
5760Sstevel@tonic-gate 		bam_exit(1);
5770Sstevel@tonic-gate 	}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	if (optind > argc) {
5800Sstevel@tonic-gate 		bam_error(INT_ERROR, "parse_args");
5810Sstevel@tonic-gate 		bam_exit(1);
5820Sstevel@tonic-gate 	} else if (optind < argc) {
5830Sstevel@tonic-gate 		bam_argv = &argv[optind];
5840Sstevel@tonic-gate 		bam_argc = argc - optind;
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	/*
5880Sstevel@tonic-gate 	 * -n implies verbose mode
5890Sstevel@tonic-gate 	 */
5900Sstevel@tonic-gate 	if (bam_check)
5910Sstevel@tonic-gate 		bam_verbose = 1;
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate static error_t
5950Sstevel@tonic-gate check_subcmd_and_options(
5960Sstevel@tonic-gate 	char *subcmd,
5970Sstevel@tonic-gate 	char *opt,
5980Sstevel@tonic-gate 	subcmd_defn_t *table,
5990Sstevel@tonic-gate 	error_t (**fp)())
6000Sstevel@tonic-gate {
6010Sstevel@tonic-gate 	int i;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	if (subcmd == NULL) {
6040Sstevel@tonic-gate 		bam_error(NEED_SUBCMD);
6050Sstevel@tonic-gate 		return (BAM_ERROR);
6060Sstevel@tonic-gate 	}
6070Sstevel@tonic-gate 
608*2115Svikram 	if (bam_argc != 0 || bam_argv) {
609*2115Svikram 		if (strcmp(subcmd, "set_option") != 0 || bam_argc != 1) {
610*2115Svikram 			bam_error(TRAILING_ARGS);
611*2115Svikram 			usage();
612*2115Svikram 			return (BAM_ERROR);
613*2115Svikram 		}
614*2115Svikram 	}
615*2115Svikram 
6160Sstevel@tonic-gate 	if (bam_root == NULL) {
6170Sstevel@tonic-gate 		bam_root = rootbuf;
6180Sstevel@tonic-gate 		bam_rootlen = 1;
6190Sstevel@tonic-gate 	}
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	/* verify that subcmd is valid */
6220Sstevel@tonic-gate 	for (i = 0; table[i].subcmd != NULL; i++) {
6230Sstevel@tonic-gate 		if (strcmp(table[i].subcmd, subcmd) == 0)
6240Sstevel@tonic-gate 			break;
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	if (table[i].subcmd == NULL) {
6280Sstevel@tonic-gate 		bam_error(INVALID_SUBCMD, subcmd);
6290Sstevel@tonic-gate 		return (BAM_ERROR);
6300Sstevel@tonic-gate 	}
6310Sstevel@tonic-gate 
632*2115Svikram 	if (table[i].unpriv == 0 && geteuid() != 0) {
633*2115Svikram 		bam_error(MUST_BE_ROOT);
634*2115Svikram 		return (BAM_ERROR);
635*2115Svikram 	}
636*2115Svikram 
637*2115Svikram 	/*
638*2115Svikram 	 * Currently only privileged commands need a lock
639*2115Svikram 	 */
640*2115Svikram 	if (table[i].unpriv == 0)
641*2115Svikram 		bam_lock();
642*2115Svikram 
6430Sstevel@tonic-gate 	/* subcmd verifies that opt is appropriate */
6440Sstevel@tonic-gate 	if (table[i].option != OPT_OPTIONAL) {
6450Sstevel@tonic-gate 		if ((table[i].option == OPT_REQ) ^ (opt != NULL)) {
6460Sstevel@tonic-gate 			if (opt)
6470Sstevel@tonic-gate 				bam_error(NO_OPT_REQ, subcmd);
6480Sstevel@tonic-gate 			else
6490Sstevel@tonic-gate 				bam_error(MISS_OPT, subcmd);
6500Sstevel@tonic-gate 			return (BAM_ERROR);
6510Sstevel@tonic-gate 		}
6520Sstevel@tonic-gate 	}
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	*fp = table[i].handler;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	return (BAM_SUCCESS);
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate 
659316Svikram 
660316Svikram static char *
661621Svikram mount_grub_slice(int *mnted, char **physlice, char **logslice, char **fs_type)
662316Svikram {
663316Svikram 	struct extmnttab mnt;
664316Svikram 	struct stat sb;
665316Svikram 	char buf[BAM_MAXLINE], dev[PATH_MAX], phys[PATH_MAX], fstype[32];
666348Svikram 	char cmd[PATH_MAX];
667316Svikram 	char *mntpt;
668316Svikram 	int p, l, f;
669316Svikram 	FILE *fp;
670316Svikram 
671316Svikram 	assert(mnted);
672316Svikram 	*mnted = 0;
673316Svikram 
674316Svikram 	/*
675621Svikram 	 * physlice, logslice, fs_type  args may be NULL
676316Svikram 	 */
677316Svikram 	if (physlice)
678316Svikram 		*physlice = NULL;
679621Svikram 	if (logslice)
680621Svikram 		*logslice = NULL;
681621Svikram 	if (fs_type)
682621Svikram 		*fs_type = NULL;
683316Svikram 
684316Svikram 	if (stat(GRUB_slice, &sb) != 0) {
685316Svikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
686316Svikram 		return (NULL);
687316Svikram 	}
688316Svikram 
689316Svikram 	fp = fopen(GRUB_slice, "r");
690316Svikram 	if (fp == NULL) {
691316Svikram 		bam_error(OPEN_FAIL, GRUB_slice, strerror(errno));
692316Svikram 		return (NULL);
693316Svikram 	}
694316Svikram 
695316Svikram 	dev[0] = fstype[0] = phys[0] = '\0';
696316Svikram 	p = sizeof ("PHYS_SLICE=") - 1;
697316Svikram 	l = sizeof ("LOG_SLICE=") - 1;
698316Svikram 	f = sizeof ("LOG_FSTYP=") - 1;
699316Svikram 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
700316Svikram 		if (strncmp(buf, "PHYS_SLICE=", p) == 0) {
701316Svikram 			(void) strlcpy(phys, buf + p, sizeof (phys));
702316Svikram 			continue;
703316Svikram 		}
704316Svikram 		if (strncmp(buf, "LOG_SLICE=", l) == 0) {
705316Svikram 			(void) strlcpy(dev, buf + l, sizeof (dev));
706316Svikram 			continue;
707316Svikram 		}
708316Svikram 		if (strncmp(buf, "LOG_FSTYP=", f) == 0) {
709316Svikram 			(void) strlcpy(fstype, buf + f, sizeof (fstype));
710316Svikram 			continue;
711316Svikram 		}
712316Svikram 	}
713316Svikram 	(void) fclose(fp);
714316Svikram 
715316Svikram 	if (dev[0] == '\0' || fstype[0] == '\0' || phys[0] == '\0') {
716316Svikram 		bam_error(BAD_SLICE_FILE, GRUB_slice);
717316Svikram 		return (NULL);
718316Svikram 	}
719316Svikram 
720316Svikram 	if (physlice) {
721316Svikram 		*physlice = s_strdup(phys);
722316Svikram 	}
723621Svikram 	if (logslice) {
724621Svikram 		*logslice = s_strdup(dev);
725621Svikram 	}
726621Svikram 	if (fs_type) {
727621Svikram 		*fs_type = s_strdup(fstype);
728621Svikram 	}
729316Svikram 
730316Svikram 	/*
731316Svikram 	 * Check if the slice is already mounted
732316Svikram 	 */
733316Svikram 	fp = fopen(MNTTAB, "r");
734316Svikram 	if (fp == NULL) {
735316Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
736621Svikram 		goto error;
737316Svikram 	}
738316Svikram 
739316Svikram 	resetmnttab(fp);
740316Svikram 
741316Svikram 	mntpt = NULL;
742316Svikram 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
743316Svikram 		if (strcmp(mnt.mnt_special, dev) == 0) {
744316Svikram 			mntpt = s_strdup(mnt.mnt_mountp);
745316Svikram 			break;
746316Svikram 		}
747316Svikram 	}
748316Svikram 
749316Svikram 	(void) fclose(fp);
750316Svikram 
751316Svikram 	if (mntpt) {
752316Svikram 		return (mntpt);
753316Svikram 	}
754316Svikram 
755316Svikram 
756316Svikram 	/*
757316Svikram 	 * GRUB slice is not mounted, we need to mount it now.
758316Svikram 	 * First create the mountpoint
759316Svikram 	 */
760316Svikram 	mntpt = s_calloc(1, PATH_MAX);
761316Svikram 	(void) snprintf(mntpt, PATH_MAX, "%s.%d", GRUB_slice_mntpt, getpid());
762316Svikram 	if (mkdir(mntpt, 0755) == -1 && errno != EEXIST) {
763316Svikram 		bam_error(MKDIR_FAILED, mntpt, strerror(errno));
764316Svikram 		free(mntpt);
765621Svikram 		goto error;
766316Svikram 	}
767316Svikram 
768348Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/mount -F %s %s %s",
769348Svikram 	    fstype, dev, mntpt);
770348Svikram 
771348Svikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
772621Svikram 		bam_error(MOUNT_FAILED, dev, fstype);
773316Svikram 		if (rmdir(mntpt) != 0) {
774316Svikram 			bam_error(RMDIR_FAILED, mntpt, strerror(errno));
775316Svikram 		}
776316Svikram 		free(mntpt);
777621Svikram 		goto error;
778316Svikram 	}
779316Svikram 
780316Svikram 	*mnted = 1;
781316Svikram 	return (mntpt);
782621Svikram 
783621Svikram error:
784621Svikram 	if (physlice) {
785621Svikram 		free(*physlice);
786621Svikram 		*physlice = NULL;
787621Svikram 	}
788621Svikram 	if (logslice) {
789621Svikram 		free(*logslice);
790621Svikram 		*logslice = NULL;
791621Svikram 	}
792621Svikram 	if (fs_type) {
793621Svikram 		free(*fs_type);
794621Svikram 		*fs_type = NULL;
795621Svikram 	}
796621Svikram 	return (NULL);
797316Svikram }
798316Svikram 
799316Svikram static void
800621Svikram umount_grub_slice(
801621Svikram 	int mnted,
802621Svikram 	char *mntpt,
803621Svikram 	char *physlice,
804621Svikram 	char *logslice,
805621Svikram 	char *fs_type)
806316Svikram {
807348Svikram 	char cmd[PATH_MAX];
808348Svikram 
809316Svikram 	/*
810316Svikram 	 * If we have not dealt with GRUB slice
811316Svikram 	 * we have nothing to do - just return.
812316Svikram 	 */
813316Svikram 	if (mntpt == NULL)
814316Svikram 		return;
815316Svikram 
816316Svikram 
817316Svikram 	/*
818316Svikram 	 * If we mounted the filesystem earlier in mount_grub_slice()
819316Svikram 	 * unmount it now.
820316Svikram 	 */
821316Svikram 	if (mnted) {
822348Svikram 		(void) snprintf(cmd, sizeof (cmd), "/sbin/umount %s",
823348Svikram 		    mntpt);
824348Svikram 		if (exec_cmd(cmd, NULL, 0) != 0) {
825348Svikram 			bam_error(UMOUNT_FAILED, mntpt);
826316Svikram 		}
827316Svikram 		if (rmdir(mntpt) != 0) {
828316Svikram 			bam_error(RMDIR_FAILED, mntpt, strerror(errno));
829316Svikram 		}
830316Svikram 	}
831621Svikram 
832316Svikram 	if (physlice)
833316Svikram 		free(physlice);
834621Svikram 	if (logslice)
835621Svikram 		free(logslice);
836621Svikram 	if (fs_type)
837621Svikram 		free(fs_type);
838621Svikram 
839316Svikram 	free(mntpt);
840316Svikram }
841316Svikram 
842621Svikram static char *
843621Svikram use_stubboot(void)
844621Svikram {
845621Svikram 	int mnted;
846621Svikram 	struct stat sb;
847621Svikram 	struct extmnttab mnt;
848621Svikram 	FILE *fp;
849621Svikram 	char cmd[PATH_MAX];
850621Svikram 
851621Svikram 	if (stat(STUBBOOT, &sb) != 0) {
852621Svikram 		bam_error(STUBBOOT_DIR_NOT_FOUND);
853621Svikram 		return (NULL);
854621Svikram 	}
855621Svikram 
856621Svikram 	/*
857621Svikram 	 * Check if stubboot is mounted. If not, mount it
858621Svikram 	 */
859621Svikram 	fp = fopen(MNTTAB, "r");
860621Svikram 	if (fp == NULL) {
861621Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
862621Svikram 		return (NULL);
863621Svikram 	}
864621Svikram 
865621Svikram 	resetmnttab(fp);
866621Svikram 
867621Svikram 	mnted = 0;
868621Svikram 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
869621Svikram 		if (strcmp(mnt.mnt_mountp, STUBBOOT) == 0) {
870621Svikram 			mnted = 1;
871621Svikram 			break;
872621Svikram 		}
873621Svikram 	}
874621Svikram 
875621Svikram 	(void) fclose(fp);
876621Svikram 
877621Svikram 	if (mnted)
878621Svikram 		return (STUBBOOT);
879621Svikram 
880621Svikram 	/*
881621Svikram 	 * Stubboot is not mounted, mount it now.
882621Svikram 	 * It should exist in /etc/vfstab
883621Svikram 	 */
884621Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/mount %s",
885621Svikram 	    STUBBOOT);
886621Svikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
887621Svikram 		bam_error(MOUNT_MNTPT_FAILED, STUBBOOT);
888621Svikram 		return (NULL);
889621Svikram 	}
890621Svikram 
891621Svikram 	return (STUBBOOT);
892621Svikram }
893621Svikram 
894621Svikram static void
895621Svikram disp_active_menu_locn(char *menu_path, char *logslice, char *fstype, int mnted)
896621Svikram {
897621Svikram 	/*
898621Svikram 	 * Check if we did a temp mount of an unmounted device.
899621Svikram 	 * If yes, print the block device and fstype for that device
900621Svikram 	 * else it is already mounted, so we print the path to the GRUB menu.
901621Svikram 	 */
902621Svikram 	if (mnted) {
903621Svikram 		bam_print(GRUB_MENU_DEVICE, logslice);
904621Svikram 		bam_print(GRUB_MENU_FSTYPE, fstype);
905621Svikram 	} else {
906621Svikram 		bam_print(GRUB_MENU_PATH, menu_path);
907621Svikram 	}
908621Svikram }
909621Svikram 
910621Svikram /*
911621Svikram  * NOTE: A single "/" is also considered a trailing slash and will
912621Svikram  * be deleted.
913621Svikram  */
914621Svikram static void
915621Svikram elide_trailing_slash(const char *src, char *dst, size_t dstsize)
916621Svikram {
917621Svikram 	size_t dstlen;
918621Svikram 
919621Svikram 	assert(src);
920621Svikram 	assert(dst);
921621Svikram 
922621Svikram 	(void) strlcpy(dst, src, dstsize);
923621Svikram 
924621Svikram 	dstlen = strlen(dst);
925621Svikram 	if (dst[dstlen - 1] == '/') {
926621Svikram 		dst[dstlen - 1] = '\0';
927621Svikram 	}
928621Svikram }
929621Svikram 
9300Sstevel@tonic-gate static error_t
9310Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[])
9320Sstevel@tonic-gate {
9330Sstevel@tonic-gate 	error_t ret;
9340Sstevel@tonic-gate 	char menu_path[PATH_MAX];
9350Sstevel@tonic-gate 	menu_t *menu;
936621Svikram 	char *mntpt, *menu_root, *logslice, *fstype;
937316Svikram 	struct stat sb;
938316Svikram 	int mnted;	/* set if we did a mount */
9390Sstevel@tonic-gate 	error_t (*f)(menu_t *mp, char *menu_path, char *opt);
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	/*
9420Sstevel@tonic-gate 	 * Check arguments
9430Sstevel@tonic-gate 	 */
9440Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
9450Sstevel@tonic-gate 	if (ret == BAM_ERROR) {
9460Sstevel@tonic-gate 		return (BAM_ERROR);
9470Sstevel@tonic-gate 	}
9480Sstevel@tonic-gate 
949316Svikram 	mntpt = NULL;
950316Svikram 	mnted = 0;
951621Svikram 	logslice = fstype = NULL;
952621Svikram 
953621Svikram 	/*
954621Svikram 	 * If the user provides an alternate root, we
955621Svikram 	 * assume they know what they are doing and we
956621Svikram 	 * use it. Else we check if there is an
957621Svikram 	 * alternate location (other than /boot/grub)
958621Svikram 	 * for the GRUB menu
959621Svikram 	 */
960621Svikram 	if (bam_alt_root) {
961621Svikram 		menu_root = bam_root;
962621Svikram 	} else if (stat(GRUB_slice, &sb) == 0) {
963621Svikram 		mntpt = mount_grub_slice(&mnted, NULL, &logslice, &fstype);
964316Svikram 		menu_root = mntpt;
965621Svikram 	} else if (stat(STUBBOOT, &sb) == 0) {
966621Svikram 		menu_root = use_stubboot();
967316Svikram 	} else {
968316Svikram 		menu_root = bam_root;
969316Svikram 	}
970316Svikram 
971621Svikram 	if (menu_root == NULL) {
972621Svikram 		bam_error(CANNOT_LOCATE_GRUB_MENU);
973621Svikram 		return (BAM_ERROR);
974621Svikram 	}
975621Svikram 
976621Svikram 	elide_trailing_slash(menu_root, menu_path, sizeof (menu_path));
977621Svikram 	(void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path));
978621Svikram 
979621Svikram 	/*
980621Svikram 	 * If listing the menu, display the active menu
981621Svikram 	 * location
982621Svikram 	 */
983621Svikram 	if (strcmp(subcmd, "list_entry") == 0) {
984621Svikram 		disp_active_menu_locn(menu_path, logslice, fstype, mnted);
985621Svikram 	}
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	menu = menu_read(menu_path);
9880Sstevel@tonic-gate 	assert(menu);
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	/*
9910Sstevel@tonic-gate 	 * Special handling for setting timeout and default
9920Sstevel@tonic-gate 	 */
9930Sstevel@tonic-gate 	if (strcmp(subcmd, "set_option") == 0) {
9940Sstevel@tonic-gate 		if (largc != 1 || largv[0] == NULL) {
9950Sstevel@tonic-gate 			usage();
996621Svikram 			menu_free(menu);
997621Svikram 			umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
9980Sstevel@tonic-gate 			return (BAM_ERROR);
9990Sstevel@tonic-gate 		}
10000Sstevel@tonic-gate 		opt = largv[0];
10010Sstevel@tonic-gate 	} else if (largc != 0) {
10020Sstevel@tonic-gate 		usage();
1003621Svikram 		menu_free(menu);
1004621Svikram 		umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
10050Sstevel@tonic-gate 		return (BAM_ERROR);
10060Sstevel@tonic-gate 	}
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	/*
10090Sstevel@tonic-gate 	 * Once the sub-cmd handler has run
10100Sstevel@tonic-gate 	 * only the line field is guaranteed to have valid values
10110Sstevel@tonic-gate 	 */
10120Sstevel@tonic-gate 	if (strcmp(subcmd, "update_entry") == 0)
10130Sstevel@tonic-gate 		ret = f(menu, bam_root, opt);
10140Sstevel@tonic-gate 	else
10150Sstevel@tonic-gate 		ret = f(menu, menu_path, opt);
10160Sstevel@tonic-gate 	if (ret == BAM_WRITE) {
1017316Svikram 		ret = menu_write(menu_root, menu);
10180Sstevel@tonic-gate 	}
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	menu_free(menu);
10210Sstevel@tonic-gate 
1022621Svikram 	umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
1023316Svikram 
10240Sstevel@tonic-gate 	return (ret);
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate static error_t
10290Sstevel@tonic-gate bam_archive(
10300Sstevel@tonic-gate 	char *subcmd,
10310Sstevel@tonic-gate 	char *opt)
10320Sstevel@tonic-gate {
10330Sstevel@tonic-gate 	error_t ret;
10340Sstevel@tonic-gate 	error_t (*f)(char *root, char *opt);
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	/*
1037662Sszhou 	 * Add trailing / for archive subcommands
1038662Sszhou 	 */
1039662Sszhou 	if (rootbuf[strlen(rootbuf) - 1] != '/')
1040662Sszhou 		(void) strcat(rootbuf, "/");
1041662Sszhou 	bam_rootlen = strlen(rootbuf);
1042662Sszhou 
1043662Sszhou 	/*
10440Sstevel@tonic-gate 	 * Check arguments
10450Sstevel@tonic-gate 	 */
10460Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f);
10470Sstevel@tonic-gate 	if (ret != BAM_SUCCESS) {
10480Sstevel@tonic-gate 		return (BAM_ERROR);
10490Sstevel@tonic-gate 	}
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate #if defined(__sparc)
10520Sstevel@tonic-gate 	/*
10530Sstevel@tonic-gate 	 * A NOP if called on SPARC during reboot
10540Sstevel@tonic-gate 	 */
10550Sstevel@tonic-gate 	if (strcmp(subcmd, "update_all") == 0)
10560Sstevel@tonic-gate 		return (BAM_SUCCESS);
10570Sstevel@tonic-gate 	else if (strcmp(subcmd, "update") != 0)
10580Sstevel@tonic-gate 		sparc_abort();
10590Sstevel@tonic-gate #endif
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	/*
10620Sstevel@tonic-gate 	 * Check archive not supported with update_all
10630Sstevel@tonic-gate 	 * since it is awkward to display out-of-sync
10640Sstevel@tonic-gate 	 * information for each BE.
10650Sstevel@tonic-gate 	 */
10660Sstevel@tonic-gate 	if (bam_check && strcmp(subcmd, "update_all") == 0) {
10670Sstevel@tonic-gate 		bam_error(CHECK_NOT_SUPPORTED, subcmd);
10680Sstevel@tonic-gate 		return (BAM_ERROR);
10690Sstevel@tonic-gate 	}
10700Sstevel@tonic-gate 
1071316Svikram 	if (strcmp(subcmd, "update_all") == 0)
1072316Svikram 		bam_update_all = 1;
1073316Svikram 
1074316Svikram 	ret = f(bam_root, opt);
1075316Svikram 
1076316Svikram 	bam_update_all = 0;
1077316Svikram 
1078316Svikram 	return (ret);
10790Sstevel@tonic-gate }
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate /*PRINTFLIKE1*/
10820Sstevel@tonic-gate static void
10830Sstevel@tonic-gate bam_error(char *format, ...)
10840Sstevel@tonic-gate {
10850Sstevel@tonic-gate 	va_list ap;
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	va_start(ap, format);
10880Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", prog);
10890Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
10900Sstevel@tonic-gate 	va_end(ap);
10910Sstevel@tonic-gate }
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate /*PRINTFLIKE1*/
10940Sstevel@tonic-gate static void
10950Sstevel@tonic-gate bam_print(char *format, ...)
10960Sstevel@tonic-gate {
10970Sstevel@tonic-gate 	va_list ap;
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	va_start(ap, format);
11000Sstevel@tonic-gate 	(void) vfprintf(stdout, format, ap);
11010Sstevel@tonic-gate 	va_end(ap);
11020Sstevel@tonic-gate }
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate static void
11050Sstevel@tonic-gate bam_exit(int excode)
11060Sstevel@tonic-gate {
11070Sstevel@tonic-gate 	bam_unlock();
11080Sstevel@tonic-gate 	exit(excode);
11090Sstevel@tonic-gate }
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate static void
11120Sstevel@tonic-gate bam_lock(void)
11130Sstevel@tonic-gate {
11140Sstevel@tonic-gate 	struct flock lock;
11150Sstevel@tonic-gate 	pid_t pid;
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS);
11180Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
11190Sstevel@tonic-gate 		/*
11200Sstevel@tonic-gate 		 * We may be invoked early in boot for archive verification.
11210Sstevel@tonic-gate 		 * In this case, root is readonly and /var/run may not exist.
11220Sstevel@tonic-gate 		 * Proceed without the lock
11230Sstevel@tonic-gate 		 */
11240Sstevel@tonic-gate 		if (errno == EROFS || errno == ENOENT) {
11250Sstevel@tonic-gate 			bam_root_readonly = 1;
11260Sstevel@tonic-gate 			return;
11270Sstevel@tonic-gate 		}
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 		bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno));
11300Sstevel@tonic-gate 		bam_exit(1);
11310Sstevel@tonic-gate 	}
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
11340Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
11350Sstevel@tonic-gate 	lock.l_start = 0;
11360Sstevel@tonic-gate 	lock.l_len = 0;
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) {
11390Sstevel@tonic-gate 		if (errno != EACCES && errno != EAGAIN) {
11400Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11410Sstevel@tonic-gate 			(void) close(bam_lock_fd);
11420Sstevel@tonic-gate 			bam_lock_fd = -1;
11430Sstevel@tonic-gate 			bam_exit(1);
11440Sstevel@tonic-gate 		}
11450Sstevel@tonic-gate 		pid = 0;
11460Sstevel@tonic-gate 		(void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0);
11470Sstevel@tonic-gate 		bam_print(FILE_LOCKED, pid);
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 		lock.l_type = F_WRLCK;
11500Sstevel@tonic-gate 		lock.l_whence = SEEK_SET;
11510Sstevel@tonic-gate 		lock.l_start = 0;
11520Sstevel@tonic-gate 		lock.l_len = 0;
11530Sstevel@tonic-gate 		if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) {
11540Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11550Sstevel@tonic-gate 			(void) close(bam_lock_fd);
11560Sstevel@tonic-gate 			bam_lock_fd = -1;
11570Sstevel@tonic-gate 			bam_exit(1);
11580Sstevel@tonic-gate 		}
11590Sstevel@tonic-gate 	}
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	/* We own the lock now */
11620Sstevel@tonic-gate 	pid = getpid();
11630Sstevel@tonic-gate 	(void) write(bam_lock_fd, &pid, sizeof (pid));
11640Sstevel@tonic-gate }
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate static void
11670Sstevel@tonic-gate bam_unlock(void)
11680Sstevel@tonic-gate {
11690Sstevel@tonic-gate 	struct flock unlock;
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	/*
11720Sstevel@tonic-gate 	 * NOP if we don't hold the lock
11730Sstevel@tonic-gate 	 */
11740Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
11750Sstevel@tonic-gate 		return;
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
11790Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
11800Sstevel@tonic-gate 	unlock.l_start = 0;
11810Sstevel@tonic-gate 	unlock.l_len = 0;
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) {
11840Sstevel@tonic-gate 		bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11850Sstevel@tonic-gate 	}
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	if (close(bam_lock_fd) == -1) {
11880Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno));
11890Sstevel@tonic-gate 	}
11900Sstevel@tonic-gate 	bam_lock_fd = -1;
11910Sstevel@tonic-gate }
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate static error_t
11940Sstevel@tonic-gate list_archive(char *root, char *opt)
11950Sstevel@tonic-gate {
11960Sstevel@tonic-gate 	filelist_t flist;
11970Sstevel@tonic-gate 	filelist_t *flistp = &flist;
11980Sstevel@tonic-gate 	line_t *lp;
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	assert(root);
12010Sstevel@tonic-gate 	assert(opt == NULL);
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
12040Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
12050Sstevel@tonic-gate 		return (BAM_ERROR);
12060Sstevel@tonic-gate 	}
12070Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
12100Sstevel@tonic-gate 		bam_print(PRINT, lp->line);
12110Sstevel@tonic-gate 	}
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	filelist_free(flistp);
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	return (BAM_SUCCESS);
12160Sstevel@tonic-gate }
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate /*
12190Sstevel@tonic-gate  * This routine writes a list of lines to a file.
12200Sstevel@tonic-gate  * The list is *not* freed
12210Sstevel@tonic-gate  */
12220Sstevel@tonic-gate static error_t
12230Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start)
12240Sstevel@tonic-gate {
12250Sstevel@tonic-gate 	char tmpfile[PATH_MAX];
12260Sstevel@tonic-gate 	char path[PATH_MAX];
12270Sstevel@tonic-gate 	FILE *fp;
12280Sstevel@tonic-gate 	int ret;
12290Sstevel@tonic-gate 	struct stat sb;
12300Sstevel@tonic-gate 	mode_t mode;
12310Sstevel@tonic-gate 	uid_t root_uid;
12320Sstevel@tonic-gate 	gid_t sys_gid;
12330Sstevel@tonic-gate 	struct passwd *pw;
12340Sstevel@tonic-gate 	struct group *gp;
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, final);
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 	if (start == NULL) {
12400Sstevel@tonic-gate 		if (stat(path, &sb) != -1) {
12410Sstevel@tonic-gate 			bam_print(UNLINK_EMPTY, path);
12420Sstevel@tonic-gate 			if (unlink(path) != 0) {
12430Sstevel@tonic-gate 				bam_error(UNLINK_FAIL, path, strerror(errno));
12440Sstevel@tonic-gate 				return (BAM_ERROR);
12450Sstevel@tonic-gate 			} else {
12460Sstevel@tonic-gate 				return (BAM_SUCCESS);
12470Sstevel@tonic-gate 			}
12480Sstevel@tonic-gate 		}
12490Sstevel@tonic-gate 	}
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	/*
12520Sstevel@tonic-gate 	 * Preserve attributes of existing file if possible,
12530Sstevel@tonic-gate 	 * otherwise ask the system for uid/gid of root/sys.
12540Sstevel@tonic-gate 	 * If all fails, fall back on hard-coded defaults.
12550Sstevel@tonic-gate 	 */
12560Sstevel@tonic-gate 	if (stat(path, &sb) != -1) {
12570Sstevel@tonic-gate 		mode = sb.st_mode;
12580Sstevel@tonic-gate 		root_uid = sb.st_uid;
12590Sstevel@tonic-gate 		sys_gid = sb.st_gid;
12600Sstevel@tonic-gate 	} else {
12610Sstevel@tonic-gate 		mode = DEFAULT_DEV_MODE;
12620Sstevel@tonic-gate 		if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
12630Sstevel@tonic-gate 			root_uid = pw->pw_uid;
12640Sstevel@tonic-gate 		} else {
12650Sstevel@tonic-gate 			if (bam_verbose)
12660Sstevel@tonic-gate 				bam_error(CANT_FIND_USER,
12670Sstevel@tonic-gate 				    DEFAULT_DEV_USER, DEFAULT_DEV_UID);
12680Sstevel@tonic-gate 			root_uid = (uid_t)DEFAULT_DEV_UID;
12690Sstevel@tonic-gate 		}
12700Sstevel@tonic-gate 		if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
12710Sstevel@tonic-gate 			sys_gid = gp->gr_gid;
12720Sstevel@tonic-gate 		} else {
12730Sstevel@tonic-gate 			if (bam_verbose)
12740Sstevel@tonic-gate 				bam_error(CANT_FIND_GROUP,
12750Sstevel@tonic-gate 				    DEFAULT_DEV_GROUP, DEFAULT_DEV_GID);
12760Sstevel@tonic-gate 			sys_gid = (gid_t)DEFAULT_DEV_GID;
12770Sstevel@tonic-gate 		}
12780Sstevel@tonic-gate 	}
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	(void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp);
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	/* Truncate tmpfile first */
12830Sstevel@tonic-gate 	fp = fopen(tmpfile, "w");
12840Sstevel@tonic-gate 	if (fp == NULL) {
12850Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
12860Sstevel@tonic-gate 		return (BAM_ERROR);
12870Sstevel@tonic-gate 	}
12880Sstevel@tonic-gate 	ret = fclose(fp);
12890Sstevel@tonic-gate 	if (ret == EOF) {
12900Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
12910Sstevel@tonic-gate 		return (BAM_ERROR);
12920Sstevel@tonic-gate 	}
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 	/* Now open it in append mode */
12950Sstevel@tonic-gate 	fp = fopen(tmpfile, "a");
12960Sstevel@tonic-gate 	if (fp == NULL) {
12970Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
12980Sstevel@tonic-gate 		return (BAM_ERROR);
12990Sstevel@tonic-gate 	}
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 	for (; start; start = start->next) {
13020Sstevel@tonic-gate 		ret = s_fputs(start->line, fp);
13030Sstevel@tonic-gate 		if (ret == EOF) {
13040Sstevel@tonic-gate 			bam_error(WRITE_FAIL, tmpfile, strerror(errno));
13050Sstevel@tonic-gate 			(void) fclose(fp);
13060Sstevel@tonic-gate 			return (BAM_ERROR);
13070Sstevel@tonic-gate 		}
13080Sstevel@tonic-gate 	}
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	ret = fclose(fp);
13110Sstevel@tonic-gate 	if (ret == EOF) {
13120Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
13130Sstevel@tonic-gate 		return (BAM_ERROR);
13140Sstevel@tonic-gate 	}
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	/*
1317271Sjg 	 * Set up desired attributes.  Ignore failures on filesystems
1318271Sjg 	 * not supporting these operations - pcfs reports unsupported
1319271Sjg 	 * operations as EINVAL.
13200Sstevel@tonic-gate 	 */
13210Sstevel@tonic-gate 	ret = chmod(tmpfile, mode);
1322271Sjg 	if (ret == -1 &&
1323271Sjg 	    errno != EINVAL && errno != ENOTSUP) {
13240Sstevel@tonic-gate 		bam_error(CHMOD_FAIL, tmpfile, strerror(errno));
13250Sstevel@tonic-gate 		return (BAM_ERROR);
13260Sstevel@tonic-gate 	}
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 	ret = chown(tmpfile, root_uid, sys_gid);
1329271Sjg 	if (ret == -1 &&
1330271Sjg 	    errno != EINVAL && errno != ENOTSUP) {
13310Sstevel@tonic-gate 		bam_error(CHOWN_FAIL, tmpfile, strerror(errno));
13320Sstevel@tonic-gate 		return (BAM_ERROR);
13330Sstevel@tonic-gate 	}
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	/*
13370Sstevel@tonic-gate 	 * Do an atomic rename
13380Sstevel@tonic-gate 	 */
13390Sstevel@tonic-gate 	ret = rename(tmpfile, path);
13400Sstevel@tonic-gate 	if (ret != 0) {
13410Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path, strerror(errno));
13420Sstevel@tonic-gate 		return (BAM_ERROR);
13430Sstevel@tonic-gate 	}
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	return (BAM_SUCCESS);
13460Sstevel@tonic-gate }
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate /*
13490Sstevel@tonic-gate  * This function should always return 0 - since we want
13500Sstevel@tonic-gate  * to create stat data for *all* files in the list.
13510Sstevel@tonic-gate  */
13520Sstevel@tonic-gate /*ARGSUSED*/
13530Sstevel@tonic-gate static int
13540Sstevel@tonic-gate cmpstat(
13550Sstevel@tonic-gate 	const char *file,
13560Sstevel@tonic-gate 	const struct stat *stat,
13570Sstevel@tonic-gate 	int flags,
13580Sstevel@tonic-gate 	struct FTW *ftw)
13590Sstevel@tonic-gate {
13600Sstevel@tonic-gate 	uint_t sz;
13610Sstevel@tonic-gate 	uint64_t *value;
13620Sstevel@tonic-gate 	uint64_t filestat[2];
13630Sstevel@tonic-gate 	int error;
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 	/*
13660Sstevel@tonic-gate 	 * We only want regular files
13670Sstevel@tonic-gate 	 */
13680Sstevel@tonic-gate 	if (!S_ISREG(stat->st_mode))
13690Sstevel@tonic-gate 		return (0);
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 	/*
13720Sstevel@tonic-gate 	 * new_nvlp may be NULL if there were errors earlier
13730Sstevel@tonic-gate 	 * but this is not fatal to update determination.
13740Sstevel@tonic-gate 	 */
13750Sstevel@tonic-gate 	if (walk_arg.new_nvlp) {
13760Sstevel@tonic-gate 		filestat[0] = stat->st_size;
13770Sstevel@tonic-gate 		filestat[1] = stat->st_mtime;
13780Sstevel@tonic-gate 		error = nvlist_add_uint64_array(walk_arg.new_nvlp,
13790Sstevel@tonic-gate 		    file + bam_rootlen, filestat, 2);
13800Sstevel@tonic-gate 		if (error)
13810Sstevel@tonic-gate 			bam_error(NVADD_FAIL, file, strerror(error));
13820Sstevel@tonic-gate 	}
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	/*
13850Sstevel@tonic-gate 	 * The remaining steps are only required if we haven't made a
13860Sstevel@tonic-gate 	 * decision about update or if we are checking (-n)
13870Sstevel@tonic-gate 	 */
13880Sstevel@tonic-gate 	if (walk_arg.need_update && !bam_check)
13890Sstevel@tonic-gate 		return (0);
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	/*
13920Sstevel@tonic-gate 	 * If we are invoked as part of system/filesyste/boot-archive
13930Sstevel@tonic-gate 	 * SMF service, ignore amd64 modules unless we are booted amd64.
13940Sstevel@tonic-gate 	 */
1395662Sszhou 	if (bam_smf_check && !is_amd64() && strstr(file, "/amd64/") != 0)
13960Sstevel@tonic-gate 		return (0);
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	/*
13990Sstevel@tonic-gate 	 * We need an update if file doesn't exist in old archive
14000Sstevel@tonic-gate 	 */
14010Sstevel@tonic-gate 	if (walk_arg.old_nvlp == NULL ||
14020Sstevel@tonic-gate 	    nvlist_lookup_uint64_array(walk_arg.old_nvlp,
14030Sstevel@tonic-gate 	    file + bam_rootlen, &value, &sz) != 0) {
14040Sstevel@tonic-gate 		if (bam_smf_check)	/* ignore new during smf check */
14050Sstevel@tonic-gate 			return (0);
14060Sstevel@tonic-gate 		walk_arg.need_update = 1;
14070Sstevel@tonic-gate 		if (bam_verbose)
14080Sstevel@tonic-gate 			bam_print(PARSEABLE_NEW_FILE, file);
14090Sstevel@tonic-gate 		return (0);
14100Sstevel@tonic-gate 	}
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 	/*
14130Sstevel@tonic-gate 	 * File exists in old archive. Check if file has changed
14140Sstevel@tonic-gate 	 */
14150Sstevel@tonic-gate 	assert(sz == 2);
14160Sstevel@tonic-gate 	bcopy(value, filestat, sizeof (filestat));
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 	if (filestat[0] != stat->st_size ||
14190Sstevel@tonic-gate 	    filestat[1] != stat->st_mtime) {
14200Sstevel@tonic-gate 		walk_arg.need_update = 1;
14210Sstevel@tonic-gate 		if (bam_verbose)
14220Sstevel@tonic-gate 			if (bam_smf_check)
14230Sstevel@tonic-gate 				bam_print("    %s\n", file);
14240Sstevel@tonic-gate 			else
14250Sstevel@tonic-gate 				bam_print(PARSEABLE_OUT_DATE, file);
14260Sstevel@tonic-gate 	}
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 	return (0);
14290Sstevel@tonic-gate }
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate /*
14320Sstevel@tonic-gate  * Check flags and presence of required files.
14330Sstevel@tonic-gate  * The force flag and/or absence of files should
14340Sstevel@tonic-gate  * trigger an update.
14350Sstevel@tonic-gate  * Suppress stdout output if check (-n) option is set
14360Sstevel@tonic-gate  * (as -n should only produce parseable output.)
14370Sstevel@tonic-gate  */
14380Sstevel@tonic-gate static void
14390Sstevel@tonic-gate check_flags_and_files(char *root)
14400Sstevel@tonic-gate {
14410Sstevel@tonic-gate 	char path[PATH_MAX];
14420Sstevel@tonic-gate 	struct stat sb;
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 	/*
14450Sstevel@tonic-gate 	 * if force, create archive unconditionally
14460Sstevel@tonic-gate 	 */
14470Sstevel@tonic-gate 	if (bam_force) {
14480Sstevel@tonic-gate 		walk_arg.need_update = 1;
14490Sstevel@tonic-gate 		if (bam_verbose && !bam_check)
14500Sstevel@tonic-gate 			bam_print(UPDATE_FORCE);
14510Sstevel@tonic-gate 		return;
14520Sstevel@tonic-gate 	}
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	/*
14550Sstevel@tonic-gate 	 * If archive is missing, create archive
14560Sstevel@tonic-gate 	 */
14570Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE);
14580Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
14590Sstevel@tonic-gate 		if (bam_verbose && !bam_check)
14600Sstevel@tonic-gate 			bam_print(UPDATE_ARCH_MISS, path);
14610Sstevel@tonic-gate 		walk_arg.need_update = 1;
14620Sstevel@tonic-gate 		return;
14630Sstevel@tonic-gate 	}
14640Sstevel@tonic-gate }
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate static error_t
14670Sstevel@tonic-gate read_one_list(char *root, filelist_t  *flistp, char *filelist)
14680Sstevel@tonic-gate {
14690Sstevel@tonic-gate 	char path[PATH_MAX];
14700Sstevel@tonic-gate 	FILE *fp;
14710Sstevel@tonic-gate 	char buf[BAM_MAXLINE];
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, filelist);
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 	fp = fopen(path, "r");
14760Sstevel@tonic-gate 	if (fp == NULL) {
14770Sstevel@tonic-gate 		if (bam_debug)
14780Sstevel@tonic-gate 			bam_error(FLIST_FAIL, path, strerror(errno));
14790Sstevel@tonic-gate 		return (BAM_ERROR);
14800Sstevel@tonic-gate 	}
14810Sstevel@tonic-gate 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
1482316Svikram 		/* skip blank lines */
1483316Svikram 		if (strspn(buf, " \t") == strlen(buf))
1484316Svikram 			continue;
14850Sstevel@tonic-gate 		append_to_flist(flistp, buf);
14860Sstevel@tonic-gate 	}
14870Sstevel@tonic-gate 	if (fclose(fp) != 0) {
14880Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, path, strerror(errno));
14890Sstevel@tonic-gate 		return (BAM_ERROR);
14900Sstevel@tonic-gate 	}
14910Sstevel@tonic-gate 	return (BAM_SUCCESS);
14920Sstevel@tonic-gate }
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate static error_t
14950Sstevel@tonic-gate read_list(char *root, filelist_t  *flistp)
14960Sstevel@tonic-gate {
14970Sstevel@tonic-gate 	int rval;
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	/*
15020Sstevel@tonic-gate 	 * Read current lists of files - only the first is mandatory
15030Sstevel@tonic-gate 	 */
15040Sstevel@tonic-gate 	rval = read_one_list(root, flistp, BOOT_FILE_LIST);
15050Sstevel@tonic-gate 	if (rval != BAM_SUCCESS)
15060Sstevel@tonic-gate 		return (rval);
15070Sstevel@tonic-gate 	(void) read_one_list(root, flistp, ETC_FILE_LIST);
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate 	if (flistp->head == NULL) {
15100Sstevel@tonic-gate 		bam_error(NO_FLIST);
15110Sstevel@tonic-gate 		return (BAM_ERROR);
15120Sstevel@tonic-gate 	}
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	return (BAM_SUCCESS);
15150Sstevel@tonic-gate }
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate static void
15180Sstevel@tonic-gate getoldstat(char *root)
15190Sstevel@tonic-gate {
15200Sstevel@tonic-gate 	char path[PATH_MAX];
15210Sstevel@tonic-gate 	int fd, error;
15220Sstevel@tonic-gate 	struct stat sb;
15230Sstevel@tonic-gate 	char *ostat;
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
15260Sstevel@tonic-gate 	fd = open(path, O_RDONLY);
15270Sstevel@tonic-gate 	if (fd == -1) {
15280Sstevel@tonic-gate 		if (bam_verbose)
15290Sstevel@tonic-gate 			bam_print(OPEN_FAIL, path, strerror(errno));
15300Sstevel@tonic-gate 		walk_arg.need_update = 1;
15310Sstevel@tonic-gate 		return;
15320Sstevel@tonic-gate 	}
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 	if (fstat(fd, &sb) != 0) {
15350Sstevel@tonic-gate 		bam_error(STAT_FAIL, path, strerror(errno));
15360Sstevel@tonic-gate 		(void) close(fd);
15370Sstevel@tonic-gate 		walk_arg.need_update = 1;
15380Sstevel@tonic-gate 		return;
15390Sstevel@tonic-gate 	}
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 	ostat = s_calloc(1, sb.st_size);
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	if (read(fd, ostat, sb.st_size) != sb.st_size) {
15440Sstevel@tonic-gate 		bam_error(READ_FAIL, path, strerror(errno));
15450Sstevel@tonic-gate 		(void) close(fd);
15460Sstevel@tonic-gate 		free(ostat);
15470Sstevel@tonic-gate 		walk_arg.need_update = 1;
15480Sstevel@tonic-gate 		return;
15490Sstevel@tonic-gate 	}
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 	(void) close(fd);
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
15540Sstevel@tonic-gate 	error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0);
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 	free(ostat);
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	if (error) {
15590Sstevel@tonic-gate 		bam_error(UNPACK_FAIL, path, strerror(error));
15600Sstevel@tonic-gate 		walk_arg.old_nvlp = NULL;
15610Sstevel@tonic-gate 		walk_arg.need_update = 1;
15620Sstevel@tonic-gate 		return;
15630Sstevel@tonic-gate 	}
15640Sstevel@tonic-gate }
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate static void
15670Sstevel@tonic-gate create_newstat(void)
15680Sstevel@tonic-gate {
15690Sstevel@tonic-gate 	int error;
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 	error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0);
15720Sstevel@tonic-gate 	if (error) {
15730Sstevel@tonic-gate 		/*
15740Sstevel@tonic-gate 		 * Not fatal - we can still create archive
15750Sstevel@tonic-gate 		 */
15760Sstevel@tonic-gate 		walk_arg.new_nvlp = NULL;
15770Sstevel@tonic-gate 		bam_error(NVALLOC_FAIL, strerror(error));
15780Sstevel@tonic-gate 	}
15790Sstevel@tonic-gate }
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate static void
15820Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp)
15830Sstevel@tonic-gate {
15840Sstevel@tonic-gate 	char path[PATH_MAX];
15850Sstevel@tonic-gate 	line_t *lp;
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
15880Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, lp->line);
15890Sstevel@tonic-gate 		/* XXX shouldn't we use FTW_MOUNT ? */
15900Sstevel@tonic-gate 		if (nftw(path, cmpstat, 20, 0) == -1) {
15910Sstevel@tonic-gate 			/*
15920Sstevel@tonic-gate 			 * Some files may not exist.
15930Sstevel@tonic-gate 			 * For example: etc/rtc_config on a x86 diskless system
15940Sstevel@tonic-gate 			 * Emit verbose message only
15950Sstevel@tonic-gate 			 */
15960Sstevel@tonic-gate 			if (bam_verbose)
15970Sstevel@tonic-gate 				bam_print(NFTW_FAIL, path, strerror(errno));
15980Sstevel@tonic-gate 		}
15990Sstevel@tonic-gate 	}
16000Sstevel@tonic-gate }
16010Sstevel@tonic-gate 
16020Sstevel@tonic-gate static void
16030Sstevel@tonic-gate savenew(char *root)
16040Sstevel@tonic-gate {
16050Sstevel@tonic-gate 	char path[PATH_MAX];
16060Sstevel@tonic-gate 	char path2[PATH_MAX];
16070Sstevel@tonic-gate 	size_t sz;
16080Sstevel@tonic-gate 	char *nstat;
16090Sstevel@tonic-gate 	int fd, wrote, error;
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	nstat = NULL;
16120Sstevel@tonic-gate 	sz = 0;
16130Sstevel@tonic-gate 	error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz,
16140Sstevel@tonic-gate 	    NV_ENCODE_XDR, 0);
16150Sstevel@tonic-gate 	if (error) {
16160Sstevel@tonic-gate 		bam_error(PACK_FAIL, strerror(error));
16170Sstevel@tonic-gate 		return;
16180Sstevel@tonic-gate 	}
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP);
16210Sstevel@tonic-gate 	fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE);
16220Sstevel@tonic-gate 	if (fd == -1) {
16230Sstevel@tonic-gate 		bam_error(OPEN_FAIL, path, strerror(errno));
16240Sstevel@tonic-gate 		free(nstat);
16250Sstevel@tonic-gate 		return;
16260Sstevel@tonic-gate 	}
16270Sstevel@tonic-gate 	wrote = write(fd, nstat, sz);
16280Sstevel@tonic-gate 	if (wrote != sz) {
16290Sstevel@tonic-gate 		bam_error(WRITE_FAIL, path, strerror(errno));
16300Sstevel@tonic-gate 		(void) close(fd);
16310Sstevel@tonic-gate 		free(nstat);
16320Sstevel@tonic-gate 		return;
16330Sstevel@tonic-gate 	}
16340Sstevel@tonic-gate 	(void) close(fd);
16350Sstevel@tonic-gate 	free(nstat);
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 	(void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT);
16380Sstevel@tonic-gate 	if (rename(path, path2) != 0) {
16390Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path2, strerror(errno));
16400Sstevel@tonic-gate 	}
16410Sstevel@tonic-gate }
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate static void
16440Sstevel@tonic-gate clear_walk_args(void)
16450Sstevel@tonic-gate {
16460Sstevel@tonic-gate 	if (walk_arg.old_nvlp)
16470Sstevel@tonic-gate 		nvlist_free(walk_arg.old_nvlp);
16480Sstevel@tonic-gate 	if (walk_arg.new_nvlp)
16490Sstevel@tonic-gate 		nvlist_free(walk_arg.new_nvlp);
16500Sstevel@tonic-gate 	walk_arg.need_update = 0;
16510Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
16520Sstevel@tonic-gate 	walk_arg.new_nvlp = NULL;
16530Sstevel@tonic-gate }
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate /*
16560Sstevel@tonic-gate  * Returns:
16570Sstevel@tonic-gate  *	0 - no update necessary
16580Sstevel@tonic-gate  *	1 - update required.
16590Sstevel@tonic-gate  *	BAM_ERROR (-1) - An error occurred
16600Sstevel@tonic-gate  *
16610Sstevel@tonic-gate  * Special handling for check (-n):
16620Sstevel@tonic-gate  * ================================
16630Sstevel@tonic-gate  * The check (-n) option produces parseable output.
16640Sstevel@tonic-gate  * To do this, we suppress all stdout messages unrelated
16650Sstevel@tonic-gate  * to out of sync files.
16660Sstevel@tonic-gate  * All stderr messages are still printed though.
16670Sstevel@tonic-gate  *
16680Sstevel@tonic-gate  */
16690Sstevel@tonic-gate static int
16700Sstevel@tonic-gate update_required(char *root)
16710Sstevel@tonic-gate {
16720Sstevel@tonic-gate 	struct stat sb;
16730Sstevel@tonic-gate 	char path[PATH_MAX];
16740Sstevel@tonic-gate 	filelist_t flist;
16750Sstevel@tonic-gate 	filelist_t *flistp = &flist;
16760Sstevel@tonic-gate 	int need_update;
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 	walk_arg.need_update = 0;
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 	/*
16830Sstevel@tonic-gate 	 * Without consulting stat data, check if we need update
16840Sstevel@tonic-gate 	 */
16850Sstevel@tonic-gate 	check_flags_and_files(root);
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	/*
16880Sstevel@tonic-gate 	 * In certain deployment scenarios, filestat may not
16890Sstevel@tonic-gate 	 * exist. Ignore it during boot-archive SMF check.
16900Sstevel@tonic-gate 	 */
16910Sstevel@tonic-gate 	if (bam_smf_check) {
16920Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
16930Sstevel@tonic-gate 		if (stat(path, &sb) != 0)
16940Sstevel@tonic-gate 			return (0);
16950Sstevel@tonic-gate 	}
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 	/*
16980Sstevel@tonic-gate 	 * consult stat data only if we haven't made a decision
16990Sstevel@tonic-gate 	 * about update. If checking (-n) however, we always
17000Sstevel@tonic-gate 	 * need stat data (since we want to compare old and new)
17010Sstevel@tonic-gate 	 */
17020Sstevel@tonic-gate 	if (!walk_arg.need_update || bam_check)
17030Sstevel@tonic-gate 		getoldstat(root);
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 	/*
17060Sstevel@tonic-gate 	 * read list of files
17070Sstevel@tonic-gate 	 */
17080Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
17090Sstevel@tonic-gate 		clear_walk_args();
17100Sstevel@tonic-gate 		return (BAM_ERROR);
17110Sstevel@tonic-gate 	}
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 	/*
17160Sstevel@tonic-gate 	 * At this point either the update is required
17170Sstevel@tonic-gate 	 * or the decision is pending. In either case
17180Sstevel@tonic-gate 	 * we need to create new stat nvlist
17190Sstevel@tonic-gate 	 */
17200Sstevel@tonic-gate 	create_newstat();
17210Sstevel@tonic-gate 
17220Sstevel@tonic-gate 	/*
17230Sstevel@tonic-gate 	 * This walk does 2 things:
17240Sstevel@tonic-gate 	 *  	- gets new stat data for every file
17250Sstevel@tonic-gate 	 *	- (optional) compare old and new stat data
17260Sstevel@tonic-gate 	 */
17270Sstevel@tonic-gate 	walk_list(root, &flist);
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	/* done with the file list */
17300Sstevel@tonic-gate 	filelist_free(flistp);
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 	/*
17330Sstevel@tonic-gate 	 * if we didn't succeed in  creating new stat data above
17340Sstevel@tonic-gate 	 * just return result of update check so that archive is built.
17350Sstevel@tonic-gate 	 */
17360Sstevel@tonic-gate 	if (walk_arg.new_nvlp == NULL) {
17370Sstevel@tonic-gate 		bam_error(NO_NEW_STAT);
17380Sstevel@tonic-gate 		need_update = walk_arg.need_update;
17390Sstevel@tonic-gate 		clear_walk_args();
17400Sstevel@tonic-gate 		return (need_update ? 1 : 0);
17410Sstevel@tonic-gate 	}
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 	/*
17450Sstevel@tonic-gate 	 * If no update required, discard newstat
17460Sstevel@tonic-gate 	 */
17470Sstevel@tonic-gate 	if (!walk_arg.need_update) {
17480Sstevel@tonic-gate 		clear_walk_args();
17490Sstevel@tonic-gate 		return (0);
17500Sstevel@tonic-gate 	}
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate 	/*
17530Sstevel@tonic-gate 	 * At this point we need an update - so save new stat data
17540Sstevel@tonic-gate 	 * However, if only checking (-n), don't save new stat data.
17550Sstevel@tonic-gate 	 */
17560Sstevel@tonic-gate 	if (!bam_check)
17570Sstevel@tonic-gate 		savenew(root);
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 	clear_walk_args();
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 	return (1);
17620Sstevel@tonic-gate }
17630Sstevel@tonic-gate 
17640Sstevel@tonic-gate static error_t
17650Sstevel@tonic-gate create_ramdisk(char *root)
17660Sstevel@tonic-gate {
17670Sstevel@tonic-gate 	char *cmdline, path[PATH_MAX];
17680Sstevel@tonic-gate 	size_t len;
17690Sstevel@tonic-gate 	struct stat sb;
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate 	/*
17720Sstevel@tonic-gate 	 * Setup command args for create_ramdisk.ksh
17730Sstevel@tonic-gate 	 */
17740Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, CREATE_RAMDISK);
17750Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
17760Sstevel@tonic-gate 		bam_error(ARCH_EXEC_MISS, path, strerror(errno));
17770Sstevel@tonic-gate 		return (BAM_ERROR);
17780Sstevel@tonic-gate 	}
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 	len = strlen(path) + strlen(root) + 10;	/* room for space + -R */
17810Sstevel@tonic-gate 	cmdline = s_calloc(1, len);
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 	if (strlen(root) > 1) {
17840Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s -R %s", path, root);
17850Sstevel@tonic-gate 		/* chop off / at the end */
17860Sstevel@tonic-gate 		cmdline[strlen(cmdline) - 1] = '\0';
17870Sstevel@tonic-gate 	} else
17880Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s", path);
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 	if (exec_cmd(cmdline, NULL, 0) != 0) {
17910Sstevel@tonic-gate 		bam_error(ARCHIVE_FAIL, cmdline);
17920Sstevel@tonic-gate 		free(cmdline);
17930Sstevel@tonic-gate 		return (BAM_ERROR);
17940Sstevel@tonic-gate 	}
17950Sstevel@tonic-gate 	free(cmdline);
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 	/*
17980Sstevel@tonic-gate 	 * Verify that the archive has been created
17990Sstevel@tonic-gate 	 */
18000Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE);
18010Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
18020Sstevel@tonic-gate 		bam_error(ARCHIVE_NOT_CREATED, path);
18030Sstevel@tonic-gate 		return (BAM_ERROR);
18040Sstevel@tonic-gate 	}
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate 	return (BAM_SUCCESS);
18070Sstevel@tonic-gate }
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate /*
18100Sstevel@tonic-gate  * Checks if target filesystem is on a ramdisk
18110Sstevel@tonic-gate  * 1 - is miniroot
18120Sstevel@tonic-gate  * 0 - is not
18130Sstevel@tonic-gate  * When in doubt assume it is not a ramdisk.
18140Sstevel@tonic-gate  */
18150Sstevel@tonic-gate static int
18160Sstevel@tonic-gate is_ramdisk(char *root)
18170Sstevel@tonic-gate {
18180Sstevel@tonic-gate 	struct extmnttab mnt;
18190Sstevel@tonic-gate 	FILE *fp;
18200Sstevel@tonic-gate 	int found;
1821316Svikram 	char mntpt[PATH_MAX];
1822316Svikram 	char *cp;
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate 	/*
18250Sstevel@tonic-gate 	 * There are 3 situations where creating archive is
18260Sstevel@tonic-gate 	 * of dubious value:
1827316Svikram 	 *	- create boot_archive on a lofi-mounted boot_archive
18280Sstevel@tonic-gate 	 *	- create it on a ramdisk which is the root filesystem
18290Sstevel@tonic-gate 	 *	- create it on a ramdisk mounted somewhere else
18300Sstevel@tonic-gate 	 * The first is not easy to detect and checking for it is not
18310Sstevel@tonic-gate 	 * worth it.
18320Sstevel@tonic-gate 	 * The other two conditions are handled here
18330Sstevel@tonic-gate 	 */
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
18360Sstevel@tonic-gate 	if (fp == NULL) {
18370Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
18380Sstevel@tonic-gate 		return (0);
18390Sstevel@tonic-gate 	}
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate 	resetmnttab(fp);
18420Sstevel@tonic-gate 
1843316Svikram 	/*
1844316Svikram 	 * Remove any trailing / from the mount point
1845316Svikram 	 */
1846316Svikram 	(void) strlcpy(mntpt, root, sizeof (mntpt));
1847316Svikram 	if (strcmp(root, "/") != 0) {
1848316Svikram 		cp = mntpt + strlen(mntpt) - 1;
1849316Svikram 		if (*cp == '/')
1850316Svikram 			*cp = '\0';
1851316Svikram 	}
18520Sstevel@tonic-gate 	found = 0;
18530Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
1854316Svikram 		if (strcmp(mnt.mnt_mountp, mntpt) == 0) {
18550Sstevel@tonic-gate 			found = 1;
18560Sstevel@tonic-gate 			break;
18570Sstevel@tonic-gate 		}
18580Sstevel@tonic-gate 	}
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 	if (!found) {
18610Sstevel@tonic-gate 		if (bam_verbose)
1862316Svikram 			bam_error(NOT_IN_MNTTAB, mntpt);
18630Sstevel@tonic-gate 		(void) fclose(fp);
18640Sstevel@tonic-gate 		return (0);
18650Sstevel@tonic-gate 	}
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 	if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) {
18680Sstevel@tonic-gate 		if (bam_verbose)
18690Sstevel@tonic-gate 			bam_error(IS_RAMDISK, bam_root);
18700Sstevel@tonic-gate 		(void) fclose(fp);
18710Sstevel@tonic-gate 		return (1);
18720Sstevel@tonic-gate 	}
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate 	(void) fclose(fp);
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 	return (0);
18770Sstevel@tonic-gate }
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate static int
18800Sstevel@tonic-gate is_newboot(char *root)
18810Sstevel@tonic-gate {
18820Sstevel@tonic-gate 	char path[PATH_MAX];
18830Sstevel@tonic-gate 	struct stat sb;
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	/*
18860Sstevel@tonic-gate 	 * We can't boot without MULTI_BOOT
18870Sstevel@tonic-gate 	 */
18880Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, MULTI_BOOT);
18890Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
18900Sstevel@tonic-gate 		if (bam_verbose)
18910Sstevel@tonic-gate 			bam_print(FILE_MISS, path);
18920Sstevel@tonic-gate 		return (0);
18930Sstevel@tonic-gate 	}
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate 	/*
18960Sstevel@tonic-gate 	 * We can't generate archive without GRUB_DIR
18970Sstevel@tonic-gate 	 */
18980Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, GRUB_DIR);
18990Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
19000Sstevel@tonic-gate 		if (bam_verbose)
19010Sstevel@tonic-gate 			bam_print(DIR_MISS, path);
19020Sstevel@tonic-gate 		return (0);
19030Sstevel@tonic-gate 	}
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate 	return (1);
19060Sstevel@tonic-gate }
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate static int
19090Sstevel@tonic-gate is_readonly(char *root)
19100Sstevel@tonic-gate {
19110Sstevel@tonic-gate 	struct statvfs vfs;
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 	/*
19140Sstevel@tonic-gate 	 * Check for RDONLY filesystem
19150Sstevel@tonic-gate 	 * When in doubt assume it is not readonly
19160Sstevel@tonic-gate 	 */
19170Sstevel@tonic-gate 	if (statvfs(root, &vfs) != 0) {
19180Sstevel@tonic-gate 		if (bam_verbose)
19190Sstevel@tonic-gate 			bam_error(STATVFS_FAIL, root, strerror(errno));
19200Sstevel@tonic-gate 		return (0);
19210Sstevel@tonic-gate 	}
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 	if (vfs.f_flag & ST_RDONLY) {
19240Sstevel@tonic-gate 		return (1);
19250Sstevel@tonic-gate 	}
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate 	return (0);
19280Sstevel@tonic-gate }
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate static error_t
19310Sstevel@tonic-gate update_archive(char *root, char *opt)
19320Sstevel@tonic-gate {
19330Sstevel@tonic-gate 	error_t ret;
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate 	assert(root);
19360Sstevel@tonic-gate 	assert(opt == NULL);
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 	/*
1939316Svikram 	 * root must belong to a GRUB boot OS,
19400Sstevel@tonic-gate 	 * don't care on sparc except for diskless clients
19410Sstevel@tonic-gate 	 */
19420Sstevel@tonic-gate 	if (!is_newboot(root)) {
1943316Svikram 		/*
1944316Svikram 		 * Emit message only if not in context of update_all.
1945316Svikram 		 * If in update_all, emit only if verbose flag is set.
1946316Svikram 		 */
1947316Svikram 		if (!bam_update_all || bam_verbose)
1948316Svikram 			bam_print(NOT_GRUB_BOOT, root);
19490Sstevel@tonic-gate 		return (BAM_SUCCESS);
19500Sstevel@tonic-gate 	}
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 	/*
1953662Sszhou 	 * If smf check is requested when / is writable (can happen
1954662Sszhou 	 * on first reboot following an upgrade because service
1955662Sszhou 	 * dependency is messed up), skip the check.
1956662Sszhou 	 */
1957662Sszhou 	if (bam_smf_check && !bam_root_readonly)
1958662Sszhou 		return (BAM_SUCCESS);
1959662Sszhou 
1960662Sszhou 	/*
1961662Sszhou 	 * root must be writable. This check applies to alternate
1962662Sszhou 	 * root (-R option); bam_root_readonly applies to '/' only.
19630Sstevel@tonic-gate 	 * Note: statvfs() does not always report the truth
19640Sstevel@tonic-gate 	 */
1965756Ssetje 	if (!bam_smf_check && !bam_check && is_readonly(root)) {
1966662Sszhou 		if (bam_verbose)
19670Sstevel@tonic-gate 			bam_print(RDONLY_FS, root);
19680Sstevel@tonic-gate 		return (BAM_SUCCESS);
19690Sstevel@tonic-gate 	}
19700Sstevel@tonic-gate 
19710Sstevel@tonic-gate 	/*
19720Sstevel@tonic-gate 	 * Don't generate archive on ramdisk
19730Sstevel@tonic-gate 	 */
19740Sstevel@tonic-gate 	if (is_ramdisk(root)) {
19750Sstevel@tonic-gate 		if (bam_verbose)
19760Sstevel@tonic-gate 			bam_print(SKIP_RAMDISK);
19770Sstevel@tonic-gate 		return (BAM_SUCCESS);
19780Sstevel@tonic-gate 	}
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate 	/*
19810Sstevel@tonic-gate 	 * Now check if updated is really needed
19820Sstevel@tonic-gate 	 */
19830Sstevel@tonic-gate 	ret = update_required(root);
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 	/*
19860Sstevel@tonic-gate 	 * The check command (-n) is *not* a dry run
19870Sstevel@tonic-gate 	 * It only checks if the archive is in sync.
19880Sstevel@tonic-gate 	 */
19890Sstevel@tonic-gate 	if (bam_check) {
19900Sstevel@tonic-gate 		bam_exit((ret != 0) ? 1 : 0);
19910Sstevel@tonic-gate 	}
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 	if (ret == 1) {
19940Sstevel@tonic-gate 		/* create the ramdisk */
19950Sstevel@tonic-gate 		ret = create_ramdisk(root);
19960Sstevel@tonic-gate 	}
19970Sstevel@tonic-gate 	return (ret);
19980Sstevel@tonic-gate }
19990Sstevel@tonic-gate 
2000316Svikram static void
20011746Svikram update_fdisk(void)
20021746Svikram {
20031746Svikram 	struct stat sb;
20041746Svikram 	char cmd[PATH_MAX];
20051746Svikram 	int ret1, ret2;
20061746Svikram 
20071746Svikram 	assert(stat(GRUB_fdisk, &sb) == 0);
20081746Svikram 	assert(stat(GRUB_fdisk_target, &sb) == 0);
20091746Svikram 
20101746Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/fdisk -F %s `/bin/cat %s`",
20111746Svikram 	    GRUB_fdisk, GRUB_fdisk_target);
20121746Svikram 
20131746Svikram 	bam_print(UPDATING_FDISK);
20141746Svikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
20151746Svikram 		bam_error(FDISK_UPDATE_FAILED);
20161746Svikram 	}
20171746Svikram 
20181746Svikram 	/*
20191746Svikram 	 * We are done, remove the files.
20201746Svikram 	 */
20211746Svikram 	ret1 = unlink(GRUB_fdisk);
20221746Svikram 	ret2 = unlink(GRUB_fdisk_target);
20231746Svikram 	if (ret1 != 0 || ret2 != 0) {
20241746Svikram 		bam_error(FILE_REMOVE_FAILED, GRUB_fdisk, GRUB_fdisk_target);
20251746Svikram 	}
20261746Svikram }
20271746Svikram 
20281746Svikram static void
2029316Svikram restore_grub_slice(void)
2030316Svikram {
2031316Svikram 	struct stat sb;
2032316Svikram 	char *mntpt, *physlice;
2033316Svikram 	int mnted;	/* set if we did a mount */
2034316Svikram 	char menupath[PATH_MAX], cmd[PATH_MAX];
2035316Svikram 
2036316Svikram 	if (stat(GRUB_slice, &sb) != 0) {
2037316Svikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
2038316Svikram 		return;
2039316Svikram 	}
2040316Svikram 
2041316Svikram 	/*
2042316Svikram 	 * If we are doing an luactivate, don't attempt to restore GRUB or else
2043316Svikram 	 * we may not be able to get to DCA boot environments. Let luactivate
2044316Svikram 	 * handle GRUB/DCA installation
2045316Svikram 	 */
2046316Svikram 	if (stat(LU_ACTIVATE_FILE, &sb) == 0) {
2047316Svikram 		return;
2048316Svikram 	}
2049316Svikram 
2050316Svikram 	mnted = 0;
2051316Svikram 	physlice = NULL;
2052621Svikram 	mntpt = mount_grub_slice(&mnted, &physlice, NULL, NULL);
2053316Svikram 	if (mntpt == NULL) {
2054316Svikram 		bam_error(CANNOT_RESTORE_GRUB_SLICE);
2055316Svikram 		return;
2056316Svikram 	}
2057316Svikram 
2058316Svikram 	(void) snprintf(menupath, sizeof (menupath), "%s%s", mntpt, GRUB_MENU);
2059316Svikram 	if (stat(menupath, &sb) == 0) {
2060621Svikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2061316Svikram 		return;
2062316Svikram 	}
2063316Svikram 
2064316Svikram 	/*
2065316Svikram 	 * The menu is missing - we need to do a restore
2066316Svikram 	 */
2067316Svikram 	bam_print(RESTORING_GRUB);
2068316Svikram 
2069316Svikram 	(void) snprintf(cmd, sizeof (cmd), "%s %s %s %s",
2070316Svikram 	    INSTALLGRUB, STAGE1, STAGE2, physlice);
2071316Svikram 
2072316Svikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2073316Svikram 		bam_error(RESTORE_GRUB_FAILED);
2074621Svikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2075316Svikram 		return;
2076316Svikram 	}
2077316Svikram 
2078316Svikram 	if (stat(GRUB_backup_menu, &sb) != 0) {
2079316Svikram 		bam_error(MISSING_BACKUP_MENU,
2080316Svikram 		    GRUB_backup_menu, strerror(errno));
2081621Svikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2082316Svikram 		return;
2083316Svikram 	}
2084316Svikram 
2085316Svikram 	(void) snprintf(cmd, sizeof (cmd), "/bin/cp %s %s",
2086316Svikram 	    GRUB_backup_menu, menupath);
2087316Svikram 
2088316Svikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2089316Svikram 		bam_error(RESTORE_MENU_FAILED, menupath);
2090621Svikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2091316Svikram 		return;
2092316Svikram 	}
2093316Svikram 
2094316Svikram 	/* Success */
2095621Svikram 	umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2096316Svikram }
2097316Svikram 
20980Sstevel@tonic-gate static error_t
20990Sstevel@tonic-gate update_all(char *root, char *opt)
21000Sstevel@tonic-gate {
21010Sstevel@tonic-gate 	struct extmnttab mnt;
21020Sstevel@tonic-gate 	struct stat sb;
21030Sstevel@tonic-gate 	FILE *fp;
21040Sstevel@tonic-gate 	char multibt[PATH_MAX];
21050Sstevel@tonic-gate 	error_t ret = BAM_SUCCESS;
21061746Svikram 	int ret1, ret2;
21070Sstevel@tonic-gate 
2108621Svikram 	assert(root);
21090Sstevel@tonic-gate 	assert(opt == NULL);
21100Sstevel@tonic-gate 
2111621Svikram 	if (bam_rootlen != 1 || *root != '/') {
2112621Svikram 		elide_trailing_slash(root, multibt, sizeof (multibt));
2113621Svikram 		bam_error(ALT_ROOT_INVALID, multibt);
2114621Svikram 		return (BAM_ERROR);
2115621Svikram 	}
2116621Svikram 
21170Sstevel@tonic-gate 	/*
21180Sstevel@tonic-gate 	 * First update archive for current root
21190Sstevel@tonic-gate 	 */
21200Sstevel@tonic-gate 	if (update_archive(root, opt) != BAM_SUCCESS)
21210Sstevel@tonic-gate 		ret = BAM_ERROR;
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate 	/*
21240Sstevel@tonic-gate 	 * Now walk the mount table, performing archive update
21250Sstevel@tonic-gate 	 * for all mounted Newboot root filesystems
21260Sstevel@tonic-gate 	 */
21270Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
21280Sstevel@tonic-gate 	if (fp == NULL) {
21290Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
2130316Svikram 		ret = BAM_ERROR;
2131316Svikram 		goto out;
21320Sstevel@tonic-gate 	}
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 	resetmnttab(fp);
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
21370Sstevel@tonic-gate 		if (mnt.mnt_special == NULL)
21380Sstevel@tonic-gate 			continue;
21390Sstevel@tonic-gate 		if (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0)
21400Sstevel@tonic-gate 			continue;
21410Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, "/") == 0)
21420Sstevel@tonic-gate 			continue;
21430Sstevel@tonic-gate 
21440Sstevel@tonic-gate 		(void) snprintf(multibt, sizeof (multibt), "%s%s",
21450Sstevel@tonic-gate 		    mnt.mnt_mountp, MULTI_BOOT);
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate 		if (stat(multibt, &sb) == -1)
21480Sstevel@tonic-gate 			continue;
21490Sstevel@tonic-gate 
21500Sstevel@tonic-gate 		/*
21510Sstevel@tonic-gate 		 * We put a trailing slash to be consistent with root = "/"
21520Sstevel@tonic-gate 		 * case, such that we don't have to print // in some cases.
21530Sstevel@tonic-gate 		 */
21540Sstevel@tonic-gate 		(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
21550Sstevel@tonic-gate 		    mnt.mnt_mountp);
21560Sstevel@tonic-gate 		bam_rootlen = strlen(rootbuf);
21570Sstevel@tonic-gate 		if (update_archive(rootbuf, opt) != BAM_SUCCESS)
21580Sstevel@tonic-gate 			ret = BAM_ERROR;
21590Sstevel@tonic-gate 	}
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 	(void) fclose(fp);
21620Sstevel@tonic-gate 
2163316Svikram out:
2164316Svikram 	if (stat(GRUB_slice, &sb) == 0) {
2165316Svikram 		restore_grub_slice();
2166316Svikram 	}
2167316Svikram 
21681746Svikram 	/*
21691746Svikram 	 * Update fdisk table as we go down. Updating it when
21701746Svikram 	 * the system is running will confuse biosdev.
21711746Svikram 	 */
21721746Svikram 	ret1 = stat(GRUB_fdisk, &sb);
21731746Svikram 	ret2 = stat(GRUB_fdisk_target, &sb);
21741746Svikram 	if ((ret1 == 0) && (ret2 == 0)) {
21751746Svikram 		update_fdisk();
21761746Svikram 	} else if ((ret1 == 0) ^ (ret2 == 0)) {
21771746Svikram 		/*
21781746Svikram 		 * It is an error for one file to be
21791746Svikram 		 * present and the other absent.
21801746Svikram 		 * It is normal for both files to be
21811746Svikram 		 * absent - it indicates that no fdisk
21821746Svikram 		 * update is required.
21831746Svikram 		 */
21841746Svikram 		bam_error(MISSING_FDISK_FILE,
21851746Svikram 		    ret1 ? GRUB_fdisk : GRUB_fdisk_target);
21861746Svikram 		ret = BAM_ERROR;
21871746Svikram 	}
21881746Svikram 
21890Sstevel@tonic-gate 	return (ret);
21900Sstevel@tonic-gate }
21910Sstevel@tonic-gate 
21920Sstevel@tonic-gate static void
21930Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp)
21940Sstevel@tonic-gate {
21950Sstevel@tonic-gate 	if (mp->start == NULL) {
21960Sstevel@tonic-gate 		mp->start = lp;
21970Sstevel@tonic-gate 	} else {
21980Sstevel@tonic-gate 		mp->end->next = lp;
2199662Sszhou 		lp->prev = mp->end;
22000Sstevel@tonic-gate 	}
22010Sstevel@tonic-gate 	mp->end = lp;
22020Sstevel@tonic-gate }
22030Sstevel@tonic-gate 
2204662Sszhou static void
2205662Sszhou unlink_line(menu_t *mp, line_t *lp)
2206662Sszhou {
2207662Sszhou 	/* unlink from list */
2208662Sszhou 	if (lp->prev)
2209662Sszhou 		lp->prev->next = lp->next;
2210662Sszhou 	else
2211662Sszhou 		mp->start = lp->next;
2212662Sszhou 	if (lp->next)
2213662Sszhou 		lp->next->prev = lp->prev;
2214662Sszhou 	else
2215662Sszhou 		mp->end = lp->prev;
2216662Sszhou }
2217662Sszhou 
2218662Sszhou static entry_t *
2219662Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end)
2220662Sszhou {
2221662Sszhou 	entry_t *ent, *prev;
2222662Sszhou 
2223662Sszhou 	ent = s_calloc(1, sizeof (entry_t));
2224662Sszhou 	ent->start = start;
2225662Sszhou 	ent->end = end;
2226662Sszhou 
2227662Sszhou 	if (mp->entries == NULL) {
2228662Sszhou 		mp->entries = ent;
2229662Sszhou 		return (ent);
2230662Sszhou 	}
2231662Sszhou 
2232662Sszhou 	prev = mp->entries;
2233662Sszhou 	while (prev->next)
2234662Sszhou 		prev = prev-> next;
2235662Sszhou 	prev->next = ent;
2236662Sszhou 	ent->prev = prev;
2237662Sszhou 	return (ent);
2238662Sszhou }
2239662Sszhou 
2240662Sszhou static void
2241662Sszhou boot_entry_addline(entry_t *ent, line_t *lp)
2242662Sszhou {
2243662Sszhou 	if (ent)
2244662Sszhou 		ent->end = lp;
2245662Sszhou }
2246662Sszhou 
22470Sstevel@tonic-gate /*
22480Sstevel@tonic-gate  * A line in menu.lst looks like
22490Sstevel@tonic-gate  * [ ]*<cmd>[ \t=]*<arg>*
22500Sstevel@tonic-gate  */
22510Sstevel@tonic-gate static void
22520Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum)
22530Sstevel@tonic-gate {
22540Sstevel@tonic-gate 	/*
22550Sstevel@tonic-gate 	 * save state across calls. This is so that
22560Sstevel@tonic-gate 	 * header gets the right entry# after title has
22570Sstevel@tonic-gate 	 * been processed
22580Sstevel@tonic-gate 	 */
2259662Sszhou 	static line_t *prev = NULL;
2260662Sszhou 	static entry_t *curr_ent = NULL;
22610Sstevel@tonic-gate 
22620Sstevel@tonic-gate 	line_t	*lp;
22630Sstevel@tonic-gate 	char *cmd, *sep, *arg;
22640Sstevel@tonic-gate 	char save, *cp, *line;
22650Sstevel@tonic-gate 	menu_flag_t flag = BAM_INVALID;
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate 	if (str == NULL) {
22680Sstevel@tonic-gate 		return;
22690Sstevel@tonic-gate 	}
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 	/*
22720Sstevel@tonic-gate 	 * First save a copy of the entire line.
22730Sstevel@tonic-gate 	 * We use this later to set the line field.
22740Sstevel@tonic-gate 	 */
22750Sstevel@tonic-gate 	line = s_strdup(str);
22760Sstevel@tonic-gate 
22770Sstevel@tonic-gate 	/* Eat up leading whitespace */
22780Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
22790Sstevel@tonic-gate 		str++;
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 	if (*str == '#') {		/* comment */
22820Sstevel@tonic-gate 		cmd = s_strdup("#");
22830Sstevel@tonic-gate 		sep = NULL;
22840Sstevel@tonic-gate 		arg = s_strdup(str + 1);
22850Sstevel@tonic-gate 		flag = BAM_COMMENT;
22860Sstevel@tonic-gate 	} else if (*str == '\0') {	/* blank line */
22870Sstevel@tonic-gate 		cmd = sep = arg = NULL;
22880Sstevel@tonic-gate 		flag = BAM_EMPTY;
22890Sstevel@tonic-gate 	} else {
22900Sstevel@tonic-gate 		/*
22910Sstevel@tonic-gate 		 * '=' is not a documented separator in grub syntax.
22920Sstevel@tonic-gate 		 * However various development bits use '=' as a
22930Sstevel@tonic-gate 		 * separator. In addition, external users also
22940Sstevel@tonic-gate 		 * use = as a separator. So we will allow that usage.
22950Sstevel@tonic-gate 		 */
22960Sstevel@tonic-gate 		cp = str;
22970Sstevel@tonic-gate 		while (*str != ' ' && *str != '\t' && *str != '=') {
22980Sstevel@tonic-gate 			if (*str == '\0') {
22990Sstevel@tonic-gate 				cmd = s_strdup(cp);
23000Sstevel@tonic-gate 				sep = arg = NULL;
23010Sstevel@tonic-gate 				break;
23020Sstevel@tonic-gate 			}
23030Sstevel@tonic-gate 			str++;
23040Sstevel@tonic-gate 		}
23050Sstevel@tonic-gate 
23060Sstevel@tonic-gate 		if (*str != '\0') {
23070Sstevel@tonic-gate 			save = *str;
23080Sstevel@tonic-gate 			*str = '\0';
23090Sstevel@tonic-gate 			cmd = s_strdup(cp);
23100Sstevel@tonic-gate 			*str = save;
23110Sstevel@tonic-gate 
23120Sstevel@tonic-gate 			str++;
23130Sstevel@tonic-gate 			save = *str;
23140Sstevel@tonic-gate 			*str = '\0';
23150Sstevel@tonic-gate 			sep = s_strdup(str - 1);
23160Sstevel@tonic-gate 			*str = save;
23170Sstevel@tonic-gate 
23180Sstevel@tonic-gate 			while (*str == ' ' || *str == '\t')
23190Sstevel@tonic-gate 				str++;
23200Sstevel@tonic-gate 			if (*str == '\0')
23210Sstevel@tonic-gate 				arg = NULL;
23220Sstevel@tonic-gate 			else
23230Sstevel@tonic-gate 				arg = s_strdup(str);
23240Sstevel@tonic-gate 		}
23250Sstevel@tonic-gate 	}
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
23280Sstevel@tonic-gate 
23290Sstevel@tonic-gate 	lp->cmd = cmd;
23300Sstevel@tonic-gate 	lp->sep = sep;
23310Sstevel@tonic-gate 	lp->arg = arg;
23320Sstevel@tonic-gate 	lp->line = line;
23330Sstevel@tonic-gate 	lp->lineNum = ++(*lineNum);
23340Sstevel@tonic-gate 	if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) {
23350Sstevel@tonic-gate 		lp->entryNum = ++(*entryNum);
23360Sstevel@tonic-gate 		lp->flags = BAM_TITLE;
23370Sstevel@tonic-gate 		if (prev && prev->flags == BAM_COMMENT &&
2338662Sszhou 		    prev->arg && strcmp(prev->arg, BAM_HDR) == 0) {
23390Sstevel@tonic-gate 			prev->entryNum = lp->entryNum;
2340662Sszhou 			curr_ent = boot_entry_new(mp, prev, lp);
2341662Sszhou 		} else {
2342662Sszhou 			curr_ent = boot_entry_new(mp, lp, lp);
2343662Sszhou 		}
23440Sstevel@tonic-gate 	} else if (flag != BAM_INVALID) {
23450Sstevel@tonic-gate 		/*
23460Sstevel@tonic-gate 		 * For header comments, the entry# is "fixed up"
23470Sstevel@tonic-gate 		 * by the subsequent title
23480Sstevel@tonic-gate 		 */
23490Sstevel@tonic-gate 		lp->entryNum = *entryNum;
23500Sstevel@tonic-gate 		lp->flags = flag;
23510Sstevel@tonic-gate 	} else {
23520Sstevel@tonic-gate 		lp->entryNum = *entryNum;
23530Sstevel@tonic-gate 		lp->flags = (*entryNum == ENTRY_INIT) ? BAM_GLOBAL : BAM_ENTRY;
23540Sstevel@tonic-gate 	}
23550Sstevel@tonic-gate 
2356662Sszhou 	/* record default, old default, and entry line ranges */
2357662Sszhou 	if (lp->flags == BAM_GLOBAL &&
2358662Sszhou 	    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) {
2359662Sszhou 		mp->curdefault = lp;
2360662Sszhou 	} else if (lp->flags == BAM_COMMENT &&
2361662Sszhou 	    strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) {
2362662Sszhou 		mp->olddefault = lp;
2363662Sszhou 	} else if (lp->flags == BAM_ENTRY ||
2364662Sszhou 	    (lp->flags == BAM_COMMENT && strcmp(lp->arg, BAM_FTR) == 0)) {
2365662Sszhou 		boot_entry_addline(curr_ent, lp);
2366662Sszhou 	}
23670Sstevel@tonic-gate 	append_line(mp, lp);
23680Sstevel@tonic-gate 
23690Sstevel@tonic-gate 	prev = lp;
23700Sstevel@tonic-gate }
23710Sstevel@tonic-gate 
2372621Svikram static void
2373621Svikram update_numbering(menu_t *mp)
2374621Svikram {
2375621Svikram 	int lineNum;
2376621Svikram 	int entryNum;
2377621Svikram 	int old_default_value;
2378621Svikram 	line_t *lp, *prev, *default_lp, *default_entry;
2379621Svikram 	char buf[PATH_MAX];
2380621Svikram 
2381621Svikram 	if (mp->start == NULL) {
2382621Svikram 		return;
2383621Svikram 	}
2384621Svikram 
2385621Svikram 	lineNum = LINE_INIT;
2386621Svikram 	entryNum = ENTRY_INIT;
2387621Svikram 	old_default_value = ENTRY_INIT;
2388621Svikram 	lp = default_lp = default_entry = NULL;
2389621Svikram 
2390621Svikram 	prev = NULL;
2391621Svikram 	for (lp = mp->start; lp; prev = lp, lp = lp->next) {
2392621Svikram 		lp->lineNum = ++lineNum;
2393621Svikram 
2394621Svikram 		/*
2395621Svikram 		 * Get the value of the default command
2396621Svikram 		 */
2397621Svikram 		if (lp->entryNum == ENTRY_INIT && lp->cmd &&
2398621Svikram 		    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 &&
2399621Svikram 		    lp->arg) {
2400621Svikram 			old_default_value = atoi(lp->arg);
2401621Svikram 			default_lp = lp;
2402621Svikram 		}
2403621Svikram 
2404621Svikram 		/*
2405621Svikram 		 * If not boot entry, nothing else to fix for this
2406621Svikram 		 * entry
2407621Svikram 		 */
2408621Svikram 		if (lp->entryNum == ENTRY_INIT)
2409621Svikram 			continue;
2410621Svikram 
2411621Svikram 		/*
2412621Svikram 		 * Record the position of the default entry.
2413621Svikram 		 * The following works because global
2414621Svikram 		 * commands like default and timeout should precede
2415621Svikram 		 * actual boot entries, so old_default_value
2416621Svikram 		 * is already known (or default cmd is missing).
2417621Svikram 		 */
2418621Svikram 		if (default_entry == NULL &&
2419621Svikram 		    old_default_value != ENTRY_INIT &&
2420621Svikram 		    lp->entryNum == old_default_value) {
2421621Svikram 			default_entry = lp;
2422621Svikram 		}
2423621Svikram 
2424621Svikram 		/*
2425621Svikram 		 * Now fixup the entry number
2426621Svikram 		 */
2427621Svikram 		if (lp->cmd && strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) {
2428621Svikram 			lp->entryNum = ++entryNum;
2429621Svikram 			/* fixup the bootadm header */
2430621Svikram 			if (prev && prev->flags == BAM_COMMENT &&
2431621Svikram 			    prev->arg && strcmp(prev->arg, BAM_HDR) == 0) {
2432621Svikram 				prev->entryNum = lp->entryNum;
2433621Svikram 			}
2434621Svikram 		} else {
2435621Svikram 			lp->entryNum = entryNum;
2436621Svikram 		}
2437621Svikram 	}
2438621Svikram 
2439621Svikram 	/*
2440621Svikram 	 * No default command in menu, simply return
2441621Svikram 	 */
2442621Svikram 	if (default_lp == NULL) {
2443621Svikram 		return;
2444621Svikram 	}
2445621Svikram 
2446621Svikram 	free(default_lp->arg);
2447621Svikram 	free(default_lp->line);
2448621Svikram 
2449621Svikram 	if (default_entry == NULL) {
2450621Svikram 		default_lp->arg = s_strdup("0");
2451621Svikram 	} else {
2452621Svikram 		(void) snprintf(buf, sizeof (buf), "%d",
2453621Svikram 		    default_entry->entryNum);
2454621Svikram 		default_lp->arg = s_strdup(buf);
2455621Svikram 	}
2456621Svikram 
2457621Svikram 	/*
2458621Svikram 	 * The following is required since only the line field gets
2459621Svikram 	 * written back to menu.lst
2460621Svikram 	 */
2461621Svikram 	(void) snprintf(buf, sizeof (buf), "%s%s%s",
2462621Svikram 	    menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg);
2463621Svikram 	default_lp->line = s_strdup(buf);
2464621Svikram }
2465621Svikram 
2466621Svikram 
24670Sstevel@tonic-gate static menu_t *
24680Sstevel@tonic-gate menu_read(char *menu_path)
24690Sstevel@tonic-gate {
24700Sstevel@tonic-gate 	FILE *fp;
24710Sstevel@tonic-gate 	char buf[BAM_MAXLINE], *cp;
24720Sstevel@tonic-gate 	menu_t *mp;
24730Sstevel@tonic-gate 	int line, entry, len, n;
24740Sstevel@tonic-gate 
24750Sstevel@tonic-gate 	mp = s_calloc(1, sizeof (menu_t));
24760Sstevel@tonic-gate 
24770Sstevel@tonic-gate 	fp = fopen(menu_path, "r");
24780Sstevel@tonic-gate 	if (fp == NULL) { /* Let the caller handle this error */
24790Sstevel@tonic-gate 		return (mp);
24800Sstevel@tonic-gate 	}
24810Sstevel@tonic-gate 
24820Sstevel@tonic-gate 
24830Sstevel@tonic-gate 	/* Note: GRUB boot entry number starts with 0 */
24840Sstevel@tonic-gate 	line = LINE_INIT;
24850Sstevel@tonic-gate 	entry = ENTRY_INIT;
24860Sstevel@tonic-gate 	cp = buf;
24870Sstevel@tonic-gate 	len = sizeof (buf);
24880Sstevel@tonic-gate 	while (s_fgets(cp, len, fp) != NULL) {
24890Sstevel@tonic-gate 		n = strlen(cp);
24900Sstevel@tonic-gate 		if (cp[n - 1] == '\\') {
24910Sstevel@tonic-gate 			len -= n - 1;
24920Sstevel@tonic-gate 			assert(len >= 2);
24930Sstevel@tonic-gate 			cp += n - 1;
24940Sstevel@tonic-gate 			continue;
24950Sstevel@tonic-gate 		}
24960Sstevel@tonic-gate 		line_parser(mp, buf, &line, &entry);
24970Sstevel@tonic-gate 		cp = buf;
24980Sstevel@tonic-gate 		len = sizeof (buf);
24990Sstevel@tonic-gate 	}
25000Sstevel@tonic-gate 
25010Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
25020Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, menu_path, strerror(errno));
25030Sstevel@tonic-gate 	}
25040Sstevel@tonic-gate 
25050Sstevel@tonic-gate 	return (mp);
25060Sstevel@tonic-gate }
25070Sstevel@tonic-gate 
25080Sstevel@tonic-gate static error_t
25090Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title)
25100Sstevel@tonic-gate {
25110Sstevel@tonic-gate 	char *eq;
25120Sstevel@tonic-gate 	char *opt_dup;
25130Sstevel@tonic-gate 	int entryNum;
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate 	assert(mp);
25160Sstevel@tonic-gate 	assert(mp->start);
25170Sstevel@tonic-gate 	assert(opt);
25180Sstevel@tonic-gate 
25190Sstevel@tonic-gate 	opt_dup = s_strdup(opt);
25200Sstevel@tonic-gate 
25210Sstevel@tonic-gate 	if (entry)
25220Sstevel@tonic-gate 		*entry = ENTRY_INIT;
25230Sstevel@tonic-gate 	if (title)
25240Sstevel@tonic-gate 		*title = NULL;
25250Sstevel@tonic-gate 
25260Sstevel@tonic-gate 	eq = strchr(opt_dup, '=');
25270Sstevel@tonic-gate 	if (eq == NULL) {
25280Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
25290Sstevel@tonic-gate 		free(opt_dup);
25300Sstevel@tonic-gate 		return (BAM_ERROR);
25310Sstevel@tonic-gate 	}
25320Sstevel@tonic-gate 
25330Sstevel@tonic-gate 	*eq = '\0';
25340Sstevel@tonic-gate 	if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) {
25350Sstevel@tonic-gate 		assert(mp->end);
25360Sstevel@tonic-gate 		entryNum = s_strtol(eq + 1);
25370Sstevel@tonic-gate 		if (entryNum < 0 || entryNum > mp->end->entryNum) {
25380Sstevel@tonic-gate 			bam_error(INVALID_ENTRY, eq + 1);
25390Sstevel@tonic-gate 			free(opt_dup);
25400Sstevel@tonic-gate 			return (BAM_ERROR);
25410Sstevel@tonic-gate 		}
25420Sstevel@tonic-gate 		*entry = entryNum;
25430Sstevel@tonic-gate 	} else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) {
25440Sstevel@tonic-gate 		*title = opt + (eq - opt_dup) + 1;
25450Sstevel@tonic-gate 	} else {
25460Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
25470Sstevel@tonic-gate 		free(opt_dup);
25480Sstevel@tonic-gate 		return (BAM_ERROR);
25490Sstevel@tonic-gate 	}
25500Sstevel@tonic-gate 
25510Sstevel@tonic-gate 	free(opt_dup);
25520Sstevel@tonic-gate 	return (BAM_SUCCESS);
25530Sstevel@tonic-gate }
25540Sstevel@tonic-gate 
25550Sstevel@tonic-gate /*
25560Sstevel@tonic-gate  * If invoked with no titles/entries (opt == NULL)
25570Sstevel@tonic-gate  * only title lines in file are printed.
25580Sstevel@tonic-gate  *
25590Sstevel@tonic-gate  * If invoked with a title or entry #, all
25600Sstevel@tonic-gate  * lines in *every* matching entry are listed
25610Sstevel@tonic-gate  */
25620Sstevel@tonic-gate static error_t
25630Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt)
25640Sstevel@tonic-gate {
25650Sstevel@tonic-gate 	line_t *lp;
25660Sstevel@tonic-gate 	int entry = ENTRY_INIT;
25670Sstevel@tonic-gate 	int found;
25680Sstevel@tonic-gate 	char *title = NULL;
25690Sstevel@tonic-gate 
25700Sstevel@tonic-gate 	assert(mp);
25710Sstevel@tonic-gate 	assert(menu_path);
25720Sstevel@tonic-gate 
25730Sstevel@tonic-gate 	if (mp->start == NULL) {
25740Sstevel@tonic-gate 		bam_error(NO_MENU, menu_path);
25750Sstevel@tonic-gate 		return (BAM_ERROR);
25760Sstevel@tonic-gate 	}
25770Sstevel@tonic-gate 
25780Sstevel@tonic-gate 	if (opt != NULL) {
25790Sstevel@tonic-gate 		if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
25800Sstevel@tonic-gate 			return (BAM_ERROR);
25810Sstevel@tonic-gate 		}
25820Sstevel@tonic-gate 		assert((entry != ENTRY_INIT) ^ (title != NULL));
25830Sstevel@tonic-gate 	} else {
25840Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0);
25850Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0);
25860Sstevel@tonic-gate 	}
25870Sstevel@tonic-gate 
25880Sstevel@tonic-gate 	found = 0;
25890Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
25900Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY)
25910Sstevel@tonic-gate 			continue;
25920Sstevel@tonic-gate 		if (opt == NULL && lp->flags == BAM_TITLE) {
25930Sstevel@tonic-gate 			bam_print(PRINT_TITLE, lp->entryNum,
25940Sstevel@tonic-gate 			    lp->arg);
25950Sstevel@tonic-gate 			found = 1;
25960Sstevel@tonic-gate 			continue;
25970Sstevel@tonic-gate 		}
25980Sstevel@tonic-gate 		if (entry != ENTRY_INIT && lp->entryNum == entry) {
25990Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
26000Sstevel@tonic-gate 			found = 1;
26010Sstevel@tonic-gate 			continue;
26020Sstevel@tonic-gate 		}
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 		/*
26050Sstevel@tonic-gate 		 * We set the entry value here so that all lines
26060Sstevel@tonic-gate 		 * in entry get printed. If we subsequently match
26070Sstevel@tonic-gate 		 * title in other entries, all lines in those
26080Sstevel@tonic-gate 		 * entries get printed as well.
26090Sstevel@tonic-gate 		 */
26100Sstevel@tonic-gate 		if (title && lp->flags == BAM_TITLE && lp->arg &&
26110Sstevel@tonic-gate 		    strncmp(title, lp->arg, strlen(title)) == 0) {
26120Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
26130Sstevel@tonic-gate 			entry = lp->entryNum;
26140Sstevel@tonic-gate 			found = 1;
26150Sstevel@tonic-gate 			continue;
26160Sstevel@tonic-gate 		}
26170Sstevel@tonic-gate 	}
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate 	if (!found) {
26200Sstevel@tonic-gate 		bam_error(NO_MATCH_ENTRY);
26210Sstevel@tonic-gate 		return (BAM_ERROR);
26220Sstevel@tonic-gate 	}
26230Sstevel@tonic-gate 
26240Sstevel@tonic-gate 	return (BAM_SUCCESS);
26250Sstevel@tonic-gate }
26260Sstevel@tonic-gate 
26270Sstevel@tonic-gate static int
26280Sstevel@tonic-gate add_boot_entry(menu_t *mp,
26290Sstevel@tonic-gate 	char *title,
26300Sstevel@tonic-gate 	char *root,
26310Sstevel@tonic-gate 	char *kernel,
26320Sstevel@tonic-gate 	char *module)
26330Sstevel@tonic-gate {
26340Sstevel@tonic-gate 	int lineNum, entryNum;
26350Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
26360Sstevel@tonic-gate 
26370Sstevel@tonic-gate 	assert(mp);
26380Sstevel@tonic-gate 
26390Sstevel@tonic-gate 	if (title == NULL) {
2640656Sszhou 		title = "Solaris";	/* default to Solaris */
26410Sstevel@tonic-gate 	}
26420Sstevel@tonic-gate 	if (kernel == NULL) {
26430Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]);
26440Sstevel@tonic-gate 		return (BAM_ERROR);
26450Sstevel@tonic-gate 	}
26460Sstevel@tonic-gate 	if (module == NULL) {
26470Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]);
26480Sstevel@tonic-gate 		return (BAM_ERROR);
26490Sstevel@tonic-gate 	}
26500Sstevel@tonic-gate 
26510Sstevel@tonic-gate 	if (mp->start) {
26520Sstevel@tonic-gate 		lineNum = mp->end->lineNum;
26530Sstevel@tonic-gate 		entryNum = mp->end->entryNum;
26540Sstevel@tonic-gate 	} else {
26550Sstevel@tonic-gate 		lineNum = LINE_INIT;
26560Sstevel@tonic-gate 		entryNum = ENTRY_INIT;
26570Sstevel@tonic-gate 	}
26580Sstevel@tonic-gate 
26590Sstevel@tonic-gate 	/*
26600Sstevel@tonic-gate 	 * No separator for comment (HDR/FTR) commands
26610Sstevel@tonic-gate 	 * The syntax for comments is #<comment>
26620Sstevel@tonic-gate 	 */
26630Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
26640Sstevel@tonic-gate 	    menu_cmds[COMMENT_CMD], BAM_HDR);
2665662Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
26660Sstevel@tonic-gate 
26670Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
26680Sstevel@tonic-gate 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
2669662Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
2670662Sszhou 
2671662Sszhou 	if (root) {
2672662Sszhou 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
2673662Sszhou 		    menu_cmds[ROOT_CMD], menu_cmds[SEP_CMD], root);
2674662Sszhou 		line_parser(mp, linebuf, &lineNum, &entryNum);
26750Sstevel@tonic-gate 	}
26760Sstevel@tonic-gate 
26770Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
26780Sstevel@tonic-gate 	    menu_cmds[KERNEL_CMD], menu_cmds[SEP_CMD], kernel);
2679662Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
26800Sstevel@tonic-gate 
26810Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
26820Sstevel@tonic-gate 	    menu_cmds[MODULE_CMD], menu_cmds[SEP_CMD], module);
2683662Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
26840Sstevel@tonic-gate 
26850Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
26860Sstevel@tonic-gate 	    menu_cmds[COMMENT_CMD], BAM_FTR);
2687662Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
26880Sstevel@tonic-gate 
26890Sstevel@tonic-gate 	return (entryNum);
26900Sstevel@tonic-gate }
26910Sstevel@tonic-gate 
26920Sstevel@tonic-gate static error_t
26930Sstevel@tonic-gate do_delete(menu_t *mp, int entryNum)
26940Sstevel@tonic-gate {
2695662Sszhou 	line_t *lp, *freed;
2696662Sszhou 	entry_t *ent, *tmp;
26970Sstevel@tonic-gate 	int deleted;
26980Sstevel@tonic-gate 
26990Sstevel@tonic-gate 	assert(entryNum != ENTRY_INIT);
27000Sstevel@tonic-gate 
2701662Sszhou 	ent = mp->entries;
2702662Sszhou 	while (ent) {
2703662Sszhou 		lp = ent->start;
2704662Sszhou 		/* check entry number and make sure it's a bootadm entry */
2705662Sszhou 		if (lp->flags != BAM_COMMENT ||
2706662Sszhou 		    strcmp(lp->arg, BAM_HDR) != 0 ||
2707662Sszhou 		    (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) {
2708662Sszhou 			ent = ent->next;
27090Sstevel@tonic-gate 			continue;
27100Sstevel@tonic-gate 		}
27110Sstevel@tonic-gate 
2712662Sszhou 		/* free the entry content */
2713662Sszhou 		do {
2714662Sszhou 			freed = lp;
2715662Sszhou 			lp = lp->next;	/* prev stays the same */
2716662Sszhou 			unlink_line(mp, freed);
2717662Sszhou 			line_free(freed);
2718662Sszhou 		} while (freed != ent->end);
2719662Sszhou 
2720662Sszhou 		/* free the entry_t structure */
2721662Sszhou 		tmp = ent;
2722662Sszhou 		ent = ent->next;
2723662Sszhou 		if (tmp->prev)
2724662Sszhou 			tmp->prev->next = ent;
27250Sstevel@tonic-gate 		else
2726662Sszhou 			mp->entries = ent;
2727662Sszhou 		if (ent)
2728662Sszhou 			ent->prev = tmp->prev;
27290Sstevel@tonic-gate 		deleted = 1;
27300Sstevel@tonic-gate 	}
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate 	if (!deleted && entryNum != ALL_ENTRIES) {
27330Sstevel@tonic-gate 		bam_error(NO_BOOTADM_MATCH);
27340Sstevel@tonic-gate 		return (BAM_ERROR);
27350Sstevel@tonic-gate 	}
27360Sstevel@tonic-gate 
2737621Svikram 	/*
2738621Svikram 	 * Now that we have deleted an entry, update
2739621Svikram 	 * the entry numbering and the default cmd.
2740621Svikram 	 */
2741621Svikram 	update_numbering(mp);
2742621Svikram 
27430Sstevel@tonic-gate 	return (BAM_SUCCESS);
27440Sstevel@tonic-gate }
27450Sstevel@tonic-gate 
27460Sstevel@tonic-gate static error_t
27470Sstevel@tonic-gate delete_all_entries(menu_t *mp, char *menu_path, char *opt)
27480Sstevel@tonic-gate {
27490Sstevel@tonic-gate 	assert(mp);
27500Sstevel@tonic-gate 	assert(opt == NULL);
27510Sstevel@tonic-gate 
27520Sstevel@tonic-gate 	if (mp->start == NULL) {
27530Sstevel@tonic-gate 		bam_print(EMPTY_FILE, menu_path);
27540Sstevel@tonic-gate 		return (BAM_SUCCESS);
27550Sstevel@tonic-gate 	}
27560Sstevel@tonic-gate 
27570Sstevel@tonic-gate 	if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) {
27580Sstevel@tonic-gate 		return (BAM_ERROR);
27590Sstevel@tonic-gate 	}
27600Sstevel@tonic-gate 
27610Sstevel@tonic-gate 	return (BAM_WRITE);
27620Sstevel@tonic-gate }
27630Sstevel@tonic-gate 
27640Sstevel@tonic-gate static FILE *
2765662Sszhou open_diskmap(char *root)
27660Sstevel@tonic-gate {
27670Sstevel@tonic-gate 	FILE *fp;
27680Sstevel@tonic-gate 	char cmd[PATH_MAX];
27690Sstevel@tonic-gate 
27700Sstevel@tonic-gate 	/* make sure we have a map file */
27710Sstevel@tonic-gate 	fp = fopen(GRUBDISK_MAP, "r");
27720Sstevel@tonic-gate 	if (fp == NULL) {
27730Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd),
2774662Sszhou 		    "%s%s > /dev/null", root, CREATE_DISKMAP);
27750Sstevel@tonic-gate 		(void) system(cmd);
27760Sstevel@tonic-gate 		fp = fopen(GRUBDISK_MAP, "r");
27770Sstevel@tonic-gate 	}
27780Sstevel@tonic-gate 	return (fp);
27790Sstevel@tonic-gate }
27800Sstevel@tonic-gate 
27810Sstevel@tonic-gate #define	SECTOR_SIZE	512
27820Sstevel@tonic-gate 
27830Sstevel@tonic-gate static int
27840Sstevel@tonic-gate get_partition(char *device)
27850Sstevel@tonic-gate {
27860Sstevel@tonic-gate 	int i, fd, is_pcfs, partno = -1;
27870Sstevel@tonic-gate 	struct mboot *mboot;
27880Sstevel@tonic-gate 	char boot_sect[SECTOR_SIZE];
27890Sstevel@tonic-gate 	char *wholedisk, *slice;
27900Sstevel@tonic-gate 
27910Sstevel@tonic-gate 	/* form whole disk (p0) */
27920Sstevel@tonic-gate 	slice = device + strlen(device) - 2;
27930Sstevel@tonic-gate 	is_pcfs = (*slice != 's');
27940Sstevel@tonic-gate 	if (!is_pcfs)
27950Sstevel@tonic-gate 		*slice = '\0';
27960Sstevel@tonic-gate 	wholedisk = s_calloc(1, strlen(device) + 3);
27970Sstevel@tonic-gate 	(void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device);
27980Sstevel@tonic-gate 	if (!is_pcfs)
27990Sstevel@tonic-gate 		*slice = 's';
28000Sstevel@tonic-gate 
28010Sstevel@tonic-gate 	/* read boot sector */
28020Sstevel@tonic-gate 	fd = open(wholedisk, O_RDONLY);
28030Sstevel@tonic-gate 	free(wholedisk);
28040Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
28050Sstevel@tonic-gate 		return (partno);
28060Sstevel@tonic-gate 	}
28070Sstevel@tonic-gate 	(void) close(fd);
28080Sstevel@tonic-gate 
28090Sstevel@tonic-gate 	/* parse fdisk table */
28100Sstevel@tonic-gate 	mboot = (struct mboot *)((void *)boot_sect);
28110Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
28120Sstevel@tonic-gate 		struct ipart *part =
28130Sstevel@tonic-gate 		    (struct ipart *)(uintptr_t)mboot->parts + i;
28140Sstevel@tonic-gate 		if (is_pcfs) {	/* looking for solaris boot part */
28150Sstevel@tonic-gate 			if (part->systid == 0xbe) {
28160Sstevel@tonic-gate 				partno = i;
28170Sstevel@tonic-gate 				break;
28180Sstevel@tonic-gate 			}
28190Sstevel@tonic-gate 		} else {	/* look for solaris partition, old and new */
28200Sstevel@tonic-gate 			if (part->systid == SUNIXOS ||
28210Sstevel@tonic-gate 			    part->systid == SUNIXOS2) {
28220Sstevel@tonic-gate 				partno = i;
28230Sstevel@tonic-gate 				break;
28240Sstevel@tonic-gate 			}
28250Sstevel@tonic-gate 		}
28260Sstevel@tonic-gate 	}
28270Sstevel@tonic-gate 	return (partno);
28280Sstevel@tonic-gate }
28290Sstevel@tonic-gate 
28300Sstevel@tonic-gate static char *
28310Sstevel@tonic-gate get_grubdisk(char *rootdev, FILE *fp, int on_bootdev)
28320Sstevel@tonic-gate {
28330Sstevel@tonic-gate 	char *grubdisk;	/* (hd#,#,#) */
28340Sstevel@tonic-gate 	char *slice;
28350Sstevel@tonic-gate 	char *grubhd;
28360Sstevel@tonic-gate 	int fdiskpart;
28370Sstevel@tonic-gate 	int found = 0;
28380Sstevel@tonic-gate 	char *devname, *ctdname = strstr(rootdev, "dsk/");
28390Sstevel@tonic-gate 	char linebuf[PATH_MAX];
28400Sstevel@tonic-gate 
28410Sstevel@tonic-gate 	if (ctdname == NULL)
28420Sstevel@tonic-gate 		return (NULL);
28430Sstevel@tonic-gate 
28440Sstevel@tonic-gate 	ctdname += strlen("dsk/");
28450Sstevel@tonic-gate 	slice = strrchr(ctdname, 's');
28460Sstevel@tonic-gate 	if (slice)
28470Sstevel@tonic-gate 		*slice = '\0';
28480Sstevel@tonic-gate 
28490Sstevel@tonic-gate 	rewind(fp);
28500Sstevel@tonic-gate 	while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) {
28510Sstevel@tonic-gate 		grubhd = strtok(linebuf, " \t\n");
28520Sstevel@tonic-gate 		if (grubhd)
28530Sstevel@tonic-gate 			devname = strtok(NULL, " \t\n");
28540Sstevel@tonic-gate 		else
28550Sstevel@tonic-gate 			devname = NULL;
28560Sstevel@tonic-gate 		if (devname && strcmp(devname, ctdname) == 0) {
28570Sstevel@tonic-gate 			found = 1;
28580Sstevel@tonic-gate 			break;
28590Sstevel@tonic-gate 		}
28600Sstevel@tonic-gate 	}
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate 	if (slice)
28630Sstevel@tonic-gate 		*slice = 's';
28640Sstevel@tonic-gate 
28650Sstevel@tonic-gate 	if (found == 0) {
28660Sstevel@tonic-gate 		if (bam_verbose)
28670Sstevel@tonic-gate 			bam_print(DISKMAP_FAIL_NONFATAL, rootdev);
28680Sstevel@tonic-gate 		grubhd = "0";	/* assume disk 0 if can't match */
28690Sstevel@tonic-gate 	}
28700Sstevel@tonic-gate 
28710Sstevel@tonic-gate 	fdiskpart = get_partition(rootdev);
28720Sstevel@tonic-gate 	if (fdiskpart == -1)
28730Sstevel@tonic-gate 		return (NULL);
28740Sstevel@tonic-gate 
28750Sstevel@tonic-gate 	grubdisk = s_calloc(1, 10);
28760Sstevel@tonic-gate 	if (slice) {
28770Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d,%c)",
28780Sstevel@tonic-gate 		    grubhd, fdiskpart, slice[1] + 'a' - '0');
28790Sstevel@tonic-gate 	} else
28800Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d)",
28810Sstevel@tonic-gate 		    grubhd, fdiskpart);
28820Sstevel@tonic-gate 
28830Sstevel@tonic-gate 	/* if root not on bootdev, change GRUB disk to 0 */
28840Sstevel@tonic-gate 	if (!on_bootdev)
28850Sstevel@tonic-gate 		grubdisk[3] = '0';
28860Sstevel@tonic-gate 	return (grubdisk);
28870Sstevel@tonic-gate }
28880Sstevel@tonic-gate 
2889656Sszhou static char *
2890656Sszhou get_title(char *rootdir)
28910Sstevel@tonic-gate {
28920Sstevel@tonic-gate 	static char title[80];	/* from /etc/release */
2893662Sszhou 	char *cp = NULL, release[PATH_MAX];
28940Sstevel@tonic-gate 	FILE *fp;
28950Sstevel@tonic-gate 
28960Sstevel@tonic-gate 	/* open the /etc/release file */
28970Sstevel@tonic-gate 	(void) snprintf(release, sizeof (release), "%s/etc/release", rootdir);
28980Sstevel@tonic-gate 
28990Sstevel@tonic-gate 	fp = fopen(release, "r");
29000Sstevel@tonic-gate 	if (fp == NULL)
2901656Sszhou 		return (NULL);
29020Sstevel@tonic-gate 
29030Sstevel@tonic-gate 	while (s_fgets(title, sizeof (title), fp) != NULL) {
29040Sstevel@tonic-gate 		cp = strstr(title, "Solaris");
29050Sstevel@tonic-gate 		if (cp)
29060Sstevel@tonic-gate 			break;
29070Sstevel@tonic-gate 	}
29080Sstevel@tonic-gate 	(void) fclose(fp);
2909662Sszhou 	return (cp == NULL ? "Solaris" : cp);
29100Sstevel@tonic-gate }
29110Sstevel@tonic-gate 
29120Sstevel@tonic-gate static char *
29130Sstevel@tonic-gate get_special(char *mountp)
29140Sstevel@tonic-gate {
29150Sstevel@tonic-gate 	FILE *mntfp;
29160Sstevel@tonic-gate 	struct mnttab mp = {0}, mpref = {0};
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate 	mntfp = fopen(MNTTAB, "r");
29190Sstevel@tonic-gate 	if (mntfp == NULL) {
29200Sstevel@tonic-gate 		return (0);
29210Sstevel@tonic-gate 	}
29220Sstevel@tonic-gate 
29230Sstevel@tonic-gate 	if (*mountp == '\0')
29240Sstevel@tonic-gate 		mpref.mnt_mountp = "/";
29250Sstevel@tonic-gate 	else
29260Sstevel@tonic-gate 		mpref.mnt_mountp = mountp;
29270Sstevel@tonic-gate 	if (getmntany(mntfp, &mp, &mpref) != 0) {
29280Sstevel@tonic-gate 		(void) fclose(mntfp);
29290Sstevel@tonic-gate 		return (NULL);
29300Sstevel@tonic-gate 	}
29310Sstevel@tonic-gate 	(void) fclose(mntfp);
29320Sstevel@tonic-gate 
29330Sstevel@tonic-gate 	return (s_strdup(mp.mnt_special));
29340Sstevel@tonic-gate }
29350Sstevel@tonic-gate 
29360Sstevel@tonic-gate static char *
29370Sstevel@tonic-gate os_to_grubdisk(char *osdisk, int on_bootdev)
29380Sstevel@tonic-gate {
29390Sstevel@tonic-gate 	FILE *fp;
29400Sstevel@tonic-gate 	char *grubdisk;
29410Sstevel@tonic-gate 
29420Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
2943662Sszhou 	fp = open_diskmap("");
29440Sstevel@tonic-gate 	if (fp == NULL) {
29450Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdisk);
29460Sstevel@tonic-gate 		return (NULL);
29470Sstevel@tonic-gate 	}
29480Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdisk, fp, on_bootdev);
29490Sstevel@tonic-gate 	(void) fclose(fp);
29500Sstevel@tonic-gate 	return (grubdisk);
29510Sstevel@tonic-gate }
29520Sstevel@tonic-gate 
29530Sstevel@tonic-gate /*
29540Sstevel@tonic-gate  * Check if root is on the boot device
29550Sstevel@tonic-gate  * Return 0 (false) on error
29560Sstevel@tonic-gate  */
29570Sstevel@tonic-gate static int
29580Sstevel@tonic-gate menu_on_bootdev(char *menu_root, FILE *fp)
29590Sstevel@tonic-gate {
29600Sstevel@tonic-gate 	int ret;
29610Sstevel@tonic-gate 	char *grubhd, *bootp, *special;
29620Sstevel@tonic-gate 
29630Sstevel@tonic-gate 	special = get_special(menu_root);
29640Sstevel@tonic-gate 	if (special == NULL)
29650Sstevel@tonic-gate 		return (0);
29660Sstevel@tonic-gate 	bootp = strstr(special, "p0:boot");
29670Sstevel@tonic-gate 	if (bootp)
29680Sstevel@tonic-gate 		*bootp = '\0';
29690Sstevel@tonic-gate 	grubhd = get_grubdisk(special, fp, 1);
29700Sstevel@tonic-gate 	free(special);
29710Sstevel@tonic-gate 
29720Sstevel@tonic-gate 	if (grubhd == NULL)
29730Sstevel@tonic-gate 		return (0);
29740Sstevel@tonic-gate 	ret = grubhd[3] == '0';
29750Sstevel@tonic-gate 	free(grubhd);
29760Sstevel@tonic-gate 	return (ret);
29770Sstevel@tonic-gate }
29780Sstevel@tonic-gate 
2979662Sszhou /*
2980662Sszhou  * look for matching bootadm entry with specified parameters
2981662Sszhou  * Here are the rules (based on existing usage):
2982662Sszhou  * - If title is specified, match on title only
2983662Sszhou  * - Else, match on grubdisk and module (don't care about kernel line).
2984662Sszhou  *   note that, if root_opt is non-zero, the absence of root line is
2985662Sszhou  *   considered a match.
2986662Sszhou  */
2987662Sszhou static entry_t *
2988662Sszhou find_boot_entry(menu_t *mp, char *title, char *root, char *module,
2989662Sszhou     int root_opt, int *entry_num)
2990662Sszhou {
2991662Sszhou 	int i;
2992662Sszhou 	line_t *lp;
2993662Sszhou 	entry_t *ent;
2994662Sszhou 
2995662Sszhou 	/* find matching entry */
2996662Sszhou 	for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) {
2997662Sszhou 		lp = ent->start;
2998662Sszhou 
2999662Sszhou 		/* first line of entry must be bootadm comment */
3000662Sszhou 		lp = ent->start;
3001662Sszhou 		if (lp->flags != BAM_COMMENT || strcmp(lp->arg, BAM_HDR) != 0) {
3002662Sszhou 			continue;
3003662Sszhou 		}
3004662Sszhou 
3005662Sszhou 		/* advance to title line */
3006662Sszhou 		lp = lp->next;
3007662Sszhou 		if (title) {
3008662Sszhou 			if (lp->flags == BAM_TITLE && lp->arg &&
3009662Sszhou 			    strcmp(lp->arg, title) == 0)
3010662Sszhou 				break;
3011662Sszhou 			continue;	/* check title only */
3012662Sszhou 		}
3013662Sszhou 
3014662Sszhou 		lp = lp->next;	/* advance to root line */
3015662Sszhou 		if (lp == NULL || strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
3016662Sszhou 			/* root command found, match grub disk */
3017662Sszhou 			if (strcmp(lp->arg, root) != 0) {
3018662Sszhou 				continue;
3019662Sszhou 			}
3020662Sszhou 			lp = lp->next;	/* advance to kernel line */
3021662Sszhou 		} else {
3022662Sszhou 			/* no root command, see if root is optional */
3023662Sszhou 			if (root_opt == 0) {
3024662Sszhou 				continue;
3025662Sszhou 			}
3026662Sszhou 		}
3027662Sszhou 
3028662Sszhou 		if (lp == NULL || lp->next == NULL) {
3029662Sszhou 			continue;
3030662Sszhou 		}
3031662Sszhou 
3032662Sszhou 		/* check for matching module entry (failsafe or normal) */
3033662Sszhou 		lp = lp->next;	/* advance to module line */
3034662Sszhou 		if (strcmp(lp->cmd, menu_cmds[MODULE_CMD]) != 0 ||
3035662Sszhou 		    strcmp(lp->arg, module) != 0) {
3036662Sszhou 			continue;
3037662Sszhou 		}
3038662Sszhou 		break;	/* match found */
3039662Sszhou 	}
3040662Sszhou 
3041662Sszhou 	*entry_num = i;
3042662Sszhou 	return (ent);
3043662Sszhou }
3044662Sszhou 
3045662Sszhou static int
3046662Sszhou update_boot_entry(menu_t *mp, char *title, char *root, char *kernel,
3047662Sszhou     char *module, int root_opt)
3048662Sszhou {
3049662Sszhou 	int i;
3050662Sszhou 	entry_t *ent;
3051662Sszhou 	line_t *lp;
3052662Sszhou 	char linebuf[BAM_MAXLINE];
3053662Sszhou 
3054662Sszhou 	/* note: don't match on title, it's updated on upgrade */
3055662Sszhou 	ent = find_boot_entry(mp, NULL, root, module, root_opt, &i);
3056662Sszhou 	if (ent == NULL)
3057662Sszhou 		return (add_boot_entry(mp, title, root_opt ? NULL : root,
3058662Sszhou 		    kernel, module));
3059662Sszhou 
3060662Sszhou 	/* replace title of exiting entry and delete root line */
3061662Sszhou 	lp = ent->start;
3062662Sszhou 	lp = lp->next;	/* title line */
3063662Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
3064662Sszhou 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
3065662Sszhou 	free(lp->arg);
3066662Sszhou 	free(lp->line);
3067662Sszhou 	lp->arg = s_strdup(title);
3068662Sszhou 	lp->line = s_strdup(linebuf);
3069662Sszhou 
3070662Sszhou 	lp = lp->next;	/* root line */
3071662Sszhou 	if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
3072662Sszhou 		if (root_opt) {		/* root line not needed */
3073662Sszhou 			line_t *tmp = lp;
3074662Sszhou 			lp = lp->next;
3075662Sszhou 			unlink_line(mp, tmp);
3076662Sszhou 			line_free(tmp);
3077662Sszhou 		} else
3078662Sszhou 			lp = lp->next;
3079662Sszhou 	}
3080662Sszhou 	return (i);
3081662Sszhou }
3082662Sszhou 
30830Sstevel@tonic-gate /*ARGSUSED*/
30840Sstevel@tonic-gate static error_t
30850Sstevel@tonic-gate update_entry(menu_t *mp, char *menu_root, char *opt)
30860Sstevel@tonic-gate {
30870Sstevel@tonic-gate 	FILE *fp;
30880Sstevel@tonic-gate 	int entry;
30890Sstevel@tonic-gate 	char *grubdisk, *title, *osdev, *osroot;
3090662Sszhou 	struct stat sbuf;
3091662Sszhou 	char failsafe[256];
30920Sstevel@tonic-gate 
30930Sstevel@tonic-gate 	assert(mp);
30940Sstevel@tonic-gate 	assert(opt);
30950Sstevel@tonic-gate 
30960Sstevel@tonic-gate 	osdev = strtok(opt, ",");
30970Sstevel@tonic-gate 	osroot = strtok(NULL, ",");
30980Sstevel@tonic-gate 	if (osroot == NULL)
30990Sstevel@tonic-gate 		osroot = menu_root;
31000Sstevel@tonic-gate 	title = get_title(osroot);
31010Sstevel@tonic-gate 
31020Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
3103662Sszhou 	fp = open_diskmap(osroot);
31040Sstevel@tonic-gate 	if (fp == NULL) {
31050Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
31060Sstevel@tonic-gate 		return (BAM_ERROR);
31070Sstevel@tonic-gate 	}
31080Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdev, fp, menu_on_bootdev(menu_root, fp));
31090Sstevel@tonic-gate 	(void) fclose(fp);
31100Sstevel@tonic-gate 	if (grubdisk == NULL) {
31110Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
31120Sstevel@tonic-gate 		return (BAM_ERROR);
31130Sstevel@tonic-gate 	}
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate 	/* add the entry for normal Solaris */
3116662Sszhou 	entry = update_boot_entry(mp, title, grubdisk,
31170Sstevel@tonic-gate 	    "/platform/i86pc/multiboot",
3118662Sszhou 	    "/platform/i86pc/boot_archive",
3119662Sszhou 	    osroot == menu_root);
31200Sstevel@tonic-gate 
31210Sstevel@tonic-gate 	/* add the entry for failsafe archive */
3122662Sszhou 	(void) snprintf(failsafe, sizeof (failsafe),
3123662Sszhou 	    "%s/boot/x86.miniroot-safe", osroot);
3124662Sszhou 	if (stat(failsafe, &sbuf) == 0)
3125662Sszhou 		(void) update_boot_entry(mp, "Solaris failsafe", grubdisk,
3126662Sszhou 		    "/boot/multiboot kernel/unix -s",
3127662Sszhou 		    "/boot/x86.miniroot-safe",
3128662Sszhou 		    osroot == menu_root);
31290Sstevel@tonic-gate 	free(grubdisk);
31300Sstevel@tonic-gate 
31310Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
31320Sstevel@tonic-gate 		return (BAM_ERROR);
31330Sstevel@tonic-gate 	}
31340Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
31350Sstevel@tonic-gate 	return (BAM_WRITE);
31360Sstevel@tonic-gate }
31370Sstevel@tonic-gate 
3138316Svikram static char *
3139316Svikram read_grub_root(void)
3140316Svikram {
3141316Svikram 	FILE *fp;
3142316Svikram 	struct stat sb;
3143316Svikram 	char buf[BAM_MAXLINE];
3144316Svikram 	char *rootstr;
3145316Svikram 
3146316Svikram 	if (stat(GRUB_slice, &sb) != 0) {
3147316Svikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
3148316Svikram 		return (NULL);
3149316Svikram 	}
3150316Svikram 
3151316Svikram 	if (stat(GRUB_root, &sb) != 0) {
3152316Svikram 		bam_error(MISSING_ROOT_FILE, GRUB_root, strerror(errno));
3153316Svikram 		return (NULL);
3154316Svikram 	}
3155316Svikram 
3156316Svikram 	fp = fopen(GRUB_root, "r");
3157316Svikram 	if (fp == NULL) {
3158316Svikram 		bam_error(OPEN_FAIL, GRUB_root, strerror(errno));
3159316Svikram 		return (NULL);
3160316Svikram 	}
3161316Svikram 
3162316Svikram 	if (s_fgets(buf, sizeof (buf), fp) == NULL) {
3163316Svikram 		bam_error(EMPTY_FILE, GRUB_root, strerror(errno));
3164316Svikram 		(void) fclose(fp);
3165316Svikram 		return (NULL);
3166316Svikram 	}
3167316Svikram 
3168316Svikram 	/*
3169316Svikram 	 * Copy buf here as check below may trash the buffer
3170316Svikram 	 */
3171316Svikram 	rootstr = s_strdup(buf);
3172316Svikram 
3173316Svikram 	if (s_fgets(buf, sizeof (buf), fp) != NULL) {
3174316Svikram 		bam_error(BAD_ROOT_FILE, GRUB_root);
3175316Svikram 		free(rootstr);
3176316Svikram 		rootstr = NULL;
3177316Svikram 	}
3178316Svikram 
3179316Svikram 	(void) fclose(fp);
3180316Svikram 
3181316Svikram 	return (rootstr);
3182316Svikram }
3183316Svikram 
3184662Sszhou static void
3185662Sszhou save_default_entry(menu_t *mp)
3186662Sszhou {
3187662Sszhou 	int lineNum, entryNum;
3188662Sszhou 	int entry = 0;	/* default is 0 */
3189662Sszhou 	char linebuf[BAM_MAXLINE];
3190662Sszhou 	line_t *lp = mp->curdefault;
3191662Sszhou 
3192662Sszhou 	if (lp)
3193662Sszhou 		entry = s_strtol(lp->arg);
3194662Sszhou 
3195662Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "#%s%d", BAM_OLDDEF, entry);
3196662Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
3197662Sszhou }
3198662Sszhou 
3199662Sszhou static void
3200662Sszhou restore_default_entry(menu_t *mp)
3201662Sszhou {
3202662Sszhou 	int entry;
3203662Sszhou 	char *str;
3204662Sszhou 	line_t *lp = mp->olddefault;
3205662Sszhou 
3206662Sszhou 	if (lp == NULL)
3207662Sszhou 		return;		/* nothing to restore */
3208662Sszhou 
3209662Sszhou 	str = lp->arg + strlen(BAM_OLDDEF);
3210662Sszhou 	entry = s_strtol(str);
3211662Sszhou 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
3212662Sszhou 
3213662Sszhou 	/* delete saved old default line */
3214662Sszhou 	mp->olddefault = NULL;
3215662Sszhou 	unlink_line(mp, lp);
3216662Sszhou 	line_free(lp);
3217662Sszhou }
3218662Sszhou 
32190Sstevel@tonic-gate /*
32200Sstevel@tonic-gate  * This function is for supporting reboot with args.
32210Sstevel@tonic-gate  * The opt value can be:
32220Sstevel@tonic-gate  * NULL		delete temp entry, if present
32230Sstevel@tonic-gate  * entry=#	switches default entry to 1
32240Sstevel@tonic-gate  * else		treated as boot-args and setup a temperary menu entry
32250Sstevel@tonic-gate  *		and make it the default
32260Sstevel@tonic-gate  */
32270Sstevel@tonic-gate #define	REBOOT_TITLE	"Solaris_reboot_transient"
32280Sstevel@tonic-gate 
3229662Sszhou /*ARGSUSED*/
32300Sstevel@tonic-gate static error_t
32310Sstevel@tonic-gate update_temp(menu_t *mp, char *menupath, char *opt)
32320Sstevel@tonic-gate {
32330Sstevel@tonic-gate 	int entry;
32340Sstevel@tonic-gate 	char *grubdisk, *rootdev;
32350Sstevel@tonic-gate 	char kernbuf[1024];
3236316Svikram 	struct stat sb;
32370Sstevel@tonic-gate 
32380Sstevel@tonic-gate 	assert(mp);
32390Sstevel@tonic-gate 
3240662Sszhou 	/* If no option, delete exiting reboot menu entry */
3241662Sszhou 	if (opt == NULL) {
3242662Sszhou 		entry_t *ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL,
3243662Sszhou 		    0, &entry);
3244662Sszhou 		if (ent == NULL)	/* not found is ok */
3245662Sszhou 			return (BAM_SUCCESS);
3246662Sszhou 		(void) do_delete(mp, entry);
3247662Sszhou 		restore_default_entry(mp);
3248662Sszhou 		return (BAM_WRITE);
3249662Sszhou 	}
3250662Sszhou 
3251662Sszhou 	/* if entry= is specified, set the default entry */
3252662Sszhou 	if (strncmp(opt, "entry=", strlen("entry=")) == 0 &&
32530Sstevel@tonic-gate 	    selector(mp, opt, &entry, NULL) == BAM_SUCCESS) {
32540Sstevel@tonic-gate 		/* this is entry=# option */
32550Sstevel@tonic-gate 		return (set_global(mp, menu_cmds[DEFAULT_CMD], entry));
32560Sstevel@tonic-gate 	}
32570Sstevel@tonic-gate 
32580Sstevel@tonic-gate 	/*
32590Sstevel@tonic-gate 	 * add a new menu entry base on opt and make it the default
32600Sstevel@tonic-gate 	 */
3261316Svikram 	grubdisk = NULL;
3262316Svikram 	if (stat(GRUB_slice, &sb) != 0) {
3263316Svikram 		/*
3264316Svikram 		 * 1. First get root disk name from mnttab
3265316Svikram 		 * 2. Translate disk name to grub name
3266316Svikram 		 * 3. Add the new menu entry
3267316Svikram 		 */
3268316Svikram 		rootdev = get_special("/");
3269316Svikram 		if (rootdev) {
3270316Svikram 			grubdisk = os_to_grubdisk(rootdev, 1);
3271316Svikram 			free(rootdev);
3272316Svikram 		}
3273316Svikram 	} else {
3274316Svikram 		/*
3275316Svikram 		 * This is an LU BE. The GRUB_root file
3276316Svikram 		 * contains entry for GRUB's "root" cmd.
3277316Svikram 		 */
3278316Svikram 		grubdisk = read_grub_root();
32790Sstevel@tonic-gate 	}
32800Sstevel@tonic-gate 	if (grubdisk == NULL) {
3281316Svikram 		bam_error(REBOOT_WITH_ARGS_FAILED);
32820Sstevel@tonic-gate 		return (BAM_ERROR);
32830Sstevel@tonic-gate 	}
32840Sstevel@tonic-gate 
32850Sstevel@tonic-gate 	/* add an entry for Solaris reboot */
32860Sstevel@tonic-gate 	(void) snprintf(kernbuf, sizeof (kernbuf),
32870Sstevel@tonic-gate 	    "/platform/i86pc/multiboot %s", opt);
32880Sstevel@tonic-gate 	entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf,
32890Sstevel@tonic-gate 	    "/platform/i86pc/boot_archive");
32900Sstevel@tonic-gate 	free(grubdisk);
32910Sstevel@tonic-gate 
32920Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
3293316Svikram 		bam_error(REBOOT_WITH_ARGS_FAILED);
32940Sstevel@tonic-gate 		return (BAM_ERROR);
32950Sstevel@tonic-gate 	}
3296662Sszhou 
3297662Sszhou 	save_default_entry(mp);
32980Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
32990Sstevel@tonic-gate 	return (BAM_WRITE);
33000Sstevel@tonic-gate }
33010Sstevel@tonic-gate 
33020Sstevel@tonic-gate static error_t
33030Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val)
33040Sstevel@tonic-gate {
33050Sstevel@tonic-gate 	line_t *lp, *found, *last;
33060Sstevel@tonic-gate 	char *cp, *str;
33070Sstevel@tonic-gate 	char prefix[BAM_MAXLINE];
33080Sstevel@tonic-gate 	size_t len;
33090Sstevel@tonic-gate 
33100Sstevel@tonic-gate 	assert(mp);
33110Sstevel@tonic-gate 	assert(globalcmd);
33120Sstevel@tonic-gate 
3313316Svikram 	if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) {
3314316Svikram 		if (val < 0 || mp->end == NULL || val > mp->end->entryNum) {
3315316Svikram 			(void) snprintf(prefix, sizeof (prefix), "%d", val);
3316316Svikram 			bam_error(INVALID_ENTRY, prefix);
3317316Svikram 			return (BAM_ERROR);
3318316Svikram 		}
3319316Svikram 	}
3320316Svikram 
33210Sstevel@tonic-gate 	found = last = NULL;
33220Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
33230Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
33240Sstevel@tonic-gate 			continue;
33250Sstevel@tonic-gate 
33260Sstevel@tonic-gate 		last = lp; /* track the last global found */
33270Sstevel@tonic-gate 
33280Sstevel@tonic-gate 		if (lp->cmd == NULL) {
33290Sstevel@tonic-gate 			bam_error(NO_CMD, lp->lineNum);
33300Sstevel@tonic-gate 			continue;
33310Sstevel@tonic-gate 		}
33320Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
33330Sstevel@tonic-gate 			continue;
33340Sstevel@tonic-gate 
33350Sstevel@tonic-gate 		if (found) {
33360Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
33370Sstevel@tonic-gate 		}
33380Sstevel@tonic-gate 		found = lp;
33390Sstevel@tonic-gate 	}
33400Sstevel@tonic-gate 
33410Sstevel@tonic-gate 	if (found == NULL) {
33420Sstevel@tonic-gate 		lp = s_calloc(1, sizeof (line_t));
33430Sstevel@tonic-gate 		if (last == NULL) {
33440Sstevel@tonic-gate 			lp->next = mp->start;
33450Sstevel@tonic-gate 			mp->start = lp;
33460Sstevel@tonic-gate 			mp->end = (mp->end) ? mp->end : lp;
33470Sstevel@tonic-gate 		} else {
33480Sstevel@tonic-gate 			lp->next = last->next;
33490Sstevel@tonic-gate 			last->next = lp;
33500Sstevel@tonic-gate 			if (lp->next == NULL)
33510Sstevel@tonic-gate 				mp->end = lp;
33520Sstevel@tonic-gate 		}
33530Sstevel@tonic-gate 		lp->flags = BAM_GLOBAL; /* other fields not needed for writes */
33540Sstevel@tonic-gate 		len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]);
33550Sstevel@tonic-gate 		len += 10;	/* val < 10 digits */
33560Sstevel@tonic-gate 		lp->line = s_calloc(1, len);
33570Sstevel@tonic-gate 		(void) snprintf(lp->line, len, "%s%s%d",
33580Sstevel@tonic-gate 		    globalcmd, menu_cmds[SEP_CMD], val);
33590Sstevel@tonic-gate 		return (BAM_WRITE);
33600Sstevel@tonic-gate 	}
33610Sstevel@tonic-gate 
33620Sstevel@tonic-gate 	/*
33630Sstevel@tonic-gate 	 * We are changing an existing entry. Retain any prefix whitespace,
33640Sstevel@tonic-gate 	 * but overwrite everything else. This preserves tabs added for
33650Sstevel@tonic-gate 	 * readability.
33660Sstevel@tonic-gate 	 */
33670Sstevel@tonic-gate 	str = found->line;
33680Sstevel@tonic-gate 	cp = prefix;
33690Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
33700Sstevel@tonic-gate 		*(cp++) = *(str++);
33710Sstevel@tonic-gate 	*cp = '\0'; /* Terminate prefix */
33720Sstevel@tonic-gate 	len = strlen(prefix) + strlen(globalcmd);
33730Sstevel@tonic-gate 	len += strlen(menu_cmds[SEP_CMD]) + 10;
33740Sstevel@tonic-gate 
33750Sstevel@tonic-gate 	free(found->line);
33760Sstevel@tonic-gate 	found->line = s_calloc(1, len);
33770Sstevel@tonic-gate 	(void) snprintf(found->line, len,
33780Sstevel@tonic-gate 		"%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val);
33790Sstevel@tonic-gate 
33800Sstevel@tonic-gate 	return (BAM_WRITE); /* need a write to menu */
33810Sstevel@tonic-gate }
33820Sstevel@tonic-gate 
33830Sstevel@tonic-gate /*ARGSUSED*/
33840Sstevel@tonic-gate static error_t
33850Sstevel@tonic-gate set_option(menu_t *mp, char *menu_path, char *opt)
33860Sstevel@tonic-gate {
33870Sstevel@tonic-gate 	int optnum, optval;
33880Sstevel@tonic-gate 	char *val;
33890Sstevel@tonic-gate 
33900Sstevel@tonic-gate 	assert(mp);
33910Sstevel@tonic-gate 	assert(opt);
33920Sstevel@tonic-gate 
33930Sstevel@tonic-gate 	val = strchr(opt, '=');
33940Sstevel@tonic-gate 	if (val == NULL) {
33950Sstevel@tonic-gate 		bam_error(INVALID_ENTRY, opt);
33960Sstevel@tonic-gate 		return (BAM_ERROR);
33970Sstevel@tonic-gate 	}
33980Sstevel@tonic-gate 
33990Sstevel@tonic-gate 	*val = '\0';
34000Sstevel@tonic-gate 	if (strcmp(opt, "default") == 0) {
34010Sstevel@tonic-gate 		optnum = DEFAULT_CMD;
34020Sstevel@tonic-gate 	} else if (strcmp(opt, "timeout") == 0) {
34030Sstevel@tonic-gate 		optnum = TIMEOUT_CMD;
34040Sstevel@tonic-gate 	} else {
34050Sstevel@tonic-gate 		bam_error(INVALID_ENTRY, opt);
34060Sstevel@tonic-gate 		return (BAM_ERROR);
34070Sstevel@tonic-gate 	}
34080Sstevel@tonic-gate 
34090Sstevel@tonic-gate 	optval = s_strtol(val + 1);
34100Sstevel@tonic-gate 	*val = '=';
34110Sstevel@tonic-gate 	return (set_global(mp, menu_cmds[optnum], optval));
34120Sstevel@tonic-gate }
34130Sstevel@tonic-gate 
34140Sstevel@tonic-gate /*
34150Sstevel@tonic-gate  * The quiet argument suppresses messages. This is used
34160Sstevel@tonic-gate  * when invoked in the context of other commands (e.g. list_entry)
34170Sstevel@tonic-gate  */
34180Sstevel@tonic-gate static error_t
34190Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet)
34200Sstevel@tonic-gate {
34210Sstevel@tonic-gate 	line_t *lp;
34220Sstevel@tonic-gate 	char *arg;
34230Sstevel@tonic-gate 	int done, ret = BAM_SUCCESS;
34240Sstevel@tonic-gate 
34250Sstevel@tonic-gate 	assert(mp);
34260Sstevel@tonic-gate 	assert(menu_path);
34270Sstevel@tonic-gate 	assert(globalcmd);
34280Sstevel@tonic-gate 
34290Sstevel@tonic-gate 	if (mp->start == NULL) {
34300Sstevel@tonic-gate 		if (!quiet)
34310Sstevel@tonic-gate 			bam_error(NO_MENU, menu_path);
34320Sstevel@tonic-gate 		return (BAM_ERROR);
34330Sstevel@tonic-gate 	}
34340Sstevel@tonic-gate 
34350Sstevel@tonic-gate 	done = 0;
34360Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
34370Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
34380Sstevel@tonic-gate 			continue;
34390Sstevel@tonic-gate 
34400Sstevel@tonic-gate 		if (lp->cmd == NULL) {
34410Sstevel@tonic-gate 			if (!quiet)
34420Sstevel@tonic-gate 				bam_error(NO_CMD, lp->lineNum);
34430Sstevel@tonic-gate 			continue;
34440Sstevel@tonic-gate 		}
34450Sstevel@tonic-gate 
34460Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
34470Sstevel@tonic-gate 			continue;
34480Sstevel@tonic-gate 
34490Sstevel@tonic-gate 		/* Found global. Check for duplicates */
34500Sstevel@tonic-gate 		if (done && !quiet) {
34510Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
34520Sstevel@tonic-gate 			ret = BAM_ERROR;
34530Sstevel@tonic-gate 		}
34540Sstevel@tonic-gate 
34550Sstevel@tonic-gate 		arg = lp->arg ? lp->arg : "";
34560Sstevel@tonic-gate 		bam_print(GLOBAL_CMD, globalcmd, arg);
34570Sstevel@tonic-gate 		done = 1;
34580Sstevel@tonic-gate 	}
34590Sstevel@tonic-gate 
34600Sstevel@tonic-gate 	if (!done && bam_verbose)
34610Sstevel@tonic-gate 		bam_print(NO_ENTRY, globalcmd);
34620Sstevel@tonic-gate 
34630Sstevel@tonic-gate 	return (ret);
34640Sstevel@tonic-gate }
34650Sstevel@tonic-gate 
34660Sstevel@tonic-gate static error_t
34670Sstevel@tonic-gate menu_write(char *root, menu_t *mp)
34680Sstevel@tonic-gate {
34690Sstevel@tonic-gate 	return (list2file(root, MENU_TMP, GRUB_MENU, mp->start));
34700Sstevel@tonic-gate }
34710Sstevel@tonic-gate 
34720Sstevel@tonic-gate static void
34730Sstevel@tonic-gate line_free(line_t *lp)
34740Sstevel@tonic-gate {
34750Sstevel@tonic-gate 	if (lp == NULL)
34760Sstevel@tonic-gate 		return;
34770Sstevel@tonic-gate 
34780Sstevel@tonic-gate 	if (lp->cmd)
34790Sstevel@tonic-gate 		free(lp->cmd);
34800Sstevel@tonic-gate 	if (lp->sep)
34810Sstevel@tonic-gate 		free(lp->sep);
34820Sstevel@tonic-gate 	if (lp->arg)
34830Sstevel@tonic-gate 		free(lp->arg);
34840Sstevel@tonic-gate 	if (lp->line)
34850Sstevel@tonic-gate 		free(lp->line);
34860Sstevel@tonic-gate 	free(lp);
34870Sstevel@tonic-gate }
34880Sstevel@tonic-gate 
34890Sstevel@tonic-gate static void
34900Sstevel@tonic-gate linelist_free(line_t *start)
34910Sstevel@tonic-gate {
34920Sstevel@tonic-gate 	line_t *lp;
34930Sstevel@tonic-gate 
34940Sstevel@tonic-gate 	while (start) {
34950Sstevel@tonic-gate 		lp = start;
34960Sstevel@tonic-gate 		start = start->next;
34970Sstevel@tonic-gate 		line_free(lp);
34980Sstevel@tonic-gate 	}
34990Sstevel@tonic-gate }
35000Sstevel@tonic-gate 
35010Sstevel@tonic-gate static void
35020Sstevel@tonic-gate filelist_free(filelist_t *flistp)
35030Sstevel@tonic-gate {
35040Sstevel@tonic-gate 	linelist_free(flistp->head);
35050Sstevel@tonic-gate 	flistp->head = NULL;
35060Sstevel@tonic-gate 	flistp->tail = NULL;
35070Sstevel@tonic-gate }
35080Sstevel@tonic-gate 
35090Sstevel@tonic-gate static void
35100Sstevel@tonic-gate menu_free(menu_t *mp)
35110Sstevel@tonic-gate {
3512662Sszhou 	entry_t *ent, *tmp;
35130Sstevel@tonic-gate 	assert(mp);
35140Sstevel@tonic-gate 
35150Sstevel@tonic-gate 	if (mp->start)
35160Sstevel@tonic-gate 		linelist_free(mp->start);
3517662Sszhou 	ent = mp->entries;
3518662Sszhou 	while (ent) {
3519662Sszhou 		tmp = ent;
3520662Sszhou 		ent = tmp->next;
3521662Sszhou 		free(tmp);
3522662Sszhou 	}
3523662Sszhou 
35240Sstevel@tonic-gate 	free(mp);
35250Sstevel@tonic-gate }
35260Sstevel@tonic-gate 
35270Sstevel@tonic-gate /*
35280Sstevel@tonic-gate  * Utility routines
35290Sstevel@tonic-gate  */
35300Sstevel@tonic-gate 
35310Sstevel@tonic-gate 
35320Sstevel@tonic-gate /*
35330Sstevel@tonic-gate  * Returns 0 on success
35340Sstevel@tonic-gate  * Any other value indicates an error
35350Sstevel@tonic-gate  */
35360Sstevel@tonic-gate static int
35370Sstevel@tonic-gate exec_cmd(char *cmdline, char *output, int64_t osize)
35380Sstevel@tonic-gate {
35390Sstevel@tonic-gate 	char buf[BUFSIZ];
35400Sstevel@tonic-gate 	int ret;
35410Sstevel@tonic-gate 	FILE *ptr;
35420Sstevel@tonic-gate 	size_t len;
35430Sstevel@tonic-gate 	sigset_t set;
35440Sstevel@tonic-gate 	void (*disp)(int);
35450Sstevel@tonic-gate 
35460Sstevel@tonic-gate 	/*
35470Sstevel@tonic-gate 	 * For security
35480Sstevel@tonic-gate 	 * - only absolute paths are allowed
35490Sstevel@tonic-gate 	 * - set IFS to space and tab
35500Sstevel@tonic-gate 	 */
35510Sstevel@tonic-gate 	if (*cmdline != '/') {
35520Sstevel@tonic-gate 		bam_error(ABS_PATH_REQ, cmdline);
35530Sstevel@tonic-gate 		return (-1);
35540Sstevel@tonic-gate 	}
35550Sstevel@tonic-gate 	(void) putenv("IFS= \t");
35560Sstevel@tonic-gate 
35570Sstevel@tonic-gate 	/*
35580Sstevel@tonic-gate 	 * We may have been exec'ed with SIGCHLD blocked
35590Sstevel@tonic-gate 	 * unblock it here
35600Sstevel@tonic-gate 	 */
35610Sstevel@tonic-gate 	(void) sigemptyset(&set);
35620Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
35630Sstevel@tonic-gate 	if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
35640Sstevel@tonic-gate 		bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno));
35650Sstevel@tonic-gate 		return (-1);
35660Sstevel@tonic-gate 	}
35670Sstevel@tonic-gate 
35680Sstevel@tonic-gate 	/*
35690Sstevel@tonic-gate 	 * Set SIGCHLD disposition to SIG_DFL for popen/pclose
35700Sstevel@tonic-gate 	 */
35710Sstevel@tonic-gate 	disp = sigset(SIGCHLD, SIG_DFL);
35720Sstevel@tonic-gate 	if (disp == SIG_ERR) {
35730Sstevel@tonic-gate 		bam_error(FAILED_SIG, strerror(errno));
35740Sstevel@tonic-gate 		return (-1);
35750Sstevel@tonic-gate 	}
35760Sstevel@tonic-gate 	if (disp == SIG_HOLD) {
35770Sstevel@tonic-gate 		bam_error(BLOCKED_SIG, cmdline);
35780Sstevel@tonic-gate 		return (-1);
35790Sstevel@tonic-gate 	}
35800Sstevel@tonic-gate 
35810Sstevel@tonic-gate 	ptr = popen(cmdline, "r");
35820Sstevel@tonic-gate 	if (ptr == NULL) {
35830Sstevel@tonic-gate 		bam_error(POPEN_FAIL, cmdline, strerror(errno));
35840Sstevel@tonic-gate 		return (-1);
35850Sstevel@tonic-gate 	}
35860Sstevel@tonic-gate 
35870Sstevel@tonic-gate 	/*
35880Sstevel@tonic-gate 	 * If we simply do a pclose() following a popen(), pclose()
35890Sstevel@tonic-gate 	 * will close the reader end of the pipe immediately even
35900Sstevel@tonic-gate 	 * if the child process has not started/exited. pclose()
35910Sstevel@tonic-gate 	 * does wait for cmd to terminate before returning though.
35920Sstevel@tonic-gate 	 * When the executed command writes its output to the pipe
35930Sstevel@tonic-gate 	 * there is no reader process and the command dies with
35940Sstevel@tonic-gate 	 * SIGPIPE. To avoid this we read repeatedly until read
35950Sstevel@tonic-gate 	 * terminates with EOF. This indicates that the command
35960Sstevel@tonic-gate 	 * (writer) has closed the pipe and we can safely do a
35970Sstevel@tonic-gate 	 * pclose().
35980Sstevel@tonic-gate 	 *
35990Sstevel@tonic-gate 	 * Since pclose() does wait for the command to exit,
36000Sstevel@tonic-gate 	 * we can safely reap the exit status of the command
36010Sstevel@tonic-gate 	 * from the value returned by pclose()
36020Sstevel@tonic-gate 	 */
36030Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), ptr) != NULL) {
36040Sstevel@tonic-gate 		/* if (bam_verbose)  XXX */
36050Sstevel@tonic-gate 			bam_print(PRINT_NO_NEWLINE, buf);
36060Sstevel@tonic-gate 		if (output && osize > 0) {
36070Sstevel@tonic-gate 			(void) snprintf(output, osize, "%s", buf);
36080Sstevel@tonic-gate 			len = strlen(buf);
36090Sstevel@tonic-gate 			output += len;
36100Sstevel@tonic-gate 			osize -= len;
36110Sstevel@tonic-gate 		}
36120Sstevel@tonic-gate 	}
36130Sstevel@tonic-gate 
36140Sstevel@tonic-gate 	ret = pclose(ptr);
36150Sstevel@tonic-gate 	if (ret == -1) {
36160Sstevel@tonic-gate 		bam_error(PCLOSE_FAIL, cmdline, strerror(errno));
36170Sstevel@tonic-gate 		return (-1);
36180Sstevel@tonic-gate 	}
36190Sstevel@tonic-gate 
36200Sstevel@tonic-gate 	if (WIFEXITED(ret)) {
36210Sstevel@tonic-gate 		return (WEXITSTATUS(ret));
36220Sstevel@tonic-gate 	} else {
36230Sstevel@tonic-gate 		bam_error(EXEC_FAIL, cmdline, ret);
36240Sstevel@tonic-gate 		return (-1);
36250Sstevel@tonic-gate 	}
36260Sstevel@tonic-gate }
36270Sstevel@tonic-gate 
36280Sstevel@tonic-gate /*
36290Sstevel@tonic-gate  * Since this function returns -1 on error
36300Sstevel@tonic-gate  * it cannot be used to convert -1. However,
36310Sstevel@tonic-gate  * that is sufficient for what we need.
36320Sstevel@tonic-gate  */
36330Sstevel@tonic-gate static long
36340Sstevel@tonic-gate s_strtol(char *str)
36350Sstevel@tonic-gate {
36360Sstevel@tonic-gate 	long l;
36370Sstevel@tonic-gate 	char *res = NULL;
36380Sstevel@tonic-gate 
36390Sstevel@tonic-gate 	if (str == NULL) {
36400Sstevel@tonic-gate 		return (-1);
36410Sstevel@tonic-gate 	}
36420Sstevel@tonic-gate 
36430Sstevel@tonic-gate 	errno = 0;
36440Sstevel@tonic-gate 	l = strtol(str, &res, 10);
36450Sstevel@tonic-gate 	if (errno || *res != '\0') {
36460Sstevel@tonic-gate 		return (-1);
36470Sstevel@tonic-gate 	}
36480Sstevel@tonic-gate 
36490Sstevel@tonic-gate 	return (l);
36500Sstevel@tonic-gate }
36510Sstevel@tonic-gate 
36520Sstevel@tonic-gate /*
36530Sstevel@tonic-gate  * Wrapper around fputs, that adds a newline (since fputs doesn't)
36540Sstevel@tonic-gate  */
36550Sstevel@tonic-gate static int
36560Sstevel@tonic-gate s_fputs(char *str, FILE *fp)
36570Sstevel@tonic-gate {
36580Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
36590Sstevel@tonic-gate 
36600Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s\n", str);
36610Sstevel@tonic-gate 	return (fputs(linebuf, fp));
36620Sstevel@tonic-gate }
36630Sstevel@tonic-gate 
36640Sstevel@tonic-gate /*
36650Sstevel@tonic-gate  * Wrapper around fgets, that strips newlines returned by fgets
36660Sstevel@tonic-gate  */
36670Sstevel@tonic-gate static char *
36680Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp)
36690Sstevel@tonic-gate {
36700Sstevel@tonic-gate 	int n;
36710Sstevel@tonic-gate 
36720Sstevel@tonic-gate 	buf = fgets(buf, buflen, fp);
36730Sstevel@tonic-gate 	if (buf) {
36740Sstevel@tonic-gate 		n = strlen(buf);
36750Sstevel@tonic-gate 		if (n == buflen - 1 && buf[n-1] != '\n')
36760Sstevel@tonic-gate 			bam_error(TOO_LONG, buflen - 1, buf);
36770Sstevel@tonic-gate 		buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1];
36780Sstevel@tonic-gate 	}
36790Sstevel@tonic-gate 
36800Sstevel@tonic-gate 	return (buf);
36810Sstevel@tonic-gate }
36820Sstevel@tonic-gate 
36830Sstevel@tonic-gate static void *
36840Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz)
36850Sstevel@tonic-gate {
36860Sstevel@tonic-gate 	void *ptr;
36870Sstevel@tonic-gate 
36880Sstevel@tonic-gate 	ptr = calloc(nelem, sz);
36890Sstevel@tonic-gate 	if (ptr == NULL) {
36900Sstevel@tonic-gate 		bam_error(NO_MEM, nelem*sz);
36910Sstevel@tonic-gate 		bam_exit(1);
36920Sstevel@tonic-gate 	}
36930Sstevel@tonic-gate 	return (ptr);
36940Sstevel@tonic-gate }
36950Sstevel@tonic-gate 
36960Sstevel@tonic-gate static char *
36970Sstevel@tonic-gate s_strdup(char *str)
36980Sstevel@tonic-gate {
36990Sstevel@tonic-gate 	char *ptr;
37000Sstevel@tonic-gate 
37010Sstevel@tonic-gate 	if (str == NULL)
37020Sstevel@tonic-gate 		return (NULL);
37030Sstevel@tonic-gate 
37040Sstevel@tonic-gate 	ptr = strdup(str);
37050Sstevel@tonic-gate 	if (ptr == NULL) {
37060Sstevel@tonic-gate 		bam_error(NO_MEM, strlen(str) + 1);
37070Sstevel@tonic-gate 		bam_exit(1);
37080Sstevel@tonic-gate 	}
37090Sstevel@tonic-gate 	return (ptr);
37100Sstevel@tonic-gate }
37110Sstevel@tonic-gate 
37120Sstevel@tonic-gate /*
37130Sstevel@tonic-gate  * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients)
37140Sstevel@tonic-gate  * Returns 0 otherwise
37150Sstevel@tonic-gate  */
37160Sstevel@tonic-gate static int
37170Sstevel@tonic-gate is_amd64(void)
37180Sstevel@tonic-gate {
37190Sstevel@tonic-gate 	static int amd64 = -1;
37200Sstevel@tonic-gate 	char isabuf[257];	/* from sysinfo(2) manpage */
37210Sstevel@tonic-gate 
37220Sstevel@tonic-gate 	if (amd64 != -1)
37230Sstevel@tonic-gate 		return (amd64);
37240Sstevel@tonic-gate 
37250Sstevel@tonic-gate 	if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 &&
37260Sstevel@tonic-gate 	    strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0)
37270Sstevel@tonic-gate 		amd64 = 1;
37280Sstevel@tonic-gate 	else if (strstr(isabuf, "i386") == NULL)
37290Sstevel@tonic-gate 		amd64 = 1;		/* diskless server */
37300Sstevel@tonic-gate 	else
37310Sstevel@tonic-gate 		amd64 = 0;
37320Sstevel@tonic-gate 
37330Sstevel@tonic-gate 	return (amd64);
37340Sstevel@tonic-gate }
37350Sstevel@tonic-gate 
37360Sstevel@tonic-gate static void
37370Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s)
37380Sstevel@tonic-gate {
37390Sstevel@tonic-gate 	line_t *lp;
37400Sstevel@tonic-gate 
37410Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
37420Sstevel@tonic-gate 	lp->line = s_strdup(s);
37430Sstevel@tonic-gate 	if (flistp->head == NULL)
37440Sstevel@tonic-gate 		flistp->head = lp;
37450Sstevel@tonic-gate 	else
37460Sstevel@tonic-gate 		flistp->tail->next = lp;
37470Sstevel@tonic-gate 	flistp->tail = lp;
37480Sstevel@tonic-gate }
3749