xref: /onnv-gate/usr/src/cmd/boot/bootadm/bootadm.c (revision 1746:418153b21f8c)
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
5*1746Svikram  * Common Development and Distribution License (the "License").
6*1746Svikram  * 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 /*
22*1746Svikram  * 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)();
1330Sstevel@tonic-gate } subcmd_defn_t;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate #define	BAM_MAXLINE	8192
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate #define	LINE_INIT	0	/* lineNum initial value */
1390Sstevel@tonic-gate #define	ENTRY_INIT	-1	/* entryNum initial value */
1400Sstevel@tonic-gate #define	ALL_ENTRIES	-2	/* selects all boot entries */
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate #define	GRUB_DIR		"/boot/grub"
1430Sstevel@tonic-gate #define	MULTI_BOOT		"/platform/i86pc/multiboot"
1440Sstevel@tonic-gate #define	BOOT_ARCHIVE		"/platform/i86pc/boot_archive"
1450Sstevel@tonic-gate #define	GRUB_MENU		"/boot/grub/menu.lst"
1460Sstevel@tonic-gate #define	MENU_TMP		"/boot/grub/menu.lst.tmp"
1470Sstevel@tonic-gate #define	RAMDISK_SPECIAL		"/ramdisk"
148621Svikram #define	STUBBOOT		"/stubboot"
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /* lock related */
1510Sstevel@tonic-gate #define	BAM_LOCK_FILE		"/var/run/bootadm.lock"
1520Sstevel@tonic-gate #define	LOCK_FILE_PERMS		(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate #define	CREATE_RAMDISK		"/boot/solaris/bin/create_ramdisk"
1550Sstevel@tonic-gate #define	CREATE_DISKMAP		"/boot/solaris/bin/create_diskmap"
1560Sstevel@tonic-gate #define	GRUBDISK_MAP		"/var/run/solaris_grubdisk.map"
1570Sstevel@tonic-gate 
158316Svikram #define	GRUB_slice		"/etc/lu/GRUB_slice"
159316Svikram #define	GRUB_root		"/etc/lu/GRUB_root"
160316Svikram #define	GRUB_backup_menu	"/etc/lu/GRUB_backup_menu"
161316Svikram #define	GRUB_slice_mntpt	"/tmp/GRUB_slice_mntpt"
162316Svikram #define	LU_ACTIVATE_FILE	"/etc/lu/DelayUpdate/activate.sh"
163*1746Svikram #define	GRUB_fdisk		"/etc/lu/GRUB_fdisk"
164*1746Svikram #define	GRUB_fdisk_target	"/etc/lu/GRUB_fdisk_target"
165316Svikram 
166316Svikram #define	INSTALLGRUB		"/sbin/installgrub"
167316Svikram #define	STAGE1			"/boot/grub/stage1"
168316Svikram #define	STAGE2			"/boot/grub/stage2"
169316Svikram 
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate  * Default file attributes
1720Sstevel@tonic-gate  */
1730Sstevel@tonic-gate #define	DEFAULT_DEV_MODE	0644	/* default permissions */
1740Sstevel@tonic-gate #define	DEFAULT_DEV_UID		0	/* user root */
1750Sstevel@tonic-gate #define	DEFAULT_DEV_GID		3	/* group sys */
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate /*
1780Sstevel@tonic-gate  * Menu related
1790Sstevel@tonic-gate  * menu_cmd_t and menu_cmds must be kept in sync
1800Sstevel@tonic-gate  */
1810Sstevel@tonic-gate typedef enum {
1820Sstevel@tonic-gate 	DEFAULT_CMD = 0,
1830Sstevel@tonic-gate 	TIMEOUT_CMD,
1840Sstevel@tonic-gate 	TITLE_CMD,
1850Sstevel@tonic-gate 	ROOT_CMD,
1860Sstevel@tonic-gate 	KERNEL_CMD,
1870Sstevel@tonic-gate 	MODULE_CMD,
1880Sstevel@tonic-gate 	SEP_CMD,
1890Sstevel@tonic-gate 	COMMENT_CMD
1900Sstevel@tonic-gate } menu_cmd_t;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate static char *menu_cmds[] = {
1930Sstevel@tonic-gate 	"default",	/* DEFAULT_CMD */
1940Sstevel@tonic-gate 	"timeout",	/* TIMEOUT_CMD */
1950Sstevel@tonic-gate 	"title",	/* TITLE_CMD */
1960Sstevel@tonic-gate 	"root",		/* ROOT_CMD */
1970Sstevel@tonic-gate 	"kernel",	/* KERNEL_CMD */
1980Sstevel@tonic-gate 	"module",	/* MODULE_CMD */
1990Sstevel@tonic-gate 	" ",		/* SEP_CMD */
2000Sstevel@tonic-gate 	"#",		/* COMMENT_CMD */
2010Sstevel@tonic-gate 	NULL
2020Sstevel@tonic-gate };
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate #define	OPT_ENTRY_NUM	"entry"
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate /*
2070Sstevel@tonic-gate  * archive related
2080Sstevel@tonic-gate  */
2090Sstevel@tonic-gate typedef struct {
2100Sstevel@tonic-gate 	line_t *head;
2110Sstevel@tonic-gate 	line_t *tail;
2120Sstevel@tonic-gate } filelist_t;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate #define	BOOT_FILE_LIST	"boot/solaris/filelist.ramdisk"
2150Sstevel@tonic-gate #define	ETC_FILE_LIST	"etc/boot/solaris/filelist.ramdisk"
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate #define	FILE_STAT	"boot/solaris/filestat.ramdisk"
2180Sstevel@tonic-gate #define	FILE_STAT_TMP	"boot/solaris/filestat.ramdisk.tmp"
2190Sstevel@tonic-gate #define	DIR_PERMS	(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
2200Sstevel@tonic-gate #define	FILE_STAT_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate #define	BAM_HDR		"---------- ADDED BY BOOTADM - DO NOT EDIT ----------"
2230Sstevel@tonic-gate #define	BAM_FTR		"---------------------END BOOTADM--------------------"
224662Sszhou #define	BAM_OLDDEF	"BOOTADM SAVED DEFAULT: "
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate /* Globals */
2270Sstevel@tonic-gate static char *prog;
2280Sstevel@tonic-gate static subcmd_t bam_cmd;
2290Sstevel@tonic-gate static char *bam_root;
2300Sstevel@tonic-gate static int bam_rootlen;
2310Sstevel@tonic-gate static int bam_root_readonly;
232621Svikram static int bam_alt_root;
2330Sstevel@tonic-gate static char *bam_subcmd;
2340Sstevel@tonic-gate static char *bam_opt;
2350Sstevel@tonic-gate static int bam_debug;
2360Sstevel@tonic-gate static char **bam_argv;
2370Sstevel@tonic-gate static int bam_argc;
2380Sstevel@tonic-gate static int bam_force;
2390Sstevel@tonic-gate static int bam_verbose;
2400Sstevel@tonic-gate static int bam_check;
2410Sstevel@tonic-gate static int bam_smf_check;
2420Sstevel@tonic-gate static int bam_lock_fd = -1;
2430Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/";
244316Svikram static int bam_update_all;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /* function prototypes */
2470Sstevel@tonic-gate static void parse_args_internal(int argc, char *argv[]);
2480Sstevel@tonic-gate static void parse_args(int argc, char *argv[]);
2490Sstevel@tonic-gate static error_t bam_menu(char *subcmd, char *opt, int argc, char *argv[]);
2500Sstevel@tonic-gate static error_t bam_archive(char *subcmd, char *opt);
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate static void bam_error(char *format, ...);
2530Sstevel@tonic-gate static void bam_print(char *format, ...);
2540Sstevel@tonic-gate static void bam_exit(int excode);
2550Sstevel@tonic-gate static void bam_lock(void);
2560Sstevel@tonic-gate static void bam_unlock(void);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate static int exec_cmd(char *cmdline, char *output, int64_t osize);
2590Sstevel@tonic-gate static error_t read_globals(menu_t *mp, char *menu_path,
2600Sstevel@tonic-gate     char *globalcmd, int quiet);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate static menu_t *menu_read(char *menu_path);
2630Sstevel@tonic-gate static error_t menu_write(char *root, menu_t *mp);
2640Sstevel@tonic-gate static void linelist_free(line_t *start);
2650Sstevel@tonic-gate static void menu_free(menu_t *mp);
2660Sstevel@tonic-gate static void line_free(line_t *lp);
2670Sstevel@tonic-gate static void filelist_free(filelist_t *flistp);
2680Sstevel@tonic-gate static error_t list2file(char *root, char *tmp,
2690Sstevel@tonic-gate     char *final, line_t *start);
2700Sstevel@tonic-gate static error_t list_entry(menu_t *mp, char *menu_path, char *opt);
2710Sstevel@tonic-gate static error_t delete_all_entries(menu_t *mp, char *menu_path, char *opt);
2720Sstevel@tonic-gate static error_t update_entry(menu_t *mp, char *root, char *opt);
2730Sstevel@tonic-gate static error_t update_temp(menu_t *mp, char *root, char *opt);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate static error_t update_archive(char *root, char *opt);
2760Sstevel@tonic-gate static error_t list_archive(char *root, char *opt);
2770Sstevel@tonic-gate static error_t update_all(char *root, char *opt);
2780Sstevel@tonic-gate static error_t read_list(char *root, filelist_t  *flistp);
2790Sstevel@tonic-gate static error_t set_global(menu_t *mp, char *globalcmd, int val);
2800Sstevel@tonic-gate static error_t set_option(menu_t *mp, char *globalcmd, char *opt);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate static long s_strtol(char *str);
2830Sstevel@tonic-gate static char *s_fgets(char *buf, int n, FILE *fp);
2840Sstevel@tonic-gate static int s_fputs(char *str, FILE *fp);
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate static void *s_calloc(size_t nelem, size_t sz);
2870Sstevel@tonic-gate static char *s_strdup(char *str);
2880Sstevel@tonic-gate static int is_readonly(char *);
2890Sstevel@tonic-gate static int is_amd64(void);
2900Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate #if defined(__sparc)
2930Sstevel@tonic-gate static void sparc_abort(void);
2940Sstevel@tonic-gate #endif
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate /* Menu related sub commands */
2970Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = {
2980Sstevel@tonic-gate 	"set_option",		OPT_OPTIONAL,	set_option,	/* PUB */
2990Sstevel@tonic-gate 	"list_entry",		OPT_OPTIONAL,	list_entry,	/* PUB */
3000Sstevel@tonic-gate 	"delete_all_entries",	OPT_ABSENT,	delete_all_entries, /* PVT */
3010Sstevel@tonic-gate 	"update_entry",		OPT_REQ,	update_entry,	/* menu */
3020Sstevel@tonic-gate 	"update_temp",		OPT_OPTIONAL,	update_temp,	/* reboot */
3030Sstevel@tonic-gate 	NULL,			0,		NULL	/* must be last */
3040Sstevel@tonic-gate };
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate /* Archive related sub commands */
3070Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = {
3080Sstevel@tonic-gate 	"update",		OPT_ABSENT,	update_archive, /* PUB */
3090Sstevel@tonic-gate 	"update_all",		OPT_ABSENT,	update_all,	/* PVT */
3100Sstevel@tonic-gate 	"list",			OPT_OPTIONAL,	list_archive,	/* PUB */
3110Sstevel@tonic-gate 	NULL,			0,		NULL	/* must be last */
3120Sstevel@tonic-gate };
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate static struct {
3150Sstevel@tonic-gate 	nvlist_t *new_nvlp;
3160Sstevel@tonic-gate 	nvlist_t *old_nvlp;
3170Sstevel@tonic-gate 	int need_update;
3180Sstevel@tonic-gate } walk_arg;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate static void
3210Sstevel@tonic-gate usage(void)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	(void) fprintf(stderr, "USAGE:\n");
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	/* archive usage */
3270Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s update-archive [-vn] [-R altroot]\n",
3280Sstevel@tonic-gate 	    prog);
3290Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-archive [-R altroot]\n", prog);
3300Sstevel@tonic-gate #ifndef __sparc
3310Sstevel@tonic-gate 	/* x86 only */
3320Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog);
3330Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog);
3340Sstevel@tonic-gate #endif
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate int
3380Sstevel@tonic-gate main(int argc, char *argv[])
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate 	error_t ret;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3430Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
3460Sstevel@tonic-gate 		prog = argv[0];
3470Sstevel@tonic-gate 	} else {
3480Sstevel@tonic-gate 		prog++;
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	if (geteuid() != 0) {
3520Sstevel@tonic-gate 		bam_error(MUST_BE_ROOT);
3530Sstevel@tonic-gate 		bam_exit(1);
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	bam_lock();
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	/*
3590Sstevel@tonic-gate 	 * Don't depend on caller's umask
3600Sstevel@tonic-gate 	 */
3610Sstevel@tonic-gate 	(void) umask(0022);
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	parse_args(argc, argv);
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate #if defined(__sparc)
3660Sstevel@tonic-gate 	/*
3670Sstevel@tonic-gate 	 * There are only two valid invocations of bootadm
3680Sstevel@tonic-gate 	 * on SPARC:
3690Sstevel@tonic-gate 	 *
3700Sstevel@tonic-gate 	 *	- SPARC diskless server creating boot_archive for i386 clients
3710Sstevel@tonic-gate 	 *	- archive creation call during reboot of a SPARC system
3720Sstevel@tonic-gate 	 *
3730Sstevel@tonic-gate 	 *	The latter should be a NOP
3740Sstevel@tonic-gate 	 */
3750Sstevel@tonic-gate 	if (bam_cmd != BAM_ARCHIVE) {
3760Sstevel@tonic-gate 		sparc_abort();
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate #endif
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	switch (bam_cmd) {
3810Sstevel@tonic-gate 		case BAM_MENU:
3820Sstevel@tonic-gate 			ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv);
3830Sstevel@tonic-gate 			break;
3840Sstevel@tonic-gate 		case BAM_ARCHIVE:
3850Sstevel@tonic-gate 			ret = bam_archive(bam_subcmd, bam_opt);
3860Sstevel@tonic-gate 			break;
3870Sstevel@tonic-gate 		default:
3880Sstevel@tonic-gate 			usage();
3890Sstevel@tonic-gate 			bam_exit(1);
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	if (ret != BAM_SUCCESS)
3930Sstevel@tonic-gate 		bam_exit(1);
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	bam_unlock();
3960Sstevel@tonic-gate 	return (0);
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate #if defined(__sparc)
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate static void
4020Sstevel@tonic-gate sparc_abort(void)
4030Sstevel@tonic-gate {
4040Sstevel@tonic-gate 	bam_error(NOT_ON_SPARC);
4050Sstevel@tonic-gate 	bam_exit(1);
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate #endif
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate /*
4110Sstevel@tonic-gate  * Equivalence of public and internal commands:
4120Sstevel@tonic-gate  *	update-archive  -- -a update
4130Sstevel@tonic-gate  *	list-archive	-- -a list
4140Sstevel@tonic-gate  *	set-menu	-- -m set_option
4150Sstevel@tonic-gate  *	list-menu	-- -m list_entry
4160Sstevel@tonic-gate  *	update-menu	-- -m update_entry
4170Sstevel@tonic-gate  */
4180Sstevel@tonic-gate static struct cmd_map {
4190Sstevel@tonic-gate 	char *bam_cmdname;
4200Sstevel@tonic-gate 	int bam_cmd;
4210Sstevel@tonic-gate 	char *bam_subcmd;
4220Sstevel@tonic-gate } cmd_map[] = {
4230Sstevel@tonic-gate 	{ "update-archive",	BAM_ARCHIVE,	"update"},
4240Sstevel@tonic-gate 	{ "list-archive",	BAM_ARCHIVE,	"list"},
4250Sstevel@tonic-gate 	{ "set-menu",		BAM_MENU,	"set_option"},
4260Sstevel@tonic-gate 	{ "list-menu",		BAM_MENU,	"list_entry"},
4270Sstevel@tonic-gate 	{ "update-menu",	BAM_MENU,	"update_entry"},
4280Sstevel@tonic-gate 	{ NULL,			0,		NULL}
4290Sstevel@tonic-gate };
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate /*
4320Sstevel@tonic-gate  * Commands syntax published in bootadm(1M) are parsed here
4330Sstevel@tonic-gate  */
4340Sstevel@tonic-gate static void
4350Sstevel@tonic-gate parse_args(int argc, char *argv[])
4360Sstevel@tonic-gate {
4370Sstevel@tonic-gate 	struct cmd_map *cmp = cmd_map;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	/* command conforming to the final spec */
4400Sstevel@tonic-gate 	if (argc > 1 && argv[1][0] != '-') {
4410Sstevel@tonic-gate 		/*
4420Sstevel@tonic-gate 		 * Map commands to internal table.
4430Sstevel@tonic-gate 		 */
4440Sstevel@tonic-gate 		while (cmp->bam_cmdname) {
4450Sstevel@tonic-gate 			if (strcmp(argv[1], cmp->bam_cmdname) == 0) {
4460Sstevel@tonic-gate 				bam_cmd = cmp->bam_cmd;
4470Sstevel@tonic-gate 				bam_subcmd = cmp->bam_subcmd;
4480Sstevel@tonic-gate 				break;
4490Sstevel@tonic-gate 			}
4500Sstevel@tonic-gate 			cmp++;
4510Sstevel@tonic-gate 		}
4520Sstevel@tonic-gate 		if (cmp->bam_cmdname == NULL) {
4530Sstevel@tonic-gate 			usage();
4540Sstevel@tonic-gate 			bam_exit(1);
4550Sstevel@tonic-gate 		}
4560Sstevel@tonic-gate 		argc--;
4570Sstevel@tonic-gate 		argv++;
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	parse_args_internal(argc, argv);
4610Sstevel@tonic-gate }
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate /*
4640Sstevel@tonic-gate  * A combination of public and private commands are parsed here.
4650Sstevel@tonic-gate  * The internal syntax and the corresponding functionality are:
4660Sstevel@tonic-gate  *	-a update	-- update-archive
4670Sstevel@tonic-gate  *	-a list		-- list-archive
4680Sstevel@tonic-gate  *	-a update-all	-- (reboot to sync all mounted OS archive)
4690Sstevel@tonic-gate  *	-m update_entry	-- update-menu
4700Sstevel@tonic-gate  *	-m list_entry	-- list-menu
4710Sstevel@tonic-gate  *	-m update_temp	-- (reboot -- [boot-args])
4720Sstevel@tonic-gate  *	-m delete_all_entries -- (called from install)
4730Sstevel@tonic-gate  */
4740Sstevel@tonic-gate static void
4750Sstevel@tonic-gate parse_args_internal(int argc, char *argv[])
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate 	int c, error;
4780Sstevel@tonic-gate 	extern char *optarg;
4790Sstevel@tonic-gate 	extern int optind, opterr;
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	/* Suppress error message from getopt */
4820Sstevel@tonic-gate 	opterr = 0;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	error = 0;
485662Sszhou 	while ((c = getopt(argc, argv, "a:d:fm:no:vCR:")) != -1) {
4860Sstevel@tonic-gate 		switch (c) {
4870Sstevel@tonic-gate 		case 'a':
4880Sstevel@tonic-gate 			if (bam_cmd) {
4890Sstevel@tonic-gate 				error = 1;
4900Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
4910Sstevel@tonic-gate 			}
4920Sstevel@tonic-gate 			bam_cmd = BAM_ARCHIVE;
4930Sstevel@tonic-gate 			bam_subcmd = optarg;
4940Sstevel@tonic-gate 			break;
4950Sstevel@tonic-gate 		case 'd':
4960Sstevel@tonic-gate 			if (bam_debug) {
4970Sstevel@tonic-gate 				error = 1;
4980Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4990Sstevel@tonic-gate 			}
5000Sstevel@tonic-gate 			bam_debug = s_strtol(optarg);
5010Sstevel@tonic-gate 			break;
5020Sstevel@tonic-gate 		case 'f':
5030Sstevel@tonic-gate 			if (bam_force) {
5040Sstevel@tonic-gate 				error = 1;
5050Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5060Sstevel@tonic-gate 			}
5070Sstevel@tonic-gate 			bam_force = 1;
5080Sstevel@tonic-gate 			break;
5090Sstevel@tonic-gate 		case 'm':
5100Sstevel@tonic-gate 			if (bam_cmd) {
5110Sstevel@tonic-gate 				error = 1;
5120Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
5130Sstevel@tonic-gate 			}
5140Sstevel@tonic-gate 			bam_cmd = BAM_MENU;
5150Sstevel@tonic-gate 			bam_subcmd = optarg;
5160Sstevel@tonic-gate 			break;
5170Sstevel@tonic-gate 		case 'n':
5180Sstevel@tonic-gate 			if (bam_check) {
5190Sstevel@tonic-gate 				error = 1;
5200Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5210Sstevel@tonic-gate 			}
5220Sstevel@tonic-gate 			bam_check = 1;
5230Sstevel@tonic-gate 			break;
5240Sstevel@tonic-gate 		case 'o':
5250Sstevel@tonic-gate 			if (bam_opt) {
5260Sstevel@tonic-gate 				error = 1;
5270Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5280Sstevel@tonic-gate 			}
5290Sstevel@tonic-gate 			bam_opt = optarg;
5300Sstevel@tonic-gate 			break;
5310Sstevel@tonic-gate 		case 'v':
5320Sstevel@tonic-gate 			if (bam_verbose) {
5330Sstevel@tonic-gate 				error = 1;
5340Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5350Sstevel@tonic-gate 			}
5360Sstevel@tonic-gate 			bam_verbose = 1;
5370Sstevel@tonic-gate 			break;
538662Sszhou 		case 'C':
539662Sszhou 			bam_smf_check = 1;
540662Sszhou 			break;
5410Sstevel@tonic-gate 		case 'R':
5420Sstevel@tonic-gate 			if (bam_root) {
5430Sstevel@tonic-gate 				error = 1;
5440Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5450Sstevel@tonic-gate 				break;
5460Sstevel@tonic-gate 			} else if (realpath(optarg, rootbuf) == NULL) {
5470Sstevel@tonic-gate 				error = 1;
5480Sstevel@tonic-gate 				bam_error(CANT_RESOLVE, optarg,
5490Sstevel@tonic-gate 				    strerror(errno));
5500Sstevel@tonic-gate 				break;
5510Sstevel@tonic-gate 			}
552621Svikram 			bam_alt_root = 1;
5530Sstevel@tonic-gate 			bam_root = rootbuf;
5540Sstevel@tonic-gate 			bam_rootlen = strlen(rootbuf);
5550Sstevel@tonic-gate 			break;
5560Sstevel@tonic-gate 		case '?':
5570Sstevel@tonic-gate 			error = 1;
5580Sstevel@tonic-gate 			bam_error(BAD_OPT, optopt);
5590Sstevel@tonic-gate 			break;
5600Sstevel@tonic-gate 		default :
5610Sstevel@tonic-gate 			error = 1;
5620Sstevel@tonic-gate 			bam_error(BAD_OPT, c);
5630Sstevel@tonic-gate 			break;
5640Sstevel@tonic-gate 		}
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	/*
5680Sstevel@tonic-gate 	 * A command option must be specfied
5690Sstevel@tonic-gate 	 */
5700Sstevel@tonic-gate 	if (!bam_cmd) {
5710Sstevel@tonic-gate 		if (bam_opt && strcmp(bam_opt, "all") == 0) {
5720Sstevel@tonic-gate 			usage();
5730Sstevel@tonic-gate 			bam_exit(0);
5740Sstevel@tonic-gate 		}
5750Sstevel@tonic-gate 		bam_error(NEED_CMD);
5760Sstevel@tonic-gate 		error = 1;
5770Sstevel@tonic-gate 	}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	if (error) {
5800Sstevel@tonic-gate 		usage();
5810Sstevel@tonic-gate 		bam_exit(1);
5820Sstevel@tonic-gate 	}
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	if (optind > argc) {
5850Sstevel@tonic-gate 		bam_error(INT_ERROR, "parse_args");
5860Sstevel@tonic-gate 		bam_exit(1);
5870Sstevel@tonic-gate 	} else if (optind < argc) {
5880Sstevel@tonic-gate 		bam_argv = &argv[optind];
5890Sstevel@tonic-gate 		bam_argc = argc - optind;
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	/*
5930Sstevel@tonic-gate 	 * -n implies verbose mode
5940Sstevel@tonic-gate 	 */
5950Sstevel@tonic-gate 	if (bam_check)
5960Sstevel@tonic-gate 		bam_verbose = 1;
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate static error_t
6000Sstevel@tonic-gate check_subcmd_and_options(
6010Sstevel@tonic-gate 	char *subcmd,
6020Sstevel@tonic-gate 	char *opt,
6030Sstevel@tonic-gate 	subcmd_defn_t *table,
6040Sstevel@tonic-gate 	error_t (**fp)())
6050Sstevel@tonic-gate {
6060Sstevel@tonic-gate 	int i;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	if (subcmd == NULL) {
6090Sstevel@tonic-gate 		bam_error(NEED_SUBCMD);
6100Sstevel@tonic-gate 		return (BAM_ERROR);
6110Sstevel@tonic-gate 	}
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	if (bam_root == NULL) {
6140Sstevel@tonic-gate 		bam_root = rootbuf;
6150Sstevel@tonic-gate 		bam_rootlen = 1;
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	/* verify that subcmd is valid */
6190Sstevel@tonic-gate 	for (i = 0; table[i].subcmd != NULL; i++) {
6200Sstevel@tonic-gate 		if (strcmp(table[i].subcmd, subcmd) == 0)
6210Sstevel@tonic-gate 			break;
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	if (table[i].subcmd == NULL) {
6250Sstevel@tonic-gate 		bam_error(INVALID_SUBCMD, subcmd);
6260Sstevel@tonic-gate 		return (BAM_ERROR);
6270Sstevel@tonic-gate 	}
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	/* subcmd verifies that opt is appropriate */
6300Sstevel@tonic-gate 	if (table[i].option != OPT_OPTIONAL) {
6310Sstevel@tonic-gate 		if ((table[i].option == OPT_REQ) ^ (opt != NULL)) {
6320Sstevel@tonic-gate 			if (opt)
6330Sstevel@tonic-gate 				bam_error(NO_OPT_REQ, subcmd);
6340Sstevel@tonic-gate 			else
6350Sstevel@tonic-gate 				bam_error(MISS_OPT, subcmd);
6360Sstevel@tonic-gate 			return (BAM_ERROR);
6370Sstevel@tonic-gate 		}
6380Sstevel@tonic-gate 	}
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	*fp = table[i].handler;
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	return (BAM_SUCCESS);
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate 
645316Svikram 
646316Svikram static char *
647621Svikram mount_grub_slice(int *mnted, char **physlice, char **logslice, char **fs_type)
648316Svikram {
649316Svikram 	struct extmnttab mnt;
650316Svikram 	struct stat sb;
651316Svikram 	char buf[BAM_MAXLINE], dev[PATH_MAX], phys[PATH_MAX], fstype[32];
652348Svikram 	char cmd[PATH_MAX];
653316Svikram 	char *mntpt;
654316Svikram 	int p, l, f;
655316Svikram 	FILE *fp;
656316Svikram 
657316Svikram 	assert(mnted);
658316Svikram 	*mnted = 0;
659316Svikram 
660316Svikram 	/*
661621Svikram 	 * physlice, logslice, fs_type  args may be NULL
662316Svikram 	 */
663316Svikram 	if (physlice)
664316Svikram 		*physlice = NULL;
665621Svikram 	if (logslice)
666621Svikram 		*logslice = NULL;
667621Svikram 	if (fs_type)
668621Svikram 		*fs_type = NULL;
669316Svikram 
670316Svikram 	if (stat(GRUB_slice, &sb) != 0) {
671316Svikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
672316Svikram 		return (NULL);
673316Svikram 	}
674316Svikram 
675316Svikram 	fp = fopen(GRUB_slice, "r");
676316Svikram 	if (fp == NULL) {
677316Svikram 		bam_error(OPEN_FAIL, GRUB_slice, strerror(errno));
678316Svikram 		return (NULL);
679316Svikram 	}
680316Svikram 
681316Svikram 	dev[0] = fstype[0] = phys[0] = '\0';
682316Svikram 	p = sizeof ("PHYS_SLICE=") - 1;
683316Svikram 	l = sizeof ("LOG_SLICE=") - 1;
684316Svikram 	f = sizeof ("LOG_FSTYP=") - 1;
685316Svikram 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
686316Svikram 		if (strncmp(buf, "PHYS_SLICE=", p) == 0) {
687316Svikram 			(void) strlcpy(phys, buf + p, sizeof (phys));
688316Svikram 			continue;
689316Svikram 		}
690316Svikram 		if (strncmp(buf, "LOG_SLICE=", l) == 0) {
691316Svikram 			(void) strlcpy(dev, buf + l, sizeof (dev));
692316Svikram 			continue;
693316Svikram 		}
694316Svikram 		if (strncmp(buf, "LOG_FSTYP=", f) == 0) {
695316Svikram 			(void) strlcpy(fstype, buf + f, sizeof (fstype));
696316Svikram 			continue;
697316Svikram 		}
698316Svikram 	}
699316Svikram 	(void) fclose(fp);
700316Svikram 
701316Svikram 	if (dev[0] == '\0' || fstype[0] == '\0' || phys[0] == '\0') {
702316Svikram 		bam_error(BAD_SLICE_FILE, GRUB_slice);
703316Svikram 		return (NULL);
704316Svikram 	}
705316Svikram 
706316Svikram 	if (physlice) {
707316Svikram 		*physlice = s_strdup(phys);
708316Svikram 	}
709621Svikram 	if (logslice) {
710621Svikram 		*logslice = s_strdup(dev);
711621Svikram 	}
712621Svikram 	if (fs_type) {
713621Svikram 		*fs_type = s_strdup(fstype);
714621Svikram 	}
715316Svikram 
716316Svikram 	/*
717316Svikram 	 * Check if the slice is already mounted
718316Svikram 	 */
719316Svikram 	fp = fopen(MNTTAB, "r");
720316Svikram 	if (fp == NULL) {
721316Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
722621Svikram 		goto error;
723316Svikram 	}
724316Svikram 
725316Svikram 	resetmnttab(fp);
726316Svikram 
727316Svikram 	mntpt = NULL;
728316Svikram 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
729316Svikram 		if (strcmp(mnt.mnt_special, dev) == 0) {
730316Svikram 			mntpt = s_strdup(mnt.mnt_mountp);
731316Svikram 			break;
732316Svikram 		}
733316Svikram 	}
734316Svikram 
735316Svikram 	(void) fclose(fp);
736316Svikram 
737316Svikram 	if (mntpt) {
738316Svikram 		return (mntpt);
739316Svikram 	}
740316Svikram 
741316Svikram 
742316Svikram 	/*
743316Svikram 	 * GRUB slice is not mounted, we need to mount it now.
744316Svikram 	 * First create the mountpoint
745316Svikram 	 */
746316Svikram 	mntpt = s_calloc(1, PATH_MAX);
747316Svikram 	(void) snprintf(mntpt, PATH_MAX, "%s.%d", GRUB_slice_mntpt, getpid());
748316Svikram 	if (mkdir(mntpt, 0755) == -1 && errno != EEXIST) {
749316Svikram 		bam_error(MKDIR_FAILED, mntpt, strerror(errno));
750316Svikram 		free(mntpt);
751621Svikram 		goto error;
752316Svikram 	}
753316Svikram 
754348Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/mount -F %s %s %s",
755348Svikram 	    fstype, dev, mntpt);
756348Svikram 
757348Svikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
758621Svikram 		bam_error(MOUNT_FAILED, dev, fstype);
759316Svikram 		if (rmdir(mntpt) != 0) {
760316Svikram 			bam_error(RMDIR_FAILED, mntpt, strerror(errno));
761316Svikram 		}
762316Svikram 		free(mntpt);
763621Svikram 		goto error;
764316Svikram 	}
765316Svikram 
766316Svikram 	*mnted = 1;
767316Svikram 	return (mntpt);
768621Svikram 
769621Svikram error:
770621Svikram 	if (physlice) {
771621Svikram 		free(*physlice);
772621Svikram 		*physlice = NULL;
773621Svikram 	}
774621Svikram 	if (logslice) {
775621Svikram 		free(*logslice);
776621Svikram 		*logslice = NULL;
777621Svikram 	}
778621Svikram 	if (fs_type) {
779621Svikram 		free(*fs_type);
780621Svikram 		*fs_type = NULL;
781621Svikram 	}
782621Svikram 	return (NULL);
783316Svikram }
784316Svikram 
785316Svikram static void
786621Svikram umount_grub_slice(
787621Svikram 	int mnted,
788621Svikram 	char *mntpt,
789621Svikram 	char *physlice,
790621Svikram 	char *logslice,
791621Svikram 	char *fs_type)
792316Svikram {
793348Svikram 	char cmd[PATH_MAX];
794348Svikram 
795316Svikram 	/*
796316Svikram 	 * If we have not dealt with GRUB slice
797316Svikram 	 * we have nothing to do - just return.
798316Svikram 	 */
799316Svikram 	if (mntpt == NULL)
800316Svikram 		return;
801316Svikram 
802316Svikram 
803316Svikram 	/*
804316Svikram 	 * If we mounted the filesystem earlier in mount_grub_slice()
805316Svikram 	 * unmount it now.
806316Svikram 	 */
807316Svikram 	if (mnted) {
808348Svikram 		(void) snprintf(cmd, sizeof (cmd), "/sbin/umount %s",
809348Svikram 		    mntpt);
810348Svikram 		if (exec_cmd(cmd, NULL, 0) != 0) {
811348Svikram 			bam_error(UMOUNT_FAILED, mntpt);
812316Svikram 		}
813316Svikram 		if (rmdir(mntpt) != 0) {
814316Svikram 			bam_error(RMDIR_FAILED, mntpt, strerror(errno));
815316Svikram 		}
816316Svikram 	}
817621Svikram 
818316Svikram 	if (physlice)
819316Svikram 		free(physlice);
820621Svikram 	if (logslice)
821621Svikram 		free(logslice);
822621Svikram 	if (fs_type)
823621Svikram 		free(fs_type);
824621Svikram 
825316Svikram 	free(mntpt);
826316Svikram }
827316Svikram 
828621Svikram static char *
829621Svikram use_stubboot(void)
830621Svikram {
831621Svikram 	int mnted;
832621Svikram 	struct stat sb;
833621Svikram 	struct extmnttab mnt;
834621Svikram 	FILE *fp;
835621Svikram 	char cmd[PATH_MAX];
836621Svikram 
837621Svikram 	if (stat(STUBBOOT, &sb) != 0) {
838621Svikram 		bam_error(STUBBOOT_DIR_NOT_FOUND);
839621Svikram 		return (NULL);
840621Svikram 	}
841621Svikram 
842621Svikram 	/*
843621Svikram 	 * Check if stubboot is mounted. If not, mount it
844621Svikram 	 */
845621Svikram 	fp = fopen(MNTTAB, "r");
846621Svikram 	if (fp == NULL) {
847621Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
848621Svikram 		return (NULL);
849621Svikram 	}
850621Svikram 
851621Svikram 	resetmnttab(fp);
852621Svikram 
853621Svikram 	mnted = 0;
854621Svikram 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
855621Svikram 		if (strcmp(mnt.mnt_mountp, STUBBOOT) == 0) {
856621Svikram 			mnted = 1;
857621Svikram 			break;
858621Svikram 		}
859621Svikram 	}
860621Svikram 
861621Svikram 	(void) fclose(fp);
862621Svikram 
863621Svikram 	if (mnted)
864621Svikram 		return (STUBBOOT);
865621Svikram 
866621Svikram 	/*
867621Svikram 	 * Stubboot is not mounted, mount it now.
868621Svikram 	 * It should exist in /etc/vfstab
869621Svikram 	 */
870621Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/mount %s",
871621Svikram 	    STUBBOOT);
872621Svikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
873621Svikram 		bam_error(MOUNT_MNTPT_FAILED, STUBBOOT);
874621Svikram 		return (NULL);
875621Svikram 	}
876621Svikram 
877621Svikram 	return (STUBBOOT);
878621Svikram }
879621Svikram 
880621Svikram static void
881621Svikram disp_active_menu_locn(char *menu_path, char *logslice, char *fstype, int mnted)
882621Svikram {
883621Svikram 	/*
884621Svikram 	 * Check if we did a temp mount of an unmounted device.
885621Svikram 	 * If yes, print the block device and fstype for that device
886621Svikram 	 * else it is already mounted, so we print the path to the GRUB menu.
887621Svikram 	 */
888621Svikram 	if (mnted) {
889621Svikram 		bam_print(GRUB_MENU_DEVICE, logslice);
890621Svikram 		bam_print(GRUB_MENU_FSTYPE, fstype);
891621Svikram 	} else {
892621Svikram 		bam_print(GRUB_MENU_PATH, menu_path);
893621Svikram 	}
894621Svikram }
895621Svikram 
896621Svikram /*
897621Svikram  * NOTE: A single "/" is also considered a trailing slash and will
898621Svikram  * be deleted.
899621Svikram  */
900621Svikram static void
901621Svikram elide_trailing_slash(const char *src, char *dst, size_t dstsize)
902621Svikram {
903621Svikram 	size_t dstlen;
904621Svikram 
905621Svikram 	assert(src);
906621Svikram 	assert(dst);
907621Svikram 
908621Svikram 	(void) strlcpy(dst, src, dstsize);
909621Svikram 
910621Svikram 	dstlen = strlen(dst);
911621Svikram 	if (dst[dstlen - 1] == '/') {
912621Svikram 		dst[dstlen - 1] = '\0';
913621Svikram 	}
914621Svikram }
915621Svikram 
9160Sstevel@tonic-gate static error_t
9170Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[])
9180Sstevel@tonic-gate {
9190Sstevel@tonic-gate 	error_t ret;
9200Sstevel@tonic-gate 	char menu_path[PATH_MAX];
9210Sstevel@tonic-gate 	menu_t *menu;
922621Svikram 	char *mntpt, *menu_root, *logslice, *fstype;
923316Svikram 	struct stat sb;
924316Svikram 	int mnted;	/* set if we did a mount */
9250Sstevel@tonic-gate 	error_t (*f)(menu_t *mp, char *menu_path, char *opt);
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	/*
9280Sstevel@tonic-gate 	 * Check arguments
9290Sstevel@tonic-gate 	 */
9300Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
9310Sstevel@tonic-gate 	if (ret == BAM_ERROR) {
9320Sstevel@tonic-gate 		return (BAM_ERROR);
9330Sstevel@tonic-gate 	}
9340Sstevel@tonic-gate 
935316Svikram 	mntpt = NULL;
936316Svikram 	mnted = 0;
937621Svikram 	logslice = fstype = NULL;
938621Svikram 
939621Svikram 	/*
940621Svikram 	 * If the user provides an alternate root, we
941621Svikram 	 * assume they know what they are doing and we
942621Svikram 	 * use it. Else we check if there is an
943621Svikram 	 * alternate location (other than /boot/grub)
944621Svikram 	 * for the GRUB menu
945621Svikram 	 */
946621Svikram 	if (bam_alt_root) {
947621Svikram 		menu_root = bam_root;
948621Svikram 	} else if (stat(GRUB_slice, &sb) == 0) {
949621Svikram 		mntpt = mount_grub_slice(&mnted, NULL, &logslice, &fstype);
950316Svikram 		menu_root = mntpt;
951621Svikram 	} else if (stat(STUBBOOT, &sb) == 0) {
952621Svikram 		menu_root = use_stubboot();
953316Svikram 	} else {
954316Svikram 		menu_root = bam_root;
955316Svikram 	}
956316Svikram 
957621Svikram 	if (menu_root == NULL) {
958621Svikram 		bam_error(CANNOT_LOCATE_GRUB_MENU);
959621Svikram 		return (BAM_ERROR);
960621Svikram 	}
961621Svikram 
962621Svikram 	elide_trailing_slash(menu_root, menu_path, sizeof (menu_path));
963621Svikram 	(void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path));
964621Svikram 
965621Svikram 	/*
966621Svikram 	 * If listing the menu, display the active menu
967621Svikram 	 * location
968621Svikram 	 */
969621Svikram 	if (strcmp(subcmd, "list_entry") == 0) {
970621Svikram 		disp_active_menu_locn(menu_path, logslice, fstype, mnted);
971621Svikram 	}
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	menu = menu_read(menu_path);
9740Sstevel@tonic-gate 	assert(menu);
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	/*
9770Sstevel@tonic-gate 	 * Special handling for setting timeout and default
9780Sstevel@tonic-gate 	 */
9790Sstevel@tonic-gate 	if (strcmp(subcmd, "set_option") == 0) {
9800Sstevel@tonic-gate 		if (largc != 1 || largv[0] == NULL) {
9810Sstevel@tonic-gate 			usage();
982621Svikram 			menu_free(menu);
983621Svikram 			umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
9840Sstevel@tonic-gate 			return (BAM_ERROR);
9850Sstevel@tonic-gate 		}
9860Sstevel@tonic-gate 		opt = largv[0];
9870Sstevel@tonic-gate 	} else if (largc != 0) {
9880Sstevel@tonic-gate 		usage();
989621Svikram 		menu_free(menu);
990621Svikram 		umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
9910Sstevel@tonic-gate 		return (BAM_ERROR);
9920Sstevel@tonic-gate 	}
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	/*
9950Sstevel@tonic-gate 	 * Once the sub-cmd handler has run
9960Sstevel@tonic-gate 	 * only the line field is guaranteed to have valid values
9970Sstevel@tonic-gate 	 */
9980Sstevel@tonic-gate 	if (strcmp(subcmd, "update_entry") == 0)
9990Sstevel@tonic-gate 		ret = f(menu, bam_root, opt);
10000Sstevel@tonic-gate 	else
10010Sstevel@tonic-gate 		ret = f(menu, menu_path, opt);
10020Sstevel@tonic-gate 	if (ret == BAM_WRITE) {
1003316Svikram 		ret = menu_write(menu_root, menu);
10040Sstevel@tonic-gate 	}
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	menu_free(menu);
10070Sstevel@tonic-gate 
1008621Svikram 	umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
1009316Svikram 
10100Sstevel@tonic-gate 	return (ret);
10110Sstevel@tonic-gate }
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate static error_t
10150Sstevel@tonic-gate bam_archive(
10160Sstevel@tonic-gate 	char *subcmd,
10170Sstevel@tonic-gate 	char *opt)
10180Sstevel@tonic-gate {
10190Sstevel@tonic-gate 	error_t ret;
10200Sstevel@tonic-gate 	error_t (*f)(char *root, char *opt);
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	/*
1023662Sszhou 	 * Add trailing / for archive subcommands
1024662Sszhou 	 */
1025662Sszhou 	if (rootbuf[strlen(rootbuf) - 1] != '/')
1026662Sszhou 		(void) strcat(rootbuf, "/");
1027662Sszhou 	bam_rootlen = strlen(rootbuf);
1028662Sszhou 
1029662Sszhou 	/*
10300Sstevel@tonic-gate 	 * Check arguments
10310Sstevel@tonic-gate 	 */
10320Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f);
10330Sstevel@tonic-gate 	if (ret != BAM_SUCCESS) {
10340Sstevel@tonic-gate 		return (BAM_ERROR);
10350Sstevel@tonic-gate 	}
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate #if defined(__sparc)
10380Sstevel@tonic-gate 	/*
10390Sstevel@tonic-gate 	 * A NOP if called on SPARC during reboot
10400Sstevel@tonic-gate 	 */
10410Sstevel@tonic-gate 	if (strcmp(subcmd, "update_all") == 0)
10420Sstevel@tonic-gate 		return (BAM_SUCCESS);
10430Sstevel@tonic-gate 	else if (strcmp(subcmd, "update") != 0)
10440Sstevel@tonic-gate 		sparc_abort();
10450Sstevel@tonic-gate #endif
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	/*
10480Sstevel@tonic-gate 	 * Check archive not supported with update_all
10490Sstevel@tonic-gate 	 * since it is awkward to display out-of-sync
10500Sstevel@tonic-gate 	 * information for each BE.
10510Sstevel@tonic-gate 	 */
10520Sstevel@tonic-gate 	if (bam_check && strcmp(subcmd, "update_all") == 0) {
10530Sstevel@tonic-gate 		bam_error(CHECK_NOT_SUPPORTED, subcmd);
10540Sstevel@tonic-gate 		return (BAM_ERROR);
10550Sstevel@tonic-gate 	}
10560Sstevel@tonic-gate 
1057316Svikram 	if (strcmp(subcmd, "update_all") == 0)
1058316Svikram 		bam_update_all = 1;
1059316Svikram 
1060316Svikram 	ret = f(bam_root, opt);
1061316Svikram 
1062316Svikram 	bam_update_all = 0;
1063316Svikram 
1064316Svikram 	return (ret);
10650Sstevel@tonic-gate }
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate /*PRINTFLIKE1*/
10680Sstevel@tonic-gate static void
10690Sstevel@tonic-gate bam_error(char *format, ...)
10700Sstevel@tonic-gate {
10710Sstevel@tonic-gate 	va_list ap;
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	va_start(ap, format);
10740Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", prog);
10750Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
10760Sstevel@tonic-gate 	va_end(ap);
10770Sstevel@tonic-gate }
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate /*PRINTFLIKE1*/
10800Sstevel@tonic-gate static void
10810Sstevel@tonic-gate bam_print(char *format, ...)
10820Sstevel@tonic-gate {
10830Sstevel@tonic-gate 	va_list ap;
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	va_start(ap, format);
10860Sstevel@tonic-gate 	(void) vfprintf(stdout, format, ap);
10870Sstevel@tonic-gate 	va_end(ap);
10880Sstevel@tonic-gate }
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate static void
10910Sstevel@tonic-gate bam_exit(int excode)
10920Sstevel@tonic-gate {
10930Sstevel@tonic-gate 	bam_unlock();
10940Sstevel@tonic-gate 	exit(excode);
10950Sstevel@tonic-gate }
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate static void
10980Sstevel@tonic-gate bam_lock(void)
10990Sstevel@tonic-gate {
11000Sstevel@tonic-gate 	struct flock lock;
11010Sstevel@tonic-gate 	pid_t pid;
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS);
11040Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
11050Sstevel@tonic-gate 		/*
11060Sstevel@tonic-gate 		 * We may be invoked early in boot for archive verification.
11070Sstevel@tonic-gate 		 * In this case, root is readonly and /var/run may not exist.
11080Sstevel@tonic-gate 		 * Proceed without the lock
11090Sstevel@tonic-gate 		 */
11100Sstevel@tonic-gate 		if (errno == EROFS || errno == ENOENT) {
11110Sstevel@tonic-gate 			bam_root_readonly = 1;
11120Sstevel@tonic-gate 			return;
11130Sstevel@tonic-gate 		}
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 		bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno));
11160Sstevel@tonic-gate 		bam_exit(1);
11170Sstevel@tonic-gate 	}
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
11200Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
11210Sstevel@tonic-gate 	lock.l_start = 0;
11220Sstevel@tonic-gate 	lock.l_len = 0;
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) {
11250Sstevel@tonic-gate 		if (errno != EACCES && errno != EAGAIN) {
11260Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11270Sstevel@tonic-gate 			(void) close(bam_lock_fd);
11280Sstevel@tonic-gate 			bam_lock_fd = -1;
11290Sstevel@tonic-gate 			bam_exit(1);
11300Sstevel@tonic-gate 		}
11310Sstevel@tonic-gate 		pid = 0;
11320Sstevel@tonic-gate 		(void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0);
11330Sstevel@tonic-gate 		bam_print(FILE_LOCKED, pid);
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 		lock.l_type = F_WRLCK;
11360Sstevel@tonic-gate 		lock.l_whence = SEEK_SET;
11370Sstevel@tonic-gate 		lock.l_start = 0;
11380Sstevel@tonic-gate 		lock.l_len = 0;
11390Sstevel@tonic-gate 		if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) {
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 	}
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	/* We own the lock now */
11480Sstevel@tonic-gate 	pid = getpid();
11490Sstevel@tonic-gate 	(void) write(bam_lock_fd, &pid, sizeof (pid));
11500Sstevel@tonic-gate }
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate static void
11530Sstevel@tonic-gate bam_unlock(void)
11540Sstevel@tonic-gate {
11550Sstevel@tonic-gate 	struct flock unlock;
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	/*
11580Sstevel@tonic-gate 	 * NOP if we don't hold the lock
11590Sstevel@tonic-gate 	 */
11600Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
11610Sstevel@tonic-gate 		return;
11620Sstevel@tonic-gate 	}
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
11650Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
11660Sstevel@tonic-gate 	unlock.l_start = 0;
11670Sstevel@tonic-gate 	unlock.l_len = 0;
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) {
11700Sstevel@tonic-gate 		bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11710Sstevel@tonic-gate 	}
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	if (close(bam_lock_fd) == -1) {
11740Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno));
11750Sstevel@tonic-gate 	}
11760Sstevel@tonic-gate 	bam_lock_fd = -1;
11770Sstevel@tonic-gate }
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate static error_t
11800Sstevel@tonic-gate list_archive(char *root, char *opt)
11810Sstevel@tonic-gate {
11820Sstevel@tonic-gate 	filelist_t flist;
11830Sstevel@tonic-gate 	filelist_t *flistp = &flist;
11840Sstevel@tonic-gate 	line_t *lp;
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	assert(root);
11870Sstevel@tonic-gate 	assert(opt == NULL);
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
11900Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
11910Sstevel@tonic-gate 		return (BAM_ERROR);
11920Sstevel@tonic-gate 	}
11930Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
11960Sstevel@tonic-gate 		bam_print(PRINT, lp->line);
11970Sstevel@tonic-gate 	}
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	filelist_free(flistp);
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	return (BAM_SUCCESS);
12020Sstevel@tonic-gate }
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate /*
12050Sstevel@tonic-gate  * This routine writes a list of lines to a file.
12060Sstevel@tonic-gate  * The list is *not* freed
12070Sstevel@tonic-gate  */
12080Sstevel@tonic-gate static error_t
12090Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start)
12100Sstevel@tonic-gate {
12110Sstevel@tonic-gate 	char tmpfile[PATH_MAX];
12120Sstevel@tonic-gate 	char path[PATH_MAX];
12130Sstevel@tonic-gate 	FILE *fp;
12140Sstevel@tonic-gate 	int ret;
12150Sstevel@tonic-gate 	struct stat sb;
12160Sstevel@tonic-gate 	mode_t mode;
12170Sstevel@tonic-gate 	uid_t root_uid;
12180Sstevel@tonic-gate 	gid_t sys_gid;
12190Sstevel@tonic-gate 	struct passwd *pw;
12200Sstevel@tonic-gate 	struct group *gp;
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, final);
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	if (start == NULL) {
12260Sstevel@tonic-gate 		if (stat(path, &sb) != -1) {
12270Sstevel@tonic-gate 			bam_print(UNLINK_EMPTY, path);
12280Sstevel@tonic-gate 			if (unlink(path) != 0) {
12290Sstevel@tonic-gate 				bam_error(UNLINK_FAIL, path, strerror(errno));
12300Sstevel@tonic-gate 				return (BAM_ERROR);
12310Sstevel@tonic-gate 			} else {
12320Sstevel@tonic-gate 				return (BAM_SUCCESS);
12330Sstevel@tonic-gate 			}
12340Sstevel@tonic-gate 		}
12350Sstevel@tonic-gate 	}
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	/*
12380Sstevel@tonic-gate 	 * Preserve attributes of existing file if possible,
12390Sstevel@tonic-gate 	 * otherwise ask the system for uid/gid of root/sys.
12400Sstevel@tonic-gate 	 * If all fails, fall back on hard-coded defaults.
12410Sstevel@tonic-gate 	 */
12420Sstevel@tonic-gate 	if (stat(path, &sb) != -1) {
12430Sstevel@tonic-gate 		mode = sb.st_mode;
12440Sstevel@tonic-gate 		root_uid = sb.st_uid;
12450Sstevel@tonic-gate 		sys_gid = sb.st_gid;
12460Sstevel@tonic-gate 	} else {
12470Sstevel@tonic-gate 		mode = DEFAULT_DEV_MODE;
12480Sstevel@tonic-gate 		if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
12490Sstevel@tonic-gate 			root_uid = pw->pw_uid;
12500Sstevel@tonic-gate 		} else {
12510Sstevel@tonic-gate 			if (bam_verbose)
12520Sstevel@tonic-gate 				bam_error(CANT_FIND_USER,
12530Sstevel@tonic-gate 				    DEFAULT_DEV_USER, DEFAULT_DEV_UID);
12540Sstevel@tonic-gate 			root_uid = (uid_t)DEFAULT_DEV_UID;
12550Sstevel@tonic-gate 		}
12560Sstevel@tonic-gate 		if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
12570Sstevel@tonic-gate 			sys_gid = gp->gr_gid;
12580Sstevel@tonic-gate 		} else {
12590Sstevel@tonic-gate 			if (bam_verbose)
12600Sstevel@tonic-gate 				bam_error(CANT_FIND_GROUP,
12610Sstevel@tonic-gate 				    DEFAULT_DEV_GROUP, DEFAULT_DEV_GID);
12620Sstevel@tonic-gate 			sys_gid = (gid_t)DEFAULT_DEV_GID;
12630Sstevel@tonic-gate 		}
12640Sstevel@tonic-gate 	}
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	(void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp);
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	/* Truncate tmpfile first */
12690Sstevel@tonic-gate 	fp = fopen(tmpfile, "w");
12700Sstevel@tonic-gate 	if (fp == NULL) {
12710Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
12720Sstevel@tonic-gate 		return (BAM_ERROR);
12730Sstevel@tonic-gate 	}
12740Sstevel@tonic-gate 	ret = fclose(fp);
12750Sstevel@tonic-gate 	if (ret == EOF) {
12760Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
12770Sstevel@tonic-gate 		return (BAM_ERROR);
12780Sstevel@tonic-gate 	}
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	/* Now open it in append mode */
12810Sstevel@tonic-gate 	fp = fopen(tmpfile, "a");
12820Sstevel@tonic-gate 	if (fp == NULL) {
12830Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
12840Sstevel@tonic-gate 		return (BAM_ERROR);
12850Sstevel@tonic-gate 	}
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 	for (; start; start = start->next) {
12880Sstevel@tonic-gate 		ret = s_fputs(start->line, fp);
12890Sstevel@tonic-gate 		if (ret == EOF) {
12900Sstevel@tonic-gate 			bam_error(WRITE_FAIL, tmpfile, strerror(errno));
12910Sstevel@tonic-gate 			(void) fclose(fp);
12920Sstevel@tonic-gate 			return (BAM_ERROR);
12930Sstevel@tonic-gate 		}
12940Sstevel@tonic-gate 	}
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 	ret = fclose(fp);
12970Sstevel@tonic-gate 	if (ret == EOF) {
12980Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
12990Sstevel@tonic-gate 		return (BAM_ERROR);
13000Sstevel@tonic-gate 	}
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	/*
1303271Sjg 	 * Set up desired attributes.  Ignore failures on filesystems
1304271Sjg 	 * not supporting these operations - pcfs reports unsupported
1305271Sjg 	 * operations as EINVAL.
13060Sstevel@tonic-gate 	 */
13070Sstevel@tonic-gate 	ret = chmod(tmpfile, mode);
1308271Sjg 	if (ret == -1 &&
1309271Sjg 	    errno != EINVAL && errno != ENOTSUP) {
13100Sstevel@tonic-gate 		bam_error(CHMOD_FAIL, tmpfile, strerror(errno));
13110Sstevel@tonic-gate 		return (BAM_ERROR);
13120Sstevel@tonic-gate 	}
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	ret = chown(tmpfile, root_uid, sys_gid);
1315271Sjg 	if (ret == -1 &&
1316271Sjg 	    errno != EINVAL && errno != ENOTSUP) {
13170Sstevel@tonic-gate 		bam_error(CHOWN_FAIL, tmpfile, strerror(errno));
13180Sstevel@tonic-gate 		return (BAM_ERROR);
13190Sstevel@tonic-gate 	}
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	/*
13230Sstevel@tonic-gate 	 * Do an atomic rename
13240Sstevel@tonic-gate 	 */
13250Sstevel@tonic-gate 	ret = rename(tmpfile, path);
13260Sstevel@tonic-gate 	if (ret != 0) {
13270Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path, strerror(errno));
13280Sstevel@tonic-gate 		return (BAM_ERROR);
13290Sstevel@tonic-gate 	}
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 	return (BAM_SUCCESS);
13320Sstevel@tonic-gate }
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate /*
13350Sstevel@tonic-gate  * This function should always return 0 - since we want
13360Sstevel@tonic-gate  * to create stat data for *all* files in the list.
13370Sstevel@tonic-gate  */
13380Sstevel@tonic-gate /*ARGSUSED*/
13390Sstevel@tonic-gate static int
13400Sstevel@tonic-gate cmpstat(
13410Sstevel@tonic-gate 	const char *file,
13420Sstevel@tonic-gate 	const struct stat *stat,
13430Sstevel@tonic-gate 	int flags,
13440Sstevel@tonic-gate 	struct FTW *ftw)
13450Sstevel@tonic-gate {
13460Sstevel@tonic-gate 	uint_t sz;
13470Sstevel@tonic-gate 	uint64_t *value;
13480Sstevel@tonic-gate 	uint64_t filestat[2];
13490Sstevel@tonic-gate 	int error;
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	/*
13520Sstevel@tonic-gate 	 * We only want regular files
13530Sstevel@tonic-gate 	 */
13540Sstevel@tonic-gate 	if (!S_ISREG(stat->st_mode))
13550Sstevel@tonic-gate 		return (0);
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	/*
13580Sstevel@tonic-gate 	 * new_nvlp may be NULL if there were errors earlier
13590Sstevel@tonic-gate 	 * but this is not fatal to update determination.
13600Sstevel@tonic-gate 	 */
13610Sstevel@tonic-gate 	if (walk_arg.new_nvlp) {
13620Sstevel@tonic-gate 		filestat[0] = stat->st_size;
13630Sstevel@tonic-gate 		filestat[1] = stat->st_mtime;
13640Sstevel@tonic-gate 		error = nvlist_add_uint64_array(walk_arg.new_nvlp,
13650Sstevel@tonic-gate 		    file + bam_rootlen, filestat, 2);
13660Sstevel@tonic-gate 		if (error)
13670Sstevel@tonic-gate 			bam_error(NVADD_FAIL, file, strerror(error));
13680Sstevel@tonic-gate 	}
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 	/*
13710Sstevel@tonic-gate 	 * The remaining steps are only required if we haven't made a
13720Sstevel@tonic-gate 	 * decision about update or if we are checking (-n)
13730Sstevel@tonic-gate 	 */
13740Sstevel@tonic-gate 	if (walk_arg.need_update && !bam_check)
13750Sstevel@tonic-gate 		return (0);
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	/*
13780Sstevel@tonic-gate 	 * If we are invoked as part of system/filesyste/boot-archive
13790Sstevel@tonic-gate 	 * SMF service, ignore amd64 modules unless we are booted amd64.
13800Sstevel@tonic-gate 	 */
1381662Sszhou 	if (bam_smf_check && !is_amd64() && strstr(file, "/amd64/") != 0)
13820Sstevel@tonic-gate 		return (0);
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	/*
13850Sstevel@tonic-gate 	 * We need an update if file doesn't exist in old archive
13860Sstevel@tonic-gate 	 */
13870Sstevel@tonic-gate 	if (walk_arg.old_nvlp == NULL ||
13880Sstevel@tonic-gate 	    nvlist_lookup_uint64_array(walk_arg.old_nvlp,
13890Sstevel@tonic-gate 	    file + bam_rootlen, &value, &sz) != 0) {
13900Sstevel@tonic-gate 		if (bam_smf_check)	/* ignore new during smf check */
13910Sstevel@tonic-gate 			return (0);
13920Sstevel@tonic-gate 		walk_arg.need_update = 1;
13930Sstevel@tonic-gate 		if (bam_verbose)
13940Sstevel@tonic-gate 			bam_print(PARSEABLE_NEW_FILE, file);
13950Sstevel@tonic-gate 		return (0);
13960Sstevel@tonic-gate 	}
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	/*
13990Sstevel@tonic-gate 	 * File exists in old archive. Check if file has changed
14000Sstevel@tonic-gate 	 */
14010Sstevel@tonic-gate 	assert(sz == 2);
14020Sstevel@tonic-gate 	bcopy(value, filestat, sizeof (filestat));
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 	if (filestat[0] != stat->st_size ||
14050Sstevel@tonic-gate 	    filestat[1] != stat->st_mtime) {
14060Sstevel@tonic-gate 		walk_arg.need_update = 1;
14070Sstevel@tonic-gate 		if (bam_verbose)
14080Sstevel@tonic-gate 			if (bam_smf_check)
14090Sstevel@tonic-gate 				bam_print("    %s\n", file);
14100Sstevel@tonic-gate 			else
14110Sstevel@tonic-gate 				bam_print(PARSEABLE_OUT_DATE, file);
14120Sstevel@tonic-gate 	}
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	return (0);
14150Sstevel@tonic-gate }
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate /*
14180Sstevel@tonic-gate  * Check flags and presence of required files.
14190Sstevel@tonic-gate  * The force flag and/or absence of files should
14200Sstevel@tonic-gate  * trigger an update.
14210Sstevel@tonic-gate  * Suppress stdout output if check (-n) option is set
14220Sstevel@tonic-gate  * (as -n should only produce parseable output.)
14230Sstevel@tonic-gate  */
14240Sstevel@tonic-gate static void
14250Sstevel@tonic-gate check_flags_and_files(char *root)
14260Sstevel@tonic-gate {
14270Sstevel@tonic-gate 	char path[PATH_MAX];
14280Sstevel@tonic-gate 	struct stat sb;
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 	/*
14310Sstevel@tonic-gate 	 * if force, create archive unconditionally
14320Sstevel@tonic-gate 	 */
14330Sstevel@tonic-gate 	if (bam_force) {
14340Sstevel@tonic-gate 		walk_arg.need_update = 1;
14350Sstevel@tonic-gate 		if (bam_verbose && !bam_check)
14360Sstevel@tonic-gate 			bam_print(UPDATE_FORCE);
14370Sstevel@tonic-gate 		return;
14380Sstevel@tonic-gate 	}
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 	/*
14410Sstevel@tonic-gate 	 * If archive is missing, create archive
14420Sstevel@tonic-gate 	 */
14430Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE);
14440Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
14450Sstevel@tonic-gate 		if (bam_verbose && !bam_check)
14460Sstevel@tonic-gate 			bam_print(UPDATE_ARCH_MISS, path);
14470Sstevel@tonic-gate 		walk_arg.need_update = 1;
14480Sstevel@tonic-gate 		return;
14490Sstevel@tonic-gate 	}
14500Sstevel@tonic-gate }
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate static error_t
14530Sstevel@tonic-gate read_one_list(char *root, filelist_t  *flistp, char *filelist)
14540Sstevel@tonic-gate {
14550Sstevel@tonic-gate 	char path[PATH_MAX];
14560Sstevel@tonic-gate 	FILE *fp;
14570Sstevel@tonic-gate 	char buf[BAM_MAXLINE];
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, filelist);
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 	fp = fopen(path, "r");
14620Sstevel@tonic-gate 	if (fp == NULL) {
14630Sstevel@tonic-gate 		if (bam_debug)
14640Sstevel@tonic-gate 			bam_error(FLIST_FAIL, path, strerror(errno));
14650Sstevel@tonic-gate 		return (BAM_ERROR);
14660Sstevel@tonic-gate 	}
14670Sstevel@tonic-gate 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
1468316Svikram 		/* skip blank lines */
1469316Svikram 		if (strspn(buf, " \t") == strlen(buf))
1470316Svikram 			continue;
14710Sstevel@tonic-gate 		append_to_flist(flistp, buf);
14720Sstevel@tonic-gate 	}
14730Sstevel@tonic-gate 	if (fclose(fp) != 0) {
14740Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, path, strerror(errno));
14750Sstevel@tonic-gate 		return (BAM_ERROR);
14760Sstevel@tonic-gate 	}
14770Sstevel@tonic-gate 	return (BAM_SUCCESS);
14780Sstevel@tonic-gate }
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate static error_t
14810Sstevel@tonic-gate read_list(char *root, filelist_t  *flistp)
14820Sstevel@tonic-gate {
14830Sstevel@tonic-gate 	int rval;
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	/*
14880Sstevel@tonic-gate 	 * Read current lists of files - only the first is mandatory
14890Sstevel@tonic-gate 	 */
14900Sstevel@tonic-gate 	rval = read_one_list(root, flistp, BOOT_FILE_LIST);
14910Sstevel@tonic-gate 	if (rval != BAM_SUCCESS)
14920Sstevel@tonic-gate 		return (rval);
14930Sstevel@tonic-gate 	(void) read_one_list(root, flistp, ETC_FILE_LIST);
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	if (flistp->head == NULL) {
14960Sstevel@tonic-gate 		bam_error(NO_FLIST);
14970Sstevel@tonic-gate 		return (BAM_ERROR);
14980Sstevel@tonic-gate 	}
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 	return (BAM_SUCCESS);
15010Sstevel@tonic-gate }
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate static void
15040Sstevel@tonic-gate getoldstat(char *root)
15050Sstevel@tonic-gate {
15060Sstevel@tonic-gate 	char path[PATH_MAX];
15070Sstevel@tonic-gate 	int fd, error;
15080Sstevel@tonic-gate 	struct stat sb;
15090Sstevel@tonic-gate 	char *ostat;
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
15120Sstevel@tonic-gate 	fd = open(path, O_RDONLY);
15130Sstevel@tonic-gate 	if (fd == -1) {
15140Sstevel@tonic-gate 		if (bam_verbose)
15150Sstevel@tonic-gate 			bam_print(OPEN_FAIL, path, strerror(errno));
15160Sstevel@tonic-gate 		walk_arg.need_update = 1;
15170Sstevel@tonic-gate 		return;
15180Sstevel@tonic-gate 	}
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	if (fstat(fd, &sb) != 0) {
15210Sstevel@tonic-gate 		bam_error(STAT_FAIL, path, strerror(errno));
15220Sstevel@tonic-gate 		(void) close(fd);
15230Sstevel@tonic-gate 		walk_arg.need_update = 1;
15240Sstevel@tonic-gate 		return;
15250Sstevel@tonic-gate 	}
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	ostat = s_calloc(1, sb.st_size);
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	if (read(fd, ostat, sb.st_size) != sb.st_size) {
15300Sstevel@tonic-gate 		bam_error(READ_FAIL, path, strerror(errno));
15310Sstevel@tonic-gate 		(void) close(fd);
15320Sstevel@tonic-gate 		free(ostat);
15330Sstevel@tonic-gate 		walk_arg.need_update = 1;
15340Sstevel@tonic-gate 		return;
15350Sstevel@tonic-gate 	}
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	(void) close(fd);
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
15400Sstevel@tonic-gate 	error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0);
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	free(ostat);
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	if (error) {
15450Sstevel@tonic-gate 		bam_error(UNPACK_FAIL, path, strerror(error));
15460Sstevel@tonic-gate 		walk_arg.old_nvlp = NULL;
15470Sstevel@tonic-gate 		walk_arg.need_update = 1;
15480Sstevel@tonic-gate 		return;
15490Sstevel@tonic-gate 	}
15500Sstevel@tonic-gate }
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate static void
15530Sstevel@tonic-gate create_newstat(void)
15540Sstevel@tonic-gate {
15550Sstevel@tonic-gate 	int error;
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0);
15580Sstevel@tonic-gate 	if (error) {
15590Sstevel@tonic-gate 		/*
15600Sstevel@tonic-gate 		 * Not fatal - we can still create archive
15610Sstevel@tonic-gate 		 */
15620Sstevel@tonic-gate 		walk_arg.new_nvlp = NULL;
15630Sstevel@tonic-gate 		bam_error(NVALLOC_FAIL, strerror(error));
15640Sstevel@tonic-gate 	}
15650Sstevel@tonic-gate }
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate static void
15680Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp)
15690Sstevel@tonic-gate {
15700Sstevel@tonic-gate 	char path[PATH_MAX];
15710Sstevel@tonic-gate 	line_t *lp;
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
15740Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, lp->line);
15750Sstevel@tonic-gate 		/* XXX shouldn't we use FTW_MOUNT ? */
15760Sstevel@tonic-gate 		if (nftw(path, cmpstat, 20, 0) == -1) {
15770Sstevel@tonic-gate 			/*
15780Sstevel@tonic-gate 			 * Some files may not exist.
15790Sstevel@tonic-gate 			 * For example: etc/rtc_config on a x86 diskless system
15800Sstevel@tonic-gate 			 * Emit verbose message only
15810Sstevel@tonic-gate 			 */
15820Sstevel@tonic-gate 			if (bam_verbose)
15830Sstevel@tonic-gate 				bam_print(NFTW_FAIL, path, strerror(errno));
15840Sstevel@tonic-gate 		}
15850Sstevel@tonic-gate 	}
15860Sstevel@tonic-gate }
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate static void
15890Sstevel@tonic-gate savenew(char *root)
15900Sstevel@tonic-gate {
15910Sstevel@tonic-gate 	char path[PATH_MAX];
15920Sstevel@tonic-gate 	char path2[PATH_MAX];
15930Sstevel@tonic-gate 	size_t sz;
15940Sstevel@tonic-gate 	char *nstat;
15950Sstevel@tonic-gate 	int fd, wrote, error;
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	nstat = NULL;
15980Sstevel@tonic-gate 	sz = 0;
15990Sstevel@tonic-gate 	error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz,
16000Sstevel@tonic-gate 	    NV_ENCODE_XDR, 0);
16010Sstevel@tonic-gate 	if (error) {
16020Sstevel@tonic-gate 		bam_error(PACK_FAIL, strerror(error));
16030Sstevel@tonic-gate 		return;
16040Sstevel@tonic-gate 	}
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP);
16070Sstevel@tonic-gate 	fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE);
16080Sstevel@tonic-gate 	if (fd == -1) {
16090Sstevel@tonic-gate 		bam_error(OPEN_FAIL, path, strerror(errno));
16100Sstevel@tonic-gate 		free(nstat);
16110Sstevel@tonic-gate 		return;
16120Sstevel@tonic-gate 	}
16130Sstevel@tonic-gate 	wrote = write(fd, nstat, sz);
16140Sstevel@tonic-gate 	if (wrote != sz) {
16150Sstevel@tonic-gate 		bam_error(WRITE_FAIL, path, strerror(errno));
16160Sstevel@tonic-gate 		(void) close(fd);
16170Sstevel@tonic-gate 		free(nstat);
16180Sstevel@tonic-gate 		return;
16190Sstevel@tonic-gate 	}
16200Sstevel@tonic-gate 	(void) close(fd);
16210Sstevel@tonic-gate 	free(nstat);
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 	(void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT);
16240Sstevel@tonic-gate 	if (rename(path, path2) != 0) {
16250Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path2, strerror(errno));
16260Sstevel@tonic-gate 	}
16270Sstevel@tonic-gate }
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate static void
16300Sstevel@tonic-gate clear_walk_args(void)
16310Sstevel@tonic-gate {
16320Sstevel@tonic-gate 	if (walk_arg.old_nvlp)
16330Sstevel@tonic-gate 		nvlist_free(walk_arg.old_nvlp);
16340Sstevel@tonic-gate 	if (walk_arg.new_nvlp)
16350Sstevel@tonic-gate 		nvlist_free(walk_arg.new_nvlp);
16360Sstevel@tonic-gate 	walk_arg.need_update = 0;
16370Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
16380Sstevel@tonic-gate 	walk_arg.new_nvlp = NULL;
16390Sstevel@tonic-gate }
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate /*
16420Sstevel@tonic-gate  * Returns:
16430Sstevel@tonic-gate  *	0 - no update necessary
16440Sstevel@tonic-gate  *	1 - update required.
16450Sstevel@tonic-gate  *	BAM_ERROR (-1) - An error occurred
16460Sstevel@tonic-gate  *
16470Sstevel@tonic-gate  * Special handling for check (-n):
16480Sstevel@tonic-gate  * ================================
16490Sstevel@tonic-gate  * The check (-n) option produces parseable output.
16500Sstevel@tonic-gate  * To do this, we suppress all stdout messages unrelated
16510Sstevel@tonic-gate  * to out of sync files.
16520Sstevel@tonic-gate  * All stderr messages are still printed though.
16530Sstevel@tonic-gate  *
16540Sstevel@tonic-gate  */
16550Sstevel@tonic-gate static int
16560Sstevel@tonic-gate update_required(char *root)
16570Sstevel@tonic-gate {
16580Sstevel@tonic-gate 	struct stat sb;
16590Sstevel@tonic-gate 	char path[PATH_MAX];
16600Sstevel@tonic-gate 	filelist_t flist;
16610Sstevel@tonic-gate 	filelist_t *flistp = &flist;
16620Sstevel@tonic-gate 	int need_update;
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 	walk_arg.need_update = 0;
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 	/*
16690Sstevel@tonic-gate 	 * Without consulting stat data, check if we need update
16700Sstevel@tonic-gate 	 */
16710Sstevel@tonic-gate 	check_flags_and_files(root);
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 	/*
16740Sstevel@tonic-gate 	 * In certain deployment scenarios, filestat may not
16750Sstevel@tonic-gate 	 * exist. Ignore it during boot-archive SMF check.
16760Sstevel@tonic-gate 	 */
16770Sstevel@tonic-gate 	if (bam_smf_check) {
16780Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
16790Sstevel@tonic-gate 		if (stat(path, &sb) != 0)
16800Sstevel@tonic-gate 			return (0);
16810Sstevel@tonic-gate 	}
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 	/*
16840Sstevel@tonic-gate 	 * consult stat data only if we haven't made a decision
16850Sstevel@tonic-gate 	 * about update. If checking (-n) however, we always
16860Sstevel@tonic-gate 	 * need stat data (since we want to compare old and new)
16870Sstevel@tonic-gate 	 */
16880Sstevel@tonic-gate 	if (!walk_arg.need_update || bam_check)
16890Sstevel@tonic-gate 		getoldstat(root);
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate 	/*
16920Sstevel@tonic-gate 	 * read list of files
16930Sstevel@tonic-gate 	 */
16940Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
16950Sstevel@tonic-gate 		clear_walk_args();
16960Sstevel@tonic-gate 		return (BAM_ERROR);
16970Sstevel@tonic-gate 	}
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
17000Sstevel@tonic-gate 
17010Sstevel@tonic-gate 	/*
17020Sstevel@tonic-gate 	 * At this point either the update is required
17030Sstevel@tonic-gate 	 * or the decision is pending. In either case
17040Sstevel@tonic-gate 	 * we need to create new stat nvlist
17050Sstevel@tonic-gate 	 */
17060Sstevel@tonic-gate 	create_newstat();
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 	/*
17090Sstevel@tonic-gate 	 * This walk does 2 things:
17100Sstevel@tonic-gate 	 *  	- gets new stat data for every file
17110Sstevel@tonic-gate 	 *	- (optional) compare old and new stat data
17120Sstevel@tonic-gate 	 */
17130Sstevel@tonic-gate 	walk_list(root, &flist);
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 	/* done with the file list */
17160Sstevel@tonic-gate 	filelist_free(flistp);
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 	/*
17190Sstevel@tonic-gate 	 * if we didn't succeed in  creating new stat data above
17200Sstevel@tonic-gate 	 * just return result of update check so that archive is built.
17210Sstevel@tonic-gate 	 */
17220Sstevel@tonic-gate 	if (walk_arg.new_nvlp == NULL) {
17230Sstevel@tonic-gate 		bam_error(NO_NEW_STAT);
17240Sstevel@tonic-gate 		need_update = walk_arg.need_update;
17250Sstevel@tonic-gate 		clear_walk_args();
17260Sstevel@tonic-gate 		return (need_update ? 1 : 0);
17270Sstevel@tonic-gate 	}
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 	/*
17310Sstevel@tonic-gate 	 * If no update required, discard newstat
17320Sstevel@tonic-gate 	 */
17330Sstevel@tonic-gate 	if (!walk_arg.need_update) {
17340Sstevel@tonic-gate 		clear_walk_args();
17350Sstevel@tonic-gate 		return (0);
17360Sstevel@tonic-gate 	}
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 	/*
17390Sstevel@tonic-gate 	 * At this point we need an update - so save new stat data
17400Sstevel@tonic-gate 	 * However, if only checking (-n), don't save new stat data.
17410Sstevel@tonic-gate 	 */
17420Sstevel@tonic-gate 	if (!bam_check)
17430Sstevel@tonic-gate 		savenew(root);
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 	clear_walk_args();
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 	return (1);
17480Sstevel@tonic-gate }
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate static error_t
17510Sstevel@tonic-gate create_ramdisk(char *root)
17520Sstevel@tonic-gate {
17530Sstevel@tonic-gate 	char *cmdline, path[PATH_MAX];
17540Sstevel@tonic-gate 	size_t len;
17550Sstevel@tonic-gate 	struct stat sb;
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 	/*
17580Sstevel@tonic-gate 	 * Setup command args for create_ramdisk.ksh
17590Sstevel@tonic-gate 	 */
17600Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, CREATE_RAMDISK);
17610Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
17620Sstevel@tonic-gate 		bam_error(ARCH_EXEC_MISS, path, strerror(errno));
17630Sstevel@tonic-gate 		return (BAM_ERROR);
17640Sstevel@tonic-gate 	}
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate 	len = strlen(path) + strlen(root) + 10;	/* room for space + -R */
17670Sstevel@tonic-gate 	cmdline = s_calloc(1, len);
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate 	if (strlen(root) > 1) {
17700Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s -R %s", path, root);
17710Sstevel@tonic-gate 		/* chop off / at the end */
17720Sstevel@tonic-gate 		cmdline[strlen(cmdline) - 1] = '\0';
17730Sstevel@tonic-gate 	} else
17740Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s", path);
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	if (exec_cmd(cmdline, NULL, 0) != 0) {
17770Sstevel@tonic-gate 		bam_error(ARCHIVE_FAIL, cmdline);
17780Sstevel@tonic-gate 		free(cmdline);
17790Sstevel@tonic-gate 		return (BAM_ERROR);
17800Sstevel@tonic-gate 	}
17810Sstevel@tonic-gate 	free(cmdline);
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 	/*
17840Sstevel@tonic-gate 	 * Verify that the archive has been created
17850Sstevel@tonic-gate 	 */
17860Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE);
17870Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
17880Sstevel@tonic-gate 		bam_error(ARCHIVE_NOT_CREATED, path);
17890Sstevel@tonic-gate 		return (BAM_ERROR);
17900Sstevel@tonic-gate 	}
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 	return (BAM_SUCCESS);
17930Sstevel@tonic-gate }
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate /*
17960Sstevel@tonic-gate  * Checks if target filesystem is on a ramdisk
17970Sstevel@tonic-gate  * 1 - is miniroot
17980Sstevel@tonic-gate  * 0 - is not
17990Sstevel@tonic-gate  * When in doubt assume it is not a ramdisk.
18000Sstevel@tonic-gate  */
18010Sstevel@tonic-gate static int
18020Sstevel@tonic-gate is_ramdisk(char *root)
18030Sstevel@tonic-gate {
18040Sstevel@tonic-gate 	struct extmnttab mnt;
18050Sstevel@tonic-gate 	FILE *fp;
18060Sstevel@tonic-gate 	int found;
1807316Svikram 	char mntpt[PATH_MAX];
1808316Svikram 	char *cp;
18090Sstevel@tonic-gate 
18100Sstevel@tonic-gate 	/*
18110Sstevel@tonic-gate 	 * There are 3 situations where creating archive is
18120Sstevel@tonic-gate 	 * of dubious value:
1813316Svikram 	 *	- create boot_archive on a lofi-mounted boot_archive
18140Sstevel@tonic-gate 	 *	- create it on a ramdisk which is the root filesystem
18150Sstevel@tonic-gate 	 *	- create it on a ramdisk mounted somewhere else
18160Sstevel@tonic-gate 	 * The first is not easy to detect and checking for it is not
18170Sstevel@tonic-gate 	 * worth it.
18180Sstevel@tonic-gate 	 * The other two conditions are handled here
18190Sstevel@tonic-gate 	 */
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
18220Sstevel@tonic-gate 	if (fp == NULL) {
18230Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
18240Sstevel@tonic-gate 		return (0);
18250Sstevel@tonic-gate 	}
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 	resetmnttab(fp);
18280Sstevel@tonic-gate 
1829316Svikram 	/*
1830316Svikram 	 * Remove any trailing / from the mount point
1831316Svikram 	 */
1832316Svikram 	(void) strlcpy(mntpt, root, sizeof (mntpt));
1833316Svikram 	if (strcmp(root, "/") != 0) {
1834316Svikram 		cp = mntpt + strlen(mntpt) - 1;
1835316Svikram 		if (*cp == '/')
1836316Svikram 			*cp = '\0';
1837316Svikram 	}
18380Sstevel@tonic-gate 	found = 0;
18390Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
1840316Svikram 		if (strcmp(mnt.mnt_mountp, mntpt) == 0) {
18410Sstevel@tonic-gate 			found = 1;
18420Sstevel@tonic-gate 			break;
18430Sstevel@tonic-gate 		}
18440Sstevel@tonic-gate 	}
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 	if (!found) {
18470Sstevel@tonic-gate 		if (bam_verbose)
1848316Svikram 			bam_error(NOT_IN_MNTTAB, mntpt);
18490Sstevel@tonic-gate 		(void) fclose(fp);
18500Sstevel@tonic-gate 		return (0);
18510Sstevel@tonic-gate 	}
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 	if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) {
18540Sstevel@tonic-gate 		if (bam_verbose)
18550Sstevel@tonic-gate 			bam_error(IS_RAMDISK, bam_root);
18560Sstevel@tonic-gate 		(void) fclose(fp);
18570Sstevel@tonic-gate 		return (1);
18580Sstevel@tonic-gate 	}
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 	(void) fclose(fp);
18610Sstevel@tonic-gate 
18620Sstevel@tonic-gate 	return (0);
18630Sstevel@tonic-gate }
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate static int
18660Sstevel@tonic-gate is_newboot(char *root)
18670Sstevel@tonic-gate {
18680Sstevel@tonic-gate 	char path[PATH_MAX];
18690Sstevel@tonic-gate 	struct stat sb;
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 	/*
18720Sstevel@tonic-gate 	 * We can't boot without MULTI_BOOT
18730Sstevel@tonic-gate 	 */
18740Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, MULTI_BOOT);
18750Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
18760Sstevel@tonic-gate 		if (bam_verbose)
18770Sstevel@tonic-gate 			bam_print(FILE_MISS, path);
18780Sstevel@tonic-gate 		return (0);
18790Sstevel@tonic-gate 	}
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate 	/*
18820Sstevel@tonic-gate 	 * We can't generate archive without GRUB_DIR
18830Sstevel@tonic-gate 	 */
18840Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, GRUB_DIR);
18850Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
18860Sstevel@tonic-gate 		if (bam_verbose)
18870Sstevel@tonic-gate 			bam_print(DIR_MISS, path);
18880Sstevel@tonic-gate 		return (0);
18890Sstevel@tonic-gate 	}
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate 	return (1);
18920Sstevel@tonic-gate }
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate static int
18950Sstevel@tonic-gate is_readonly(char *root)
18960Sstevel@tonic-gate {
18970Sstevel@tonic-gate 	struct statvfs vfs;
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate 	/*
19000Sstevel@tonic-gate 	 * Check for RDONLY filesystem
19010Sstevel@tonic-gate 	 * When in doubt assume it is not readonly
19020Sstevel@tonic-gate 	 */
19030Sstevel@tonic-gate 	if (statvfs(root, &vfs) != 0) {
19040Sstevel@tonic-gate 		if (bam_verbose)
19050Sstevel@tonic-gate 			bam_error(STATVFS_FAIL, root, strerror(errno));
19060Sstevel@tonic-gate 		return (0);
19070Sstevel@tonic-gate 	}
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	if (vfs.f_flag & ST_RDONLY) {
19100Sstevel@tonic-gate 		return (1);
19110Sstevel@tonic-gate 	}
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 	return (0);
19140Sstevel@tonic-gate }
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate static error_t
19170Sstevel@tonic-gate update_archive(char *root, char *opt)
19180Sstevel@tonic-gate {
19190Sstevel@tonic-gate 	error_t ret;
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	assert(root);
19220Sstevel@tonic-gate 	assert(opt == NULL);
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 	/*
1925316Svikram 	 * root must belong to a GRUB boot OS,
19260Sstevel@tonic-gate 	 * don't care on sparc except for diskless clients
19270Sstevel@tonic-gate 	 */
19280Sstevel@tonic-gate 	if (!is_newboot(root)) {
1929316Svikram 		/*
1930316Svikram 		 * Emit message only if not in context of update_all.
1931316Svikram 		 * If in update_all, emit only if verbose flag is set.
1932316Svikram 		 */
1933316Svikram 		if (!bam_update_all || bam_verbose)
1934316Svikram 			bam_print(NOT_GRUB_BOOT, root);
19350Sstevel@tonic-gate 		return (BAM_SUCCESS);
19360Sstevel@tonic-gate 	}
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 	/*
1939662Sszhou 	 * If smf check is requested when / is writable (can happen
1940662Sszhou 	 * on first reboot following an upgrade because service
1941662Sszhou 	 * dependency is messed up), skip the check.
1942662Sszhou 	 */
1943662Sszhou 	if (bam_smf_check && !bam_root_readonly)
1944662Sszhou 		return (BAM_SUCCESS);
1945662Sszhou 
1946662Sszhou 	/*
1947662Sszhou 	 * root must be writable. This check applies to alternate
1948662Sszhou 	 * root (-R option); bam_root_readonly applies to '/' only.
19490Sstevel@tonic-gate 	 * Note: statvfs() does not always report the truth
19500Sstevel@tonic-gate 	 */
1951756Ssetje 	if (!bam_smf_check && !bam_check && is_readonly(root)) {
1952662Sszhou 		if (bam_verbose)
19530Sstevel@tonic-gate 			bam_print(RDONLY_FS, root);
19540Sstevel@tonic-gate 		return (BAM_SUCCESS);
19550Sstevel@tonic-gate 	}
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate 	/*
19580Sstevel@tonic-gate 	 * Don't generate archive on ramdisk
19590Sstevel@tonic-gate 	 */
19600Sstevel@tonic-gate 	if (is_ramdisk(root)) {
19610Sstevel@tonic-gate 		if (bam_verbose)
19620Sstevel@tonic-gate 			bam_print(SKIP_RAMDISK);
19630Sstevel@tonic-gate 		return (BAM_SUCCESS);
19640Sstevel@tonic-gate 	}
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 	/*
19670Sstevel@tonic-gate 	 * Now check if updated is really needed
19680Sstevel@tonic-gate 	 */
19690Sstevel@tonic-gate 	ret = update_required(root);
19700Sstevel@tonic-gate 
19710Sstevel@tonic-gate 	/*
19720Sstevel@tonic-gate 	 * The check command (-n) is *not* a dry run
19730Sstevel@tonic-gate 	 * It only checks if the archive is in sync.
19740Sstevel@tonic-gate 	 */
19750Sstevel@tonic-gate 	if (bam_check) {
19760Sstevel@tonic-gate 		bam_exit((ret != 0) ? 1 : 0);
19770Sstevel@tonic-gate 	}
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate 	if (ret == 1) {
19800Sstevel@tonic-gate 		/* create the ramdisk */
19810Sstevel@tonic-gate 		ret = create_ramdisk(root);
19820Sstevel@tonic-gate 	}
19830Sstevel@tonic-gate 	return (ret);
19840Sstevel@tonic-gate }
19850Sstevel@tonic-gate 
1986316Svikram static void
1987*1746Svikram update_fdisk(void)
1988*1746Svikram {
1989*1746Svikram 	struct stat sb;
1990*1746Svikram 	char cmd[PATH_MAX];
1991*1746Svikram 	int ret1, ret2;
1992*1746Svikram 
1993*1746Svikram 	assert(stat(GRUB_fdisk, &sb) == 0);
1994*1746Svikram 	assert(stat(GRUB_fdisk_target, &sb) == 0);
1995*1746Svikram 
1996*1746Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/fdisk -F %s `/bin/cat %s`",
1997*1746Svikram 	    GRUB_fdisk, GRUB_fdisk_target);
1998*1746Svikram 
1999*1746Svikram 	bam_print(UPDATING_FDISK);
2000*1746Svikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2001*1746Svikram 		bam_error(FDISK_UPDATE_FAILED);
2002*1746Svikram 	}
2003*1746Svikram 
2004*1746Svikram 	/*
2005*1746Svikram 	 * We are done, remove the files.
2006*1746Svikram 	 */
2007*1746Svikram 	ret1 = unlink(GRUB_fdisk);
2008*1746Svikram 	ret2 = unlink(GRUB_fdisk_target);
2009*1746Svikram 	if (ret1 != 0 || ret2 != 0) {
2010*1746Svikram 		bam_error(FILE_REMOVE_FAILED, GRUB_fdisk, GRUB_fdisk_target);
2011*1746Svikram 	}
2012*1746Svikram }
2013*1746Svikram 
2014*1746Svikram static void
2015316Svikram restore_grub_slice(void)
2016316Svikram {
2017316Svikram 	struct stat sb;
2018316Svikram 	char *mntpt, *physlice;
2019316Svikram 	int mnted;	/* set if we did a mount */
2020316Svikram 	char menupath[PATH_MAX], cmd[PATH_MAX];
2021316Svikram 
2022316Svikram 	if (stat(GRUB_slice, &sb) != 0) {
2023316Svikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
2024316Svikram 		return;
2025316Svikram 	}
2026316Svikram 
2027316Svikram 	/*
2028316Svikram 	 * If we are doing an luactivate, don't attempt to restore GRUB or else
2029316Svikram 	 * we may not be able to get to DCA boot environments. Let luactivate
2030316Svikram 	 * handle GRUB/DCA installation
2031316Svikram 	 */
2032316Svikram 	if (stat(LU_ACTIVATE_FILE, &sb) == 0) {
2033316Svikram 		return;
2034316Svikram 	}
2035316Svikram 
2036316Svikram 	mnted = 0;
2037316Svikram 	physlice = NULL;
2038621Svikram 	mntpt = mount_grub_slice(&mnted, &physlice, NULL, NULL);
2039316Svikram 	if (mntpt == NULL) {
2040316Svikram 		bam_error(CANNOT_RESTORE_GRUB_SLICE);
2041316Svikram 		return;
2042316Svikram 	}
2043316Svikram 
2044316Svikram 	(void) snprintf(menupath, sizeof (menupath), "%s%s", mntpt, GRUB_MENU);
2045316Svikram 	if (stat(menupath, &sb) == 0) {
2046621Svikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2047316Svikram 		return;
2048316Svikram 	}
2049316Svikram 
2050316Svikram 	/*
2051316Svikram 	 * The menu is missing - we need to do a restore
2052316Svikram 	 */
2053316Svikram 	bam_print(RESTORING_GRUB);
2054316Svikram 
2055316Svikram 	(void) snprintf(cmd, sizeof (cmd), "%s %s %s %s",
2056316Svikram 	    INSTALLGRUB, STAGE1, STAGE2, physlice);
2057316Svikram 
2058316Svikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2059316Svikram 		bam_error(RESTORE_GRUB_FAILED);
2060621Svikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2061316Svikram 		return;
2062316Svikram 	}
2063316Svikram 
2064316Svikram 	if (stat(GRUB_backup_menu, &sb) != 0) {
2065316Svikram 		bam_error(MISSING_BACKUP_MENU,
2066316Svikram 		    GRUB_backup_menu, strerror(errno));
2067621Svikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2068316Svikram 		return;
2069316Svikram 	}
2070316Svikram 
2071316Svikram 	(void) snprintf(cmd, sizeof (cmd), "/bin/cp %s %s",
2072316Svikram 	    GRUB_backup_menu, menupath);
2073316Svikram 
2074316Svikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2075316Svikram 		bam_error(RESTORE_MENU_FAILED, menupath);
2076621Svikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2077316Svikram 		return;
2078316Svikram 	}
2079316Svikram 
2080316Svikram 	/* Success */
2081621Svikram 	umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2082316Svikram }
2083316Svikram 
20840Sstevel@tonic-gate static error_t
20850Sstevel@tonic-gate update_all(char *root, char *opt)
20860Sstevel@tonic-gate {
20870Sstevel@tonic-gate 	struct extmnttab mnt;
20880Sstevel@tonic-gate 	struct stat sb;
20890Sstevel@tonic-gate 	FILE *fp;
20900Sstevel@tonic-gate 	char multibt[PATH_MAX];
20910Sstevel@tonic-gate 	error_t ret = BAM_SUCCESS;
2092*1746Svikram 	int ret1, ret2;
20930Sstevel@tonic-gate 
2094621Svikram 	assert(root);
20950Sstevel@tonic-gate 	assert(opt == NULL);
20960Sstevel@tonic-gate 
2097621Svikram 	if (bam_rootlen != 1 || *root != '/') {
2098621Svikram 		elide_trailing_slash(root, multibt, sizeof (multibt));
2099621Svikram 		bam_error(ALT_ROOT_INVALID, multibt);
2100621Svikram 		return (BAM_ERROR);
2101621Svikram 	}
2102621Svikram 
21030Sstevel@tonic-gate 	/*
21040Sstevel@tonic-gate 	 * First update archive for current root
21050Sstevel@tonic-gate 	 */
21060Sstevel@tonic-gate 	if (update_archive(root, opt) != BAM_SUCCESS)
21070Sstevel@tonic-gate 		ret = BAM_ERROR;
21080Sstevel@tonic-gate 
21090Sstevel@tonic-gate 	/*
21100Sstevel@tonic-gate 	 * Now walk the mount table, performing archive update
21110Sstevel@tonic-gate 	 * for all mounted Newboot root filesystems
21120Sstevel@tonic-gate 	 */
21130Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
21140Sstevel@tonic-gate 	if (fp == NULL) {
21150Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
2116316Svikram 		ret = BAM_ERROR;
2117316Svikram 		goto out;
21180Sstevel@tonic-gate 	}
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate 	resetmnttab(fp);
21210Sstevel@tonic-gate 
21220Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
21230Sstevel@tonic-gate 		if (mnt.mnt_special == NULL)
21240Sstevel@tonic-gate 			continue;
21250Sstevel@tonic-gate 		if (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0)
21260Sstevel@tonic-gate 			continue;
21270Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, "/") == 0)
21280Sstevel@tonic-gate 			continue;
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 		(void) snprintf(multibt, sizeof (multibt), "%s%s",
21310Sstevel@tonic-gate 		    mnt.mnt_mountp, MULTI_BOOT);
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate 		if (stat(multibt, &sb) == -1)
21340Sstevel@tonic-gate 			continue;
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 		/*
21370Sstevel@tonic-gate 		 * We put a trailing slash to be consistent with root = "/"
21380Sstevel@tonic-gate 		 * case, such that we don't have to print // in some cases.
21390Sstevel@tonic-gate 		 */
21400Sstevel@tonic-gate 		(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
21410Sstevel@tonic-gate 		    mnt.mnt_mountp);
21420Sstevel@tonic-gate 		bam_rootlen = strlen(rootbuf);
21430Sstevel@tonic-gate 		if (update_archive(rootbuf, opt) != BAM_SUCCESS)
21440Sstevel@tonic-gate 			ret = BAM_ERROR;
21450Sstevel@tonic-gate 	}
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate 	(void) fclose(fp);
21480Sstevel@tonic-gate 
2149316Svikram out:
2150316Svikram 	if (stat(GRUB_slice, &sb) == 0) {
2151316Svikram 		restore_grub_slice();
2152316Svikram 	}
2153316Svikram 
2154*1746Svikram 	/*
2155*1746Svikram 	 * Update fdisk table as we go down. Updating it when
2156*1746Svikram 	 * the system is running will confuse biosdev.
2157*1746Svikram 	 */
2158*1746Svikram 	ret1 = stat(GRUB_fdisk, &sb);
2159*1746Svikram 	ret2 = stat(GRUB_fdisk_target, &sb);
2160*1746Svikram 	if ((ret1 == 0) && (ret2 == 0)) {
2161*1746Svikram 		update_fdisk();
2162*1746Svikram 	} else if ((ret1 == 0) ^ (ret2 == 0)) {
2163*1746Svikram 		/*
2164*1746Svikram 		 * It is an error for one file to be
2165*1746Svikram 		 * present and the other absent.
2166*1746Svikram 		 * It is normal for both files to be
2167*1746Svikram 		 * absent - it indicates that no fdisk
2168*1746Svikram 		 * update is required.
2169*1746Svikram 		 */
2170*1746Svikram 		bam_error(MISSING_FDISK_FILE,
2171*1746Svikram 		    ret1 ? GRUB_fdisk : GRUB_fdisk_target);
2172*1746Svikram 		ret = BAM_ERROR;
2173*1746Svikram 	}
2174*1746Svikram 
21750Sstevel@tonic-gate 	return (ret);
21760Sstevel@tonic-gate }
21770Sstevel@tonic-gate 
21780Sstevel@tonic-gate static void
21790Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp)
21800Sstevel@tonic-gate {
21810Sstevel@tonic-gate 	if (mp->start == NULL) {
21820Sstevel@tonic-gate 		mp->start = lp;
21830Sstevel@tonic-gate 	} else {
21840Sstevel@tonic-gate 		mp->end->next = lp;
2185662Sszhou 		lp->prev = mp->end;
21860Sstevel@tonic-gate 	}
21870Sstevel@tonic-gate 	mp->end = lp;
21880Sstevel@tonic-gate }
21890Sstevel@tonic-gate 
2190662Sszhou static void
2191662Sszhou unlink_line(menu_t *mp, line_t *lp)
2192662Sszhou {
2193662Sszhou 	/* unlink from list */
2194662Sszhou 	if (lp->prev)
2195662Sszhou 		lp->prev->next = lp->next;
2196662Sszhou 	else
2197662Sszhou 		mp->start = lp->next;
2198662Sszhou 	if (lp->next)
2199662Sszhou 		lp->next->prev = lp->prev;
2200662Sszhou 	else
2201662Sszhou 		mp->end = lp->prev;
2202662Sszhou }
2203662Sszhou 
2204662Sszhou static entry_t *
2205662Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end)
2206662Sszhou {
2207662Sszhou 	entry_t *ent, *prev;
2208662Sszhou 
2209662Sszhou 	ent = s_calloc(1, sizeof (entry_t));
2210662Sszhou 	ent->start = start;
2211662Sszhou 	ent->end = end;
2212662Sszhou 
2213662Sszhou 	if (mp->entries == NULL) {
2214662Sszhou 		mp->entries = ent;
2215662Sszhou 		return (ent);
2216662Sszhou 	}
2217662Sszhou 
2218662Sszhou 	prev = mp->entries;
2219662Sszhou 	while (prev->next)
2220662Sszhou 		prev = prev-> next;
2221662Sszhou 	prev->next = ent;
2222662Sszhou 	ent->prev = prev;
2223662Sszhou 	return (ent);
2224662Sszhou }
2225662Sszhou 
2226662Sszhou static void
2227662Sszhou boot_entry_addline(entry_t *ent, line_t *lp)
2228662Sszhou {
2229662Sszhou 	if (ent)
2230662Sszhou 		ent->end = lp;
2231662Sszhou }
2232662Sszhou 
22330Sstevel@tonic-gate /*
22340Sstevel@tonic-gate  * A line in menu.lst looks like
22350Sstevel@tonic-gate  * [ ]*<cmd>[ \t=]*<arg>*
22360Sstevel@tonic-gate  */
22370Sstevel@tonic-gate static void
22380Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum)
22390Sstevel@tonic-gate {
22400Sstevel@tonic-gate 	/*
22410Sstevel@tonic-gate 	 * save state across calls. This is so that
22420Sstevel@tonic-gate 	 * header gets the right entry# after title has
22430Sstevel@tonic-gate 	 * been processed
22440Sstevel@tonic-gate 	 */
2245662Sszhou 	static line_t *prev = NULL;
2246662Sszhou 	static entry_t *curr_ent = NULL;
22470Sstevel@tonic-gate 
22480Sstevel@tonic-gate 	line_t	*lp;
22490Sstevel@tonic-gate 	char *cmd, *sep, *arg;
22500Sstevel@tonic-gate 	char save, *cp, *line;
22510Sstevel@tonic-gate 	menu_flag_t flag = BAM_INVALID;
22520Sstevel@tonic-gate 
22530Sstevel@tonic-gate 	if (str == NULL) {
22540Sstevel@tonic-gate 		return;
22550Sstevel@tonic-gate 	}
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 	/*
22580Sstevel@tonic-gate 	 * First save a copy of the entire line.
22590Sstevel@tonic-gate 	 * We use this later to set the line field.
22600Sstevel@tonic-gate 	 */
22610Sstevel@tonic-gate 	line = s_strdup(str);
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate 	/* Eat up leading whitespace */
22640Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
22650Sstevel@tonic-gate 		str++;
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate 	if (*str == '#') {		/* comment */
22680Sstevel@tonic-gate 		cmd = s_strdup("#");
22690Sstevel@tonic-gate 		sep = NULL;
22700Sstevel@tonic-gate 		arg = s_strdup(str + 1);
22710Sstevel@tonic-gate 		flag = BAM_COMMENT;
22720Sstevel@tonic-gate 	} else if (*str == '\0') {	/* blank line */
22730Sstevel@tonic-gate 		cmd = sep = arg = NULL;
22740Sstevel@tonic-gate 		flag = BAM_EMPTY;
22750Sstevel@tonic-gate 	} else {
22760Sstevel@tonic-gate 		/*
22770Sstevel@tonic-gate 		 * '=' is not a documented separator in grub syntax.
22780Sstevel@tonic-gate 		 * However various development bits use '=' as a
22790Sstevel@tonic-gate 		 * separator. In addition, external users also
22800Sstevel@tonic-gate 		 * use = as a separator. So we will allow that usage.
22810Sstevel@tonic-gate 		 */
22820Sstevel@tonic-gate 		cp = str;
22830Sstevel@tonic-gate 		while (*str != ' ' && *str != '\t' && *str != '=') {
22840Sstevel@tonic-gate 			if (*str == '\0') {
22850Sstevel@tonic-gate 				cmd = s_strdup(cp);
22860Sstevel@tonic-gate 				sep = arg = NULL;
22870Sstevel@tonic-gate 				break;
22880Sstevel@tonic-gate 			}
22890Sstevel@tonic-gate 			str++;
22900Sstevel@tonic-gate 		}
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate 		if (*str != '\0') {
22930Sstevel@tonic-gate 			save = *str;
22940Sstevel@tonic-gate 			*str = '\0';
22950Sstevel@tonic-gate 			cmd = s_strdup(cp);
22960Sstevel@tonic-gate 			*str = save;
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate 			str++;
22990Sstevel@tonic-gate 			save = *str;
23000Sstevel@tonic-gate 			*str = '\0';
23010Sstevel@tonic-gate 			sep = s_strdup(str - 1);
23020Sstevel@tonic-gate 			*str = save;
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 			while (*str == ' ' || *str == '\t')
23050Sstevel@tonic-gate 				str++;
23060Sstevel@tonic-gate 			if (*str == '\0')
23070Sstevel@tonic-gate 				arg = NULL;
23080Sstevel@tonic-gate 			else
23090Sstevel@tonic-gate 				arg = s_strdup(str);
23100Sstevel@tonic-gate 		}
23110Sstevel@tonic-gate 	}
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
23140Sstevel@tonic-gate 
23150Sstevel@tonic-gate 	lp->cmd = cmd;
23160Sstevel@tonic-gate 	lp->sep = sep;
23170Sstevel@tonic-gate 	lp->arg = arg;
23180Sstevel@tonic-gate 	lp->line = line;
23190Sstevel@tonic-gate 	lp->lineNum = ++(*lineNum);
23200Sstevel@tonic-gate 	if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) {
23210Sstevel@tonic-gate 		lp->entryNum = ++(*entryNum);
23220Sstevel@tonic-gate 		lp->flags = BAM_TITLE;
23230Sstevel@tonic-gate 		if (prev && prev->flags == BAM_COMMENT &&
2324662Sszhou 		    prev->arg && strcmp(prev->arg, BAM_HDR) == 0) {
23250Sstevel@tonic-gate 			prev->entryNum = lp->entryNum;
2326662Sszhou 			curr_ent = boot_entry_new(mp, prev, lp);
2327662Sszhou 		} else {
2328662Sszhou 			curr_ent = boot_entry_new(mp, lp, lp);
2329662Sszhou 		}
23300Sstevel@tonic-gate 	} else if (flag != BAM_INVALID) {
23310Sstevel@tonic-gate 		/*
23320Sstevel@tonic-gate 		 * For header comments, the entry# is "fixed up"
23330Sstevel@tonic-gate 		 * by the subsequent title
23340Sstevel@tonic-gate 		 */
23350Sstevel@tonic-gate 		lp->entryNum = *entryNum;
23360Sstevel@tonic-gate 		lp->flags = flag;
23370Sstevel@tonic-gate 	} else {
23380Sstevel@tonic-gate 		lp->entryNum = *entryNum;
23390Sstevel@tonic-gate 		lp->flags = (*entryNum == ENTRY_INIT) ? BAM_GLOBAL : BAM_ENTRY;
23400Sstevel@tonic-gate 	}
23410Sstevel@tonic-gate 
2342662Sszhou 	/* record default, old default, and entry line ranges */
2343662Sszhou 	if (lp->flags == BAM_GLOBAL &&
2344662Sszhou 	    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) {
2345662Sszhou 		mp->curdefault = lp;
2346662Sszhou 	} else if (lp->flags == BAM_COMMENT &&
2347662Sszhou 	    strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) {
2348662Sszhou 		mp->olddefault = lp;
2349662Sszhou 	} else if (lp->flags == BAM_ENTRY ||
2350662Sszhou 	    (lp->flags == BAM_COMMENT && strcmp(lp->arg, BAM_FTR) == 0)) {
2351662Sszhou 		boot_entry_addline(curr_ent, lp);
2352662Sszhou 	}
23530Sstevel@tonic-gate 	append_line(mp, lp);
23540Sstevel@tonic-gate 
23550Sstevel@tonic-gate 	prev = lp;
23560Sstevel@tonic-gate }
23570Sstevel@tonic-gate 
2358621Svikram static void
2359621Svikram update_numbering(menu_t *mp)
2360621Svikram {
2361621Svikram 	int lineNum;
2362621Svikram 	int entryNum;
2363621Svikram 	int old_default_value;
2364621Svikram 	line_t *lp, *prev, *default_lp, *default_entry;
2365621Svikram 	char buf[PATH_MAX];
2366621Svikram 
2367621Svikram 	if (mp->start == NULL) {
2368621Svikram 		return;
2369621Svikram 	}
2370621Svikram 
2371621Svikram 	lineNum = LINE_INIT;
2372621Svikram 	entryNum = ENTRY_INIT;
2373621Svikram 	old_default_value = ENTRY_INIT;
2374621Svikram 	lp = default_lp = default_entry = NULL;
2375621Svikram 
2376621Svikram 	prev = NULL;
2377621Svikram 	for (lp = mp->start; lp; prev = lp, lp = lp->next) {
2378621Svikram 		lp->lineNum = ++lineNum;
2379621Svikram 
2380621Svikram 		/*
2381621Svikram 		 * Get the value of the default command
2382621Svikram 		 */
2383621Svikram 		if (lp->entryNum == ENTRY_INIT && lp->cmd &&
2384621Svikram 		    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 &&
2385621Svikram 		    lp->arg) {
2386621Svikram 			old_default_value = atoi(lp->arg);
2387621Svikram 			default_lp = lp;
2388621Svikram 		}
2389621Svikram 
2390621Svikram 		/*
2391621Svikram 		 * If not boot entry, nothing else to fix for this
2392621Svikram 		 * entry
2393621Svikram 		 */
2394621Svikram 		if (lp->entryNum == ENTRY_INIT)
2395621Svikram 			continue;
2396621Svikram 
2397621Svikram 		/*
2398621Svikram 		 * Record the position of the default entry.
2399621Svikram 		 * The following works because global
2400621Svikram 		 * commands like default and timeout should precede
2401621Svikram 		 * actual boot entries, so old_default_value
2402621Svikram 		 * is already known (or default cmd is missing).
2403621Svikram 		 */
2404621Svikram 		if (default_entry == NULL &&
2405621Svikram 		    old_default_value != ENTRY_INIT &&
2406621Svikram 		    lp->entryNum == old_default_value) {
2407621Svikram 			default_entry = lp;
2408621Svikram 		}
2409621Svikram 
2410621Svikram 		/*
2411621Svikram 		 * Now fixup the entry number
2412621Svikram 		 */
2413621Svikram 		if (lp->cmd && strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) {
2414621Svikram 			lp->entryNum = ++entryNum;
2415621Svikram 			/* fixup the bootadm header */
2416621Svikram 			if (prev && prev->flags == BAM_COMMENT &&
2417621Svikram 			    prev->arg && strcmp(prev->arg, BAM_HDR) == 0) {
2418621Svikram 				prev->entryNum = lp->entryNum;
2419621Svikram 			}
2420621Svikram 		} else {
2421621Svikram 			lp->entryNum = entryNum;
2422621Svikram 		}
2423621Svikram 	}
2424621Svikram 
2425621Svikram 	/*
2426621Svikram 	 * No default command in menu, simply return
2427621Svikram 	 */
2428621Svikram 	if (default_lp == NULL) {
2429621Svikram 		return;
2430621Svikram 	}
2431621Svikram 
2432621Svikram 	free(default_lp->arg);
2433621Svikram 	free(default_lp->line);
2434621Svikram 
2435621Svikram 	if (default_entry == NULL) {
2436621Svikram 		default_lp->arg = s_strdup("0");
2437621Svikram 	} else {
2438621Svikram 		(void) snprintf(buf, sizeof (buf), "%d",
2439621Svikram 		    default_entry->entryNum);
2440621Svikram 		default_lp->arg = s_strdup(buf);
2441621Svikram 	}
2442621Svikram 
2443621Svikram 	/*
2444621Svikram 	 * The following is required since only the line field gets
2445621Svikram 	 * written back to menu.lst
2446621Svikram 	 */
2447621Svikram 	(void) snprintf(buf, sizeof (buf), "%s%s%s",
2448621Svikram 	    menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg);
2449621Svikram 	default_lp->line = s_strdup(buf);
2450621Svikram }
2451621Svikram 
2452621Svikram 
24530Sstevel@tonic-gate static menu_t *
24540Sstevel@tonic-gate menu_read(char *menu_path)
24550Sstevel@tonic-gate {
24560Sstevel@tonic-gate 	FILE *fp;
24570Sstevel@tonic-gate 	char buf[BAM_MAXLINE], *cp;
24580Sstevel@tonic-gate 	menu_t *mp;
24590Sstevel@tonic-gate 	int line, entry, len, n;
24600Sstevel@tonic-gate 
24610Sstevel@tonic-gate 	mp = s_calloc(1, sizeof (menu_t));
24620Sstevel@tonic-gate 
24630Sstevel@tonic-gate 	fp = fopen(menu_path, "r");
24640Sstevel@tonic-gate 	if (fp == NULL) { /* Let the caller handle this error */
24650Sstevel@tonic-gate 		return (mp);
24660Sstevel@tonic-gate 	}
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 	/* Note: GRUB boot entry number starts with 0 */
24700Sstevel@tonic-gate 	line = LINE_INIT;
24710Sstevel@tonic-gate 	entry = ENTRY_INIT;
24720Sstevel@tonic-gate 	cp = buf;
24730Sstevel@tonic-gate 	len = sizeof (buf);
24740Sstevel@tonic-gate 	while (s_fgets(cp, len, fp) != NULL) {
24750Sstevel@tonic-gate 		n = strlen(cp);
24760Sstevel@tonic-gate 		if (cp[n - 1] == '\\') {
24770Sstevel@tonic-gate 			len -= n - 1;
24780Sstevel@tonic-gate 			assert(len >= 2);
24790Sstevel@tonic-gate 			cp += n - 1;
24800Sstevel@tonic-gate 			continue;
24810Sstevel@tonic-gate 		}
24820Sstevel@tonic-gate 		line_parser(mp, buf, &line, &entry);
24830Sstevel@tonic-gate 		cp = buf;
24840Sstevel@tonic-gate 		len = sizeof (buf);
24850Sstevel@tonic-gate 	}
24860Sstevel@tonic-gate 
24870Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
24880Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, menu_path, strerror(errno));
24890Sstevel@tonic-gate 	}
24900Sstevel@tonic-gate 
24910Sstevel@tonic-gate 	return (mp);
24920Sstevel@tonic-gate }
24930Sstevel@tonic-gate 
24940Sstevel@tonic-gate static error_t
24950Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title)
24960Sstevel@tonic-gate {
24970Sstevel@tonic-gate 	char *eq;
24980Sstevel@tonic-gate 	char *opt_dup;
24990Sstevel@tonic-gate 	int entryNum;
25000Sstevel@tonic-gate 
25010Sstevel@tonic-gate 	assert(mp);
25020Sstevel@tonic-gate 	assert(mp->start);
25030Sstevel@tonic-gate 	assert(opt);
25040Sstevel@tonic-gate 
25050Sstevel@tonic-gate 	opt_dup = s_strdup(opt);
25060Sstevel@tonic-gate 
25070Sstevel@tonic-gate 	if (entry)
25080Sstevel@tonic-gate 		*entry = ENTRY_INIT;
25090Sstevel@tonic-gate 	if (title)
25100Sstevel@tonic-gate 		*title = NULL;
25110Sstevel@tonic-gate 
25120Sstevel@tonic-gate 	eq = strchr(opt_dup, '=');
25130Sstevel@tonic-gate 	if (eq == NULL) {
25140Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
25150Sstevel@tonic-gate 		free(opt_dup);
25160Sstevel@tonic-gate 		return (BAM_ERROR);
25170Sstevel@tonic-gate 	}
25180Sstevel@tonic-gate 
25190Sstevel@tonic-gate 	*eq = '\0';
25200Sstevel@tonic-gate 	if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) {
25210Sstevel@tonic-gate 		assert(mp->end);
25220Sstevel@tonic-gate 		entryNum = s_strtol(eq + 1);
25230Sstevel@tonic-gate 		if (entryNum < 0 || entryNum > mp->end->entryNum) {
25240Sstevel@tonic-gate 			bam_error(INVALID_ENTRY, eq + 1);
25250Sstevel@tonic-gate 			free(opt_dup);
25260Sstevel@tonic-gate 			return (BAM_ERROR);
25270Sstevel@tonic-gate 		}
25280Sstevel@tonic-gate 		*entry = entryNum;
25290Sstevel@tonic-gate 	} else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) {
25300Sstevel@tonic-gate 		*title = opt + (eq - opt_dup) + 1;
25310Sstevel@tonic-gate 	} else {
25320Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
25330Sstevel@tonic-gate 		free(opt_dup);
25340Sstevel@tonic-gate 		return (BAM_ERROR);
25350Sstevel@tonic-gate 	}
25360Sstevel@tonic-gate 
25370Sstevel@tonic-gate 	free(opt_dup);
25380Sstevel@tonic-gate 	return (BAM_SUCCESS);
25390Sstevel@tonic-gate }
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate /*
25420Sstevel@tonic-gate  * If invoked with no titles/entries (opt == NULL)
25430Sstevel@tonic-gate  * only title lines in file are printed.
25440Sstevel@tonic-gate  *
25450Sstevel@tonic-gate  * If invoked with a title or entry #, all
25460Sstevel@tonic-gate  * lines in *every* matching entry are listed
25470Sstevel@tonic-gate  */
25480Sstevel@tonic-gate static error_t
25490Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt)
25500Sstevel@tonic-gate {
25510Sstevel@tonic-gate 	line_t *lp;
25520Sstevel@tonic-gate 	int entry = ENTRY_INIT;
25530Sstevel@tonic-gate 	int found;
25540Sstevel@tonic-gate 	char *title = NULL;
25550Sstevel@tonic-gate 
25560Sstevel@tonic-gate 	assert(mp);
25570Sstevel@tonic-gate 	assert(menu_path);
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate 	if (mp->start == NULL) {
25600Sstevel@tonic-gate 		bam_error(NO_MENU, menu_path);
25610Sstevel@tonic-gate 		return (BAM_ERROR);
25620Sstevel@tonic-gate 	}
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate 	if (opt != NULL) {
25650Sstevel@tonic-gate 		if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
25660Sstevel@tonic-gate 			return (BAM_ERROR);
25670Sstevel@tonic-gate 		}
25680Sstevel@tonic-gate 		assert((entry != ENTRY_INIT) ^ (title != NULL));
25690Sstevel@tonic-gate 	} else {
25700Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0);
25710Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0);
25720Sstevel@tonic-gate 	}
25730Sstevel@tonic-gate 
25740Sstevel@tonic-gate 	found = 0;
25750Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
25760Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY)
25770Sstevel@tonic-gate 			continue;
25780Sstevel@tonic-gate 		if (opt == NULL && lp->flags == BAM_TITLE) {
25790Sstevel@tonic-gate 			bam_print(PRINT_TITLE, lp->entryNum,
25800Sstevel@tonic-gate 			    lp->arg);
25810Sstevel@tonic-gate 			found = 1;
25820Sstevel@tonic-gate 			continue;
25830Sstevel@tonic-gate 		}
25840Sstevel@tonic-gate 		if (entry != ENTRY_INIT && lp->entryNum == entry) {
25850Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
25860Sstevel@tonic-gate 			found = 1;
25870Sstevel@tonic-gate 			continue;
25880Sstevel@tonic-gate 		}
25890Sstevel@tonic-gate 
25900Sstevel@tonic-gate 		/*
25910Sstevel@tonic-gate 		 * We set the entry value here so that all lines
25920Sstevel@tonic-gate 		 * in entry get printed. If we subsequently match
25930Sstevel@tonic-gate 		 * title in other entries, all lines in those
25940Sstevel@tonic-gate 		 * entries get printed as well.
25950Sstevel@tonic-gate 		 */
25960Sstevel@tonic-gate 		if (title && lp->flags == BAM_TITLE && lp->arg &&
25970Sstevel@tonic-gate 		    strncmp(title, lp->arg, strlen(title)) == 0) {
25980Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
25990Sstevel@tonic-gate 			entry = lp->entryNum;
26000Sstevel@tonic-gate 			found = 1;
26010Sstevel@tonic-gate 			continue;
26020Sstevel@tonic-gate 		}
26030Sstevel@tonic-gate 	}
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate 	if (!found) {
26060Sstevel@tonic-gate 		bam_error(NO_MATCH_ENTRY);
26070Sstevel@tonic-gate 		return (BAM_ERROR);
26080Sstevel@tonic-gate 	}
26090Sstevel@tonic-gate 
26100Sstevel@tonic-gate 	return (BAM_SUCCESS);
26110Sstevel@tonic-gate }
26120Sstevel@tonic-gate 
26130Sstevel@tonic-gate static int
26140Sstevel@tonic-gate add_boot_entry(menu_t *mp,
26150Sstevel@tonic-gate 	char *title,
26160Sstevel@tonic-gate 	char *root,
26170Sstevel@tonic-gate 	char *kernel,
26180Sstevel@tonic-gate 	char *module)
26190Sstevel@tonic-gate {
26200Sstevel@tonic-gate 	int lineNum, entryNum;
26210Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
26220Sstevel@tonic-gate 
26230Sstevel@tonic-gate 	assert(mp);
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate 	if (title == NULL) {
2626656Sszhou 		title = "Solaris";	/* default to Solaris */
26270Sstevel@tonic-gate 	}
26280Sstevel@tonic-gate 	if (kernel == NULL) {
26290Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]);
26300Sstevel@tonic-gate 		return (BAM_ERROR);
26310Sstevel@tonic-gate 	}
26320Sstevel@tonic-gate 	if (module == NULL) {
26330Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]);
26340Sstevel@tonic-gate 		return (BAM_ERROR);
26350Sstevel@tonic-gate 	}
26360Sstevel@tonic-gate 
26370Sstevel@tonic-gate 	if (mp->start) {
26380Sstevel@tonic-gate 		lineNum = mp->end->lineNum;
26390Sstevel@tonic-gate 		entryNum = mp->end->entryNum;
26400Sstevel@tonic-gate 	} else {
26410Sstevel@tonic-gate 		lineNum = LINE_INIT;
26420Sstevel@tonic-gate 		entryNum = ENTRY_INIT;
26430Sstevel@tonic-gate 	}
26440Sstevel@tonic-gate 
26450Sstevel@tonic-gate 	/*
26460Sstevel@tonic-gate 	 * No separator for comment (HDR/FTR) commands
26470Sstevel@tonic-gate 	 * The syntax for comments is #<comment>
26480Sstevel@tonic-gate 	 */
26490Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
26500Sstevel@tonic-gate 	    menu_cmds[COMMENT_CMD], BAM_HDR);
2651662Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
26520Sstevel@tonic-gate 
26530Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
26540Sstevel@tonic-gate 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
2655662Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
2656662Sszhou 
2657662Sszhou 	if (root) {
2658662Sszhou 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
2659662Sszhou 		    menu_cmds[ROOT_CMD], menu_cmds[SEP_CMD], root);
2660662Sszhou 		line_parser(mp, linebuf, &lineNum, &entryNum);
26610Sstevel@tonic-gate 	}
26620Sstevel@tonic-gate 
26630Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
26640Sstevel@tonic-gate 	    menu_cmds[KERNEL_CMD], menu_cmds[SEP_CMD], kernel);
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[MODULE_CMD], menu_cmds[SEP_CMD], module);
2669662Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
26700Sstevel@tonic-gate 
26710Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
26720Sstevel@tonic-gate 	    menu_cmds[COMMENT_CMD], BAM_FTR);
2673662Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 	return (entryNum);
26760Sstevel@tonic-gate }
26770Sstevel@tonic-gate 
26780Sstevel@tonic-gate static error_t
26790Sstevel@tonic-gate do_delete(menu_t *mp, int entryNum)
26800Sstevel@tonic-gate {
2681662Sszhou 	line_t *lp, *freed;
2682662Sszhou 	entry_t *ent, *tmp;
26830Sstevel@tonic-gate 	int deleted;
26840Sstevel@tonic-gate 
26850Sstevel@tonic-gate 	assert(entryNum != ENTRY_INIT);
26860Sstevel@tonic-gate 
2687662Sszhou 	ent = mp->entries;
2688662Sszhou 	while (ent) {
2689662Sszhou 		lp = ent->start;
2690662Sszhou 		/* check entry number and make sure it's a bootadm entry */
2691662Sszhou 		if (lp->flags != BAM_COMMENT ||
2692662Sszhou 		    strcmp(lp->arg, BAM_HDR) != 0 ||
2693662Sszhou 		    (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) {
2694662Sszhou 			ent = ent->next;
26950Sstevel@tonic-gate 			continue;
26960Sstevel@tonic-gate 		}
26970Sstevel@tonic-gate 
2698662Sszhou 		/* free the entry content */
2699662Sszhou 		do {
2700662Sszhou 			freed = lp;
2701662Sszhou 			lp = lp->next;	/* prev stays the same */
2702662Sszhou 			unlink_line(mp, freed);
2703662Sszhou 			line_free(freed);
2704662Sszhou 		} while (freed != ent->end);
2705662Sszhou 
2706662Sszhou 		/* free the entry_t structure */
2707662Sszhou 		tmp = ent;
2708662Sszhou 		ent = ent->next;
2709662Sszhou 		if (tmp->prev)
2710662Sszhou 			tmp->prev->next = ent;
27110Sstevel@tonic-gate 		else
2712662Sszhou 			mp->entries = ent;
2713662Sszhou 		if (ent)
2714662Sszhou 			ent->prev = tmp->prev;
27150Sstevel@tonic-gate 		deleted = 1;
27160Sstevel@tonic-gate 	}
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate 	if (!deleted && entryNum != ALL_ENTRIES) {
27190Sstevel@tonic-gate 		bam_error(NO_BOOTADM_MATCH);
27200Sstevel@tonic-gate 		return (BAM_ERROR);
27210Sstevel@tonic-gate 	}
27220Sstevel@tonic-gate 
2723621Svikram 	/*
2724621Svikram 	 * Now that we have deleted an entry, update
2725621Svikram 	 * the entry numbering and the default cmd.
2726621Svikram 	 */
2727621Svikram 	update_numbering(mp);
2728621Svikram 
27290Sstevel@tonic-gate 	return (BAM_SUCCESS);
27300Sstevel@tonic-gate }
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate static error_t
27330Sstevel@tonic-gate delete_all_entries(menu_t *mp, char *menu_path, char *opt)
27340Sstevel@tonic-gate {
27350Sstevel@tonic-gate 	assert(mp);
27360Sstevel@tonic-gate 	assert(opt == NULL);
27370Sstevel@tonic-gate 
27380Sstevel@tonic-gate 	if (mp->start == NULL) {
27390Sstevel@tonic-gate 		bam_print(EMPTY_FILE, menu_path);
27400Sstevel@tonic-gate 		return (BAM_SUCCESS);
27410Sstevel@tonic-gate 	}
27420Sstevel@tonic-gate 
27430Sstevel@tonic-gate 	if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) {
27440Sstevel@tonic-gate 		return (BAM_ERROR);
27450Sstevel@tonic-gate 	}
27460Sstevel@tonic-gate 
27470Sstevel@tonic-gate 	return (BAM_WRITE);
27480Sstevel@tonic-gate }
27490Sstevel@tonic-gate 
27500Sstevel@tonic-gate static FILE *
2751662Sszhou open_diskmap(char *root)
27520Sstevel@tonic-gate {
27530Sstevel@tonic-gate 	FILE *fp;
27540Sstevel@tonic-gate 	char cmd[PATH_MAX];
27550Sstevel@tonic-gate 
27560Sstevel@tonic-gate 	/* make sure we have a map file */
27570Sstevel@tonic-gate 	fp = fopen(GRUBDISK_MAP, "r");
27580Sstevel@tonic-gate 	if (fp == NULL) {
27590Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd),
2760662Sszhou 		    "%s%s > /dev/null", root, CREATE_DISKMAP);
27610Sstevel@tonic-gate 		(void) system(cmd);
27620Sstevel@tonic-gate 		fp = fopen(GRUBDISK_MAP, "r");
27630Sstevel@tonic-gate 	}
27640Sstevel@tonic-gate 	return (fp);
27650Sstevel@tonic-gate }
27660Sstevel@tonic-gate 
27670Sstevel@tonic-gate #define	SECTOR_SIZE	512
27680Sstevel@tonic-gate 
27690Sstevel@tonic-gate static int
27700Sstevel@tonic-gate get_partition(char *device)
27710Sstevel@tonic-gate {
27720Sstevel@tonic-gate 	int i, fd, is_pcfs, partno = -1;
27730Sstevel@tonic-gate 	struct mboot *mboot;
27740Sstevel@tonic-gate 	char boot_sect[SECTOR_SIZE];
27750Sstevel@tonic-gate 	char *wholedisk, *slice;
27760Sstevel@tonic-gate 
27770Sstevel@tonic-gate 	/* form whole disk (p0) */
27780Sstevel@tonic-gate 	slice = device + strlen(device) - 2;
27790Sstevel@tonic-gate 	is_pcfs = (*slice != 's');
27800Sstevel@tonic-gate 	if (!is_pcfs)
27810Sstevel@tonic-gate 		*slice = '\0';
27820Sstevel@tonic-gate 	wholedisk = s_calloc(1, strlen(device) + 3);
27830Sstevel@tonic-gate 	(void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device);
27840Sstevel@tonic-gate 	if (!is_pcfs)
27850Sstevel@tonic-gate 		*slice = 's';
27860Sstevel@tonic-gate 
27870Sstevel@tonic-gate 	/* read boot sector */
27880Sstevel@tonic-gate 	fd = open(wholedisk, O_RDONLY);
27890Sstevel@tonic-gate 	free(wholedisk);
27900Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
27910Sstevel@tonic-gate 		return (partno);
27920Sstevel@tonic-gate 	}
27930Sstevel@tonic-gate 	(void) close(fd);
27940Sstevel@tonic-gate 
27950Sstevel@tonic-gate 	/* parse fdisk table */
27960Sstevel@tonic-gate 	mboot = (struct mboot *)((void *)boot_sect);
27970Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
27980Sstevel@tonic-gate 		struct ipart *part =
27990Sstevel@tonic-gate 		    (struct ipart *)(uintptr_t)mboot->parts + i;
28000Sstevel@tonic-gate 		if (is_pcfs) {	/* looking for solaris boot part */
28010Sstevel@tonic-gate 			if (part->systid == 0xbe) {
28020Sstevel@tonic-gate 				partno = i;
28030Sstevel@tonic-gate 				break;
28040Sstevel@tonic-gate 			}
28050Sstevel@tonic-gate 		} else {	/* look for solaris partition, old and new */
28060Sstevel@tonic-gate 			if (part->systid == SUNIXOS ||
28070Sstevel@tonic-gate 			    part->systid == SUNIXOS2) {
28080Sstevel@tonic-gate 				partno = i;
28090Sstevel@tonic-gate 				break;
28100Sstevel@tonic-gate 			}
28110Sstevel@tonic-gate 		}
28120Sstevel@tonic-gate 	}
28130Sstevel@tonic-gate 	return (partno);
28140Sstevel@tonic-gate }
28150Sstevel@tonic-gate 
28160Sstevel@tonic-gate static char *
28170Sstevel@tonic-gate get_grubdisk(char *rootdev, FILE *fp, int on_bootdev)
28180Sstevel@tonic-gate {
28190Sstevel@tonic-gate 	char *grubdisk;	/* (hd#,#,#) */
28200Sstevel@tonic-gate 	char *slice;
28210Sstevel@tonic-gate 	char *grubhd;
28220Sstevel@tonic-gate 	int fdiskpart;
28230Sstevel@tonic-gate 	int found = 0;
28240Sstevel@tonic-gate 	char *devname, *ctdname = strstr(rootdev, "dsk/");
28250Sstevel@tonic-gate 	char linebuf[PATH_MAX];
28260Sstevel@tonic-gate 
28270Sstevel@tonic-gate 	if (ctdname == NULL)
28280Sstevel@tonic-gate 		return (NULL);
28290Sstevel@tonic-gate 
28300Sstevel@tonic-gate 	ctdname += strlen("dsk/");
28310Sstevel@tonic-gate 	slice = strrchr(ctdname, 's');
28320Sstevel@tonic-gate 	if (slice)
28330Sstevel@tonic-gate 		*slice = '\0';
28340Sstevel@tonic-gate 
28350Sstevel@tonic-gate 	rewind(fp);
28360Sstevel@tonic-gate 	while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) {
28370Sstevel@tonic-gate 		grubhd = strtok(linebuf, " \t\n");
28380Sstevel@tonic-gate 		if (grubhd)
28390Sstevel@tonic-gate 			devname = strtok(NULL, " \t\n");
28400Sstevel@tonic-gate 		else
28410Sstevel@tonic-gate 			devname = NULL;
28420Sstevel@tonic-gate 		if (devname && strcmp(devname, ctdname) == 0) {
28430Sstevel@tonic-gate 			found = 1;
28440Sstevel@tonic-gate 			break;
28450Sstevel@tonic-gate 		}
28460Sstevel@tonic-gate 	}
28470Sstevel@tonic-gate 
28480Sstevel@tonic-gate 	if (slice)
28490Sstevel@tonic-gate 		*slice = 's';
28500Sstevel@tonic-gate 
28510Sstevel@tonic-gate 	if (found == 0) {
28520Sstevel@tonic-gate 		if (bam_verbose)
28530Sstevel@tonic-gate 			bam_print(DISKMAP_FAIL_NONFATAL, rootdev);
28540Sstevel@tonic-gate 		grubhd = "0";	/* assume disk 0 if can't match */
28550Sstevel@tonic-gate 	}
28560Sstevel@tonic-gate 
28570Sstevel@tonic-gate 	fdiskpart = get_partition(rootdev);
28580Sstevel@tonic-gate 	if (fdiskpart == -1)
28590Sstevel@tonic-gate 		return (NULL);
28600Sstevel@tonic-gate 
28610Sstevel@tonic-gate 	grubdisk = s_calloc(1, 10);
28620Sstevel@tonic-gate 	if (slice) {
28630Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d,%c)",
28640Sstevel@tonic-gate 		    grubhd, fdiskpart, slice[1] + 'a' - '0');
28650Sstevel@tonic-gate 	} else
28660Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d)",
28670Sstevel@tonic-gate 		    grubhd, fdiskpart);
28680Sstevel@tonic-gate 
28690Sstevel@tonic-gate 	/* if root not on bootdev, change GRUB disk to 0 */
28700Sstevel@tonic-gate 	if (!on_bootdev)
28710Sstevel@tonic-gate 		grubdisk[3] = '0';
28720Sstevel@tonic-gate 	return (grubdisk);
28730Sstevel@tonic-gate }
28740Sstevel@tonic-gate 
2875656Sszhou static char *
2876656Sszhou get_title(char *rootdir)
28770Sstevel@tonic-gate {
28780Sstevel@tonic-gate 	static char title[80];	/* from /etc/release */
2879662Sszhou 	char *cp = NULL, release[PATH_MAX];
28800Sstevel@tonic-gate 	FILE *fp;
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate 	/* open the /etc/release file */
28830Sstevel@tonic-gate 	(void) snprintf(release, sizeof (release), "%s/etc/release", rootdir);
28840Sstevel@tonic-gate 
28850Sstevel@tonic-gate 	fp = fopen(release, "r");
28860Sstevel@tonic-gate 	if (fp == NULL)
2887656Sszhou 		return (NULL);
28880Sstevel@tonic-gate 
28890Sstevel@tonic-gate 	while (s_fgets(title, sizeof (title), fp) != NULL) {
28900Sstevel@tonic-gate 		cp = strstr(title, "Solaris");
28910Sstevel@tonic-gate 		if (cp)
28920Sstevel@tonic-gate 			break;
28930Sstevel@tonic-gate 	}
28940Sstevel@tonic-gate 	(void) fclose(fp);
2895662Sszhou 	return (cp == NULL ? "Solaris" : cp);
28960Sstevel@tonic-gate }
28970Sstevel@tonic-gate 
28980Sstevel@tonic-gate static char *
28990Sstevel@tonic-gate get_special(char *mountp)
29000Sstevel@tonic-gate {
29010Sstevel@tonic-gate 	FILE *mntfp;
29020Sstevel@tonic-gate 	struct mnttab mp = {0}, mpref = {0};
29030Sstevel@tonic-gate 
29040Sstevel@tonic-gate 	mntfp = fopen(MNTTAB, "r");
29050Sstevel@tonic-gate 	if (mntfp == NULL) {
29060Sstevel@tonic-gate 		return (0);
29070Sstevel@tonic-gate 	}
29080Sstevel@tonic-gate 
29090Sstevel@tonic-gate 	if (*mountp == '\0')
29100Sstevel@tonic-gate 		mpref.mnt_mountp = "/";
29110Sstevel@tonic-gate 	else
29120Sstevel@tonic-gate 		mpref.mnt_mountp = mountp;
29130Sstevel@tonic-gate 	if (getmntany(mntfp, &mp, &mpref) != 0) {
29140Sstevel@tonic-gate 		(void) fclose(mntfp);
29150Sstevel@tonic-gate 		return (NULL);
29160Sstevel@tonic-gate 	}
29170Sstevel@tonic-gate 	(void) fclose(mntfp);
29180Sstevel@tonic-gate 
29190Sstevel@tonic-gate 	return (s_strdup(mp.mnt_special));
29200Sstevel@tonic-gate }
29210Sstevel@tonic-gate 
29220Sstevel@tonic-gate static char *
29230Sstevel@tonic-gate os_to_grubdisk(char *osdisk, int on_bootdev)
29240Sstevel@tonic-gate {
29250Sstevel@tonic-gate 	FILE *fp;
29260Sstevel@tonic-gate 	char *grubdisk;
29270Sstevel@tonic-gate 
29280Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
2929662Sszhou 	fp = open_diskmap("");
29300Sstevel@tonic-gate 	if (fp == NULL) {
29310Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdisk);
29320Sstevel@tonic-gate 		return (NULL);
29330Sstevel@tonic-gate 	}
29340Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdisk, fp, on_bootdev);
29350Sstevel@tonic-gate 	(void) fclose(fp);
29360Sstevel@tonic-gate 	return (grubdisk);
29370Sstevel@tonic-gate }
29380Sstevel@tonic-gate 
29390Sstevel@tonic-gate /*
29400Sstevel@tonic-gate  * Check if root is on the boot device
29410Sstevel@tonic-gate  * Return 0 (false) on error
29420Sstevel@tonic-gate  */
29430Sstevel@tonic-gate static int
29440Sstevel@tonic-gate menu_on_bootdev(char *menu_root, FILE *fp)
29450Sstevel@tonic-gate {
29460Sstevel@tonic-gate 	int ret;
29470Sstevel@tonic-gate 	char *grubhd, *bootp, *special;
29480Sstevel@tonic-gate 
29490Sstevel@tonic-gate 	special = get_special(menu_root);
29500Sstevel@tonic-gate 	if (special == NULL)
29510Sstevel@tonic-gate 		return (0);
29520Sstevel@tonic-gate 	bootp = strstr(special, "p0:boot");
29530Sstevel@tonic-gate 	if (bootp)
29540Sstevel@tonic-gate 		*bootp = '\0';
29550Sstevel@tonic-gate 	grubhd = get_grubdisk(special, fp, 1);
29560Sstevel@tonic-gate 	free(special);
29570Sstevel@tonic-gate 
29580Sstevel@tonic-gate 	if (grubhd == NULL)
29590Sstevel@tonic-gate 		return (0);
29600Sstevel@tonic-gate 	ret = grubhd[3] == '0';
29610Sstevel@tonic-gate 	free(grubhd);
29620Sstevel@tonic-gate 	return (ret);
29630Sstevel@tonic-gate }
29640Sstevel@tonic-gate 
2965662Sszhou /*
2966662Sszhou  * look for matching bootadm entry with specified parameters
2967662Sszhou  * Here are the rules (based on existing usage):
2968662Sszhou  * - If title is specified, match on title only
2969662Sszhou  * - Else, match on grubdisk and module (don't care about kernel line).
2970662Sszhou  *   note that, if root_opt is non-zero, the absence of root line is
2971662Sszhou  *   considered a match.
2972662Sszhou  */
2973662Sszhou static entry_t *
2974662Sszhou find_boot_entry(menu_t *mp, char *title, char *root, char *module,
2975662Sszhou     int root_opt, int *entry_num)
2976662Sszhou {
2977662Sszhou 	int i;
2978662Sszhou 	line_t *lp;
2979662Sszhou 	entry_t *ent;
2980662Sszhou 
2981662Sszhou 	/* find matching entry */
2982662Sszhou 	for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) {
2983662Sszhou 		lp = ent->start;
2984662Sszhou 
2985662Sszhou 		/* first line of entry must be bootadm comment */
2986662Sszhou 		lp = ent->start;
2987662Sszhou 		if (lp->flags != BAM_COMMENT || strcmp(lp->arg, BAM_HDR) != 0) {
2988662Sszhou 			continue;
2989662Sszhou 		}
2990662Sszhou 
2991662Sszhou 		/* advance to title line */
2992662Sszhou 		lp = lp->next;
2993662Sszhou 		if (title) {
2994662Sszhou 			if (lp->flags == BAM_TITLE && lp->arg &&
2995662Sszhou 			    strcmp(lp->arg, title) == 0)
2996662Sszhou 				break;
2997662Sszhou 			continue;	/* check title only */
2998662Sszhou 		}
2999662Sszhou 
3000662Sszhou 		lp = lp->next;	/* advance to root line */
3001662Sszhou 		if (lp == NULL || strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
3002662Sszhou 			/* root command found, match grub disk */
3003662Sszhou 			if (strcmp(lp->arg, root) != 0) {
3004662Sszhou 				continue;
3005662Sszhou 			}
3006662Sszhou 			lp = lp->next;	/* advance to kernel line */
3007662Sszhou 		} else {
3008662Sszhou 			/* no root command, see if root is optional */
3009662Sszhou 			if (root_opt == 0) {
3010662Sszhou 				continue;
3011662Sszhou 			}
3012662Sszhou 		}
3013662Sszhou 
3014662Sszhou 		if (lp == NULL || lp->next == NULL) {
3015662Sszhou 			continue;
3016662Sszhou 		}
3017662Sszhou 
3018662Sszhou 		/* check for matching module entry (failsafe or normal) */
3019662Sszhou 		lp = lp->next;	/* advance to module line */
3020662Sszhou 		if (strcmp(lp->cmd, menu_cmds[MODULE_CMD]) != 0 ||
3021662Sszhou 		    strcmp(lp->arg, module) != 0) {
3022662Sszhou 			continue;
3023662Sszhou 		}
3024662Sszhou 		break;	/* match found */
3025662Sszhou 	}
3026662Sszhou 
3027662Sszhou 	*entry_num = i;
3028662Sszhou 	return (ent);
3029662Sszhou }
3030662Sszhou 
3031662Sszhou static int
3032662Sszhou update_boot_entry(menu_t *mp, char *title, char *root, char *kernel,
3033662Sszhou     char *module, int root_opt)
3034662Sszhou {
3035662Sszhou 	int i;
3036662Sszhou 	entry_t *ent;
3037662Sszhou 	line_t *lp;
3038662Sszhou 	char linebuf[BAM_MAXLINE];
3039662Sszhou 
3040662Sszhou 	/* note: don't match on title, it's updated on upgrade */
3041662Sszhou 	ent = find_boot_entry(mp, NULL, root, module, root_opt, &i);
3042662Sszhou 	if (ent == NULL)
3043662Sszhou 		return (add_boot_entry(mp, title, root_opt ? NULL : root,
3044662Sszhou 		    kernel, module));
3045662Sszhou 
3046662Sszhou 	/* replace title of exiting entry and delete root line */
3047662Sszhou 	lp = ent->start;
3048662Sszhou 	lp = lp->next;	/* title line */
3049662Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
3050662Sszhou 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
3051662Sszhou 	free(lp->arg);
3052662Sszhou 	free(lp->line);
3053662Sszhou 	lp->arg = s_strdup(title);
3054662Sszhou 	lp->line = s_strdup(linebuf);
3055662Sszhou 
3056662Sszhou 	lp = lp->next;	/* root line */
3057662Sszhou 	if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
3058662Sszhou 		if (root_opt) {		/* root line not needed */
3059662Sszhou 			line_t *tmp = lp;
3060662Sszhou 			lp = lp->next;
3061662Sszhou 			unlink_line(mp, tmp);
3062662Sszhou 			line_free(tmp);
3063662Sszhou 		} else
3064662Sszhou 			lp = lp->next;
3065662Sszhou 	}
3066662Sszhou 	return (i);
3067662Sszhou }
3068662Sszhou 
30690Sstevel@tonic-gate /*ARGSUSED*/
30700Sstevel@tonic-gate static error_t
30710Sstevel@tonic-gate update_entry(menu_t *mp, char *menu_root, char *opt)
30720Sstevel@tonic-gate {
30730Sstevel@tonic-gate 	FILE *fp;
30740Sstevel@tonic-gate 	int entry;
30750Sstevel@tonic-gate 	char *grubdisk, *title, *osdev, *osroot;
3076662Sszhou 	struct stat sbuf;
3077662Sszhou 	char failsafe[256];
30780Sstevel@tonic-gate 
30790Sstevel@tonic-gate 	assert(mp);
30800Sstevel@tonic-gate 	assert(opt);
30810Sstevel@tonic-gate 
30820Sstevel@tonic-gate 	osdev = strtok(opt, ",");
30830Sstevel@tonic-gate 	osroot = strtok(NULL, ",");
30840Sstevel@tonic-gate 	if (osroot == NULL)
30850Sstevel@tonic-gate 		osroot = menu_root;
30860Sstevel@tonic-gate 	title = get_title(osroot);
30870Sstevel@tonic-gate 
30880Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
3089662Sszhou 	fp = open_diskmap(osroot);
30900Sstevel@tonic-gate 	if (fp == NULL) {
30910Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
30920Sstevel@tonic-gate 		return (BAM_ERROR);
30930Sstevel@tonic-gate 	}
30940Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdev, fp, menu_on_bootdev(menu_root, fp));
30950Sstevel@tonic-gate 	(void) fclose(fp);
30960Sstevel@tonic-gate 	if (grubdisk == NULL) {
30970Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
30980Sstevel@tonic-gate 		return (BAM_ERROR);
30990Sstevel@tonic-gate 	}
31000Sstevel@tonic-gate 
31010Sstevel@tonic-gate 	/* add the entry for normal Solaris */
3102662Sszhou 	entry = update_boot_entry(mp, title, grubdisk,
31030Sstevel@tonic-gate 	    "/platform/i86pc/multiboot",
3104662Sszhou 	    "/platform/i86pc/boot_archive",
3105662Sszhou 	    osroot == menu_root);
31060Sstevel@tonic-gate 
31070Sstevel@tonic-gate 	/* add the entry for failsafe archive */
3108662Sszhou 	(void) snprintf(failsafe, sizeof (failsafe),
3109662Sszhou 	    "%s/boot/x86.miniroot-safe", osroot);
3110662Sszhou 	if (stat(failsafe, &sbuf) == 0)
3111662Sszhou 		(void) update_boot_entry(mp, "Solaris failsafe", grubdisk,
3112662Sszhou 		    "/boot/multiboot kernel/unix -s",
3113662Sszhou 		    "/boot/x86.miniroot-safe",
3114662Sszhou 		    osroot == menu_root);
31150Sstevel@tonic-gate 	free(grubdisk);
31160Sstevel@tonic-gate 
31170Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
31180Sstevel@tonic-gate 		return (BAM_ERROR);
31190Sstevel@tonic-gate 	}
31200Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
31210Sstevel@tonic-gate 	return (BAM_WRITE);
31220Sstevel@tonic-gate }
31230Sstevel@tonic-gate 
3124316Svikram static char *
3125316Svikram read_grub_root(void)
3126316Svikram {
3127316Svikram 	FILE *fp;
3128316Svikram 	struct stat sb;
3129316Svikram 	char buf[BAM_MAXLINE];
3130316Svikram 	char *rootstr;
3131316Svikram 
3132316Svikram 	if (stat(GRUB_slice, &sb) != 0) {
3133316Svikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
3134316Svikram 		return (NULL);
3135316Svikram 	}
3136316Svikram 
3137316Svikram 	if (stat(GRUB_root, &sb) != 0) {
3138316Svikram 		bam_error(MISSING_ROOT_FILE, GRUB_root, strerror(errno));
3139316Svikram 		return (NULL);
3140316Svikram 	}
3141316Svikram 
3142316Svikram 	fp = fopen(GRUB_root, "r");
3143316Svikram 	if (fp == NULL) {
3144316Svikram 		bam_error(OPEN_FAIL, GRUB_root, strerror(errno));
3145316Svikram 		return (NULL);
3146316Svikram 	}
3147316Svikram 
3148316Svikram 	if (s_fgets(buf, sizeof (buf), fp) == NULL) {
3149316Svikram 		bam_error(EMPTY_FILE, GRUB_root, strerror(errno));
3150316Svikram 		(void) fclose(fp);
3151316Svikram 		return (NULL);
3152316Svikram 	}
3153316Svikram 
3154316Svikram 	/*
3155316Svikram 	 * Copy buf here as check below may trash the buffer
3156316Svikram 	 */
3157316Svikram 	rootstr = s_strdup(buf);
3158316Svikram 
3159316Svikram 	if (s_fgets(buf, sizeof (buf), fp) != NULL) {
3160316Svikram 		bam_error(BAD_ROOT_FILE, GRUB_root);
3161316Svikram 		free(rootstr);
3162316Svikram 		rootstr = NULL;
3163316Svikram 	}
3164316Svikram 
3165316Svikram 	(void) fclose(fp);
3166316Svikram 
3167316Svikram 	return (rootstr);
3168316Svikram }
3169316Svikram 
3170662Sszhou static void
3171662Sszhou save_default_entry(menu_t *mp)
3172662Sszhou {
3173662Sszhou 	int lineNum, entryNum;
3174662Sszhou 	int entry = 0;	/* default is 0 */
3175662Sszhou 	char linebuf[BAM_MAXLINE];
3176662Sszhou 	line_t *lp = mp->curdefault;
3177662Sszhou 
3178662Sszhou 	if (lp)
3179662Sszhou 		entry = s_strtol(lp->arg);
3180662Sszhou 
3181662Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "#%s%d", BAM_OLDDEF, entry);
3182662Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
3183662Sszhou }
3184662Sszhou 
3185662Sszhou static void
3186662Sszhou restore_default_entry(menu_t *mp)
3187662Sszhou {
3188662Sszhou 	int entry;
3189662Sszhou 	char *str;
3190662Sszhou 	line_t *lp = mp->olddefault;
3191662Sszhou 
3192662Sszhou 	if (lp == NULL)
3193662Sszhou 		return;		/* nothing to restore */
3194662Sszhou 
3195662Sszhou 	str = lp->arg + strlen(BAM_OLDDEF);
3196662Sszhou 	entry = s_strtol(str);
3197662Sszhou 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
3198662Sszhou 
3199662Sszhou 	/* delete saved old default line */
3200662Sszhou 	mp->olddefault = NULL;
3201662Sszhou 	unlink_line(mp, lp);
3202662Sszhou 	line_free(lp);
3203662Sszhou }
3204662Sszhou 
32050Sstevel@tonic-gate /*
32060Sstevel@tonic-gate  * This function is for supporting reboot with args.
32070Sstevel@tonic-gate  * The opt value can be:
32080Sstevel@tonic-gate  * NULL		delete temp entry, if present
32090Sstevel@tonic-gate  * entry=#	switches default entry to 1
32100Sstevel@tonic-gate  * else		treated as boot-args and setup a temperary menu entry
32110Sstevel@tonic-gate  *		and make it the default
32120Sstevel@tonic-gate  */
32130Sstevel@tonic-gate #define	REBOOT_TITLE	"Solaris_reboot_transient"
32140Sstevel@tonic-gate 
3215662Sszhou /*ARGSUSED*/
32160Sstevel@tonic-gate static error_t
32170Sstevel@tonic-gate update_temp(menu_t *mp, char *menupath, char *opt)
32180Sstevel@tonic-gate {
32190Sstevel@tonic-gate 	int entry;
32200Sstevel@tonic-gate 	char *grubdisk, *rootdev;
32210Sstevel@tonic-gate 	char kernbuf[1024];
3222316Svikram 	struct stat sb;
32230Sstevel@tonic-gate 
32240Sstevel@tonic-gate 	assert(mp);
32250Sstevel@tonic-gate 
3226662Sszhou 	/* If no option, delete exiting reboot menu entry */
3227662Sszhou 	if (opt == NULL) {
3228662Sszhou 		entry_t *ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL,
3229662Sszhou 		    0, &entry);
3230662Sszhou 		if (ent == NULL)	/* not found is ok */
3231662Sszhou 			return (BAM_SUCCESS);
3232662Sszhou 		(void) do_delete(mp, entry);
3233662Sszhou 		restore_default_entry(mp);
3234662Sszhou 		return (BAM_WRITE);
3235662Sszhou 	}
3236662Sszhou 
3237662Sszhou 	/* if entry= is specified, set the default entry */
3238662Sszhou 	if (strncmp(opt, "entry=", strlen("entry=")) == 0 &&
32390Sstevel@tonic-gate 	    selector(mp, opt, &entry, NULL) == BAM_SUCCESS) {
32400Sstevel@tonic-gate 		/* this is entry=# option */
32410Sstevel@tonic-gate 		return (set_global(mp, menu_cmds[DEFAULT_CMD], entry));
32420Sstevel@tonic-gate 	}
32430Sstevel@tonic-gate 
32440Sstevel@tonic-gate 	/*
32450Sstevel@tonic-gate 	 * add a new menu entry base on opt and make it the default
32460Sstevel@tonic-gate 	 */
3247316Svikram 	grubdisk = NULL;
3248316Svikram 	if (stat(GRUB_slice, &sb) != 0) {
3249316Svikram 		/*
3250316Svikram 		 * 1. First get root disk name from mnttab
3251316Svikram 		 * 2. Translate disk name to grub name
3252316Svikram 		 * 3. Add the new menu entry
3253316Svikram 		 */
3254316Svikram 		rootdev = get_special("/");
3255316Svikram 		if (rootdev) {
3256316Svikram 			grubdisk = os_to_grubdisk(rootdev, 1);
3257316Svikram 			free(rootdev);
3258316Svikram 		}
3259316Svikram 	} else {
3260316Svikram 		/*
3261316Svikram 		 * This is an LU BE. The GRUB_root file
3262316Svikram 		 * contains entry for GRUB's "root" cmd.
3263316Svikram 		 */
3264316Svikram 		grubdisk = read_grub_root();
32650Sstevel@tonic-gate 	}
32660Sstevel@tonic-gate 	if (grubdisk == NULL) {
3267316Svikram 		bam_error(REBOOT_WITH_ARGS_FAILED);
32680Sstevel@tonic-gate 		return (BAM_ERROR);
32690Sstevel@tonic-gate 	}
32700Sstevel@tonic-gate 
32710Sstevel@tonic-gate 	/* add an entry for Solaris reboot */
32720Sstevel@tonic-gate 	(void) snprintf(kernbuf, sizeof (kernbuf),
32730Sstevel@tonic-gate 	    "/platform/i86pc/multiboot %s", opt);
32740Sstevel@tonic-gate 	entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf,
32750Sstevel@tonic-gate 	    "/platform/i86pc/boot_archive");
32760Sstevel@tonic-gate 	free(grubdisk);
32770Sstevel@tonic-gate 
32780Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
3279316Svikram 		bam_error(REBOOT_WITH_ARGS_FAILED);
32800Sstevel@tonic-gate 		return (BAM_ERROR);
32810Sstevel@tonic-gate 	}
3282662Sszhou 
3283662Sszhou 	save_default_entry(mp);
32840Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
32850Sstevel@tonic-gate 	return (BAM_WRITE);
32860Sstevel@tonic-gate }
32870Sstevel@tonic-gate 
32880Sstevel@tonic-gate static error_t
32890Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val)
32900Sstevel@tonic-gate {
32910Sstevel@tonic-gate 	line_t *lp, *found, *last;
32920Sstevel@tonic-gate 	char *cp, *str;
32930Sstevel@tonic-gate 	char prefix[BAM_MAXLINE];
32940Sstevel@tonic-gate 	size_t len;
32950Sstevel@tonic-gate 
32960Sstevel@tonic-gate 	assert(mp);
32970Sstevel@tonic-gate 	assert(globalcmd);
32980Sstevel@tonic-gate 
3299316Svikram 	if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) {
3300316Svikram 		if (val < 0 || mp->end == NULL || val > mp->end->entryNum) {
3301316Svikram 			(void) snprintf(prefix, sizeof (prefix), "%d", val);
3302316Svikram 			bam_error(INVALID_ENTRY, prefix);
3303316Svikram 			return (BAM_ERROR);
3304316Svikram 		}
3305316Svikram 	}
3306316Svikram 
33070Sstevel@tonic-gate 	found = last = NULL;
33080Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
33090Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
33100Sstevel@tonic-gate 			continue;
33110Sstevel@tonic-gate 
33120Sstevel@tonic-gate 		last = lp; /* track the last global found */
33130Sstevel@tonic-gate 
33140Sstevel@tonic-gate 		if (lp->cmd == NULL) {
33150Sstevel@tonic-gate 			bam_error(NO_CMD, lp->lineNum);
33160Sstevel@tonic-gate 			continue;
33170Sstevel@tonic-gate 		}
33180Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
33190Sstevel@tonic-gate 			continue;
33200Sstevel@tonic-gate 
33210Sstevel@tonic-gate 		if (found) {
33220Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
33230Sstevel@tonic-gate 		}
33240Sstevel@tonic-gate 		found = lp;
33250Sstevel@tonic-gate 	}
33260Sstevel@tonic-gate 
33270Sstevel@tonic-gate 	if (found == NULL) {
33280Sstevel@tonic-gate 		lp = s_calloc(1, sizeof (line_t));
33290Sstevel@tonic-gate 		if (last == NULL) {
33300Sstevel@tonic-gate 			lp->next = mp->start;
33310Sstevel@tonic-gate 			mp->start = lp;
33320Sstevel@tonic-gate 			mp->end = (mp->end) ? mp->end : lp;
33330Sstevel@tonic-gate 		} else {
33340Sstevel@tonic-gate 			lp->next = last->next;
33350Sstevel@tonic-gate 			last->next = lp;
33360Sstevel@tonic-gate 			if (lp->next == NULL)
33370Sstevel@tonic-gate 				mp->end = lp;
33380Sstevel@tonic-gate 		}
33390Sstevel@tonic-gate 		lp->flags = BAM_GLOBAL; /* other fields not needed for writes */
33400Sstevel@tonic-gate 		len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]);
33410Sstevel@tonic-gate 		len += 10;	/* val < 10 digits */
33420Sstevel@tonic-gate 		lp->line = s_calloc(1, len);
33430Sstevel@tonic-gate 		(void) snprintf(lp->line, len, "%s%s%d",
33440Sstevel@tonic-gate 		    globalcmd, menu_cmds[SEP_CMD], val);
33450Sstevel@tonic-gate 		return (BAM_WRITE);
33460Sstevel@tonic-gate 	}
33470Sstevel@tonic-gate 
33480Sstevel@tonic-gate 	/*
33490Sstevel@tonic-gate 	 * We are changing an existing entry. Retain any prefix whitespace,
33500Sstevel@tonic-gate 	 * but overwrite everything else. This preserves tabs added for
33510Sstevel@tonic-gate 	 * readability.
33520Sstevel@tonic-gate 	 */
33530Sstevel@tonic-gate 	str = found->line;
33540Sstevel@tonic-gate 	cp = prefix;
33550Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
33560Sstevel@tonic-gate 		*(cp++) = *(str++);
33570Sstevel@tonic-gate 	*cp = '\0'; /* Terminate prefix */
33580Sstevel@tonic-gate 	len = strlen(prefix) + strlen(globalcmd);
33590Sstevel@tonic-gate 	len += strlen(menu_cmds[SEP_CMD]) + 10;
33600Sstevel@tonic-gate 
33610Sstevel@tonic-gate 	free(found->line);
33620Sstevel@tonic-gate 	found->line = s_calloc(1, len);
33630Sstevel@tonic-gate 	(void) snprintf(found->line, len,
33640Sstevel@tonic-gate 		"%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val);
33650Sstevel@tonic-gate 
33660Sstevel@tonic-gate 	return (BAM_WRITE); /* need a write to menu */
33670Sstevel@tonic-gate }
33680Sstevel@tonic-gate 
33690Sstevel@tonic-gate /*ARGSUSED*/
33700Sstevel@tonic-gate static error_t
33710Sstevel@tonic-gate set_option(menu_t *mp, char *menu_path, char *opt)
33720Sstevel@tonic-gate {
33730Sstevel@tonic-gate 	int optnum, optval;
33740Sstevel@tonic-gate 	char *val;
33750Sstevel@tonic-gate 
33760Sstevel@tonic-gate 	assert(mp);
33770Sstevel@tonic-gate 	assert(opt);
33780Sstevel@tonic-gate 
33790Sstevel@tonic-gate 	val = strchr(opt, '=');
33800Sstevel@tonic-gate 	if (val == NULL) {
33810Sstevel@tonic-gate 		bam_error(INVALID_ENTRY, opt);
33820Sstevel@tonic-gate 		return (BAM_ERROR);
33830Sstevel@tonic-gate 	}
33840Sstevel@tonic-gate 
33850Sstevel@tonic-gate 	*val = '\0';
33860Sstevel@tonic-gate 	if (strcmp(opt, "default") == 0) {
33870Sstevel@tonic-gate 		optnum = DEFAULT_CMD;
33880Sstevel@tonic-gate 	} else if (strcmp(opt, "timeout") == 0) {
33890Sstevel@tonic-gate 		optnum = TIMEOUT_CMD;
33900Sstevel@tonic-gate 	} else {
33910Sstevel@tonic-gate 		bam_error(INVALID_ENTRY, opt);
33920Sstevel@tonic-gate 		return (BAM_ERROR);
33930Sstevel@tonic-gate 	}
33940Sstevel@tonic-gate 
33950Sstevel@tonic-gate 	optval = s_strtol(val + 1);
33960Sstevel@tonic-gate 	*val = '=';
33970Sstevel@tonic-gate 	return (set_global(mp, menu_cmds[optnum], optval));
33980Sstevel@tonic-gate }
33990Sstevel@tonic-gate 
34000Sstevel@tonic-gate /*
34010Sstevel@tonic-gate  * The quiet argument suppresses messages. This is used
34020Sstevel@tonic-gate  * when invoked in the context of other commands (e.g. list_entry)
34030Sstevel@tonic-gate  */
34040Sstevel@tonic-gate static error_t
34050Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet)
34060Sstevel@tonic-gate {
34070Sstevel@tonic-gate 	line_t *lp;
34080Sstevel@tonic-gate 	char *arg;
34090Sstevel@tonic-gate 	int done, ret = BAM_SUCCESS;
34100Sstevel@tonic-gate 
34110Sstevel@tonic-gate 	assert(mp);
34120Sstevel@tonic-gate 	assert(menu_path);
34130Sstevel@tonic-gate 	assert(globalcmd);
34140Sstevel@tonic-gate 
34150Sstevel@tonic-gate 	if (mp->start == NULL) {
34160Sstevel@tonic-gate 		if (!quiet)
34170Sstevel@tonic-gate 			bam_error(NO_MENU, menu_path);
34180Sstevel@tonic-gate 		return (BAM_ERROR);
34190Sstevel@tonic-gate 	}
34200Sstevel@tonic-gate 
34210Sstevel@tonic-gate 	done = 0;
34220Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
34230Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
34240Sstevel@tonic-gate 			continue;
34250Sstevel@tonic-gate 
34260Sstevel@tonic-gate 		if (lp->cmd == NULL) {
34270Sstevel@tonic-gate 			if (!quiet)
34280Sstevel@tonic-gate 				bam_error(NO_CMD, lp->lineNum);
34290Sstevel@tonic-gate 			continue;
34300Sstevel@tonic-gate 		}
34310Sstevel@tonic-gate 
34320Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
34330Sstevel@tonic-gate 			continue;
34340Sstevel@tonic-gate 
34350Sstevel@tonic-gate 		/* Found global. Check for duplicates */
34360Sstevel@tonic-gate 		if (done && !quiet) {
34370Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
34380Sstevel@tonic-gate 			ret = BAM_ERROR;
34390Sstevel@tonic-gate 		}
34400Sstevel@tonic-gate 
34410Sstevel@tonic-gate 		arg = lp->arg ? lp->arg : "";
34420Sstevel@tonic-gate 		bam_print(GLOBAL_CMD, globalcmd, arg);
34430Sstevel@tonic-gate 		done = 1;
34440Sstevel@tonic-gate 	}
34450Sstevel@tonic-gate 
34460Sstevel@tonic-gate 	if (!done && bam_verbose)
34470Sstevel@tonic-gate 		bam_print(NO_ENTRY, globalcmd);
34480Sstevel@tonic-gate 
34490Sstevel@tonic-gate 	return (ret);
34500Sstevel@tonic-gate }
34510Sstevel@tonic-gate 
34520Sstevel@tonic-gate static error_t
34530Sstevel@tonic-gate menu_write(char *root, menu_t *mp)
34540Sstevel@tonic-gate {
34550Sstevel@tonic-gate 	return (list2file(root, MENU_TMP, GRUB_MENU, mp->start));
34560Sstevel@tonic-gate }
34570Sstevel@tonic-gate 
34580Sstevel@tonic-gate static void
34590Sstevel@tonic-gate line_free(line_t *lp)
34600Sstevel@tonic-gate {
34610Sstevel@tonic-gate 	if (lp == NULL)
34620Sstevel@tonic-gate 		return;
34630Sstevel@tonic-gate 
34640Sstevel@tonic-gate 	if (lp->cmd)
34650Sstevel@tonic-gate 		free(lp->cmd);
34660Sstevel@tonic-gate 	if (lp->sep)
34670Sstevel@tonic-gate 		free(lp->sep);
34680Sstevel@tonic-gate 	if (lp->arg)
34690Sstevel@tonic-gate 		free(lp->arg);
34700Sstevel@tonic-gate 	if (lp->line)
34710Sstevel@tonic-gate 		free(lp->line);
34720Sstevel@tonic-gate 	free(lp);
34730Sstevel@tonic-gate }
34740Sstevel@tonic-gate 
34750Sstevel@tonic-gate static void
34760Sstevel@tonic-gate linelist_free(line_t *start)
34770Sstevel@tonic-gate {
34780Sstevel@tonic-gate 	line_t *lp;
34790Sstevel@tonic-gate 
34800Sstevel@tonic-gate 	while (start) {
34810Sstevel@tonic-gate 		lp = start;
34820Sstevel@tonic-gate 		start = start->next;
34830Sstevel@tonic-gate 		line_free(lp);
34840Sstevel@tonic-gate 	}
34850Sstevel@tonic-gate }
34860Sstevel@tonic-gate 
34870Sstevel@tonic-gate static void
34880Sstevel@tonic-gate filelist_free(filelist_t *flistp)
34890Sstevel@tonic-gate {
34900Sstevel@tonic-gate 	linelist_free(flistp->head);
34910Sstevel@tonic-gate 	flistp->head = NULL;
34920Sstevel@tonic-gate 	flistp->tail = NULL;
34930Sstevel@tonic-gate }
34940Sstevel@tonic-gate 
34950Sstevel@tonic-gate static void
34960Sstevel@tonic-gate menu_free(menu_t *mp)
34970Sstevel@tonic-gate {
3498662Sszhou 	entry_t *ent, *tmp;
34990Sstevel@tonic-gate 	assert(mp);
35000Sstevel@tonic-gate 
35010Sstevel@tonic-gate 	if (mp->start)
35020Sstevel@tonic-gate 		linelist_free(mp->start);
3503662Sszhou 	ent = mp->entries;
3504662Sszhou 	while (ent) {
3505662Sszhou 		tmp = ent;
3506662Sszhou 		ent = tmp->next;
3507662Sszhou 		free(tmp);
3508662Sszhou 	}
3509662Sszhou 
35100Sstevel@tonic-gate 	free(mp);
35110Sstevel@tonic-gate }
35120Sstevel@tonic-gate 
35130Sstevel@tonic-gate /*
35140Sstevel@tonic-gate  * Utility routines
35150Sstevel@tonic-gate  */
35160Sstevel@tonic-gate 
35170Sstevel@tonic-gate 
35180Sstevel@tonic-gate /*
35190Sstevel@tonic-gate  * Returns 0 on success
35200Sstevel@tonic-gate  * Any other value indicates an error
35210Sstevel@tonic-gate  */
35220Sstevel@tonic-gate static int
35230Sstevel@tonic-gate exec_cmd(char *cmdline, char *output, int64_t osize)
35240Sstevel@tonic-gate {
35250Sstevel@tonic-gate 	char buf[BUFSIZ];
35260Sstevel@tonic-gate 	int ret;
35270Sstevel@tonic-gate 	FILE *ptr;
35280Sstevel@tonic-gate 	size_t len;
35290Sstevel@tonic-gate 	sigset_t set;
35300Sstevel@tonic-gate 	void (*disp)(int);
35310Sstevel@tonic-gate 
35320Sstevel@tonic-gate 	/*
35330Sstevel@tonic-gate 	 * For security
35340Sstevel@tonic-gate 	 * - only absolute paths are allowed
35350Sstevel@tonic-gate 	 * - set IFS to space and tab
35360Sstevel@tonic-gate 	 */
35370Sstevel@tonic-gate 	if (*cmdline != '/') {
35380Sstevel@tonic-gate 		bam_error(ABS_PATH_REQ, cmdline);
35390Sstevel@tonic-gate 		return (-1);
35400Sstevel@tonic-gate 	}
35410Sstevel@tonic-gate 	(void) putenv("IFS= \t");
35420Sstevel@tonic-gate 
35430Sstevel@tonic-gate 	/*
35440Sstevel@tonic-gate 	 * We may have been exec'ed with SIGCHLD blocked
35450Sstevel@tonic-gate 	 * unblock it here
35460Sstevel@tonic-gate 	 */
35470Sstevel@tonic-gate 	(void) sigemptyset(&set);
35480Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
35490Sstevel@tonic-gate 	if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
35500Sstevel@tonic-gate 		bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno));
35510Sstevel@tonic-gate 		return (-1);
35520Sstevel@tonic-gate 	}
35530Sstevel@tonic-gate 
35540Sstevel@tonic-gate 	/*
35550Sstevel@tonic-gate 	 * Set SIGCHLD disposition to SIG_DFL for popen/pclose
35560Sstevel@tonic-gate 	 */
35570Sstevel@tonic-gate 	disp = sigset(SIGCHLD, SIG_DFL);
35580Sstevel@tonic-gate 	if (disp == SIG_ERR) {
35590Sstevel@tonic-gate 		bam_error(FAILED_SIG, strerror(errno));
35600Sstevel@tonic-gate 		return (-1);
35610Sstevel@tonic-gate 	}
35620Sstevel@tonic-gate 	if (disp == SIG_HOLD) {
35630Sstevel@tonic-gate 		bam_error(BLOCKED_SIG, cmdline);
35640Sstevel@tonic-gate 		return (-1);
35650Sstevel@tonic-gate 	}
35660Sstevel@tonic-gate 
35670Sstevel@tonic-gate 	ptr = popen(cmdline, "r");
35680Sstevel@tonic-gate 	if (ptr == NULL) {
35690Sstevel@tonic-gate 		bam_error(POPEN_FAIL, cmdline, strerror(errno));
35700Sstevel@tonic-gate 		return (-1);
35710Sstevel@tonic-gate 	}
35720Sstevel@tonic-gate 
35730Sstevel@tonic-gate 	/*
35740Sstevel@tonic-gate 	 * If we simply do a pclose() following a popen(), pclose()
35750Sstevel@tonic-gate 	 * will close the reader end of the pipe immediately even
35760Sstevel@tonic-gate 	 * if the child process has not started/exited. pclose()
35770Sstevel@tonic-gate 	 * does wait for cmd to terminate before returning though.
35780Sstevel@tonic-gate 	 * When the executed command writes its output to the pipe
35790Sstevel@tonic-gate 	 * there is no reader process and the command dies with
35800Sstevel@tonic-gate 	 * SIGPIPE. To avoid this we read repeatedly until read
35810Sstevel@tonic-gate 	 * terminates with EOF. This indicates that the command
35820Sstevel@tonic-gate 	 * (writer) has closed the pipe and we can safely do a
35830Sstevel@tonic-gate 	 * pclose().
35840Sstevel@tonic-gate 	 *
35850Sstevel@tonic-gate 	 * Since pclose() does wait for the command to exit,
35860Sstevel@tonic-gate 	 * we can safely reap the exit status of the command
35870Sstevel@tonic-gate 	 * from the value returned by pclose()
35880Sstevel@tonic-gate 	 */
35890Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), ptr) != NULL) {
35900Sstevel@tonic-gate 		/* if (bam_verbose)  XXX */
35910Sstevel@tonic-gate 			bam_print(PRINT_NO_NEWLINE, buf);
35920Sstevel@tonic-gate 		if (output && osize > 0) {
35930Sstevel@tonic-gate 			(void) snprintf(output, osize, "%s", buf);
35940Sstevel@tonic-gate 			len = strlen(buf);
35950Sstevel@tonic-gate 			output += len;
35960Sstevel@tonic-gate 			osize -= len;
35970Sstevel@tonic-gate 		}
35980Sstevel@tonic-gate 	}
35990Sstevel@tonic-gate 
36000Sstevel@tonic-gate 	ret = pclose(ptr);
36010Sstevel@tonic-gate 	if (ret == -1) {
36020Sstevel@tonic-gate 		bam_error(PCLOSE_FAIL, cmdline, strerror(errno));
36030Sstevel@tonic-gate 		return (-1);
36040Sstevel@tonic-gate 	}
36050Sstevel@tonic-gate 
36060Sstevel@tonic-gate 	if (WIFEXITED(ret)) {
36070Sstevel@tonic-gate 		return (WEXITSTATUS(ret));
36080Sstevel@tonic-gate 	} else {
36090Sstevel@tonic-gate 		bam_error(EXEC_FAIL, cmdline, ret);
36100Sstevel@tonic-gate 		return (-1);
36110Sstevel@tonic-gate 	}
36120Sstevel@tonic-gate }
36130Sstevel@tonic-gate 
36140Sstevel@tonic-gate /*
36150Sstevel@tonic-gate  * Since this function returns -1 on error
36160Sstevel@tonic-gate  * it cannot be used to convert -1. However,
36170Sstevel@tonic-gate  * that is sufficient for what we need.
36180Sstevel@tonic-gate  */
36190Sstevel@tonic-gate static long
36200Sstevel@tonic-gate s_strtol(char *str)
36210Sstevel@tonic-gate {
36220Sstevel@tonic-gate 	long l;
36230Sstevel@tonic-gate 	char *res = NULL;
36240Sstevel@tonic-gate 
36250Sstevel@tonic-gate 	if (str == NULL) {
36260Sstevel@tonic-gate 		return (-1);
36270Sstevel@tonic-gate 	}
36280Sstevel@tonic-gate 
36290Sstevel@tonic-gate 	errno = 0;
36300Sstevel@tonic-gate 	l = strtol(str, &res, 10);
36310Sstevel@tonic-gate 	if (errno || *res != '\0') {
36320Sstevel@tonic-gate 		return (-1);
36330Sstevel@tonic-gate 	}
36340Sstevel@tonic-gate 
36350Sstevel@tonic-gate 	return (l);
36360Sstevel@tonic-gate }
36370Sstevel@tonic-gate 
36380Sstevel@tonic-gate /*
36390Sstevel@tonic-gate  * Wrapper around fputs, that adds a newline (since fputs doesn't)
36400Sstevel@tonic-gate  */
36410Sstevel@tonic-gate static int
36420Sstevel@tonic-gate s_fputs(char *str, FILE *fp)
36430Sstevel@tonic-gate {
36440Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
36450Sstevel@tonic-gate 
36460Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s\n", str);
36470Sstevel@tonic-gate 	return (fputs(linebuf, fp));
36480Sstevel@tonic-gate }
36490Sstevel@tonic-gate 
36500Sstevel@tonic-gate /*
36510Sstevel@tonic-gate  * Wrapper around fgets, that strips newlines returned by fgets
36520Sstevel@tonic-gate  */
36530Sstevel@tonic-gate static char *
36540Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp)
36550Sstevel@tonic-gate {
36560Sstevel@tonic-gate 	int n;
36570Sstevel@tonic-gate 
36580Sstevel@tonic-gate 	buf = fgets(buf, buflen, fp);
36590Sstevel@tonic-gate 	if (buf) {
36600Sstevel@tonic-gate 		n = strlen(buf);
36610Sstevel@tonic-gate 		if (n == buflen - 1 && buf[n-1] != '\n')
36620Sstevel@tonic-gate 			bam_error(TOO_LONG, buflen - 1, buf);
36630Sstevel@tonic-gate 		buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1];
36640Sstevel@tonic-gate 	}
36650Sstevel@tonic-gate 
36660Sstevel@tonic-gate 	return (buf);
36670Sstevel@tonic-gate }
36680Sstevel@tonic-gate 
36690Sstevel@tonic-gate static void *
36700Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz)
36710Sstevel@tonic-gate {
36720Sstevel@tonic-gate 	void *ptr;
36730Sstevel@tonic-gate 
36740Sstevel@tonic-gate 	ptr = calloc(nelem, sz);
36750Sstevel@tonic-gate 	if (ptr == NULL) {
36760Sstevel@tonic-gate 		bam_error(NO_MEM, nelem*sz);
36770Sstevel@tonic-gate 		bam_exit(1);
36780Sstevel@tonic-gate 	}
36790Sstevel@tonic-gate 	return (ptr);
36800Sstevel@tonic-gate }
36810Sstevel@tonic-gate 
36820Sstevel@tonic-gate static char *
36830Sstevel@tonic-gate s_strdup(char *str)
36840Sstevel@tonic-gate {
36850Sstevel@tonic-gate 	char *ptr;
36860Sstevel@tonic-gate 
36870Sstevel@tonic-gate 	if (str == NULL)
36880Sstevel@tonic-gate 		return (NULL);
36890Sstevel@tonic-gate 
36900Sstevel@tonic-gate 	ptr = strdup(str);
36910Sstevel@tonic-gate 	if (ptr == NULL) {
36920Sstevel@tonic-gate 		bam_error(NO_MEM, strlen(str) + 1);
36930Sstevel@tonic-gate 		bam_exit(1);
36940Sstevel@tonic-gate 	}
36950Sstevel@tonic-gate 	return (ptr);
36960Sstevel@tonic-gate }
36970Sstevel@tonic-gate 
36980Sstevel@tonic-gate /*
36990Sstevel@tonic-gate  * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients)
37000Sstevel@tonic-gate  * Returns 0 otherwise
37010Sstevel@tonic-gate  */
37020Sstevel@tonic-gate static int
37030Sstevel@tonic-gate is_amd64(void)
37040Sstevel@tonic-gate {
37050Sstevel@tonic-gate 	static int amd64 = -1;
37060Sstevel@tonic-gate 	char isabuf[257];	/* from sysinfo(2) manpage */
37070Sstevel@tonic-gate 
37080Sstevel@tonic-gate 	if (amd64 != -1)
37090Sstevel@tonic-gate 		return (amd64);
37100Sstevel@tonic-gate 
37110Sstevel@tonic-gate 	if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 &&
37120Sstevel@tonic-gate 	    strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0)
37130Sstevel@tonic-gate 		amd64 = 1;
37140Sstevel@tonic-gate 	else if (strstr(isabuf, "i386") == NULL)
37150Sstevel@tonic-gate 		amd64 = 1;		/* diskless server */
37160Sstevel@tonic-gate 	else
37170Sstevel@tonic-gate 		amd64 = 0;
37180Sstevel@tonic-gate 
37190Sstevel@tonic-gate 	return (amd64);
37200Sstevel@tonic-gate }
37210Sstevel@tonic-gate 
37220Sstevel@tonic-gate static void
37230Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s)
37240Sstevel@tonic-gate {
37250Sstevel@tonic-gate 	line_t *lp;
37260Sstevel@tonic-gate 
37270Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
37280Sstevel@tonic-gate 	lp->line = s_strdup(s);
37290Sstevel@tonic-gate 	if (flistp->head == NULL)
37300Sstevel@tonic-gate 		flistp->head = lp;
37310Sstevel@tonic-gate 	else
37320Sstevel@tonic-gate 		flistp->tail->next = lp;
37330Sstevel@tonic-gate 	flistp->tail = lp;
37340Sstevel@tonic-gate }
3735