xref: /illumos-gate/usr/src/cmd/boot/bootadm/bootadm.c (revision 58091fd8689db902780a10667e0e8118a9454b8f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5fbac2b2bSvikram  * Common Development and Distribution License (the "License").
6fbac2b2bSvikram  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22fbac2b2bSvikram  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * bootadm(1M) is a new utility for managing bootability of
307c478bd9Sstevel@tonic-gate  * Solaris *Newboot* environments. It has two primary tasks:
317c478bd9Sstevel@tonic-gate  * 	- Allow end users to manage bootability of Newboot Solaris instances
327c478bd9Sstevel@tonic-gate  *	- Provide services to other subsystems in Solaris (primarily Install)
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /* Headers */
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <stdarg.h>
447c478bd9Sstevel@tonic-gate #include <limits.h>
457c478bd9Sstevel@tonic-gate #include <signal.h>
467c478bd9Sstevel@tonic-gate #include <sys/wait.h>
477c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
487c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
497c478bd9Sstevel@tonic-gate #include <libnvpair.h>
507c478bd9Sstevel@tonic-gate #include <ftw.h>
517c478bd9Sstevel@tonic-gate #include <fcntl.h>
527c478bd9Sstevel@tonic-gate #include <strings.h>
537c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
547c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
55*58091fd8Ssetje #include <sys/param.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include <pwd.h>
587c478bd9Sstevel@tonic-gate #include <grp.h>
597c478bd9Sstevel@tonic-gate #include <device_info.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #include <libintl.h>
627c478bd9Sstevel@tonic-gate #include <locale.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #include <assert.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include "message.h"
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
697c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
707c478bd9Sstevel@tonic-gate #endif	/* TEXT_DOMAIN */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /* Type definitions */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /* Primary subcmds */
757c478bd9Sstevel@tonic-gate typedef enum {
767c478bd9Sstevel@tonic-gate 	BAM_MENU = 3,
777c478bd9Sstevel@tonic-gate 	BAM_ARCHIVE
787c478bd9Sstevel@tonic-gate } subcmd_t;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /* GRUB menu per-line classification */
817c478bd9Sstevel@tonic-gate typedef enum {
827c478bd9Sstevel@tonic-gate 	BAM_INVALID = 0,
837c478bd9Sstevel@tonic-gate 	BAM_EMPTY,
847c478bd9Sstevel@tonic-gate 	BAM_COMMENT,
857c478bd9Sstevel@tonic-gate 	BAM_GLOBAL,
867c478bd9Sstevel@tonic-gate 	BAM_ENTRY,
877c478bd9Sstevel@tonic-gate 	BAM_TITLE
887c478bd9Sstevel@tonic-gate } menu_flag_t;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /* struct for menu.lst contents */
917c478bd9Sstevel@tonic-gate typedef struct line {
927c478bd9Sstevel@tonic-gate 	int  lineNum;	/* Line number in menu.lst */
937c478bd9Sstevel@tonic-gate 	int  entryNum;	/* menu boot entry #. ENTRY_INIT if not applicable */
947c478bd9Sstevel@tonic-gate 	char *cmd;
957c478bd9Sstevel@tonic-gate 	char *sep;
967c478bd9Sstevel@tonic-gate 	char *arg;
977c478bd9Sstevel@tonic-gate 	char *line;
987c478bd9Sstevel@tonic-gate 	menu_flag_t flags;
997c478bd9Sstevel@tonic-gate 	struct line *next;
1008c1b6884Sszhou 	struct line *prev;
1017c478bd9Sstevel@tonic-gate } line_t;
1027c478bd9Sstevel@tonic-gate 
1038c1b6884Sszhou typedef struct entry {
1048c1b6884Sszhou 	struct entry *next;
1058c1b6884Sszhou 	struct entry *prev;
1068c1b6884Sszhou 	line_t *start;
1078c1b6884Sszhou 	line_t *end;
1088c1b6884Sszhou } entry_t;
1098c1b6884Sszhou 
1107c478bd9Sstevel@tonic-gate typedef struct {
1117c478bd9Sstevel@tonic-gate 	line_t *start;
1127c478bd9Sstevel@tonic-gate 	line_t *end;
1138c1b6884Sszhou 	line_t *curdefault;	/* line containing default */
1148c1b6884Sszhou 	line_t *olddefault;	/* old default line (commented) */
1158c1b6884Sszhou 	entry_t *entries;	/* os entries */
1167c478bd9Sstevel@tonic-gate } menu_t;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate typedef enum {
1197c478bd9Sstevel@tonic-gate     OPT_ABSENT = 0,	/* No option */
1207c478bd9Sstevel@tonic-gate     OPT_REQ,		/* option required */
1217c478bd9Sstevel@tonic-gate     OPT_OPTIONAL	/* option may or may not be present */
1227c478bd9Sstevel@tonic-gate } option_t;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate typedef enum {
125b610f78eSvikram     BAM_ERROR = -1,	/* Must be negative. add_boot_entry() depends on it */
1267c478bd9Sstevel@tonic-gate     BAM_SUCCESS = 0,
1277c478bd9Sstevel@tonic-gate     BAM_WRITE = 2
1287c478bd9Sstevel@tonic-gate } error_t;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate typedef struct {
1317c478bd9Sstevel@tonic-gate 	char	*subcmd;
1327c478bd9Sstevel@tonic-gate 	option_t option;
1337c478bd9Sstevel@tonic-gate 	error_t (*handler)();
1341a97e40eSvikram 	int	unpriv;			/* is this an unprivileged command */
1357c478bd9Sstevel@tonic-gate } subcmd_defn_t;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate #define	BAM_MAXLINE	8192
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate #define	LINE_INIT	0	/* lineNum initial value */
1417c478bd9Sstevel@tonic-gate #define	ENTRY_INIT	-1	/* entryNum initial value */
1427c478bd9Sstevel@tonic-gate #define	ALL_ENTRIES	-2	/* selects all boot entries */
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate #define	GRUB_DIR		"/boot/grub"
1457c478bd9Sstevel@tonic-gate #define	MULTI_BOOT		"/platform/i86pc/multiboot"
1467c478bd9Sstevel@tonic-gate #define	BOOT_ARCHIVE		"/platform/i86pc/boot_archive"
1477c478bd9Sstevel@tonic-gate #define	GRUB_MENU		"/boot/grub/menu.lst"
1487c478bd9Sstevel@tonic-gate #define	MENU_TMP		"/boot/grub/menu.lst.tmp"
1497c478bd9Sstevel@tonic-gate #define	RAMDISK_SPECIAL		"/ramdisk"
15040541d5dSvikram #define	STUBBOOT		"/stubboot"
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /* lock related */
1537c478bd9Sstevel@tonic-gate #define	BAM_LOCK_FILE		"/var/run/bootadm.lock"
1547c478bd9Sstevel@tonic-gate #define	LOCK_FILE_PERMS		(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate #define	CREATE_RAMDISK		"/boot/solaris/bin/create_ramdisk"
1577c478bd9Sstevel@tonic-gate #define	CREATE_DISKMAP		"/boot/solaris/bin/create_diskmap"
1587c478bd9Sstevel@tonic-gate #define	GRUBDISK_MAP		"/var/run/solaris_grubdisk.map"
1597c478bd9Sstevel@tonic-gate 
160b610f78eSvikram #define	GRUB_slice		"/etc/lu/GRUB_slice"
161b610f78eSvikram #define	GRUB_root		"/etc/lu/GRUB_root"
162b610f78eSvikram #define	GRUB_backup_menu	"/etc/lu/GRUB_backup_menu"
163b610f78eSvikram #define	GRUB_slice_mntpt	"/tmp/GRUB_slice_mntpt"
164b610f78eSvikram #define	LU_ACTIVATE_FILE	"/etc/lu/DelayUpdate/activate.sh"
165fbac2b2bSvikram #define	GRUB_fdisk		"/etc/lu/GRUB_fdisk"
166fbac2b2bSvikram #define	GRUB_fdisk_target	"/etc/lu/GRUB_fdisk_target"
167b610f78eSvikram 
168b610f78eSvikram #define	INSTALLGRUB		"/sbin/installgrub"
169b610f78eSvikram #define	STAGE1			"/boot/grub/stage1"
170b610f78eSvikram #define	STAGE2			"/boot/grub/stage2"
171b610f78eSvikram 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  * Default file attributes
1747c478bd9Sstevel@tonic-gate  */
1757c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_MODE	0644	/* default permissions */
1767c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_UID		0	/* user root */
1777c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_GID		3	/* group sys */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate  * Menu related
1817c478bd9Sstevel@tonic-gate  * menu_cmd_t and menu_cmds must be kept in sync
1827c478bd9Sstevel@tonic-gate  */
1837c478bd9Sstevel@tonic-gate typedef enum {
1847c478bd9Sstevel@tonic-gate 	DEFAULT_CMD = 0,
1857c478bd9Sstevel@tonic-gate 	TIMEOUT_CMD,
1867c478bd9Sstevel@tonic-gate 	TITLE_CMD,
1877c478bd9Sstevel@tonic-gate 	ROOT_CMD,
1887c478bd9Sstevel@tonic-gate 	KERNEL_CMD,
1897c478bd9Sstevel@tonic-gate 	MODULE_CMD,
1907c478bd9Sstevel@tonic-gate 	SEP_CMD,
1917c478bd9Sstevel@tonic-gate 	COMMENT_CMD
1927c478bd9Sstevel@tonic-gate } menu_cmd_t;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate static char *menu_cmds[] = {
1957c478bd9Sstevel@tonic-gate 	"default",	/* DEFAULT_CMD */
1967c478bd9Sstevel@tonic-gate 	"timeout",	/* TIMEOUT_CMD */
1977c478bd9Sstevel@tonic-gate 	"title",	/* TITLE_CMD */
1987c478bd9Sstevel@tonic-gate 	"root",		/* ROOT_CMD */
1997c478bd9Sstevel@tonic-gate 	"kernel",	/* KERNEL_CMD */
2007c478bd9Sstevel@tonic-gate 	"module",	/* MODULE_CMD */
2017c478bd9Sstevel@tonic-gate 	" ",		/* SEP_CMD */
2027c478bd9Sstevel@tonic-gate 	"#",		/* COMMENT_CMD */
2037c478bd9Sstevel@tonic-gate 	NULL
2047c478bd9Sstevel@tonic-gate };
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate #define	OPT_ENTRY_NUM	"entry"
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * archive related
2107c478bd9Sstevel@tonic-gate  */
2117c478bd9Sstevel@tonic-gate typedef struct {
2127c478bd9Sstevel@tonic-gate 	line_t *head;
2137c478bd9Sstevel@tonic-gate 	line_t *tail;
2147c478bd9Sstevel@tonic-gate } filelist_t;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate #define	BOOT_FILE_LIST	"boot/solaris/filelist.ramdisk"
2177c478bd9Sstevel@tonic-gate #define	ETC_FILE_LIST	"etc/boot/solaris/filelist.ramdisk"
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate #define	FILE_STAT	"boot/solaris/filestat.ramdisk"
2207c478bd9Sstevel@tonic-gate #define	FILE_STAT_TMP	"boot/solaris/filestat.ramdisk.tmp"
2217c478bd9Sstevel@tonic-gate #define	DIR_PERMS	(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
2227c478bd9Sstevel@tonic-gate #define	FILE_STAT_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate #define	BAM_HDR		"---------- ADDED BY BOOTADM - DO NOT EDIT ----------"
2257c478bd9Sstevel@tonic-gate #define	BAM_FTR		"---------------------END BOOTADM--------------------"
2268c1b6884Sszhou #define	BAM_OLDDEF	"BOOTADM SAVED DEFAULT: "
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /* Globals */
2297c478bd9Sstevel@tonic-gate static char *prog;
2307c478bd9Sstevel@tonic-gate static subcmd_t bam_cmd;
2317c478bd9Sstevel@tonic-gate static char *bam_root;
2327c478bd9Sstevel@tonic-gate static int bam_rootlen;
2337c478bd9Sstevel@tonic-gate static int bam_root_readonly;
23440541d5dSvikram static int bam_alt_root;
2357c478bd9Sstevel@tonic-gate static char *bam_subcmd;
2367c478bd9Sstevel@tonic-gate static char *bam_opt;
2377c478bd9Sstevel@tonic-gate static int bam_debug;
2387c478bd9Sstevel@tonic-gate static char **bam_argv;
2397c478bd9Sstevel@tonic-gate static int bam_argc;
2407c478bd9Sstevel@tonic-gate static int bam_force;
2417c478bd9Sstevel@tonic-gate static int bam_verbose;
2427c478bd9Sstevel@tonic-gate static int bam_check;
2437c478bd9Sstevel@tonic-gate static int bam_smf_check;
2447c478bd9Sstevel@tonic-gate static int bam_lock_fd = -1;
2457c478bd9Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/";
246b610f78eSvikram static int bam_update_all;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate /* function prototypes */
2497c478bd9Sstevel@tonic-gate static void parse_args_internal(int argc, char *argv[]);
2507c478bd9Sstevel@tonic-gate static void parse_args(int argc, char *argv[]);
2517c478bd9Sstevel@tonic-gate static error_t bam_menu(char *subcmd, char *opt, int argc, char *argv[]);
2527c478bd9Sstevel@tonic-gate static error_t bam_archive(char *subcmd, char *opt);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate static void bam_error(char *format, ...);
2557c478bd9Sstevel@tonic-gate static void bam_print(char *format, ...);
2567c478bd9Sstevel@tonic-gate static void bam_exit(int excode);
2577c478bd9Sstevel@tonic-gate static void bam_lock(void);
2587c478bd9Sstevel@tonic-gate static void bam_unlock(void);
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate static int exec_cmd(char *cmdline, char *output, int64_t osize);
2617c478bd9Sstevel@tonic-gate static error_t read_globals(menu_t *mp, char *menu_path,
2627c478bd9Sstevel@tonic-gate     char *globalcmd, int quiet);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate static menu_t *menu_read(char *menu_path);
2657c478bd9Sstevel@tonic-gate static error_t menu_write(char *root, menu_t *mp);
2667c478bd9Sstevel@tonic-gate static void linelist_free(line_t *start);
2677c478bd9Sstevel@tonic-gate static void menu_free(menu_t *mp);
2687c478bd9Sstevel@tonic-gate static void line_free(line_t *lp);
2697c478bd9Sstevel@tonic-gate static void filelist_free(filelist_t *flistp);
2707c478bd9Sstevel@tonic-gate static error_t list2file(char *root, char *tmp,
2717c478bd9Sstevel@tonic-gate     char *final, line_t *start);
2727c478bd9Sstevel@tonic-gate static error_t list_entry(menu_t *mp, char *menu_path, char *opt);
2737c478bd9Sstevel@tonic-gate static error_t delete_all_entries(menu_t *mp, char *menu_path, char *opt);
2747c478bd9Sstevel@tonic-gate static error_t update_entry(menu_t *mp, char *root, char *opt);
2757c478bd9Sstevel@tonic-gate static error_t update_temp(menu_t *mp, char *root, char *opt);
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate static error_t update_archive(char *root, char *opt);
2787c478bd9Sstevel@tonic-gate static error_t list_archive(char *root, char *opt);
2797c478bd9Sstevel@tonic-gate static error_t update_all(char *root, char *opt);
2807c478bd9Sstevel@tonic-gate static error_t read_list(char *root, filelist_t  *flistp);
2817c478bd9Sstevel@tonic-gate static error_t set_global(menu_t *mp, char *globalcmd, int val);
2827c478bd9Sstevel@tonic-gate static error_t set_option(menu_t *mp, char *globalcmd, char *opt);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate static long s_strtol(char *str);
2857c478bd9Sstevel@tonic-gate static char *s_fgets(char *buf, int n, FILE *fp);
2867c478bd9Sstevel@tonic-gate static int s_fputs(char *str, FILE *fp);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate static void *s_calloc(size_t nelem, size_t sz);
2897c478bd9Sstevel@tonic-gate static char *s_strdup(char *str);
2907c478bd9Sstevel@tonic-gate static int is_readonly(char *);
2917c478bd9Sstevel@tonic-gate static int is_amd64(void);
2927c478bd9Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate #if defined(__sparc)
2957c478bd9Sstevel@tonic-gate static void sparc_abort(void);
2967c478bd9Sstevel@tonic-gate #endif
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate /* Menu related sub commands */
2997c478bd9Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = {
3001a97e40eSvikram 	"set_option",		OPT_OPTIONAL,	set_option, 0,	/* PUB */
3011a97e40eSvikram 	"list_entry",		OPT_OPTIONAL,	list_entry, 1,	/* PUB */
3021a97e40eSvikram 	"delete_all_entries",	OPT_ABSENT,	delete_all_entries, 0, /* PVT */
3031a97e40eSvikram 	"update_entry",		OPT_REQ,	update_entry, 0, /* menu */
3041a97e40eSvikram 	"update_temp",		OPT_OPTIONAL,	update_temp, 0,	/* reboot */
3051a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
3067c478bd9Sstevel@tonic-gate };
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /* Archive related sub commands */
3097c478bd9Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = {
3101a97e40eSvikram 	"update",		OPT_ABSENT,	update_archive, 0, /* PUB */
3111a97e40eSvikram 	"update_all",		OPT_ABSENT,	update_all, 0,	/* PVT */
3121a97e40eSvikram 	"list",			OPT_OPTIONAL,	list_archive, 1, /* PUB */
3131a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
3147c478bd9Sstevel@tonic-gate };
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate static struct {
3177c478bd9Sstevel@tonic-gate 	nvlist_t *new_nvlp;
3187c478bd9Sstevel@tonic-gate 	nvlist_t *old_nvlp;
3197c478bd9Sstevel@tonic-gate 	int need_update;
3207c478bd9Sstevel@tonic-gate } walk_arg;
3217c478bd9Sstevel@tonic-gate 
322*58091fd8Ssetje 
323*58091fd8Ssetje static struct safefile {
324*58091fd8Ssetje 	char *name;
325*58091fd8Ssetje 	struct safefile *next;
326*58091fd8Ssetje };
327*58091fd8Ssetje 
328*58091fd8Ssetje struct safefile *safefiles = NULL;
329*58091fd8Ssetje #define	NEED_UPDATE_FILE "/etc/svc/volatile/boot_archive_needs_update"
330*58091fd8Ssetje 
3317c478bd9Sstevel@tonic-gate static void
3327c478bd9Sstevel@tonic-gate usage(void)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "USAGE:\n");
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/* archive usage */
3387c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s update-archive [-vn] [-R altroot]\n",
3397c478bd9Sstevel@tonic-gate 	    prog);
3407c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-archive [-R altroot]\n", prog);
3417c478bd9Sstevel@tonic-gate #ifndef __sparc
3427c478bd9Sstevel@tonic-gate 	/* x86 only */
3437c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog);
3447c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog);
3457c478bd9Sstevel@tonic-gate #endif
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate int
3497c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate 	error_t ret;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3547c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
3577c478bd9Sstevel@tonic-gate 		prog = argv[0];
3587c478bd9Sstevel@tonic-gate 	} else {
3597c478bd9Sstevel@tonic-gate 		prog++;
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	/*
3647c478bd9Sstevel@tonic-gate 	 * Don't depend on caller's umask
3657c478bd9Sstevel@tonic-gate 	 */
3667c478bd9Sstevel@tonic-gate 	(void) umask(0022);
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate #if defined(__sparc)
3717c478bd9Sstevel@tonic-gate 	/*
3727c478bd9Sstevel@tonic-gate 	 * There are only two valid invocations of bootadm
3737c478bd9Sstevel@tonic-gate 	 * on SPARC:
3747c478bd9Sstevel@tonic-gate 	 *
3757c478bd9Sstevel@tonic-gate 	 *	- SPARC diskless server creating boot_archive for i386 clients
3767c478bd9Sstevel@tonic-gate 	 *	- archive creation call during reboot of a SPARC system
3777c478bd9Sstevel@tonic-gate 	 *
3787c478bd9Sstevel@tonic-gate 	 *	The latter should be a NOP
3797c478bd9Sstevel@tonic-gate 	 */
3807c478bd9Sstevel@tonic-gate 	if (bam_cmd != BAM_ARCHIVE) {
3817c478bd9Sstevel@tonic-gate 		sparc_abort();
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate #endif
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	switch (bam_cmd) {
3867c478bd9Sstevel@tonic-gate 		case BAM_MENU:
3877c478bd9Sstevel@tonic-gate 			ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv);
3887c478bd9Sstevel@tonic-gate 			break;
3897c478bd9Sstevel@tonic-gate 		case BAM_ARCHIVE:
3907c478bd9Sstevel@tonic-gate 			ret = bam_archive(bam_subcmd, bam_opt);
3917c478bd9Sstevel@tonic-gate 			break;
3927c478bd9Sstevel@tonic-gate 		default:
3937c478bd9Sstevel@tonic-gate 			usage();
3947c478bd9Sstevel@tonic-gate 			bam_exit(1);
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS)
3987c478bd9Sstevel@tonic-gate 		bam_exit(1);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	bam_unlock();
4017c478bd9Sstevel@tonic-gate 	return (0);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate #if defined(__sparc)
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate static void
4077c478bd9Sstevel@tonic-gate sparc_abort(void)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	bam_error(NOT_ON_SPARC);
4107c478bd9Sstevel@tonic-gate 	bam_exit(1);
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate #endif
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate  * Equivalence of public and internal commands:
4177c478bd9Sstevel@tonic-gate  *	update-archive  -- -a update
4187c478bd9Sstevel@tonic-gate  *	list-archive	-- -a list
4197c478bd9Sstevel@tonic-gate  *	set-menu	-- -m set_option
4207c478bd9Sstevel@tonic-gate  *	list-menu	-- -m list_entry
4217c478bd9Sstevel@tonic-gate  *	update-menu	-- -m update_entry
4227c478bd9Sstevel@tonic-gate  */
4237c478bd9Sstevel@tonic-gate static struct cmd_map {
4247c478bd9Sstevel@tonic-gate 	char *bam_cmdname;
4257c478bd9Sstevel@tonic-gate 	int bam_cmd;
4267c478bd9Sstevel@tonic-gate 	char *bam_subcmd;
4277c478bd9Sstevel@tonic-gate } cmd_map[] = {
4287c478bd9Sstevel@tonic-gate 	{ "update-archive",	BAM_ARCHIVE,	"update"},
4297c478bd9Sstevel@tonic-gate 	{ "list-archive",	BAM_ARCHIVE,	"list"},
4307c478bd9Sstevel@tonic-gate 	{ "set-menu",		BAM_MENU,	"set_option"},
4317c478bd9Sstevel@tonic-gate 	{ "list-menu",		BAM_MENU,	"list_entry"},
4327c478bd9Sstevel@tonic-gate 	{ "update-menu",	BAM_MENU,	"update_entry"},
4337c478bd9Sstevel@tonic-gate 	{ NULL,			0,		NULL}
4347c478bd9Sstevel@tonic-gate };
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate  * Commands syntax published in bootadm(1M) are parsed here
4387c478bd9Sstevel@tonic-gate  */
4397c478bd9Sstevel@tonic-gate static void
4407c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[])
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 	struct cmd_map *cmp = cmd_map;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	/* command conforming to the final spec */
4457c478bd9Sstevel@tonic-gate 	if (argc > 1 && argv[1][0] != '-') {
4467c478bd9Sstevel@tonic-gate 		/*
4477c478bd9Sstevel@tonic-gate 		 * Map commands to internal table.
4487c478bd9Sstevel@tonic-gate 		 */
4497c478bd9Sstevel@tonic-gate 		while (cmp->bam_cmdname) {
4507c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], cmp->bam_cmdname) == 0) {
4517c478bd9Sstevel@tonic-gate 				bam_cmd = cmp->bam_cmd;
4527c478bd9Sstevel@tonic-gate 				bam_subcmd = cmp->bam_subcmd;
4537c478bd9Sstevel@tonic-gate 				break;
4547c478bd9Sstevel@tonic-gate 			}
4557c478bd9Sstevel@tonic-gate 			cmp++;
4567c478bd9Sstevel@tonic-gate 		}
4577c478bd9Sstevel@tonic-gate 		if (cmp->bam_cmdname == NULL) {
4587c478bd9Sstevel@tonic-gate 			usage();
4597c478bd9Sstevel@tonic-gate 			bam_exit(1);
4607c478bd9Sstevel@tonic-gate 		}
4617c478bd9Sstevel@tonic-gate 		argc--;
4627c478bd9Sstevel@tonic-gate 		argv++;
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	parse_args_internal(argc, argv);
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate /*
4697c478bd9Sstevel@tonic-gate  * A combination of public and private commands are parsed here.
4707c478bd9Sstevel@tonic-gate  * The internal syntax and the corresponding functionality are:
4717c478bd9Sstevel@tonic-gate  *	-a update	-- update-archive
4727c478bd9Sstevel@tonic-gate  *	-a list		-- list-archive
4737c478bd9Sstevel@tonic-gate  *	-a update-all	-- (reboot to sync all mounted OS archive)
4747c478bd9Sstevel@tonic-gate  *	-m update_entry	-- update-menu
4757c478bd9Sstevel@tonic-gate  *	-m list_entry	-- list-menu
4767c478bd9Sstevel@tonic-gate  *	-m update_temp	-- (reboot -- [boot-args])
4777c478bd9Sstevel@tonic-gate  *	-m delete_all_entries -- (called from install)
4787c478bd9Sstevel@tonic-gate  */
4797c478bd9Sstevel@tonic-gate static void
4807c478bd9Sstevel@tonic-gate parse_args_internal(int argc, char *argv[])
4817c478bd9Sstevel@tonic-gate {
4827c478bd9Sstevel@tonic-gate 	int c, error;
4837c478bd9Sstevel@tonic-gate 	extern char *optarg;
4847c478bd9Sstevel@tonic-gate 	extern int optind, opterr;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	/* Suppress error message from getopt */
4877c478bd9Sstevel@tonic-gate 	opterr = 0;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	error = 0;
4908c1b6884Sszhou 	while ((c = getopt(argc, argv, "a:d:fm:no:vCR:")) != -1) {
4917c478bd9Sstevel@tonic-gate 		switch (c) {
4927c478bd9Sstevel@tonic-gate 		case 'a':
4937c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
4947c478bd9Sstevel@tonic-gate 				error = 1;
4957c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
4967c478bd9Sstevel@tonic-gate 			}
4977c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_ARCHIVE;
4987c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
4997c478bd9Sstevel@tonic-gate 			break;
5007c478bd9Sstevel@tonic-gate 		case 'd':
5017c478bd9Sstevel@tonic-gate 			if (bam_debug) {
5027c478bd9Sstevel@tonic-gate 				error = 1;
5037c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5047c478bd9Sstevel@tonic-gate 			}
5057c478bd9Sstevel@tonic-gate 			bam_debug = s_strtol(optarg);
5067c478bd9Sstevel@tonic-gate 			break;
5077c478bd9Sstevel@tonic-gate 		case 'f':
5087c478bd9Sstevel@tonic-gate 			if (bam_force) {
5097c478bd9Sstevel@tonic-gate 				error = 1;
5107c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5117c478bd9Sstevel@tonic-gate 			}
5127c478bd9Sstevel@tonic-gate 			bam_force = 1;
5137c478bd9Sstevel@tonic-gate 			break;
5147c478bd9Sstevel@tonic-gate 		case 'm':
5157c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
5167c478bd9Sstevel@tonic-gate 				error = 1;
5177c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
5187c478bd9Sstevel@tonic-gate 			}
5197c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_MENU;
5207c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
5217c478bd9Sstevel@tonic-gate 			break;
5227c478bd9Sstevel@tonic-gate 		case 'n':
5237c478bd9Sstevel@tonic-gate 			if (bam_check) {
5247c478bd9Sstevel@tonic-gate 				error = 1;
5257c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5267c478bd9Sstevel@tonic-gate 			}
5277c478bd9Sstevel@tonic-gate 			bam_check = 1;
5287c478bd9Sstevel@tonic-gate 			break;
5297c478bd9Sstevel@tonic-gate 		case 'o':
5307c478bd9Sstevel@tonic-gate 			if (bam_opt) {
5317c478bd9Sstevel@tonic-gate 				error = 1;
5327c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5337c478bd9Sstevel@tonic-gate 			}
5347c478bd9Sstevel@tonic-gate 			bam_opt = optarg;
5357c478bd9Sstevel@tonic-gate 			break;
5367c478bd9Sstevel@tonic-gate 		case 'v':
5377c478bd9Sstevel@tonic-gate 			if (bam_verbose) {
5387c478bd9Sstevel@tonic-gate 				error = 1;
5397c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5407c478bd9Sstevel@tonic-gate 			}
5417c478bd9Sstevel@tonic-gate 			bam_verbose = 1;
5427c478bd9Sstevel@tonic-gate 			break;
5438c1b6884Sszhou 		case 'C':
5448c1b6884Sszhou 			bam_smf_check = 1;
5458c1b6884Sszhou 			break;
5467c478bd9Sstevel@tonic-gate 		case 'R':
5477c478bd9Sstevel@tonic-gate 			if (bam_root) {
5487c478bd9Sstevel@tonic-gate 				error = 1;
5497c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5507c478bd9Sstevel@tonic-gate 				break;
5517c478bd9Sstevel@tonic-gate 			} else if (realpath(optarg, rootbuf) == NULL) {
5527c478bd9Sstevel@tonic-gate 				error = 1;
5537c478bd9Sstevel@tonic-gate 				bam_error(CANT_RESOLVE, optarg,
5547c478bd9Sstevel@tonic-gate 				    strerror(errno));
5557c478bd9Sstevel@tonic-gate 				break;
5567c478bd9Sstevel@tonic-gate 			}
55740541d5dSvikram 			bam_alt_root = 1;
5587c478bd9Sstevel@tonic-gate 			bam_root = rootbuf;
5597c478bd9Sstevel@tonic-gate 			bam_rootlen = strlen(rootbuf);
5607c478bd9Sstevel@tonic-gate 			break;
5617c478bd9Sstevel@tonic-gate 		case '?':
5627c478bd9Sstevel@tonic-gate 			error = 1;
5637c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, optopt);
5647c478bd9Sstevel@tonic-gate 			break;
5657c478bd9Sstevel@tonic-gate 		default :
5667c478bd9Sstevel@tonic-gate 			error = 1;
5677c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, c);
5687c478bd9Sstevel@tonic-gate 			break;
5697c478bd9Sstevel@tonic-gate 		}
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	/*
5737c478bd9Sstevel@tonic-gate 	 * A command option must be specfied
5747c478bd9Sstevel@tonic-gate 	 */
5757c478bd9Sstevel@tonic-gate 	if (!bam_cmd) {
5767c478bd9Sstevel@tonic-gate 		if (bam_opt && strcmp(bam_opt, "all") == 0) {
5777c478bd9Sstevel@tonic-gate 			usage();
5787c478bd9Sstevel@tonic-gate 			bam_exit(0);
5797c478bd9Sstevel@tonic-gate 		}
5807c478bd9Sstevel@tonic-gate 		bam_error(NEED_CMD);
5817c478bd9Sstevel@tonic-gate 		error = 1;
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	if (error) {
5857c478bd9Sstevel@tonic-gate 		usage();
5867c478bd9Sstevel@tonic-gate 		bam_exit(1);
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	if (optind > argc) {
5907c478bd9Sstevel@tonic-gate 		bam_error(INT_ERROR, "parse_args");
5917c478bd9Sstevel@tonic-gate 		bam_exit(1);
5927c478bd9Sstevel@tonic-gate 	} else if (optind < argc) {
5937c478bd9Sstevel@tonic-gate 		bam_argv = &argv[optind];
5947c478bd9Sstevel@tonic-gate 		bam_argc = argc - optind;
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	/*
5987c478bd9Sstevel@tonic-gate 	 * -n implies verbose mode
5997c478bd9Sstevel@tonic-gate 	 */
6007c478bd9Sstevel@tonic-gate 	if (bam_check)
6017c478bd9Sstevel@tonic-gate 		bam_verbose = 1;
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate static error_t
6057c478bd9Sstevel@tonic-gate check_subcmd_and_options(
6067c478bd9Sstevel@tonic-gate 	char *subcmd,
6077c478bd9Sstevel@tonic-gate 	char *opt,
6087c478bd9Sstevel@tonic-gate 	subcmd_defn_t *table,
6097c478bd9Sstevel@tonic-gate 	error_t (**fp)())
6107c478bd9Sstevel@tonic-gate {
6117c478bd9Sstevel@tonic-gate 	int i;
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	if (subcmd == NULL) {
6147c478bd9Sstevel@tonic-gate 		bam_error(NEED_SUBCMD);
6157c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 
6181a97e40eSvikram 	if (bam_argc != 0 || bam_argv) {
6191a97e40eSvikram 		if (strcmp(subcmd, "set_option") != 0 || bam_argc != 1) {
6201a97e40eSvikram 			bam_error(TRAILING_ARGS);
6211a97e40eSvikram 			usage();
6221a97e40eSvikram 			return (BAM_ERROR);
6231a97e40eSvikram 		}
6241a97e40eSvikram 	}
6251a97e40eSvikram 
6267c478bd9Sstevel@tonic-gate 	if (bam_root == NULL) {
6277c478bd9Sstevel@tonic-gate 		bam_root = rootbuf;
6287c478bd9Sstevel@tonic-gate 		bam_rootlen = 1;
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	/* verify that subcmd is valid */
6327c478bd9Sstevel@tonic-gate 	for (i = 0; table[i].subcmd != NULL; i++) {
6337c478bd9Sstevel@tonic-gate 		if (strcmp(table[i].subcmd, subcmd) == 0)
6347c478bd9Sstevel@tonic-gate 			break;
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	if (table[i].subcmd == NULL) {
6387c478bd9Sstevel@tonic-gate 		bam_error(INVALID_SUBCMD, subcmd);
6397c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 
6421a97e40eSvikram 	if (table[i].unpriv == 0 && geteuid() != 0) {
6431a97e40eSvikram 		bam_error(MUST_BE_ROOT);
6441a97e40eSvikram 		return (BAM_ERROR);
6451a97e40eSvikram 	}
6461a97e40eSvikram 
6471a97e40eSvikram 	/*
6481a97e40eSvikram 	 * Currently only privileged commands need a lock
6491a97e40eSvikram 	 */
6501a97e40eSvikram 	if (table[i].unpriv == 0)
6511a97e40eSvikram 		bam_lock();
6521a97e40eSvikram 
6537c478bd9Sstevel@tonic-gate 	/* subcmd verifies that opt is appropriate */
6547c478bd9Sstevel@tonic-gate 	if (table[i].option != OPT_OPTIONAL) {
6557c478bd9Sstevel@tonic-gate 		if ((table[i].option == OPT_REQ) ^ (opt != NULL)) {
6567c478bd9Sstevel@tonic-gate 			if (opt)
6577c478bd9Sstevel@tonic-gate 				bam_error(NO_OPT_REQ, subcmd);
6587c478bd9Sstevel@tonic-gate 			else
6597c478bd9Sstevel@tonic-gate 				bam_error(MISS_OPT, subcmd);
6607c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
6617c478bd9Sstevel@tonic-gate 		}
6627c478bd9Sstevel@tonic-gate 	}
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	*fp = table[i].handler;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate 
669b610f78eSvikram 
670b610f78eSvikram static char *
67140541d5dSvikram mount_grub_slice(int *mnted, char **physlice, char **logslice, char **fs_type)
672b610f78eSvikram {
673b610f78eSvikram 	struct extmnttab mnt;
674b610f78eSvikram 	struct stat sb;
675b610f78eSvikram 	char buf[BAM_MAXLINE], dev[PATH_MAX], phys[PATH_MAX], fstype[32];
676f0f2c544Svikram 	char cmd[PATH_MAX];
677b610f78eSvikram 	char *mntpt;
678b610f78eSvikram 	int p, l, f;
679b610f78eSvikram 	FILE *fp;
680b610f78eSvikram 
681b610f78eSvikram 	assert(mnted);
682b610f78eSvikram 	*mnted = 0;
683b610f78eSvikram 
684b610f78eSvikram 	/*
68540541d5dSvikram 	 * physlice, logslice, fs_type  args may be NULL
686b610f78eSvikram 	 */
687b610f78eSvikram 	if (physlice)
688b610f78eSvikram 		*physlice = NULL;
68940541d5dSvikram 	if (logslice)
69040541d5dSvikram 		*logslice = NULL;
69140541d5dSvikram 	if (fs_type)
69240541d5dSvikram 		*fs_type = NULL;
693b610f78eSvikram 
694b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
695b610f78eSvikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
696b610f78eSvikram 		return (NULL);
697b610f78eSvikram 	}
698b610f78eSvikram 
699b610f78eSvikram 	fp = fopen(GRUB_slice, "r");
700b610f78eSvikram 	if (fp == NULL) {
701b610f78eSvikram 		bam_error(OPEN_FAIL, GRUB_slice, strerror(errno));
702b610f78eSvikram 		return (NULL);
703b610f78eSvikram 	}
704b610f78eSvikram 
705b610f78eSvikram 	dev[0] = fstype[0] = phys[0] = '\0';
706b610f78eSvikram 	p = sizeof ("PHYS_SLICE=") - 1;
707b610f78eSvikram 	l = sizeof ("LOG_SLICE=") - 1;
708b610f78eSvikram 	f = sizeof ("LOG_FSTYP=") - 1;
709b610f78eSvikram 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
710b610f78eSvikram 		if (strncmp(buf, "PHYS_SLICE=", p) == 0) {
711b610f78eSvikram 			(void) strlcpy(phys, buf + p, sizeof (phys));
712b610f78eSvikram 			continue;
713b610f78eSvikram 		}
714b610f78eSvikram 		if (strncmp(buf, "LOG_SLICE=", l) == 0) {
715b610f78eSvikram 			(void) strlcpy(dev, buf + l, sizeof (dev));
716b610f78eSvikram 			continue;
717b610f78eSvikram 		}
718b610f78eSvikram 		if (strncmp(buf, "LOG_FSTYP=", f) == 0) {
719b610f78eSvikram 			(void) strlcpy(fstype, buf + f, sizeof (fstype));
720b610f78eSvikram 			continue;
721b610f78eSvikram 		}
722b610f78eSvikram 	}
723b610f78eSvikram 	(void) fclose(fp);
724b610f78eSvikram 
725b610f78eSvikram 	if (dev[0] == '\0' || fstype[0] == '\0' || phys[0] == '\0') {
726b610f78eSvikram 		bam_error(BAD_SLICE_FILE, GRUB_slice);
727b610f78eSvikram 		return (NULL);
728b610f78eSvikram 	}
729b610f78eSvikram 
730b610f78eSvikram 	if (physlice) {
731b610f78eSvikram 		*physlice = s_strdup(phys);
732b610f78eSvikram 	}
73340541d5dSvikram 	if (logslice) {
73440541d5dSvikram 		*logslice = s_strdup(dev);
73540541d5dSvikram 	}
73640541d5dSvikram 	if (fs_type) {
73740541d5dSvikram 		*fs_type = s_strdup(fstype);
73840541d5dSvikram 	}
739b610f78eSvikram 
740b610f78eSvikram 	/*
741b610f78eSvikram 	 * Check if the slice is already mounted
742b610f78eSvikram 	 */
743b610f78eSvikram 	fp = fopen(MNTTAB, "r");
744b610f78eSvikram 	if (fp == NULL) {
745b610f78eSvikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
74640541d5dSvikram 		goto error;
747b610f78eSvikram 	}
748b610f78eSvikram 
749b610f78eSvikram 	resetmnttab(fp);
750b610f78eSvikram 
751b610f78eSvikram 	mntpt = NULL;
752b610f78eSvikram 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
753b610f78eSvikram 		if (strcmp(mnt.mnt_special, dev) == 0) {
754b610f78eSvikram 			mntpt = s_strdup(mnt.mnt_mountp);
755b610f78eSvikram 			break;
756b610f78eSvikram 		}
757b610f78eSvikram 	}
758b610f78eSvikram 
759b610f78eSvikram 	(void) fclose(fp);
760b610f78eSvikram 
761b610f78eSvikram 	if (mntpt) {
762b610f78eSvikram 		return (mntpt);
763b610f78eSvikram 	}
764b610f78eSvikram 
765b610f78eSvikram 
766b610f78eSvikram 	/*
767b610f78eSvikram 	 * GRUB slice is not mounted, we need to mount it now.
768b610f78eSvikram 	 * First create the mountpoint
769b610f78eSvikram 	 */
770b610f78eSvikram 	mntpt = s_calloc(1, PATH_MAX);
771b610f78eSvikram 	(void) snprintf(mntpt, PATH_MAX, "%s.%d", GRUB_slice_mntpt, getpid());
772b610f78eSvikram 	if (mkdir(mntpt, 0755) == -1 && errno != EEXIST) {
773b610f78eSvikram 		bam_error(MKDIR_FAILED, mntpt, strerror(errno));
774b610f78eSvikram 		free(mntpt);
77540541d5dSvikram 		goto error;
776b610f78eSvikram 	}
777b610f78eSvikram 
778f0f2c544Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/mount -F %s %s %s",
779f0f2c544Svikram 	    fstype, dev, mntpt);
780f0f2c544Svikram 
781f0f2c544Svikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
78240541d5dSvikram 		bam_error(MOUNT_FAILED, dev, fstype);
783b610f78eSvikram 		if (rmdir(mntpt) != 0) {
784b610f78eSvikram 			bam_error(RMDIR_FAILED, mntpt, strerror(errno));
785b610f78eSvikram 		}
786b610f78eSvikram 		free(mntpt);
78740541d5dSvikram 		goto error;
788b610f78eSvikram 	}
789b610f78eSvikram 
790b610f78eSvikram 	*mnted = 1;
791b610f78eSvikram 	return (mntpt);
79240541d5dSvikram 
79340541d5dSvikram error:
79440541d5dSvikram 	if (physlice) {
79540541d5dSvikram 		free(*physlice);
79640541d5dSvikram 		*physlice = NULL;
79740541d5dSvikram 	}
79840541d5dSvikram 	if (logslice) {
79940541d5dSvikram 		free(*logslice);
80040541d5dSvikram 		*logslice = NULL;
80140541d5dSvikram 	}
80240541d5dSvikram 	if (fs_type) {
80340541d5dSvikram 		free(*fs_type);
80440541d5dSvikram 		*fs_type = NULL;
80540541d5dSvikram 	}
80640541d5dSvikram 	return (NULL);
807b610f78eSvikram }
808b610f78eSvikram 
809b610f78eSvikram static void
81040541d5dSvikram umount_grub_slice(
81140541d5dSvikram 	int mnted,
81240541d5dSvikram 	char *mntpt,
81340541d5dSvikram 	char *physlice,
81440541d5dSvikram 	char *logslice,
81540541d5dSvikram 	char *fs_type)
816b610f78eSvikram {
817f0f2c544Svikram 	char cmd[PATH_MAX];
818f0f2c544Svikram 
819b610f78eSvikram 	/*
820b610f78eSvikram 	 * If we have not dealt with GRUB slice
821b610f78eSvikram 	 * we have nothing to do - just return.
822b610f78eSvikram 	 */
823b610f78eSvikram 	if (mntpt == NULL)
824b610f78eSvikram 		return;
825b610f78eSvikram 
826b610f78eSvikram 
827b610f78eSvikram 	/*
828b610f78eSvikram 	 * If we mounted the filesystem earlier in mount_grub_slice()
829b610f78eSvikram 	 * unmount it now.
830b610f78eSvikram 	 */
831b610f78eSvikram 	if (mnted) {
832f0f2c544Svikram 		(void) snprintf(cmd, sizeof (cmd), "/sbin/umount %s",
833f0f2c544Svikram 		    mntpt);
834f0f2c544Svikram 		if (exec_cmd(cmd, NULL, 0) != 0) {
835f0f2c544Svikram 			bam_error(UMOUNT_FAILED, mntpt);
836b610f78eSvikram 		}
837b610f78eSvikram 		if (rmdir(mntpt) != 0) {
838b610f78eSvikram 			bam_error(RMDIR_FAILED, mntpt, strerror(errno));
839b610f78eSvikram 		}
840b610f78eSvikram 	}
84140541d5dSvikram 
842b610f78eSvikram 	if (physlice)
843b610f78eSvikram 		free(physlice);
84440541d5dSvikram 	if (logslice)
84540541d5dSvikram 		free(logslice);
84640541d5dSvikram 	if (fs_type)
84740541d5dSvikram 		free(fs_type);
84840541d5dSvikram 
849b610f78eSvikram 	free(mntpt);
850b610f78eSvikram }
851b610f78eSvikram 
85240541d5dSvikram static char *
85340541d5dSvikram use_stubboot(void)
85440541d5dSvikram {
85540541d5dSvikram 	int mnted;
85640541d5dSvikram 	struct stat sb;
85740541d5dSvikram 	struct extmnttab mnt;
85840541d5dSvikram 	FILE *fp;
85940541d5dSvikram 	char cmd[PATH_MAX];
86040541d5dSvikram 
86140541d5dSvikram 	if (stat(STUBBOOT, &sb) != 0) {
86240541d5dSvikram 		bam_error(STUBBOOT_DIR_NOT_FOUND);
86340541d5dSvikram 		return (NULL);
86440541d5dSvikram 	}
86540541d5dSvikram 
86640541d5dSvikram 	/*
86740541d5dSvikram 	 * Check if stubboot is mounted. If not, mount it
86840541d5dSvikram 	 */
86940541d5dSvikram 	fp = fopen(MNTTAB, "r");
87040541d5dSvikram 	if (fp == NULL) {
87140541d5dSvikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
87240541d5dSvikram 		return (NULL);
87340541d5dSvikram 	}
87440541d5dSvikram 
87540541d5dSvikram 	resetmnttab(fp);
87640541d5dSvikram 
87740541d5dSvikram 	mnted = 0;
87840541d5dSvikram 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
87940541d5dSvikram 		if (strcmp(mnt.mnt_mountp, STUBBOOT) == 0) {
88040541d5dSvikram 			mnted = 1;
88140541d5dSvikram 			break;
88240541d5dSvikram 		}
88340541d5dSvikram 	}
88440541d5dSvikram 
88540541d5dSvikram 	(void) fclose(fp);
88640541d5dSvikram 
88740541d5dSvikram 	if (mnted)
88840541d5dSvikram 		return (STUBBOOT);
88940541d5dSvikram 
89040541d5dSvikram 	/*
89140541d5dSvikram 	 * Stubboot is not mounted, mount it now.
89240541d5dSvikram 	 * It should exist in /etc/vfstab
89340541d5dSvikram 	 */
89440541d5dSvikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/mount %s",
89540541d5dSvikram 	    STUBBOOT);
89640541d5dSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
89740541d5dSvikram 		bam_error(MOUNT_MNTPT_FAILED, STUBBOOT);
89840541d5dSvikram 		return (NULL);
89940541d5dSvikram 	}
90040541d5dSvikram 
90140541d5dSvikram 	return (STUBBOOT);
90240541d5dSvikram }
90340541d5dSvikram 
90440541d5dSvikram static void
90540541d5dSvikram disp_active_menu_locn(char *menu_path, char *logslice, char *fstype, int mnted)
90640541d5dSvikram {
90740541d5dSvikram 	/*
90840541d5dSvikram 	 * Check if we did a temp mount of an unmounted device.
90940541d5dSvikram 	 * If yes, print the block device and fstype for that device
91040541d5dSvikram 	 * else it is already mounted, so we print the path to the GRUB menu.
91140541d5dSvikram 	 */
91240541d5dSvikram 	if (mnted) {
91340541d5dSvikram 		bam_print(GRUB_MENU_DEVICE, logslice);
91440541d5dSvikram 		bam_print(GRUB_MENU_FSTYPE, fstype);
91540541d5dSvikram 	} else {
91640541d5dSvikram 		bam_print(GRUB_MENU_PATH, menu_path);
91740541d5dSvikram 	}
91840541d5dSvikram }
91940541d5dSvikram 
92040541d5dSvikram /*
92140541d5dSvikram  * NOTE: A single "/" is also considered a trailing slash and will
92240541d5dSvikram  * be deleted.
92340541d5dSvikram  */
92440541d5dSvikram static void
92540541d5dSvikram elide_trailing_slash(const char *src, char *dst, size_t dstsize)
92640541d5dSvikram {
92740541d5dSvikram 	size_t dstlen;
92840541d5dSvikram 
92940541d5dSvikram 	assert(src);
93040541d5dSvikram 	assert(dst);
93140541d5dSvikram 
93240541d5dSvikram 	(void) strlcpy(dst, src, dstsize);
93340541d5dSvikram 
93440541d5dSvikram 	dstlen = strlen(dst);
93540541d5dSvikram 	if (dst[dstlen - 1] == '/') {
93640541d5dSvikram 		dst[dstlen - 1] = '\0';
93740541d5dSvikram 	}
93840541d5dSvikram }
93940541d5dSvikram 
9407c478bd9Sstevel@tonic-gate static error_t
9417c478bd9Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[])
9427c478bd9Sstevel@tonic-gate {
9437c478bd9Sstevel@tonic-gate 	error_t ret;
9447c478bd9Sstevel@tonic-gate 	char menu_path[PATH_MAX];
9457c478bd9Sstevel@tonic-gate 	menu_t *menu;
94640541d5dSvikram 	char *mntpt, *menu_root, *logslice, *fstype;
947b610f78eSvikram 	struct stat sb;
948b610f78eSvikram 	int mnted;	/* set if we did a mount */
9497c478bd9Sstevel@tonic-gate 	error_t (*f)(menu_t *mp, char *menu_path, char *opt);
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	/*
9527c478bd9Sstevel@tonic-gate 	 * Check arguments
9537c478bd9Sstevel@tonic-gate 	 */
9547c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
9557c478bd9Sstevel@tonic-gate 	if (ret == BAM_ERROR) {
9567c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate 
959b610f78eSvikram 	mntpt = NULL;
960b610f78eSvikram 	mnted = 0;
96140541d5dSvikram 	logslice = fstype = NULL;
96240541d5dSvikram 
96340541d5dSvikram 	/*
96440541d5dSvikram 	 * If the user provides an alternate root, we
96540541d5dSvikram 	 * assume they know what they are doing and we
96640541d5dSvikram 	 * use it. Else we check if there is an
96740541d5dSvikram 	 * alternate location (other than /boot/grub)
96840541d5dSvikram 	 * for the GRUB menu
96940541d5dSvikram 	 */
97040541d5dSvikram 	if (bam_alt_root) {
97140541d5dSvikram 		menu_root = bam_root;
97240541d5dSvikram 	} else if (stat(GRUB_slice, &sb) == 0) {
97340541d5dSvikram 		mntpt = mount_grub_slice(&mnted, NULL, &logslice, &fstype);
974b610f78eSvikram 		menu_root = mntpt;
97540541d5dSvikram 	} else if (stat(STUBBOOT, &sb) == 0) {
97640541d5dSvikram 		menu_root = use_stubboot();
977b610f78eSvikram 	} else {
978b610f78eSvikram 		menu_root = bam_root;
979b610f78eSvikram 	}
980b610f78eSvikram 
98140541d5dSvikram 	if (menu_root == NULL) {
98240541d5dSvikram 		bam_error(CANNOT_LOCATE_GRUB_MENU);
98340541d5dSvikram 		return (BAM_ERROR);
98440541d5dSvikram 	}
98540541d5dSvikram 
98640541d5dSvikram 	elide_trailing_slash(menu_root, menu_path, sizeof (menu_path));
98740541d5dSvikram 	(void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path));
98840541d5dSvikram 
98940541d5dSvikram 	/*
99040541d5dSvikram 	 * If listing the menu, display the active menu
99140541d5dSvikram 	 * location
99240541d5dSvikram 	 */
99340541d5dSvikram 	if (strcmp(subcmd, "list_entry") == 0) {
99440541d5dSvikram 		disp_active_menu_locn(menu_path, logslice, fstype, mnted);
99540541d5dSvikram 	}
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	menu = menu_read(menu_path);
9987c478bd9Sstevel@tonic-gate 	assert(menu);
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	/*
10017c478bd9Sstevel@tonic-gate 	 * Special handling for setting timeout and default
10027c478bd9Sstevel@tonic-gate 	 */
10037c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "set_option") == 0) {
10047c478bd9Sstevel@tonic-gate 		if (largc != 1 || largv[0] == NULL) {
10057c478bd9Sstevel@tonic-gate 			usage();
100640541d5dSvikram 			menu_free(menu);
100740541d5dSvikram 			umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
10087c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
10097c478bd9Sstevel@tonic-gate 		}
10107c478bd9Sstevel@tonic-gate 		opt = largv[0];
10117c478bd9Sstevel@tonic-gate 	} else if (largc != 0) {
10127c478bd9Sstevel@tonic-gate 		usage();
101340541d5dSvikram 		menu_free(menu);
101440541d5dSvikram 		umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
10157c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
10167c478bd9Sstevel@tonic-gate 	}
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	/*
10197c478bd9Sstevel@tonic-gate 	 * Once the sub-cmd handler has run
10207c478bd9Sstevel@tonic-gate 	 * only the line field is guaranteed to have valid values
10217c478bd9Sstevel@tonic-gate 	 */
10227c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "update_entry") == 0)
10237c478bd9Sstevel@tonic-gate 		ret = f(menu, bam_root, opt);
10247c478bd9Sstevel@tonic-gate 	else
10257c478bd9Sstevel@tonic-gate 		ret = f(menu, menu_path, opt);
10267c478bd9Sstevel@tonic-gate 	if (ret == BAM_WRITE) {
1027b610f78eSvikram 		ret = menu_write(menu_root, menu);
10287c478bd9Sstevel@tonic-gate 	}
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	menu_free(menu);
10317c478bd9Sstevel@tonic-gate 
103240541d5dSvikram 	umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
1033b610f78eSvikram 
10347c478bd9Sstevel@tonic-gate 	return (ret);
10357c478bd9Sstevel@tonic-gate }
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate static error_t
10397c478bd9Sstevel@tonic-gate bam_archive(
10407c478bd9Sstevel@tonic-gate 	char *subcmd,
10417c478bd9Sstevel@tonic-gate 	char *opt)
10427c478bd9Sstevel@tonic-gate {
10437c478bd9Sstevel@tonic-gate 	error_t ret;
10447c478bd9Sstevel@tonic-gate 	error_t (*f)(char *root, char *opt);
10457c478bd9Sstevel@tonic-gate 
10468c1b6884Sszhou 	/*
10478c1b6884Sszhou 	 * Add trailing / for archive subcommands
10488c1b6884Sszhou 	 */
10498c1b6884Sszhou 	if (rootbuf[strlen(rootbuf) - 1] != '/')
10508c1b6884Sszhou 		(void) strcat(rootbuf, "/");
10518c1b6884Sszhou 	bam_rootlen = strlen(rootbuf);
10528c1b6884Sszhou 
10537c478bd9Sstevel@tonic-gate 	/*
10547c478bd9Sstevel@tonic-gate 	 * Check arguments
10557c478bd9Sstevel@tonic-gate 	 */
10567c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f);
10577c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS) {
10587c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
10597c478bd9Sstevel@tonic-gate 	}
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate #if defined(__sparc)
10627c478bd9Sstevel@tonic-gate 	/*
10637c478bd9Sstevel@tonic-gate 	 * A NOP if called on SPARC during reboot
10647c478bd9Sstevel@tonic-gate 	 */
10657c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "update_all") == 0)
10667c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
10677c478bd9Sstevel@tonic-gate 	else if (strcmp(subcmd, "update") != 0)
10687c478bd9Sstevel@tonic-gate 		sparc_abort();
10697c478bd9Sstevel@tonic-gate #endif
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	/*
10727c478bd9Sstevel@tonic-gate 	 * Check archive not supported with update_all
10737c478bd9Sstevel@tonic-gate 	 * since it is awkward to display out-of-sync
10747c478bd9Sstevel@tonic-gate 	 * information for each BE.
10757c478bd9Sstevel@tonic-gate 	 */
10767c478bd9Sstevel@tonic-gate 	if (bam_check && strcmp(subcmd, "update_all") == 0) {
10777c478bd9Sstevel@tonic-gate 		bam_error(CHECK_NOT_SUPPORTED, subcmd);
10787c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
10797c478bd9Sstevel@tonic-gate 	}
10807c478bd9Sstevel@tonic-gate 
1081b610f78eSvikram 	if (strcmp(subcmd, "update_all") == 0)
1082b610f78eSvikram 		bam_update_all = 1;
1083b610f78eSvikram 
1084b610f78eSvikram 	ret = f(bam_root, opt);
1085b610f78eSvikram 
1086b610f78eSvikram 	bam_update_all = 0;
1087b610f78eSvikram 
1088b610f78eSvikram 	return (ret);
10897c478bd9Sstevel@tonic-gate }
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
10927c478bd9Sstevel@tonic-gate static void
10937c478bd9Sstevel@tonic-gate bam_error(char *format, ...)
10947c478bd9Sstevel@tonic-gate {
10957c478bd9Sstevel@tonic-gate 	va_list ap;
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	va_start(ap, format);
10987c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", prog);
10997c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
11007c478bd9Sstevel@tonic-gate 	va_end(ap);
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
11047c478bd9Sstevel@tonic-gate static void
11057c478bd9Sstevel@tonic-gate bam_print(char *format, ...)
11067c478bd9Sstevel@tonic-gate {
11077c478bd9Sstevel@tonic-gate 	va_list ap;
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	va_start(ap, format);
11107c478bd9Sstevel@tonic-gate 	(void) vfprintf(stdout, format, ap);
11117c478bd9Sstevel@tonic-gate 	va_end(ap);
11127c478bd9Sstevel@tonic-gate }
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate static void
11157c478bd9Sstevel@tonic-gate bam_exit(int excode)
11167c478bd9Sstevel@tonic-gate {
11177c478bd9Sstevel@tonic-gate 	bam_unlock();
11187c478bd9Sstevel@tonic-gate 	exit(excode);
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate static void
11227c478bd9Sstevel@tonic-gate bam_lock(void)
11237c478bd9Sstevel@tonic-gate {
11247c478bd9Sstevel@tonic-gate 	struct flock lock;
11257c478bd9Sstevel@tonic-gate 	pid_t pid;
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS);
11287c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
11297c478bd9Sstevel@tonic-gate 		/*
11307c478bd9Sstevel@tonic-gate 		 * We may be invoked early in boot for archive verification.
11317c478bd9Sstevel@tonic-gate 		 * In this case, root is readonly and /var/run may not exist.
11327c478bd9Sstevel@tonic-gate 		 * Proceed without the lock
11337c478bd9Sstevel@tonic-gate 		 */
11347c478bd9Sstevel@tonic-gate 		if (errno == EROFS || errno == ENOENT) {
11357c478bd9Sstevel@tonic-gate 			bam_root_readonly = 1;
11367c478bd9Sstevel@tonic-gate 			return;
11377c478bd9Sstevel@tonic-gate 		}
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno));
11407c478bd9Sstevel@tonic-gate 		bam_exit(1);
11417c478bd9Sstevel@tonic-gate 	}
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
11447c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
11457c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
11467c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) {
11497c478bd9Sstevel@tonic-gate 		if (errno != EACCES && errno != EAGAIN) {
11507c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11517c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
11527c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
11537c478bd9Sstevel@tonic-gate 			bam_exit(1);
11547c478bd9Sstevel@tonic-gate 		}
11557c478bd9Sstevel@tonic-gate 		pid = 0;
11567c478bd9Sstevel@tonic-gate 		(void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0);
11577c478bd9Sstevel@tonic-gate 		bam_print(FILE_LOCKED, pid);
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 		lock.l_type = F_WRLCK;
11607c478bd9Sstevel@tonic-gate 		lock.l_whence = SEEK_SET;
11617c478bd9Sstevel@tonic-gate 		lock.l_start = 0;
11627c478bd9Sstevel@tonic-gate 		lock.l_len = 0;
11637c478bd9Sstevel@tonic-gate 		if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) {
11647c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11657c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
11667c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
11677c478bd9Sstevel@tonic-gate 			bam_exit(1);
11687c478bd9Sstevel@tonic-gate 		}
11697c478bd9Sstevel@tonic-gate 	}
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	/* We own the lock now */
11727c478bd9Sstevel@tonic-gate 	pid = getpid();
11737c478bd9Sstevel@tonic-gate 	(void) write(bam_lock_fd, &pid, sizeof (pid));
11747c478bd9Sstevel@tonic-gate }
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate static void
11777c478bd9Sstevel@tonic-gate bam_unlock(void)
11787c478bd9Sstevel@tonic-gate {
11797c478bd9Sstevel@tonic-gate 	struct flock unlock;
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	/*
11827c478bd9Sstevel@tonic-gate 	 * NOP if we don't hold the lock
11837c478bd9Sstevel@tonic-gate 	 */
11847c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
11857c478bd9Sstevel@tonic-gate 		return;
11867c478bd9Sstevel@tonic-gate 	}
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
11897c478bd9Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
11907c478bd9Sstevel@tonic-gate 	unlock.l_start = 0;
11917c478bd9Sstevel@tonic-gate 	unlock.l_len = 0;
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) {
11947c478bd9Sstevel@tonic-gate 		bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11957c478bd9Sstevel@tonic-gate 	}
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	if (close(bam_lock_fd) == -1) {
11987c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno));
11997c478bd9Sstevel@tonic-gate 	}
12007c478bd9Sstevel@tonic-gate 	bam_lock_fd = -1;
12017c478bd9Sstevel@tonic-gate }
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate static error_t
12047c478bd9Sstevel@tonic-gate list_archive(char *root, char *opt)
12057c478bd9Sstevel@tonic-gate {
12067c478bd9Sstevel@tonic-gate 	filelist_t flist;
12077c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
12087c478bd9Sstevel@tonic-gate 	line_t *lp;
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	assert(root);
12117c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
12147c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
12157c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12167c478bd9Sstevel@tonic-gate 	}
12177c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
12207c478bd9Sstevel@tonic-gate 		bam_print(PRINT, lp->line);
12217c478bd9Sstevel@tonic-gate 	}
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate /*
12297c478bd9Sstevel@tonic-gate  * This routine writes a list of lines to a file.
12307c478bd9Sstevel@tonic-gate  * The list is *not* freed
12317c478bd9Sstevel@tonic-gate  */
12327c478bd9Sstevel@tonic-gate static error_t
12337c478bd9Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start)
12347c478bd9Sstevel@tonic-gate {
12357c478bd9Sstevel@tonic-gate 	char tmpfile[PATH_MAX];
12367c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
12377c478bd9Sstevel@tonic-gate 	FILE *fp;
12387c478bd9Sstevel@tonic-gate 	int ret;
12397c478bd9Sstevel@tonic-gate 	struct stat sb;
12407c478bd9Sstevel@tonic-gate 	mode_t mode;
12417c478bd9Sstevel@tonic-gate 	uid_t root_uid;
12427c478bd9Sstevel@tonic-gate 	gid_t sys_gid;
12437c478bd9Sstevel@tonic-gate 	struct passwd *pw;
12447c478bd9Sstevel@tonic-gate 	struct group *gp;
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, final);
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 	if (start == NULL) {
12507c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != -1) {
12517c478bd9Sstevel@tonic-gate 			bam_print(UNLINK_EMPTY, path);
12527c478bd9Sstevel@tonic-gate 			if (unlink(path) != 0) {
12537c478bd9Sstevel@tonic-gate 				bam_error(UNLINK_FAIL, path, strerror(errno));
12547c478bd9Sstevel@tonic-gate 				return (BAM_ERROR);
12557c478bd9Sstevel@tonic-gate 			} else {
12567c478bd9Sstevel@tonic-gate 				return (BAM_SUCCESS);
12577c478bd9Sstevel@tonic-gate 			}
12587c478bd9Sstevel@tonic-gate 		}
12597c478bd9Sstevel@tonic-gate 	}
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	/*
12627c478bd9Sstevel@tonic-gate 	 * Preserve attributes of existing file if possible,
12637c478bd9Sstevel@tonic-gate 	 * otherwise ask the system for uid/gid of root/sys.
12647c478bd9Sstevel@tonic-gate 	 * If all fails, fall back on hard-coded defaults.
12657c478bd9Sstevel@tonic-gate 	 */
12667c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != -1) {
12677c478bd9Sstevel@tonic-gate 		mode = sb.st_mode;
12687c478bd9Sstevel@tonic-gate 		root_uid = sb.st_uid;
12697c478bd9Sstevel@tonic-gate 		sys_gid = sb.st_gid;
12707c478bd9Sstevel@tonic-gate 	} else {
12717c478bd9Sstevel@tonic-gate 		mode = DEFAULT_DEV_MODE;
12727c478bd9Sstevel@tonic-gate 		if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
12737c478bd9Sstevel@tonic-gate 			root_uid = pw->pw_uid;
12747c478bd9Sstevel@tonic-gate 		} else {
12757c478bd9Sstevel@tonic-gate 			if (bam_verbose)
12767c478bd9Sstevel@tonic-gate 				bam_error(CANT_FIND_USER,
12777c478bd9Sstevel@tonic-gate 				    DEFAULT_DEV_USER, DEFAULT_DEV_UID);
12787c478bd9Sstevel@tonic-gate 			root_uid = (uid_t)DEFAULT_DEV_UID;
12797c478bd9Sstevel@tonic-gate 		}
12807c478bd9Sstevel@tonic-gate 		if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
12817c478bd9Sstevel@tonic-gate 			sys_gid = gp->gr_gid;
12827c478bd9Sstevel@tonic-gate 		} else {
12837c478bd9Sstevel@tonic-gate 			if (bam_verbose)
12847c478bd9Sstevel@tonic-gate 				bam_error(CANT_FIND_GROUP,
12857c478bd9Sstevel@tonic-gate 				    DEFAULT_DEV_GROUP, DEFAULT_DEV_GID);
12867c478bd9Sstevel@tonic-gate 			sys_gid = (gid_t)DEFAULT_DEV_GID;
12877c478bd9Sstevel@tonic-gate 		}
12887c478bd9Sstevel@tonic-gate 	}
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	(void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp);
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 	/* Truncate tmpfile first */
12937c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "w");
12947c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
12957c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
12967c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12977c478bd9Sstevel@tonic-gate 	}
12987c478bd9Sstevel@tonic-gate 	ret = fclose(fp);
12997c478bd9Sstevel@tonic-gate 	if (ret == EOF) {
13007c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
13017c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13027c478bd9Sstevel@tonic-gate 	}
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	/* Now open it in append mode */
13057c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "a");
13067c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
13077c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
13087c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13097c478bd9Sstevel@tonic-gate 	}
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	for (; start; start = start->next) {
13127c478bd9Sstevel@tonic-gate 		ret = s_fputs(start->line, fp);
13137c478bd9Sstevel@tonic-gate 		if (ret == EOF) {
13147c478bd9Sstevel@tonic-gate 			bam_error(WRITE_FAIL, tmpfile, strerror(errno));
13157c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
13167c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
13177c478bd9Sstevel@tonic-gate 		}
13187c478bd9Sstevel@tonic-gate 	}
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	ret = fclose(fp);
13217c478bd9Sstevel@tonic-gate 	if (ret == EOF) {
13227c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
13237c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13247c478bd9Sstevel@tonic-gate 	}
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	/*
1327de531be4Sjg 	 * Set up desired attributes.  Ignore failures on filesystems
1328de531be4Sjg 	 * not supporting these operations - pcfs reports unsupported
1329de531be4Sjg 	 * operations as EINVAL.
13307c478bd9Sstevel@tonic-gate 	 */
13317c478bd9Sstevel@tonic-gate 	ret = chmod(tmpfile, mode);
1332de531be4Sjg 	if (ret == -1 &&
1333de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
13347c478bd9Sstevel@tonic-gate 		bam_error(CHMOD_FAIL, tmpfile, strerror(errno));
13357c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13367c478bd9Sstevel@tonic-gate 	}
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 	ret = chown(tmpfile, root_uid, sys_gid);
1339de531be4Sjg 	if (ret == -1 &&
1340de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
13417c478bd9Sstevel@tonic-gate 		bam_error(CHOWN_FAIL, tmpfile, strerror(errno));
13427c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13437c478bd9Sstevel@tonic-gate 	}
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	/*
13477c478bd9Sstevel@tonic-gate 	 * Do an atomic rename
13487c478bd9Sstevel@tonic-gate 	 */
13497c478bd9Sstevel@tonic-gate 	ret = rename(tmpfile, path);
13507c478bd9Sstevel@tonic-gate 	if (ret != 0) {
13517c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path, strerror(errno));
13527c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13537c478bd9Sstevel@tonic-gate 	}
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
13567c478bd9Sstevel@tonic-gate }
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate /*
13597c478bd9Sstevel@tonic-gate  * This function should always return 0 - since we want
13607c478bd9Sstevel@tonic-gate  * to create stat data for *all* files in the list.
13617c478bd9Sstevel@tonic-gate  */
13627c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13637c478bd9Sstevel@tonic-gate static int
13647c478bd9Sstevel@tonic-gate cmpstat(
13657c478bd9Sstevel@tonic-gate 	const char *file,
13667c478bd9Sstevel@tonic-gate 	const struct stat *stat,
13677c478bd9Sstevel@tonic-gate 	int flags,
13687c478bd9Sstevel@tonic-gate 	struct FTW *ftw)
13697c478bd9Sstevel@tonic-gate {
13707c478bd9Sstevel@tonic-gate 	uint_t sz;
13717c478bd9Sstevel@tonic-gate 	uint64_t *value;
13727c478bd9Sstevel@tonic-gate 	uint64_t filestat[2];
13737c478bd9Sstevel@tonic-gate 	int error;
13747c478bd9Sstevel@tonic-gate 
1375*58091fd8Ssetje 	struct safefile *safefilep;
1376*58091fd8Ssetje 	FILE *fp;
1377*58091fd8Ssetje 
13787c478bd9Sstevel@tonic-gate 	/*
13797c478bd9Sstevel@tonic-gate 	 * We only want regular files
13807c478bd9Sstevel@tonic-gate 	 */
13817c478bd9Sstevel@tonic-gate 	if (!S_ISREG(stat->st_mode))
13827c478bd9Sstevel@tonic-gate 		return (0);
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	/*
13857c478bd9Sstevel@tonic-gate 	 * new_nvlp may be NULL if there were errors earlier
13867c478bd9Sstevel@tonic-gate 	 * but this is not fatal to update determination.
13877c478bd9Sstevel@tonic-gate 	 */
13887c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp) {
13897c478bd9Sstevel@tonic-gate 		filestat[0] = stat->st_size;
13907c478bd9Sstevel@tonic-gate 		filestat[1] = stat->st_mtime;
13917c478bd9Sstevel@tonic-gate 		error = nvlist_add_uint64_array(walk_arg.new_nvlp,
13927c478bd9Sstevel@tonic-gate 		    file + bam_rootlen, filestat, 2);
13937c478bd9Sstevel@tonic-gate 		if (error)
13947c478bd9Sstevel@tonic-gate 			bam_error(NVADD_FAIL, file, strerror(error));
13957c478bd9Sstevel@tonic-gate 	}
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	/*
13987c478bd9Sstevel@tonic-gate 	 * The remaining steps are only required if we haven't made a
13997c478bd9Sstevel@tonic-gate 	 * decision about update or if we are checking (-n)
14007c478bd9Sstevel@tonic-gate 	 */
14017c478bd9Sstevel@tonic-gate 	if (walk_arg.need_update && !bam_check)
14027c478bd9Sstevel@tonic-gate 		return (0);
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	/*
1405*58091fd8Ssetje 	 * If we are invoked as part of system/filesyste/boot-archive, then
1406*58091fd8Ssetje 	 * there are a number of things we should not worry about
14077c478bd9Sstevel@tonic-gate 	 */
1408*58091fd8Ssetje 	if (bam_smf_check) {
1409*58091fd8Ssetje 		/* ignore amd64 modules unless we are booted amd64. */
1410*58091fd8Ssetje 		if (!is_amd64() && strstr(file, "/amd64/") != 0)
1411*58091fd8Ssetje 			return (0);
1412*58091fd8Ssetje 
1413*58091fd8Ssetje 		/* read in list of safe files */
1414*58091fd8Ssetje 		if (safefiles == NULL)
1415*58091fd8Ssetje 			if (fp = fopen("/boot/solaris/filelist.safe", "r")) {
1416*58091fd8Ssetje 				safefiles = s_calloc(1,
1417*58091fd8Ssetje 				    sizeof (struct safefile));
1418*58091fd8Ssetje 				safefilep = safefiles;
1419*58091fd8Ssetje 				safefilep->name = s_calloc(1, MAXPATHLEN +
1420*58091fd8Ssetje 				    MAXNAMELEN);
1421*58091fd8Ssetje 				safefilep->next = NULL;
1422*58091fd8Ssetje 				while (s_fgets(safefilep->name, MAXPATHLEN +
1423*58091fd8Ssetje 				    MAXNAMELEN, fp) != NULL) {
1424*58091fd8Ssetje 					safefilep->next = s_calloc(1,
1425*58091fd8Ssetje 					    sizeof (struct safefile));
1426*58091fd8Ssetje 					safefilep = safefilep->next;
1427*58091fd8Ssetje 					safefilep->name = s_calloc(1,
1428*58091fd8Ssetje 					    MAXPATHLEN + MAXNAMELEN);
1429*58091fd8Ssetje 					safefilep->next = NULL;
1430*58091fd8Ssetje 				}
1431*58091fd8Ssetje 				(void) fclose(fp);
1432*58091fd8Ssetje 			}
1433*58091fd8Ssetje 
1434*58091fd8Ssetje 		safefilep = safefiles;
1435*58091fd8Ssetje 		while (safefilep->next != NULL)
1436*58091fd8Ssetje 			if (strcmp(file, safefilep->name) != 0) {
1437*58091fd8Ssetje 				fp = fopen(NEED_UPDATE_FILE, "w");
1438*58091fd8Ssetje 				if (fclose(fp) != 0)
1439*58091fd8Ssetje 					bam_error(CLOSE_FAIL, NEED_UPDATE_FILE,
1440*58091fd8Ssetje 						strerror(errno));
1441*58091fd8Ssetje 				return (0);
1442*58091fd8Ssetje 			}
1443*58091fd8Ssetje 	}
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	/*
14467c478bd9Sstevel@tonic-gate 	 * We need an update if file doesn't exist in old archive
14477c478bd9Sstevel@tonic-gate 	 */
14487c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp == NULL ||
14497c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint64_array(walk_arg.old_nvlp,
14507c478bd9Sstevel@tonic-gate 	    file + bam_rootlen, &value, &sz) != 0) {
14517c478bd9Sstevel@tonic-gate 		if (bam_smf_check)	/* ignore new during smf check */
14527c478bd9Sstevel@tonic-gate 			return (0);
14537c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
14547c478bd9Sstevel@tonic-gate 		if (bam_verbose)
14557c478bd9Sstevel@tonic-gate 			bam_print(PARSEABLE_NEW_FILE, file);
14567c478bd9Sstevel@tonic-gate 		return (0);
14577c478bd9Sstevel@tonic-gate 	}
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	/*
14607c478bd9Sstevel@tonic-gate 	 * File exists in old archive. Check if file has changed
14617c478bd9Sstevel@tonic-gate 	 */
14627c478bd9Sstevel@tonic-gate 	assert(sz == 2);
14637c478bd9Sstevel@tonic-gate 	bcopy(value, filestat, sizeof (filestat));
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	if (filestat[0] != stat->st_size ||
14667c478bd9Sstevel@tonic-gate 	    filestat[1] != stat->st_mtime) {
14677c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
14687c478bd9Sstevel@tonic-gate 		if (bam_verbose)
14697c478bd9Sstevel@tonic-gate 			if (bam_smf_check)
14707c478bd9Sstevel@tonic-gate 				bam_print("    %s\n", file);
14717c478bd9Sstevel@tonic-gate 			else
14727c478bd9Sstevel@tonic-gate 				bam_print(PARSEABLE_OUT_DATE, file);
14737c478bd9Sstevel@tonic-gate 	}
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 	return (0);
14767c478bd9Sstevel@tonic-gate }
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate /*
14797c478bd9Sstevel@tonic-gate  * Check flags and presence of required files.
14807c478bd9Sstevel@tonic-gate  * The force flag and/or absence of files should
14817c478bd9Sstevel@tonic-gate  * trigger an update.
14827c478bd9Sstevel@tonic-gate  * Suppress stdout output if check (-n) option is set
14837c478bd9Sstevel@tonic-gate  * (as -n should only produce parseable output.)
14847c478bd9Sstevel@tonic-gate  */
14857c478bd9Sstevel@tonic-gate static void
14867c478bd9Sstevel@tonic-gate check_flags_and_files(char *root)
14877c478bd9Sstevel@tonic-gate {
14887c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
14897c478bd9Sstevel@tonic-gate 	struct stat sb;
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	/*
14927c478bd9Sstevel@tonic-gate 	 * if force, create archive unconditionally
14937c478bd9Sstevel@tonic-gate 	 */
14947c478bd9Sstevel@tonic-gate 	if (bam_force) {
14957c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
14967c478bd9Sstevel@tonic-gate 		if (bam_verbose && !bam_check)
14977c478bd9Sstevel@tonic-gate 			bam_print(UPDATE_FORCE);
14987c478bd9Sstevel@tonic-gate 		return;
14997c478bd9Sstevel@tonic-gate 	}
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	/*
15027c478bd9Sstevel@tonic-gate 	 * If archive is missing, create archive
15037c478bd9Sstevel@tonic-gate 	 */
15047c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE);
15057c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
15067c478bd9Sstevel@tonic-gate 		if (bam_verbose && !bam_check)
15077c478bd9Sstevel@tonic-gate 			bam_print(UPDATE_ARCH_MISS, path);
15087c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
15097c478bd9Sstevel@tonic-gate 		return;
15107c478bd9Sstevel@tonic-gate 	}
15117c478bd9Sstevel@tonic-gate }
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate static error_t
15147c478bd9Sstevel@tonic-gate read_one_list(char *root, filelist_t  *flistp, char *filelist)
15157c478bd9Sstevel@tonic-gate {
15167c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
15177c478bd9Sstevel@tonic-gate 	FILE *fp;
15187c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE];
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, filelist);
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 	fp = fopen(path, "r");
15237c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
15247c478bd9Sstevel@tonic-gate 		if (bam_debug)
15257c478bd9Sstevel@tonic-gate 			bam_error(FLIST_FAIL, path, strerror(errno));
15267c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15277c478bd9Sstevel@tonic-gate 	}
15287c478bd9Sstevel@tonic-gate 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
1529b610f78eSvikram 		/* skip blank lines */
1530b610f78eSvikram 		if (strspn(buf, " \t") == strlen(buf))
1531b610f78eSvikram 			continue;
15327c478bd9Sstevel@tonic-gate 		append_to_flist(flistp, buf);
15337c478bd9Sstevel@tonic-gate 	}
15347c478bd9Sstevel@tonic-gate 	if (fclose(fp) != 0) {
15357c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, path, strerror(errno));
15367c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15377c478bd9Sstevel@tonic-gate 	}
15387c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
15397c478bd9Sstevel@tonic-gate }
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate static error_t
15427c478bd9Sstevel@tonic-gate read_list(char *root, filelist_t  *flistp)
15437c478bd9Sstevel@tonic-gate {
15447c478bd9Sstevel@tonic-gate 	int rval;
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	/*
15497c478bd9Sstevel@tonic-gate 	 * Read current lists of files - only the first is mandatory
15507c478bd9Sstevel@tonic-gate 	 */
15517c478bd9Sstevel@tonic-gate 	rval = read_one_list(root, flistp, BOOT_FILE_LIST);
15527c478bd9Sstevel@tonic-gate 	if (rval != BAM_SUCCESS)
15537c478bd9Sstevel@tonic-gate 		return (rval);
15547c478bd9Sstevel@tonic-gate 	(void) read_one_list(root, flistp, ETC_FILE_LIST);
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL) {
15577c478bd9Sstevel@tonic-gate 		bam_error(NO_FLIST);
15587c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15597c478bd9Sstevel@tonic-gate 	}
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
15627c478bd9Sstevel@tonic-gate }
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate static void
15657c478bd9Sstevel@tonic-gate getoldstat(char *root)
15667c478bd9Sstevel@tonic-gate {
15677c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
15687c478bd9Sstevel@tonic-gate 	int fd, error;
15697c478bd9Sstevel@tonic-gate 	struct stat sb;
15707c478bd9Sstevel@tonic-gate 	char *ostat;
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
15737c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDONLY);
15747c478bd9Sstevel@tonic-gate 	if (fd == -1) {
15757c478bd9Sstevel@tonic-gate 		if (bam_verbose)
15767c478bd9Sstevel@tonic-gate 			bam_print(OPEN_FAIL, path, strerror(errno));
15777c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
15787c478bd9Sstevel@tonic-gate 		return;
15797c478bd9Sstevel@tonic-gate 	}
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 	if (fstat(fd, &sb) != 0) {
15827c478bd9Sstevel@tonic-gate 		bam_error(STAT_FAIL, path, strerror(errno));
15837c478bd9Sstevel@tonic-gate 		(void) close(fd);
15847c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
15857c478bd9Sstevel@tonic-gate 		return;
15867c478bd9Sstevel@tonic-gate 	}
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 	ostat = s_calloc(1, sb.st_size);
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	if (read(fd, ostat, sb.st_size) != sb.st_size) {
15917c478bd9Sstevel@tonic-gate 		bam_error(READ_FAIL, path, strerror(errno));
15927c478bd9Sstevel@tonic-gate 		(void) close(fd);
15937c478bd9Sstevel@tonic-gate 		free(ostat);
15947c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
15957c478bd9Sstevel@tonic-gate 		return;
15967c478bd9Sstevel@tonic-gate 	}
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 	(void) close(fd);
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
16017c478bd9Sstevel@tonic-gate 	error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0);
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 	free(ostat);
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 	if (error) {
16067c478bd9Sstevel@tonic-gate 		bam_error(UNPACK_FAIL, path, strerror(error));
16077c478bd9Sstevel@tonic-gate 		walk_arg.old_nvlp = NULL;
16087c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
16097c478bd9Sstevel@tonic-gate 		return;
16107c478bd9Sstevel@tonic-gate 	}
16117c478bd9Sstevel@tonic-gate }
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate static void
16147c478bd9Sstevel@tonic-gate create_newstat(void)
16157c478bd9Sstevel@tonic-gate {
16167c478bd9Sstevel@tonic-gate 	int error;
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 	error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0);
16197c478bd9Sstevel@tonic-gate 	if (error) {
16207c478bd9Sstevel@tonic-gate 		/*
16217c478bd9Sstevel@tonic-gate 		 * Not fatal - we can still create archive
16227c478bd9Sstevel@tonic-gate 		 */
16237c478bd9Sstevel@tonic-gate 		walk_arg.new_nvlp = NULL;
16247c478bd9Sstevel@tonic-gate 		bam_error(NVALLOC_FAIL, strerror(error));
16257c478bd9Sstevel@tonic-gate 	}
16267c478bd9Sstevel@tonic-gate }
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate static void
16297c478bd9Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp)
16307c478bd9Sstevel@tonic-gate {
16317c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
16327c478bd9Sstevel@tonic-gate 	line_t *lp;
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
16357c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, lp->line);
16367c478bd9Sstevel@tonic-gate 		/* XXX shouldn't we use FTW_MOUNT ? */
16377c478bd9Sstevel@tonic-gate 		if (nftw(path, cmpstat, 20, 0) == -1) {
16387c478bd9Sstevel@tonic-gate 			/*
16397c478bd9Sstevel@tonic-gate 			 * Some files may not exist.
16407c478bd9Sstevel@tonic-gate 			 * For example: etc/rtc_config on a x86 diskless system
16417c478bd9Sstevel@tonic-gate 			 * Emit verbose message only
16427c478bd9Sstevel@tonic-gate 			 */
16437c478bd9Sstevel@tonic-gate 			if (bam_verbose)
16447c478bd9Sstevel@tonic-gate 				bam_print(NFTW_FAIL, path, strerror(errno));
16457c478bd9Sstevel@tonic-gate 		}
16467c478bd9Sstevel@tonic-gate 	}
16477c478bd9Sstevel@tonic-gate }
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate static void
16507c478bd9Sstevel@tonic-gate savenew(char *root)
16517c478bd9Sstevel@tonic-gate {
16527c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
16537c478bd9Sstevel@tonic-gate 	char path2[PATH_MAX];
16547c478bd9Sstevel@tonic-gate 	size_t sz;
16557c478bd9Sstevel@tonic-gate 	char *nstat;
16567c478bd9Sstevel@tonic-gate 	int fd, wrote, error;
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	nstat = NULL;
16597c478bd9Sstevel@tonic-gate 	sz = 0;
16607c478bd9Sstevel@tonic-gate 	error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz,
16617c478bd9Sstevel@tonic-gate 	    NV_ENCODE_XDR, 0);
16627c478bd9Sstevel@tonic-gate 	if (error) {
16637c478bd9Sstevel@tonic-gate 		bam_error(PACK_FAIL, strerror(error));
16647c478bd9Sstevel@tonic-gate 		return;
16657c478bd9Sstevel@tonic-gate 	}
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP);
16687c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE);
16697c478bd9Sstevel@tonic-gate 	if (fd == -1) {
16707c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, path, strerror(errno));
16717c478bd9Sstevel@tonic-gate 		free(nstat);
16727c478bd9Sstevel@tonic-gate 		return;
16737c478bd9Sstevel@tonic-gate 	}
16747c478bd9Sstevel@tonic-gate 	wrote = write(fd, nstat, sz);
16757c478bd9Sstevel@tonic-gate 	if (wrote != sz) {
16767c478bd9Sstevel@tonic-gate 		bam_error(WRITE_FAIL, path, strerror(errno));
16777c478bd9Sstevel@tonic-gate 		(void) close(fd);
16787c478bd9Sstevel@tonic-gate 		free(nstat);
16797c478bd9Sstevel@tonic-gate 		return;
16807c478bd9Sstevel@tonic-gate 	}
16817c478bd9Sstevel@tonic-gate 	(void) close(fd);
16827c478bd9Sstevel@tonic-gate 	free(nstat);
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	(void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT);
16857c478bd9Sstevel@tonic-gate 	if (rename(path, path2) != 0) {
16867c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path2, strerror(errno));
16877c478bd9Sstevel@tonic-gate 	}
16887c478bd9Sstevel@tonic-gate }
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate static void
16917c478bd9Sstevel@tonic-gate clear_walk_args(void)
16927c478bd9Sstevel@tonic-gate {
16937c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp)
16947c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.old_nvlp);
16957c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp)
16967c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.new_nvlp);
16977c478bd9Sstevel@tonic-gate 	walk_arg.need_update = 0;
16987c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
16997c478bd9Sstevel@tonic-gate 	walk_arg.new_nvlp = NULL;
17007c478bd9Sstevel@tonic-gate }
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate /*
17037c478bd9Sstevel@tonic-gate  * Returns:
17047c478bd9Sstevel@tonic-gate  *	0 - no update necessary
17057c478bd9Sstevel@tonic-gate  *	1 - update required.
17067c478bd9Sstevel@tonic-gate  *	BAM_ERROR (-1) - An error occurred
17077c478bd9Sstevel@tonic-gate  *
17087c478bd9Sstevel@tonic-gate  * Special handling for check (-n):
17097c478bd9Sstevel@tonic-gate  * ================================
17107c478bd9Sstevel@tonic-gate  * The check (-n) option produces parseable output.
17117c478bd9Sstevel@tonic-gate  * To do this, we suppress all stdout messages unrelated
17127c478bd9Sstevel@tonic-gate  * to out of sync files.
17137c478bd9Sstevel@tonic-gate  * All stderr messages are still printed though.
17147c478bd9Sstevel@tonic-gate  *
17157c478bd9Sstevel@tonic-gate  */
17167c478bd9Sstevel@tonic-gate static int
17177c478bd9Sstevel@tonic-gate update_required(char *root)
17187c478bd9Sstevel@tonic-gate {
17197c478bd9Sstevel@tonic-gate 	struct stat sb;
17207c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
17217c478bd9Sstevel@tonic-gate 	filelist_t flist;
17227c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
17237c478bd9Sstevel@tonic-gate 	int need_update;
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 	walk_arg.need_update = 0;
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	/*
17307c478bd9Sstevel@tonic-gate 	 * Without consulting stat data, check if we need update
17317c478bd9Sstevel@tonic-gate 	 */
17327c478bd9Sstevel@tonic-gate 	check_flags_and_files(root);
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	/*
17357c478bd9Sstevel@tonic-gate 	 * In certain deployment scenarios, filestat may not
17367c478bd9Sstevel@tonic-gate 	 * exist. Ignore it during boot-archive SMF check.
17377c478bd9Sstevel@tonic-gate 	 */
17387c478bd9Sstevel@tonic-gate 	if (bam_smf_check) {
17397c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
17407c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != 0)
17417c478bd9Sstevel@tonic-gate 			return (0);
17427c478bd9Sstevel@tonic-gate 	}
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	/*
17457c478bd9Sstevel@tonic-gate 	 * consult stat data only if we haven't made a decision
17467c478bd9Sstevel@tonic-gate 	 * about update. If checking (-n) however, we always
17477c478bd9Sstevel@tonic-gate 	 * need stat data (since we want to compare old and new)
17487c478bd9Sstevel@tonic-gate 	 */
17497c478bd9Sstevel@tonic-gate 	if (!walk_arg.need_update || bam_check)
17507c478bd9Sstevel@tonic-gate 		getoldstat(root);
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 	/*
17537c478bd9Sstevel@tonic-gate 	 * read list of files
17547c478bd9Sstevel@tonic-gate 	 */
17557c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
17567c478bd9Sstevel@tonic-gate 		clear_walk_args();
17577c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
17587c478bd9Sstevel@tonic-gate 	}
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 	/*
17637c478bd9Sstevel@tonic-gate 	 * At this point either the update is required
17647c478bd9Sstevel@tonic-gate 	 * or the decision is pending. In either case
17657c478bd9Sstevel@tonic-gate 	 * we need to create new stat nvlist
17667c478bd9Sstevel@tonic-gate 	 */
17677c478bd9Sstevel@tonic-gate 	create_newstat();
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	/*
17707c478bd9Sstevel@tonic-gate 	 * This walk does 2 things:
17717c478bd9Sstevel@tonic-gate 	 *  	- gets new stat data for every file
17727c478bd9Sstevel@tonic-gate 	 *	- (optional) compare old and new stat data
17737c478bd9Sstevel@tonic-gate 	 */
17747c478bd9Sstevel@tonic-gate 	walk_list(root, &flist);
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	/* done with the file list */
17777c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	/*
17807c478bd9Sstevel@tonic-gate 	 * if we didn't succeed in  creating new stat data above
17817c478bd9Sstevel@tonic-gate 	 * just return result of update check so that archive is built.
17827c478bd9Sstevel@tonic-gate 	 */
17837c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp == NULL) {
17847c478bd9Sstevel@tonic-gate 		bam_error(NO_NEW_STAT);
17857c478bd9Sstevel@tonic-gate 		need_update = walk_arg.need_update;
17867c478bd9Sstevel@tonic-gate 		clear_walk_args();
17877c478bd9Sstevel@tonic-gate 		return (need_update ? 1 : 0);
17887c478bd9Sstevel@tonic-gate 	}
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 	/*
17927c478bd9Sstevel@tonic-gate 	 * If no update required, discard newstat
17937c478bd9Sstevel@tonic-gate 	 */
17947c478bd9Sstevel@tonic-gate 	if (!walk_arg.need_update) {
17957c478bd9Sstevel@tonic-gate 		clear_walk_args();
17967c478bd9Sstevel@tonic-gate 		return (0);
17977c478bd9Sstevel@tonic-gate 	}
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	/*
18007c478bd9Sstevel@tonic-gate 	 * At this point we need an update - so save new stat data
18017c478bd9Sstevel@tonic-gate 	 * However, if only checking (-n), don't save new stat data.
18027c478bd9Sstevel@tonic-gate 	 */
18037c478bd9Sstevel@tonic-gate 	if (!bam_check)
18047c478bd9Sstevel@tonic-gate 		savenew(root);
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	clear_walk_args();
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	return (1);
18097c478bd9Sstevel@tonic-gate }
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate static error_t
18127c478bd9Sstevel@tonic-gate create_ramdisk(char *root)
18137c478bd9Sstevel@tonic-gate {
18147c478bd9Sstevel@tonic-gate 	char *cmdline, path[PATH_MAX];
18157c478bd9Sstevel@tonic-gate 	size_t len;
18167c478bd9Sstevel@tonic-gate 	struct stat sb;
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	/*
18197c478bd9Sstevel@tonic-gate 	 * Setup command args for create_ramdisk.ksh
18207c478bd9Sstevel@tonic-gate 	 */
18217c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, CREATE_RAMDISK);
18227c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
18237c478bd9Sstevel@tonic-gate 		bam_error(ARCH_EXEC_MISS, path, strerror(errno));
18247c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18257c478bd9Sstevel@tonic-gate 	}
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 	len = strlen(path) + strlen(root) + 10;	/* room for space + -R */
18287c478bd9Sstevel@tonic-gate 	cmdline = s_calloc(1, len);
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 	if (strlen(root) > 1) {
18317c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s -R %s", path, root);
18327c478bd9Sstevel@tonic-gate 		/* chop off / at the end */
18337c478bd9Sstevel@tonic-gate 		cmdline[strlen(cmdline) - 1] = '\0';
18347c478bd9Sstevel@tonic-gate 	} else
18357c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s", path);
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	if (exec_cmd(cmdline, NULL, 0) != 0) {
18387c478bd9Sstevel@tonic-gate 		bam_error(ARCHIVE_FAIL, cmdline);
18397c478bd9Sstevel@tonic-gate 		free(cmdline);
18407c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18417c478bd9Sstevel@tonic-gate 	}
18427c478bd9Sstevel@tonic-gate 	free(cmdline);
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	/*
18457c478bd9Sstevel@tonic-gate 	 * Verify that the archive has been created
18467c478bd9Sstevel@tonic-gate 	 */
18477c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE);
18487c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
18497c478bd9Sstevel@tonic-gate 		bam_error(ARCHIVE_NOT_CREATED, path);
18507c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18517c478bd9Sstevel@tonic-gate 	}
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
18547c478bd9Sstevel@tonic-gate }
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate /*
18577c478bd9Sstevel@tonic-gate  * Checks if target filesystem is on a ramdisk
18587c478bd9Sstevel@tonic-gate  * 1 - is miniroot
18597c478bd9Sstevel@tonic-gate  * 0 - is not
18607c478bd9Sstevel@tonic-gate  * When in doubt assume it is not a ramdisk.
18617c478bd9Sstevel@tonic-gate  */
18627c478bd9Sstevel@tonic-gate static int
18637c478bd9Sstevel@tonic-gate is_ramdisk(char *root)
18647c478bd9Sstevel@tonic-gate {
18657c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
18667c478bd9Sstevel@tonic-gate 	FILE *fp;
18677c478bd9Sstevel@tonic-gate 	int found;
1868b610f78eSvikram 	char mntpt[PATH_MAX];
1869b610f78eSvikram 	char *cp;
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	/*
18727c478bd9Sstevel@tonic-gate 	 * There are 3 situations where creating archive is
18737c478bd9Sstevel@tonic-gate 	 * of dubious value:
1874b610f78eSvikram 	 *	- create boot_archive on a lofi-mounted boot_archive
18757c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk which is the root filesystem
18767c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk mounted somewhere else
18777c478bd9Sstevel@tonic-gate 	 * The first is not easy to detect and checking for it is not
18787c478bd9Sstevel@tonic-gate 	 * worth it.
18797c478bd9Sstevel@tonic-gate 	 * The other two conditions are handled here
18807c478bd9Sstevel@tonic-gate 	 */
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
18837c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
18847c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
18857c478bd9Sstevel@tonic-gate 		return (0);
18867c478bd9Sstevel@tonic-gate 	}
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
18897c478bd9Sstevel@tonic-gate 
1890b610f78eSvikram 	/*
1891b610f78eSvikram 	 * Remove any trailing / from the mount point
1892b610f78eSvikram 	 */
1893b610f78eSvikram 	(void) strlcpy(mntpt, root, sizeof (mntpt));
1894b610f78eSvikram 	if (strcmp(root, "/") != 0) {
1895b610f78eSvikram 		cp = mntpt + strlen(mntpt) - 1;
1896b610f78eSvikram 		if (*cp == '/')
1897b610f78eSvikram 			*cp = '\0';
1898b610f78eSvikram 	}
18997c478bd9Sstevel@tonic-gate 	found = 0;
19007c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
1901b610f78eSvikram 		if (strcmp(mnt.mnt_mountp, mntpt) == 0) {
19027c478bd9Sstevel@tonic-gate 			found = 1;
19037c478bd9Sstevel@tonic-gate 			break;
19047c478bd9Sstevel@tonic-gate 		}
19057c478bd9Sstevel@tonic-gate 	}
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 	if (!found) {
19087c478bd9Sstevel@tonic-gate 		if (bam_verbose)
1909b610f78eSvikram 			bam_error(NOT_IN_MNTTAB, mntpt);
19107c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
19117c478bd9Sstevel@tonic-gate 		return (0);
19127c478bd9Sstevel@tonic-gate 	}
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 	if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) {
19157c478bd9Sstevel@tonic-gate 		if (bam_verbose)
19167c478bd9Sstevel@tonic-gate 			bam_error(IS_RAMDISK, bam_root);
19177c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
19187c478bd9Sstevel@tonic-gate 		return (1);
19197c478bd9Sstevel@tonic-gate 	}
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 	return (0);
19247c478bd9Sstevel@tonic-gate }
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate static int
19277c478bd9Sstevel@tonic-gate is_newboot(char *root)
19287c478bd9Sstevel@tonic-gate {
19297c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
19307c478bd9Sstevel@tonic-gate 	struct stat sb;
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 	/*
19337c478bd9Sstevel@tonic-gate 	 * We can't boot without MULTI_BOOT
19347c478bd9Sstevel@tonic-gate 	 */
19357c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, MULTI_BOOT);
19367c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
19377c478bd9Sstevel@tonic-gate 		if (bam_verbose)
19387c478bd9Sstevel@tonic-gate 			bam_print(FILE_MISS, path);
19397c478bd9Sstevel@tonic-gate 		return (0);
19407c478bd9Sstevel@tonic-gate 	}
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate 	/*
19437c478bd9Sstevel@tonic-gate 	 * We can't generate archive without GRUB_DIR
19447c478bd9Sstevel@tonic-gate 	 */
19457c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, GRUB_DIR);
19467c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
19477c478bd9Sstevel@tonic-gate 		if (bam_verbose)
19487c478bd9Sstevel@tonic-gate 			bam_print(DIR_MISS, path);
19497c478bd9Sstevel@tonic-gate 		return (0);
19507c478bd9Sstevel@tonic-gate 	}
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 	return (1);
19537c478bd9Sstevel@tonic-gate }
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate static int
19567c478bd9Sstevel@tonic-gate is_readonly(char *root)
19577c478bd9Sstevel@tonic-gate {
19587c478bd9Sstevel@tonic-gate 	struct statvfs vfs;
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 	/*
19617c478bd9Sstevel@tonic-gate 	 * Check for RDONLY filesystem
19627c478bd9Sstevel@tonic-gate 	 * When in doubt assume it is not readonly
19637c478bd9Sstevel@tonic-gate 	 */
19647c478bd9Sstevel@tonic-gate 	if (statvfs(root, &vfs) != 0) {
19657c478bd9Sstevel@tonic-gate 		if (bam_verbose)
19667c478bd9Sstevel@tonic-gate 			bam_error(STATVFS_FAIL, root, strerror(errno));
19677c478bd9Sstevel@tonic-gate 		return (0);
19687c478bd9Sstevel@tonic-gate 	}
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 	if (vfs.f_flag & ST_RDONLY) {
19717c478bd9Sstevel@tonic-gate 		return (1);
19727c478bd9Sstevel@tonic-gate 	}
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	return (0);
19757c478bd9Sstevel@tonic-gate }
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate static error_t
19787c478bd9Sstevel@tonic-gate update_archive(char *root, char *opt)
19797c478bd9Sstevel@tonic-gate {
19807c478bd9Sstevel@tonic-gate 	error_t ret;
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 	assert(root);
19837c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	/*
1986b610f78eSvikram 	 * root must belong to a GRUB boot OS,
19877c478bd9Sstevel@tonic-gate 	 * don't care on sparc except for diskless clients
19887c478bd9Sstevel@tonic-gate 	 */
19897c478bd9Sstevel@tonic-gate 	if (!is_newboot(root)) {
1990b610f78eSvikram 		/*
1991b610f78eSvikram 		 * Emit message only if not in context of update_all.
1992b610f78eSvikram 		 * If in update_all, emit only if verbose flag is set.
1993b610f78eSvikram 		 */
1994b610f78eSvikram 		if (!bam_update_all || bam_verbose)
1995b610f78eSvikram 			bam_print(NOT_GRUB_BOOT, root);
19967c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
19977c478bd9Sstevel@tonic-gate 	}
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 	/*
20008c1b6884Sszhou 	 * If smf check is requested when / is writable (can happen
20018c1b6884Sszhou 	 * on first reboot following an upgrade because service
20028c1b6884Sszhou 	 * dependency is messed up), skip the check.
20038c1b6884Sszhou 	 */
20048c1b6884Sszhou 	if (bam_smf_check && !bam_root_readonly)
20058c1b6884Sszhou 		return (BAM_SUCCESS);
20068c1b6884Sszhou 
20078c1b6884Sszhou 	/*
20088c1b6884Sszhou 	 * root must be writable. This check applies to alternate
20098c1b6884Sszhou 	 * root (-R option); bam_root_readonly applies to '/' only.
20107c478bd9Sstevel@tonic-gate 	 * Note: statvfs() does not always report the truth
20117c478bd9Sstevel@tonic-gate 	 */
201261cb17bdSsetje 	if (!bam_smf_check && !bam_check && is_readonly(root)) {
20138c1b6884Sszhou 		if (bam_verbose)
20147c478bd9Sstevel@tonic-gate 			bam_print(RDONLY_FS, root);
20157c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
20167c478bd9Sstevel@tonic-gate 	}
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	/*
20197c478bd9Sstevel@tonic-gate 	 * Don't generate archive on ramdisk
20207c478bd9Sstevel@tonic-gate 	 */
20217c478bd9Sstevel@tonic-gate 	if (is_ramdisk(root)) {
20227c478bd9Sstevel@tonic-gate 		if (bam_verbose)
20237c478bd9Sstevel@tonic-gate 			bam_print(SKIP_RAMDISK);
20247c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
20257c478bd9Sstevel@tonic-gate 	}
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 	/*
20287c478bd9Sstevel@tonic-gate 	 * Now check if updated is really needed
20297c478bd9Sstevel@tonic-gate 	 */
20307c478bd9Sstevel@tonic-gate 	ret = update_required(root);
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	/*
20337c478bd9Sstevel@tonic-gate 	 * The check command (-n) is *not* a dry run
20347c478bd9Sstevel@tonic-gate 	 * It only checks if the archive is in sync.
20357c478bd9Sstevel@tonic-gate 	 */
20367c478bd9Sstevel@tonic-gate 	if (bam_check) {
20377c478bd9Sstevel@tonic-gate 		bam_exit((ret != 0) ? 1 : 0);
20387c478bd9Sstevel@tonic-gate 	}
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	if (ret == 1) {
20417c478bd9Sstevel@tonic-gate 		/* create the ramdisk */
20427c478bd9Sstevel@tonic-gate 		ret = create_ramdisk(root);
20437c478bd9Sstevel@tonic-gate 	}
20447c478bd9Sstevel@tonic-gate 	return (ret);
20457c478bd9Sstevel@tonic-gate }
20467c478bd9Sstevel@tonic-gate 
2047fbac2b2bSvikram static void
2048fbac2b2bSvikram update_fdisk(void)
2049fbac2b2bSvikram {
2050fbac2b2bSvikram 	struct stat sb;
2051fbac2b2bSvikram 	char cmd[PATH_MAX];
2052fbac2b2bSvikram 	int ret1, ret2;
2053fbac2b2bSvikram 
2054fbac2b2bSvikram 	assert(stat(GRUB_fdisk, &sb) == 0);
2055fbac2b2bSvikram 	assert(stat(GRUB_fdisk_target, &sb) == 0);
2056fbac2b2bSvikram 
2057fbac2b2bSvikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/fdisk -F %s `/bin/cat %s`",
2058fbac2b2bSvikram 	    GRUB_fdisk, GRUB_fdisk_target);
2059fbac2b2bSvikram 
2060fbac2b2bSvikram 	bam_print(UPDATING_FDISK);
2061fbac2b2bSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2062fbac2b2bSvikram 		bam_error(FDISK_UPDATE_FAILED);
2063fbac2b2bSvikram 	}
2064fbac2b2bSvikram 
2065fbac2b2bSvikram 	/*
2066fbac2b2bSvikram 	 * We are done, remove the files.
2067fbac2b2bSvikram 	 */
2068fbac2b2bSvikram 	ret1 = unlink(GRUB_fdisk);
2069fbac2b2bSvikram 	ret2 = unlink(GRUB_fdisk_target);
2070fbac2b2bSvikram 	if (ret1 != 0 || ret2 != 0) {
2071fbac2b2bSvikram 		bam_error(FILE_REMOVE_FAILED, GRUB_fdisk, GRUB_fdisk_target);
2072fbac2b2bSvikram 	}
2073fbac2b2bSvikram }
2074fbac2b2bSvikram 
2075b610f78eSvikram static void
2076b610f78eSvikram restore_grub_slice(void)
2077b610f78eSvikram {
2078b610f78eSvikram 	struct stat sb;
2079b610f78eSvikram 	char *mntpt, *physlice;
2080b610f78eSvikram 	int mnted;	/* set if we did a mount */
2081b610f78eSvikram 	char menupath[PATH_MAX], cmd[PATH_MAX];
2082b610f78eSvikram 
2083b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
2084b610f78eSvikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
2085b610f78eSvikram 		return;
2086b610f78eSvikram 	}
2087b610f78eSvikram 
2088b610f78eSvikram 	/*
2089b610f78eSvikram 	 * If we are doing an luactivate, don't attempt to restore GRUB or else
2090b610f78eSvikram 	 * we may not be able to get to DCA boot environments. Let luactivate
2091b610f78eSvikram 	 * handle GRUB/DCA installation
2092b610f78eSvikram 	 */
2093b610f78eSvikram 	if (stat(LU_ACTIVATE_FILE, &sb) == 0) {
2094b610f78eSvikram 		return;
2095b610f78eSvikram 	}
2096b610f78eSvikram 
2097b610f78eSvikram 	mnted = 0;
2098b610f78eSvikram 	physlice = NULL;
209940541d5dSvikram 	mntpt = mount_grub_slice(&mnted, &physlice, NULL, NULL);
2100b610f78eSvikram 	if (mntpt == NULL) {
2101b610f78eSvikram 		bam_error(CANNOT_RESTORE_GRUB_SLICE);
2102b610f78eSvikram 		return;
2103b610f78eSvikram 	}
2104b610f78eSvikram 
2105b610f78eSvikram 	(void) snprintf(menupath, sizeof (menupath), "%s%s", mntpt, GRUB_MENU);
2106b610f78eSvikram 	if (stat(menupath, &sb) == 0) {
210740541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2108b610f78eSvikram 		return;
2109b610f78eSvikram 	}
2110b610f78eSvikram 
2111b610f78eSvikram 	/*
2112b610f78eSvikram 	 * The menu is missing - we need to do a restore
2113b610f78eSvikram 	 */
2114b610f78eSvikram 	bam_print(RESTORING_GRUB);
2115b610f78eSvikram 
2116b610f78eSvikram 	(void) snprintf(cmd, sizeof (cmd), "%s %s %s %s",
2117b610f78eSvikram 	    INSTALLGRUB, STAGE1, STAGE2, physlice);
2118b610f78eSvikram 
2119b610f78eSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2120b610f78eSvikram 		bam_error(RESTORE_GRUB_FAILED);
212140541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2122b610f78eSvikram 		return;
2123b610f78eSvikram 	}
2124b610f78eSvikram 
2125b610f78eSvikram 	if (stat(GRUB_backup_menu, &sb) != 0) {
2126b610f78eSvikram 		bam_error(MISSING_BACKUP_MENU,
2127b610f78eSvikram 		    GRUB_backup_menu, strerror(errno));
212840541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2129b610f78eSvikram 		return;
2130b610f78eSvikram 	}
2131b610f78eSvikram 
2132b610f78eSvikram 	(void) snprintf(cmd, sizeof (cmd), "/bin/cp %s %s",
2133b610f78eSvikram 	    GRUB_backup_menu, menupath);
2134b610f78eSvikram 
2135b610f78eSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2136b610f78eSvikram 		bam_error(RESTORE_MENU_FAILED, menupath);
213740541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2138b610f78eSvikram 		return;
2139b610f78eSvikram 	}
2140b610f78eSvikram 
2141b610f78eSvikram 	/* Success */
214240541d5dSvikram 	umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2143b610f78eSvikram }
2144b610f78eSvikram 
21457c478bd9Sstevel@tonic-gate static error_t
21467c478bd9Sstevel@tonic-gate update_all(char *root, char *opt)
21477c478bd9Sstevel@tonic-gate {
21487c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
21497c478bd9Sstevel@tonic-gate 	struct stat sb;
21507c478bd9Sstevel@tonic-gate 	FILE *fp;
21517c478bd9Sstevel@tonic-gate 	char multibt[PATH_MAX];
21527c478bd9Sstevel@tonic-gate 	error_t ret = BAM_SUCCESS;
2153fbac2b2bSvikram 	int ret1, ret2;
21547c478bd9Sstevel@tonic-gate 
215540541d5dSvikram 	assert(root);
21567c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
21577c478bd9Sstevel@tonic-gate 
215840541d5dSvikram 	if (bam_rootlen != 1 || *root != '/') {
215940541d5dSvikram 		elide_trailing_slash(root, multibt, sizeof (multibt));
216040541d5dSvikram 		bam_error(ALT_ROOT_INVALID, multibt);
216140541d5dSvikram 		return (BAM_ERROR);
216240541d5dSvikram 	}
216340541d5dSvikram 
21647c478bd9Sstevel@tonic-gate 	/*
21657c478bd9Sstevel@tonic-gate 	 * First update archive for current root
21667c478bd9Sstevel@tonic-gate 	 */
21677c478bd9Sstevel@tonic-gate 	if (update_archive(root, opt) != BAM_SUCCESS)
21687c478bd9Sstevel@tonic-gate 		ret = BAM_ERROR;
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 	/*
21717c478bd9Sstevel@tonic-gate 	 * Now walk the mount table, performing archive update
21727c478bd9Sstevel@tonic-gate 	 * for all mounted Newboot root filesystems
21737c478bd9Sstevel@tonic-gate 	 */
21747c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
21757c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
21767c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
2177b610f78eSvikram 		ret = BAM_ERROR;
2178b610f78eSvikram 		goto out;
21797c478bd9Sstevel@tonic-gate 	}
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
21847c478bd9Sstevel@tonic-gate 		if (mnt.mnt_special == NULL)
21857c478bd9Sstevel@tonic-gate 			continue;
21867c478bd9Sstevel@tonic-gate 		if (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0)
21877c478bd9Sstevel@tonic-gate 			continue;
21887c478bd9Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, "/") == 0)
21897c478bd9Sstevel@tonic-gate 			continue;
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate 		(void) snprintf(multibt, sizeof (multibt), "%s%s",
21927c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp, MULTI_BOOT);
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 		if (stat(multibt, &sb) == -1)
21957c478bd9Sstevel@tonic-gate 			continue;
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate 		/*
21987c478bd9Sstevel@tonic-gate 		 * We put a trailing slash to be consistent with root = "/"
21997c478bd9Sstevel@tonic-gate 		 * case, such that we don't have to print // in some cases.
22007c478bd9Sstevel@tonic-gate 		 */
22017c478bd9Sstevel@tonic-gate 		(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
22027c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp);
22037c478bd9Sstevel@tonic-gate 		bam_rootlen = strlen(rootbuf);
22047c478bd9Sstevel@tonic-gate 		if (update_archive(rootbuf, opt) != BAM_SUCCESS)
22057c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
22067c478bd9Sstevel@tonic-gate 	}
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
22097c478bd9Sstevel@tonic-gate 
2210b610f78eSvikram out:
2211b610f78eSvikram 	if (stat(GRUB_slice, &sb) == 0) {
2212b610f78eSvikram 		restore_grub_slice();
2213b610f78eSvikram 	}
2214b610f78eSvikram 
2215fbac2b2bSvikram 	/*
2216fbac2b2bSvikram 	 * Update fdisk table as we go down. Updating it when
2217fbac2b2bSvikram 	 * the system is running will confuse biosdev.
2218fbac2b2bSvikram 	 */
2219fbac2b2bSvikram 	ret1 = stat(GRUB_fdisk, &sb);
2220fbac2b2bSvikram 	ret2 = stat(GRUB_fdisk_target, &sb);
2221fbac2b2bSvikram 	if ((ret1 == 0) && (ret2 == 0)) {
2222fbac2b2bSvikram 		update_fdisk();
2223fbac2b2bSvikram 	} else if ((ret1 == 0) ^ (ret2 == 0)) {
2224fbac2b2bSvikram 		/*
2225fbac2b2bSvikram 		 * It is an error for one file to be
2226fbac2b2bSvikram 		 * present and the other absent.
2227fbac2b2bSvikram 		 * It is normal for both files to be
2228fbac2b2bSvikram 		 * absent - it indicates that no fdisk
2229fbac2b2bSvikram 		 * update is required.
2230fbac2b2bSvikram 		 */
2231fbac2b2bSvikram 		bam_error(MISSING_FDISK_FILE,
2232fbac2b2bSvikram 		    ret1 ? GRUB_fdisk : GRUB_fdisk_target);
2233fbac2b2bSvikram 		ret = BAM_ERROR;
2234fbac2b2bSvikram 	}
2235fbac2b2bSvikram 
22367c478bd9Sstevel@tonic-gate 	return (ret);
22377c478bd9Sstevel@tonic-gate }
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate static void
22407c478bd9Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp)
22417c478bd9Sstevel@tonic-gate {
22427c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
22437c478bd9Sstevel@tonic-gate 		mp->start = lp;
22447c478bd9Sstevel@tonic-gate 	} else {
22457c478bd9Sstevel@tonic-gate 		mp->end->next = lp;
22468c1b6884Sszhou 		lp->prev = mp->end;
22477c478bd9Sstevel@tonic-gate 	}
22487c478bd9Sstevel@tonic-gate 	mp->end = lp;
22497c478bd9Sstevel@tonic-gate }
22507c478bd9Sstevel@tonic-gate 
22518c1b6884Sszhou static void
22528c1b6884Sszhou unlink_line(menu_t *mp, line_t *lp)
22538c1b6884Sszhou {
22548c1b6884Sszhou 	/* unlink from list */
22558c1b6884Sszhou 	if (lp->prev)
22568c1b6884Sszhou 		lp->prev->next = lp->next;
22578c1b6884Sszhou 	else
22588c1b6884Sszhou 		mp->start = lp->next;
22598c1b6884Sszhou 	if (lp->next)
22608c1b6884Sszhou 		lp->next->prev = lp->prev;
22618c1b6884Sszhou 	else
22628c1b6884Sszhou 		mp->end = lp->prev;
22638c1b6884Sszhou }
22648c1b6884Sszhou 
22658c1b6884Sszhou static entry_t *
22668c1b6884Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end)
22678c1b6884Sszhou {
22688c1b6884Sszhou 	entry_t *ent, *prev;
22698c1b6884Sszhou 
22708c1b6884Sszhou 	ent = s_calloc(1, sizeof (entry_t));
22718c1b6884Sszhou 	ent->start = start;
22728c1b6884Sszhou 	ent->end = end;
22738c1b6884Sszhou 
22748c1b6884Sszhou 	if (mp->entries == NULL) {
22758c1b6884Sszhou 		mp->entries = ent;
22768c1b6884Sszhou 		return (ent);
22778c1b6884Sszhou 	}
22788c1b6884Sszhou 
22798c1b6884Sszhou 	prev = mp->entries;
22808c1b6884Sszhou 	while (prev->next)
22818c1b6884Sszhou 		prev = prev-> next;
22828c1b6884Sszhou 	prev->next = ent;
22838c1b6884Sszhou 	ent->prev = prev;
22848c1b6884Sszhou 	return (ent);
22858c1b6884Sszhou }
22868c1b6884Sszhou 
22878c1b6884Sszhou static void
22888c1b6884Sszhou boot_entry_addline(entry_t *ent, line_t *lp)
22898c1b6884Sszhou {
22908c1b6884Sszhou 	if (ent)
22918c1b6884Sszhou 		ent->end = lp;
22928c1b6884Sszhou }
22938c1b6884Sszhou 
22947c478bd9Sstevel@tonic-gate /*
22957c478bd9Sstevel@tonic-gate  * A line in menu.lst looks like
22967c478bd9Sstevel@tonic-gate  * [ ]*<cmd>[ \t=]*<arg>*
22977c478bd9Sstevel@tonic-gate  */
22987c478bd9Sstevel@tonic-gate static void
22997c478bd9Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum)
23007c478bd9Sstevel@tonic-gate {
23017c478bd9Sstevel@tonic-gate 	/*
23027c478bd9Sstevel@tonic-gate 	 * save state across calls. This is so that
23037c478bd9Sstevel@tonic-gate 	 * header gets the right entry# after title has
23047c478bd9Sstevel@tonic-gate 	 * been processed
23057c478bd9Sstevel@tonic-gate 	 */
23068c1b6884Sszhou 	static line_t *prev = NULL;
23078c1b6884Sszhou 	static entry_t *curr_ent = NULL;
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 	line_t	*lp;
23107c478bd9Sstevel@tonic-gate 	char *cmd, *sep, *arg;
23117c478bd9Sstevel@tonic-gate 	char save, *cp, *line;
23127c478bd9Sstevel@tonic-gate 	menu_flag_t flag = BAM_INVALID;
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 	if (str == NULL) {
23157c478bd9Sstevel@tonic-gate 		return;
23167c478bd9Sstevel@tonic-gate 	}
23177c478bd9Sstevel@tonic-gate 
23187c478bd9Sstevel@tonic-gate 	/*
23197c478bd9Sstevel@tonic-gate 	 * First save a copy of the entire line.
23207c478bd9Sstevel@tonic-gate 	 * We use this later to set the line field.
23217c478bd9Sstevel@tonic-gate 	 */
23227c478bd9Sstevel@tonic-gate 	line = s_strdup(str);
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 	/* Eat up leading whitespace */
23257c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
23267c478bd9Sstevel@tonic-gate 		str++;
23277c478bd9Sstevel@tonic-gate 
23287c478bd9Sstevel@tonic-gate 	if (*str == '#') {		/* comment */
23297c478bd9Sstevel@tonic-gate 		cmd = s_strdup("#");
23307c478bd9Sstevel@tonic-gate 		sep = NULL;
23317c478bd9Sstevel@tonic-gate 		arg = s_strdup(str + 1);
23327c478bd9Sstevel@tonic-gate 		flag = BAM_COMMENT;
23337c478bd9Sstevel@tonic-gate 	} else if (*str == '\0') {	/* blank line */
23347c478bd9Sstevel@tonic-gate 		cmd = sep = arg = NULL;
23357c478bd9Sstevel@tonic-gate 		flag = BAM_EMPTY;
23367c478bd9Sstevel@tonic-gate 	} else {
23377c478bd9Sstevel@tonic-gate 		/*
23387c478bd9Sstevel@tonic-gate 		 * '=' is not a documented separator in grub syntax.
23397c478bd9Sstevel@tonic-gate 		 * However various development bits use '=' as a
23407c478bd9Sstevel@tonic-gate 		 * separator. In addition, external users also
23417c478bd9Sstevel@tonic-gate 		 * use = as a separator. So we will allow that usage.
23427c478bd9Sstevel@tonic-gate 		 */
23437c478bd9Sstevel@tonic-gate 		cp = str;
23447c478bd9Sstevel@tonic-gate 		while (*str != ' ' && *str != '\t' && *str != '=') {
23457c478bd9Sstevel@tonic-gate 			if (*str == '\0') {
23467c478bd9Sstevel@tonic-gate 				cmd = s_strdup(cp);
23477c478bd9Sstevel@tonic-gate 				sep = arg = NULL;
23487c478bd9Sstevel@tonic-gate 				break;
23497c478bd9Sstevel@tonic-gate 			}
23507c478bd9Sstevel@tonic-gate 			str++;
23517c478bd9Sstevel@tonic-gate 		}
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 		if (*str != '\0') {
23547c478bd9Sstevel@tonic-gate 			save = *str;
23557c478bd9Sstevel@tonic-gate 			*str = '\0';
23567c478bd9Sstevel@tonic-gate 			cmd = s_strdup(cp);
23577c478bd9Sstevel@tonic-gate 			*str = save;
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 			str++;
23607c478bd9Sstevel@tonic-gate 			save = *str;
23617c478bd9Sstevel@tonic-gate 			*str = '\0';
23627c478bd9Sstevel@tonic-gate 			sep = s_strdup(str - 1);
23637c478bd9Sstevel@tonic-gate 			*str = save;
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 			while (*str == ' ' || *str == '\t')
23667c478bd9Sstevel@tonic-gate 				str++;
23677c478bd9Sstevel@tonic-gate 			if (*str == '\0')
23687c478bd9Sstevel@tonic-gate 				arg = NULL;
23697c478bd9Sstevel@tonic-gate 			else
23707c478bd9Sstevel@tonic-gate 				arg = s_strdup(str);
23717c478bd9Sstevel@tonic-gate 		}
23727c478bd9Sstevel@tonic-gate 	}
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate 	lp->cmd = cmd;
23777c478bd9Sstevel@tonic-gate 	lp->sep = sep;
23787c478bd9Sstevel@tonic-gate 	lp->arg = arg;
23797c478bd9Sstevel@tonic-gate 	lp->line = line;
23807c478bd9Sstevel@tonic-gate 	lp->lineNum = ++(*lineNum);
23817c478bd9Sstevel@tonic-gate 	if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) {
23827c478bd9Sstevel@tonic-gate 		lp->entryNum = ++(*entryNum);
23837c478bd9Sstevel@tonic-gate 		lp->flags = BAM_TITLE;
23847c478bd9Sstevel@tonic-gate 		if (prev && prev->flags == BAM_COMMENT &&
23858c1b6884Sszhou 		    prev->arg && strcmp(prev->arg, BAM_HDR) == 0) {
23867c478bd9Sstevel@tonic-gate 			prev->entryNum = lp->entryNum;
23878c1b6884Sszhou 			curr_ent = boot_entry_new(mp, prev, lp);
23888c1b6884Sszhou 		} else {
23898c1b6884Sszhou 			curr_ent = boot_entry_new(mp, lp, lp);
23908c1b6884Sszhou 		}
23917c478bd9Sstevel@tonic-gate 	} else if (flag != BAM_INVALID) {
23927c478bd9Sstevel@tonic-gate 		/*
23937c478bd9Sstevel@tonic-gate 		 * For header comments, the entry# is "fixed up"
23947c478bd9Sstevel@tonic-gate 		 * by the subsequent title
23957c478bd9Sstevel@tonic-gate 		 */
23967c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
23977c478bd9Sstevel@tonic-gate 		lp->flags = flag;
23987c478bd9Sstevel@tonic-gate 	} else {
23997c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
24007c478bd9Sstevel@tonic-gate 		lp->flags = (*entryNum == ENTRY_INIT) ? BAM_GLOBAL : BAM_ENTRY;
24017c478bd9Sstevel@tonic-gate 	}
24027c478bd9Sstevel@tonic-gate 
24038c1b6884Sszhou 	/* record default, old default, and entry line ranges */
24048c1b6884Sszhou 	if (lp->flags == BAM_GLOBAL &&
24058c1b6884Sszhou 	    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) {
24068c1b6884Sszhou 		mp->curdefault = lp;
24078c1b6884Sszhou 	} else if (lp->flags == BAM_COMMENT &&
24088c1b6884Sszhou 	    strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) {
24098c1b6884Sszhou 		mp->olddefault = lp;
24108c1b6884Sszhou 	} else if (lp->flags == BAM_ENTRY ||
24118c1b6884Sszhou 	    (lp->flags == BAM_COMMENT && strcmp(lp->arg, BAM_FTR) == 0)) {
24128c1b6884Sszhou 		boot_entry_addline(curr_ent, lp);
24138c1b6884Sszhou 	}
24147c478bd9Sstevel@tonic-gate 	append_line(mp, lp);
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 	prev = lp;
24177c478bd9Sstevel@tonic-gate }
24187c478bd9Sstevel@tonic-gate 
241940541d5dSvikram static void
242040541d5dSvikram update_numbering(menu_t *mp)
242140541d5dSvikram {
242240541d5dSvikram 	int lineNum;
242340541d5dSvikram 	int entryNum;
242440541d5dSvikram 	int old_default_value;
242540541d5dSvikram 	line_t *lp, *prev, *default_lp, *default_entry;
242640541d5dSvikram 	char buf[PATH_MAX];
242740541d5dSvikram 
242840541d5dSvikram 	if (mp->start == NULL) {
242940541d5dSvikram 		return;
243040541d5dSvikram 	}
243140541d5dSvikram 
243240541d5dSvikram 	lineNum = LINE_INIT;
243340541d5dSvikram 	entryNum = ENTRY_INIT;
243440541d5dSvikram 	old_default_value = ENTRY_INIT;
243540541d5dSvikram 	lp = default_lp = default_entry = NULL;
243640541d5dSvikram 
243740541d5dSvikram 	prev = NULL;
243840541d5dSvikram 	for (lp = mp->start; lp; prev = lp, lp = lp->next) {
243940541d5dSvikram 		lp->lineNum = ++lineNum;
244040541d5dSvikram 
244140541d5dSvikram 		/*
244240541d5dSvikram 		 * Get the value of the default command
244340541d5dSvikram 		 */
244440541d5dSvikram 		if (lp->entryNum == ENTRY_INIT && lp->cmd &&
244540541d5dSvikram 		    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 &&
244640541d5dSvikram 		    lp->arg) {
244740541d5dSvikram 			old_default_value = atoi(lp->arg);
244840541d5dSvikram 			default_lp = lp;
244940541d5dSvikram 		}
245040541d5dSvikram 
245140541d5dSvikram 		/*
245240541d5dSvikram 		 * If not boot entry, nothing else to fix for this
245340541d5dSvikram 		 * entry
245440541d5dSvikram 		 */
245540541d5dSvikram 		if (lp->entryNum == ENTRY_INIT)
245640541d5dSvikram 			continue;
245740541d5dSvikram 
245840541d5dSvikram 		/*
245940541d5dSvikram 		 * Record the position of the default entry.
246040541d5dSvikram 		 * The following works because global
246140541d5dSvikram 		 * commands like default and timeout should precede
246240541d5dSvikram 		 * actual boot entries, so old_default_value
246340541d5dSvikram 		 * is already known (or default cmd is missing).
246440541d5dSvikram 		 */
246540541d5dSvikram 		if (default_entry == NULL &&
246640541d5dSvikram 		    old_default_value != ENTRY_INIT &&
246740541d5dSvikram 		    lp->entryNum == old_default_value) {
246840541d5dSvikram 			default_entry = lp;
246940541d5dSvikram 		}
247040541d5dSvikram 
247140541d5dSvikram 		/*
247240541d5dSvikram 		 * Now fixup the entry number
247340541d5dSvikram 		 */
247440541d5dSvikram 		if (lp->cmd && strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) {
247540541d5dSvikram 			lp->entryNum = ++entryNum;
247640541d5dSvikram 			/* fixup the bootadm header */
247740541d5dSvikram 			if (prev && prev->flags == BAM_COMMENT &&
247840541d5dSvikram 			    prev->arg && strcmp(prev->arg, BAM_HDR) == 0) {
247940541d5dSvikram 				prev->entryNum = lp->entryNum;
248040541d5dSvikram 			}
248140541d5dSvikram 		} else {
248240541d5dSvikram 			lp->entryNum = entryNum;
248340541d5dSvikram 		}
248440541d5dSvikram 	}
248540541d5dSvikram 
248640541d5dSvikram 	/*
248740541d5dSvikram 	 * No default command in menu, simply return
248840541d5dSvikram 	 */
248940541d5dSvikram 	if (default_lp == NULL) {
249040541d5dSvikram 		return;
249140541d5dSvikram 	}
249240541d5dSvikram 
249340541d5dSvikram 	free(default_lp->arg);
249440541d5dSvikram 	free(default_lp->line);
249540541d5dSvikram 
249640541d5dSvikram 	if (default_entry == NULL) {
249740541d5dSvikram 		default_lp->arg = s_strdup("0");
249840541d5dSvikram 	} else {
249940541d5dSvikram 		(void) snprintf(buf, sizeof (buf), "%d",
250040541d5dSvikram 		    default_entry->entryNum);
250140541d5dSvikram 		default_lp->arg = s_strdup(buf);
250240541d5dSvikram 	}
250340541d5dSvikram 
250440541d5dSvikram 	/*
250540541d5dSvikram 	 * The following is required since only the line field gets
250640541d5dSvikram 	 * written back to menu.lst
250740541d5dSvikram 	 */
250840541d5dSvikram 	(void) snprintf(buf, sizeof (buf), "%s%s%s",
250940541d5dSvikram 	    menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg);
251040541d5dSvikram 	default_lp->line = s_strdup(buf);
251140541d5dSvikram }
251240541d5dSvikram 
251340541d5dSvikram 
25147c478bd9Sstevel@tonic-gate static menu_t *
25157c478bd9Sstevel@tonic-gate menu_read(char *menu_path)
25167c478bd9Sstevel@tonic-gate {
25177c478bd9Sstevel@tonic-gate 	FILE *fp;
25187c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE], *cp;
25197c478bd9Sstevel@tonic-gate 	menu_t *mp;
25207c478bd9Sstevel@tonic-gate 	int line, entry, len, n;
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate 	mp = s_calloc(1, sizeof (menu_t));
25237c478bd9Sstevel@tonic-gate 
25247c478bd9Sstevel@tonic-gate 	fp = fopen(menu_path, "r");
25257c478bd9Sstevel@tonic-gate 	if (fp == NULL) { /* Let the caller handle this error */
25267c478bd9Sstevel@tonic-gate 		return (mp);
25277c478bd9Sstevel@tonic-gate 	}
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate 	/* Note: GRUB boot entry number starts with 0 */
25317c478bd9Sstevel@tonic-gate 	line = LINE_INIT;
25327c478bd9Sstevel@tonic-gate 	entry = ENTRY_INIT;
25337c478bd9Sstevel@tonic-gate 	cp = buf;
25347c478bd9Sstevel@tonic-gate 	len = sizeof (buf);
25357c478bd9Sstevel@tonic-gate 	while (s_fgets(cp, len, fp) != NULL) {
25367c478bd9Sstevel@tonic-gate 		n = strlen(cp);
25377c478bd9Sstevel@tonic-gate 		if (cp[n - 1] == '\\') {
25387c478bd9Sstevel@tonic-gate 			len -= n - 1;
25397c478bd9Sstevel@tonic-gate 			assert(len >= 2);
25407c478bd9Sstevel@tonic-gate 			cp += n - 1;
25417c478bd9Sstevel@tonic-gate 			continue;
25427c478bd9Sstevel@tonic-gate 		}
25437c478bd9Sstevel@tonic-gate 		line_parser(mp, buf, &line, &entry);
25447c478bd9Sstevel@tonic-gate 		cp = buf;
25457c478bd9Sstevel@tonic-gate 		len = sizeof (buf);
25467c478bd9Sstevel@tonic-gate 	}
25477c478bd9Sstevel@tonic-gate 
25487c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
25497c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, menu_path, strerror(errno));
25507c478bd9Sstevel@tonic-gate 	}
25517c478bd9Sstevel@tonic-gate 
25527c478bd9Sstevel@tonic-gate 	return (mp);
25537c478bd9Sstevel@tonic-gate }
25547c478bd9Sstevel@tonic-gate 
25557c478bd9Sstevel@tonic-gate static error_t
25567c478bd9Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title)
25577c478bd9Sstevel@tonic-gate {
25587c478bd9Sstevel@tonic-gate 	char *eq;
25597c478bd9Sstevel@tonic-gate 	char *opt_dup;
25607c478bd9Sstevel@tonic-gate 	int entryNum;
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 	assert(mp);
25637c478bd9Sstevel@tonic-gate 	assert(mp->start);
25647c478bd9Sstevel@tonic-gate 	assert(opt);
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate 	opt_dup = s_strdup(opt);
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 	if (entry)
25697c478bd9Sstevel@tonic-gate 		*entry = ENTRY_INIT;
25707c478bd9Sstevel@tonic-gate 	if (title)
25717c478bd9Sstevel@tonic-gate 		*title = NULL;
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 	eq = strchr(opt_dup, '=');
25747c478bd9Sstevel@tonic-gate 	if (eq == NULL) {
25757c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
25767c478bd9Sstevel@tonic-gate 		free(opt_dup);
25777c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
25787c478bd9Sstevel@tonic-gate 	}
25797c478bd9Sstevel@tonic-gate 
25807c478bd9Sstevel@tonic-gate 	*eq = '\0';
25817c478bd9Sstevel@tonic-gate 	if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) {
25827c478bd9Sstevel@tonic-gate 		assert(mp->end);
25837c478bd9Sstevel@tonic-gate 		entryNum = s_strtol(eq + 1);
25847c478bd9Sstevel@tonic-gate 		if (entryNum < 0 || entryNum > mp->end->entryNum) {
25857c478bd9Sstevel@tonic-gate 			bam_error(INVALID_ENTRY, eq + 1);
25867c478bd9Sstevel@tonic-gate 			free(opt_dup);
25877c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
25887c478bd9Sstevel@tonic-gate 		}
25897c478bd9Sstevel@tonic-gate 		*entry = entryNum;
25907c478bd9Sstevel@tonic-gate 	} else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) {
25917c478bd9Sstevel@tonic-gate 		*title = opt + (eq - opt_dup) + 1;
25927c478bd9Sstevel@tonic-gate 	} else {
25937c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
25947c478bd9Sstevel@tonic-gate 		free(opt_dup);
25957c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
25967c478bd9Sstevel@tonic-gate 	}
25977c478bd9Sstevel@tonic-gate 
25987c478bd9Sstevel@tonic-gate 	free(opt_dup);
25997c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
26007c478bd9Sstevel@tonic-gate }
26017c478bd9Sstevel@tonic-gate 
26027c478bd9Sstevel@tonic-gate /*
26037c478bd9Sstevel@tonic-gate  * If invoked with no titles/entries (opt == NULL)
26047c478bd9Sstevel@tonic-gate  * only title lines in file are printed.
26057c478bd9Sstevel@tonic-gate  *
26067c478bd9Sstevel@tonic-gate  * If invoked with a title or entry #, all
26077c478bd9Sstevel@tonic-gate  * lines in *every* matching entry are listed
26087c478bd9Sstevel@tonic-gate  */
26097c478bd9Sstevel@tonic-gate static error_t
26107c478bd9Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt)
26117c478bd9Sstevel@tonic-gate {
26127c478bd9Sstevel@tonic-gate 	line_t *lp;
26137c478bd9Sstevel@tonic-gate 	int entry = ENTRY_INIT;
26147c478bd9Sstevel@tonic-gate 	int found;
26157c478bd9Sstevel@tonic-gate 	char *title = NULL;
26167c478bd9Sstevel@tonic-gate 
26177c478bd9Sstevel@tonic-gate 	assert(mp);
26187c478bd9Sstevel@tonic-gate 	assert(menu_path);
26197c478bd9Sstevel@tonic-gate 
26207c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
26217c478bd9Sstevel@tonic-gate 		bam_error(NO_MENU, menu_path);
26227c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
26237c478bd9Sstevel@tonic-gate 	}
26247c478bd9Sstevel@tonic-gate 
26257c478bd9Sstevel@tonic-gate 	if (opt != NULL) {
26267c478bd9Sstevel@tonic-gate 		if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
26277c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
26287c478bd9Sstevel@tonic-gate 		}
26297c478bd9Sstevel@tonic-gate 		assert((entry != ENTRY_INIT) ^ (title != NULL));
26307c478bd9Sstevel@tonic-gate 	} else {
26317c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0);
26327c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0);
26337c478bd9Sstevel@tonic-gate 	}
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate 	found = 0;
26367c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
26377c478bd9Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY)
26387c478bd9Sstevel@tonic-gate 			continue;
26397c478bd9Sstevel@tonic-gate 		if (opt == NULL && lp->flags == BAM_TITLE) {
26407c478bd9Sstevel@tonic-gate 			bam_print(PRINT_TITLE, lp->entryNum,
26417c478bd9Sstevel@tonic-gate 			    lp->arg);
26427c478bd9Sstevel@tonic-gate 			found = 1;
26437c478bd9Sstevel@tonic-gate 			continue;
26447c478bd9Sstevel@tonic-gate 		}
26457c478bd9Sstevel@tonic-gate 		if (entry != ENTRY_INIT && lp->entryNum == entry) {
26467c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
26477c478bd9Sstevel@tonic-gate 			found = 1;
26487c478bd9Sstevel@tonic-gate 			continue;
26497c478bd9Sstevel@tonic-gate 		}
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 		/*
26527c478bd9Sstevel@tonic-gate 		 * We set the entry value here so that all lines
26537c478bd9Sstevel@tonic-gate 		 * in entry get printed. If we subsequently match
26547c478bd9Sstevel@tonic-gate 		 * title in other entries, all lines in those
26557c478bd9Sstevel@tonic-gate 		 * entries get printed as well.
26567c478bd9Sstevel@tonic-gate 		 */
26577c478bd9Sstevel@tonic-gate 		if (title && lp->flags == BAM_TITLE && lp->arg &&
26587c478bd9Sstevel@tonic-gate 		    strncmp(title, lp->arg, strlen(title)) == 0) {
26597c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
26607c478bd9Sstevel@tonic-gate 			entry = lp->entryNum;
26617c478bd9Sstevel@tonic-gate 			found = 1;
26627c478bd9Sstevel@tonic-gate 			continue;
26637c478bd9Sstevel@tonic-gate 		}
26647c478bd9Sstevel@tonic-gate 	}
26657c478bd9Sstevel@tonic-gate 
26667c478bd9Sstevel@tonic-gate 	if (!found) {
26677c478bd9Sstevel@tonic-gate 		bam_error(NO_MATCH_ENTRY);
26687c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
26697c478bd9Sstevel@tonic-gate 	}
26707c478bd9Sstevel@tonic-gate 
26717c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
26727c478bd9Sstevel@tonic-gate }
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate static int
26757c478bd9Sstevel@tonic-gate add_boot_entry(menu_t *mp,
26767c478bd9Sstevel@tonic-gate 	char *title,
26777c478bd9Sstevel@tonic-gate 	char *root,
26787c478bd9Sstevel@tonic-gate 	char *kernel,
26797c478bd9Sstevel@tonic-gate 	char *module)
26807c478bd9Sstevel@tonic-gate {
26817c478bd9Sstevel@tonic-gate 	int lineNum, entryNum;
26827c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 	assert(mp);
26857c478bd9Sstevel@tonic-gate 
26867c478bd9Sstevel@tonic-gate 	if (title == NULL) {
2687ee3b8144Sszhou 		title = "Solaris";	/* default to Solaris */
26887c478bd9Sstevel@tonic-gate 	}
26897c478bd9Sstevel@tonic-gate 	if (kernel == NULL) {
26907c478bd9Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]);
26917c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
26927c478bd9Sstevel@tonic-gate 	}
26937c478bd9Sstevel@tonic-gate 	if (module == NULL) {
26947c478bd9Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]);
26957c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
26967c478bd9Sstevel@tonic-gate 	}
26977c478bd9Sstevel@tonic-gate 
26987c478bd9Sstevel@tonic-gate 	if (mp->start) {
26997c478bd9Sstevel@tonic-gate 		lineNum = mp->end->lineNum;
27007c478bd9Sstevel@tonic-gate 		entryNum = mp->end->entryNum;
27017c478bd9Sstevel@tonic-gate 	} else {
27027c478bd9Sstevel@tonic-gate 		lineNum = LINE_INIT;
27037c478bd9Sstevel@tonic-gate 		entryNum = ENTRY_INIT;
27047c478bd9Sstevel@tonic-gate 	}
27057c478bd9Sstevel@tonic-gate 
27067c478bd9Sstevel@tonic-gate 	/*
27077c478bd9Sstevel@tonic-gate 	 * No separator for comment (HDR/FTR) commands
27087c478bd9Sstevel@tonic-gate 	 * The syntax for comments is #<comment>
27097c478bd9Sstevel@tonic-gate 	 */
27107c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
27117c478bd9Sstevel@tonic-gate 	    menu_cmds[COMMENT_CMD], BAM_HDR);
27128c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
27137c478bd9Sstevel@tonic-gate 
27147c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
27157c478bd9Sstevel@tonic-gate 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
27168c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
27177c478bd9Sstevel@tonic-gate 
27188c1b6884Sszhou 	if (root) {
27198c1b6884Sszhou 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
27208c1b6884Sszhou 		    menu_cmds[ROOT_CMD], menu_cmds[SEP_CMD], root);
27218c1b6884Sszhou 		line_parser(mp, linebuf, &lineNum, &entryNum);
27227c478bd9Sstevel@tonic-gate 	}
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
27257c478bd9Sstevel@tonic-gate 	    menu_cmds[KERNEL_CMD], menu_cmds[SEP_CMD], kernel);
27268c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
27297c478bd9Sstevel@tonic-gate 	    menu_cmds[MODULE_CMD], menu_cmds[SEP_CMD], module);
27308c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
27317c478bd9Sstevel@tonic-gate 
27327c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
27337c478bd9Sstevel@tonic-gate 	    menu_cmds[COMMENT_CMD], BAM_FTR);
27348c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
27357c478bd9Sstevel@tonic-gate 
27367c478bd9Sstevel@tonic-gate 	return (entryNum);
27377c478bd9Sstevel@tonic-gate }
27387c478bd9Sstevel@tonic-gate 
27397c478bd9Sstevel@tonic-gate static error_t
27407c478bd9Sstevel@tonic-gate do_delete(menu_t *mp, int entryNum)
27417c478bd9Sstevel@tonic-gate {
27428c1b6884Sszhou 	line_t *lp, *freed;
27438c1b6884Sszhou 	entry_t *ent, *tmp;
27447c478bd9Sstevel@tonic-gate 	int deleted;
27457c478bd9Sstevel@tonic-gate 
27467c478bd9Sstevel@tonic-gate 	assert(entryNum != ENTRY_INIT);
27477c478bd9Sstevel@tonic-gate 
27488c1b6884Sszhou 	ent = mp->entries;
27498c1b6884Sszhou 	while (ent) {
27508c1b6884Sszhou 		lp = ent->start;
27518c1b6884Sszhou 		/* check entry number and make sure it's a bootadm entry */
27528c1b6884Sszhou 		if (lp->flags != BAM_COMMENT ||
27538c1b6884Sszhou 		    strcmp(lp->arg, BAM_HDR) != 0 ||
27548c1b6884Sszhou 		    (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) {
27558c1b6884Sszhou 			ent = ent->next;
27567c478bd9Sstevel@tonic-gate 			continue;
27577c478bd9Sstevel@tonic-gate 		}
27587c478bd9Sstevel@tonic-gate 
27598c1b6884Sszhou 		/* free the entry content */
27608c1b6884Sszhou 		do {
27618c1b6884Sszhou 			freed = lp;
27628c1b6884Sszhou 			lp = lp->next;	/* prev stays the same */
27638c1b6884Sszhou 			unlink_line(mp, freed);
27648c1b6884Sszhou 			line_free(freed);
27658c1b6884Sszhou 		} while (freed != ent->end);
27668c1b6884Sszhou 
27678c1b6884Sszhou 		/* free the entry_t structure */
27688c1b6884Sszhou 		tmp = ent;
27698c1b6884Sszhou 		ent = ent->next;
27708c1b6884Sszhou 		if (tmp->prev)
27718c1b6884Sszhou 			tmp->prev->next = ent;
27727c478bd9Sstevel@tonic-gate 		else
27738c1b6884Sszhou 			mp->entries = ent;
27748c1b6884Sszhou 		if (ent)
27758c1b6884Sszhou 			ent->prev = tmp->prev;
27767c478bd9Sstevel@tonic-gate 		deleted = 1;
27777c478bd9Sstevel@tonic-gate 	}
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate 	if (!deleted && entryNum != ALL_ENTRIES) {
27807c478bd9Sstevel@tonic-gate 		bam_error(NO_BOOTADM_MATCH);
27817c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27827c478bd9Sstevel@tonic-gate 	}
27837c478bd9Sstevel@tonic-gate 
278440541d5dSvikram 	/*
278540541d5dSvikram 	 * Now that we have deleted an entry, update
278640541d5dSvikram 	 * the entry numbering and the default cmd.
278740541d5dSvikram 	 */
278840541d5dSvikram 	update_numbering(mp);
278940541d5dSvikram 
27907c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
27917c478bd9Sstevel@tonic-gate }
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate static error_t
27947c478bd9Sstevel@tonic-gate delete_all_entries(menu_t *mp, char *menu_path, char *opt)
27957c478bd9Sstevel@tonic-gate {
27967c478bd9Sstevel@tonic-gate 	assert(mp);
27977c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
28007c478bd9Sstevel@tonic-gate 		bam_print(EMPTY_FILE, menu_path);
28017c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
28027c478bd9Sstevel@tonic-gate 	}
28037c478bd9Sstevel@tonic-gate 
28047c478bd9Sstevel@tonic-gate 	if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) {
28057c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
28067c478bd9Sstevel@tonic-gate 	}
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
28097c478bd9Sstevel@tonic-gate }
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate static FILE *
28128c1b6884Sszhou open_diskmap(char *root)
28137c478bd9Sstevel@tonic-gate {
28147c478bd9Sstevel@tonic-gate 	FILE *fp;
28157c478bd9Sstevel@tonic-gate 	char cmd[PATH_MAX];
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate 	/* make sure we have a map file */
28187c478bd9Sstevel@tonic-gate 	fp = fopen(GRUBDISK_MAP, "r");
28197c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
28207c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd),
28218c1b6884Sszhou 		    "%s%s > /dev/null", root, CREATE_DISKMAP);
28227c478bd9Sstevel@tonic-gate 		(void) system(cmd);
28237c478bd9Sstevel@tonic-gate 		fp = fopen(GRUBDISK_MAP, "r");
28247c478bd9Sstevel@tonic-gate 	}
28257c478bd9Sstevel@tonic-gate 	return (fp);
28267c478bd9Sstevel@tonic-gate }
28277c478bd9Sstevel@tonic-gate 
28287c478bd9Sstevel@tonic-gate #define	SECTOR_SIZE	512
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate static int
28317c478bd9Sstevel@tonic-gate get_partition(char *device)
28327c478bd9Sstevel@tonic-gate {
28337c478bd9Sstevel@tonic-gate 	int i, fd, is_pcfs, partno = -1;
28347c478bd9Sstevel@tonic-gate 	struct mboot *mboot;
28357c478bd9Sstevel@tonic-gate 	char boot_sect[SECTOR_SIZE];
28367c478bd9Sstevel@tonic-gate 	char *wholedisk, *slice;
28377c478bd9Sstevel@tonic-gate 
28387c478bd9Sstevel@tonic-gate 	/* form whole disk (p0) */
28397c478bd9Sstevel@tonic-gate 	slice = device + strlen(device) - 2;
28407c478bd9Sstevel@tonic-gate 	is_pcfs = (*slice != 's');
28417c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
28427c478bd9Sstevel@tonic-gate 		*slice = '\0';
28437c478bd9Sstevel@tonic-gate 	wholedisk = s_calloc(1, strlen(device) + 3);
28447c478bd9Sstevel@tonic-gate 	(void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device);
28457c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
28467c478bd9Sstevel@tonic-gate 		*slice = 's';
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate 	/* read boot sector */
28497c478bd9Sstevel@tonic-gate 	fd = open(wholedisk, O_RDONLY);
28507c478bd9Sstevel@tonic-gate 	free(wholedisk);
28517c478bd9Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
28527c478bd9Sstevel@tonic-gate 		return (partno);
28537c478bd9Sstevel@tonic-gate 	}
28547c478bd9Sstevel@tonic-gate 	(void) close(fd);
28557c478bd9Sstevel@tonic-gate 
28567c478bd9Sstevel@tonic-gate 	/* parse fdisk table */
28577c478bd9Sstevel@tonic-gate 	mboot = (struct mboot *)((void *)boot_sect);
28587c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
28597c478bd9Sstevel@tonic-gate 		struct ipart *part =
28607c478bd9Sstevel@tonic-gate 		    (struct ipart *)(uintptr_t)mboot->parts + i;
28617c478bd9Sstevel@tonic-gate 		if (is_pcfs) {	/* looking for solaris boot part */
28627c478bd9Sstevel@tonic-gate 			if (part->systid == 0xbe) {
28637c478bd9Sstevel@tonic-gate 				partno = i;
28647c478bd9Sstevel@tonic-gate 				break;
28657c478bd9Sstevel@tonic-gate 			}
28667c478bd9Sstevel@tonic-gate 		} else {	/* look for solaris partition, old and new */
28677c478bd9Sstevel@tonic-gate 			if (part->systid == SUNIXOS ||
28687c478bd9Sstevel@tonic-gate 			    part->systid == SUNIXOS2) {
28697c478bd9Sstevel@tonic-gate 				partno = i;
28707c478bd9Sstevel@tonic-gate 				break;
28717c478bd9Sstevel@tonic-gate 			}
28727c478bd9Sstevel@tonic-gate 		}
28737c478bd9Sstevel@tonic-gate 	}
28747c478bd9Sstevel@tonic-gate 	return (partno);
28757c478bd9Sstevel@tonic-gate }
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate static char *
28787c478bd9Sstevel@tonic-gate get_grubdisk(char *rootdev, FILE *fp, int on_bootdev)
28797c478bd9Sstevel@tonic-gate {
28807c478bd9Sstevel@tonic-gate 	char *grubdisk;	/* (hd#,#,#) */
28817c478bd9Sstevel@tonic-gate 	char *slice;
28827c478bd9Sstevel@tonic-gate 	char *grubhd;
28837c478bd9Sstevel@tonic-gate 	int fdiskpart;
28847c478bd9Sstevel@tonic-gate 	int found = 0;
28857c478bd9Sstevel@tonic-gate 	char *devname, *ctdname = strstr(rootdev, "dsk/");
28867c478bd9Sstevel@tonic-gate 	char linebuf[PATH_MAX];
28877c478bd9Sstevel@tonic-gate 
28887c478bd9Sstevel@tonic-gate 	if (ctdname == NULL)
28897c478bd9Sstevel@tonic-gate 		return (NULL);
28907c478bd9Sstevel@tonic-gate 
28917c478bd9Sstevel@tonic-gate 	ctdname += strlen("dsk/");
28927c478bd9Sstevel@tonic-gate 	slice = strrchr(ctdname, 's');
28937c478bd9Sstevel@tonic-gate 	if (slice)
28947c478bd9Sstevel@tonic-gate 		*slice = '\0';
28957c478bd9Sstevel@tonic-gate 
28967c478bd9Sstevel@tonic-gate 	rewind(fp);
28977c478bd9Sstevel@tonic-gate 	while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) {
28987c478bd9Sstevel@tonic-gate 		grubhd = strtok(linebuf, " \t\n");
28997c478bd9Sstevel@tonic-gate 		if (grubhd)
29007c478bd9Sstevel@tonic-gate 			devname = strtok(NULL, " \t\n");
29017c478bd9Sstevel@tonic-gate 		else
29027c478bd9Sstevel@tonic-gate 			devname = NULL;
29037c478bd9Sstevel@tonic-gate 		if (devname && strcmp(devname, ctdname) == 0) {
29047c478bd9Sstevel@tonic-gate 			found = 1;
29057c478bd9Sstevel@tonic-gate 			break;
29067c478bd9Sstevel@tonic-gate 		}
29077c478bd9Sstevel@tonic-gate 	}
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate 	if (slice)
29107c478bd9Sstevel@tonic-gate 		*slice = 's';
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate 	if (found == 0) {
29137c478bd9Sstevel@tonic-gate 		if (bam_verbose)
29147c478bd9Sstevel@tonic-gate 			bam_print(DISKMAP_FAIL_NONFATAL, rootdev);
29157c478bd9Sstevel@tonic-gate 		grubhd = "0";	/* assume disk 0 if can't match */
29167c478bd9Sstevel@tonic-gate 	}
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate 	fdiskpart = get_partition(rootdev);
29197c478bd9Sstevel@tonic-gate 	if (fdiskpart == -1)
29207c478bd9Sstevel@tonic-gate 		return (NULL);
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate 	grubdisk = s_calloc(1, 10);
29237c478bd9Sstevel@tonic-gate 	if (slice) {
29247c478bd9Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d,%c)",
29257c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart, slice[1] + 'a' - '0');
29267c478bd9Sstevel@tonic-gate 	} else
29277c478bd9Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d)",
29287c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart);
29297c478bd9Sstevel@tonic-gate 
29307c478bd9Sstevel@tonic-gate 	/* if root not on bootdev, change GRUB disk to 0 */
29317c478bd9Sstevel@tonic-gate 	if (!on_bootdev)
29327c478bd9Sstevel@tonic-gate 		grubdisk[3] = '0';
29337c478bd9Sstevel@tonic-gate 	return (grubdisk);
29347c478bd9Sstevel@tonic-gate }
29357c478bd9Sstevel@tonic-gate 
2936ee3b8144Sszhou static char *
2937ee3b8144Sszhou get_title(char *rootdir)
29387c478bd9Sstevel@tonic-gate {
29397c478bd9Sstevel@tonic-gate 	static char title[80];	/* from /etc/release */
29408c1b6884Sszhou 	char *cp = NULL, release[PATH_MAX];
29417c478bd9Sstevel@tonic-gate 	FILE *fp;
29427c478bd9Sstevel@tonic-gate 
29437c478bd9Sstevel@tonic-gate 	/* open the /etc/release file */
29447c478bd9Sstevel@tonic-gate 	(void) snprintf(release, sizeof (release), "%s/etc/release", rootdir);
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 	fp = fopen(release, "r");
29477c478bd9Sstevel@tonic-gate 	if (fp == NULL)
2948ee3b8144Sszhou 		return (NULL);
29497c478bd9Sstevel@tonic-gate 
29507c478bd9Sstevel@tonic-gate 	while (s_fgets(title, sizeof (title), fp) != NULL) {
29517c478bd9Sstevel@tonic-gate 		cp = strstr(title, "Solaris");
29527c478bd9Sstevel@tonic-gate 		if (cp)
29537c478bd9Sstevel@tonic-gate 			break;
29547c478bd9Sstevel@tonic-gate 	}
29557c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
29568c1b6884Sszhou 	return (cp == NULL ? "Solaris" : cp);
29577c478bd9Sstevel@tonic-gate }
29587c478bd9Sstevel@tonic-gate 
29597c478bd9Sstevel@tonic-gate static char *
29607c478bd9Sstevel@tonic-gate get_special(char *mountp)
29617c478bd9Sstevel@tonic-gate {
29627c478bd9Sstevel@tonic-gate 	FILE *mntfp;
29637c478bd9Sstevel@tonic-gate 	struct mnttab mp = {0}, mpref = {0};
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate 	mntfp = fopen(MNTTAB, "r");
29667c478bd9Sstevel@tonic-gate 	if (mntfp == NULL) {
29677c478bd9Sstevel@tonic-gate 		return (0);
29687c478bd9Sstevel@tonic-gate 	}
29697c478bd9Sstevel@tonic-gate 
29707c478bd9Sstevel@tonic-gate 	if (*mountp == '\0')
29717c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = "/";
29727c478bd9Sstevel@tonic-gate 	else
29737c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = mountp;
29747c478bd9Sstevel@tonic-gate 	if (getmntany(mntfp, &mp, &mpref) != 0) {
29757c478bd9Sstevel@tonic-gate 		(void) fclose(mntfp);
29767c478bd9Sstevel@tonic-gate 		return (NULL);
29777c478bd9Sstevel@tonic-gate 	}
29787c478bd9Sstevel@tonic-gate 	(void) fclose(mntfp);
29797c478bd9Sstevel@tonic-gate 
29807c478bd9Sstevel@tonic-gate 	return (s_strdup(mp.mnt_special));
29817c478bd9Sstevel@tonic-gate }
29827c478bd9Sstevel@tonic-gate 
29837c478bd9Sstevel@tonic-gate static char *
29847c478bd9Sstevel@tonic-gate os_to_grubdisk(char *osdisk, int on_bootdev)
29857c478bd9Sstevel@tonic-gate {
29867c478bd9Sstevel@tonic-gate 	FILE *fp;
29877c478bd9Sstevel@tonic-gate 	char *grubdisk;
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
29908c1b6884Sszhou 	fp = open_diskmap("");
29917c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
29927c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdisk);
29937c478bd9Sstevel@tonic-gate 		return (NULL);
29947c478bd9Sstevel@tonic-gate 	}
29957c478bd9Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdisk, fp, on_bootdev);
29967c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
29977c478bd9Sstevel@tonic-gate 	return (grubdisk);
29987c478bd9Sstevel@tonic-gate }
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate /*
30017c478bd9Sstevel@tonic-gate  * Check if root is on the boot device
30027c478bd9Sstevel@tonic-gate  * Return 0 (false) on error
30037c478bd9Sstevel@tonic-gate  */
30047c478bd9Sstevel@tonic-gate static int
30057c478bd9Sstevel@tonic-gate menu_on_bootdev(char *menu_root, FILE *fp)
30067c478bd9Sstevel@tonic-gate {
30077c478bd9Sstevel@tonic-gate 	int ret;
30087c478bd9Sstevel@tonic-gate 	char *grubhd, *bootp, *special;
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate 	special = get_special(menu_root);
30117c478bd9Sstevel@tonic-gate 	if (special == NULL)
30127c478bd9Sstevel@tonic-gate 		return (0);
30137c478bd9Sstevel@tonic-gate 	bootp = strstr(special, "p0:boot");
30147c478bd9Sstevel@tonic-gate 	if (bootp)
30157c478bd9Sstevel@tonic-gate 		*bootp = '\0';
30167c478bd9Sstevel@tonic-gate 	grubhd = get_grubdisk(special, fp, 1);
30177c478bd9Sstevel@tonic-gate 	free(special);
30187c478bd9Sstevel@tonic-gate 
30197c478bd9Sstevel@tonic-gate 	if (grubhd == NULL)
30207c478bd9Sstevel@tonic-gate 		return (0);
30217c478bd9Sstevel@tonic-gate 	ret = grubhd[3] == '0';
30227c478bd9Sstevel@tonic-gate 	free(grubhd);
30237c478bd9Sstevel@tonic-gate 	return (ret);
30247c478bd9Sstevel@tonic-gate }
30257c478bd9Sstevel@tonic-gate 
30268c1b6884Sszhou /*
30278c1b6884Sszhou  * look for matching bootadm entry with specified parameters
30288c1b6884Sszhou  * Here are the rules (based on existing usage):
30298c1b6884Sszhou  * - If title is specified, match on title only
30308c1b6884Sszhou  * - Else, match on grubdisk and module (don't care about kernel line).
30318c1b6884Sszhou  *   note that, if root_opt is non-zero, the absence of root line is
30328c1b6884Sszhou  *   considered a match.
30338c1b6884Sszhou  */
30348c1b6884Sszhou static entry_t *
30358c1b6884Sszhou find_boot_entry(menu_t *mp, char *title, char *root, char *module,
30368c1b6884Sszhou     int root_opt, int *entry_num)
30378c1b6884Sszhou {
30388c1b6884Sszhou 	int i;
30398c1b6884Sszhou 	line_t *lp;
30408c1b6884Sszhou 	entry_t *ent;
30418c1b6884Sszhou 
30428c1b6884Sszhou 	/* find matching entry */
30438c1b6884Sszhou 	for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) {
30448c1b6884Sszhou 		lp = ent->start;
30458c1b6884Sszhou 
30468c1b6884Sszhou 		/* first line of entry must be bootadm comment */
30478c1b6884Sszhou 		lp = ent->start;
30488c1b6884Sszhou 		if (lp->flags != BAM_COMMENT || strcmp(lp->arg, BAM_HDR) != 0) {
30498c1b6884Sszhou 			continue;
30508c1b6884Sszhou 		}
30518c1b6884Sszhou 
30528c1b6884Sszhou 		/* advance to title line */
30538c1b6884Sszhou 		lp = lp->next;
30548c1b6884Sszhou 		if (title) {
30558c1b6884Sszhou 			if (lp->flags == BAM_TITLE && lp->arg &&
30568c1b6884Sszhou 			    strcmp(lp->arg, title) == 0)
30578c1b6884Sszhou 				break;
30588c1b6884Sszhou 			continue;	/* check title only */
30598c1b6884Sszhou 		}
30608c1b6884Sszhou 
30618c1b6884Sszhou 		lp = lp->next;	/* advance to root line */
30628c1b6884Sszhou 		if (lp == NULL || strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
30638c1b6884Sszhou 			/* root command found, match grub disk */
30648c1b6884Sszhou 			if (strcmp(lp->arg, root) != 0) {
30658c1b6884Sszhou 				continue;
30668c1b6884Sszhou 			}
30678c1b6884Sszhou 			lp = lp->next;	/* advance to kernel line */
30688c1b6884Sszhou 		} else {
30698c1b6884Sszhou 			/* no root command, see if root is optional */
30708c1b6884Sszhou 			if (root_opt == 0) {
30718c1b6884Sszhou 				continue;
30728c1b6884Sszhou 			}
30738c1b6884Sszhou 		}
30748c1b6884Sszhou 
30758c1b6884Sszhou 		if (lp == NULL || lp->next == NULL) {
30768c1b6884Sszhou 			continue;
30778c1b6884Sszhou 		}
30788c1b6884Sszhou 
30798c1b6884Sszhou 		/* check for matching module entry (failsafe or normal) */
30808c1b6884Sszhou 		lp = lp->next;	/* advance to module line */
30818c1b6884Sszhou 		if (strcmp(lp->cmd, menu_cmds[MODULE_CMD]) != 0 ||
30828c1b6884Sszhou 		    strcmp(lp->arg, module) != 0) {
30838c1b6884Sszhou 			continue;
30848c1b6884Sszhou 		}
30858c1b6884Sszhou 		break;	/* match found */
30868c1b6884Sszhou 	}
30878c1b6884Sszhou 
30888c1b6884Sszhou 	*entry_num = i;
30898c1b6884Sszhou 	return (ent);
30908c1b6884Sszhou }
30918c1b6884Sszhou 
30928c1b6884Sszhou static int
30938c1b6884Sszhou update_boot_entry(menu_t *mp, char *title, char *root, char *kernel,
30948c1b6884Sszhou     char *module, int root_opt)
30958c1b6884Sszhou {
30968c1b6884Sszhou 	int i;
30978c1b6884Sszhou 	entry_t *ent;
30988c1b6884Sszhou 	line_t *lp;
30998c1b6884Sszhou 	char linebuf[BAM_MAXLINE];
31008c1b6884Sszhou 
31018c1b6884Sszhou 	/* note: don't match on title, it's updated on upgrade */
31028c1b6884Sszhou 	ent = find_boot_entry(mp, NULL, root, module, root_opt, &i);
31038c1b6884Sszhou 	if (ent == NULL)
31048c1b6884Sszhou 		return (add_boot_entry(mp, title, root_opt ? NULL : root,
31058c1b6884Sszhou 		    kernel, module));
31068c1b6884Sszhou 
31078c1b6884Sszhou 	/* replace title of exiting entry and delete root line */
31088c1b6884Sszhou 	lp = ent->start;
31098c1b6884Sszhou 	lp = lp->next;	/* title line */
31108c1b6884Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
31118c1b6884Sszhou 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
31128c1b6884Sszhou 	free(lp->arg);
31138c1b6884Sszhou 	free(lp->line);
31148c1b6884Sszhou 	lp->arg = s_strdup(title);
31158c1b6884Sszhou 	lp->line = s_strdup(linebuf);
31168c1b6884Sszhou 
31178c1b6884Sszhou 	lp = lp->next;	/* root line */
31188c1b6884Sszhou 	if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
31198c1b6884Sszhou 		if (root_opt) {		/* root line not needed */
31208c1b6884Sszhou 			line_t *tmp = lp;
31218c1b6884Sszhou 			lp = lp->next;
31228c1b6884Sszhou 			unlink_line(mp, tmp);
31238c1b6884Sszhou 			line_free(tmp);
31248c1b6884Sszhou 		} else
31258c1b6884Sszhou 			lp = lp->next;
31268c1b6884Sszhou 	}
31278c1b6884Sszhou 	return (i);
31288c1b6884Sszhou }
31298c1b6884Sszhou 
31307c478bd9Sstevel@tonic-gate /*ARGSUSED*/
31317c478bd9Sstevel@tonic-gate static error_t
31327c478bd9Sstevel@tonic-gate update_entry(menu_t *mp, char *menu_root, char *opt)
31337c478bd9Sstevel@tonic-gate {
31347c478bd9Sstevel@tonic-gate 	FILE *fp;
31357c478bd9Sstevel@tonic-gate 	int entry;
31367c478bd9Sstevel@tonic-gate 	char *grubdisk, *title, *osdev, *osroot;
31378c1b6884Sszhou 	struct stat sbuf;
31388c1b6884Sszhou 	char failsafe[256];
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate 	assert(mp);
31417c478bd9Sstevel@tonic-gate 	assert(opt);
31427c478bd9Sstevel@tonic-gate 
31437c478bd9Sstevel@tonic-gate 	osdev = strtok(opt, ",");
31447c478bd9Sstevel@tonic-gate 	osroot = strtok(NULL, ",");
31457c478bd9Sstevel@tonic-gate 	if (osroot == NULL)
31467c478bd9Sstevel@tonic-gate 		osroot = menu_root;
31477c478bd9Sstevel@tonic-gate 	title = get_title(osroot);
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
31508c1b6884Sszhou 	fp = open_diskmap(osroot);
31517c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
31527c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
31537c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
31547c478bd9Sstevel@tonic-gate 	}
31557c478bd9Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdev, fp, menu_on_bootdev(menu_root, fp));
31567c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
31577c478bd9Sstevel@tonic-gate 	if (grubdisk == NULL) {
31587c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
31597c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
31607c478bd9Sstevel@tonic-gate 	}
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 	/* add the entry for normal Solaris */
31638c1b6884Sszhou 	entry = update_boot_entry(mp, title, grubdisk,
31647c478bd9Sstevel@tonic-gate 	    "/platform/i86pc/multiboot",
31658c1b6884Sszhou 	    "/platform/i86pc/boot_archive",
31668c1b6884Sszhou 	    osroot == menu_root);
31677c478bd9Sstevel@tonic-gate 
31687c478bd9Sstevel@tonic-gate 	/* add the entry for failsafe archive */
31698c1b6884Sszhou 	(void) snprintf(failsafe, sizeof (failsafe),
31708c1b6884Sszhou 	    "%s/boot/x86.miniroot-safe", osroot);
31718c1b6884Sszhou 	if (stat(failsafe, &sbuf) == 0)
31728c1b6884Sszhou 		(void) update_boot_entry(mp, "Solaris failsafe", grubdisk,
31738c1b6884Sszhou 		    "/boot/multiboot kernel/unix -s",
31748c1b6884Sszhou 		    "/boot/x86.miniroot-safe",
31758c1b6884Sszhou 		    osroot == menu_root);
31767c478bd9Sstevel@tonic-gate 	free(grubdisk);
31777c478bd9Sstevel@tonic-gate 
31787c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
31797c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
31807c478bd9Sstevel@tonic-gate 	}
31817c478bd9Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
31827c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
31837c478bd9Sstevel@tonic-gate }
31847c478bd9Sstevel@tonic-gate 
3185b610f78eSvikram static char *
3186b610f78eSvikram read_grub_root(void)
3187b610f78eSvikram {
3188b610f78eSvikram 	FILE *fp;
3189b610f78eSvikram 	struct stat sb;
3190b610f78eSvikram 	char buf[BAM_MAXLINE];
3191b610f78eSvikram 	char *rootstr;
3192b610f78eSvikram 
3193b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
3194b610f78eSvikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
3195b610f78eSvikram 		return (NULL);
3196b610f78eSvikram 	}
3197b610f78eSvikram 
3198b610f78eSvikram 	if (stat(GRUB_root, &sb) != 0) {
3199b610f78eSvikram 		bam_error(MISSING_ROOT_FILE, GRUB_root, strerror(errno));
3200b610f78eSvikram 		return (NULL);
3201b610f78eSvikram 	}
3202b610f78eSvikram 
3203b610f78eSvikram 	fp = fopen(GRUB_root, "r");
3204b610f78eSvikram 	if (fp == NULL) {
3205b610f78eSvikram 		bam_error(OPEN_FAIL, GRUB_root, strerror(errno));
3206b610f78eSvikram 		return (NULL);
3207b610f78eSvikram 	}
3208b610f78eSvikram 
3209b610f78eSvikram 	if (s_fgets(buf, sizeof (buf), fp) == NULL) {
3210b610f78eSvikram 		bam_error(EMPTY_FILE, GRUB_root, strerror(errno));
3211b610f78eSvikram 		(void) fclose(fp);
3212b610f78eSvikram 		return (NULL);
3213b610f78eSvikram 	}
3214b610f78eSvikram 
3215b610f78eSvikram 	/*
3216b610f78eSvikram 	 * Copy buf here as check below may trash the buffer
3217b610f78eSvikram 	 */
3218b610f78eSvikram 	rootstr = s_strdup(buf);
3219b610f78eSvikram 
3220b610f78eSvikram 	if (s_fgets(buf, sizeof (buf), fp) != NULL) {
3221b610f78eSvikram 		bam_error(BAD_ROOT_FILE, GRUB_root);
3222b610f78eSvikram 		free(rootstr);
3223b610f78eSvikram 		rootstr = NULL;
3224b610f78eSvikram 	}
3225b610f78eSvikram 
3226b610f78eSvikram 	(void) fclose(fp);
3227b610f78eSvikram 
3228b610f78eSvikram 	return (rootstr);
3229b610f78eSvikram }
3230b610f78eSvikram 
32318c1b6884Sszhou static void
32328c1b6884Sszhou save_default_entry(menu_t *mp)
32338c1b6884Sszhou {
32348c1b6884Sszhou 	int lineNum, entryNum;
32358c1b6884Sszhou 	int entry = 0;	/* default is 0 */
32368c1b6884Sszhou 	char linebuf[BAM_MAXLINE];
32378c1b6884Sszhou 	line_t *lp = mp->curdefault;
32388c1b6884Sszhou 
32398c1b6884Sszhou 	if (lp)
32408c1b6884Sszhou 		entry = s_strtol(lp->arg);
32418c1b6884Sszhou 
32428c1b6884Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "#%s%d", BAM_OLDDEF, entry);
32438c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
32448c1b6884Sszhou }
32458c1b6884Sszhou 
32468c1b6884Sszhou static void
32478c1b6884Sszhou restore_default_entry(menu_t *mp)
32488c1b6884Sszhou {
32498c1b6884Sszhou 	int entry;
32508c1b6884Sszhou 	char *str;
32518c1b6884Sszhou 	line_t *lp = mp->olddefault;
32528c1b6884Sszhou 
32538c1b6884Sszhou 	if (lp == NULL)
32548c1b6884Sszhou 		return;		/* nothing to restore */
32558c1b6884Sszhou 
32568c1b6884Sszhou 	str = lp->arg + strlen(BAM_OLDDEF);
32578c1b6884Sszhou 	entry = s_strtol(str);
32588c1b6884Sszhou 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
32598c1b6884Sszhou 
32608c1b6884Sszhou 	/* delete saved old default line */
32618c1b6884Sszhou 	mp->olddefault = NULL;
32628c1b6884Sszhou 	unlink_line(mp, lp);
32638c1b6884Sszhou 	line_free(lp);
32648c1b6884Sszhou }
32658c1b6884Sszhou 
32667c478bd9Sstevel@tonic-gate /*
32677c478bd9Sstevel@tonic-gate  * This function is for supporting reboot with args.
32687c478bd9Sstevel@tonic-gate  * The opt value can be:
32697c478bd9Sstevel@tonic-gate  * NULL		delete temp entry, if present
32707c478bd9Sstevel@tonic-gate  * entry=#	switches default entry to 1
32717c478bd9Sstevel@tonic-gate  * else		treated as boot-args and setup a temperary menu entry
32727c478bd9Sstevel@tonic-gate  *		and make it the default
32737c478bd9Sstevel@tonic-gate  */
32747c478bd9Sstevel@tonic-gate #define	REBOOT_TITLE	"Solaris_reboot_transient"
32757c478bd9Sstevel@tonic-gate 
32768c1b6884Sszhou /*ARGSUSED*/
32777c478bd9Sstevel@tonic-gate static error_t
32787c478bd9Sstevel@tonic-gate update_temp(menu_t *mp, char *menupath, char *opt)
32797c478bd9Sstevel@tonic-gate {
32807c478bd9Sstevel@tonic-gate 	int entry;
32817c478bd9Sstevel@tonic-gate 	char *grubdisk, *rootdev;
32827c478bd9Sstevel@tonic-gate 	char kernbuf[1024];
3283b610f78eSvikram 	struct stat sb;
32847c478bd9Sstevel@tonic-gate 
32857c478bd9Sstevel@tonic-gate 	assert(mp);
32867c478bd9Sstevel@tonic-gate 
32878c1b6884Sszhou 	/* If no option, delete exiting reboot menu entry */
32888c1b6884Sszhou 	if (opt == NULL) {
32898c1b6884Sszhou 		entry_t *ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL,
32908c1b6884Sszhou 		    0, &entry);
32918c1b6884Sszhou 		if (ent == NULL)	/* not found is ok */
32928c1b6884Sszhou 			return (BAM_SUCCESS);
32938c1b6884Sszhou 		(void) do_delete(mp, entry);
32948c1b6884Sszhou 		restore_default_entry(mp);
32958c1b6884Sszhou 		return (BAM_WRITE);
32968c1b6884Sszhou 	}
32978c1b6884Sszhou 
32988c1b6884Sszhou 	/* if entry= is specified, set the default entry */
32998c1b6884Sszhou 	if (strncmp(opt, "entry=", strlen("entry=")) == 0 &&
33007c478bd9Sstevel@tonic-gate 	    selector(mp, opt, &entry, NULL) == BAM_SUCCESS) {
33017c478bd9Sstevel@tonic-gate 		/* this is entry=# option */
33027c478bd9Sstevel@tonic-gate 		return (set_global(mp, menu_cmds[DEFAULT_CMD], entry));
33037c478bd9Sstevel@tonic-gate 	}
33047c478bd9Sstevel@tonic-gate 
33057c478bd9Sstevel@tonic-gate 	/*
33067c478bd9Sstevel@tonic-gate 	 * add a new menu entry base on opt and make it the default
33077c478bd9Sstevel@tonic-gate 	 */
3308b610f78eSvikram 	grubdisk = NULL;
3309b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
3310b610f78eSvikram 		/*
3311b610f78eSvikram 		 * 1. First get root disk name from mnttab
3312b610f78eSvikram 		 * 2. Translate disk name to grub name
3313b610f78eSvikram 		 * 3. Add the new menu entry
3314b610f78eSvikram 		 */
3315b610f78eSvikram 		rootdev = get_special("/");
3316b610f78eSvikram 		if (rootdev) {
3317b610f78eSvikram 			grubdisk = os_to_grubdisk(rootdev, 1);
3318b610f78eSvikram 			free(rootdev);
3319b610f78eSvikram 		}
3320b610f78eSvikram 	} else {
3321b610f78eSvikram 		/*
3322b610f78eSvikram 		 * This is an LU BE. The GRUB_root file
3323b610f78eSvikram 		 * contains entry for GRUB's "root" cmd.
3324b610f78eSvikram 		 */
3325b610f78eSvikram 		grubdisk = read_grub_root();
33267c478bd9Sstevel@tonic-gate 	}
33277c478bd9Sstevel@tonic-gate 	if (grubdisk == NULL) {
3328b610f78eSvikram 		bam_error(REBOOT_WITH_ARGS_FAILED);
33297c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
33307c478bd9Sstevel@tonic-gate 	}
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate 	/* add an entry for Solaris reboot */
33337c478bd9Sstevel@tonic-gate 	(void) snprintf(kernbuf, sizeof (kernbuf),
33347c478bd9Sstevel@tonic-gate 	    "/platform/i86pc/multiboot %s", opt);
33357c478bd9Sstevel@tonic-gate 	entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf,
33367c478bd9Sstevel@tonic-gate 	    "/platform/i86pc/boot_archive");
33377c478bd9Sstevel@tonic-gate 	free(grubdisk);
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
3340b610f78eSvikram 		bam_error(REBOOT_WITH_ARGS_FAILED);
33417c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
33427c478bd9Sstevel@tonic-gate 	}
33438c1b6884Sszhou 
33448c1b6884Sszhou 	save_default_entry(mp);
33457c478bd9Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
33467c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
33477c478bd9Sstevel@tonic-gate }
33487c478bd9Sstevel@tonic-gate 
33497c478bd9Sstevel@tonic-gate static error_t
33507c478bd9Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val)
33517c478bd9Sstevel@tonic-gate {
33527c478bd9Sstevel@tonic-gate 	line_t *lp, *found, *last;
33537c478bd9Sstevel@tonic-gate 	char *cp, *str;
33547c478bd9Sstevel@tonic-gate 	char prefix[BAM_MAXLINE];
33557c478bd9Sstevel@tonic-gate 	size_t len;
33567c478bd9Sstevel@tonic-gate 
33577c478bd9Sstevel@tonic-gate 	assert(mp);
33587c478bd9Sstevel@tonic-gate 	assert(globalcmd);
33597c478bd9Sstevel@tonic-gate 
3360b610f78eSvikram 	if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) {
3361b610f78eSvikram 		if (val < 0 || mp->end == NULL || val > mp->end->entryNum) {
3362b610f78eSvikram 			(void) snprintf(prefix, sizeof (prefix), "%d", val);
3363b610f78eSvikram 			bam_error(INVALID_ENTRY, prefix);
3364b610f78eSvikram 			return (BAM_ERROR);
3365b610f78eSvikram 		}
3366b610f78eSvikram 	}
3367b610f78eSvikram 
33687c478bd9Sstevel@tonic-gate 	found = last = NULL;
33697c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
33707c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
33717c478bd9Sstevel@tonic-gate 			continue;
33727c478bd9Sstevel@tonic-gate 
33737c478bd9Sstevel@tonic-gate 		last = lp; /* track the last global found */
33747c478bd9Sstevel@tonic-gate 
33757c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
33767c478bd9Sstevel@tonic-gate 			bam_error(NO_CMD, lp->lineNum);
33777c478bd9Sstevel@tonic-gate 			continue;
33787c478bd9Sstevel@tonic-gate 		}
33797c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
33807c478bd9Sstevel@tonic-gate 			continue;
33817c478bd9Sstevel@tonic-gate 
33827c478bd9Sstevel@tonic-gate 		if (found) {
33837c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
33847c478bd9Sstevel@tonic-gate 		}
33857c478bd9Sstevel@tonic-gate 		found = lp;
33867c478bd9Sstevel@tonic-gate 	}
33877c478bd9Sstevel@tonic-gate 
33887c478bd9Sstevel@tonic-gate 	if (found == NULL) {
33897c478bd9Sstevel@tonic-gate 		lp = s_calloc(1, sizeof (line_t));
33907c478bd9Sstevel@tonic-gate 		if (last == NULL) {
33917c478bd9Sstevel@tonic-gate 			lp->next = mp->start;
33927c478bd9Sstevel@tonic-gate 			mp->start = lp;
33937c478bd9Sstevel@tonic-gate 			mp->end = (mp->end) ? mp->end : lp;
33947c478bd9Sstevel@tonic-gate 		} else {
33957c478bd9Sstevel@tonic-gate 			lp->next = last->next;
33967c478bd9Sstevel@tonic-gate 			last->next = lp;
33977c478bd9Sstevel@tonic-gate 			if (lp->next == NULL)
33987c478bd9Sstevel@tonic-gate 				mp->end = lp;
33997c478bd9Sstevel@tonic-gate 		}
34007c478bd9Sstevel@tonic-gate 		lp->flags = BAM_GLOBAL; /* other fields not needed for writes */
34017c478bd9Sstevel@tonic-gate 		len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]);
34027c478bd9Sstevel@tonic-gate 		len += 10;	/* val < 10 digits */
34037c478bd9Sstevel@tonic-gate 		lp->line = s_calloc(1, len);
34047c478bd9Sstevel@tonic-gate 		(void) snprintf(lp->line, len, "%s%s%d",
34057c478bd9Sstevel@tonic-gate 		    globalcmd, menu_cmds[SEP_CMD], val);
34067c478bd9Sstevel@tonic-gate 		return (BAM_WRITE);
34077c478bd9Sstevel@tonic-gate 	}
34087c478bd9Sstevel@tonic-gate 
34097c478bd9Sstevel@tonic-gate 	/*
34107c478bd9Sstevel@tonic-gate 	 * We are changing an existing entry. Retain any prefix whitespace,
34117c478bd9Sstevel@tonic-gate 	 * but overwrite everything else. This preserves tabs added for
34127c478bd9Sstevel@tonic-gate 	 * readability.
34137c478bd9Sstevel@tonic-gate 	 */
34147c478bd9Sstevel@tonic-gate 	str = found->line;
34157c478bd9Sstevel@tonic-gate 	cp = prefix;
34167c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
34177c478bd9Sstevel@tonic-gate 		*(cp++) = *(str++);
34187c478bd9Sstevel@tonic-gate 	*cp = '\0'; /* Terminate prefix */
34197c478bd9Sstevel@tonic-gate 	len = strlen(prefix) + strlen(globalcmd);
34207c478bd9Sstevel@tonic-gate 	len += strlen(menu_cmds[SEP_CMD]) + 10;
34217c478bd9Sstevel@tonic-gate 
34227c478bd9Sstevel@tonic-gate 	free(found->line);
34237c478bd9Sstevel@tonic-gate 	found->line = s_calloc(1, len);
34247c478bd9Sstevel@tonic-gate 	(void) snprintf(found->line, len,
34257c478bd9Sstevel@tonic-gate 		"%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val);
34267c478bd9Sstevel@tonic-gate 
34277c478bd9Sstevel@tonic-gate 	return (BAM_WRITE); /* need a write to menu */
34287c478bd9Sstevel@tonic-gate }
34297c478bd9Sstevel@tonic-gate 
34307c478bd9Sstevel@tonic-gate /*ARGSUSED*/
34317c478bd9Sstevel@tonic-gate static error_t
34327c478bd9Sstevel@tonic-gate set_option(menu_t *mp, char *menu_path, char *opt)
34337c478bd9Sstevel@tonic-gate {
34347c478bd9Sstevel@tonic-gate 	int optnum, optval;
34357c478bd9Sstevel@tonic-gate 	char *val;
34367c478bd9Sstevel@tonic-gate 
34377c478bd9Sstevel@tonic-gate 	assert(mp);
34387c478bd9Sstevel@tonic-gate 	assert(opt);
34397c478bd9Sstevel@tonic-gate 
34407c478bd9Sstevel@tonic-gate 	val = strchr(opt, '=');
34417c478bd9Sstevel@tonic-gate 	if (val == NULL) {
34427c478bd9Sstevel@tonic-gate 		bam_error(INVALID_ENTRY, opt);
34437c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
34447c478bd9Sstevel@tonic-gate 	}
34457c478bd9Sstevel@tonic-gate 
34467c478bd9Sstevel@tonic-gate 	*val = '\0';
34477c478bd9Sstevel@tonic-gate 	if (strcmp(opt, "default") == 0) {
34487c478bd9Sstevel@tonic-gate 		optnum = DEFAULT_CMD;
34497c478bd9Sstevel@tonic-gate 	} else if (strcmp(opt, "timeout") == 0) {
34507c478bd9Sstevel@tonic-gate 		optnum = TIMEOUT_CMD;
34517c478bd9Sstevel@tonic-gate 	} else {
34527c478bd9Sstevel@tonic-gate 		bam_error(INVALID_ENTRY, opt);
34537c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
34547c478bd9Sstevel@tonic-gate 	}
34557c478bd9Sstevel@tonic-gate 
34567c478bd9Sstevel@tonic-gate 	optval = s_strtol(val + 1);
34577c478bd9Sstevel@tonic-gate 	*val = '=';
34587c478bd9Sstevel@tonic-gate 	return (set_global(mp, menu_cmds[optnum], optval));
34597c478bd9Sstevel@tonic-gate }
34607c478bd9Sstevel@tonic-gate 
34617c478bd9Sstevel@tonic-gate /*
34627c478bd9Sstevel@tonic-gate  * The quiet argument suppresses messages. This is used
34637c478bd9Sstevel@tonic-gate  * when invoked in the context of other commands (e.g. list_entry)
34647c478bd9Sstevel@tonic-gate  */
34657c478bd9Sstevel@tonic-gate static error_t
34667c478bd9Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet)
34677c478bd9Sstevel@tonic-gate {
34687c478bd9Sstevel@tonic-gate 	line_t *lp;
34697c478bd9Sstevel@tonic-gate 	char *arg;
34707c478bd9Sstevel@tonic-gate 	int done, ret = BAM_SUCCESS;
34717c478bd9Sstevel@tonic-gate 
34727c478bd9Sstevel@tonic-gate 	assert(mp);
34737c478bd9Sstevel@tonic-gate 	assert(menu_path);
34747c478bd9Sstevel@tonic-gate 	assert(globalcmd);
34757c478bd9Sstevel@tonic-gate 
34767c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
34777c478bd9Sstevel@tonic-gate 		if (!quiet)
34787c478bd9Sstevel@tonic-gate 			bam_error(NO_MENU, menu_path);
34797c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
34807c478bd9Sstevel@tonic-gate 	}
34817c478bd9Sstevel@tonic-gate 
34827c478bd9Sstevel@tonic-gate 	done = 0;
34837c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
34847c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
34857c478bd9Sstevel@tonic-gate 			continue;
34867c478bd9Sstevel@tonic-gate 
34877c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
34887c478bd9Sstevel@tonic-gate 			if (!quiet)
34897c478bd9Sstevel@tonic-gate 				bam_error(NO_CMD, lp->lineNum);
34907c478bd9Sstevel@tonic-gate 			continue;
34917c478bd9Sstevel@tonic-gate 		}
34927c478bd9Sstevel@tonic-gate 
34937c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
34947c478bd9Sstevel@tonic-gate 			continue;
34957c478bd9Sstevel@tonic-gate 
34967c478bd9Sstevel@tonic-gate 		/* Found global. Check for duplicates */
34977c478bd9Sstevel@tonic-gate 		if (done && !quiet) {
34987c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
34997c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
35007c478bd9Sstevel@tonic-gate 		}
35017c478bd9Sstevel@tonic-gate 
35027c478bd9Sstevel@tonic-gate 		arg = lp->arg ? lp->arg : "";
35037c478bd9Sstevel@tonic-gate 		bam_print(GLOBAL_CMD, globalcmd, arg);
35047c478bd9Sstevel@tonic-gate 		done = 1;
35057c478bd9Sstevel@tonic-gate 	}
35067c478bd9Sstevel@tonic-gate 
35077c478bd9Sstevel@tonic-gate 	if (!done && bam_verbose)
35087c478bd9Sstevel@tonic-gate 		bam_print(NO_ENTRY, globalcmd);
35097c478bd9Sstevel@tonic-gate 
35107c478bd9Sstevel@tonic-gate 	return (ret);
35117c478bd9Sstevel@tonic-gate }
35127c478bd9Sstevel@tonic-gate 
35137c478bd9Sstevel@tonic-gate static error_t
35147c478bd9Sstevel@tonic-gate menu_write(char *root, menu_t *mp)
35157c478bd9Sstevel@tonic-gate {
35167c478bd9Sstevel@tonic-gate 	return (list2file(root, MENU_TMP, GRUB_MENU, mp->start));
35177c478bd9Sstevel@tonic-gate }
35187c478bd9Sstevel@tonic-gate 
35197c478bd9Sstevel@tonic-gate static void
35207c478bd9Sstevel@tonic-gate line_free(line_t *lp)
35217c478bd9Sstevel@tonic-gate {
35227c478bd9Sstevel@tonic-gate 	if (lp == NULL)
35237c478bd9Sstevel@tonic-gate 		return;
35247c478bd9Sstevel@tonic-gate 
35257c478bd9Sstevel@tonic-gate 	if (lp->cmd)
35267c478bd9Sstevel@tonic-gate 		free(lp->cmd);
35277c478bd9Sstevel@tonic-gate 	if (lp->sep)
35287c478bd9Sstevel@tonic-gate 		free(lp->sep);
35297c478bd9Sstevel@tonic-gate 	if (lp->arg)
35307c478bd9Sstevel@tonic-gate 		free(lp->arg);
35317c478bd9Sstevel@tonic-gate 	if (lp->line)
35327c478bd9Sstevel@tonic-gate 		free(lp->line);
35337c478bd9Sstevel@tonic-gate 	free(lp);
35347c478bd9Sstevel@tonic-gate }
35357c478bd9Sstevel@tonic-gate 
35367c478bd9Sstevel@tonic-gate static void
35377c478bd9Sstevel@tonic-gate linelist_free(line_t *start)
35387c478bd9Sstevel@tonic-gate {
35397c478bd9Sstevel@tonic-gate 	line_t *lp;
35407c478bd9Sstevel@tonic-gate 
35417c478bd9Sstevel@tonic-gate 	while (start) {
35427c478bd9Sstevel@tonic-gate 		lp = start;
35437c478bd9Sstevel@tonic-gate 		start = start->next;
35447c478bd9Sstevel@tonic-gate 		line_free(lp);
35457c478bd9Sstevel@tonic-gate 	}
35467c478bd9Sstevel@tonic-gate }
35477c478bd9Sstevel@tonic-gate 
35487c478bd9Sstevel@tonic-gate static void
35497c478bd9Sstevel@tonic-gate filelist_free(filelist_t *flistp)
35507c478bd9Sstevel@tonic-gate {
35517c478bd9Sstevel@tonic-gate 	linelist_free(flistp->head);
35527c478bd9Sstevel@tonic-gate 	flistp->head = NULL;
35537c478bd9Sstevel@tonic-gate 	flistp->tail = NULL;
35547c478bd9Sstevel@tonic-gate }
35557c478bd9Sstevel@tonic-gate 
35567c478bd9Sstevel@tonic-gate static void
35577c478bd9Sstevel@tonic-gate menu_free(menu_t *mp)
35587c478bd9Sstevel@tonic-gate {
35598c1b6884Sszhou 	entry_t *ent, *tmp;
35607c478bd9Sstevel@tonic-gate 	assert(mp);
35617c478bd9Sstevel@tonic-gate 
35627c478bd9Sstevel@tonic-gate 	if (mp->start)
35637c478bd9Sstevel@tonic-gate 		linelist_free(mp->start);
35648c1b6884Sszhou 	ent = mp->entries;
35658c1b6884Sszhou 	while (ent) {
35668c1b6884Sszhou 		tmp = ent;
35678c1b6884Sszhou 		ent = tmp->next;
35688c1b6884Sszhou 		free(tmp);
35698c1b6884Sszhou 	}
35707c478bd9Sstevel@tonic-gate 
35718c1b6884Sszhou 	free(mp);
35727c478bd9Sstevel@tonic-gate }
35737c478bd9Sstevel@tonic-gate 
35747c478bd9Sstevel@tonic-gate /*
35757c478bd9Sstevel@tonic-gate  * Utility routines
35767c478bd9Sstevel@tonic-gate  */
35777c478bd9Sstevel@tonic-gate 
35787c478bd9Sstevel@tonic-gate 
35797c478bd9Sstevel@tonic-gate /*
35807c478bd9Sstevel@tonic-gate  * Returns 0 on success
35817c478bd9Sstevel@tonic-gate  * Any other value indicates an error
35827c478bd9Sstevel@tonic-gate  */
35837c478bd9Sstevel@tonic-gate static int
35847c478bd9Sstevel@tonic-gate exec_cmd(char *cmdline, char *output, int64_t osize)
35857c478bd9Sstevel@tonic-gate {
35867c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
35877c478bd9Sstevel@tonic-gate 	int ret;
35887c478bd9Sstevel@tonic-gate 	FILE *ptr;
35897c478bd9Sstevel@tonic-gate 	size_t len;
35907c478bd9Sstevel@tonic-gate 	sigset_t set;
35917c478bd9Sstevel@tonic-gate 	void (*disp)(int);
35927c478bd9Sstevel@tonic-gate 
35937c478bd9Sstevel@tonic-gate 	/*
35947c478bd9Sstevel@tonic-gate 	 * For security
35957c478bd9Sstevel@tonic-gate 	 * - only absolute paths are allowed
35967c478bd9Sstevel@tonic-gate 	 * - set IFS to space and tab
35977c478bd9Sstevel@tonic-gate 	 */
35987c478bd9Sstevel@tonic-gate 	if (*cmdline != '/') {
35997c478bd9Sstevel@tonic-gate 		bam_error(ABS_PATH_REQ, cmdline);
36007c478bd9Sstevel@tonic-gate 		return (-1);
36017c478bd9Sstevel@tonic-gate 	}
36027c478bd9Sstevel@tonic-gate 	(void) putenv("IFS= \t");
36037c478bd9Sstevel@tonic-gate 
36047c478bd9Sstevel@tonic-gate 	/*
36057c478bd9Sstevel@tonic-gate 	 * We may have been exec'ed with SIGCHLD blocked
36067c478bd9Sstevel@tonic-gate 	 * unblock it here
36077c478bd9Sstevel@tonic-gate 	 */
36087c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
36097c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
36107c478bd9Sstevel@tonic-gate 	if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
36117c478bd9Sstevel@tonic-gate 		bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno));
36127c478bd9Sstevel@tonic-gate 		return (-1);
36137c478bd9Sstevel@tonic-gate 	}
36147c478bd9Sstevel@tonic-gate 
36157c478bd9Sstevel@tonic-gate 	/*
36167c478bd9Sstevel@tonic-gate 	 * Set SIGCHLD disposition to SIG_DFL for popen/pclose
36177c478bd9Sstevel@tonic-gate 	 */
36187c478bd9Sstevel@tonic-gate 	disp = sigset(SIGCHLD, SIG_DFL);
36197c478bd9Sstevel@tonic-gate 	if (disp == SIG_ERR) {
36207c478bd9Sstevel@tonic-gate 		bam_error(FAILED_SIG, strerror(errno));
36217c478bd9Sstevel@tonic-gate 		return (-1);
36227c478bd9Sstevel@tonic-gate 	}
36237c478bd9Sstevel@tonic-gate 	if (disp == SIG_HOLD) {
36247c478bd9Sstevel@tonic-gate 		bam_error(BLOCKED_SIG, cmdline);
36257c478bd9Sstevel@tonic-gate 		return (-1);
36267c478bd9Sstevel@tonic-gate 	}
36277c478bd9Sstevel@tonic-gate 
36287c478bd9Sstevel@tonic-gate 	ptr = popen(cmdline, "r");
36297c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
36307c478bd9Sstevel@tonic-gate 		bam_error(POPEN_FAIL, cmdline, strerror(errno));
36317c478bd9Sstevel@tonic-gate 		return (-1);
36327c478bd9Sstevel@tonic-gate 	}
36337c478bd9Sstevel@tonic-gate 
36347c478bd9Sstevel@tonic-gate 	/*
36357c478bd9Sstevel@tonic-gate 	 * If we simply do a pclose() following a popen(), pclose()
36367c478bd9Sstevel@tonic-gate 	 * will close the reader end of the pipe immediately even
36377c478bd9Sstevel@tonic-gate 	 * if the child process has not started/exited. pclose()
36387c478bd9Sstevel@tonic-gate 	 * does wait for cmd to terminate before returning though.
36397c478bd9Sstevel@tonic-gate 	 * When the executed command writes its output to the pipe
36407c478bd9Sstevel@tonic-gate 	 * there is no reader process and the command dies with
36417c478bd9Sstevel@tonic-gate 	 * SIGPIPE. To avoid this we read repeatedly until read
36427c478bd9Sstevel@tonic-gate 	 * terminates with EOF. This indicates that the command
36437c478bd9Sstevel@tonic-gate 	 * (writer) has closed the pipe and we can safely do a
36447c478bd9Sstevel@tonic-gate 	 * pclose().
36457c478bd9Sstevel@tonic-gate 	 *
36467c478bd9Sstevel@tonic-gate 	 * Since pclose() does wait for the command to exit,
36477c478bd9Sstevel@tonic-gate 	 * we can safely reap the exit status of the command
36487c478bd9Sstevel@tonic-gate 	 * from the value returned by pclose()
36497c478bd9Sstevel@tonic-gate 	 */
36507c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), ptr) != NULL) {
36517c478bd9Sstevel@tonic-gate 		/* if (bam_verbose)  XXX */
36527c478bd9Sstevel@tonic-gate 			bam_print(PRINT_NO_NEWLINE, buf);
36537c478bd9Sstevel@tonic-gate 		if (output && osize > 0) {
36547c478bd9Sstevel@tonic-gate 			(void) snprintf(output, osize, "%s", buf);
36557c478bd9Sstevel@tonic-gate 			len = strlen(buf);
36567c478bd9Sstevel@tonic-gate 			output += len;
36577c478bd9Sstevel@tonic-gate 			osize -= len;
36587c478bd9Sstevel@tonic-gate 		}
36597c478bd9Sstevel@tonic-gate 	}
36607c478bd9Sstevel@tonic-gate 
36617c478bd9Sstevel@tonic-gate 	ret = pclose(ptr);
36627c478bd9Sstevel@tonic-gate 	if (ret == -1) {
36637c478bd9Sstevel@tonic-gate 		bam_error(PCLOSE_FAIL, cmdline, strerror(errno));
36647c478bd9Sstevel@tonic-gate 		return (-1);
36657c478bd9Sstevel@tonic-gate 	}
36667c478bd9Sstevel@tonic-gate 
36677c478bd9Sstevel@tonic-gate 	if (WIFEXITED(ret)) {
36687c478bd9Sstevel@tonic-gate 		return (WEXITSTATUS(ret));
36697c478bd9Sstevel@tonic-gate 	} else {
36707c478bd9Sstevel@tonic-gate 		bam_error(EXEC_FAIL, cmdline, ret);
36717c478bd9Sstevel@tonic-gate 		return (-1);
36727c478bd9Sstevel@tonic-gate 	}
36737c478bd9Sstevel@tonic-gate }
36747c478bd9Sstevel@tonic-gate 
36757c478bd9Sstevel@tonic-gate /*
36767c478bd9Sstevel@tonic-gate  * Since this function returns -1 on error
36777c478bd9Sstevel@tonic-gate  * it cannot be used to convert -1. However,
36787c478bd9Sstevel@tonic-gate  * that is sufficient for what we need.
36797c478bd9Sstevel@tonic-gate  */
36807c478bd9Sstevel@tonic-gate static long
36817c478bd9Sstevel@tonic-gate s_strtol(char *str)
36827c478bd9Sstevel@tonic-gate {
36837c478bd9Sstevel@tonic-gate 	long l;
36847c478bd9Sstevel@tonic-gate 	char *res = NULL;
36857c478bd9Sstevel@tonic-gate 
36867c478bd9Sstevel@tonic-gate 	if (str == NULL) {
36877c478bd9Sstevel@tonic-gate 		return (-1);
36887c478bd9Sstevel@tonic-gate 	}
36897c478bd9Sstevel@tonic-gate 
36907c478bd9Sstevel@tonic-gate 	errno = 0;
36917c478bd9Sstevel@tonic-gate 	l = strtol(str, &res, 10);
36927c478bd9Sstevel@tonic-gate 	if (errno || *res != '\0') {
36937c478bd9Sstevel@tonic-gate 		return (-1);
36947c478bd9Sstevel@tonic-gate 	}
36957c478bd9Sstevel@tonic-gate 
36967c478bd9Sstevel@tonic-gate 	return (l);
36977c478bd9Sstevel@tonic-gate }
36987c478bd9Sstevel@tonic-gate 
36997c478bd9Sstevel@tonic-gate /*
37007c478bd9Sstevel@tonic-gate  * Wrapper around fputs, that adds a newline (since fputs doesn't)
37017c478bd9Sstevel@tonic-gate  */
37027c478bd9Sstevel@tonic-gate static int
37037c478bd9Sstevel@tonic-gate s_fputs(char *str, FILE *fp)
37047c478bd9Sstevel@tonic-gate {
37057c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
37067c478bd9Sstevel@tonic-gate 
37077c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s\n", str);
37087c478bd9Sstevel@tonic-gate 	return (fputs(linebuf, fp));
37097c478bd9Sstevel@tonic-gate }
37107c478bd9Sstevel@tonic-gate 
37117c478bd9Sstevel@tonic-gate /*
37127c478bd9Sstevel@tonic-gate  * Wrapper around fgets, that strips newlines returned by fgets
37137c478bd9Sstevel@tonic-gate  */
37147c478bd9Sstevel@tonic-gate static char *
37157c478bd9Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp)
37167c478bd9Sstevel@tonic-gate {
37177c478bd9Sstevel@tonic-gate 	int n;
37187c478bd9Sstevel@tonic-gate 
37197c478bd9Sstevel@tonic-gate 	buf = fgets(buf, buflen, fp);
37207c478bd9Sstevel@tonic-gate 	if (buf) {
37217c478bd9Sstevel@tonic-gate 		n = strlen(buf);
37227c478bd9Sstevel@tonic-gate 		if (n == buflen - 1 && buf[n-1] != '\n')
37237c478bd9Sstevel@tonic-gate 			bam_error(TOO_LONG, buflen - 1, buf);
37247c478bd9Sstevel@tonic-gate 		buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1];
37257c478bd9Sstevel@tonic-gate 	}
37267c478bd9Sstevel@tonic-gate 
37277c478bd9Sstevel@tonic-gate 	return (buf);
37287c478bd9Sstevel@tonic-gate }
37297c478bd9Sstevel@tonic-gate 
37307c478bd9Sstevel@tonic-gate static void *
37317c478bd9Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz)
37327c478bd9Sstevel@tonic-gate {
37337c478bd9Sstevel@tonic-gate 	void *ptr;
37347c478bd9Sstevel@tonic-gate 
37357c478bd9Sstevel@tonic-gate 	ptr = calloc(nelem, sz);
37367c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
37377c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, nelem*sz);
37387c478bd9Sstevel@tonic-gate 		bam_exit(1);
37397c478bd9Sstevel@tonic-gate 	}
37407c478bd9Sstevel@tonic-gate 	return (ptr);
37417c478bd9Sstevel@tonic-gate }
37427c478bd9Sstevel@tonic-gate 
37437c478bd9Sstevel@tonic-gate static char *
37447c478bd9Sstevel@tonic-gate s_strdup(char *str)
37457c478bd9Sstevel@tonic-gate {
37467c478bd9Sstevel@tonic-gate 	char *ptr;
37477c478bd9Sstevel@tonic-gate 
37487c478bd9Sstevel@tonic-gate 	if (str == NULL)
37497c478bd9Sstevel@tonic-gate 		return (NULL);
37507c478bd9Sstevel@tonic-gate 
37517c478bd9Sstevel@tonic-gate 	ptr = strdup(str);
37527c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
37537c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, strlen(str) + 1);
37547c478bd9Sstevel@tonic-gate 		bam_exit(1);
37557c478bd9Sstevel@tonic-gate 	}
37567c478bd9Sstevel@tonic-gate 	return (ptr);
37577c478bd9Sstevel@tonic-gate }
37587c478bd9Sstevel@tonic-gate 
37597c478bd9Sstevel@tonic-gate /*
37607c478bd9Sstevel@tonic-gate  * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients)
37617c478bd9Sstevel@tonic-gate  * Returns 0 otherwise
37627c478bd9Sstevel@tonic-gate  */
37637c478bd9Sstevel@tonic-gate static int
37647c478bd9Sstevel@tonic-gate is_amd64(void)
37657c478bd9Sstevel@tonic-gate {
37667c478bd9Sstevel@tonic-gate 	static int amd64 = -1;
37677c478bd9Sstevel@tonic-gate 	char isabuf[257];	/* from sysinfo(2) manpage */
37687c478bd9Sstevel@tonic-gate 
37697c478bd9Sstevel@tonic-gate 	if (amd64 != -1)
37707c478bd9Sstevel@tonic-gate 		return (amd64);
37717c478bd9Sstevel@tonic-gate 
37727c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 &&
37737c478bd9Sstevel@tonic-gate 	    strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0)
37747c478bd9Sstevel@tonic-gate 		amd64 = 1;
37757c478bd9Sstevel@tonic-gate 	else if (strstr(isabuf, "i386") == NULL)
37767c478bd9Sstevel@tonic-gate 		amd64 = 1;		/* diskless server */
37777c478bd9Sstevel@tonic-gate 	else
37787c478bd9Sstevel@tonic-gate 		amd64 = 0;
37797c478bd9Sstevel@tonic-gate 
37807c478bd9Sstevel@tonic-gate 	return (amd64);
37817c478bd9Sstevel@tonic-gate }
37827c478bd9Sstevel@tonic-gate 
37837c478bd9Sstevel@tonic-gate static void
37847c478bd9Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s)
37857c478bd9Sstevel@tonic-gate {
37867c478bd9Sstevel@tonic-gate 	line_t *lp;
37877c478bd9Sstevel@tonic-gate 
37887c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
37897c478bd9Sstevel@tonic-gate 	lp->line = s_strdup(s);
37907c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL)
37917c478bd9Sstevel@tonic-gate 		flistp->head = lp;
37927c478bd9Sstevel@tonic-gate 	else
37937c478bd9Sstevel@tonic-gate 		flistp->tail->next = lp;
37947c478bd9Sstevel@tonic-gate 	flistp->tail = lp;
37957c478bd9Sstevel@tonic-gate }
3796