xref: /onnv-gate/usr/src/cmd/boot/bootadm/bootadm.c (revision 348:5841de0daf2d)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * bootadm(1M) is a new utility for managing bootability of
310Sstevel@tonic-gate  * Solaris *Newboot* environments. It has two primary tasks:
320Sstevel@tonic-gate  * 	- Allow end users to manage bootability of Newboot Solaris instances
330Sstevel@tonic-gate  *	- Provide services to other subsystems in Solaris (primarily Install)
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /* Headers */
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <stdlib.h>
400Sstevel@tonic-gate #include <string.h>
410Sstevel@tonic-gate #include <unistd.h>
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include <sys/stat.h>
440Sstevel@tonic-gate #include <stdarg.h>
450Sstevel@tonic-gate #include <limits.h>
460Sstevel@tonic-gate #include <signal.h>
470Sstevel@tonic-gate #include <sys/wait.h>
480Sstevel@tonic-gate #include <sys/mnttab.h>
490Sstevel@tonic-gate #include <sys/statvfs.h>
500Sstevel@tonic-gate #include <libnvpair.h>
510Sstevel@tonic-gate #include <ftw.h>
520Sstevel@tonic-gate #include <fcntl.h>
530Sstevel@tonic-gate #include <strings.h>
540Sstevel@tonic-gate #include <sys/systeminfo.h>
550Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #include <pwd.h>
580Sstevel@tonic-gate #include <grp.h>
590Sstevel@tonic-gate #include <device_info.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #include <libintl.h>
620Sstevel@tonic-gate #include <locale.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include <assert.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include "message.h"
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #ifndef TEXT_DOMAIN
690Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
700Sstevel@tonic-gate #endif	/* TEXT_DOMAIN */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /* Type definitions */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /* Primary subcmds */
750Sstevel@tonic-gate typedef enum {
760Sstevel@tonic-gate 	BAM_MENU = 3,
770Sstevel@tonic-gate 	BAM_ARCHIVE
780Sstevel@tonic-gate } subcmd_t;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /* GRUB menu per-line classification */
810Sstevel@tonic-gate typedef enum {
820Sstevel@tonic-gate 	BAM_INVALID = 0,
830Sstevel@tonic-gate 	BAM_EMPTY,
840Sstevel@tonic-gate 	BAM_COMMENT,
850Sstevel@tonic-gate 	BAM_GLOBAL,
860Sstevel@tonic-gate 	BAM_ENTRY,
870Sstevel@tonic-gate 	BAM_TITLE
880Sstevel@tonic-gate } menu_flag_t;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /* struct for menu.lst contents */
910Sstevel@tonic-gate typedef struct line {
920Sstevel@tonic-gate 	int  lineNum;	/* Line number in menu.lst */
930Sstevel@tonic-gate 	int  entryNum;	/* menu boot entry #. ENTRY_INIT if not applicable */
940Sstevel@tonic-gate 	char *cmd;
950Sstevel@tonic-gate 	char *sep;
960Sstevel@tonic-gate 	char *arg;
970Sstevel@tonic-gate 	char *line;
980Sstevel@tonic-gate 	menu_flag_t flags;
990Sstevel@tonic-gate 	struct line *next;
1000Sstevel@tonic-gate } line_t;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate typedef struct {
1030Sstevel@tonic-gate 	line_t *start;
1040Sstevel@tonic-gate 	line_t *end;
1050Sstevel@tonic-gate } menu_t;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate typedef enum {
1080Sstevel@tonic-gate     OPT_ABSENT = 0,	/* No option */
1090Sstevel@tonic-gate     OPT_REQ,		/* option required */
1100Sstevel@tonic-gate     OPT_OPTIONAL	/* option may or may not be present */
1110Sstevel@tonic-gate } option_t;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate typedef enum {
114316Svikram     BAM_ERROR = -1,	/* Must be negative. add_boot_entry() depends on it */
1150Sstevel@tonic-gate     BAM_SUCCESS = 0,
1160Sstevel@tonic-gate     BAM_WRITE = 2
1170Sstevel@tonic-gate } error_t;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate typedef struct {
1200Sstevel@tonic-gate 	char	*subcmd;
1210Sstevel@tonic-gate 	option_t option;
1220Sstevel@tonic-gate 	error_t (*handler)();
1230Sstevel@tonic-gate } subcmd_defn_t;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate #define	BAM_MAXLINE	8192
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate #define	LINE_INIT	0	/* lineNum initial value */
1290Sstevel@tonic-gate #define	ENTRY_INIT	-1	/* entryNum initial value */
1300Sstevel@tonic-gate #define	ALL_ENTRIES	-2	/* selects all boot entries */
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate #define	GRUB_DIR		"/boot/grub"
1330Sstevel@tonic-gate #define	MULTI_BOOT		"/platform/i86pc/multiboot"
1340Sstevel@tonic-gate #define	BOOT_ARCHIVE		"/platform/i86pc/boot_archive"
1350Sstevel@tonic-gate #define	GRUB_MENU		"/boot/grub/menu.lst"
1360Sstevel@tonic-gate #define	MENU_TMP		"/boot/grub/menu.lst.tmp"
1370Sstevel@tonic-gate #define	RAMDISK_SPECIAL		"/ramdisk"
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /* lock related */
1400Sstevel@tonic-gate #define	BAM_LOCK_FILE		"/var/run/bootadm.lock"
1410Sstevel@tonic-gate #define	LOCK_FILE_PERMS		(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate #define	CREATE_RAMDISK		"/boot/solaris/bin/create_ramdisk"
1440Sstevel@tonic-gate #define	CREATE_DISKMAP		"/boot/solaris/bin/create_diskmap"
1450Sstevel@tonic-gate #define	GRUBDISK_MAP		"/var/run/solaris_grubdisk.map"
1460Sstevel@tonic-gate 
147316Svikram #define	GRUB_slice		"/etc/lu/GRUB_slice"
148316Svikram #define	GRUB_root		"/etc/lu/GRUB_root"
149316Svikram #define	GRUB_backup_menu	"/etc/lu/GRUB_backup_menu"
150316Svikram #define	GRUB_slice_mntpt	"/tmp/GRUB_slice_mntpt"
151316Svikram #define	LU_ACTIVATE_FILE	"/etc/lu/DelayUpdate/activate.sh"
152316Svikram 
153316Svikram #define	INSTALLGRUB		"/sbin/installgrub"
154316Svikram #define	STAGE1			"/boot/grub/stage1"
155316Svikram #define	STAGE2			"/boot/grub/stage2"
156316Svikram 
157316Svikram #define	QUIET			1
158316Svikram 
159316Svikram 
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate  * Default file attributes
1620Sstevel@tonic-gate  */
1630Sstevel@tonic-gate #define	DEFAULT_DEV_MODE	0644	/* default permissions */
1640Sstevel@tonic-gate #define	DEFAULT_DEV_UID		0	/* user root */
1650Sstevel@tonic-gate #define	DEFAULT_DEV_GID		3	/* group sys */
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate  * Menu related
1690Sstevel@tonic-gate  * menu_cmd_t and menu_cmds must be kept in sync
1700Sstevel@tonic-gate  */
1710Sstevel@tonic-gate typedef enum {
1720Sstevel@tonic-gate 	DEFAULT_CMD = 0,
1730Sstevel@tonic-gate 	TIMEOUT_CMD,
1740Sstevel@tonic-gate 	TITLE_CMD,
1750Sstevel@tonic-gate 	ROOT_CMD,
1760Sstevel@tonic-gate 	KERNEL_CMD,
1770Sstevel@tonic-gate 	MODULE_CMD,
1780Sstevel@tonic-gate 	SEP_CMD,
1790Sstevel@tonic-gate 	COMMENT_CMD
1800Sstevel@tonic-gate } menu_cmd_t;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate static char *menu_cmds[] = {
1830Sstevel@tonic-gate 	"default",	/* DEFAULT_CMD */
1840Sstevel@tonic-gate 	"timeout",	/* TIMEOUT_CMD */
1850Sstevel@tonic-gate 	"title",	/* TITLE_CMD */
1860Sstevel@tonic-gate 	"root",		/* ROOT_CMD */
1870Sstevel@tonic-gate 	"kernel",	/* KERNEL_CMD */
1880Sstevel@tonic-gate 	"module",	/* MODULE_CMD */
1890Sstevel@tonic-gate 	" ",		/* SEP_CMD */
1900Sstevel@tonic-gate 	"#",		/* COMMENT_CMD */
1910Sstevel@tonic-gate 	NULL
1920Sstevel@tonic-gate };
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate #define	OPT_ENTRY_NUM	"entry"
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate  * archive related
1980Sstevel@tonic-gate  */
1990Sstevel@tonic-gate typedef struct {
2000Sstevel@tonic-gate 	line_t *head;
2010Sstevel@tonic-gate 	line_t *tail;
2020Sstevel@tonic-gate } filelist_t;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate #define	BOOT_FILE_LIST	"boot/solaris/filelist.ramdisk"
2050Sstevel@tonic-gate #define	ETC_FILE_LIST	"etc/boot/solaris/filelist.ramdisk"
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate #define	FILE_STAT	"boot/solaris/filestat.ramdisk"
2080Sstevel@tonic-gate #define	FILE_STAT_TMP	"boot/solaris/filestat.ramdisk.tmp"
2090Sstevel@tonic-gate #define	DIR_PERMS	(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
2100Sstevel@tonic-gate #define	FILE_STAT_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate #define	BAM_HDR		"---------- ADDED BY BOOTADM - DO NOT EDIT ----------"
2130Sstevel@tonic-gate #define	BAM_FTR		"---------------------END BOOTADM--------------------"
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate /* Globals */
2170Sstevel@tonic-gate static char *prog;
2180Sstevel@tonic-gate static subcmd_t bam_cmd;
2190Sstevel@tonic-gate static char *bam_root;
2200Sstevel@tonic-gate static int bam_rootlen;
2210Sstevel@tonic-gate static int bam_root_readonly;
2220Sstevel@tonic-gate static char *bam_subcmd;
2230Sstevel@tonic-gate static char *bam_opt;
2240Sstevel@tonic-gate static int bam_debug;
2250Sstevel@tonic-gate static char **bam_argv;
2260Sstevel@tonic-gate static int bam_argc;
2270Sstevel@tonic-gate static int bam_force;
2280Sstevel@tonic-gate static int bam_verbose;
2290Sstevel@tonic-gate static int bam_check;
2300Sstevel@tonic-gate static int bam_smf_check;
2310Sstevel@tonic-gate static int bam_lock_fd = -1;
2320Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/";
233316Svikram static int bam_update_all;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate /* function prototypes */
2360Sstevel@tonic-gate static void parse_args_internal(int argc, char *argv[]);
2370Sstevel@tonic-gate static void parse_args(int argc, char *argv[]);
2380Sstevel@tonic-gate static error_t bam_menu(char *subcmd, char *opt, int argc, char *argv[]);
2390Sstevel@tonic-gate static error_t bam_archive(char *subcmd, char *opt);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate static void bam_error(char *format, ...);
2420Sstevel@tonic-gate static void bam_print(char *format, ...);
2430Sstevel@tonic-gate static void bam_exit(int excode);
2440Sstevel@tonic-gate static void bam_lock(void);
2450Sstevel@tonic-gate static void bam_unlock(void);
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate static int exec_cmd(char *cmdline, char *output, int64_t osize);
2480Sstevel@tonic-gate static error_t read_globals(menu_t *mp, char *menu_path,
2490Sstevel@tonic-gate     char *globalcmd, int quiet);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate static menu_t *menu_read(char *menu_path);
2520Sstevel@tonic-gate static error_t menu_write(char *root, menu_t *mp);
2530Sstevel@tonic-gate static void linelist_free(line_t *start);
2540Sstevel@tonic-gate static void menu_free(menu_t *mp);
2550Sstevel@tonic-gate static void line_free(line_t *lp);
2560Sstevel@tonic-gate static void filelist_free(filelist_t *flistp);
2570Sstevel@tonic-gate static error_t list2file(char *root, char *tmp,
2580Sstevel@tonic-gate     char *final, line_t *start);
2590Sstevel@tonic-gate static error_t list_entry(menu_t *mp, char *menu_path, char *opt);
2600Sstevel@tonic-gate static error_t delete_entry(menu_t *mp, char *menu_path, char *opt);
2610Sstevel@tonic-gate static error_t delete_all_entries(menu_t *mp, char *menu_path, char *opt);
2620Sstevel@tonic-gate static error_t update_entry(menu_t *mp, char *root, char *opt);
2630Sstevel@tonic-gate static error_t update_temp(menu_t *mp, char *root, char *opt);
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate static error_t update_archive(char *root, char *opt);
2660Sstevel@tonic-gate static error_t list_archive(char *root, char *opt);
2670Sstevel@tonic-gate static error_t update_all(char *root, char *opt);
2680Sstevel@tonic-gate static error_t read_list(char *root, filelist_t  *flistp);
2690Sstevel@tonic-gate static error_t set_global(menu_t *mp, char *globalcmd, int val);
2700Sstevel@tonic-gate static error_t set_option(menu_t *mp, char *globalcmd, char *opt);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate static long s_strtol(char *str);
2730Sstevel@tonic-gate static char *s_fgets(char *buf, int n, FILE *fp);
2740Sstevel@tonic-gate static int s_fputs(char *str, FILE *fp);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate static void *s_calloc(size_t nelem, size_t sz);
2770Sstevel@tonic-gate static char *s_strdup(char *str);
2780Sstevel@tonic-gate static int is_readonly(char *);
2790Sstevel@tonic-gate static int is_amd64(void);
2800Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate #if defined(__sparc)
2830Sstevel@tonic-gate static void sparc_abort(void);
2840Sstevel@tonic-gate #endif
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate /* Menu related sub commands */
2870Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = {
2880Sstevel@tonic-gate 	"set_option",		OPT_OPTIONAL,	set_option,	/* PUB */
2890Sstevel@tonic-gate 	"list_entry",		OPT_OPTIONAL,	list_entry,	/* PUB */
2900Sstevel@tonic-gate 	"delete_all_entries",	OPT_ABSENT,	delete_all_entries, /* PVT */
2910Sstevel@tonic-gate 	"update_entry",		OPT_REQ,	update_entry,	/* menu */
2920Sstevel@tonic-gate 	"update_temp",		OPT_OPTIONAL,	update_temp,	/* reboot */
2930Sstevel@tonic-gate 	NULL,			0,		NULL	/* must be last */
2940Sstevel@tonic-gate };
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate /* Archive related sub commands */
2970Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = {
2980Sstevel@tonic-gate 	"update",		OPT_ABSENT,	update_archive, /* PUB */
2990Sstevel@tonic-gate 	"update_all",		OPT_ABSENT,	update_all,	/* PVT */
3000Sstevel@tonic-gate 	"list",			OPT_OPTIONAL,	list_archive,	/* PUB */
3010Sstevel@tonic-gate 	NULL,			0,		NULL	/* must be last */
3020Sstevel@tonic-gate };
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate static struct {
3050Sstevel@tonic-gate 	nvlist_t *new_nvlp;
3060Sstevel@tonic-gate 	nvlist_t *old_nvlp;
3070Sstevel@tonic-gate 	int need_update;
3080Sstevel@tonic-gate } walk_arg;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate static void
3110Sstevel@tonic-gate usage(void)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate 	(void) fprintf(stderr, "USAGE:\n");
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	/* archive usage */
3170Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s update-archive [-vn] [-R altroot]\n",
3180Sstevel@tonic-gate 	    prog);
3190Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-archive [-R altroot]\n", prog);
3200Sstevel@tonic-gate #ifndef __sparc
3210Sstevel@tonic-gate 	/* x86 only */
3220Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog);
3230Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog);
3240Sstevel@tonic-gate #endif
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate int
3280Sstevel@tonic-gate main(int argc, char *argv[])
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate 	error_t ret;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3330Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
3360Sstevel@tonic-gate 		prog = argv[0];
3370Sstevel@tonic-gate 	} else {
3380Sstevel@tonic-gate 		prog++;
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	if (geteuid() != 0) {
3420Sstevel@tonic-gate 		bam_error(MUST_BE_ROOT);
3430Sstevel@tonic-gate 		bam_exit(1);
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	bam_lock();
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	/*
3490Sstevel@tonic-gate 	 * Don't depend on caller's umask
3500Sstevel@tonic-gate 	 */
3510Sstevel@tonic-gate 	(void) umask(0022);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	parse_args(argc, argv);
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate #if defined(__sparc)
3560Sstevel@tonic-gate 	/*
3570Sstevel@tonic-gate 	 * There are only two valid invocations of bootadm
3580Sstevel@tonic-gate 	 * on SPARC:
3590Sstevel@tonic-gate 	 *
3600Sstevel@tonic-gate 	 *	- SPARC diskless server creating boot_archive for i386 clients
3610Sstevel@tonic-gate 	 *	- archive creation call during reboot of a SPARC system
3620Sstevel@tonic-gate 	 *
3630Sstevel@tonic-gate 	 *	The latter should be a NOP
3640Sstevel@tonic-gate 	 */
3650Sstevel@tonic-gate 	if (bam_cmd != BAM_ARCHIVE) {
3660Sstevel@tonic-gate 		sparc_abort();
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate #endif
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	switch (bam_cmd) {
3710Sstevel@tonic-gate 		case BAM_MENU:
3720Sstevel@tonic-gate 			ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv);
3730Sstevel@tonic-gate 			break;
3740Sstevel@tonic-gate 		case BAM_ARCHIVE:
3750Sstevel@tonic-gate 			ret = bam_archive(bam_subcmd, bam_opt);
3760Sstevel@tonic-gate 			break;
3770Sstevel@tonic-gate 		default:
3780Sstevel@tonic-gate 			usage();
3790Sstevel@tonic-gate 			bam_exit(1);
3800Sstevel@tonic-gate 	}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	if (ret != BAM_SUCCESS)
3830Sstevel@tonic-gate 		bam_exit(1);
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	bam_unlock();
3860Sstevel@tonic-gate 	return (0);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate #if defined(__sparc)
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate static void
3920Sstevel@tonic-gate sparc_abort(void)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	bam_error(NOT_ON_SPARC);
3950Sstevel@tonic-gate 	bam_exit(1);
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate #endif
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate /*
4010Sstevel@tonic-gate  * Equivalence of public and internal commands:
4020Sstevel@tonic-gate  *	update-archive  -- -a update
4030Sstevel@tonic-gate  *	list-archive	-- -a list
4040Sstevel@tonic-gate  *	set-menu	-- -m set_option
4050Sstevel@tonic-gate  *	list-menu	-- -m list_entry
4060Sstevel@tonic-gate  *	update-menu	-- -m update_entry
4070Sstevel@tonic-gate  */
4080Sstevel@tonic-gate static struct cmd_map {
4090Sstevel@tonic-gate 	char *bam_cmdname;
4100Sstevel@tonic-gate 	int bam_cmd;
4110Sstevel@tonic-gate 	char *bam_subcmd;
4120Sstevel@tonic-gate } cmd_map[] = {
4130Sstevel@tonic-gate 	{ "update-archive",	BAM_ARCHIVE,	"update"},
4140Sstevel@tonic-gate 	{ "list-archive",	BAM_ARCHIVE,	"list"},
4150Sstevel@tonic-gate 	{ "set-menu",		BAM_MENU,	"set_option"},
4160Sstevel@tonic-gate 	{ "list-menu",		BAM_MENU,	"list_entry"},
4170Sstevel@tonic-gate 	{ "update-menu",	BAM_MENU,	"update_entry"},
4180Sstevel@tonic-gate 	{ NULL,			0,		NULL}
4190Sstevel@tonic-gate };
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate /*
4220Sstevel@tonic-gate  * Commands syntax published in bootadm(1M) are parsed here
4230Sstevel@tonic-gate  */
4240Sstevel@tonic-gate static void
4250Sstevel@tonic-gate parse_args(int argc, char *argv[])
4260Sstevel@tonic-gate {
4270Sstevel@tonic-gate 	struct cmd_map *cmp = cmd_map;
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	/* command conforming to the final spec */
4300Sstevel@tonic-gate 	if (argc > 1 && argv[1][0] != '-') {
4310Sstevel@tonic-gate 		/*
4320Sstevel@tonic-gate 		 * Map commands to internal table.
4330Sstevel@tonic-gate 		 */
4340Sstevel@tonic-gate 		while (cmp->bam_cmdname) {
4350Sstevel@tonic-gate 			if (strcmp(argv[1], cmp->bam_cmdname) == 0) {
4360Sstevel@tonic-gate 				bam_cmd = cmp->bam_cmd;
4370Sstevel@tonic-gate 				bam_subcmd = cmp->bam_subcmd;
4380Sstevel@tonic-gate 				break;
4390Sstevel@tonic-gate 			}
4400Sstevel@tonic-gate 			cmp++;
4410Sstevel@tonic-gate 		}
4420Sstevel@tonic-gate 		if (cmp->bam_cmdname == NULL) {
4430Sstevel@tonic-gate 			usage();
4440Sstevel@tonic-gate 			bam_exit(1);
4450Sstevel@tonic-gate 		}
4460Sstevel@tonic-gate 		argc--;
4470Sstevel@tonic-gate 		argv++;
4480Sstevel@tonic-gate 	}
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	parse_args_internal(argc, argv);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate /*
4540Sstevel@tonic-gate  * A combination of public and private commands are parsed here.
4550Sstevel@tonic-gate  * The internal syntax and the corresponding functionality are:
4560Sstevel@tonic-gate  *	-a update	-- update-archive
4570Sstevel@tonic-gate  *	-a list		-- list-archive
4580Sstevel@tonic-gate  *	-a update-all	-- (reboot to sync all mounted OS archive)
4590Sstevel@tonic-gate  *	-m update_entry	-- update-menu
4600Sstevel@tonic-gate  *	-m list_entry	-- list-menu
4610Sstevel@tonic-gate  *	-m update_temp	-- (reboot -- [boot-args])
4620Sstevel@tonic-gate  *	-m delete_all_entries -- (called from install)
4630Sstevel@tonic-gate  */
4640Sstevel@tonic-gate static void
4650Sstevel@tonic-gate parse_args_internal(int argc, char *argv[])
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate 	int c, error;
4680Sstevel@tonic-gate 	extern char *optarg;
4690Sstevel@tonic-gate 	extern int optind, opterr;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	/* Suppress error message from getopt */
4720Sstevel@tonic-gate 	opterr = 0;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	error = 0;
4750Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "a:d:fm:no:vR:")) != -1) {
4760Sstevel@tonic-gate 		switch (c) {
4770Sstevel@tonic-gate 		case 'a':
4780Sstevel@tonic-gate 			if (bam_cmd) {
4790Sstevel@tonic-gate 				error = 1;
4800Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
4810Sstevel@tonic-gate 			}
4820Sstevel@tonic-gate 			bam_cmd = BAM_ARCHIVE;
4830Sstevel@tonic-gate 			bam_subcmd = optarg;
4840Sstevel@tonic-gate 			break;
4850Sstevel@tonic-gate 		case 'd':
4860Sstevel@tonic-gate 			if (bam_debug) {
4870Sstevel@tonic-gate 				error = 1;
4880Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4890Sstevel@tonic-gate 			}
4900Sstevel@tonic-gate 			bam_debug = s_strtol(optarg);
4910Sstevel@tonic-gate 			break;
4920Sstevel@tonic-gate 		case 'f':
4930Sstevel@tonic-gate 			if (bam_force) {
4940Sstevel@tonic-gate 				error = 1;
4950Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4960Sstevel@tonic-gate 			}
4970Sstevel@tonic-gate 			bam_force = 1;
4980Sstevel@tonic-gate 			break;
4990Sstevel@tonic-gate 		case 'm':
5000Sstevel@tonic-gate 			if (bam_cmd) {
5010Sstevel@tonic-gate 				error = 1;
5020Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
5030Sstevel@tonic-gate 			}
5040Sstevel@tonic-gate 			bam_cmd = BAM_MENU;
5050Sstevel@tonic-gate 			bam_subcmd = optarg;
5060Sstevel@tonic-gate 			break;
5070Sstevel@tonic-gate 		case 'n':
5080Sstevel@tonic-gate 			if (bam_check) {
5090Sstevel@tonic-gate 				error = 1;
5100Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5110Sstevel@tonic-gate 			}
5120Sstevel@tonic-gate 			bam_check = 1;
5130Sstevel@tonic-gate 			bam_smf_check = bam_root_readonly;
5140Sstevel@tonic-gate 			break;
5150Sstevel@tonic-gate 		case 'o':
5160Sstevel@tonic-gate 			if (bam_opt) {
5170Sstevel@tonic-gate 				error = 1;
5180Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5190Sstevel@tonic-gate 			}
5200Sstevel@tonic-gate 			bam_opt = optarg;
5210Sstevel@tonic-gate 			break;
5220Sstevel@tonic-gate 		case 'v':
5230Sstevel@tonic-gate 			if (bam_verbose) {
5240Sstevel@tonic-gate 				error = 1;
5250Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5260Sstevel@tonic-gate 			}
5270Sstevel@tonic-gate 			bam_verbose = 1;
5280Sstevel@tonic-gate 			break;
5290Sstevel@tonic-gate 		case 'R':
5300Sstevel@tonic-gate 			if (bam_root) {
5310Sstevel@tonic-gate 				error = 1;
5320Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5330Sstevel@tonic-gate 				break;
5340Sstevel@tonic-gate 			} else if (realpath(optarg, rootbuf) == NULL) {
5350Sstevel@tonic-gate 				error = 1;
5360Sstevel@tonic-gate 				bam_error(CANT_RESOLVE, optarg,
5370Sstevel@tonic-gate 				    strerror(errno));
5380Sstevel@tonic-gate 				break;
5390Sstevel@tonic-gate 			}
5400Sstevel@tonic-gate 			bam_root = rootbuf;
5410Sstevel@tonic-gate 			if (rootbuf[strlen(rootbuf) - 1] != '/')
5420Sstevel@tonic-gate 				(void) strcat(rootbuf, "/");
5430Sstevel@tonic-gate 			bam_rootlen = strlen(rootbuf);
5440Sstevel@tonic-gate 			break;
5450Sstevel@tonic-gate 		case '?':
5460Sstevel@tonic-gate 			error = 1;
5470Sstevel@tonic-gate 			bam_error(BAD_OPT, optopt);
5480Sstevel@tonic-gate 			break;
5490Sstevel@tonic-gate 		default :
5500Sstevel@tonic-gate 			error = 1;
5510Sstevel@tonic-gate 			bam_error(BAD_OPT, c);
5520Sstevel@tonic-gate 			break;
5530Sstevel@tonic-gate 		}
5540Sstevel@tonic-gate 	}
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	/*
5570Sstevel@tonic-gate 	 * A command option must be specfied
5580Sstevel@tonic-gate 	 */
5590Sstevel@tonic-gate 	if (!bam_cmd) {
5600Sstevel@tonic-gate 		if (bam_opt && strcmp(bam_opt, "all") == 0) {
5610Sstevel@tonic-gate 			usage();
5620Sstevel@tonic-gate 			bam_exit(0);
5630Sstevel@tonic-gate 		}
5640Sstevel@tonic-gate 		bam_error(NEED_CMD);
5650Sstevel@tonic-gate 		error = 1;
5660Sstevel@tonic-gate 	}
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	if (error) {
5690Sstevel@tonic-gate 		usage();
5700Sstevel@tonic-gate 		bam_exit(1);
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	if (optind > argc) {
5740Sstevel@tonic-gate 		bam_error(INT_ERROR, "parse_args");
5750Sstevel@tonic-gate 		bam_exit(1);
5760Sstevel@tonic-gate 	} else if (optind < argc) {
5770Sstevel@tonic-gate 		bam_argv = &argv[optind];
5780Sstevel@tonic-gate 		bam_argc = argc - optind;
5790Sstevel@tonic-gate 	}
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	/*
5820Sstevel@tonic-gate 	 * -n implies verbose mode
5830Sstevel@tonic-gate 	 */
5840Sstevel@tonic-gate 	if (bam_check)
5850Sstevel@tonic-gate 		bam_verbose = 1;
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate static error_t
5890Sstevel@tonic-gate check_subcmd_and_options(
5900Sstevel@tonic-gate 	char *subcmd,
5910Sstevel@tonic-gate 	char *opt,
5920Sstevel@tonic-gate 	subcmd_defn_t *table,
5930Sstevel@tonic-gate 	error_t (**fp)())
5940Sstevel@tonic-gate {
5950Sstevel@tonic-gate 	int i;
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	if (subcmd == NULL) {
5980Sstevel@tonic-gate 		bam_error(NEED_SUBCMD);
5990Sstevel@tonic-gate 		return (BAM_ERROR);
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	if (bam_root == NULL) {
6030Sstevel@tonic-gate 		bam_root = rootbuf;
6040Sstevel@tonic-gate 		bam_rootlen = 1;
6050Sstevel@tonic-gate 	}
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	/* verify that subcmd is valid */
6080Sstevel@tonic-gate 	for (i = 0; table[i].subcmd != NULL; i++) {
6090Sstevel@tonic-gate 		if (strcmp(table[i].subcmd, subcmd) == 0)
6100Sstevel@tonic-gate 			break;
6110Sstevel@tonic-gate 	}
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	if (table[i].subcmd == NULL) {
6140Sstevel@tonic-gate 		bam_error(INVALID_SUBCMD, subcmd);
6150Sstevel@tonic-gate 		return (BAM_ERROR);
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	/* subcmd verifies that opt is appropriate */
6190Sstevel@tonic-gate 	if (table[i].option != OPT_OPTIONAL) {
6200Sstevel@tonic-gate 		if ((table[i].option == OPT_REQ) ^ (opt != NULL)) {
6210Sstevel@tonic-gate 			if (opt)
6220Sstevel@tonic-gate 				bam_error(NO_OPT_REQ, subcmd);
6230Sstevel@tonic-gate 			else
6240Sstevel@tonic-gate 				bam_error(MISS_OPT, subcmd);
6250Sstevel@tonic-gate 			return (BAM_ERROR);
6260Sstevel@tonic-gate 		}
6270Sstevel@tonic-gate 	}
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	*fp = table[i].handler;
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	return (BAM_SUCCESS);
6320Sstevel@tonic-gate }
6330Sstevel@tonic-gate 
634316Svikram 
635316Svikram static char *
636316Svikram mount_grub_slice(int *mnted, char **physlice, int mode)
637316Svikram {
638316Svikram 	struct extmnttab mnt;
639316Svikram 	struct stat sb;
640316Svikram 	char buf[BAM_MAXLINE], dev[PATH_MAX], phys[PATH_MAX], fstype[32];
641*348Svikram 	char cmd[PATH_MAX];
642316Svikram 	char *mntpt;
643316Svikram 	int p, l, f;
644316Svikram 	FILE *fp;
645316Svikram 
646316Svikram 	assert(mnted);
647316Svikram 	*mnted = 0;
648316Svikram 
649316Svikram 	/*
650316Svikram 	 * physlice arg may be NULL
651316Svikram 	 */
652316Svikram 	if (physlice)
653316Svikram 		*physlice = NULL;
654316Svikram 
655316Svikram 	if (stat(GRUB_slice, &sb) != 0) {
656316Svikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
657316Svikram 		return (NULL);
658316Svikram 	}
659316Svikram 
660316Svikram 	fp = fopen(GRUB_slice, "r");
661316Svikram 	if (fp == NULL) {
662316Svikram 		bam_error(OPEN_FAIL, GRUB_slice, strerror(errno));
663316Svikram 		return (NULL);
664316Svikram 	}
665316Svikram 
666316Svikram 	dev[0] = fstype[0] = phys[0] = '\0';
667316Svikram 	p = sizeof ("PHYS_SLICE=") - 1;
668316Svikram 	l = sizeof ("LOG_SLICE=") - 1;
669316Svikram 	f = sizeof ("LOG_FSTYP=") - 1;
670316Svikram 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
671316Svikram 		if (strncmp(buf, "PHYS_SLICE=", p) == 0) {
672316Svikram 			(void) strlcpy(phys, buf + p, sizeof (phys));
673316Svikram 			continue;
674316Svikram 		}
675316Svikram 		if (strncmp(buf, "LOG_SLICE=", l) == 0) {
676316Svikram 			(void) strlcpy(dev, buf + l, sizeof (dev));
677316Svikram 			continue;
678316Svikram 		}
679316Svikram 		if (strncmp(buf, "LOG_FSTYP=", f) == 0) {
680316Svikram 			(void) strlcpy(fstype, buf + f, sizeof (fstype));
681316Svikram 			continue;
682316Svikram 		}
683316Svikram 	}
684316Svikram 	(void) fclose(fp);
685316Svikram 
686316Svikram 	if (dev[0] == '\0' || fstype[0] == '\0' || phys[0] == '\0') {
687316Svikram 		bam_error(BAD_SLICE_FILE, GRUB_slice);
688316Svikram 		return (NULL);
689316Svikram 	}
690316Svikram 
691316Svikram 	if (mode != QUIET)
692316Svikram 		bam_print(USING_GRUB_SLICE, dev);
693316Svikram 
694316Svikram 	if (physlice) {
695316Svikram 		*physlice = s_strdup(phys);
696316Svikram 	}
697316Svikram 
698316Svikram 	/*
699316Svikram 	 * Check if the slice is already mounted
700316Svikram 	 */
701316Svikram 	fp = fopen(MNTTAB, "r");
702316Svikram 	if (fp == NULL) {
703316Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
704316Svikram 		if (physlice) {
705316Svikram 			free(*physlice);
706316Svikram 			*physlice = NULL;
707316Svikram 		}
708316Svikram 		return (NULL);
709316Svikram 	}
710316Svikram 
711316Svikram 	resetmnttab(fp);
712316Svikram 
713316Svikram 	mntpt = NULL;
714316Svikram 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
715316Svikram 		if (strcmp(mnt.mnt_special, dev) == 0) {
716316Svikram 			mntpt = s_strdup(mnt.mnt_mountp);
717316Svikram 			break;
718316Svikram 		}
719316Svikram 	}
720316Svikram 
721316Svikram 	(void) fclose(fp);
722316Svikram 
723316Svikram 	if (mntpt) {
724316Svikram 		return (mntpt);
725316Svikram 	}
726316Svikram 
727316Svikram 
728316Svikram 	/*
729316Svikram 	 * GRUB slice is not mounted, we need to mount it now.
730316Svikram 	 * First create the mountpoint
731316Svikram 	 */
732316Svikram 	mntpt = s_calloc(1, PATH_MAX);
733316Svikram 	(void) snprintf(mntpt, PATH_MAX, "%s.%d", GRUB_slice_mntpt, getpid());
734316Svikram 	if (mkdir(mntpt, 0755) == -1 && errno != EEXIST) {
735316Svikram 		bam_error(MKDIR_FAILED, mntpt, strerror(errno));
736316Svikram 		free(mntpt);
737316Svikram 		if (physlice) {
738316Svikram 			free(*physlice);
739316Svikram 			*physlice = NULL;
740316Svikram 		}
741316Svikram 		return (NULL);
742316Svikram 	}
743316Svikram 
744*348Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/mount -F %s %s %s",
745*348Svikram 	    fstype, dev, mntpt);
746*348Svikram 
747*348Svikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
748*348Svikram 		bam_error(MOUNT_FAILED, dev, fstype, mntpt);
749316Svikram 		if (rmdir(mntpt) != 0) {
750316Svikram 			bam_error(RMDIR_FAILED, mntpt, strerror(errno));
751316Svikram 		}
752316Svikram 		free(mntpt);
753316Svikram 		if (physlice) {
754316Svikram 			free(*physlice);
755316Svikram 			*physlice = NULL;
756316Svikram 		}
757316Svikram 		return (NULL);
758316Svikram 	}
759316Svikram 
760316Svikram 	*mnted = 1;
761316Svikram 	return (mntpt);
762316Svikram }
763316Svikram 
764316Svikram static void
765316Svikram umount_grub_slice(int mnted, char *mntpt, char *physlice)
766316Svikram {
767*348Svikram 	char cmd[PATH_MAX];
768*348Svikram 
769316Svikram 	/*
770316Svikram 	 * If we have not dealt with GRUB slice
771316Svikram 	 * we have nothing to do - just return.
772316Svikram 	 */
773316Svikram 	if (mntpt == NULL)
774316Svikram 		return;
775316Svikram 
776316Svikram 
777316Svikram 	/*
778316Svikram 	 * If we mounted the filesystem earlier in mount_grub_slice()
779316Svikram 	 * unmount it now.
780316Svikram 	 */
781316Svikram 	if (mnted) {
782*348Svikram 		(void) snprintf(cmd, sizeof (cmd), "/sbin/umount %s",
783*348Svikram 		    mntpt);
784*348Svikram 		if (exec_cmd(cmd, NULL, 0) != 0) {
785*348Svikram 			bam_error(UMOUNT_FAILED, mntpt);
786316Svikram 		}
787316Svikram 		if (rmdir(mntpt) != 0) {
788316Svikram 			bam_error(RMDIR_FAILED, mntpt, strerror(errno));
789316Svikram 		}
790316Svikram 	}
791316Svikram 	if (physlice)
792316Svikram 		free(physlice);
793316Svikram 	free(mntpt);
794316Svikram }
795316Svikram 
7960Sstevel@tonic-gate static error_t
7970Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[])
7980Sstevel@tonic-gate {
7990Sstevel@tonic-gate 	error_t ret;
8000Sstevel@tonic-gate 	char menu_path[PATH_MAX];
8010Sstevel@tonic-gate 	menu_t *menu;
802316Svikram 	char *mntpt, *menu_root;
803316Svikram 	struct stat sb;
804316Svikram 	int mnted;	/* set if we did a mount */
805316Svikram 	int mode;
8060Sstevel@tonic-gate 	error_t (*f)(menu_t *mp, char *menu_path, char *opt);
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	/*
8090Sstevel@tonic-gate 	 * Check arguments
8100Sstevel@tonic-gate 	 */
8110Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
8120Sstevel@tonic-gate 	if (ret == BAM_ERROR) {
8130Sstevel@tonic-gate 		return (BAM_ERROR);
8140Sstevel@tonic-gate 	}
8150Sstevel@tonic-gate 
816316Svikram 	if (strcmp(subcmd, "update_temp") == 0)
817316Svikram 		mode = QUIET;
818316Svikram 	else
819316Svikram 		mode = 0;
820316Svikram 
821316Svikram 	mntpt = NULL;
822316Svikram 	mnted = 0;
823316Svikram 	if (stat(GRUB_slice, &sb) == 0) {
824316Svikram 		mntpt = mount_grub_slice(&mnted, NULL, mode);
825316Svikram 		if (mntpt == NULL) {
826316Svikram 			return (BAM_ERROR);
827316Svikram 		}
828316Svikram 		menu_root = mntpt;
829316Svikram 	} else {
830316Svikram 		menu_root = bam_root;
831316Svikram 	}
832316Svikram 
8330Sstevel@tonic-gate 	(void) snprintf(menu_path, sizeof (menu_path), "%s%s",
834316Svikram 	    menu_root, GRUB_MENU);
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	menu = menu_read(menu_path);
8370Sstevel@tonic-gate 	assert(menu);
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	/*
8400Sstevel@tonic-gate 	 * Special handling for setting timeout and default
8410Sstevel@tonic-gate 	 */
8420Sstevel@tonic-gate 	if (strcmp(subcmd, "set_option") == 0) {
8430Sstevel@tonic-gate 		if (largc != 1 || largv[0] == NULL) {
8440Sstevel@tonic-gate 			usage();
845316Svikram 			umount_grub_slice(mnted, mntpt, NULL);
8460Sstevel@tonic-gate 			return (BAM_ERROR);
8470Sstevel@tonic-gate 		}
8480Sstevel@tonic-gate 		opt = largv[0];
8490Sstevel@tonic-gate 	} else if (largc != 0) {
8500Sstevel@tonic-gate 		usage();
851316Svikram 		umount_grub_slice(mnted, mntpt, NULL);
8520Sstevel@tonic-gate 		return (BAM_ERROR);
8530Sstevel@tonic-gate 	}
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	/*
8560Sstevel@tonic-gate 	 * Once the sub-cmd handler has run
8570Sstevel@tonic-gate 	 * only the line field is guaranteed to have valid values
8580Sstevel@tonic-gate 	 */
8590Sstevel@tonic-gate 	if (strcmp(subcmd, "update_entry") == 0)
8600Sstevel@tonic-gate 		ret = f(menu, bam_root, opt);
8610Sstevel@tonic-gate 	else
8620Sstevel@tonic-gate 		ret = f(menu, menu_path, opt);
8630Sstevel@tonic-gate 	if (ret == BAM_WRITE) {
864316Svikram 		ret = menu_write(menu_root, menu);
8650Sstevel@tonic-gate 	}
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	menu_free(menu);
8680Sstevel@tonic-gate 
869316Svikram 	umount_grub_slice(mnted, mntpt, NULL);
870316Svikram 
8710Sstevel@tonic-gate 	return (ret);
8720Sstevel@tonic-gate }
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate static error_t
8760Sstevel@tonic-gate bam_archive(
8770Sstevel@tonic-gate 	char *subcmd,
8780Sstevel@tonic-gate 	char *opt)
8790Sstevel@tonic-gate {
8800Sstevel@tonic-gate 	error_t ret;
8810Sstevel@tonic-gate 	error_t (*f)(char *root, char *opt);
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	/*
8840Sstevel@tonic-gate 	 * Check arguments
8850Sstevel@tonic-gate 	 */
8860Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f);
8870Sstevel@tonic-gate 	if (ret != BAM_SUCCESS) {
8880Sstevel@tonic-gate 		return (BAM_ERROR);
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate #if defined(__sparc)
8920Sstevel@tonic-gate 	/*
8930Sstevel@tonic-gate 	 * A NOP if called on SPARC during reboot
8940Sstevel@tonic-gate 	 */
8950Sstevel@tonic-gate 	if (strcmp(subcmd, "update_all") == 0)
8960Sstevel@tonic-gate 		return (BAM_SUCCESS);
8970Sstevel@tonic-gate 	else if (strcmp(subcmd, "update") != 0)
8980Sstevel@tonic-gate 		sparc_abort();
8990Sstevel@tonic-gate #endif
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	/*
9020Sstevel@tonic-gate 	 * Check archive not supported with update_all
9030Sstevel@tonic-gate 	 * since it is awkward to display out-of-sync
9040Sstevel@tonic-gate 	 * information for each BE.
9050Sstevel@tonic-gate 	 */
9060Sstevel@tonic-gate 	if (bam_check && strcmp(subcmd, "update_all") == 0) {
9070Sstevel@tonic-gate 		bam_error(CHECK_NOT_SUPPORTED, subcmd);
9080Sstevel@tonic-gate 		return (BAM_ERROR);
9090Sstevel@tonic-gate 	}
9100Sstevel@tonic-gate 
911316Svikram 	if (strcmp(subcmd, "update_all") == 0)
912316Svikram 		bam_update_all = 1;
913316Svikram 
914316Svikram 	ret = f(bam_root, opt);
915316Svikram 
916316Svikram 	bam_update_all = 0;
917316Svikram 
918316Svikram 	return (ret);
9190Sstevel@tonic-gate }
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate /*PRINTFLIKE1*/
9220Sstevel@tonic-gate static void
9230Sstevel@tonic-gate bam_error(char *format, ...)
9240Sstevel@tonic-gate {
9250Sstevel@tonic-gate 	va_list ap;
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	va_start(ap, format);
9280Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", prog);
9290Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
9300Sstevel@tonic-gate 	va_end(ap);
9310Sstevel@tonic-gate }
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate /*PRINTFLIKE1*/
9340Sstevel@tonic-gate static void
9350Sstevel@tonic-gate bam_print(char *format, ...)
9360Sstevel@tonic-gate {
9370Sstevel@tonic-gate 	va_list ap;
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 	va_start(ap, format);
9400Sstevel@tonic-gate 	(void) vfprintf(stdout, format, ap);
9410Sstevel@tonic-gate 	va_end(ap);
9420Sstevel@tonic-gate }
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate static void
9450Sstevel@tonic-gate bam_exit(int excode)
9460Sstevel@tonic-gate {
9470Sstevel@tonic-gate 	bam_unlock();
9480Sstevel@tonic-gate 	exit(excode);
9490Sstevel@tonic-gate }
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate static void
9520Sstevel@tonic-gate bam_lock(void)
9530Sstevel@tonic-gate {
9540Sstevel@tonic-gate 	struct flock lock;
9550Sstevel@tonic-gate 	pid_t pid;
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS);
9580Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
9590Sstevel@tonic-gate 		/*
9600Sstevel@tonic-gate 		 * We may be invoked early in boot for archive verification.
9610Sstevel@tonic-gate 		 * In this case, root is readonly and /var/run may not exist.
9620Sstevel@tonic-gate 		 * Proceed without the lock
9630Sstevel@tonic-gate 		 */
9640Sstevel@tonic-gate 		if (errno == EROFS || errno == ENOENT) {
9650Sstevel@tonic-gate 			bam_root_readonly = 1;
9660Sstevel@tonic-gate 			return;
9670Sstevel@tonic-gate 		}
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 		bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno));
9700Sstevel@tonic-gate 		bam_exit(1);
9710Sstevel@tonic-gate 	}
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
9740Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
9750Sstevel@tonic-gate 	lock.l_start = 0;
9760Sstevel@tonic-gate 	lock.l_len = 0;
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) {
9790Sstevel@tonic-gate 		if (errno != EACCES && errno != EAGAIN) {
9800Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
9810Sstevel@tonic-gate 			(void) close(bam_lock_fd);
9820Sstevel@tonic-gate 			bam_lock_fd = -1;
9830Sstevel@tonic-gate 			bam_exit(1);
9840Sstevel@tonic-gate 		}
9850Sstevel@tonic-gate 		pid = 0;
9860Sstevel@tonic-gate 		(void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0);
9870Sstevel@tonic-gate 		bam_print(FILE_LOCKED, pid);
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 		lock.l_type = F_WRLCK;
9900Sstevel@tonic-gate 		lock.l_whence = SEEK_SET;
9910Sstevel@tonic-gate 		lock.l_start = 0;
9920Sstevel@tonic-gate 		lock.l_len = 0;
9930Sstevel@tonic-gate 		if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) {
9940Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
9950Sstevel@tonic-gate 			(void) close(bam_lock_fd);
9960Sstevel@tonic-gate 			bam_lock_fd = -1;
9970Sstevel@tonic-gate 			bam_exit(1);
9980Sstevel@tonic-gate 		}
9990Sstevel@tonic-gate 	}
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	/* We own the lock now */
10020Sstevel@tonic-gate 	pid = getpid();
10030Sstevel@tonic-gate 	(void) write(bam_lock_fd, &pid, sizeof (pid));
10040Sstevel@tonic-gate }
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate static void
10070Sstevel@tonic-gate bam_unlock(void)
10080Sstevel@tonic-gate {
10090Sstevel@tonic-gate 	struct flock unlock;
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	/*
10120Sstevel@tonic-gate 	 * NOP if we don't hold the lock
10130Sstevel@tonic-gate 	 */
10140Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
10150Sstevel@tonic-gate 		return;
10160Sstevel@tonic-gate 	}
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
10190Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
10200Sstevel@tonic-gate 	unlock.l_start = 0;
10210Sstevel@tonic-gate 	unlock.l_len = 0;
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) {
10240Sstevel@tonic-gate 		bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
10250Sstevel@tonic-gate 	}
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	if (close(bam_lock_fd) == -1) {
10280Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno));
10290Sstevel@tonic-gate 	}
10300Sstevel@tonic-gate 	bam_lock_fd = -1;
10310Sstevel@tonic-gate }
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate static error_t
10340Sstevel@tonic-gate list_archive(char *root, char *opt)
10350Sstevel@tonic-gate {
10360Sstevel@tonic-gate 	filelist_t flist;
10370Sstevel@tonic-gate 	filelist_t *flistp = &flist;
10380Sstevel@tonic-gate 	line_t *lp;
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	assert(root);
10410Sstevel@tonic-gate 	assert(opt == NULL);
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
10440Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
10450Sstevel@tonic-gate 		return (BAM_ERROR);
10460Sstevel@tonic-gate 	}
10470Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
10500Sstevel@tonic-gate 		bam_print(PRINT, lp->line);
10510Sstevel@tonic-gate 	}
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	filelist_free(flistp);
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	return (BAM_SUCCESS);
10560Sstevel@tonic-gate }
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate /*
10590Sstevel@tonic-gate  * This routine writes a list of lines to a file.
10600Sstevel@tonic-gate  * The list is *not* freed
10610Sstevel@tonic-gate  */
10620Sstevel@tonic-gate static error_t
10630Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start)
10640Sstevel@tonic-gate {
10650Sstevel@tonic-gate 	char tmpfile[PATH_MAX];
10660Sstevel@tonic-gate 	char path[PATH_MAX];
10670Sstevel@tonic-gate 	FILE *fp;
10680Sstevel@tonic-gate 	int ret;
10690Sstevel@tonic-gate 	struct stat sb;
10700Sstevel@tonic-gate 	mode_t mode;
10710Sstevel@tonic-gate 	uid_t root_uid;
10720Sstevel@tonic-gate 	gid_t sys_gid;
10730Sstevel@tonic-gate 	struct passwd *pw;
10740Sstevel@tonic-gate 	struct group *gp;
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, final);
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	if (start == NULL) {
10800Sstevel@tonic-gate 		if (stat(path, &sb) != -1) {
10810Sstevel@tonic-gate 			bam_print(UNLINK_EMPTY, path);
10820Sstevel@tonic-gate 			if (unlink(path) != 0) {
10830Sstevel@tonic-gate 				bam_error(UNLINK_FAIL, path, strerror(errno));
10840Sstevel@tonic-gate 				return (BAM_ERROR);
10850Sstevel@tonic-gate 			} else {
10860Sstevel@tonic-gate 				return (BAM_SUCCESS);
10870Sstevel@tonic-gate 			}
10880Sstevel@tonic-gate 		}
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	/*
10920Sstevel@tonic-gate 	 * Preserve attributes of existing file if possible,
10930Sstevel@tonic-gate 	 * otherwise ask the system for uid/gid of root/sys.
10940Sstevel@tonic-gate 	 * If all fails, fall back on hard-coded defaults.
10950Sstevel@tonic-gate 	 */
10960Sstevel@tonic-gate 	if (stat(path, &sb) != -1) {
10970Sstevel@tonic-gate 		mode = sb.st_mode;
10980Sstevel@tonic-gate 		root_uid = sb.st_uid;
10990Sstevel@tonic-gate 		sys_gid = sb.st_gid;
11000Sstevel@tonic-gate 	} else {
11010Sstevel@tonic-gate 		mode = DEFAULT_DEV_MODE;
11020Sstevel@tonic-gate 		if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
11030Sstevel@tonic-gate 			root_uid = pw->pw_uid;
11040Sstevel@tonic-gate 		} else {
11050Sstevel@tonic-gate 			if (bam_verbose)
11060Sstevel@tonic-gate 				bam_error(CANT_FIND_USER,
11070Sstevel@tonic-gate 				    DEFAULT_DEV_USER, DEFAULT_DEV_UID);
11080Sstevel@tonic-gate 			root_uid = (uid_t)DEFAULT_DEV_UID;
11090Sstevel@tonic-gate 		}
11100Sstevel@tonic-gate 		if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
11110Sstevel@tonic-gate 			sys_gid = gp->gr_gid;
11120Sstevel@tonic-gate 		} else {
11130Sstevel@tonic-gate 			if (bam_verbose)
11140Sstevel@tonic-gate 				bam_error(CANT_FIND_GROUP,
11150Sstevel@tonic-gate 				    DEFAULT_DEV_GROUP, DEFAULT_DEV_GID);
11160Sstevel@tonic-gate 			sys_gid = (gid_t)DEFAULT_DEV_GID;
11170Sstevel@tonic-gate 		}
11180Sstevel@tonic-gate 	}
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	(void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp);
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	/* Truncate tmpfile first */
11230Sstevel@tonic-gate 	fp = fopen(tmpfile, "w");
11240Sstevel@tonic-gate 	if (fp == NULL) {
11250Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
11260Sstevel@tonic-gate 		return (BAM_ERROR);
11270Sstevel@tonic-gate 	}
11280Sstevel@tonic-gate 	ret = fclose(fp);
11290Sstevel@tonic-gate 	if (ret == EOF) {
11300Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
11310Sstevel@tonic-gate 		return (BAM_ERROR);
11320Sstevel@tonic-gate 	}
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	/* Now open it in append mode */
11350Sstevel@tonic-gate 	fp = fopen(tmpfile, "a");
11360Sstevel@tonic-gate 	if (fp == NULL) {
11370Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
11380Sstevel@tonic-gate 		return (BAM_ERROR);
11390Sstevel@tonic-gate 	}
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	for (; start; start = start->next) {
11420Sstevel@tonic-gate 		ret = s_fputs(start->line, fp);
11430Sstevel@tonic-gate 		if (ret == EOF) {
11440Sstevel@tonic-gate 			bam_error(WRITE_FAIL, tmpfile, strerror(errno));
11450Sstevel@tonic-gate 			(void) fclose(fp);
11460Sstevel@tonic-gate 			return (BAM_ERROR);
11470Sstevel@tonic-gate 		}
11480Sstevel@tonic-gate 	}
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	ret = fclose(fp);
11510Sstevel@tonic-gate 	if (ret == EOF) {
11520Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
11530Sstevel@tonic-gate 		return (BAM_ERROR);
11540Sstevel@tonic-gate 	}
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	/*
1157271Sjg 	 * Set up desired attributes.  Ignore failures on filesystems
1158271Sjg 	 * not supporting these operations - pcfs reports unsupported
1159271Sjg 	 * operations as EINVAL.
11600Sstevel@tonic-gate 	 */
11610Sstevel@tonic-gate 	ret = chmod(tmpfile, mode);
1162271Sjg 	if (ret == -1 &&
1163271Sjg 	    errno != EINVAL && errno != ENOTSUP) {
11640Sstevel@tonic-gate 		bam_error(CHMOD_FAIL, tmpfile, strerror(errno));
11650Sstevel@tonic-gate 		return (BAM_ERROR);
11660Sstevel@tonic-gate 	}
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	ret = chown(tmpfile, root_uid, sys_gid);
1169271Sjg 	if (ret == -1 &&
1170271Sjg 	    errno != EINVAL && errno != ENOTSUP) {
11710Sstevel@tonic-gate 		bam_error(CHOWN_FAIL, tmpfile, strerror(errno));
11720Sstevel@tonic-gate 		return (BAM_ERROR);
11730Sstevel@tonic-gate 	}
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 	/*
11770Sstevel@tonic-gate 	 * Do an atomic rename
11780Sstevel@tonic-gate 	 */
11790Sstevel@tonic-gate 	ret = rename(tmpfile, path);
11800Sstevel@tonic-gate 	if (ret != 0) {
11810Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path, strerror(errno));
11820Sstevel@tonic-gate 		return (BAM_ERROR);
11830Sstevel@tonic-gate 	}
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	return (BAM_SUCCESS);
11860Sstevel@tonic-gate }
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate /*
11890Sstevel@tonic-gate  * This function should always return 0 - since we want
11900Sstevel@tonic-gate  * to create stat data for *all* files in the list.
11910Sstevel@tonic-gate  */
11920Sstevel@tonic-gate /*ARGSUSED*/
11930Sstevel@tonic-gate static int
11940Sstevel@tonic-gate cmpstat(
11950Sstevel@tonic-gate 	const char *file,
11960Sstevel@tonic-gate 	const struct stat *stat,
11970Sstevel@tonic-gate 	int flags,
11980Sstevel@tonic-gate 	struct FTW *ftw)
11990Sstevel@tonic-gate {
12000Sstevel@tonic-gate 	uint_t sz;
12010Sstevel@tonic-gate 	uint64_t *value;
12020Sstevel@tonic-gate 	uint64_t filestat[2];
12030Sstevel@tonic-gate 	int error;
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	/*
12060Sstevel@tonic-gate 	 * We only want regular files
12070Sstevel@tonic-gate 	 */
12080Sstevel@tonic-gate 	if (!S_ISREG(stat->st_mode))
12090Sstevel@tonic-gate 		return (0);
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	/*
12120Sstevel@tonic-gate 	 * new_nvlp may be NULL if there were errors earlier
12130Sstevel@tonic-gate 	 * but this is not fatal to update determination.
12140Sstevel@tonic-gate 	 */
12150Sstevel@tonic-gate 	if (walk_arg.new_nvlp) {
12160Sstevel@tonic-gate 		filestat[0] = stat->st_size;
12170Sstevel@tonic-gate 		filestat[1] = stat->st_mtime;
12180Sstevel@tonic-gate 		error = nvlist_add_uint64_array(walk_arg.new_nvlp,
12190Sstevel@tonic-gate 		    file + bam_rootlen, filestat, 2);
12200Sstevel@tonic-gate 		if (error)
12210Sstevel@tonic-gate 			bam_error(NVADD_FAIL, file, strerror(error));
12220Sstevel@tonic-gate 	}
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	/*
12250Sstevel@tonic-gate 	 * The remaining steps are only required if we haven't made a
12260Sstevel@tonic-gate 	 * decision about update or if we are checking (-n)
12270Sstevel@tonic-gate 	 */
12280Sstevel@tonic-gate 	if (walk_arg.need_update && !bam_check)
12290Sstevel@tonic-gate 		return (0);
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	/*
12320Sstevel@tonic-gate 	 * If we are invoked as part of system/filesyste/boot-archive
12330Sstevel@tonic-gate 	 * SMF service, ignore amd64 modules unless we are booted amd64.
12340Sstevel@tonic-gate 	 */
12350Sstevel@tonic-gate 	if (bam_smf_check && !is_amd64() && strstr(file, "/amd64/") == 0)
12360Sstevel@tonic-gate 		return (0);
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	/*
12390Sstevel@tonic-gate 	 * We need an update if file doesn't exist in old archive
12400Sstevel@tonic-gate 	 */
12410Sstevel@tonic-gate 	if (walk_arg.old_nvlp == NULL ||
12420Sstevel@tonic-gate 	    nvlist_lookup_uint64_array(walk_arg.old_nvlp,
12430Sstevel@tonic-gate 	    file + bam_rootlen, &value, &sz) != 0) {
12440Sstevel@tonic-gate 		if (bam_smf_check)	/* ignore new during smf check */
12450Sstevel@tonic-gate 			return (0);
12460Sstevel@tonic-gate 		walk_arg.need_update = 1;
12470Sstevel@tonic-gate 		if (bam_verbose)
12480Sstevel@tonic-gate 			bam_print(PARSEABLE_NEW_FILE, file);
12490Sstevel@tonic-gate 		return (0);
12500Sstevel@tonic-gate 	}
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	/*
12530Sstevel@tonic-gate 	 * File exists in old archive. Check if file has changed
12540Sstevel@tonic-gate 	 */
12550Sstevel@tonic-gate 	assert(sz == 2);
12560Sstevel@tonic-gate 	bcopy(value, filestat, sizeof (filestat));
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	if (filestat[0] != stat->st_size ||
12590Sstevel@tonic-gate 	    filestat[1] != stat->st_mtime) {
12600Sstevel@tonic-gate 		walk_arg.need_update = 1;
12610Sstevel@tonic-gate 		if (bam_verbose)
12620Sstevel@tonic-gate 			if (bam_smf_check)
12630Sstevel@tonic-gate 				bam_print("    %s\n", file);
12640Sstevel@tonic-gate 			else
12650Sstevel@tonic-gate 				bam_print(PARSEABLE_OUT_DATE, file);
12660Sstevel@tonic-gate 	}
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	return (0);
12690Sstevel@tonic-gate }
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate /*
12720Sstevel@tonic-gate  * Check flags and presence of required files.
12730Sstevel@tonic-gate  * The force flag and/or absence of files should
12740Sstevel@tonic-gate  * trigger an update.
12750Sstevel@tonic-gate  * Suppress stdout output if check (-n) option is set
12760Sstevel@tonic-gate  * (as -n should only produce parseable output.)
12770Sstevel@tonic-gate  */
12780Sstevel@tonic-gate static void
12790Sstevel@tonic-gate check_flags_and_files(char *root)
12800Sstevel@tonic-gate {
12810Sstevel@tonic-gate 	char path[PATH_MAX];
12820Sstevel@tonic-gate 	struct stat sb;
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	/*
12850Sstevel@tonic-gate 	 * if force, create archive unconditionally
12860Sstevel@tonic-gate 	 */
12870Sstevel@tonic-gate 	if (bam_force) {
12880Sstevel@tonic-gate 		walk_arg.need_update = 1;
12890Sstevel@tonic-gate 		if (bam_verbose && !bam_check)
12900Sstevel@tonic-gate 			bam_print(UPDATE_FORCE);
12910Sstevel@tonic-gate 		return;
12920Sstevel@tonic-gate 	}
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 	/*
12950Sstevel@tonic-gate 	 * If archive is missing, create archive
12960Sstevel@tonic-gate 	 */
12970Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE);
12980Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
12990Sstevel@tonic-gate 		if (bam_verbose && !bam_check)
13000Sstevel@tonic-gate 			bam_print(UPDATE_ARCH_MISS, path);
13010Sstevel@tonic-gate 		walk_arg.need_update = 1;
13020Sstevel@tonic-gate 		return;
13030Sstevel@tonic-gate 	}
13040Sstevel@tonic-gate }
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate static error_t
13070Sstevel@tonic-gate read_one_list(char *root, filelist_t  *flistp, char *filelist)
13080Sstevel@tonic-gate {
13090Sstevel@tonic-gate 	char path[PATH_MAX];
13100Sstevel@tonic-gate 	FILE *fp;
13110Sstevel@tonic-gate 	char buf[BAM_MAXLINE];
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, filelist);
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	fp = fopen(path, "r");
13160Sstevel@tonic-gate 	if (fp == NULL) {
13170Sstevel@tonic-gate 		if (bam_debug)
13180Sstevel@tonic-gate 			bam_error(FLIST_FAIL, path, strerror(errno));
13190Sstevel@tonic-gate 		return (BAM_ERROR);
13200Sstevel@tonic-gate 	}
13210Sstevel@tonic-gate 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
1322316Svikram 		/* skip blank lines */
1323316Svikram 		if (strspn(buf, " \t") == strlen(buf))
1324316Svikram 			continue;
13250Sstevel@tonic-gate 		append_to_flist(flistp, buf);
13260Sstevel@tonic-gate 	}
13270Sstevel@tonic-gate 	if (fclose(fp) != 0) {
13280Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, path, strerror(errno));
13290Sstevel@tonic-gate 		return (BAM_ERROR);
13300Sstevel@tonic-gate 	}
13310Sstevel@tonic-gate 	return (BAM_SUCCESS);
13320Sstevel@tonic-gate }
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate static error_t
13350Sstevel@tonic-gate read_list(char *root, filelist_t  *flistp)
13360Sstevel@tonic-gate {
13370Sstevel@tonic-gate 	int rval;
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 	/*
13420Sstevel@tonic-gate 	 * Read current lists of files - only the first is mandatory
13430Sstevel@tonic-gate 	 */
13440Sstevel@tonic-gate 	rval = read_one_list(root, flistp, BOOT_FILE_LIST);
13450Sstevel@tonic-gate 	if (rval != BAM_SUCCESS)
13460Sstevel@tonic-gate 		return (rval);
13470Sstevel@tonic-gate 	(void) read_one_list(root, flistp, ETC_FILE_LIST);
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	if (flistp->head == NULL) {
13500Sstevel@tonic-gate 		bam_error(NO_FLIST);
13510Sstevel@tonic-gate 		return (BAM_ERROR);
13520Sstevel@tonic-gate 	}
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	return (BAM_SUCCESS);
13550Sstevel@tonic-gate }
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate static void
13580Sstevel@tonic-gate getoldstat(char *root)
13590Sstevel@tonic-gate {
13600Sstevel@tonic-gate 	char path[PATH_MAX];
13610Sstevel@tonic-gate 	int fd, error;
13620Sstevel@tonic-gate 	struct stat sb;
13630Sstevel@tonic-gate 	char *ostat;
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
13660Sstevel@tonic-gate 	fd = open(path, O_RDONLY);
13670Sstevel@tonic-gate 	if (fd == -1) {
13680Sstevel@tonic-gate 		if (bam_verbose)
13690Sstevel@tonic-gate 			bam_print(OPEN_FAIL, path, strerror(errno));
13700Sstevel@tonic-gate 		walk_arg.need_update = 1;
13710Sstevel@tonic-gate 		return;
13720Sstevel@tonic-gate 	}
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 	if (fstat(fd, &sb) != 0) {
13750Sstevel@tonic-gate 		bam_error(STAT_FAIL, path, strerror(errno));
13760Sstevel@tonic-gate 		(void) close(fd);
13770Sstevel@tonic-gate 		walk_arg.need_update = 1;
13780Sstevel@tonic-gate 		return;
13790Sstevel@tonic-gate 	}
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 	ostat = s_calloc(1, sb.st_size);
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 	if (read(fd, ostat, sb.st_size) != sb.st_size) {
13840Sstevel@tonic-gate 		bam_error(READ_FAIL, path, strerror(errno));
13850Sstevel@tonic-gate 		(void) close(fd);
13860Sstevel@tonic-gate 		free(ostat);
13870Sstevel@tonic-gate 		walk_arg.need_update = 1;
13880Sstevel@tonic-gate 		return;
13890Sstevel@tonic-gate 	}
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	(void) close(fd);
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
13940Sstevel@tonic-gate 	error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0);
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 	free(ostat);
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	if (error) {
13990Sstevel@tonic-gate 		bam_error(UNPACK_FAIL, path, strerror(error));
14000Sstevel@tonic-gate 		walk_arg.old_nvlp = NULL;
14010Sstevel@tonic-gate 		walk_arg.need_update = 1;
14020Sstevel@tonic-gate 		return;
14030Sstevel@tonic-gate 	}
14040Sstevel@tonic-gate }
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate static void
14070Sstevel@tonic-gate create_newstat(void)
14080Sstevel@tonic-gate {
14090Sstevel@tonic-gate 	int error;
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0);
14120Sstevel@tonic-gate 	if (error) {
14130Sstevel@tonic-gate 		/*
14140Sstevel@tonic-gate 		 * Not fatal - we can still create archive
14150Sstevel@tonic-gate 		 */
14160Sstevel@tonic-gate 		walk_arg.new_nvlp = NULL;
14170Sstevel@tonic-gate 		bam_error(NVALLOC_FAIL, strerror(error));
14180Sstevel@tonic-gate 	}
14190Sstevel@tonic-gate }
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate static void
14220Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp)
14230Sstevel@tonic-gate {
14240Sstevel@tonic-gate 	char path[PATH_MAX];
14250Sstevel@tonic-gate 	line_t *lp;
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
14280Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, lp->line);
14290Sstevel@tonic-gate 		/* XXX shouldn't we use FTW_MOUNT ? */
14300Sstevel@tonic-gate 		if (nftw(path, cmpstat, 20, 0) == -1) {
14310Sstevel@tonic-gate 			/*
14320Sstevel@tonic-gate 			 * Some files may not exist.
14330Sstevel@tonic-gate 			 * For example: etc/rtc_config on a x86 diskless system
14340Sstevel@tonic-gate 			 * Emit verbose message only
14350Sstevel@tonic-gate 			 */
14360Sstevel@tonic-gate 			if (bam_verbose)
14370Sstevel@tonic-gate 				bam_print(NFTW_FAIL, path, strerror(errno));
14380Sstevel@tonic-gate 		}
14390Sstevel@tonic-gate 	}
14400Sstevel@tonic-gate }
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate static void
14430Sstevel@tonic-gate savenew(char *root)
14440Sstevel@tonic-gate {
14450Sstevel@tonic-gate 	char path[PATH_MAX];
14460Sstevel@tonic-gate 	char path2[PATH_MAX];
14470Sstevel@tonic-gate 	size_t sz;
14480Sstevel@tonic-gate 	char *nstat;
14490Sstevel@tonic-gate 	int fd, wrote, error;
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	nstat = NULL;
14520Sstevel@tonic-gate 	sz = 0;
14530Sstevel@tonic-gate 	error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz,
14540Sstevel@tonic-gate 	    NV_ENCODE_XDR, 0);
14550Sstevel@tonic-gate 	if (error) {
14560Sstevel@tonic-gate 		bam_error(PACK_FAIL, strerror(error));
14570Sstevel@tonic-gate 		return;
14580Sstevel@tonic-gate 	}
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP);
14610Sstevel@tonic-gate 	fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE);
14620Sstevel@tonic-gate 	if (fd == -1) {
14630Sstevel@tonic-gate 		bam_error(OPEN_FAIL, path, strerror(errno));
14640Sstevel@tonic-gate 		free(nstat);
14650Sstevel@tonic-gate 		return;
14660Sstevel@tonic-gate 	}
14670Sstevel@tonic-gate 	wrote = write(fd, nstat, sz);
14680Sstevel@tonic-gate 	if (wrote != sz) {
14690Sstevel@tonic-gate 		bam_error(WRITE_FAIL, path, strerror(errno));
14700Sstevel@tonic-gate 		(void) close(fd);
14710Sstevel@tonic-gate 		free(nstat);
14720Sstevel@tonic-gate 		return;
14730Sstevel@tonic-gate 	}
14740Sstevel@tonic-gate 	(void) close(fd);
14750Sstevel@tonic-gate 	free(nstat);
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 	(void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT);
14780Sstevel@tonic-gate 	if (rename(path, path2) != 0) {
14790Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path2, strerror(errno));
14800Sstevel@tonic-gate 	}
14810Sstevel@tonic-gate }
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate static void
14840Sstevel@tonic-gate clear_walk_args(void)
14850Sstevel@tonic-gate {
14860Sstevel@tonic-gate 	if (walk_arg.old_nvlp)
14870Sstevel@tonic-gate 		nvlist_free(walk_arg.old_nvlp);
14880Sstevel@tonic-gate 	if (walk_arg.new_nvlp)
14890Sstevel@tonic-gate 		nvlist_free(walk_arg.new_nvlp);
14900Sstevel@tonic-gate 	walk_arg.need_update = 0;
14910Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
14920Sstevel@tonic-gate 	walk_arg.new_nvlp = NULL;
14930Sstevel@tonic-gate }
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate /*
14960Sstevel@tonic-gate  * Returns:
14970Sstevel@tonic-gate  *	0 - no update necessary
14980Sstevel@tonic-gate  *	1 - update required.
14990Sstevel@tonic-gate  *	BAM_ERROR (-1) - An error occurred
15000Sstevel@tonic-gate  *
15010Sstevel@tonic-gate  * Special handling for check (-n):
15020Sstevel@tonic-gate  * ================================
15030Sstevel@tonic-gate  * The check (-n) option produces parseable output.
15040Sstevel@tonic-gate  * To do this, we suppress all stdout messages unrelated
15050Sstevel@tonic-gate  * to out of sync files.
15060Sstevel@tonic-gate  * All stderr messages are still printed though.
15070Sstevel@tonic-gate  *
15080Sstevel@tonic-gate  */
15090Sstevel@tonic-gate static int
15100Sstevel@tonic-gate update_required(char *root)
15110Sstevel@tonic-gate {
15120Sstevel@tonic-gate 	struct stat sb;
15130Sstevel@tonic-gate 	char path[PATH_MAX];
15140Sstevel@tonic-gate 	filelist_t flist;
15150Sstevel@tonic-gate 	filelist_t *flistp = &flist;
15160Sstevel@tonic-gate 	int need_update;
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	walk_arg.need_update = 0;
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 	/*
15230Sstevel@tonic-gate 	 * Without consulting stat data, check if we need update
15240Sstevel@tonic-gate 	 */
15250Sstevel@tonic-gate 	check_flags_and_files(root);
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	/*
15280Sstevel@tonic-gate 	 * In certain deployment scenarios, filestat may not
15290Sstevel@tonic-gate 	 * exist. Ignore it during boot-archive SMF check.
15300Sstevel@tonic-gate 	 */
15310Sstevel@tonic-gate 	if (bam_smf_check) {
15320Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
15330Sstevel@tonic-gate 		if (stat(path, &sb) != 0)
15340Sstevel@tonic-gate 			return (0);
15350Sstevel@tonic-gate 	}
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	/*
15380Sstevel@tonic-gate 	 * consult stat data only if we haven't made a decision
15390Sstevel@tonic-gate 	 * about update. If checking (-n) however, we always
15400Sstevel@tonic-gate 	 * need stat data (since we want to compare old and new)
15410Sstevel@tonic-gate 	 */
15420Sstevel@tonic-gate 	if (!walk_arg.need_update || bam_check)
15430Sstevel@tonic-gate 		getoldstat(root);
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 	/*
15460Sstevel@tonic-gate 	 * read list of files
15470Sstevel@tonic-gate 	 */
15480Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
15490Sstevel@tonic-gate 		clear_walk_args();
15500Sstevel@tonic-gate 		return (BAM_ERROR);
15510Sstevel@tonic-gate 	}
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	/*
15560Sstevel@tonic-gate 	 * At this point either the update is required
15570Sstevel@tonic-gate 	 * or the decision is pending. In either case
15580Sstevel@tonic-gate 	 * we need to create new stat nvlist
15590Sstevel@tonic-gate 	 */
15600Sstevel@tonic-gate 	create_newstat();
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	/*
15630Sstevel@tonic-gate 	 * This walk does 2 things:
15640Sstevel@tonic-gate 	 *  	- gets new stat data for every file
15650Sstevel@tonic-gate 	 *	- (optional) compare old and new stat data
15660Sstevel@tonic-gate 	 */
15670Sstevel@tonic-gate 	walk_list(root, &flist);
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 	/* done with the file list */
15700Sstevel@tonic-gate 	filelist_free(flistp);
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	/*
15730Sstevel@tonic-gate 	 * if we didn't succeed in  creating new stat data above
15740Sstevel@tonic-gate 	 * just return result of update check so that archive is built.
15750Sstevel@tonic-gate 	 */
15760Sstevel@tonic-gate 	if (walk_arg.new_nvlp == NULL) {
15770Sstevel@tonic-gate 		bam_error(NO_NEW_STAT);
15780Sstevel@tonic-gate 		need_update = walk_arg.need_update;
15790Sstevel@tonic-gate 		clear_walk_args();
15800Sstevel@tonic-gate 		return (need_update ? 1 : 0);
15810Sstevel@tonic-gate 	}
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	/*
15850Sstevel@tonic-gate 	 * If no update required, discard newstat
15860Sstevel@tonic-gate 	 */
15870Sstevel@tonic-gate 	if (!walk_arg.need_update) {
15880Sstevel@tonic-gate 		clear_walk_args();
15890Sstevel@tonic-gate 		return (0);
15900Sstevel@tonic-gate 	}
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	/*
15930Sstevel@tonic-gate 	 * At this point we need an update - so save new stat data
15940Sstevel@tonic-gate 	 * However, if only checking (-n), don't save new stat data.
15950Sstevel@tonic-gate 	 */
15960Sstevel@tonic-gate 	if (!bam_check)
15970Sstevel@tonic-gate 		savenew(root);
15980Sstevel@tonic-gate 
15990Sstevel@tonic-gate 	clear_walk_args();
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	return (1);
16020Sstevel@tonic-gate }
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate static error_t
16050Sstevel@tonic-gate create_ramdisk(char *root)
16060Sstevel@tonic-gate {
16070Sstevel@tonic-gate 	char *cmdline, path[PATH_MAX];
16080Sstevel@tonic-gate 	size_t len;
16090Sstevel@tonic-gate 	struct stat sb;
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	/*
16120Sstevel@tonic-gate 	 * Setup command args for create_ramdisk.ksh
16130Sstevel@tonic-gate 	 */
16140Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, CREATE_RAMDISK);
16150Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
16160Sstevel@tonic-gate 		bam_error(ARCH_EXEC_MISS, path, strerror(errno));
16170Sstevel@tonic-gate 		return (BAM_ERROR);
16180Sstevel@tonic-gate 	}
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 	len = strlen(path) + strlen(root) + 10;	/* room for space + -R */
16210Sstevel@tonic-gate 	cmdline = s_calloc(1, len);
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 	if (strlen(root) > 1) {
16240Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s -R %s", path, root);
16250Sstevel@tonic-gate 		/* chop off / at the end */
16260Sstevel@tonic-gate 		cmdline[strlen(cmdline) - 1] = '\0';
16270Sstevel@tonic-gate 	} else
16280Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s", path);
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	if (exec_cmd(cmdline, NULL, 0) != 0) {
16310Sstevel@tonic-gate 		bam_error(ARCHIVE_FAIL, cmdline);
16320Sstevel@tonic-gate 		free(cmdline);
16330Sstevel@tonic-gate 		return (BAM_ERROR);
16340Sstevel@tonic-gate 	}
16350Sstevel@tonic-gate 	free(cmdline);
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 	/*
16380Sstevel@tonic-gate 	 * Verify that the archive has been created
16390Sstevel@tonic-gate 	 */
16400Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE);
16410Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
16420Sstevel@tonic-gate 		bam_error(ARCHIVE_NOT_CREATED, path);
16430Sstevel@tonic-gate 		return (BAM_ERROR);
16440Sstevel@tonic-gate 	}
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	return (BAM_SUCCESS);
16470Sstevel@tonic-gate }
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate /*
16500Sstevel@tonic-gate  * Checks if target filesystem is on a ramdisk
16510Sstevel@tonic-gate  * 1 - is miniroot
16520Sstevel@tonic-gate  * 0 - is not
16530Sstevel@tonic-gate  * When in doubt assume it is not a ramdisk.
16540Sstevel@tonic-gate  */
16550Sstevel@tonic-gate static int
16560Sstevel@tonic-gate is_ramdisk(char *root)
16570Sstevel@tonic-gate {
16580Sstevel@tonic-gate 	struct extmnttab mnt;
16590Sstevel@tonic-gate 	FILE *fp;
16600Sstevel@tonic-gate 	int found;
1661316Svikram 	char mntpt[PATH_MAX];
1662316Svikram 	char *cp;
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 	/*
16650Sstevel@tonic-gate 	 * There are 3 situations where creating archive is
16660Sstevel@tonic-gate 	 * of dubious value:
1667316Svikram 	 *	- create boot_archive on a lofi-mounted boot_archive
16680Sstevel@tonic-gate 	 *	- create it on a ramdisk which is the root filesystem
16690Sstevel@tonic-gate 	 *	- create it on a ramdisk mounted somewhere else
16700Sstevel@tonic-gate 	 * The first is not easy to detect and checking for it is not
16710Sstevel@tonic-gate 	 * worth it.
16720Sstevel@tonic-gate 	 * The other two conditions are handled here
16730Sstevel@tonic-gate 	 */
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
16760Sstevel@tonic-gate 	if (fp == NULL) {
16770Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
16780Sstevel@tonic-gate 		return (0);
16790Sstevel@tonic-gate 	}
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	resetmnttab(fp);
16820Sstevel@tonic-gate 
1683316Svikram 	/*
1684316Svikram 	 * Remove any trailing / from the mount point
1685316Svikram 	 */
1686316Svikram 	(void) strlcpy(mntpt, root, sizeof (mntpt));
1687316Svikram 	if (strcmp(root, "/") != 0) {
1688316Svikram 		cp = mntpt + strlen(mntpt) - 1;
1689316Svikram 		if (*cp == '/')
1690316Svikram 			*cp = '\0';
1691316Svikram 	}
16920Sstevel@tonic-gate 	found = 0;
16930Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
1694316Svikram 		if (strcmp(mnt.mnt_mountp, mntpt) == 0) {
16950Sstevel@tonic-gate 			found = 1;
16960Sstevel@tonic-gate 			break;
16970Sstevel@tonic-gate 		}
16980Sstevel@tonic-gate 	}
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	if (!found) {
17010Sstevel@tonic-gate 		if (bam_verbose)
1702316Svikram 			bam_error(NOT_IN_MNTTAB, mntpt);
17030Sstevel@tonic-gate 		(void) fclose(fp);
17040Sstevel@tonic-gate 		return (0);
17050Sstevel@tonic-gate 	}
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 	if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) {
17080Sstevel@tonic-gate 		if (bam_verbose)
17090Sstevel@tonic-gate 			bam_error(IS_RAMDISK, bam_root);
17100Sstevel@tonic-gate 		(void) fclose(fp);
17110Sstevel@tonic-gate 		return (1);
17120Sstevel@tonic-gate 	}
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	(void) fclose(fp);
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate 	return (0);
17170Sstevel@tonic-gate }
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate static int
17200Sstevel@tonic-gate is_newboot(char *root)
17210Sstevel@tonic-gate {
17220Sstevel@tonic-gate 	char path[PATH_MAX];
17230Sstevel@tonic-gate 	struct stat sb;
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 	/*
17260Sstevel@tonic-gate 	 * We can't boot without MULTI_BOOT
17270Sstevel@tonic-gate 	 */
17280Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, MULTI_BOOT);
17290Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
17300Sstevel@tonic-gate 		if (bam_verbose)
17310Sstevel@tonic-gate 			bam_print(FILE_MISS, path);
17320Sstevel@tonic-gate 		return (0);
17330Sstevel@tonic-gate 	}
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 	/*
17360Sstevel@tonic-gate 	 * We can't generate archive without GRUB_DIR
17370Sstevel@tonic-gate 	 */
17380Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, GRUB_DIR);
17390Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
17400Sstevel@tonic-gate 		if (bam_verbose)
17410Sstevel@tonic-gate 			bam_print(DIR_MISS, path);
17420Sstevel@tonic-gate 		return (0);
17430Sstevel@tonic-gate 	}
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 	return (1);
17460Sstevel@tonic-gate }
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate static int
17490Sstevel@tonic-gate is_readonly(char *root)
17500Sstevel@tonic-gate {
17510Sstevel@tonic-gate 	struct statvfs vfs;
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 	/*
17540Sstevel@tonic-gate 	 * Check for RDONLY filesystem
17550Sstevel@tonic-gate 	 * When in doubt assume it is not readonly
17560Sstevel@tonic-gate 	 */
17570Sstevel@tonic-gate 	if (statvfs(root, &vfs) != 0) {
17580Sstevel@tonic-gate 		if (bam_verbose)
17590Sstevel@tonic-gate 			bam_error(STATVFS_FAIL, root, strerror(errno));
17600Sstevel@tonic-gate 		return (0);
17610Sstevel@tonic-gate 	}
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 	if (vfs.f_flag & ST_RDONLY) {
17640Sstevel@tonic-gate 		return (1);
17650Sstevel@tonic-gate 	}
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 	return (0);
17680Sstevel@tonic-gate }
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate static error_t
17710Sstevel@tonic-gate update_archive(char *root, char *opt)
17720Sstevel@tonic-gate {
17730Sstevel@tonic-gate 	error_t ret;
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	assert(root);
17760Sstevel@tonic-gate 	assert(opt == NULL);
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate 	/*
1779316Svikram 	 * root must belong to a GRUB boot OS,
17800Sstevel@tonic-gate 	 * don't care on sparc except for diskless clients
17810Sstevel@tonic-gate 	 */
17820Sstevel@tonic-gate 	if (!is_newboot(root)) {
1783316Svikram 		/*
1784316Svikram 		 * Emit message only if not in context of update_all.
1785316Svikram 		 * If in update_all, emit only if verbose flag is set.
1786316Svikram 		 */
1787316Svikram 		if (!bam_update_all || bam_verbose)
1788316Svikram 			bam_print(NOT_GRUB_BOOT, root);
17890Sstevel@tonic-gate 		return (BAM_SUCCESS);
17900Sstevel@tonic-gate 	}
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 	/*
17930Sstevel@tonic-gate 	 * root must be writable
17940Sstevel@tonic-gate 	 * Note: statvfs() does not always report the truth
17950Sstevel@tonic-gate 	 */
17960Sstevel@tonic-gate 	if (is_readonly(root)) {
17970Sstevel@tonic-gate 		if (!bam_smf_check && bam_verbose)
17980Sstevel@tonic-gate 			bam_print(RDONLY_FS, root);
17990Sstevel@tonic-gate 		return (BAM_SUCCESS);
18000Sstevel@tonic-gate 	}
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 	/*
18030Sstevel@tonic-gate 	 * Don't generate archive on ramdisk
18040Sstevel@tonic-gate 	 */
18050Sstevel@tonic-gate 	if (is_ramdisk(root)) {
18060Sstevel@tonic-gate 		if (bam_verbose)
18070Sstevel@tonic-gate 			bam_print(SKIP_RAMDISK);
18080Sstevel@tonic-gate 		return (BAM_SUCCESS);
18090Sstevel@tonic-gate 	}
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate 	/*
18120Sstevel@tonic-gate 	 * Now check if updated is really needed
18130Sstevel@tonic-gate 	 */
18140Sstevel@tonic-gate 	ret = update_required(root);
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 	/*
18170Sstevel@tonic-gate 	 * The check command (-n) is *not* a dry run
18180Sstevel@tonic-gate 	 * It only checks if the archive is in sync.
18190Sstevel@tonic-gate 	 */
18200Sstevel@tonic-gate 	if (bam_check) {
18210Sstevel@tonic-gate 		bam_exit((ret != 0) ? 1 : 0);
18220Sstevel@tonic-gate 	}
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate 	if (ret == 1) {
18250Sstevel@tonic-gate 		/* create the ramdisk */
18260Sstevel@tonic-gate 		ret = create_ramdisk(root);
18270Sstevel@tonic-gate 	}
18280Sstevel@tonic-gate 	return (ret);
18290Sstevel@tonic-gate }
18300Sstevel@tonic-gate 
1831316Svikram static void
1832316Svikram restore_grub_slice(void)
1833316Svikram {
1834316Svikram 	struct stat sb;
1835316Svikram 	char *mntpt, *physlice;
1836316Svikram 	int mnted;	/* set if we did a mount */
1837316Svikram 	char menupath[PATH_MAX], cmd[PATH_MAX];
1838316Svikram 
1839316Svikram 	if (stat(GRUB_slice, &sb) != 0) {
1840316Svikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
1841316Svikram 		return;
1842316Svikram 	}
1843316Svikram 
1844316Svikram 	/*
1845316Svikram 	 * If we are doing an luactivate, don't attempt to restore GRUB or else
1846316Svikram 	 * we may not be able to get to DCA boot environments. Let luactivate
1847316Svikram 	 * handle GRUB/DCA installation
1848316Svikram 	 */
1849316Svikram 	if (stat(LU_ACTIVATE_FILE, &sb) == 0) {
1850316Svikram 		return;
1851316Svikram 	}
1852316Svikram 
1853316Svikram 	mnted = 0;
1854316Svikram 	physlice = NULL;
1855316Svikram 	mntpt = mount_grub_slice(&mnted, &physlice, QUIET);
1856316Svikram 	if (mntpt == NULL) {
1857316Svikram 		bam_error(CANNOT_RESTORE_GRUB_SLICE);
1858316Svikram 		return;
1859316Svikram 	}
1860316Svikram 
1861316Svikram 	(void) snprintf(menupath, sizeof (menupath), "%s%s", mntpt, GRUB_MENU);
1862316Svikram 	if (stat(menupath, &sb) == 0) {
1863316Svikram 		umount_grub_slice(mnted, mntpt, physlice);
1864316Svikram 		return;
1865316Svikram 	}
1866316Svikram 
1867316Svikram 	/*
1868316Svikram 	 * The menu is missing - we need to do a restore
1869316Svikram 	 */
1870316Svikram 	bam_print(RESTORING_GRUB);
1871316Svikram 
1872316Svikram 	(void) snprintf(cmd, sizeof (cmd), "%s %s %s %s",
1873316Svikram 	    INSTALLGRUB, STAGE1, STAGE2, physlice);
1874316Svikram 
1875316Svikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
1876316Svikram 		bam_error(RESTORE_GRUB_FAILED);
1877316Svikram 		umount_grub_slice(mnted, mntpt, physlice);
1878316Svikram 		return;
1879316Svikram 	}
1880316Svikram 
1881316Svikram 	if (stat(GRUB_backup_menu, &sb) != 0) {
1882316Svikram 		bam_error(MISSING_BACKUP_MENU,
1883316Svikram 		    GRUB_backup_menu, strerror(errno));
1884316Svikram 		umount_grub_slice(mnted, mntpt, physlice);
1885316Svikram 		return;
1886316Svikram 	}
1887316Svikram 
1888316Svikram 	(void) snprintf(cmd, sizeof (cmd), "/bin/cp %s %s",
1889316Svikram 	    GRUB_backup_menu, menupath);
1890316Svikram 
1891316Svikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
1892316Svikram 		bam_error(RESTORE_MENU_FAILED, menupath);
1893316Svikram 		umount_grub_slice(mnted, mntpt, physlice);
1894316Svikram 		return;
1895316Svikram 	}
1896316Svikram 
1897316Svikram 	/* Success */
1898316Svikram 	umount_grub_slice(mnted, mntpt, physlice);
1899316Svikram }
1900316Svikram 
19010Sstevel@tonic-gate static error_t
19020Sstevel@tonic-gate update_all(char *root, char *opt)
19030Sstevel@tonic-gate {
19040Sstevel@tonic-gate 	struct extmnttab mnt;
19050Sstevel@tonic-gate 	struct stat sb;
19060Sstevel@tonic-gate 	FILE *fp;
19070Sstevel@tonic-gate 	char multibt[PATH_MAX];
19080Sstevel@tonic-gate 	error_t ret = BAM_SUCCESS;
19090Sstevel@tonic-gate 
19100Sstevel@tonic-gate 	assert(bam_rootlen == 1 && root[0] == '/');
19110Sstevel@tonic-gate 	assert(opt == NULL);
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 	/*
19140Sstevel@tonic-gate 	 * First update archive for current root
19150Sstevel@tonic-gate 	 */
19160Sstevel@tonic-gate 	if (update_archive(root, opt) != BAM_SUCCESS)
19170Sstevel@tonic-gate 		ret = BAM_ERROR;
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate 	/*
19200Sstevel@tonic-gate 	 * Now walk the mount table, performing archive update
19210Sstevel@tonic-gate 	 * for all mounted Newboot root filesystems
19220Sstevel@tonic-gate 	 */
19230Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
19240Sstevel@tonic-gate 	if (fp == NULL) {
19250Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
1926316Svikram 		ret = BAM_ERROR;
1927316Svikram 		goto out;
19280Sstevel@tonic-gate 	}
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 	resetmnttab(fp);
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
19330Sstevel@tonic-gate 		if (mnt.mnt_special == NULL)
19340Sstevel@tonic-gate 			continue;
19350Sstevel@tonic-gate 		if (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0)
19360Sstevel@tonic-gate 			continue;
19370Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, "/") == 0)
19380Sstevel@tonic-gate 			continue;
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate 		(void) snprintf(multibt, sizeof (multibt), "%s%s",
19410Sstevel@tonic-gate 		    mnt.mnt_mountp, MULTI_BOOT);
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 		if (stat(multibt, &sb) == -1)
19440Sstevel@tonic-gate 			continue;
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 		/*
19470Sstevel@tonic-gate 		 * We put a trailing slash to be consistent with root = "/"
19480Sstevel@tonic-gate 		 * case, such that we don't have to print // in some cases.
19490Sstevel@tonic-gate 		 */
19500Sstevel@tonic-gate 		(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
19510Sstevel@tonic-gate 		    mnt.mnt_mountp);
19520Sstevel@tonic-gate 		bam_rootlen = strlen(rootbuf);
19530Sstevel@tonic-gate 		if (update_archive(rootbuf, opt) != BAM_SUCCESS)
19540Sstevel@tonic-gate 			ret = BAM_ERROR;
19550Sstevel@tonic-gate 	}
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate 	(void) fclose(fp);
19580Sstevel@tonic-gate 
1959316Svikram out:
1960316Svikram 	if (stat(GRUB_slice, &sb) == 0) {
1961316Svikram 		restore_grub_slice();
1962316Svikram 	}
1963316Svikram 
19640Sstevel@tonic-gate 	return (ret);
19650Sstevel@tonic-gate }
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate static void
19680Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp)
19690Sstevel@tonic-gate {
19700Sstevel@tonic-gate 	if (mp->start == NULL) {
19710Sstevel@tonic-gate 		mp->start = lp;
19720Sstevel@tonic-gate 	} else {
19730Sstevel@tonic-gate 		mp->end->next = lp;
19740Sstevel@tonic-gate 	}
19750Sstevel@tonic-gate 	mp->end = lp;
19760Sstevel@tonic-gate }
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate /*
19790Sstevel@tonic-gate  * A line in menu.lst looks like
19800Sstevel@tonic-gate  * [ ]*<cmd>[ \t=]*<arg>*
19810Sstevel@tonic-gate  */
19820Sstevel@tonic-gate static void
19830Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum)
19840Sstevel@tonic-gate {
19850Sstevel@tonic-gate 	/*
19860Sstevel@tonic-gate 	 * save state across calls. This is so that
19870Sstevel@tonic-gate 	 * header gets the right entry# after title has
19880Sstevel@tonic-gate 	 * been processed
19890Sstevel@tonic-gate 	 */
19900Sstevel@tonic-gate 	static line_t *prev;
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 	line_t	*lp;
19930Sstevel@tonic-gate 	char *cmd, *sep, *arg;
19940Sstevel@tonic-gate 	char save, *cp, *line;
19950Sstevel@tonic-gate 	menu_flag_t flag = BAM_INVALID;
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 	if (str == NULL) {
19980Sstevel@tonic-gate 		return;
19990Sstevel@tonic-gate 	}
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate 	/*
20020Sstevel@tonic-gate 	 * First save a copy of the entire line.
20030Sstevel@tonic-gate 	 * We use this later to set the line field.
20040Sstevel@tonic-gate 	 */
20050Sstevel@tonic-gate 	line = s_strdup(str);
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate 	/* Eat up leading whitespace */
20080Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
20090Sstevel@tonic-gate 		str++;
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 	if (*str == '#') {		/* comment */
20120Sstevel@tonic-gate 		cmd = s_strdup("#");
20130Sstevel@tonic-gate 		sep = NULL;
20140Sstevel@tonic-gate 		arg = s_strdup(str + 1);
20150Sstevel@tonic-gate 		flag = BAM_COMMENT;
20160Sstevel@tonic-gate 	} else if (*str == '\0') {	/* blank line */
20170Sstevel@tonic-gate 		cmd = sep = arg = NULL;
20180Sstevel@tonic-gate 		flag = BAM_EMPTY;
20190Sstevel@tonic-gate 	} else {
20200Sstevel@tonic-gate 		/*
20210Sstevel@tonic-gate 		 * '=' is not a documented separator in grub syntax.
20220Sstevel@tonic-gate 		 * However various development bits use '=' as a
20230Sstevel@tonic-gate 		 * separator. In addition, external users also
20240Sstevel@tonic-gate 		 * use = as a separator. So we will allow that usage.
20250Sstevel@tonic-gate 		 */
20260Sstevel@tonic-gate 		cp = str;
20270Sstevel@tonic-gate 		while (*str != ' ' && *str != '\t' && *str != '=') {
20280Sstevel@tonic-gate 			if (*str == '\0') {
20290Sstevel@tonic-gate 				cmd = s_strdup(cp);
20300Sstevel@tonic-gate 				sep = arg = NULL;
20310Sstevel@tonic-gate 				break;
20320Sstevel@tonic-gate 			}
20330Sstevel@tonic-gate 			str++;
20340Sstevel@tonic-gate 		}
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 		if (*str != '\0') {
20370Sstevel@tonic-gate 			save = *str;
20380Sstevel@tonic-gate 			*str = '\0';
20390Sstevel@tonic-gate 			cmd = s_strdup(cp);
20400Sstevel@tonic-gate 			*str = save;
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 			str++;
20430Sstevel@tonic-gate 			save = *str;
20440Sstevel@tonic-gate 			*str = '\0';
20450Sstevel@tonic-gate 			sep = s_strdup(str - 1);
20460Sstevel@tonic-gate 			*str = save;
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate 			while (*str == ' ' || *str == '\t')
20490Sstevel@tonic-gate 				str++;
20500Sstevel@tonic-gate 			if (*str == '\0')
20510Sstevel@tonic-gate 				arg = NULL;
20520Sstevel@tonic-gate 			else
20530Sstevel@tonic-gate 				arg = s_strdup(str);
20540Sstevel@tonic-gate 		}
20550Sstevel@tonic-gate 	}
20560Sstevel@tonic-gate 
20570Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
20580Sstevel@tonic-gate 
20590Sstevel@tonic-gate 	lp->cmd = cmd;
20600Sstevel@tonic-gate 	lp->sep = sep;
20610Sstevel@tonic-gate 	lp->arg = arg;
20620Sstevel@tonic-gate 	lp->line = line;
20630Sstevel@tonic-gate 	lp->lineNum = ++(*lineNum);
20640Sstevel@tonic-gate 	if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) {
20650Sstevel@tonic-gate 		lp->entryNum = ++(*entryNum);
20660Sstevel@tonic-gate 		lp->flags = BAM_TITLE;
20670Sstevel@tonic-gate 		if (prev && prev->flags == BAM_COMMENT &&
20680Sstevel@tonic-gate 		    prev->arg && strcmp(prev->arg, BAM_HDR) == 0)
20690Sstevel@tonic-gate 			prev->entryNum = lp->entryNum;
20700Sstevel@tonic-gate 	} else if (flag != BAM_INVALID) {
20710Sstevel@tonic-gate 		/*
20720Sstevel@tonic-gate 		 * For header comments, the entry# is "fixed up"
20730Sstevel@tonic-gate 		 * by the subsequent title
20740Sstevel@tonic-gate 		 */
20750Sstevel@tonic-gate 		lp->entryNum = *entryNum;
20760Sstevel@tonic-gate 		lp->flags = flag;
20770Sstevel@tonic-gate 	} else {
20780Sstevel@tonic-gate 		lp->entryNum = *entryNum;
20790Sstevel@tonic-gate 		lp->flags = (*entryNum == ENTRY_INIT) ? BAM_GLOBAL : BAM_ENTRY;
20800Sstevel@tonic-gate 	}
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 	append_line(mp, lp);
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 	prev = lp;
20850Sstevel@tonic-gate }
20860Sstevel@tonic-gate 
20870Sstevel@tonic-gate static menu_t *
20880Sstevel@tonic-gate menu_read(char *menu_path)
20890Sstevel@tonic-gate {
20900Sstevel@tonic-gate 	FILE *fp;
20910Sstevel@tonic-gate 	char buf[BAM_MAXLINE], *cp;
20920Sstevel@tonic-gate 	menu_t *mp;
20930Sstevel@tonic-gate 	int line, entry, len, n;
20940Sstevel@tonic-gate 
20950Sstevel@tonic-gate 	mp = s_calloc(1, sizeof (menu_t));
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate 	fp = fopen(menu_path, "r");
20980Sstevel@tonic-gate 	if (fp == NULL) { /* Let the caller handle this error */
20990Sstevel@tonic-gate 		return (mp);
21000Sstevel@tonic-gate 	}
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 
21030Sstevel@tonic-gate 	/* Note: GRUB boot entry number starts with 0 */
21040Sstevel@tonic-gate 	line = LINE_INIT;
21050Sstevel@tonic-gate 	entry = ENTRY_INIT;
21060Sstevel@tonic-gate 	cp = buf;
21070Sstevel@tonic-gate 	len = sizeof (buf);
21080Sstevel@tonic-gate 	while (s_fgets(cp, len, fp) != NULL) {
21090Sstevel@tonic-gate 		n = strlen(cp);
21100Sstevel@tonic-gate 		if (cp[n - 1] == '\\') {
21110Sstevel@tonic-gate 			len -= n - 1;
21120Sstevel@tonic-gate 			assert(len >= 2);
21130Sstevel@tonic-gate 			cp += n - 1;
21140Sstevel@tonic-gate 			continue;
21150Sstevel@tonic-gate 		}
21160Sstevel@tonic-gate 		line_parser(mp, buf, &line, &entry);
21170Sstevel@tonic-gate 		cp = buf;
21180Sstevel@tonic-gate 		len = sizeof (buf);
21190Sstevel@tonic-gate 	}
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
21220Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, menu_path, strerror(errno));
21230Sstevel@tonic-gate 	}
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 	return (mp);
21260Sstevel@tonic-gate }
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate static error_t
21290Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title)
21300Sstevel@tonic-gate {
21310Sstevel@tonic-gate 	char *eq;
21320Sstevel@tonic-gate 	char *opt_dup;
21330Sstevel@tonic-gate 	int entryNum;
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate 	assert(mp);
21360Sstevel@tonic-gate 	assert(mp->start);
21370Sstevel@tonic-gate 	assert(opt);
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate 	opt_dup = s_strdup(opt);
21400Sstevel@tonic-gate 
21410Sstevel@tonic-gate 	if (entry)
21420Sstevel@tonic-gate 		*entry = ENTRY_INIT;
21430Sstevel@tonic-gate 	if (title)
21440Sstevel@tonic-gate 		*title = NULL;
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 	eq = strchr(opt_dup, '=');
21470Sstevel@tonic-gate 	if (eq == NULL) {
21480Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
21490Sstevel@tonic-gate 		free(opt_dup);
21500Sstevel@tonic-gate 		return (BAM_ERROR);
21510Sstevel@tonic-gate 	}
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate 	*eq = '\0';
21540Sstevel@tonic-gate 	if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) {
21550Sstevel@tonic-gate 		assert(mp->end);
21560Sstevel@tonic-gate 		entryNum = s_strtol(eq + 1);
21570Sstevel@tonic-gate 		if (entryNum < 0 || entryNum > mp->end->entryNum) {
21580Sstevel@tonic-gate 			bam_error(INVALID_ENTRY, eq + 1);
21590Sstevel@tonic-gate 			free(opt_dup);
21600Sstevel@tonic-gate 			return (BAM_ERROR);
21610Sstevel@tonic-gate 		}
21620Sstevel@tonic-gate 		*entry = entryNum;
21630Sstevel@tonic-gate 	} else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) {
21640Sstevel@tonic-gate 		*title = opt + (eq - opt_dup) + 1;
21650Sstevel@tonic-gate 	} else {
21660Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
21670Sstevel@tonic-gate 		free(opt_dup);
21680Sstevel@tonic-gate 		return (BAM_ERROR);
21690Sstevel@tonic-gate 	}
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate 	free(opt_dup);
21720Sstevel@tonic-gate 	return (BAM_SUCCESS);
21730Sstevel@tonic-gate }
21740Sstevel@tonic-gate 
21750Sstevel@tonic-gate /*
21760Sstevel@tonic-gate  * If invoked with no titles/entries (opt == NULL)
21770Sstevel@tonic-gate  * only title lines in file are printed.
21780Sstevel@tonic-gate  *
21790Sstevel@tonic-gate  * If invoked with a title or entry #, all
21800Sstevel@tonic-gate  * lines in *every* matching entry are listed
21810Sstevel@tonic-gate  */
21820Sstevel@tonic-gate static error_t
21830Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt)
21840Sstevel@tonic-gate {
21850Sstevel@tonic-gate 	line_t *lp;
21860Sstevel@tonic-gate 	int entry = ENTRY_INIT;
21870Sstevel@tonic-gate 	int found;
21880Sstevel@tonic-gate 	char *title = NULL;
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 	assert(mp);
21910Sstevel@tonic-gate 	assert(menu_path);
21920Sstevel@tonic-gate 
21930Sstevel@tonic-gate 	if (mp->start == NULL) {
21940Sstevel@tonic-gate 		bam_error(NO_MENU, menu_path);
21950Sstevel@tonic-gate 		return (BAM_ERROR);
21960Sstevel@tonic-gate 	}
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate 	if (opt != NULL) {
21990Sstevel@tonic-gate 		if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
22000Sstevel@tonic-gate 			return (BAM_ERROR);
22010Sstevel@tonic-gate 		}
22020Sstevel@tonic-gate 		assert((entry != ENTRY_INIT) ^ (title != NULL));
22030Sstevel@tonic-gate 	} else {
22040Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0);
22050Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0);
22060Sstevel@tonic-gate 	}
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 	found = 0;
22090Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
22100Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY)
22110Sstevel@tonic-gate 			continue;
22120Sstevel@tonic-gate 		if (opt == NULL && lp->flags == BAM_TITLE) {
22130Sstevel@tonic-gate 			bam_print(PRINT_TITLE, lp->entryNum,
22140Sstevel@tonic-gate 			    lp->arg);
22150Sstevel@tonic-gate 			found = 1;
22160Sstevel@tonic-gate 			continue;
22170Sstevel@tonic-gate 		}
22180Sstevel@tonic-gate 		if (entry != ENTRY_INIT && lp->entryNum == entry) {
22190Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
22200Sstevel@tonic-gate 			found = 1;
22210Sstevel@tonic-gate 			continue;
22220Sstevel@tonic-gate 		}
22230Sstevel@tonic-gate 
22240Sstevel@tonic-gate 		/*
22250Sstevel@tonic-gate 		 * We set the entry value here so that all lines
22260Sstevel@tonic-gate 		 * in entry get printed. If we subsequently match
22270Sstevel@tonic-gate 		 * title in other entries, all lines in those
22280Sstevel@tonic-gate 		 * entries get printed as well.
22290Sstevel@tonic-gate 		 */
22300Sstevel@tonic-gate 		if (title && lp->flags == BAM_TITLE && lp->arg &&
22310Sstevel@tonic-gate 		    strncmp(title, lp->arg, strlen(title)) == 0) {
22320Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
22330Sstevel@tonic-gate 			entry = lp->entryNum;
22340Sstevel@tonic-gate 			found = 1;
22350Sstevel@tonic-gate 			continue;
22360Sstevel@tonic-gate 		}
22370Sstevel@tonic-gate 	}
22380Sstevel@tonic-gate 
22390Sstevel@tonic-gate 	if (!found) {
22400Sstevel@tonic-gate 		bam_error(NO_MATCH_ENTRY);
22410Sstevel@tonic-gate 		return (BAM_ERROR);
22420Sstevel@tonic-gate 	}
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate 	return (BAM_SUCCESS);
22450Sstevel@tonic-gate }
22460Sstevel@tonic-gate 
22470Sstevel@tonic-gate static int
22480Sstevel@tonic-gate add_boot_entry(menu_t *mp,
22490Sstevel@tonic-gate 	char *title,
22500Sstevel@tonic-gate 	char *root,
22510Sstevel@tonic-gate 	char *kernel,
22520Sstevel@tonic-gate 	char *module)
22530Sstevel@tonic-gate {
22540Sstevel@tonic-gate 	menu_t dummy;
22550Sstevel@tonic-gate 	int lineNum, entryNum;
22560Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate 	assert(mp);
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate 	if (title == NULL) {
22610Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[TITLE_CMD]);
22620Sstevel@tonic-gate 		return (BAM_ERROR);
22630Sstevel@tonic-gate 	}
22640Sstevel@tonic-gate 	if (root == NULL) {
22650Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[ROOT_CMD]);
22660Sstevel@tonic-gate 		return (BAM_ERROR);
22670Sstevel@tonic-gate 	}
22680Sstevel@tonic-gate 	if (kernel == NULL) {
22690Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]);
22700Sstevel@tonic-gate 		return (BAM_ERROR);
22710Sstevel@tonic-gate 	}
22720Sstevel@tonic-gate 	if (module == NULL) {
22730Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]);
22740Sstevel@tonic-gate 		return (BAM_ERROR);
22750Sstevel@tonic-gate 	}
22760Sstevel@tonic-gate 
22770Sstevel@tonic-gate 	if (mp->start) {
22780Sstevel@tonic-gate 		lineNum = mp->end->lineNum;
22790Sstevel@tonic-gate 		entryNum = mp->end->entryNum;
22800Sstevel@tonic-gate 	} else {
22810Sstevel@tonic-gate 		lineNum = LINE_INIT;
22820Sstevel@tonic-gate 		entryNum = ENTRY_INIT;
22830Sstevel@tonic-gate 	}
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 	/*
22860Sstevel@tonic-gate 	 * No separator for comment (HDR/FTR) commands
22870Sstevel@tonic-gate 	 * The syntax for comments is #<comment>
22880Sstevel@tonic-gate 	 */
22890Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
22900Sstevel@tonic-gate 	    menu_cmds[COMMENT_CMD], BAM_HDR);
22910Sstevel@tonic-gate 	dummy.start = dummy.end = NULL;
22920Sstevel@tonic-gate 	line_parser(&dummy, linebuf, &lineNum, &entryNum);
22930Sstevel@tonic-gate 	if (dummy.start == NULL || dummy.start->flags != BAM_COMMENT) {
22940Sstevel@tonic-gate 		line_free(dummy.start);
22950Sstevel@tonic-gate 		bam_error(INVALID_HDR, BAM_HDR);
22960Sstevel@tonic-gate 		return (BAM_ERROR);
22970Sstevel@tonic-gate 	}
22980Sstevel@tonic-gate 	assert(dummy.start == dummy.end);
22990Sstevel@tonic-gate 	append_line(mp, dummy.start);
23000Sstevel@tonic-gate 
23010Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
23020Sstevel@tonic-gate 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
23030Sstevel@tonic-gate 	dummy.start = dummy.end = NULL;
23040Sstevel@tonic-gate 	line_parser(&dummy, linebuf, &lineNum, &entryNum);
23050Sstevel@tonic-gate 	if (dummy.start == NULL || dummy.start->flags != BAM_TITLE) {
23060Sstevel@tonic-gate 		line_free(dummy.start);
23070Sstevel@tonic-gate 		bam_error(INVALID_TITLE, title);
23080Sstevel@tonic-gate 		return (BAM_ERROR);
23090Sstevel@tonic-gate 	}
23100Sstevel@tonic-gate 	assert(dummy.start == dummy.end);
23110Sstevel@tonic-gate 	append_line(mp, dummy.start);
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate 
23140Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
23150Sstevel@tonic-gate 	    menu_cmds[ROOT_CMD], menu_cmds[SEP_CMD], root);
23160Sstevel@tonic-gate 	dummy.start = dummy.end = NULL;
23170Sstevel@tonic-gate 	line_parser(&dummy, linebuf, &lineNum, &entryNum);
23180Sstevel@tonic-gate 	if (dummy.start == NULL || dummy.start->flags != BAM_ENTRY) {
23190Sstevel@tonic-gate 		line_free(dummy.start);
23200Sstevel@tonic-gate 		bam_error(INVALID_ROOT, root);
23210Sstevel@tonic-gate 		return (BAM_ERROR);
23220Sstevel@tonic-gate 	}
23230Sstevel@tonic-gate 	assert(dummy.start == dummy.end);
23240Sstevel@tonic-gate 	append_line(mp, dummy.start);
23250Sstevel@tonic-gate 
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
23280Sstevel@tonic-gate 	    menu_cmds[KERNEL_CMD], menu_cmds[SEP_CMD], kernel);
23290Sstevel@tonic-gate 	dummy.start = dummy.end = NULL;
23300Sstevel@tonic-gate 	line_parser(&dummy, linebuf, &lineNum, &entryNum);
23310Sstevel@tonic-gate 	if (dummy.start == NULL || dummy.start->flags != BAM_ENTRY) {
23320Sstevel@tonic-gate 		line_free(dummy.start);
23330Sstevel@tonic-gate 		bam_error(INVALID_KERNEL, kernel);
23340Sstevel@tonic-gate 		return (BAM_ERROR);
23350Sstevel@tonic-gate 	}
23360Sstevel@tonic-gate 	assert(dummy.start == dummy.end);
23370Sstevel@tonic-gate 	append_line(mp, dummy.start);
23380Sstevel@tonic-gate 
23390Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
23400Sstevel@tonic-gate 	    menu_cmds[MODULE_CMD], menu_cmds[SEP_CMD], module);
23410Sstevel@tonic-gate 	dummy.start = dummy.end = NULL;
23420Sstevel@tonic-gate 	line_parser(&dummy, linebuf, &lineNum, &entryNum);
23430Sstevel@tonic-gate 	if (dummy.start == NULL || dummy.start->flags != BAM_ENTRY) {
23440Sstevel@tonic-gate 		line_free(dummy.start);
23450Sstevel@tonic-gate 		bam_error(INVALID_MODULE, module);
23460Sstevel@tonic-gate 		return (BAM_ERROR);
23470Sstevel@tonic-gate 	}
23480Sstevel@tonic-gate 	assert(dummy.start == dummy.end);
23490Sstevel@tonic-gate 	append_line(mp, dummy.start);
23500Sstevel@tonic-gate 
23510Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
23520Sstevel@tonic-gate 	    menu_cmds[COMMENT_CMD], BAM_FTR);
23530Sstevel@tonic-gate 	dummy.start = dummy.end = NULL;
23540Sstevel@tonic-gate 	line_parser(&dummy, linebuf, &lineNum, &entryNum);
23550Sstevel@tonic-gate 	if (dummy.start == NULL || dummy.start->flags != BAM_COMMENT) {
23560Sstevel@tonic-gate 		line_free(dummy.start);
23570Sstevel@tonic-gate 		bam_error(INVALID_FOOTER, BAM_FTR);
23580Sstevel@tonic-gate 		return (BAM_ERROR);
23590Sstevel@tonic-gate 	}
23600Sstevel@tonic-gate 	assert(dummy.start == dummy.end);
23610Sstevel@tonic-gate 	append_line(mp, dummy.start);
23620Sstevel@tonic-gate 
23630Sstevel@tonic-gate 	return (entryNum);
23640Sstevel@tonic-gate }
23650Sstevel@tonic-gate 
23660Sstevel@tonic-gate static error_t
23670Sstevel@tonic-gate do_delete(menu_t *mp, int entryNum)
23680Sstevel@tonic-gate {
23690Sstevel@tonic-gate 	int bootadm_entry = 0;
23700Sstevel@tonic-gate 	line_t *lp, *prev, *save;
23710Sstevel@tonic-gate 	int deleted;
23720Sstevel@tonic-gate 
23730Sstevel@tonic-gate 	assert(entryNum != ENTRY_INIT);
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate 	deleted = 0;
23760Sstevel@tonic-gate 	prev = NULL;
23770Sstevel@tonic-gate 	for (lp = mp->start; lp; ) {
23780Sstevel@tonic-gate 
23790Sstevel@tonic-gate 		if (lp->entryNum == ENTRY_INIT) {
23800Sstevel@tonic-gate 			prev = lp;
23810Sstevel@tonic-gate 			lp = lp->next;
23820Sstevel@tonic-gate 			continue;
23830Sstevel@tonic-gate 		}
23840Sstevel@tonic-gate 
23850Sstevel@tonic-gate 		if (entryNum != ALL_ENTRIES && lp->entryNum != entryNum) {
23860Sstevel@tonic-gate 			prev = lp;
23870Sstevel@tonic-gate 			lp = lp->next;
23880Sstevel@tonic-gate 			continue;
23890Sstevel@tonic-gate 		}
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate 		/*
23920Sstevel@tonic-gate 		 * can only delete bootadm entries
23930Sstevel@tonic-gate 		 */
23940Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT && strcmp(lp->arg, BAM_HDR) == 0) {
23950Sstevel@tonic-gate 			bootadm_entry = 1;
23960Sstevel@tonic-gate 		}
23970Sstevel@tonic-gate 
23980Sstevel@tonic-gate 		if (!bootadm_entry) {
23990Sstevel@tonic-gate 			prev = lp;
24000Sstevel@tonic-gate 			lp = lp->next;
24010Sstevel@tonic-gate 			continue;
24020Sstevel@tonic-gate 		}
24030Sstevel@tonic-gate 
24040Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT && strcmp(lp->arg, BAM_FTR) == 0)
24050Sstevel@tonic-gate 			bootadm_entry = 0;
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 		if (prev == NULL)
24080Sstevel@tonic-gate 			mp->start = lp->next;
24090Sstevel@tonic-gate 		else
24100Sstevel@tonic-gate 			prev->next = lp->next;
24110Sstevel@tonic-gate 		if (mp->end == lp)
24120Sstevel@tonic-gate 			mp->end = prev;
24130Sstevel@tonic-gate 		save = lp->next;
24140Sstevel@tonic-gate 		line_free(lp);
24150Sstevel@tonic-gate 		lp = save;	/* prev stays the same */
24160Sstevel@tonic-gate 
24170Sstevel@tonic-gate 		deleted = 1;
24180Sstevel@tonic-gate 	}
24190Sstevel@tonic-gate 
24200Sstevel@tonic-gate 	if (!deleted && entryNum != ALL_ENTRIES) {
24210Sstevel@tonic-gate 		bam_error(NO_BOOTADM_MATCH);
24220Sstevel@tonic-gate 		return (BAM_ERROR);
24230Sstevel@tonic-gate 	}
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate 	return (BAM_SUCCESS);
24260Sstevel@tonic-gate }
24270Sstevel@tonic-gate 
24280Sstevel@tonic-gate static error_t
24290Sstevel@tonic-gate delete_entry(menu_t *mp, char *menu_path, char *opt)
24300Sstevel@tonic-gate {
24310Sstevel@tonic-gate 	int entry = ENTRY_INIT;
24320Sstevel@tonic-gate 	char *title = NULL;
24330Sstevel@tonic-gate 	line_t *lp;
24340Sstevel@tonic-gate 
24350Sstevel@tonic-gate 	assert(mp);
24360Sstevel@tonic-gate 	assert(opt);
24370Sstevel@tonic-gate 
24380Sstevel@tonic-gate 	/*
24390Sstevel@tonic-gate 	 * Do a quick check. If the file is empty
24400Sstevel@tonic-gate 	 * we have nothing to delete
24410Sstevel@tonic-gate 	 */
24420Sstevel@tonic-gate 	if (mp->start == NULL) {
24430Sstevel@tonic-gate 		bam_print(EMPTY_FILE, menu_path);
24440Sstevel@tonic-gate 		return (BAM_SUCCESS);
24450Sstevel@tonic-gate 	}
24460Sstevel@tonic-gate 
24470Sstevel@tonic-gate 	if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
24480Sstevel@tonic-gate 		return (BAM_ERROR);
24490Sstevel@tonic-gate 	}
24500Sstevel@tonic-gate 	assert((entry != ENTRY_INIT) ^ (title != NULL));
24510Sstevel@tonic-gate 
24520Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
24530Sstevel@tonic-gate 		if (entry != ENTRY_INIT)
24540Sstevel@tonic-gate 			break;
24550Sstevel@tonic-gate 		assert(title);
24560Sstevel@tonic-gate 		if (lp->flags == BAM_TITLE &&
24570Sstevel@tonic-gate 		    lp->arg && strcmp(lp->arg, title) == 0) {
24580Sstevel@tonic-gate 			entry = lp->entryNum;
24590Sstevel@tonic-gate 			break;
24600Sstevel@tonic-gate 		}
24610Sstevel@tonic-gate 	}
24620Sstevel@tonic-gate 
24630Sstevel@tonic-gate 	if (entry == ENTRY_INIT) {
24640Sstevel@tonic-gate 		bam_error(NO_MATCH, title);
24650Sstevel@tonic-gate 		return (BAM_ERROR);
24660Sstevel@tonic-gate 	}
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate 	if (do_delete(mp, entry) != BAM_SUCCESS) {
24690Sstevel@tonic-gate 		return (BAM_ERROR);
24700Sstevel@tonic-gate 	}
24710Sstevel@tonic-gate 
24720Sstevel@tonic-gate 	return (BAM_WRITE);
24730Sstevel@tonic-gate }
24740Sstevel@tonic-gate 
24750Sstevel@tonic-gate static error_t
24760Sstevel@tonic-gate delete_all_entries(menu_t *mp, char *menu_path, char *opt)
24770Sstevel@tonic-gate {
24780Sstevel@tonic-gate 	assert(mp);
24790Sstevel@tonic-gate 	assert(opt == NULL);
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 	if (mp->start == NULL) {
24820Sstevel@tonic-gate 		bam_print(EMPTY_FILE, menu_path);
24830Sstevel@tonic-gate 		return (BAM_SUCCESS);
24840Sstevel@tonic-gate 	}
24850Sstevel@tonic-gate 
24860Sstevel@tonic-gate 	if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) {
24870Sstevel@tonic-gate 		return (BAM_ERROR);
24880Sstevel@tonic-gate 	}
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate 	return (BAM_WRITE);
24910Sstevel@tonic-gate }
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate static FILE *
24940Sstevel@tonic-gate open_diskmap(void)
24950Sstevel@tonic-gate {
24960Sstevel@tonic-gate 	FILE *fp;
24970Sstevel@tonic-gate 	char cmd[PATH_MAX];
24980Sstevel@tonic-gate 
24990Sstevel@tonic-gate 	/* make sure we have a map file */
25000Sstevel@tonic-gate 	fp = fopen(GRUBDISK_MAP, "r");
25010Sstevel@tonic-gate 	if (fp == NULL) {
25020Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd),
25030Sstevel@tonic-gate 		    "%s > /dev/null", CREATE_DISKMAP);
25040Sstevel@tonic-gate 		(void) system(cmd);
25050Sstevel@tonic-gate 		fp = fopen(GRUBDISK_MAP, "r");
25060Sstevel@tonic-gate 	}
25070Sstevel@tonic-gate 	return (fp);
25080Sstevel@tonic-gate }
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate #define	SECTOR_SIZE	512
25110Sstevel@tonic-gate 
25120Sstevel@tonic-gate static int
25130Sstevel@tonic-gate get_partition(char *device)
25140Sstevel@tonic-gate {
25150Sstevel@tonic-gate 	int i, fd, is_pcfs, partno = -1;
25160Sstevel@tonic-gate 	struct mboot *mboot;
25170Sstevel@tonic-gate 	char boot_sect[SECTOR_SIZE];
25180Sstevel@tonic-gate 	char *wholedisk, *slice;
25190Sstevel@tonic-gate 
25200Sstevel@tonic-gate 	/* form whole disk (p0) */
25210Sstevel@tonic-gate 	slice = device + strlen(device) - 2;
25220Sstevel@tonic-gate 	is_pcfs = (*slice != 's');
25230Sstevel@tonic-gate 	if (!is_pcfs)
25240Sstevel@tonic-gate 		*slice = '\0';
25250Sstevel@tonic-gate 	wholedisk = s_calloc(1, strlen(device) + 3);
25260Sstevel@tonic-gate 	(void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device);
25270Sstevel@tonic-gate 	if (!is_pcfs)
25280Sstevel@tonic-gate 		*slice = 's';
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 	/* read boot sector */
25310Sstevel@tonic-gate 	fd = open(wholedisk, O_RDONLY);
25320Sstevel@tonic-gate 	free(wholedisk);
25330Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
25340Sstevel@tonic-gate 		return (partno);
25350Sstevel@tonic-gate 	}
25360Sstevel@tonic-gate 	(void) close(fd);
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate 	/* parse fdisk table */
25390Sstevel@tonic-gate 	mboot = (struct mboot *)((void *)boot_sect);
25400Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
25410Sstevel@tonic-gate 		struct ipart *part =
25420Sstevel@tonic-gate 		    (struct ipart *)(uintptr_t)mboot->parts + i;
25430Sstevel@tonic-gate 		if (is_pcfs) {	/* looking for solaris boot part */
25440Sstevel@tonic-gate 			if (part->systid == 0xbe) {
25450Sstevel@tonic-gate 				partno = i;
25460Sstevel@tonic-gate 				break;
25470Sstevel@tonic-gate 			}
25480Sstevel@tonic-gate 		} else {	/* look for solaris partition, old and new */
25490Sstevel@tonic-gate 			if (part->systid == SUNIXOS ||
25500Sstevel@tonic-gate 			    part->systid == SUNIXOS2) {
25510Sstevel@tonic-gate 				partno = i;
25520Sstevel@tonic-gate 				break;
25530Sstevel@tonic-gate 			}
25540Sstevel@tonic-gate 		}
25550Sstevel@tonic-gate 	}
25560Sstevel@tonic-gate 	return (partno);
25570Sstevel@tonic-gate }
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate static char *
25600Sstevel@tonic-gate get_grubdisk(char *rootdev, FILE *fp, int on_bootdev)
25610Sstevel@tonic-gate {
25620Sstevel@tonic-gate 	char *grubdisk;	/* (hd#,#,#) */
25630Sstevel@tonic-gate 	char *slice;
25640Sstevel@tonic-gate 	char *grubhd;
25650Sstevel@tonic-gate 	int fdiskpart;
25660Sstevel@tonic-gate 	int found = 0;
25670Sstevel@tonic-gate 	char *devname, *ctdname = strstr(rootdev, "dsk/");
25680Sstevel@tonic-gate 	char linebuf[PATH_MAX];
25690Sstevel@tonic-gate 
25700Sstevel@tonic-gate 	if (ctdname == NULL)
25710Sstevel@tonic-gate 		return (NULL);
25720Sstevel@tonic-gate 
25730Sstevel@tonic-gate 	ctdname += strlen("dsk/");
25740Sstevel@tonic-gate 	slice = strrchr(ctdname, 's');
25750Sstevel@tonic-gate 	if (slice)
25760Sstevel@tonic-gate 		*slice = '\0';
25770Sstevel@tonic-gate 
25780Sstevel@tonic-gate 	rewind(fp);
25790Sstevel@tonic-gate 	while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) {
25800Sstevel@tonic-gate 		grubhd = strtok(linebuf, " \t\n");
25810Sstevel@tonic-gate 		if (grubhd)
25820Sstevel@tonic-gate 			devname = strtok(NULL, " \t\n");
25830Sstevel@tonic-gate 		else
25840Sstevel@tonic-gate 			devname = NULL;
25850Sstevel@tonic-gate 		if (devname && strcmp(devname, ctdname) == 0) {
25860Sstevel@tonic-gate 			found = 1;
25870Sstevel@tonic-gate 			break;
25880Sstevel@tonic-gate 		}
25890Sstevel@tonic-gate 	}
25900Sstevel@tonic-gate 
25910Sstevel@tonic-gate 	if (slice)
25920Sstevel@tonic-gate 		*slice = 's';
25930Sstevel@tonic-gate 
25940Sstevel@tonic-gate 	if (found == 0) {
25950Sstevel@tonic-gate 		if (bam_verbose)
25960Sstevel@tonic-gate 			bam_print(DISKMAP_FAIL_NONFATAL, rootdev);
25970Sstevel@tonic-gate 		grubhd = "0";	/* assume disk 0 if can't match */
25980Sstevel@tonic-gate 	}
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate 	fdiskpart = get_partition(rootdev);
26010Sstevel@tonic-gate 	if (fdiskpart == -1)
26020Sstevel@tonic-gate 		return (NULL);
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 	grubdisk = s_calloc(1, 10);
26050Sstevel@tonic-gate 	if (slice) {
26060Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d,%c)",
26070Sstevel@tonic-gate 		    grubhd, fdiskpart, slice[1] + 'a' - '0');
26080Sstevel@tonic-gate 	} else
26090Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d)",
26100Sstevel@tonic-gate 		    grubhd, fdiskpart);
26110Sstevel@tonic-gate 
26120Sstevel@tonic-gate 	/* if root not on bootdev, change GRUB disk to 0 */
26130Sstevel@tonic-gate 	if (!on_bootdev)
26140Sstevel@tonic-gate 		grubdisk[3] = '0';
26150Sstevel@tonic-gate 	return (grubdisk);
26160Sstevel@tonic-gate }
26170Sstevel@tonic-gate 
26180Sstevel@tonic-gate static char *get_title(char *rootdir)
26190Sstevel@tonic-gate {
26200Sstevel@tonic-gate 	static char title[80];	/* from /etc/release */
26210Sstevel@tonic-gate 	char *cp, release[PATH_MAX];
26220Sstevel@tonic-gate 	FILE *fp;
26230Sstevel@tonic-gate 
26240Sstevel@tonic-gate 	/* open the /etc/release file */
26250Sstevel@tonic-gate 	(void) snprintf(release, sizeof (release), "%s/etc/release", rootdir);
26260Sstevel@tonic-gate 
26270Sstevel@tonic-gate 	fp = fopen(release, "r");
26280Sstevel@tonic-gate 	if (fp == NULL)
26290Sstevel@tonic-gate 		return ("Solaris");	/* default to Solaris */
26300Sstevel@tonic-gate 
26310Sstevel@tonic-gate 	while (s_fgets(title, sizeof (title), fp) != NULL) {
26320Sstevel@tonic-gate 		cp = strstr(title, "Solaris");
26330Sstevel@tonic-gate 		if (cp)
26340Sstevel@tonic-gate 			break;
26350Sstevel@tonic-gate 	}
26360Sstevel@tonic-gate 	(void) fclose(fp);
26370Sstevel@tonic-gate 	return (cp);
26380Sstevel@tonic-gate }
26390Sstevel@tonic-gate 
26400Sstevel@tonic-gate static char *
26410Sstevel@tonic-gate get_special(char *mountp)
26420Sstevel@tonic-gate {
26430Sstevel@tonic-gate 	FILE *mntfp;
26440Sstevel@tonic-gate 	struct mnttab mp = {0}, mpref = {0};
26450Sstevel@tonic-gate 
26460Sstevel@tonic-gate 	mntfp = fopen(MNTTAB, "r");
26470Sstevel@tonic-gate 	if (mntfp == NULL) {
26480Sstevel@tonic-gate 		return (0);
26490Sstevel@tonic-gate 	}
26500Sstevel@tonic-gate 
26510Sstevel@tonic-gate 	if (*mountp == '\0')
26520Sstevel@tonic-gate 		mpref.mnt_mountp = "/";
26530Sstevel@tonic-gate 	else
26540Sstevel@tonic-gate 		mpref.mnt_mountp = mountp;
26550Sstevel@tonic-gate 	if (getmntany(mntfp, &mp, &mpref) != 0) {
26560Sstevel@tonic-gate 		(void) fclose(mntfp);
26570Sstevel@tonic-gate 		return (NULL);
26580Sstevel@tonic-gate 	}
26590Sstevel@tonic-gate 	(void) fclose(mntfp);
26600Sstevel@tonic-gate 
26610Sstevel@tonic-gate 	return (s_strdup(mp.mnt_special));
26620Sstevel@tonic-gate }
26630Sstevel@tonic-gate 
26640Sstevel@tonic-gate static char *
26650Sstevel@tonic-gate os_to_grubdisk(char *osdisk, int on_bootdev)
26660Sstevel@tonic-gate {
26670Sstevel@tonic-gate 	FILE *fp;
26680Sstevel@tonic-gate 	char *grubdisk;
26690Sstevel@tonic-gate 
26700Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
26710Sstevel@tonic-gate 	fp = open_diskmap();
26720Sstevel@tonic-gate 	if (fp == NULL) {
26730Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdisk);
26740Sstevel@tonic-gate 		return (NULL);
26750Sstevel@tonic-gate 	}
26760Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdisk, fp, on_bootdev);
26770Sstevel@tonic-gate 	(void) fclose(fp);
26780Sstevel@tonic-gate 	return (grubdisk);
26790Sstevel@tonic-gate }
26800Sstevel@tonic-gate 
26810Sstevel@tonic-gate /*
26820Sstevel@tonic-gate  * Check if root is on the boot device
26830Sstevel@tonic-gate  * Return 0 (false) on error
26840Sstevel@tonic-gate  */
26850Sstevel@tonic-gate static int
26860Sstevel@tonic-gate menu_on_bootdev(char *menu_root, FILE *fp)
26870Sstevel@tonic-gate {
26880Sstevel@tonic-gate 	int ret;
26890Sstevel@tonic-gate 	char *grubhd, *bootp, *special;
26900Sstevel@tonic-gate 
26910Sstevel@tonic-gate 	special = get_special(menu_root);
26920Sstevel@tonic-gate 	if (special == NULL)
26930Sstevel@tonic-gate 		return (0);
26940Sstevel@tonic-gate 	bootp = strstr(special, "p0:boot");
26950Sstevel@tonic-gate 	if (bootp)
26960Sstevel@tonic-gate 		*bootp = '\0';
26970Sstevel@tonic-gate 	grubhd = get_grubdisk(special, fp, 1);
26980Sstevel@tonic-gate 	free(special);
26990Sstevel@tonic-gate 
27000Sstevel@tonic-gate 	if (grubhd == NULL)
27010Sstevel@tonic-gate 		return (0);
27020Sstevel@tonic-gate 	ret = grubhd[3] == '0';
27030Sstevel@tonic-gate 	free(grubhd);
27040Sstevel@tonic-gate 	return (ret);
27050Sstevel@tonic-gate }
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate /*ARGSUSED*/
27080Sstevel@tonic-gate static error_t
27090Sstevel@tonic-gate update_entry(menu_t *mp, char *menu_root, char *opt)
27100Sstevel@tonic-gate {
27110Sstevel@tonic-gate 	FILE *fp;
27120Sstevel@tonic-gate 	int entry;
27130Sstevel@tonic-gate 	line_t *lp;
27140Sstevel@tonic-gate 	char *grubdisk, *title, *osdev, *osroot;
27150Sstevel@tonic-gate 	int bootadm_entry, entry_to_delete;
27160Sstevel@tonic-gate 
27170Sstevel@tonic-gate 	assert(mp);
27180Sstevel@tonic-gate 	assert(opt);
27190Sstevel@tonic-gate 
27200Sstevel@tonic-gate 	osdev = strtok(opt, ",");
27210Sstevel@tonic-gate 	osroot = strtok(NULL, ",");
27220Sstevel@tonic-gate 	if (osroot == NULL)
27230Sstevel@tonic-gate 		osroot = menu_root;
27240Sstevel@tonic-gate 	title = get_title(osroot);
27250Sstevel@tonic-gate 
27260Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
27270Sstevel@tonic-gate 	fp = open_diskmap();
27280Sstevel@tonic-gate 	if (fp == NULL) {
27290Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
27300Sstevel@tonic-gate 		return (BAM_ERROR);
27310Sstevel@tonic-gate 	}
27320Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdev, fp, menu_on_bootdev(menu_root, fp));
27330Sstevel@tonic-gate 	(void) fclose(fp);
27340Sstevel@tonic-gate 	if (grubdisk == NULL) {
27350Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
27360Sstevel@tonic-gate 		return (BAM_ERROR);
27370Sstevel@tonic-gate 	}
27380Sstevel@tonic-gate 
27390Sstevel@tonic-gate 	/* delete existing entries with matching grub hd name */
27400Sstevel@tonic-gate 	for (;;) {
27410Sstevel@tonic-gate 		entry_to_delete = -1;
27420Sstevel@tonic-gate 		bootadm_entry = 0;
27430Sstevel@tonic-gate 		for (lp = mp->start; lp; lp = lp->next) {
27440Sstevel@tonic-gate 			/*
27450Sstevel@tonic-gate 			 * can only delete bootadm entries
27460Sstevel@tonic-gate 			 */
27470Sstevel@tonic-gate 			if (lp->flags == BAM_COMMENT) {
27480Sstevel@tonic-gate 				if (strcmp(lp->arg, BAM_HDR) == 0)
27490Sstevel@tonic-gate 					bootadm_entry = 1;
27500Sstevel@tonic-gate 				else if (strcmp(lp->arg, BAM_FTR) == 0)
27510Sstevel@tonic-gate 					bootadm_entry = 0;
27520Sstevel@tonic-gate 			}
27530Sstevel@tonic-gate 
27540Sstevel@tonic-gate 			if (bootadm_entry && lp->flags == BAM_ENTRY &&
27550Sstevel@tonic-gate 			    strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0 &&
27560Sstevel@tonic-gate 			    strcmp(lp->arg, grubdisk) == 0) {
27570Sstevel@tonic-gate 				entry_to_delete = lp->entryNum;
27580Sstevel@tonic-gate 			}
27590Sstevel@tonic-gate 		}
27600Sstevel@tonic-gate 		if (entry_to_delete == -1)
27610Sstevel@tonic-gate 			break;
27620Sstevel@tonic-gate 		(void) do_delete(mp, entry_to_delete);
27630Sstevel@tonic-gate 	}
27640Sstevel@tonic-gate 
27650Sstevel@tonic-gate 	/* add the entry for normal Solaris */
27660Sstevel@tonic-gate 	entry = add_boot_entry(mp, title, grubdisk,
27670Sstevel@tonic-gate 	    "/platform/i86pc/multiboot",
27680Sstevel@tonic-gate 	    "/platform/i86pc/boot_archive");
27690Sstevel@tonic-gate 
27700Sstevel@tonic-gate 	/* add the entry for failsafe archive */
27710Sstevel@tonic-gate 	(void) add_boot_entry(mp, "Solaris failsafe", grubdisk,
27720Sstevel@tonic-gate 	    "/boot/multiboot kernel/unix -s",
27730Sstevel@tonic-gate 	    "/boot/x86.miniroot-safe");
27740Sstevel@tonic-gate 	free(grubdisk);
27750Sstevel@tonic-gate 
27760Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
27770Sstevel@tonic-gate 		return (BAM_ERROR);
27780Sstevel@tonic-gate 	}
27790Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
27800Sstevel@tonic-gate 	return (BAM_WRITE);
27810Sstevel@tonic-gate }
27820Sstevel@tonic-gate 
2783316Svikram static char *
2784316Svikram read_grub_root(void)
2785316Svikram {
2786316Svikram 	FILE *fp;
2787316Svikram 	struct stat sb;
2788316Svikram 	char buf[BAM_MAXLINE];
2789316Svikram 	char *rootstr;
2790316Svikram 
2791316Svikram 	if (stat(GRUB_slice, &sb) != 0) {
2792316Svikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
2793316Svikram 		return (NULL);
2794316Svikram 	}
2795316Svikram 
2796316Svikram 	if (stat(GRUB_root, &sb) != 0) {
2797316Svikram 		bam_error(MISSING_ROOT_FILE, GRUB_root, strerror(errno));
2798316Svikram 		return (NULL);
2799316Svikram 	}
2800316Svikram 
2801316Svikram 	fp = fopen(GRUB_root, "r");
2802316Svikram 	if (fp == NULL) {
2803316Svikram 		bam_error(OPEN_FAIL, GRUB_root, strerror(errno));
2804316Svikram 		return (NULL);
2805316Svikram 	}
2806316Svikram 
2807316Svikram 	if (s_fgets(buf, sizeof (buf), fp) == NULL) {
2808316Svikram 		bam_error(EMPTY_FILE, GRUB_root, strerror(errno));
2809316Svikram 		(void) fclose(fp);
2810316Svikram 		return (NULL);
2811316Svikram 	}
2812316Svikram 
2813316Svikram 	/*
2814316Svikram 	 * Copy buf here as check below may trash the buffer
2815316Svikram 	 */
2816316Svikram 	rootstr = s_strdup(buf);
2817316Svikram 
2818316Svikram 	if (s_fgets(buf, sizeof (buf), fp) != NULL) {
2819316Svikram 		bam_error(BAD_ROOT_FILE, GRUB_root);
2820316Svikram 		free(rootstr);
2821316Svikram 		rootstr = NULL;
2822316Svikram 	}
2823316Svikram 
2824316Svikram 	(void) fclose(fp);
2825316Svikram 
2826316Svikram 	return (rootstr);
2827316Svikram }
2828316Svikram 
28290Sstevel@tonic-gate /*
28300Sstevel@tonic-gate  * This function is for supporting reboot with args.
28310Sstevel@tonic-gate  * The opt value can be:
28320Sstevel@tonic-gate  * NULL		delete temp entry, if present
28330Sstevel@tonic-gate  * entry=#	switches default entry to 1
28340Sstevel@tonic-gate  * else		treated as boot-args and setup a temperary menu entry
28350Sstevel@tonic-gate  *		and make it the default
28360Sstevel@tonic-gate  */
28370Sstevel@tonic-gate #define	REBOOT_TITLE	"Solaris_reboot_transient"
28380Sstevel@tonic-gate 
28390Sstevel@tonic-gate static error_t
28400Sstevel@tonic-gate update_temp(menu_t *mp, char *menupath, char *opt)
28410Sstevel@tonic-gate {
28420Sstevel@tonic-gate 	int entry;
28430Sstevel@tonic-gate 	char *grubdisk, *rootdev;
28440Sstevel@tonic-gate 	char kernbuf[1024];
2845316Svikram 	struct stat sb;
28460Sstevel@tonic-gate 
28470Sstevel@tonic-gate 	assert(mp);
28480Sstevel@tonic-gate 
28490Sstevel@tonic-gate 	if (opt != NULL &&
28500Sstevel@tonic-gate 	    strncmp(opt, "entry=", strlen("entry=")) == 0 &&
28510Sstevel@tonic-gate 	    selector(mp, opt, &entry, NULL) == BAM_SUCCESS) {
28520Sstevel@tonic-gate 		/* this is entry=# option */
28530Sstevel@tonic-gate 		return (set_global(mp, menu_cmds[DEFAULT_CMD], entry));
28540Sstevel@tonic-gate 	}
28550Sstevel@tonic-gate 
28560Sstevel@tonic-gate 	/* If no option, delete exiting reboot menu entry */
28570Sstevel@tonic-gate 	if (opt == NULL)
28580Sstevel@tonic-gate 		return (delete_entry(mp, menupath, "title="REBOOT_TITLE));
28590Sstevel@tonic-gate 
28600Sstevel@tonic-gate 	/*
28610Sstevel@tonic-gate 	 * add a new menu entry base on opt and make it the default
28620Sstevel@tonic-gate 	 */
2863316Svikram 	grubdisk = NULL;
2864316Svikram 	if (stat(GRUB_slice, &sb) != 0) {
2865316Svikram 		/*
2866316Svikram 		 * 1. First get root disk name from mnttab
2867316Svikram 		 * 2. Translate disk name to grub name
2868316Svikram 		 * 3. Add the new menu entry
2869316Svikram 		 */
2870316Svikram 		rootdev = get_special("/");
2871316Svikram 		if (rootdev) {
2872316Svikram 			grubdisk = os_to_grubdisk(rootdev, 1);
2873316Svikram 			free(rootdev);
2874316Svikram 		}
2875316Svikram 	} else {
2876316Svikram 		/*
2877316Svikram 		 * This is an LU BE. The GRUB_root file
2878316Svikram 		 * contains entry for GRUB's "root" cmd.
2879316Svikram 		 */
2880316Svikram 		grubdisk = read_grub_root();
28810Sstevel@tonic-gate 	}
28820Sstevel@tonic-gate 	if (grubdisk == NULL) {
2883316Svikram 		bam_error(REBOOT_WITH_ARGS_FAILED);
28840Sstevel@tonic-gate 		return (BAM_ERROR);
28850Sstevel@tonic-gate 	}
28860Sstevel@tonic-gate 
28870Sstevel@tonic-gate 	/* add an entry for Solaris reboot */
28880Sstevel@tonic-gate 	(void) snprintf(kernbuf, sizeof (kernbuf),
28890Sstevel@tonic-gate 	    "/platform/i86pc/multiboot %s", opt);
28900Sstevel@tonic-gate 	entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf,
28910Sstevel@tonic-gate 	    "/platform/i86pc/boot_archive");
28920Sstevel@tonic-gate 	free(grubdisk);
28930Sstevel@tonic-gate 
28940Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
2895316Svikram 		bam_error(REBOOT_WITH_ARGS_FAILED);
28960Sstevel@tonic-gate 		return (BAM_ERROR);
28970Sstevel@tonic-gate 	}
28980Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
28990Sstevel@tonic-gate 	return (BAM_WRITE);
29000Sstevel@tonic-gate }
29010Sstevel@tonic-gate 
29020Sstevel@tonic-gate static error_t
29030Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val)
29040Sstevel@tonic-gate {
29050Sstevel@tonic-gate 	line_t *lp, *found, *last;
29060Sstevel@tonic-gate 	char *cp, *str;
29070Sstevel@tonic-gate 	char prefix[BAM_MAXLINE];
29080Sstevel@tonic-gate 	size_t len;
29090Sstevel@tonic-gate 
29100Sstevel@tonic-gate 	assert(mp);
29110Sstevel@tonic-gate 	assert(globalcmd);
29120Sstevel@tonic-gate 
2913316Svikram 	if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) {
2914316Svikram 		if (val < 0 || mp->end == NULL || val > mp->end->entryNum) {
2915316Svikram 			(void) snprintf(prefix, sizeof (prefix), "%d", val);
2916316Svikram 			bam_error(INVALID_ENTRY, prefix);
2917316Svikram 			return (BAM_ERROR);
2918316Svikram 		}
2919316Svikram 	}
2920316Svikram 
29210Sstevel@tonic-gate 	found = last = NULL;
29220Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
29230Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
29240Sstevel@tonic-gate 			continue;
29250Sstevel@tonic-gate 
29260Sstevel@tonic-gate 		last = lp; /* track the last global found */
29270Sstevel@tonic-gate 
29280Sstevel@tonic-gate 		if (lp->cmd == NULL) {
29290Sstevel@tonic-gate 			bam_error(NO_CMD, lp->lineNum);
29300Sstevel@tonic-gate 			continue;
29310Sstevel@tonic-gate 		}
29320Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
29330Sstevel@tonic-gate 			continue;
29340Sstevel@tonic-gate 
29350Sstevel@tonic-gate 		if (found) {
29360Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
29370Sstevel@tonic-gate 		}
29380Sstevel@tonic-gate 		found = lp;
29390Sstevel@tonic-gate 	}
29400Sstevel@tonic-gate 
29410Sstevel@tonic-gate 	if (found == NULL) {
29420Sstevel@tonic-gate 		lp = s_calloc(1, sizeof (line_t));
29430Sstevel@tonic-gate 		if (last == NULL) {
29440Sstevel@tonic-gate 			lp->next = mp->start;
29450Sstevel@tonic-gate 			mp->start = lp;
29460Sstevel@tonic-gate 			mp->end = (mp->end) ? mp->end : lp;
29470Sstevel@tonic-gate 		} else {
29480Sstevel@tonic-gate 			lp->next = last->next;
29490Sstevel@tonic-gate 			last->next = lp;
29500Sstevel@tonic-gate 			if (lp->next == NULL)
29510Sstevel@tonic-gate 				mp->end = lp;
29520Sstevel@tonic-gate 		}
29530Sstevel@tonic-gate 		lp->flags = BAM_GLOBAL; /* other fields not needed for writes */
29540Sstevel@tonic-gate 		len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]);
29550Sstevel@tonic-gate 		len += 10;	/* val < 10 digits */
29560Sstevel@tonic-gate 		lp->line = s_calloc(1, len);
29570Sstevel@tonic-gate 		(void) snprintf(lp->line, len, "%s%s%d",
29580Sstevel@tonic-gate 		    globalcmd, menu_cmds[SEP_CMD], val);
29590Sstevel@tonic-gate 		return (BAM_WRITE);
29600Sstevel@tonic-gate 	}
29610Sstevel@tonic-gate 
29620Sstevel@tonic-gate 	/*
29630Sstevel@tonic-gate 	 * We are changing an existing entry. Retain any prefix whitespace,
29640Sstevel@tonic-gate 	 * but overwrite everything else. This preserves tabs added for
29650Sstevel@tonic-gate 	 * readability.
29660Sstevel@tonic-gate 	 */
29670Sstevel@tonic-gate 	str = found->line;
29680Sstevel@tonic-gate 	cp = prefix;
29690Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
29700Sstevel@tonic-gate 		*(cp++) = *(str++);
29710Sstevel@tonic-gate 	*cp = '\0'; /* Terminate prefix */
29720Sstevel@tonic-gate 	len = strlen(prefix) + strlen(globalcmd);
29730Sstevel@tonic-gate 	len += strlen(menu_cmds[SEP_CMD]) + 10;
29740Sstevel@tonic-gate 
29750Sstevel@tonic-gate 	free(found->line);
29760Sstevel@tonic-gate 	found->line = s_calloc(1, len);
29770Sstevel@tonic-gate 	(void) snprintf(found->line, len,
29780Sstevel@tonic-gate 		"%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val);
29790Sstevel@tonic-gate 
29800Sstevel@tonic-gate 	return (BAM_WRITE); /* need a write to menu */
29810Sstevel@tonic-gate }
29820Sstevel@tonic-gate 
29830Sstevel@tonic-gate /*ARGSUSED*/
29840Sstevel@tonic-gate static error_t
29850Sstevel@tonic-gate set_option(menu_t *mp, char *menu_path, char *opt)
29860Sstevel@tonic-gate {
29870Sstevel@tonic-gate 	int optnum, optval;
29880Sstevel@tonic-gate 	char *val;
29890Sstevel@tonic-gate 
29900Sstevel@tonic-gate 	assert(mp);
29910Sstevel@tonic-gate 	assert(opt);
29920Sstevel@tonic-gate 
29930Sstevel@tonic-gate 	val = strchr(opt, '=');
29940Sstevel@tonic-gate 	if (val == NULL) {
29950Sstevel@tonic-gate 		bam_error(INVALID_ENTRY, opt);
29960Sstevel@tonic-gate 		return (BAM_ERROR);
29970Sstevel@tonic-gate 	}
29980Sstevel@tonic-gate 
29990Sstevel@tonic-gate 	*val = '\0';
30000Sstevel@tonic-gate 	if (strcmp(opt, "default") == 0) {
30010Sstevel@tonic-gate 		optnum = DEFAULT_CMD;
30020Sstevel@tonic-gate 	} else if (strcmp(opt, "timeout") == 0) {
30030Sstevel@tonic-gate 		optnum = TIMEOUT_CMD;
30040Sstevel@tonic-gate 	} else {
30050Sstevel@tonic-gate 		bam_error(INVALID_ENTRY, opt);
30060Sstevel@tonic-gate 		return (BAM_ERROR);
30070Sstevel@tonic-gate 	}
30080Sstevel@tonic-gate 
30090Sstevel@tonic-gate 	optval = s_strtol(val + 1);
30100Sstevel@tonic-gate 	*val = '=';
30110Sstevel@tonic-gate 	return (set_global(mp, menu_cmds[optnum], optval));
30120Sstevel@tonic-gate }
30130Sstevel@tonic-gate 
30140Sstevel@tonic-gate /*
30150Sstevel@tonic-gate  * The quiet argument suppresses messages. This is used
30160Sstevel@tonic-gate  * when invoked in the context of other commands (e.g. list_entry)
30170Sstevel@tonic-gate  */
30180Sstevel@tonic-gate static error_t
30190Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet)
30200Sstevel@tonic-gate {
30210Sstevel@tonic-gate 	line_t *lp;
30220Sstevel@tonic-gate 	char *arg;
30230Sstevel@tonic-gate 	int done, ret = BAM_SUCCESS;
30240Sstevel@tonic-gate 
30250Sstevel@tonic-gate 	assert(mp);
30260Sstevel@tonic-gate 	assert(menu_path);
30270Sstevel@tonic-gate 	assert(globalcmd);
30280Sstevel@tonic-gate 
30290Sstevel@tonic-gate 	if (mp->start == NULL) {
30300Sstevel@tonic-gate 		if (!quiet)
30310Sstevel@tonic-gate 			bam_error(NO_MENU, menu_path);
30320Sstevel@tonic-gate 		return (BAM_ERROR);
30330Sstevel@tonic-gate 	}
30340Sstevel@tonic-gate 
30350Sstevel@tonic-gate 	done = 0;
30360Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
30370Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
30380Sstevel@tonic-gate 			continue;
30390Sstevel@tonic-gate 
30400Sstevel@tonic-gate 		if (lp->cmd == NULL) {
30410Sstevel@tonic-gate 			if (!quiet)
30420Sstevel@tonic-gate 				bam_error(NO_CMD, lp->lineNum);
30430Sstevel@tonic-gate 			continue;
30440Sstevel@tonic-gate 		}
30450Sstevel@tonic-gate 
30460Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
30470Sstevel@tonic-gate 			continue;
30480Sstevel@tonic-gate 
30490Sstevel@tonic-gate 		/* Found global. Check for duplicates */
30500Sstevel@tonic-gate 		if (done && !quiet) {
30510Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
30520Sstevel@tonic-gate 			ret = BAM_ERROR;
30530Sstevel@tonic-gate 		}
30540Sstevel@tonic-gate 
30550Sstevel@tonic-gate 		arg = lp->arg ? lp->arg : "";
30560Sstevel@tonic-gate 		bam_print(GLOBAL_CMD, globalcmd, arg);
30570Sstevel@tonic-gate 		done = 1;
30580Sstevel@tonic-gate 	}
30590Sstevel@tonic-gate 
30600Sstevel@tonic-gate 	if (!done && bam_verbose)
30610Sstevel@tonic-gate 		bam_print(NO_ENTRY, globalcmd);
30620Sstevel@tonic-gate 
30630Sstevel@tonic-gate 	return (ret);
30640Sstevel@tonic-gate }
30650Sstevel@tonic-gate 
30660Sstevel@tonic-gate static error_t
30670Sstevel@tonic-gate menu_write(char *root, menu_t *mp)
30680Sstevel@tonic-gate {
30690Sstevel@tonic-gate 	return (list2file(root, MENU_TMP, GRUB_MENU, mp->start));
30700Sstevel@tonic-gate }
30710Sstevel@tonic-gate 
30720Sstevel@tonic-gate static void
30730Sstevel@tonic-gate line_free(line_t *lp)
30740Sstevel@tonic-gate {
30750Sstevel@tonic-gate 	if (lp == NULL)
30760Sstevel@tonic-gate 		return;
30770Sstevel@tonic-gate 
30780Sstevel@tonic-gate 	if (lp->cmd)
30790Sstevel@tonic-gate 		free(lp->cmd);
30800Sstevel@tonic-gate 	if (lp->sep)
30810Sstevel@tonic-gate 		free(lp->sep);
30820Sstevel@tonic-gate 	if (lp->arg)
30830Sstevel@tonic-gate 		free(lp->arg);
30840Sstevel@tonic-gate 	if (lp->line)
30850Sstevel@tonic-gate 		free(lp->line);
30860Sstevel@tonic-gate 	free(lp);
30870Sstevel@tonic-gate }
30880Sstevel@tonic-gate 
30890Sstevel@tonic-gate static void
30900Sstevel@tonic-gate linelist_free(line_t *start)
30910Sstevel@tonic-gate {
30920Sstevel@tonic-gate 	line_t *lp;
30930Sstevel@tonic-gate 
30940Sstevel@tonic-gate 	while (start) {
30950Sstevel@tonic-gate 		lp = start;
30960Sstevel@tonic-gate 		start = start->next;
30970Sstevel@tonic-gate 		line_free(lp);
30980Sstevel@tonic-gate 	}
30990Sstevel@tonic-gate }
31000Sstevel@tonic-gate 
31010Sstevel@tonic-gate static void
31020Sstevel@tonic-gate filelist_free(filelist_t *flistp)
31030Sstevel@tonic-gate {
31040Sstevel@tonic-gate 	linelist_free(flistp->head);
31050Sstevel@tonic-gate 	flistp->head = NULL;
31060Sstevel@tonic-gate 	flistp->tail = NULL;
31070Sstevel@tonic-gate }
31080Sstevel@tonic-gate 
31090Sstevel@tonic-gate static void
31100Sstevel@tonic-gate menu_free(menu_t *mp)
31110Sstevel@tonic-gate {
31120Sstevel@tonic-gate 	assert(mp);
31130Sstevel@tonic-gate 
31140Sstevel@tonic-gate 	if (mp->start)
31150Sstevel@tonic-gate 		linelist_free(mp->start);
31160Sstevel@tonic-gate 	free(mp);
31170Sstevel@tonic-gate 
31180Sstevel@tonic-gate }
31190Sstevel@tonic-gate 
31200Sstevel@tonic-gate /*
31210Sstevel@tonic-gate  * Utility routines
31220Sstevel@tonic-gate  */
31230Sstevel@tonic-gate 
31240Sstevel@tonic-gate 
31250Sstevel@tonic-gate /*
31260Sstevel@tonic-gate  * Returns 0 on success
31270Sstevel@tonic-gate  * Any other value indicates an error
31280Sstevel@tonic-gate  */
31290Sstevel@tonic-gate static int
31300Sstevel@tonic-gate exec_cmd(char *cmdline, char *output, int64_t osize)
31310Sstevel@tonic-gate {
31320Sstevel@tonic-gate 	char buf[BUFSIZ];
31330Sstevel@tonic-gate 	int ret;
31340Sstevel@tonic-gate 	FILE *ptr;
31350Sstevel@tonic-gate 	size_t len;
31360Sstevel@tonic-gate 	sigset_t set;
31370Sstevel@tonic-gate 	void (*disp)(int);
31380Sstevel@tonic-gate 
31390Sstevel@tonic-gate 	/*
31400Sstevel@tonic-gate 	 * For security
31410Sstevel@tonic-gate 	 * - only absolute paths are allowed
31420Sstevel@tonic-gate 	 * - set IFS to space and tab
31430Sstevel@tonic-gate 	 */
31440Sstevel@tonic-gate 	if (*cmdline != '/') {
31450Sstevel@tonic-gate 		bam_error(ABS_PATH_REQ, cmdline);
31460Sstevel@tonic-gate 		return (-1);
31470Sstevel@tonic-gate 	}
31480Sstevel@tonic-gate 	(void) putenv("IFS= \t");
31490Sstevel@tonic-gate 
31500Sstevel@tonic-gate 	/*
31510Sstevel@tonic-gate 	 * We may have been exec'ed with SIGCHLD blocked
31520Sstevel@tonic-gate 	 * unblock it here
31530Sstevel@tonic-gate 	 */
31540Sstevel@tonic-gate 	(void) sigemptyset(&set);
31550Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
31560Sstevel@tonic-gate 	if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
31570Sstevel@tonic-gate 		bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno));
31580Sstevel@tonic-gate 		return (-1);
31590Sstevel@tonic-gate 	}
31600Sstevel@tonic-gate 
31610Sstevel@tonic-gate 	/*
31620Sstevel@tonic-gate 	 * Set SIGCHLD disposition to SIG_DFL for popen/pclose
31630Sstevel@tonic-gate 	 */
31640Sstevel@tonic-gate 	disp = sigset(SIGCHLD, SIG_DFL);
31650Sstevel@tonic-gate 	if (disp == SIG_ERR) {
31660Sstevel@tonic-gate 		bam_error(FAILED_SIG, strerror(errno));
31670Sstevel@tonic-gate 		return (-1);
31680Sstevel@tonic-gate 	}
31690Sstevel@tonic-gate 	if (disp == SIG_HOLD) {
31700Sstevel@tonic-gate 		bam_error(BLOCKED_SIG, cmdline);
31710Sstevel@tonic-gate 		return (-1);
31720Sstevel@tonic-gate 	}
31730Sstevel@tonic-gate 
31740Sstevel@tonic-gate 	ptr = popen(cmdline, "r");
31750Sstevel@tonic-gate 	if (ptr == NULL) {
31760Sstevel@tonic-gate 		bam_error(POPEN_FAIL, cmdline, strerror(errno));
31770Sstevel@tonic-gate 		return (-1);
31780Sstevel@tonic-gate 	}
31790Sstevel@tonic-gate 
31800Sstevel@tonic-gate 	/*
31810Sstevel@tonic-gate 	 * If we simply do a pclose() following a popen(), pclose()
31820Sstevel@tonic-gate 	 * will close the reader end of the pipe immediately even
31830Sstevel@tonic-gate 	 * if the child process has not started/exited. pclose()
31840Sstevel@tonic-gate 	 * does wait for cmd to terminate before returning though.
31850Sstevel@tonic-gate 	 * When the executed command writes its output to the pipe
31860Sstevel@tonic-gate 	 * there is no reader process and the command dies with
31870Sstevel@tonic-gate 	 * SIGPIPE. To avoid this we read repeatedly until read
31880Sstevel@tonic-gate 	 * terminates with EOF. This indicates that the command
31890Sstevel@tonic-gate 	 * (writer) has closed the pipe and we can safely do a
31900Sstevel@tonic-gate 	 * pclose().
31910Sstevel@tonic-gate 	 *
31920Sstevel@tonic-gate 	 * Since pclose() does wait for the command to exit,
31930Sstevel@tonic-gate 	 * we can safely reap the exit status of the command
31940Sstevel@tonic-gate 	 * from the value returned by pclose()
31950Sstevel@tonic-gate 	 */
31960Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), ptr) != NULL) {
31970Sstevel@tonic-gate 		/* if (bam_verbose)  XXX */
31980Sstevel@tonic-gate 			bam_print(PRINT_NO_NEWLINE, buf);
31990Sstevel@tonic-gate 		if (output && osize > 0) {
32000Sstevel@tonic-gate 			(void) snprintf(output, osize, "%s", buf);
32010Sstevel@tonic-gate 			len = strlen(buf);
32020Sstevel@tonic-gate 			output += len;
32030Sstevel@tonic-gate 			osize -= len;
32040Sstevel@tonic-gate 		}
32050Sstevel@tonic-gate 	}
32060Sstevel@tonic-gate 
32070Sstevel@tonic-gate 	ret = pclose(ptr);
32080Sstevel@tonic-gate 	if (ret == -1) {
32090Sstevel@tonic-gate 		bam_error(PCLOSE_FAIL, cmdline, strerror(errno));
32100Sstevel@tonic-gate 		return (-1);
32110Sstevel@tonic-gate 	}
32120Sstevel@tonic-gate 
32130Sstevel@tonic-gate 	if (WIFEXITED(ret)) {
32140Sstevel@tonic-gate 		return (WEXITSTATUS(ret));
32150Sstevel@tonic-gate 	} else {
32160Sstevel@tonic-gate 		bam_error(EXEC_FAIL, cmdline, ret);
32170Sstevel@tonic-gate 		return (-1);
32180Sstevel@tonic-gate 	}
32190Sstevel@tonic-gate }
32200Sstevel@tonic-gate 
32210Sstevel@tonic-gate /*
32220Sstevel@tonic-gate  * Since this function returns -1 on error
32230Sstevel@tonic-gate  * it cannot be used to convert -1. However,
32240Sstevel@tonic-gate  * that is sufficient for what we need.
32250Sstevel@tonic-gate  */
32260Sstevel@tonic-gate static long
32270Sstevel@tonic-gate s_strtol(char *str)
32280Sstevel@tonic-gate {
32290Sstevel@tonic-gate 	long l;
32300Sstevel@tonic-gate 	char *res = NULL;
32310Sstevel@tonic-gate 
32320Sstevel@tonic-gate 	if (str == NULL) {
32330Sstevel@tonic-gate 		return (-1);
32340Sstevel@tonic-gate 	}
32350Sstevel@tonic-gate 
32360Sstevel@tonic-gate 	errno = 0;
32370Sstevel@tonic-gate 	l = strtol(str, &res, 10);
32380Sstevel@tonic-gate 	if (errno || *res != '\0') {
32390Sstevel@tonic-gate 		return (-1);
32400Sstevel@tonic-gate 	}
32410Sstevel@tonic-gate 
32420Sstevel@tonic-gate 	return (l);
32430Sstevel@tonic-gate }
32440Sstevel@tonic-gate 
32450Sstevel@tonic-gate /*
32460Sstevel@tonic-gate  * Wrapper around fputs, that adds a newline (since fputs doesn't)
32470Sstevel@tonic-gate  */
32480Sstevel@tonic-gate static int
32490Sstevel@tonic-gate s_fputs(char *str, FILE *fp)
32500Sstevel@tonic-gate {
32510Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
32520Sstevel@tonic-gate 
32530Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s\n", str);
32540Sstevel@tonic-gate 	return (fputs(linebuf, fp));
32550Sstevel@tonic-gate }
32560Sstevel@tonic-gate 
32570Sstevel@tonic-gate /*
32580Sstevel@tonic-gate  * Wrapper around fgets, that strips newlines returned by fgets
32590Sstevel@tonic-gate  */
32600Sstevel@tonic-gate static char *
32610Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp)
32620Sstevel@tonic-gate {
32630Sstevel@tonic-gate 	int n;
32640Sstevel@tonic-gate 
32650Sstevel@tonic-gate 	buf = fgets(buf, buflen, fp);
32660Sstevel@tonic-gate 	if (buf) {
32670Sstevel@tonic-gate 		n = strlen(buf);
32680Sstevel@tonic-gate 		if (n == buflen - 1 && buf[n-1] != '\n')
32690Sstevel@tonic-gate 			bam_error(TOO_LONG, buflen - 1, buf);
32700Sstevel@tonic-gate 		buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1];
32710Sstevel@tonic-gate 	}
32720Sstevel@tonic-gate 
32730Sstevel@tonic-gate 	return (buf);
32740Sstevel@tonic-gate }
32750Sstevel@tonic-gate 
32760Sstevel@tonic-gate static void *
32770Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz)
32780Sstevel@tonic-gate {
32790Sstevel@tonic-gate 	void *ptr;
32800Sstevel@tonic-gate 
32810Sstevel@tonic-gate 	ptr = calloc(nelem, sz);
32820Sstevel@tonic-gate 	if (ptr == NULL) {
32830Sstevel@tonic-gate 		bam_error(NO_MEM, nelem*sz);
32840Sstevel@tonic-gate 		bam_exit(1);
32850Sstevel@tonic-gate 	}
32860Sstevel@tonic-gate 	return (ptr);
32870Sstevel@tonic-gate }
32880Sstevel@tonic-gate 
32890Sstevel@tonic-gate static char *
32900Sstevel@tonic-gate s_strdup(char *str)
32910Sstevel@tonic-gate {
32920Sstevel@tonic-gate 	char *ptr;
32930Sstevel@tonic-gate 
32940Sstevel@tonic-gate 	if (str == NULL)
32950Sstevel@tonic-gate 		return (NULL);
32960Sstevel@tonic-gate 
32970Sstevel@tonic-gate 	ptr = strdup(str);
32980Sstevel@tonic-gate 	if (ptr == NULL) {
32990Sstevel@tonic-gate 		bam_error(NO_MEM, strlen(str) + 1);
33000Sstevel@tonic-gate 		bam_exit(1);
33010Sstevel@tonic-gate 	}
33020Sstevel@tonic-gate 	return (ptr);
33030Sstevel@tonic-gate }
33040Sstevel@tonic-gate 
33050Sstevel@tonic-gate /*
33060Sstevel@tonic-gate  * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients)
33070Sstevel@tonic-gate  * Returns 0 otherwise
33080Sstevel@tonic-gate  */
33090Sstevel@tonic-gate static int
33100Sstevel@tonic-gate is_amd64(void)
33110Sstevel@tonic-gate {
33120Sstevel@tonic-gate 	static int amd64 = -1;
33130Sstevel@tonic-gate 	char isabuf[257];	/* from sysinfo(2) manpage */
33140Sstevel@tonic-gate 
33150Sstevel@tonic-gate 	if (amd64 != -1)
33160Sstevel@tonic-gate 		return (amd64);
33170Sstevel@tonic-gate 
33180Sstevel@tonic-gate 	if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 &&
33190Sstevel@tonic-gate 	    strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0)
33200Sstevel@tonic-gate 		amd64 = 1;
33210Sstevel@tonic-gate 	else if (strstr(isabuf, "i386") == NULL)
33220Sstevel@tonic-gate 		amd64 = 1;		/* diskless server */
33230Sstevel@tonic-gate 	else
33240Sstevel@tonic-gate 		amd64 = 0;
33250Sstevel@tonic-gate 
33260Sstevel@tonic-gate 	return (amd64);
33270Sstevel@tonic-gate }
33280Sstevel@tonic-gate 
33290Sstevel@tonic-gate static void
33300Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s)
33310Sstevel@tonic-gate {
33320Sstevel@tonic-gate 	line_t *lp;
33330Sstevel@tonic-gate 
33340Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
33350Sstevel@tonic-gate 	lp->line = s_strdup(s);
33360Sstevel@tonic-gate 	if (flistp->head == NULL)
33370Sstevel@tonic-gate 		flistp->head = lp;
33380Sstevel@tonic-gate 	else
33390Sstevel@tonic-gate 		flistp->tail->next = lp;
33400Sstevel@tonic-gate 	flistp->tail = lp;
33410Sstevel@tonic-gate }
3342