xref: /titanic_50/usr/src/cmd/boot/bootadm/bootadm.c (revision a28d77b893c584d28f7235fcdb504676cd090aba)
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>
5558091fd8Ssetje #include <sys/param.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include <pwd.h>
587c478bd9Sstevel@tonic-gate #include <grp.h>
597c478bd9Sstevel@tonic-gate #include <device_info.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #include <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 
32258091fd8Ssetje 
3239632cfadSsetje struct safefile {
32458091fd8Ssetje 	char *name;
32558091fd8Ssetje 	struct safefile *next;
32658091fd8Ssetje };
32758091fd8Ssetje 
32858091fd8Ssetje struct safefile *safefiles = NULL;
32958091fd8Ssetje #define	NEED_UPDATE_FILE "/etc/svc/volatile/boot_archive_needs_update"
33058091fd8Ssetje 
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 
10467c478bd9Sstevel@tonic-gate 	/*
10478c1b6884Sszhou 	 * Add trailing / for archive subcommands
10488c1b6884Sszhou 	 */
10498c1b6884Sszhou 	if (rootbuf[strlen(rootbuf) - 1] != '/')
10508c1b6884Sszhou 		(void) strcat(rootbuf, "/");
10518c1b6884Sszhou 	bam_rootlen = strlen(rootbuf);
10528c1b6884Sszhou 
10538c1b6884Sszhou 	/*
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 
137558091fd8Ssetje 	struct safefile *safefilep;
137658091fd8Ssetje 	FILE *fp;
137758091fd8Ssetje 
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 	/*
140558091fd8Ssetje 	 * If we are invoked as part of system/filesyste/boot-archive, then
140658091fd8Ssetje 	 * there are a number of things we should not worry about
14077c478bd9Sstevel@tonic-gate 	 */
140858091fd8Ssetje 	if (bam_smf_check) {
140958091fd8Ssetje 		/* ignore amd64 modules unless we are booted amd64. */
141058091fd8Ssetje 		if (!is_amd64() && strstr(file, "/amd64/") != 0)
14117c478bd9Sstevel@tonic-gate 			return (0);
14127c478bd9Sstevel@tonic-gate 
141358091fd8Ssetje 		/* read in list of safe files */
141458091fd8Ssetje 		if (safefiles == NULL)
141558091fd8Ssetje 			if (fp = fopen("/boot/solaris/filelist.safe", "r")) {
141658091fd8Ssetje 				safefiles = s_calloc(1,
141758091fd8Ssetje 				    sizeof (struct safefile));
141858091fd8Ssetje 				safefilep = safefiles;
141958091fd8Ssetje 				safefilep->name = s_calloc(1, MAXPATHLEN +
142058091fd8Ssetje 				    MAXNAMELEN);
142158091fd8Ssetje 				safefilep->next = NULL;
142258091fd8Ssetje 				while (s_fgets(safefilep->name, MAXPATHLEN +
142358091fd8Ssetje 				    MAXNAMELEN, fp) != NULL) {
142458091fd8Ssetje 					safefilep->next = s_calloc(1,
142558091fd8Ssetje 					    sizeof (struct safefile));
142658091fd8Ssetje 					safefilep = safefilep->next;
142758091fd8Ssetje 					safefilep->name = s_calloc(1,
142858091fd8Ssetje 					    MAXPATHLEN + MAXNAMELEN);
142958091fd8Ssetje 					safefilep->next = NULL;
143058091fd8Ssetje 				}
143158091fd8Ssetje 				(void) fclose(fp);
143258091fd8Ssetje 			}
143358091fd8Ssetje 
143458091fd8Ssetje 		safefilep = safefiles;
143558091fd8Ssetje 		while (safefilep->next != NULL)
143658091fd8Ssetje 			if (strcmp(file, safefilep->name) != 0) {
143758091fd8Ssetje 				fp = fopen(NEED_UPDATE_FILE, "w");
143858091fd8Ssetje 				if (fclose(fp) != 0)
143958091fd8Ssetje 					bam_error(CLOSE_FAIL, NEED_UPDATE_FILE,
144058091fd8Ssetje 						strerror(errno));
144158091fd8Ssetje 				return (0);
144258091fd8Ssetje 			}
144358091fd8Ssetje 	}
144458091fd8Ssetje 
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 
1613*a28d77b8Svikram /*
1614*a28d77b8Svikram  * Checks if a file in the current (old) archive has
1615*a28d77b8Svikram  * been deleted from the root filesystem. This is needed for
1616*a28d77b8Svikram  * software like Trusted Extensions (TX) that switch early
1617*a28d77b8Svikram  * in boot based on presence/absence of a kernel module.
1618*a28d77b8Svikram  */
1619*a28d77b8Svikram static void
1620*a28d77b8Svikram check4stale(char *root)
1621*a28d77b8Svikram {
1622*a28d77b8Svikram 	nvpair_t	*nvp;
1623*a28d77b8Svikram 	nvlist_t	*nvlp;
1624*a28d77b8Svikram 	char 		*file;
1625*a28d77b8Svikram 	char		path[PATH_MAX];
1626*a28d77b8Svikram 	struct stat	sb;
1627*a28d77b8Svikram 
1628*a28d77b8Svikram 	/*
1629*a28d77b8Svikram 	 * Skip stale file check during smf check
1630*a28d77b8Svikram 	 */
1631*a28d77b8Svikram 	if (bam_smf_check)
1632*a28d77b8Svikram 		return;
1633*a28d77b8Svikram 
1634*a28d77b8Svikram 	/* Nothing to do if no old stats */
1635*a28d77b8Svikram 	if ((nvlp = walk_arg.old_nvlp) == NULL)
1636*a28d77b8Svikram 		return;
1637*a28d77b8Svikram 
1638*a28d77b8Svikram 	for (nvp = nvlist_next_nvpair(nvlp, NULL); nvp;
1639*a28d77b8Svikram 	    nvp = nvlist_next_nvpair(nvlp, nvp)) {
1640*a28d77b8Svikram 		file = nvpair_name(nvp);
1641*a28d77b8Svikram 		if (file == NULL)
1642*a28d77b8Svikram 			continue;
1643*a28d77b8Svikram 		(void) snprintf(path, sizeof (path), "%s/%s",
1644*a28d77b8Svikram 		    root, file);
1645*a28d77b8Svikram 		if (stat(path, &sb) == -1) {
1646*a28d77b8Svikram 			walk_arg.need_update = 1;
1647*a28d77b8Svikram 			if (bam_verbose)
1648*a28d77b8Svikram 				bam_print(PARSEABLE_STALE_FILE, path);
1649*a28d77b8Svikram 		}
1650*a28d77b8Svikram 	}
1651*a28d77b8Svikram }
1652*a28d77b8Svikram 
16537c478bd9Sstevel@tonic-gate static void
16547c478bd9Sstevel@tonic-gate create_newstat(void)
16557c478bd9Sstevel@tonic-gate {
16567c478bd9Sstevel@tonic-gate 	int error;
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0);
16597c478bd9Sstevel@tonic-gate 	if (error) {
16607c478bd9Sstevel@tonic-gate 		/*
16617c478bd9Sstevel@tonic-gate 		 * Not fatal - we can still create archive
16627c478bd9Sstevel@tonic-gate 		 */
16637c478bd9Sstevel@tonic-gate 		walk_arg.new_nvlp = NULL;
16647c478bd9Sstevel@tonic-gate 		bam_error(NVALLOC_FAIL, strerror(error));
16657c478bd9Sstevel@tonic-gate 	}
16667c478bd9Sstevel@tonic-gate }
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate static void
16697c478bd9Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp)
16707c478bd9Sstevel@tonic-gate {
16717c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
16727c478bd9Sstevel@tonic-gate 	line_t *lp;
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
16757c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, lp->line);
16767c478bd9Sstevel@tonic-gate 		/* XXX shouldn't we use FTW_MOUNT ? */
16777c478bd9Sstevel@tonic-gate 		if (nftw(path, cmpstat, 20, 0) == -1) {
16787c478bd9Sstevel@tonic-gate 			/*
16797c478bd9Sstevel@tonic-gate 			 * Some files may not exist.
16807c478bd9Sstevel@tonic-gate 			 * For example: etc/rtc_config on a x86 diskless system
16817c478bd9Sstevel@tonic-gate 			 * Emit verbose message only
16827c478bd9Sstevel@tonic-gate 			 */
16837c478bd9Sstevel@tonic-gate 			if (bam_verbose)
16847c478bd9Sstevel@tonic-gate 				bam_print(NFTW_FAIL, path, strerror(errno));
16857c478bd9Sstevel@tonic-gate 		}
16867c478bd9Sstevel@tonic-gate 	}
16877c478bd9Sstevel@tonic-gate }
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate static void
16907c478bd9Sstevel@tonic-gate savenew(char *root)
16917c478bd9Sstevel@tonic-gate {
16927c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
16937c478bd9Sstevel@tonic-gate 	char path2[PATH_MAX];
16947c478bd9Sstevel@tonic-gate 	size_t sz;
16957c478bd9Sstevel@tonic-gate 	char *nstat;
16967c478bd9Sstevel@tonic-gate 	int fd, wrote, error;
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	nstat = NULL;
16997c478bd9Sstevel@tonic-gate 	sz = 0;
17007c478bd9Sstevel@tonic-gate 	error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz,
17017c478bd9Sstevel@tonic-gate 	    NV_ENCODE_XDR, 0);
17027c478bd9Sstevel@tonic-gate 	if (error) {
17037c478bd9Sstevel@tonic-gate 		bam_error(PACK_FAIL, strerror(error));
17047c478bd9Sstevel@tonic-gate 		return;
17057c478bd9Sstevel@tonic-gate 	}
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP);
17087c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE);
17097c478bd9Sstevel@tonic-gate 	if (fd == -1) {
17107c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, path, strerror(errno));
17117c478bd9Sstevel@tonic-gate 		free(nstat);
17127c478bd9Sstevel@tonic-gate 		return;
17137c478bd9Sstevel@tonic-gate 	}
17147c478bd9Sstevel@tonic-gate 	wrote = write(fd, nstat, sz);
17157c478bd9Sstevel@tonic-gate 	if (wrote != sz) {
17167c478bd9Sstevel@tonic-gate 		bam_error(WRITE_FAIL, path, strerror(errno));
17177c478bd9Sstevel@tonic-gate 		(void) close(fd);
17187c478bd9Sstevel@tonic-gate 		free(nstat);
17197c478bd9Sstevel@tonic-gate 		return;
17207c478bd9Sstevel@tonic-gate 	}
17217c478bd9Sstevel@tonic-gate 	(void) close(fd);
17227c478bd9Sstevel@tonic-gate 	free(nstat);
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	(void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT);
17257c478bd9Sstevel@tonic-gate 	if (rename(path, path2) != 0) {
17267c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path2, strerror(errno));
17277c478bd9Sstevel@tonic-gate 	}
17287c478bd9Sstevel@tonic-gate }
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate static void
17317c478bd9Sstevel@tonic-gate clear_walk_args(void)
17327c478bd9Sstevel@tonic-gate {
17337c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp)
17347c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.old_nvlp);
17357c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp)
17367c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.new_nvlp);
17377c478bd9Sstevel@tonic-gate 	walk_arg.need_update = 0;
17387c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
17397c478bd9Sstevel@tonic-gate 	walk_arg.new_nvlp = NULL;
17407c478bd9Sstevel@tonic-gate }
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate /*
17437c478bd9Sstevel@tonic-gate  * Returns:
17447c478bd9Sstevel@tonic-gate  *	0 - no update necessary
17457c478bd9Sstevel@tonic-gate  *	1 - update required.
17467c478bd9Sstevel@tonic-gate  *	BAM_ERROR (-1) - An error occurred
17477c478bd9Sstevel@tonic-gate  *
17487c478bd9Sstevel@tonic-gate  * Special handling for check (-n):
17497c478bd9Sstevel@tonic-gate  * ================================
17507c478bd9Sstevel@tonic-gate  * The check (-n) option produces parseable output.
17517c478bd9Sstevel@tonic-gate  * To do this, we suppress all stdout messages unrelated
17527c478bd9Sstevel@tonic-gate  * to out of sync files.
17537c478bd9Sstevel@tonic-gate  * All stderr messages are still printed though.
17547c478bd9Sstevel@tonic-gate  *
17557c478bd9Sstevel@tonic-gate  */
17567c478bd9Sstevel@tonic-gate static int
17577c478bd9Sstevel@tonic-gate update_required(char *root)
17587c478bd9Sstevel@tonic-gate {
17597c478bd9Sstevel@tonic-gate 	struct stat sb;
17607c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
17617c478bd9Sstevel@tonic-gate 	filelist_t flist;
17627c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
17637c478bd9Sstevel@tonic-gate 	int need_update;
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 	walk_arg.need_update = 0;
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	/*
17707c478bd9Sstevel@tonic-gate 	 * Without consulting stat data, check if we need update
17717c478bd9Sstevel@tonic-gate 	 */
17727c478bd9Sstevel@tonic-gate 	check_flags_and_files(root);
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 	/*
17757c478bd9Sstevel@tonic-gate 	 * In certain deployment scenarios, filestat may not
17767c478bd9Sstevel@tonic-gate 	 * exist. Ignore it during boot-archive SMF check.
17777c478bd9Sstevel@tonic-gate 	 */
17787c478bd9Sstevel@tonic-gate 	if (bam_smf_check) {
17797c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
17807c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != 0)
17817c478bd9Sstevel@tonic-gate 			return (0);
17827c478bd9Sstevel@tonic-gate 	}
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 	/*
17857c478bd9Sstevel@tonic-gate 	 * consult stat data only if we haven't made a decision
17867c478bd9Sstevel@tonic-gate 	 * about update. If checking (-n) however, we always
17877c478bd9Sstevel@tonic-gate 	 * need stat data (since we want to compare old and new)
17887c478bd9Sstevel@tonic-gate 	 */
17897c478bd9Sstevel@tonic-gate 	if (!walk_arg.need_update || bam_check)
17907c478bd9Sstevel@tonic-gate 		getoldstat(root);
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 	/*
1793*a28d77b8Svikram 	 * Check if the archive contains files that are no longer
1794*a28d77b8Svikram 	 * present on the root filesystem.
1795*a28d77b8Svikram 	 */
1796*a28d77b8Svikram 	if (!walk_arg.need_update || bam_check)
1797*a28d77b8Svikram 		check4stale(root);
1798*a28d77b8Svikram 
1799*a28d77b8Svikram 	/*
18007c478bd9Sstevel@tonic-gate 	 * read list of files
18017c478bd9Sstevel@tonic-gate 	 */
18027c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
18037c478bd9Sstevel@tonic-gate 		clear_walk_args();
18047c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18057c478bd9Sstevel@tonic-gate 	}
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	/*
18107c478bd9Sstevel@tonic-gate 	 * At this point either the update is required
18117c478bd9Sstevel@tonic-gate 	 * or the decision is pending. In either case
18127c478bd9Sstevel@tonic-gate 	 * we need to create new stat nvlist
18137c478bd9Sstevel@tonic-gate 	 */
18147c478bd9Sstevel@tonic-gate 	create_newstat();
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	/*
18177c478bd9Sstevel@tonic-gate 	 * This walk does 2 things:
18187c478bd9Sstevel@tonic-gate 	 *  	- gets new stat data for every file
18197c478bd9Sstevel@tonic-gate 	 *	- (optional) compare old and new stat data
18207c478bd9Sstevel@tonic-gate 	 */
18217c478bd9Sstevel@tonic-gate 	walk_list(root, &flist);
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 	/* done with the file list */
18247c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	/*
18277c478bd9Sstevel@tonic-gate 	 * if we didn't succeed in  creating new stat data above
18287c478bd9Sstevel@tonic-gate 	 * just return result of update check so that archive is built.
18297c478bd9Sstevel@tonic-gate 	 */
18307c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp == NULL) {
18317c478bd9Sstevel@tonic-gate 		bam_error(NO_NEW_STAT);
18327c478bd9Sstevel@tonic-gate 		need_update = walk_arg.need_update;
18337c478bd9Sstevel@tonic-gate 		clear_walk_args();
18347c478bd9Sstevel@tonic-gate 		return (need_update ? 1 : 0);
18357c478bd9Sstevel@tonic-gate 	}
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 	/*
18397c478bd9Sstevel@tonic-gate 	 * If no update required, discard newstat
18407c478bd9Sstevel@tonic-gate 	 */
18417c478bd9Sstevel@tonic-gate 	if (!walk_arg.need_update) {
18427c478bd9Sstevel@tonic-gate 		clear_walk_args();
18437c478bd9Sstevel@tonic-gate 		return (0);
18447c478bd9Sstevel@tonic-gate 	}
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 	/*
18477c478bd9Sstevel@tonic-gate 	 * At this point we need an update - so save new stat data
18487c478bd9Sstevel@tonic-gate 	 * However, if only checking (-n), don't save new stat data.
18497c478bd9Sstevel@tonic-gate 	 */
18507c478bd9Sstevel@tonic-gate 	if (!bam_check)
18517c478bd9Sstevel@tonic-gate 		savenew(root);
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	clear_walk_args();
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	return (1);
18567c478bd9Sstevel@tonic-gate }
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate static error_t
18597c478bd9Sstevel@tonic-gate create_ramdisk(char *root)
18607c478bd9Sstevel@tonic-gate {
18617c478bd9Sstevel@tonic-gate 	char *cmdline, path[PATH_MAX];
18627c478bd9Sstevel@tonic-gate 	size_t len;
18637c478bd9Sstevel@tonic-gate 	struct stat sb;
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	/*
18667c478bd9Sstevel@tonic-gate 	 * Setup command args for create_ramdisk.ksh
18677c478bd9Sstevel@tonic-gate 	 */
18687c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, CREATE_RAMDISK);
18697c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
18707c478bd9Sstevel@tonic-gate 		bam_error(ARCH_EXEC_MISS, path, strerror(errno));
18717c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18727c478bd9Sstevel@tonic-gate 	}
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	len = strlen(path) + strlen(root) + 10;	/* room for space + -R */
18757c478bd9Sstevel@tonic-gate 	cmdline = s_calloc(1, len);
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 	if (strlen(root) > 1) {
18787c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s -R %s", path, root);
18797c478bd9Sstevel@tonic-gate 		/* chop off / at the end */
18807c478bd9Sstevel@tonic-gate 		cmdline[strlen(cmdline) - 1] = '\0';
18817c478bd9Sstevel@tonic-gate 	} else
18827c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s", path);
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 	if (exec_cmd(cmdline, NULL, 0) != 0) {
18857c478bd9Sstevel@tonic-gate 		bam_error(ARCHIVE_FAIL, cmdline);
18867c478bd9Sstevel@tonic-gate 		free(cmdline);
18877c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18887c478bd9Sstevel@tonic-gate 	}
18897c478bd9Sstevel@tonic-gate 	free(cmdline);
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	/*
18927c478bd9Sstevel@tonic-gate 	 * Verify that the archive has been created
18937c478bd9Sstevel@tonic-gate 	 */
18947c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE);
18957c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
18967c478bd9Sstevel@tonic-gate 		bam_error(ARCHIVE_NOT_CREATED, path);
18977c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18987c478bd9Sstevel@tonic-gate 	}
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
19017c478bd9Sstevel@tonic-gate }
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate /*
19047c478bd9Sstevel@tonic-gate  * Checks if target filesystem is on a ramdisk
19057c478bd9Sstevel@tonic-gate  * 1 - is miniroot
19067c478bd9Sstevel@tonic-gate  * 0 - is not
19077c478bd9Sstevel@tonic-gate  * When in doubt assume it is not a ramdisk.
19087c478bd9Sstevel@tonic-gate  */
19097c478bd9Sstevel@tonic-gate static int
19107c478bd9Sstevel@tonic-gate is_ramdisk(char *root)
19117c478bd9Sstevel@tonic-gate {
19127c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
19137c478bd9Sstevel@tonic-gate 	FILE *fp;
19147c478bd9Sstevel@tonic-gate 	int found;
1915b610f78eSvikram 	char mntpt[PATH_MAX];
1916b610f78eSvikram 	char *cp;
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 	/*
19197c478bd9Sstevel@tonic-gate 	 * There are 3 situations where creating archive is
19207c478bd9Sstevel@tonic-gate 	 * of dubious value:
1921b610f78eSvikram 	 *	- create boot_archive on a lofi-mounted boot_archive
19227c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk which is the root filesystem
19237c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk mounted somewhere else
19247c478bd9Sstevel@tonic-gate 	 * The first is not easy to detect and checking for it is not
19257c478bd9Sstevel@tonic-gate 	 * worth it.
19267c478bd9Sstevel@tonic-gate 	 * The other two conditions are handled here
19277c478bd9Sstevel@tonic-gate 	 */
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
19307c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
19317c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
19327c478bd9Sstevel@tonic-gate 		return (0);
19337c478bd9Sstevel@tonic-gate 	}
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
19367c478bd9Sstevel@tonic-gate 
1937b610f78eSvikram 	/*
1938b610f78eSvikram 	 * Remove any trailing / from the mount point
1939b610f78eSvikram 	 */
1940b610f78eSvikram 	(void) strlcpy(mntpt, root, sizeof (mntpt));
1941b610f78eSvikram 	if (strcmp(root, "/") != 0) {
1942b610f78eSvikram 		cp = mntpt + strlen(mntpt) - 1;
1943b610f78eSvikram 		if (*cp == '/')
1944b610f78eSvikram 			*cp = '\0';
1945b610f78eSvikram 	}
19467c478bd9Sstevel@tonic-gate 	found = 0;
19477c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
1948b610f78eSvikram 		if (strcmp(mnt.mnt_mountp, mntpt) == 0) {
19497c478bd9Sstevel@tonic-gate 			found = 1;
19507c478bd9Sstevel@tonic-gate 			break;
19517c478bd9Sstevel@tonic-gate 		}
19527c478bd9Sstevel@tonic-gate 	}
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	if (!found) {
19557c478bd9Sstevel@tonic-gate 		if (bam_verbose)
1956b610f78eSvikram 			bam_error(NOT_IN_MNTTAB, mntpt);
19577c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
19587c478bd9Sstevel@tonic-gate 		return (0);
19597c478bd9Sstevel@tonic-gate 	}
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) {
19627c478bd9Sstevel@tonic-gate 		if (bam_verbose)
19637c478bd9Sstevel@tonic-gate 			bam_error(IS_RAMDISK, bam_root);
19647c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
19657c478bd9Sstevel@tonic-gate 		return (1);
19667c478bd9Sstevel@tonic-gate 	}
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 	return (0);
19717c478bd9Sstevel@tonic-gate }
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate static int
19747c478bd9Sstevel@tonic-gate is_newboot(char *root)
19757c478bd9Sstevel@tonic-gate {
19767c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
19777c478bd9Sstevel@tonic-gate 	struct stat sb;
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 	/*
19807c478bd9Sstevel@tonic-gate 	 * We can't boot without MULTI_BOOT
19817c478bd9Sstevel@tonic-gate 	 */
19827c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, MULTI_BOOT);
19837c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
19847c478bd9Sstevel@tonic-gate 		if (bam_verbose)
19857c478bd9Sstevel@tonic-gate 			bam_print(FILE_MISS, path);
19867c478bd9Sstevel@tonic-gate 		return (0);
19877c478bd9Sstevel@tonic-gate 	}
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate 	/*
19907c478bd9Sstevel@tonic-gate 	 * We can't generate archive without GRUB_DIR
19917c478bd9Sstevel@tonic-gate 	 */
19927c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, GRUB_DIR);
19937c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
19947c478bd9Sstevel@tonic-gate 		if (bam_verbose)
19957c478bd9Sstevel@tonic-gate 			bam_print(DIR_MISS, path);
19967c478bd9Sstevel@tonic-gate 		return (0);
19977c478bd9Sstevel@tonic-gate 	}
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 	return (1);
20007c478bd9Sstevel@tonic-gate }
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate static int
20037c478bd9Sstevel@tonic-gate is_readonly(char *root)
20047c478bd9Sstevel@tonic-gate {
20057c478bd9Sstevel@tonic-gate 	struct statvfs vfs;
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	/*
20087c478bd9Sstevel@tonic-gate 	 * Check for RDONLY filesystem
20097c478bd9Sstevel@tonic-gate 	 * When in doubt assume it is not readonly
20107c478bd9Sstevel@tonic-gate 	 */
20117c478bd9Sstevel@tonic-gate 	if (statvfs(root, &vfs) != 0) {
20127c478bd9Sstevel@tonic-gate 		if (bam_verbose)
20137c478bd9Sstevel@tonic-gate 			bam_error(STATVFS_FAIL, root, strerror(errno));
20147c478bd9Sstevel@tonic-gate 		return (0);
20157c478bd9Sstevel@tonic-gate 	}
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 	if (vfs.f_flag & ST_RDONLY) {
20187c478bd9Sstevel@tonic-gate 		return (1);
20197c478bd9Sstevel@tonic-gate 	}
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 	return (0);
20227c478bd9Sstevel@tonic-gate }
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate static error_t
20257c478bd9Sstevel@tonic-gate update_archive(char *root, char *opt)
20267c478bd9Sstevel@tonic-gate {
20277c478bd9Sstevel@tonic-gate 	error_t ret;
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 	assert(root);
20307c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	/*
2033b610f78eSvikram 	 * root must belong to a GRUB boot OS,
20347c478bd9Sstevel@tonic-gate 	 * don't care on sparc except for diskless clients
20357c478bd9Sstevel@tonic-gate 	 */
20367c478bd9Sstevel@tonic-gate 	if (!is_newboot(root)) {
2037b610f78eSvikram 		/*
2038b610f78eSvikram 		 * Emit message only if not in context of update_all.
2039b610f78eSvikram 		 * If in update_all, emit only if verbose flag is set.
2040b610f78eSvikram 		 */
2041b610f78eSvikram 		if (!bam_update_all || bam_verbose)
2042b610f78eSvikram 			bam_print(NOT_GRUB_BOOT, root);
20437c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
20447c478bd9Sstevel@tonic-gate 	}
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	/*
20478c1b6884Sszhou 	 * If smf check is requested when / is writable (can happen
20488c1b6884Sszhou 	 * on first reboot following an upgrade because service
20498c1b6884Sszhou 	 * dependency is messed up), skip the check.
20508c1b6884Sszhou 	 */
20518c1b6884Sszhou 	if (bam_smf_check && !bam_root_readonly)
20528c1b6884Sszhou 		return (BAM_SUCCESS);
20538c1b6884Sszhou 
20548c1b6884Sszhou 	/*
20558c1b6884Sszhou 	 * root must be writable. This check applies to alternate
20568c1b6884Sszhou 	 * root (-R option); bam_root_readonly applies to '/' only.
20577c478bd9Sstevel@tonic-gate 	 * Note: statvfs() does not always report the truth
20587c478bd9Sstevel@tonic-gate 	 */
205961cb17bdSsetje 	if (!bam_smf_check && !bam_check && is_readonly(root)) {
20608c1b6884Sszhou 		if (bam_verbose)
20617c478bd9Sstevel@tonic-gate 			bam_print(RDONLY_FS, root);
20627c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
20637c478bd9Sstevel@tonic-gate 	}
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 	/*
20667c478bd9Sstevel@tonic-gate 	 * Don't generate archive on ramdisk
20677c478bd9Sstevel@tonic-gate 	 */
20687c478bd9Sstevel@tonic-gate 	if (is_ramdisk(root)) {
20697c478bd9Sstevel@tonic-gate 		if (bam_verbose)
20707c478bd9Sstevel@tonic-gate 			bam_print(SKIP_RAMDISK);
20717c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
20727c478bd9Sstevel@tonic-gate 	}
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	/*
20757c478bd9Sstevel@tonic-gate 	 * Now check if updated is really needed
20767c478bd9Sstevel@tonic-gate 	 */
20777c478bd9Sstevel@tonic-gate 	ret = update_required(root);
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 	/*
20807c478bd9Sstevel@tonic-gate 	 * The check command (-n) is *not* a dry run
20817c478bd9Sstevel@tonic-gate 	 * It only checks if the archive is in sync.
20827c478bd9Sstevel@tonic-gate 	 */
20837c478bd9Sstevel@tonic-gate 	if (bam_check) {
20847c478bd9Sstevel@tonic-gate 		bam_exit((ret != 0) ? 1 : 0);
20857c478bd9Sstevel@tonic-gate 	}
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 	if (ret == 1) {
20887c478bd9Sstevel@tonic-gate 		/* create the ramdisk */
20897c478bd9Sstevel@tonic-gate 		ret = create_ramdisk(root);
20907c478bd9Sstevel@tonic-gate 	}
20917c478bd9Sstevel@tonic-gate 	return (ret);
20927c478bd9Sstevel@tonic-gate }
20937c478bd9Sstevel@tonic-gate 
2094b610f78eSvikram static void
2095fbac2b2bSvikram update_fdisk(void)
2096fbac2b2bSvikram {
2097fbac2b2bSvikram 	struct stat sb;
2098fbac2b2bSvikram 	char cmd[PATH_MAX];
2099fbac2b2bSvikram 	int ret1, ret2;
2100fbac2b2bSvikram 
2101fbac2b2bSvikram 	assert(stat(GRUB_fdisk, &sb) == 0);
2102fbac2b2bSvikram 	assert(stat(GRUB_fdisk_target, &sb) == 0);
2103fbac2b2bSvikram 
2104fbac2b2bSvikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/fdisk -F %s `/bin/cat %s`",
2105fbac2b2bSvikram 	    GRUB_fdisk, GRUB_fdisk_target);
2106fbac2b2bSvikram 
2107fbac2b2bSvikram 	bam_print(UPDATING_FDISK);
2108fbac2b2bSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2109fbac2b2bSvikram 		bam_error(FDISK_UPDATE_FAILED);
2110fbac2b2bSvikram 	}
2111fbac2b2bSvikram 
2112fbac2b2bSvikram 	/*
2113fbac2b2bSvikram 	 * We are done, remove the files.
2114fbac2b2bSvikram 	 */
2115fbac2b2bSvikram 	ret1 = unlink(GRUB_fdisk);
2116fbac2b2bSvikram 	ret2 = unlink(GRUB_fdisk_target);
2117fbac2b2bSvikram 	if (ret1 != 0 || ret2 != 0) {
2118fbac2b2bSvikram 		bam_error(FILE_REMOVE_FAILED, GRUB_fdisk, GRUB_fdisk_target);
2119fbac2b2bSvikram 	}
2120fbac2b2bSvikram }
2121fbac2b2bSvikram 
2122fbac2b2bSvikram static void
2123b610f78eSvikram restore_grub_slice(void)
2124b610f78eSvikram {
2125b610f78eSvikram 	struct stat sb;
2126b610f78eSvikram 	char *mntpt, *physlice;
2127b610f78eSvikram 	int mnted;	/* set if we did a mount */
2128b610f78eSvikram 	char menupath[PATH_MAX], cmd[PATH_MAX];
2129b610f78eSvikram 
2130b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
2131b610f78eSvikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
2132b610f78eSvikram 		return;
2133b610f78eSvikram 	}
2134b610f78eSvikram 
2135b610f78eSvikram 	/*
2136b610f78eSvikram 	 * If we are doing an luactivate, don't attempt to restore GRUB or else
2137b610f78eSvikram 	 * we may not be able to get to DCA boot environments. Let luactivate
2138b610f78eSvikram 	 * handle GRUB/DCA installation
2139b610f78eSvikram 	 */
2140b610f78eSvikram 	if (stat(LU_ACTIVATE_FILE, &sb) == 0) {
2141b610f78eSvikram 		return;
2142b610f78eSvikram 	}
2143b610f78eSvikram 
2144b610f78eSvikram 	mnted = 0;
2145b610f78eSvikram 	physlice = NULL;
214640541d5dSvikram 	mntpt = mount_grub_slice(&mnted, &physlice, NULL, NULL);
2147b610f78eSvikram 	if (mntpt == NULL) {
2148b610f78eSvikram 		bam_error(CANNOT_RESTORE_GRUB_SLICE);
2149b610f78eSvikram 		return;
2150b610f78eSvikram 	}
2151b610f78eSvikram 
2152b610f78eSvikram 	(void) snprintf(menupath, sizeof (menupath), "%s%s", mntpt, GRUB_MENU);
2153b610f78eSvikram 	if (stat(menupath, &sb) == 0) {
215440541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2155b610f78eSvikram 		return;
2156b610f78eSvikram 	}
2157b610f78eSvikram 
2158b610f78eSvikram 	/*
2159b610f78eSvikram 	 * The menu is missing - we need to do a restore
2160b610f78eSvikram 	 */
2161b610f78eSvikram 	bam_print(RESTORING_GRUB);
2162b610f78eSvikram 
2163b610f78eSvikram 	(void) snprintf(cmd, sizeof (cmd), "%s %s %s %s",
2164b610f78eSvikram 	    INSTALLGRUB, STAGE1, STAGE2, physlice);
2165b610f78eSvikram 
2166b610f78eSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2167b610f78eSvikram 		bam_error(RESTORE_GRUB_FAILED);
216840541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2169b610f78eSvikram 		return;
2170b610f78eSvikram 	}
2171b610f78eSvikram 
2172b610f78eSvikram 	if (stat(GRUB_backup_menu, &sb) != 0) {
2173b610f78eSvikram 		bam_error(MISSING_BACKUP_MENU,
2174b610f78eSvikram 		    GRUB_backup_menu, strerror(errno));
217540541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2176b610f78eSvikram 		return;
2177b610f78eSvikram 	}
2178b610f78eSvikram 
2179b610f78eSvikram 	(void) snprintf(cmd, sizeof (cmd), "/bin/cp %s %s",
2180b610f78eSvikram 	    GRUB_backup_menu, menupath);
2181b610f78eSvikram 
2182b610f78eSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2183b610f78eSvikram 		bam_error(RESTORE_MENU_FAILED, menupath);
218440541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2185b610f78eSvikram 		return;
2186b610f78eSvikram 	}
2187b610f78eSvikram 
2188b610f78eSvikram 	/* Success */
218940541d5dSvikram 	umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2190b610f78eSvikram }
2191b610f78eSvikram 
21927c478bd9Sstevel@tonic-gate static error_t
21937c478bd9Sstevel@tonic-gate update_all(char *root, char *opt)
21947c478bd9Sstevel@tonic-gate {
21957c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
21967c478bd9Sstevel@tonic-gate 	struct stat sb;
21977c478bd9Sstevel@tonic-gate 	FILE *fp;
21987c478bd9Sstevel@tonic-gate 	char multibt[PATH_MAX];
21997c478bd9Sstevel@tonic-gate 	error_t ret = BAM_SUCCESS;
2200fbac2b2bSvikram 	int ret1, ret2;
22017c478bd9Sstevel@tonic-gate 
220240541d5dSvikram 	assert(root);
22037c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
22047c478bd9Sstevel@tonic-gate 
220540541d5dSvikram 	if (bam_rootlen != 1 || *root != '/') {
220640541d5dSvikram 		elide_trailing_slash(root, multibt, sizeof (multibt));
220740541d5dSvikram 		bam_error(ALT_ROOT_INVALID, multibt);
220840541d5dSvikram 		return (BAM_ERROR);
220940541d5dSvikram 	}
221040541d5dSvikram 
22117c478bd9Sstevel@tonic-gate 	/*
22127c478bd9Sstevel@tonic-gate 	 * First update archive for current root
22137c478bd9Sstevel@tonic-gate 	 */
22147c478bd9Sstevel@tonic-gate 	if (update_archive(root, opt) != BAM_SUCCESS)
22157c478bd9Sstevel@tonic-gate 		ret = BAM_ERROR;
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 	/*
22187c478bd9Sstevel@tonic-gate 	 * Now walk the mount table, performing archive update
22197c478bd9Sstevel@tonic-gate 	 * for all mounted Newboot root filesystems
22207c478bd9Sstevel@tonic-gate 	 */
22217c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
22227c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
22237c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
2224b610f78eSvikram 		ret = BAM_ERROR;
2225b610f78eSvikram 		goto out;
22267c478bd9Sstevel@tonic-gate 	}
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
22317c478bd9Sstevel@tonic-gate 		if (mnt.mnt_special == NULL)
22327c478bd9Sstevel@tonic-gate 			continue;
22337c478bd9Sstevel@tonic-gate 		if (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0)
22347c478bd9Sstevel@tonic-gate 			continue;
22357c478bd9Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, "/") == 0)
22367c478bd9Sstevel@tonic-gate 			continue;
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate 		(void) snprintf(multibt, sizeof (multibt), "%s%s",
22397c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp, MULTI_BOOT);
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 		if (stat(multibt, &sb) == -1)
22427c478bd9Sstevel@tonic-gate 			continue;
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate 		/*
22457c478bd9Sstevel@tonic-gate 		 * We put a trailing slash to be consistent with root = "/"
22467c478bd9Sstevel@tonic-gate 		 * case, such that we don't have to print // in some cases.
22477c478bd9Sstevel@tonic-gate 		 */
22487c478bd9Sstevel@tonic-gate 		(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
22497c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp);
22507c478bd9Sstevel@tonic-gate 		bam_rootlen = strlen(rootbuf);
22517c478bd9Sstevel@tonic-gate 		if (update_archive(rootbuf, opt) != BAM_SUCCESS)
22527c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
22537c478bd9Sstevel@tonic-gate 	}
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
22567c478bd9Sstevel@tonic-gate 
2257b610f78eSvikram out:
2258b610f78eSvikram 	if (stat(GRUB_slice, &sb) == 0) {
2259b610f78eSvikram 		restore_grub_slice();
2260b610f78eSvikram 	}
2261b610f78eSvikram 
2262fbac2b2bSvikram 	/*
2263fbac2b2bSvikram 	 * Update fdisk table as we go down. Updating it when
2264fbac2b2bSvikram 	 * the system is running will confuse biosdev.
2265fbac2b2bSvikram 	 */
2266fbac2b2bSvikram 	ret1 = stat(GRUB_fdisk, &sb);
2267fbac2b2bSvikram 	ret2 = stat(GRUB_fdisk_target, &sb);
2268fbac2b2bSvikram 	if ((ret1 == 0) && (ret2 == 0)) {
2269fbac2b2bSvikram 		update_fdisk();
2270fbac2b2bSvikram 	} else if ((ret1 == 0) ^ (ret2 == 0)) {
2271fbac2b2bSvikram 		/*
2272fbac2b2bSvikram 		 * It is an error for one file to be
2273fbac2b2bSvikram 		 * present and the other absent.
2274fbac2b2bSvikram 		 * It is normal for both files to be
2275fbac2b2bSvikram 		 * absent - it indicates that no fdisk
2276fbac2b2bSvikram 		 * update is required.
2277fbac2b2bSvikram 		 */
2278fbac2b2bSvikram 		bam_error(MISSING_FDISK_FILE,
2279fbac2b2bSvikram 		    ret1 ? GRUB_fdisk : GRUB_fdisk_target);
2280fbac2b2bSvikram 		ret = BAM_ERROR;
2281fbac2b2bSvikram 	}
2282fbac2b2bSvikram 
22837c478bd9Sstevel@tonic-gate 	return (ret);
22847c478bd9Sstevel@tonic-gate }
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate static void
22877c478bd9Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp)
22887c478bd9Sstevel@tonic-gate {
22897c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
22907c478bd9Sstevel@tonic-gate 		mp->start = lp;
22917c478bd9Sstevel@tonic-gate 	} else {
22927c478bd9Sstevel@tonic-gate 		mp->end->next = lp;
22938c1b6884Sszhou 		lp->prev = mp->end;
22947c478bd9Sstevel@tonic-gate 	}
22957c478bd9Sstevel@tonic-gate 	mp->end = lp;
22967c478bd9Sstevel@tonic-gate }
22977c478bd9Sstevel@tonic-gate 
22988c1b6884Sszhou static void
22998c1b6884Sszhou unlink_line(menu_t *mp, line_t *lp)
23008c1b6884Sszhou {
23018c1b6884Sszhou 	/* unlink from list */
23028c1b6884Sszhou 	if (lp->prev)
23038c1b6884Sszhou 		lp->prev->next = lp->next;
23048c1b6884Sszhou 	else
23058c1b6884Sszhou 		mp->start = lp->next;
23068c1b6884Sszhou 	if (lp->next)
23078c1b6884Sszhou 		lp->next->prev = lp->prev;
23088c1b6884Sszhou 	else
23098c1b6884Sszhou 		mp->end = lp->prev;
23108c1b6884Sszhou }
23118c1b6884Sszhou 
23128c1b6884Sszhou static entry_t *
23138c1b6884Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end)
23148c1b6884Sszhou {
23158c1b6884Sszhou 	entry_t *ent, *prev;
23168c1b6884Sszhou 
23178c1b6884Sszhou 	ent = s_calloc(1, sizeof (entry_t));
23188c1b6884Sszhou 	ent->start = start;
23198c1b6884Sszhou 	ent->end = end;
23208c1b6884Sszhou 
23218c1b6884Sszhou 	if (mp->entries == NULL) {
23228c1b6884Sszhou 		mp->entries = ent;
23238c1b6884Sszhou 		return (ent);
23248c1b6884Sszhou 	}
23258c1b6884Sszhou 
23268c1b6884Sszhou 	prev = mp->entries;
23278c1b6884Sszhou 	while (prev->next)
23288c1b6884Sszhou 		prev = prev-> next;
23298c1b6884Sszhou 	prev->next = ent;
23308c1b6884Sszhou 	ent->prev = prev;
23318c1b6884Sszhou 	return (ent);
23328c1b6884Sszhou }
23338c1b6884Sszhou 
23348c1b6884Sszhou static void
23358c1b6884Sszhou boot_entry_addline(entry_t *ent, line_t *lp)
23368c1b6884Sszhou {
23378c1b6884Sszhou 	if (ent)
23388c1b6884Sszhou 		ent->end = lp;
23398c1b6884Sszhou }
23408c1b6884Sszhou 
23417c478bd9Sstevel@tonic-gate /*
23427c478bd9Sstevel@tonic-gate  * A line in menu.lst looks like
23437c478bd9Sstevel@tonic-gate  * [ ]*<cmd>[ \t=]*<arg>*
23447c478bd9Sstevel@tonic-gate  */
23457c478bd9Sstevel@tonic-gate static void
23467c478bd9Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum)
23477c478bd9Sstevel@tonic-gate {
23487c478bd9Sstevel@tonic-gate 	/*
23497c478bd9Sstevel@tonic-gate 	 * save state across calls. This is so that
23507c478bd9Sstevel@tonic-gate 	 * header gets the right entry# after title has
23517c478bd9Sstevel@tonic-gate 	 * been processed
23527c478bd9Sstevel@tonic-gate 	 */
23538c1b6884Sszhou 	static line_t *prev = NULL;
23548c1b6884Sszhou 	static entry_t *curr_ent = NULL;
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate 	line_t	*lp;
23577c478bd9Sstevel@tonic-gate 	char *cmd, *sep, *arg;
23587c478bd9Sstevel@tonic-gate 	char save, *cp, *line;
23597c478bd9Sstevel@tonic-gate 	menu_flag_t flag = BAM_INVALID;
23607c478bd9Sstevel@tonic-gate 
23617c478bd9Sstevel@tonic-gate 	if (str == NULL) {
23627c478bd9Sstevel@tonic-gate 		return;
23637c478bd9Sstevel@tonic-gate 	}
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 	/*
23667c478bd9Sstevel@tonic-gate 	 * First save a copy of the entire line.
23677c478bd9Sstevel@tonic-gate 	 * We use this later to set the line field.
23687c478bd9Sstevel@tonic-gate 	 */
23697c478bd9Sstevel@tonic-gate 	line = s_strdup(str);
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	/* Eat up leading whitespace */
23727c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
23737c478bd9Sstevel@tonic-gate 		str++;
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 	if (*str == '#') {		/* comment */
23767c478bd9Sstevel@tonic-gate 		cmd = s_strdup("#");
23777c478bd9Sstevel@tonic-gate 		sep = NULL;
23787c478bd9Sstevel@tonic-gate 		arg = s_strdup(str + 1);
23797c478bd9Sstevel@tonic-gate 		flag = BAM_COMMENT;
23807c478bd9Sstevel@tonic-gate 	} else if (*str == '\0') {	/* blank line */
23817c478bd9Sstevel@tonic-gate 		cmd = sep = arg = NULL;
23827c478bd9Sstevel@tonic-gate 		flag = BAM_EMPTY;
23837c478bd9Sstevel@tonic-gate 	} else {
23847c478bd9Sstevel@tonic-gate 		/*
23857c478bd9Sstevel@tonic-gate 		 * '=' is not a documented separator in grub syntax.
23867c478bd9Sstevel@tonic-gate 		 * However various development bits use '=' as a
23877c478bd9Sstevel@tonic-gate 		 * separator. In addition, external users also
23887c478bd9Sstevel@tonic-gate 		 * use = as a separator. So we will allow that usage.
23897c478bd9Sstevel@tonic-gate 		 */
23907c478bd9Sstevel@tonic-gate 		cp = str;
23917c478bd9Sstevel@tonic-gate 		while (*str != ' ' && *str != '\t' && *str != '=') {
23927c478bd9Sstevel@tonic-gate 			if (*str == '\0') {
23937c478bd9Sstevel@tonic-gate 				cmd = s_strdup(cp);
23947c478bd9Sstevel@tonic-gate 				sep = arg = NULL;
23957c478bd9Sstevel@tonic-gate 				break;
23967c478bd9Sstevel@tonic-gate 			}
23977c478bd9Sstevel@tonic-gate 			str++;
23987c478bd9Sstevel@tonic-gate 		}
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 		if (*str != '\0') {
24017c478bd9Sstevel@tonic-gate 			save = *str;
24027c478bd9Sstevel@tonic-gate 			*str = '\0';
24037c478bd9Sstevel@tonic-gate 			cmd = s_strdup(cp);
24047c478bd9Sstevel@tonic-gate 			*str = save;
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 			str++;
24077c478bd9Sstevel@tonic-gate 			save = *str;
24087c478bd9Sstevel@tonic-gate 			*str = '\0';
24097c478bd9Sstevel@tonic-gate 			sep = s_strdup(str - 1);
24107c478bd9Sstevel@tonic-gate 			*str = save;
24117c478bd9Sstevel@tonic-gate 
24127c478bd9Sstevel@tonic-gate 			while (*str == ' ' || *str == '\t')
24137c478bd9Sstevel@tonic-gate 				str++;
24147c478bd9Sstevel@tonic-gate 			if (*str == '\0')
24157c478bd9Sstevel@tonic-gate 				arg = NULL;
24167c478bd9Sstevel@tonic-gate 			else
24177c478bd9Sstevel@tonic-gate 				arg = s_strdup(str);
24187c478bd9Sstevel@tonic-gate 		}
24197c478bd9Sstevel@tonic-gate 	}
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 	lp->cmd = cmd;
24247c478bd9Sstevel@tonic-gate 	lp->sep = sep;
24257c478bd9Sstevel@tonic-gate 	lp->arg = arg;
24267c478bd9Sstevel@tonic-gate 	lp->line = line;
24277c478bd9Sstevel@tonic-gate 	lp->lineNum = ++(*lineNum);
24287c478bd9Sstevel@tonic-gate 	if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) {
24297c478bd9Sstevel@tonic-gate 		lp->entryNum = ++(*entryNum);
24307c478bd9Sstevel@tonic-gate 		lp->flags = BAM_TITLE;
24317c478bd9Sstevel@tonic-gate 		if (prev && prev->flags == BAM_COMMENT &&
24328c1b6884Sszhou 		    prev->arg && strcmp(prev->arg, BAM_HDR) == 0) {
24337c478bd9Sstevel@tonic-gate 			prev->entryNum = lp->entryNum;
24348c1b6884Sszhou 			curr_ent = boot_entry_new(mp, prev, lp);
24358c1b6884Sszhou 		} else {
24368c1b6884Sszhou 			curr_ent = boot_entry_new(mp, lp, lp);
24378c1b6884Sszhou 		}
24387c478bd9Sstevel@tonic-gate 	} else if (flag != BAM_INVALID) {
24397c478bd9Sstevel@tonic-gate 		/*
24407c478bd9Sstevel@tonic-gate 		 * For header comments, the entry# is "fixed up"
24417c478bd9Sstevel@tonic-gate 		 * by the subsequent title
24427c478bd9Sstevel@tonic-gate 		 */
24437c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
24447c478bd9Sstevel@tonic-gate 		lp->flags = flag;
24457c478bd9Sstevel@tonic-gate 	} else {
24467c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
24477c478bd9Sstevel@tonic-gate 		lp->flags = (*entryNum == ENTRY_INIT) ? BAM_GLOBAL : BAM_ENTRY;
24487c478bd9Sstevel@tonic-gate 	}
24497c478bd9Sstevel@tonic-gate 
24508c1b6884Sszhou 	/* record default, old default, and entry line ranges */
24518c1b6884Sszhou 	if (lp->flags == BAM_GLOBAL &&
24528c1b6884Sszhou 	    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) {
24538c1b6884Sszhou 		mp->curdefault = lp;
24548c1b6884Sszhou 	} else if (lp->flags == BAM_COMMENT &&
24558c1b6884Sszhou 	    strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) {
24568c1b6884Sszhou 		mp->olddefault = lp;
24578c1b6884Sszhou 	} else if (lp->flags == BAM_ENTRY ||
24588c1b6884Sszhou 	    (lp->flags == BAM_COMMENT && strcmp(lp->arg, BAM_FTR) == 0)) {
24598c1b6884Sszhou 		boot_entry_addline(curr_ent, lp);
24608c1b6884Sszhou 	}
24617c478bd9Sstevel@tonic-gate 	append_line(mp, lp);
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate 	prev = lp;
24647c478bd9Sstevel@tonic-gate }
24657c478bd9Sstevel@tonic-gate 
246640541d5dSvikram static void
246740541d5dSvikram update_numbering(menu_t *mp)
246840541d5dSvikram {
246940541d5dSvikram 	int lineNum;
247040541d5dSvikram 	int entryNum;
247140541d5dSvikram 	int old_default_value;
247240541d5dSvikram 	line_t *lp, *prev, *default_lp, *default_entry;
247340541d5dSvikram 	char buf[PATH_MAX];
247440541d5dSvikram 
247540541d5dSvikram 	if (mp->start == NULL) {
247640541d5dSvikram 		return;
247740541d5dSvikram 	}
247840541d5dSvikram 
247940541d5dSvikram 	lineNum = LINE_INIT;
248040541d5dSvikram 	entryNum = ENTRY_INIT;
248140541d5dSvikram 	old_default_value = ENTRY_INIT;
248240541d5dSvikram 	lp = default_lp = default_entry = NULL;
248340541d5dSvikram 
248440541d5dSvikram 	prev = NULL;
248540541d5dSvikram 	for (lp = mp->start; lp; prev = lp, lp = lp->next) {
248640541d5dSvikram 		lp->lineNum = ++lineNum;
248740541d5dSvikram 
248840541d5dSvikram 		/*
248940541d5dSvikram 		 * Get the value of the default command
249040541d5dSvikram 		 */
249140541d5dSvikram 		if (lp->entryNum == ENTRY_INIT && lp->cmd &&
249240541d5dSvikram 		    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 &&
249340541d5dSvikram 		    lp->arg) {
249440541d5dSvikram 			old_default_value = atoi(lp->arg);
249540541d5dSvikram 			default_lp = lp;
249640541d5dSvikram 		}
249740541d5dSvikram 
249840541d5dSvikram 		/*
249940541d5dSvikram 		 * If not boot entry, nothing else to fix for this
250040541d5dSvikram 		 * entry
250140541d5dSvikram 		 */
250240541d5dSvikram 		if (lp->entryNum == ENTRY_INIT)
250340541d5dSvikram 			continue;
250440541d5dSvikram 
250540541d5dSvikram 		/*
250640541d5dSvikram 		 * Record the position of the default entry.
250740541d5dSvikram 		 * The following works because global
250840541d5dSvikram 		 * commands like default and timeout should precede
250940541d5dSvikram 		 * actual boot entries, so old_default_value
251040541d5dSvikram 		 * is already known (or default cmd is missing).
251140541d5dSvikram 		 */
251240541d5dSvikram 		if (default_entry == NULL &&
251340541d5dSvikram 		    old_default_value != ENTRY_INIT &&
251440541d5dSvikram 		    lp->entryNum == old_default_value) {
251540541d5dSvikram 			default_entry = lp;
251640541d5dSvikram 		}
251740541d5dSvikram 
251840541d5dSvikram 		/*
251940541d5dSvikram 		 * Now fixup the entry number
252040541d5dSvikram 		 */
252140541d5dSvikram 		if (lp->cmd && strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) {
252240541d5dSvikram 			lp->entryNum = ++entryNum;
252340541d5dSvikram 			/* fixup the bootadm header */
252440541d5dSvikram 			if (prev && prev->flags == BAM_COMMENT &&
252540541d5dSvikram 			    prev->arg && strcmp(prev->arg, BAM_HDR) == 0) {
252640541d5dSvikram 				prev->entryNum = lp->entryNum;
252740541d5dSvikram 			}
252840541d5dSvikram 		} else {
252940541d5dSvikram 			lp->entryNum = entryNum;
253040541d5dSvikram 		}
253140541d5dSvikram 	}
253240541d5dSvikram 
253340541d5dSvikram 	/*
253440541d5dSvikram 	 * No default command in menu, simply return
253540541d5dSvikram 	 */
253640541d5dSvikram 	if (default_lp == NULL) {
253740541d5dSvikram 		return;
253840541d5dSvikram 	}
253940541d5dSvikram 
254040541d5dSvikram 	free(default_lp->arg);
254140541d5dSvikram 	free(default_lp->line);
254240541d5dSvikram 
254340541d5dSvikram 	if (default_entry == NULL) {
254440541d5dSvikram 		default_lp->arg = s_strdup("0");
254540541d5dSvikram 	} else {
254640541d5dSvikram 		(void) snprintf(buf, sizeof (buf), "%d",
254740541d5dSvikram 		    default_entry->entryNum);
254840541d5dSvikram 		default_lp->arg = s_strdup(buf);
254940541d5dSvikram 	}
255040541d5dSvikram 
255140541d5dSvikram 	/*
255240541d5dSvikram 	 * The following is required since only the line field gets
255340541d5dSvikram 	 * written back to menu.lst
255440541d5dSvikram 	 */
255540541d5dSvikram 	(void) snprintf(buf, sizeof (buf), "%s%s%s",
255640541d5dSvikram 	    menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg);
255740541d5dSvikram 	default_lp->line = s_strdup(buf);
255840541d5dSvikram }
255940541d5dSvikram 
256040541d5dSvikram 
25617c478bd9Sstevel@tonic-gate static menu_t *
25627c478bd9Sstevel@tonic-gate menu_read(char *menu_path)
25637c478bd9Sstevel@tonic-gate {
25647c478bd9Sstevel@tonic-gate 	FILE *fp;
25657c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE], *cp;
25667c478bd9Sstevel@tonic-gate 	menu_t *mp;
25677c478bd9Sstevel@tonic-gate 	int line, entry, len, n;
25687c478bd9Sstevel@tonic-gate 
25697c478bd9Sstevel@tonic-gate 	mp = s_calloc(1, sizeof (menu_t));
25707c478bd9Sstevel@tonic-gate 
25717c478bd9Sstevel@tonic-gate 	fp = fopen(menu_path, "r");
25727c478bd9Sstevel@tonic-gate 	if (fp == NULL) { /* Let the caller handle this error */
25737c478bd9Sstevel@tonic-gate 		return (mp);
25747c478bd9Sstevel@tonic-gate 	}
25757c478bd9Sstevel@tonic-gate 
25767c478bd9Sstevel@tonic-gate 
25777c478bd9Sstevel@tonic-gate 	/* Note: GRUB boot entry number starts with 0 */
25787c478bd9Sstevel@tonic-gate 	line = LINE_INIT;
25797c478bd9Sstevel@tonic-gate 	entry = ENTRY_INIT;
25807c478bd9Sstevel@tonic-gate 	cp = buf;
25817c478bd9Sstevel@tonic-gate 	len = sizeof (buf);
25827c478bd9Sstevel@tonic-gate 	while (s_fgets(cp, len, fp) != NULL) {
25837c478bd9Sstevel@tonic-gate 		n = strlen(cp);
25847c478bd9Sstevel@tonic-gate 		if (cp[n - 1] == '\\') {
25857c478bd9Sstevel@tonic-gate 			len -= n - 1;
25867c478bd9Sstevel@tonic-gate 			assert(len >= 2);
25877c478bd9Sstevel@tonic-gate 			cp += n - 1;
25887c478bd9Sstevel@tonic-gate 			continue;
25897c478bd9Sstevel@tonic-gate 		}
25907c478bd9Sstevel@tonic-gate 		line_parser(mp, buf, &line, &entry);
25917c478bd9Sstevel@tonic-gate 		cp = buf;
25927c478bd9Sstevel@tonic-gate 		len = sizeof (buf);
25937c478bd9Sstevel@tonic-gate 	}
25947c478bd9Sstevel@tonic-gate 
25957c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
25967c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, menu_path, strerror(errno));
25977c478bd9Sstevel@tonic-gate 	}
25987c478bd9Sstevel@tonic-gate 
25997c478bd9Sstevel@tonic-gate 	return (mp);
26007c478bd9Sstevel@tonic-gate }
26017c478bd9Sstevel@tonic-gate 
26027c478bd9Sstevel@tonic-gate static error_t
26037c478bd9Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title)
26047c478bd9Sstevel@tonic-gate {
26057c478bd9Sstevel@tonic-gate 	char *eq;
26067c478bd9Sstevel@tonic-gate 	char *opt_dup;
26077c478bd9Sstevel@tonic-gate 	int entryNum;
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate 	assert(mp);
26107c478bd9Sstevel@tonic-gate 	assert(mp->start);
26117c478bd9Sstevel@tonic-gate 	assert(opt);
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate 	opt_dup = s_strdup(opt);
26147c478bd9Sstevel@tonic-gate 
26157c478bd9Sstevel@tonic-gate 	if (entry)
26167c478bd9Sstevel@tonic-gate 		*entry = ENTRY_INIT;
26177c478bd9Sstevel@tonic-gate 	if (title)
26187c478bd9Sstevel@tonic-gate 		*title = NULL;
26197c478bd9Sstevel@tonic-gate 
26207c478bd9Sstevel@tonic-gate 	eq = strchr(opt_dup, '=');
26217c478bd9Sstevel@tonic-gate 	if (eq == NULL) {
26227c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
26237c478bd9Sstevel@tonic-gate 		free(opt_dup);
26247c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
26257c478bd9Sstevel@tonic-gate 	}
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate 	*eq = '\0';
26287c478bd9Sstevel@tonic-gate 	if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) {
26297c478bd9Sstevel@tonic-gate 		assert(mp->end);
26307c478bd9Sstevel@tonic-gate 		entryNum = s_strtol(eq + 1);
26317c478bd9Sstevel@tonic-gate 		if (entryNum < 0 || entryNum > mp->end->entryNum) {
26327c478bd9Sstevel@tonic-gate 			bam_error(INVALID_ENTRY, eq + 1);
26337c478bd9Sstevel@tonic-gate 			free(opt_dup);
26347c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
26357c478bd9Sstevel@tonic-gate 		}
26367c478bd9Sstevel@tonic-gate 		*entry = entryNum;
26377c478bd9Sstevel@tonic-gate 	} else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) {
26387c478bd9Sstevel@tonic-gate 		*title = opt + (eq - opt_dup) + 1;
26397c478bd9Sstevel@tonic-gate 	} else {
26407c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
26417c478bd9Sstevel@tonic-gate 		free(opt_dup);
26427c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
26437c478bd9Sstevel@tonic-gate 	}
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate 	free(opt_dup);
26467c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
26477c478bd9Sstevel@tonic-gate }
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate /*
26507c478bd9Sstevel@tonic-gate  * If invoked with no titles/entries (opt == NULL)
26517c478bd9Sstevel@tonic-gate  * only title lines in file are printed.
26527c478bd9Sstevel@tonic-gate  *
26537c478bd9Sstevel@tonic-gate  * If invoked with a title or entry #, all
26547c478bd9Sstevel@tonic-gate  * lines in *every* matching entry are listed
26557c478bd9Sstevel@tonic-gate  */
26567c478bd9Sstevel@tonic-gate static error_t
26577c478bd9Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt)
26587c478bd9Sstevel@tonic-gate {
26597c478bd9Sstevel@tonic-gate 	line_t *lp;
26607c478bd9Sstevel@tonic-gate 	int entry = ENTRY_INIT;
26617c478bd9Sstevel@tonic-gate 	int found;
26627c478bd9Sstevel@tonic-gate 	char *title = NULL;
26637c478bd9Sstevel@tonic-gate 
26647c478bd9Sstevel@tonic-gate 	assert(mp);
26657c478bd9Sstevel@tonic-gate 	assert(menu_path);
26667c478bd9Sstevel@tonic-gate 
26677c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
26687c478bd9Sstevel@tonic-gate 		bam_error(NO_MENU, menu_path);
26697c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
26707c478bd9Sstevel@tonic-gate 	}
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate 	if (opt != NULL) {
26737c478bd9Sstevel@tonic-gate 		if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
26747c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
26757c478bd9Sstevel@tonic-gate 		}
26767c478bd9Sstevel@tonic-gate 		assert((entry != ENTRY_INIT) ^ (title != NULL));
26777c478bd9Sstevel@tonic-gate 	} else {
26787c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0);
26797c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0);
26807c478bd9Sstevel@tonic-gate 	}
26817c478bd9Sstevel@tonic-gate 
26827c478bd9Sstevel@tonic-gate 	found = 0;
26837c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
26847c478bd9Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY)
26857c478bd9Sstevel@tonic-gate 			continue;
26867c478bd9Sstevel@tonic-gate 		if (opt == NULL && lp->flags == BAM_TITLE) {
26877c478bd9Sstevel@tonic-gate 			bam_print(PRINT_TITLE, lp->entryNum,
26887c478bd9Sstevel@tonic-gate 			    lp->arg);
26897c478bd9Sstevel@tonic-gate 			found = 1;
26907c478bd9Sstevel@tonic-gate 			continue;
26917c478bd9Sstevel@tonic-gate 		}
26927c478bd9Sstevel@tonic-gate 		if (entry != ENTRY_INIT && lp->entryNum == entry) {
26937c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
26947c478bd9Sstevel@tonic-gate 			found = 1;
26957c478bd9Sstevel@tonic-gate 			continue;
26967c478bd9Sstevel@tonic-gate 		}
26977c478bd9Sstevel@tonic-gate 
26987c478bd9Sstevel@tonic-gate 		/*
26997c478bd9Sstevel@tonic-gate 		 * We set the entry value here so that all lines
27007c478bd9Sstevel@tonic-gate 		 * in entry get printed. If we subsequently match
27017c478bd9Sstevel@tonic-gate 		 * title in other entries, all lines in those
27027c478bd9Sstevel@tonic-gate 		 * entries get printed as well.
27037c478bd9Sstevel@tonic-gate 		 */
27047c478bd9Sstevel@tonic-gate 		if (title && lp->flags == BAM_TITLE && lp->arg &&
27057c478bd9Sstevel@tonic-gate 		    strncmp(title, lp->arg, strlen(title)) == 0) {
27067c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
27077c478bd9Sstevel@tonic-gate 			entry = lp->entryNum;
27087c478bd9Sstevel@tonic-gate 			found = 1;
27097c478bd9Sstevel@tonic-gate 			continue;
27107c478bd9Sstevel@tonic-gate 		}
27117c478bd9Sstevel@tonic-gate 	}
27127c478bd9Sstevel@tonic-gate 
27137c478bd9Sstevel@tonic-gate 	if (!found) {
27147c478bd9Sstevel@tonic-gate 		bam_error(NO_MATCH_ENTRY);
27157c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27167c478bd9Sstevel@tonic-gate 	}
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
27197c478bd9Sstevel@tonic-gate }
27207c478bd9Sstevel@tonic-gate 
27217c478bd9Sstevel@tonic-gate static int
27227c478bd9Sstevel@tonic-gate add_boot_entry(menu_t *mp,
27237c478bd9Sstevel@tonic-gate 	char *title,
27247c478bd9Sstevel@tonic-gate 	char *root,
27257c478bd9Sstevel@tonic-gate 	char *kernel,
27267c478bd9Sstevel@tonic-gate 	char *module)
27277c478bd9Sstevel@tonic-gate {
27287c478bd9Sstevel@tonic-gate 	int lineNum, entryNum;
27297c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
27307c478bd9Sstevel@tonic-gate 
27317c478bd9Sstevel@tonic-gate 	assert(mp);
27327c478bd9Sstevel@tonic-gate 
27337c478bd9Sstevel@tonic-gate 	if (title == NULL) {
2734ee3b8144Sszhou 		title = "Solaris";	/* default to Solaris */
27357c478bd9Sstevel@tonic-gate 	}
27367c478bd9Sstevel@tonic-gate 	if (kernel == NULL) {
27377c478bd9Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]);
27387c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27397c478bd9Sstevel@tonic-gate 	}
27407c478bd9Sstevel@tonic-gate 	if (module == NULL) {
27417c478bd9Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]);
27427c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27437c478bd9Sstevel@tonic-gate 	}
27447c478bd9Sstevel@tonic-gate 
27457c478bd9Sstevel@tonic-gate 	if (mp->start) {
27467c478bd9Sstevel@tonic-gate 		lineNum = mp->end->lineNum;
27477c478bd9Sstevel@tonic-gate 		entryNum = mp->end->entryNum;
27487c478bd9Sstevel@tonic-gate 	} else {
27497c478bd9Sstevel@tonic-gate 		lineNum = LINE_INIT;
27507c478bd9Sstevel@tonic-gate 		entryNum = ENTRY_INIT;
27517c478bd9Sstevel@tonic-gate 	}
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate 	/*
27547c478bd9Sstevel@tonic-gate 	 * No separator for comment (HDR/FTR) commands
27557c478bd9Sstevel@tonic-gate 	 * The syntax for comments is #<comment>
27567c478bd9Sstevel@tonic-gate 	 */
27577c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
27587c478bd9Sstevel@tonic-gate 	    menu_cmds[COMMENT_CMD], BAM_HDR);
27598c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
27607c478bd9Sstevel@tonic-gate 
27617c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
27627c478bd9Sstevel@tonic-gate 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
27638c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
27647c478bd9Sstevel@tonic-gate 
27658c1b6884Sszhou 	if (root) {
27667c478bd9Sstevel@tonic-gate 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
27677c478bd9Sstevel@tonic-gate 		    menu_cmds[ROOT_CMD], menu_cmds[SEP_CMD], root);
27688c1b6884Sszhou 		line_parser(mp, linebuf, &lineNum, &entryNum);
27697c478bd9Sstevel@tonic-gate 	}
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
27727c478bd9Sstevel@tonic-gate 	    menu_cmds[KERNEL_CMD], menu_cmds[SEP_CMD], kernel);
27738c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
27767c478bd9Sstevel@tonic-gate 	    menu_cmds[MODULE_CMD], menu_cmds[SEP_CMD], module);
27778c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
27807c478bd9Sstevel@tonic-gate 	    menu_cmds[COMMENT_CMD], BAM_FTR);
27818c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate 	return (entryNum);
27847c478bd9Sstevel@tonic-gate }
27857c478bd9Sstevel@tonic-gate 
27867c478bd9Sstevel@tonic-gate static error_t
27877c478bd9Sstevel@tonic-gate do_delete(menu_t *mp, int entryNum)
27887c478bd9Sstevel@tonic-gate {
27898c1b6884Sszhou 	line_t *lp, *freed;
27908c1b6884Sszhou 	entry_t *ent, *tmp;
27917c478bd9Sstevel@tonic-gate 	int deleted;
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 	assert(entryNum != ENTRY_INIT);
27947c478bd9Sstevel@tonic-gate 
27958c1b6884Sszhou 	ent = mp->entries;
27968c1b6884Sszhou 	while (ent) {
27978c1b6884Sszhou 		lp = ent->start;
27988c1b6884Sszhou 		/* check entry number and make sure it's a bootadm entry */
27998c1b6884Sszhou 		if (lp->flags != BAM_COMMENT ||
28008c1b6884Sszhou 		    strcmp(lp->arg, BAM_HDR) != 0 ||
28018c1b6884Sszhou 		    (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) {
28028c1b6884Sszhou 			ent = ent->next;
28037c478bd9Sstevel@tonic-gate 			continue;
28047c478bd9Sstevel@tonic-gate 		}
28057c478bd9Sstevel@tonic-gate 
28068c1b6884Sszhou 		/* free the entry content */
28078c1b6884Sszhou 		do {
28088c1b6884Sszhou 			freed = lp;
28098c1b6884Sszhou 			lp = lp->next;	/* prev stays the same */
28108c1b6884Sszhou 			unlink_line(mp, freed);
28118c1b6884Sszhou 			line_free(freed);
28128c1b6884Sszhou 		} while (freed != ent->end);
28137c478bd9Sstevel@tonic-gate 
28148c1b6884Sszhou 		/* free the entry_t structure */
28158c1b6884Sszhou 		tmp = ent;
28168c1b6884Sszhou 		ent = ent->next;
28178c1b6884Sszhou 		if (tmp->prev)
28188c1b6884Sszhou 			tmp->prev->next = ent;
28197c478bd9Sstevel@tonic-gate 		else
28208c1b6884Sszhou 			mp->entries = ent;
28218c1b6884Sszhou 		if (ent)
28228c1b6884Sszhou 			ent->prev = tmp->prev;
28237c478bd9Sstevel@tonic-gate 		deleted = 1;
28247c478bd9Sstevel@tonic-gate 	}
28257c478bd9Sstevel@tonic-gate 
28267c478bd9Sstevel@tonic-gate 	if (!deleted && entryNum != ALL_ENTRIES) {
28277c478bd9Sstevel@tonic-gate 		bam_error(NO_BOOTADM_MATCH);
28287c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
28297c478bd9Sstevel@tonic-gate 	}
28307c478bd9Sstevel@tonic-gate 
283140541d5dSvikram 	/*
283240541d5dSvikram 	 * Now that we have deleted an entry, update
283340541d5dSvikram 	 * the entry numbering and the default cmd.
283440541d5dSvikram 	 */
283540541d5dSvikram 	update_numbering(mp);
283640541d5dSvikram 
28377c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
28387c478bd9Sstevel@tonic-gate }
28397c478bd9Sstevel@tonic-gate 
28407c478bd9Sstevel@tonic-gate static error_t
28417c478bd9Sstevel@tonic-gate delete_all_entries(menu_t *mp, char *menu_path, char *opt)
28427c478bd9Sstevel@tonic-gate {
28437c478bd9Sstevel@tonic-gate 	assert(mp);
28447c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
28457c478bd9Sstevel@tonic-gate 
28467c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
28477c478bd9Sstevel@tonic-gate 		bam_print(EMPTY_FILE, menu_path);
28487c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
28497c478bd9Sstevel@tonic-gate 	}
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 	if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) {
28527c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
28537c478bd9Sstevel@tonic-gate 	}
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
28567c478bd9Sstevel@tonic-gate }
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate static FILE *
28598c1b6884Sszhou open_diskmap(char *root)
28607c478bd9Sstevel@tonic-gate {
28617c478bd9Sstevel@tonic-gate 	FILE *fp;
28627c478bd9Sstevel@tonic-gate 	char cmd[PATH_MAX];
28637c478bd9Sstevel@tonic-gate 
28647c478bd9Sstevel@tonic-gate 	/* make sure we have a map file */
28657c478bd9Sstevel@tonic-gate 	fp = fopen(GRUBDISK_MAP, "r");
28667c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
28677c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd),
28688c1b6884Sszhou 		    "%s%s > /dev/null", root, CREATE_DISKMAP);
28697c478bd9Sstevel@tonic-gate 		(void) system(cmd);
28707c478bd9Sstevel@tonic-gate 		fp = fopen(GRUBDISK_MAP, "r");
28717c478bd9Sstevel@tonic-gate 	}
28727c478bd9Sstevel@tonic-gate 	return (fp);
28737c478bd9Sstevel@tonic-gate }
28747c478bd9Sstevel@tonic-gate 
28757c478bd9Sstevel@tonic-gate #define	SECTOR_SIZE	512
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate static int
28787c478bd9Sstevel@tonic-gate get_partition(char *device)
28797c478bd9Sstevel@tonic-gate {
28807c478bd9Sstevel@tonic-gate 	int i, fd, is_pcfs, partno = -1;
28817c478bd9Sstevel@tonic-gate 	struct mboot *mboot;
28827c478bd9Sstevel@tonic-gate 	char boot_sect[SECTOR_SIZE];
28837c478bd9Sstevel@tonic-gate 	char *wholedisk, *slice;
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate 	/* form whole disk (p0) */
28867c478bd9Sstevel@tonic-gate 	slice = device + strlen(device) - 2;
28877c478bd9Sstevel@tonic-gate 	is_pcfs = (*slice != 's');
28887c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
28897c478bd9Sstevel@tonic-gate 		*slice = '\0';
28907c478bd9Sstevel@tonic-gate 	wholedisk = s_calloc(1, strlen(device) + 3);
28917c478bd9Sstevel@tonic-gate 	(void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device);
28927c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
28937c478bd9Sstevel@tonic-gate 		*slice = 's';
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate 	/* read boot sector */
28967c478bd9Sstevel@tonic-gate 	fd = open(wholedisk, O_RDONLY);
28977c478bd9Sstevel@tonic-gate 	free(wholedisk);
28987c478bd9Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
28997c478bd9Sstevel@tonic-gate 		return (partno);
29007c478bd9Sstevel@tonic-gate 	}
29017c478bd9Sstevel@tonic-gate 	(void) close(fd);
29027c478bd9Sstevel@tonic-gate 
29037c478bd9Sstevel@tonic-gate 	/* parse fdisk table */
29047c478bd9Sstevel@tonic-gate 	mboot = (struct mboot *)((void *)boot_sect);
29057c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
29067c478bd9Sstevel@tonic-gate 		struct ipart *part =
29077c478bd9Sstevel@tonic-gate 		    (struct ipart *)(uintptr_t)mboot->parts + i;
29087c478bd9Sstevel@tonic-gate 		if (is_pcfs) {	/* looking for solaris boot part */
29097c478bd9Sstevel@tonic-gate 			if (part->systid == 0xbe) {
29107c478bd9Sstevel@tonic-gate 				partno = i;
29117c478bd9Sstevel@tonic-gate 				break;
29127c478bd9Sstevel@tonic-gate 			}
29137c478bd9Sstevel@tonic-gate 		} else {	/* look for solaris partition, old and new */
29147c478bd9Sstevel@tonic-gate 			if (part->systid == SUNIXOS ||
29157c478bd9Sstevel@tonic-gate 			    part->systid == SUNIXOS2) {
29167c478bd9Sstevel@tonic-gate 				partno = i;
29177c478bd9Sstevel@tonic-gate 				break;
29187c478bd9Sstevel@tonic-gate 			}
29197c478bd9Sstevel@tonic-gate 		}
29207c478bd9Sstevel@tonic-gate 	}
29217c478bd9Sstevel@tonic-gate 	return (partno);
29227c478bd9Sstevel@tonic-gate }
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate static char *
29257c478bd9Sstevel@tonic-gate get_grubdisk(char *rootdev, FILE *fp, int on_bootdev)
29267c478bd9Sstevel@tonic-gate {
29277c478bd9Sstevel@tonic-gate 	char *grubdisk;	/* (hd#,#,#) */
29287c478bd9Sstevel@tonic-gate 	char *slice;
29297c478bd9Sstevel@tonic-gate 	char *grubhd;
29307c478bd9Sstevel@tonic-gate 	int fdiskpart;
29317c478bd9Sstevel@tonic-gate 	int found = 0;
29327c478bd9Sstevel@tonic-gate 	char *devname, *ctdname = strstr(rootdev, "dsk/");
29337c478bd9Sstevel@tonic-gate 	char linebuf[PATH_MAX];
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 	if (ctdname == NULL)
29367c478bd9Sstevel@tonic-gate 		return (NULL);
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate 	ctdname += strlen("dsk/");
29397c478bd9Sstevel@tonic-gate 	slice = strrchr(ctdname, 's');
29407c478bd9Sstevel@tonic-gate 	if (slice)
29417c478bd9Sstevel@tonic-gate 		*slice = '\0';
29427c478bd9Sstevel@tonic-gate 
29437c478bd9Sstevel@tonic-gate 	rewind(fp);
29447c478bd9Sstevel@tonic-gate 	while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) {
29457c478bd9Sstevel@tonic-gate 		grubhd = strtok(linebuf, " \t\n");
29467c478bd9Sstevel@tonic-gate 		if (grubhd)
29477c478bd9Sstevel@tonic-gate 			devname = strtok(NULL, " \t\n");
29487c478bd9Sstevel@tonic-gate 		else
29497c478bd9Sstevel@tonic-gate 			devname = NULL;
29507c478bd9Sstevel@tonic-gate 		if (devname && strcmp(devname, ctdname) == 0) {
29517c478bd9Sstevel@tonic-gate 			found = 1;
29527c478bd9Sstevel@tonic-gate 			break;
29537c478bd9Sstevel@tonic-gate 		}
29547c478bd9Sstevel@tonic-gate 	}
29557c478bd9Sstevel@tonic-gate 
29567c478bd9Sstevel@tonic-gate 	if (slice)
29577c478bd9Sstevel@tonic-gate 		*slice = 's';
29587c478bd9Sstevel@tonic-gate 
29597c478bd9Sstevel@tonic-gate 	if (found == 0) {
29607c478bd9Sstevel@tonic-gate 		if (bam_verbose)
29617c478bd9Sstevel@tonic-gate 			bam_print(DISKMAP_FAIL_NONFATAL, rootdev);
29627c478bd9Sstevel@tonic-gate 		grubhd = "0";	/* assume disk 0 if can't match */
29637c478bd9Sstevel@tonic-gate 	}
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate 	fdiskpart = get_partition(rootdev);
29667c478bd9Sstevel@tonic-gate 	if (fdiskpart == -1)
29677c478bd9Sstevel@tonic-gate 		return (NULL);
29687c478bd9Sstevel@tonic-gate 
29697c478bd9Sstevel@tonic-gate 	grubdisk = s_calloc(1, 10);
29707c478bd9Sstevel@tonic-gate 	if (slice) {
29717c478bd9Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d,%c)",
29727c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart, slice[1] + 'a' - '0');
29737c478bd9Sstevel@tonic-gate 	} else
29747c478bd9Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d)",
29757c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart);
29767c478bd9Sstevel@tonic-gate 
29777c478bd9Sstevel@tonic-gate 	/* if root not on bootdev, change GRUB disk to 0 */
29787c478bd9Sstevel@tonic-gate 	if (!on_bootdev)
29797c478bd9Sstevel@tonic-gate 		grubdisk[3] = '0';
29807c478bd9Sstevel@tonic-gate 	return (grubdisk);
29817c478bd9Sstevel@tonic-gate }
29827c478bd9Sstevel@tonic-gate 
2983ee3b8144Sszhou static char *
2984ee3b8144Sszhou get_title(char *rootdir)
29857c478bd9Sstevel@tonic-gate {
29867c478bd9Sstevel@tonic-gate 	static char title[80];	/* from /etc/release */
29878c1b6884Sszhou 	char *cp = NULL, release[PATH_MAX];
29887c478bd9Sstevel@tonic-gate 	FILE *fp;
29897c478bd9Sstevel@tonic-gate 
29907c478bd9Sstevel@tonic-gate 	/* open the /etc/release file */
29917c478bd9Sstevel@tonic-gate 	(void) snprintf(release, sizeof (release), "%s/etc/release", rootdir);
29927c478bd9Sstevel@tonic-gate 
29937c478bd9Sstevel@tonic-gate 	fp = fopen(release, "r");
29947c478bd9Sstevel@tonic-gate 	if (fp == NULL)
2995ee3b8144Sszhou 		return (NULL);
29967c478bd9Sstevel@tonic-gate 
29977c478bd9Sstevel@tonic-gate 	while (s_fgets(title, sizeof (title), fp) != NULL) {
29987c478bd9Sstevel@tonic-gate 		cp = strstr(title, "Solaris");
29997c478bd9Sstevel@tonic-gate 		if (cp)
30007c478bd9Sstevel@tonic-gate 			break;
30017c478bd9Sstevel@tonic-gate 	}
30027c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
30038c1b6884Sszhou 	return (cp == NULL ? "Solaris" : cp);
30047c478bd9Sstevel@tonic-gate }
30057c478bd9Sstevel@tonic-gate 
30067c478bd9Sstevel@tonic-gate static char *
30077c478bd9Sstevel@tonic-gate get_special(char *mountp)
30087c478bd9Sstevel@tonic-gate {
30097c478bd9Sstevel@tonic-gate 	FILE *mntfp;
30107c478bd9Sstevel@tonic-gate 	struct mnttab mp = {0}, mpref = {0};
30117c478bd9Sstevel@tonic-gate 
30127c478bd9Sstevel@tonic-gate 	mntfp = fopen(MNTTAB, "r");
30137c478bd9Sstevel@tonic-gate 	if (mntfp == NULL) {
30147c478bd9Sstevel@tonic-gate 		return (0);
30157c478bd9Sstevel@tonic-gate 	}
30167c478bd9Sstevel@tonic-gate 
30177c478bd9Sstevel@tonic-gate 	if (*mountp == '\0')
30187c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = "/";
30197c478bd9Sstevel@tonic-gate 	else
30207c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = mountp;
30217c478bd9Sstevel@tonic-gate 	if (getmntany(mntfp, &mp, &mpref) != 0) {
30227c478bd9Sstevel@tonic-gate 		(void) fclose(mntfp);
30237c478bd9Sstevel@tonic-gate 		return (NULL);
30247c478bd9Sstevel@tonic-gate 	}
30257c478bd9Sstevel@tonic-gate 	(void) fclose(mntfp);
30267c478bd9Sstevel@tonic-gate 
30277c478bd9Sstevel@tonic-gate 	return (s_strdup(mp.mnt_special));
30287c478bd9Sstevel@tonic-gate }
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate static char *
30317c478bd9Sstevel@tonic-gate os_to_grubdisk(char *osdisk, int on_bootdev)
30327c478bd9Sstevel@tonic-gate {
30337c478bd9Sstevel@tonic-gate 	FILE *fp;
30347c478bd9Sstevel@tonic-gate 	char *grubdisk;
30357c478bd9Sstevel@tonic-gate 
30367c478bd9Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
30378c1b6884Sszhou 	fp = open_diskmap("");
30387c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
30397c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdisk);
30407c478bd9Sstevel@tonic-gate 		return (NULL);
30417c478bd9Sstevel@tonic-gate 	}
30427c478bd9Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdisk, fp, on_bootdev);
30437c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
30447c478bd9Sstevel@tonic-gate 	return (grubdisk);
30457c478bd9Sstevel@tonic-gate }
30467c478bd9Sstevel@tonic-gate 
30477c478bd9Sstevel@tonic-gate /*
30487c478bd9Sstevel@tonic-gate  * Check if root is on the boot device
30497c478bd9Sstevel@tonic-gate  * Return 0 (false) on error
30507c478bd9Sstevel@tonic-gate  */
30517c478bd9Sstevel@tonic-gate static int
30527c478bd9Sstevel@tonic-gate menu_on_bootdev(char *menu_root, FILE *fp)
30537c478bd9Sstevel@tonic-gate {
30547c478bd9Sstevel@tonic-gate 	int ret;
30557c478bd9Sstevel@tonic-gate 	char *grubhd, *bootp, *special;
30567c478bd9Sstevel@tonic-gate 
30577c478bd9Sstevel@tonic-gate 	special = get_special(menu_root);
30587c478bd9Sstevel@tonic-gate 	if (special == NULL)
30597c478bd9Sstevel@tonic-gate 		return (0);
30607c478bd9Sstevel@tonic-gate 	bootp = strstr(special, "p0:boot");
30617c478bd9Sstevel@tonic-gate 	if (bootp)
30627c478bd9Sstevel@tonic-gate 		*bootp = '\0';
30637c478bd9Sstevel@tonic-gate 	grubhd = get_grubdisk(special, fp, 1);
30647c478bd9Sstevel@tonic-gate 	free(special);
30657c478bd9Sstevel@tonic-gate 
30667c478bd9Sstevel@tonic-gate 	if (grubhd == NULL)
30677c478bd9Sstevel@tonic-gate 		return (0);
30687c478bd9Sstevel@tonic-gate 	ret = grubhd[3] == '0';
30697c478bd9Sstevel@tonic-gate 	free(grubhd);
30707c478bd9Sstevel@tonic-gate 	return (ret);
30717c478bd9Sstevel@tonic-gate }
30727c478bd9Sstevel@tonic-gate 
30738c1b6884Sszhou /*
30748c1b6884Sszhou  * look for matching bootadm entry with specified parameters
30758c1b6884Sszhou  * Here are the rules (based on existing usage):
30768c1b6884Sszhou  * - If title is specified, match on title only
30778c1b6884Sszhou  * - Else, match on grubdisk and module (don't care about kernel line).
30788c1b6884Sszhou  *   note that, if root_opt is non-zero, the absence of root line is
30798c1b6884Sszhou  *   considered a match.
30808c1b6884Sszhou  */
30818c1b6884Sszhou static entry_t *
30828c1b6884Sszhou find_boot_entry(menu_t *mp, char *title, char *root, char *module,
30838c1b6884Sszhou     int root_opt, int *entry_num)
30848c1b6884Sszhou {
30858c1b6884Sszhou 	int i;
30868c1b6884Sszhou 	line_t *lp;
30878c1b6884Sszhou 	entry_t *ent;
30888c1b6884Sszhou 
30898c1b6884Sszhou 	/* find matching entry */
30908c1b6884Sszhou 	for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) {
30918c1b6884Sszhou 		lp = ent->start;
30928c1b6884Sszhou 
30938c1b6884Sszhou 		/* first line of entry must be bootadm comment */
30948c1b6884Sszhou 		lp = ent->start;
30958c1b6884Sszhou 		if (lp->flags != BAM_COMMENT || strcmp(lp->arg, BAM_HDR) != 0) {
30968c1b6884Sszhou 			continue;
30978c1b6884Sszhou 		}
30988c1b6884Sszhou 
30998c1b6884Sszhou 		/* advance to title line */
31008c1b6884Sszhou 		lp = lp->next;
31018c1b6884Sszhou 		if (title) {
31028c1b6884Sszhou 			if (lp->flags == BAM_TITLE && lp->arg &&
31038c1b6884Sszhou 			    strcmp(lp->arg, title) == 0)
31048c1b6884Sszhou 				break;
31058c1b6884Sszhou 			continue;	/* check title only */
31068c1b6884Sszhou 		}
31078c1b6884Sszhou 
31088c1b6884Sszhou 		lp = lp->next;	/* advance to root line */
31098c1b6884Sszhou 		if (lp == NULL || strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
31108c1b6884Sszhou 			/* root command found, match grub disk */
31118c1b6884Sszhou 			if (strcmp(lp->arg, root) != 0) {
31128c1b6884Sszhou 				continue;
31138c1b6884Sszhou 			}
31148c1b6884Sszhou 			lp = lp->next;	/* advance to kernel line */
31158c1b6884Sszhou 		} else {
31168c1b6884Sszhou 			/* no root command, see if root is optional */
31178c1b6884Sszhou 			if (root_opt == 0) {
31188c1b6884Sszhou 				continue;
31198c1b6884Sszhou 			}
31208c1b6884Sszhou 		}
31218c1b6884Sszhou 
31228c1b6884Sszhou 		if (lp == NULL || lp->next == NULL) {
31238c1b6884Sszhou 			continue;
31248c1b6884Sszhou 		}
31258c1b6884Sszhou 
31268c1b6884Sszhou 		/* check for matching module entry (failsafe or normal) */
31278c1b6884Sszhou 		lp = lp->next;	/* advance to module line */
31288c1b6884Sszhou 		if (strcmp(lp->cmd, menu_cmds[MODULE_CMD]) != 0 ||
31298c1b6884Sszhou 		    strcmp(lp->arg, module) != 0) {
31308c1b6884Sszhou 			continue;
31318c1b6884Sszhou 		}
31328c1b6884Sszhou 		break;	/* match found */
31338c1b6884Sszhou 	}
31348c1b6884Sszhou 
31358c1b6884Sszhou 	*entry_num = i;
31368c1b6884Sszhou 	return (ent);
31378c1b6884Sszhou }
31388c1b6884Sszhou 
31398c1b6884Sszhou static int
31408c1b6884Sszhou update_boot_entry(menu_t *mp, char *title, char *root, char *kernel,
31418c1b6884Sszhou     char *module, int root_opt)
31428c1b6884Sszhou {
31438c1b6884Sszhou 	int i;
31448c1b6884Sszhou 	entry_t *ent;
31458c1b6884Sszhou 	line_t *lp;
31468c1b6884Sszhou 	char linebuf[BAM_MAXLINE];
31478c1b6884Sszhou 
31488c1b6884Sszhou 	/* note: don't match on title, it's updated on upgrade */
31498c1b6884Sszhou 	ent = find_boot_entry(mp, NULL, root, module, root_opt, &i);
31508c1b6884Sszhou 	if (ent == NULL)
31518c1b6884Sszhou 		return (add_boot_entry(mp, title, root_opt ? NULL : root,
31528c1b6884Sszhou 		    kernel, module));
31538c1b6884Sszhou 
31548c1b6884Sszhou 	/* replace title of exiting entry and delete root line */
31558c1b6884Sszhou 	lp = ent->start;
31568c1b6884Sszhou 	lp = lp->next;	/* title line */
31578c1b6884Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
31588c1b6884Sszhou 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
31598c1b6884Sszhou 	free(lp->arg);
31608c1b6884Sszhou 	free(lp->line);
31618c1b6884Sszhou 	lp->arg = s_strdup(title);
31628c1b6884Sszhou 	lp->line = s_strdup(linebuf);
31638c1b6884Sszhou 
31648c1b6884Sszhou 	lp = lp->next;	/* root line */
31658c1b6884Sszhou 	if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
31668c1b6884Sszhou 		if (root_opt) {		/* root line not needed */
31678c1b6884Sszhou 			line_t *tmp = lp;
31688c1b6884Sszhou 			lp = lp->next;
31698c1b6884Sszhou 			unlink_line(mp, tmp);
31708c1b6884Sszhou 			line_free(tmp);
31718c1b6884Sszhou 		} else
31728c1b6884Sszhou 			lp = lp->next;
31738c1b6884Sszhou 	}
31748c1b6884Sszhou 	return (i);
31758c1b6884Sszhou }
31768c1b6884Sszhou 
31777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
31787c478bd9Sstevel@tonic-gate static error_t
31797c478bd9Sstevel@tonic-gate update_entry(menu_t *mp, char *menu_root, char *opt)
31807c478bd9Sstevel@tonic-gate {
31817c478bd9Sstevel@tonic-gate 	FILE *fp;
31827c478bd9Sstevel@tonic-gate 	int entry;
31837c478bd9Sstevel@tonic-gate 	char *grubdisk, *title, *osdev, *osroot;
31848c1b6884Sszhou 	struct stat sbuf;
31858c1b6884Sszhou 	char failsafe[256];
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate 	assert(mp);
31887c478bd9Sstevel@tonic-gate 	assert(opt);
31897c478bd9Sstevel@tonic-gate 
31907c478bd9Sstevel@tonic-gate 	osdev = strtok(opt, ",");
31917c478bd9Sstevel@tonic-gate 	osroot = strtok(NULL, ",");
31927c478bd9Sstevel@tonic-gate 	if (osroot == NULL)
31937c478bd9Sstevel@tonic-gate 		osroot = menu_root;
31947c478bd9Sstevel@tonic-gate 	title = get_title(osroot);
31957c478bd9Sstevel@tonic-gate 
31967c478bd9Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
31978c1b6884Sszhou 	fp = open_diskmap(osroot);
31987c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
31997c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
32007c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
32017c478bd9Sstevel@tonic-gate 	}
32027c478bd9Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdev, fp, menu_on_bootdev(menu_root, fp));
32037c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
32047c478bd9Sstevel@tonic-gate 	if (grubdisk == NULL) {
32057c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
32067c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
32077c478bd9Sstevel@tonic-gate 	}
32087c478bd9Sstevel@tonic-gate 
32097c478bd9Sstevel@tonic-gate 	/* add the entry for normal Solaris */
32108c1b6884Sszhou 	entry = update_boot_entry(mp, title, grubdisk,
32117c478bd9Sstevel@tonic-gate 	    "/platform/i86pc/multiboot",
32128c1b6884Sszhou 	    "/platform/i86pc/boot_archive",
32138c1b6884Sszhou 	    osroot == menu_root);
32147c478bd9Sstevel@tonic-gate 
32157c478bd9Sstevel@tonic-gate 	/* add the entry for failsafe archive */
32168c1b6884Sszhou 	(void) snprintf(failsafe, sizeof (failsafe),
32178c1b6884Sszhou 	    "%s/boot/x86.miniroot-safe", osroot);
32188c1b6884Sszhou 	if (stat(failsafe, &sbuf) == 0)
32198c1b6884Sszhou 		(void) update_boot_entry(mp, "Solaris failsafe", grubdisk,
32207c478bd9Sstevel@tonic-gate 		    "/boot/multiboot kernel/unix -s",
32218c1b6884Sszhou 		    "/boot/x86.miniroot-safe",
32228c1b6884Sszhou 		    osroot == menu_root);
32237c478bd9Sstevel@tonic-gate 	free(grubdisk);
32247c478bd9Sstevel@tonic-gate 
32257c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
32267c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
32277c478bd9Sstevel@tonic-gate 	}
32287c478bd9Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
32297c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
32307c478bd9Sstevel@tonic-gate }
32317c478bd9Sstevel@tonic-gate 
3232b610f78eSvikram static char *
3233b610f78eSvikram read_grub_root(void)
3234b610f78eSvikram {
3235b610f78eSvikram 	FILE *fp;
3236b610f78eSvikram 	struct stat sb;
3237b610f78eSvikram 	char buf[BAM_MAXLINE];
3238b610f78eSvikram 	char *rootstr;
3239b610f78eSvikram 
3240b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
3241b610f78eSvikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
3242b610f78eSvikram 		return (NULL);
3243b610f78eSvikram 	}
3244b610f78eSvikram 
3245b610f78eSvikram 	if (stat(GRUB_root, &sb) != 0) {
3246b610f78eSvikram 		bam_error(MISSING_ROOT_FILE, GRUB_root, strerror(errno));
3247b610f78eSvikram 		return (NULL);
3248b610f78eSvikram 	}
3249b610f78eSvikram 
3250b610f78eSvikram 	fp = fopen(GRUB_root, "r");
3251b610f78eSvikram 	if (fp == NULL) {
3252b610f78eSvikram 		bam_error(OPEN_FAIL, GRUB_root, strerror(errno));
3253b610f78eSvikram 		return (NULL);
3254b610f78eSvikram 	}
3255b610f78eSvikram 
3256b610f78eSvikram 	if (s_fgets(buf, sizeof (buf), fp) == NULL) {
3257b610f78eSvikram 		bam_error(EMPTY_FILE, GRUB_root, strerror(errno));
3258b610f78eSvikram 		(void) fclose(fp);
3259b610f78eSvikram 		return (NULL);
3260b610f78eSvikram 	}
3261b610f78eSvikram 
3262b610f78eSvikram 	/*
3263b610f78eSvikram 	 * Copy buf here as check below may trash the buffer
3264b610f78eSvikram 	 */
3265b610f78eSvikram 	rootstr = s_strdup(buf);
3266b610f78eSvikram 
3267b610f78eSvikram 	if (s_fgets(buf, sizeof (buf), fp) != NULL) {
3268b610f78eSvikram 		bam_error(BAD_ROOT_FILE, GRUB_root);
3269b610f78eSvikram 		free(rootstr);
3270b610f78eSvikram 		rootstr = NULL;
3271b610f78eSvikram 	}
3272b610f78eSvikram 
3273b610f78eSvikram 	(void) fclose(fp);
3274b610f78eSvikram 
3275b610f78eSvikram 	return (rootstr);
3276b610f78eSvikram }
3277b610f78eSvikram 
32788c1b6884Sszhou static void
32798c1b6884Sszhou save_default_entry(menu_t *mp)
32808c1b6884Sszhou {
32818c1b6884Sszhou 	int lineNum, entryNum;
32828c1b6884Sszhou 	int entry = 0;	/* default is 0 */
32838c1b6884Sszhou 	char linebuf[BAM_MAXLINE];
32848c1b6884Sszhou 	line_t *lp = mp->curdefault;
32858c1b6884Sszhou 
32868c1b6884Sszhou 	if (lp)
32878c1b6884Sszhou 		entry = s_strtol(lp->arg);
32888c1b6884Sszhou 
32898c1b6884Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "#%s%d", BAM_OLDDEF, entry);
32908c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
32918c1b6884Sszhou }
32928c1b6884Sszhou 
32938c1b6884Sszhou static void
32948c1b6884Sszhou restore_default_entry(menu_t *mp)
32958c1b6884Sszhou {
32968c1b6884Sszhou 	int entry;
32978c1b6884Sszhou 	char *str;
32988c1b6884Sszhou 	line_t *lp = mp->olddefault;
32998c1b6884Sszhou 
33008c1b6884Sszhou 	if (lp == NULL)
33018c1b6884Sszhou 		return;		/* nothing to restore */
33028c1b6884Sszhou 
33038c1b6884Sszhou 	str = lp->arg + strlen(BAM_OLDDEF);
33048c1b6884Sszhou 	entry = s_strtol(str);
33058c1b6884Sszhou 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
33068c1b6884Sszhou 
33078c1b6884Sszhou 	/* delete saved old default line */
33088c1b6884Sszhou 	mp->olddefault = NULL;
33098c1b6884Sszhou 	unlink_line(mp, lp);
33108c1b6884Sszhou 	line_free(lp);
33118c1b6884Sszhou }
33128c1b6884Sszhou 
33137c478bd9Sstevel@tonic-gate /*
33147c478bd9Sstevel@tonic-gate  * This function is for supporting reboot with args.
33157c478bd9Sstevel@tonic-gate  * The opt value can be:
33167c478bd9Sstevel@tonic-gate  * NULL		delete temp entry, if present
33177c478bd9Sstevel@tonic-gate  * entry=#	switches default entry to 1
33187c478bd9Sstevel@tonic-gate  * else		treated as boot-args and setup a temperary menu entry
33197c478bd9Sstevel@tonic-gate  *		and make it the default
33207c478bd9Sstevel@tonic-gate  */
33217c478bd9Sstevel@tonic-gate #define	REBOOT_TITLE	"Solaris_reboot_transient"
33227c478bd9Sstevel@tonic-gate 
33238c1b6884Sszhou /*ARGSUSED*/
33247c478bd9Sstevel@tonic-gate static error_t
33257c478bd9Sstevel@tonic-gate update_temp(menu_t *mp, char *menupath, char *opt)
33267c478bd9Sstevel@tonic-gate {
33277c478bd9Sstevel@tonic-gate 	int entry;
33287c478bd9Sstevel@tonic-gate 	char *grubdisk, *rootdev;
33297c478bd9Sstevel@tonic-gate 	char kernbuf[1024];
3330b610f78eSvikram 	struct stat sb;
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate 	assert(mp);
33337c478bd9Sstevel@tonic-gate 
33348c1b6884Sszhou 	/* If no option, delete exiting reboot menu entry */
33358c1b6884Sszhou 	if (opt == NULL) {
33368c1b6884Sszhou 		entry_t *ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL,
33378c1b6884Sszhou 		    0, &entry);
33388c1b6884Sszhou 		if (ent == NULL)	/* not found is ok */
33398c1b6884Sszhou 			return (BAM_SUCCESS);
33408c1b6884Sszhou 		(void) do_delete(mp, entry);
33418c1b6884Sszhou 		restore_default_entry(mp);
33428c1b6884Sszhou 		return (BAM_WRITE);
33438c1b6884Sszhou 	}
33448c1b6884Sszhou 
33458c1b6884Sszhou 	/* if entry= is specified, set the default entry */
33468c1b6884Sszhou 	if (strncmp(opt, "entry=", strlen("entry=")) == 0 &&
33477c478bd9Sstevel@tonic-gate 	    selector(mp, opt, &entry, NULL) == BAM_SUCCESS) {
33487c478bd9Sstevel@tonic-gate 		/* this is entry=# option */
33497c478bd9Sstevel@tonic-gate 		return (set_global(mp, menu_cmds[DEFAULT_CMD], entry));
33507c478bd9Sstevel@tonic-gate 	}
33517c478bd9Sstevel@tonic-gate 
33527c478bd9Sstevel@tonic-gate 	/*
33537c478bd9Sstevel@tonic-gate 	 * add a new menu entry base on opt and make it the default
3354b610f78eSvikram 	 */
3355b610f78eSvikram 	grubdisk = NULL;
3356b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
3357b610f78eSvikram 		/*
33587c478bd9Sstevel@tonic-gate 		 * 1. First get root disk name from mnttab
33597c478bd9Sstevel@tonic-gate 		 * 2. Translate disk name to grub name
33607c478bd9Sstevel@tonic-gate 		 * 3. Add the new menu entry
33617c478bd9Sstevel@tonic-gate 		 */
33627c478bd9Sstevel@tonic-gate 		rootdev = get_special("/");
33637c478bd9Sstevel@tonic-gate 		if (rootdev) {
33647c478bd9Sstevel@tonic-gate 			grubdisk = os_to_grubdisk(rootdev, 1);
33657c478bd9Sstevel@tonic-gate 			free(rootdev);
33667c478bd9Sstevel@tonic-gate 		}
3367b610f78eSvikram 	} else {
3368b610f78eSvikram 		/*
3369b610f78eSvikram 		 * This is an LU BE. The GRUB_root file
3370b610f78eSvikram 		 * contains entry for GRUB's "root" cmd.
3371b610f78eSvikram 		 */
3372b610f78eSvikram 		grubdisk = read_grub_root();
3373b610f78eSvikram 	}
33747c478bd9Sstevel@tonic-gate 	if (grubdisk == NULL) {
3375b610f78eSvikram 		bam_error(REBOOT_WITH_ARGS_FAILED);
33767c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
33777c478bd9Sstevel@tonic-gate 	}
33787c478bd9Sstevel@tonic-gate 
33797c478bd9Sstevel@tonic-gate 	/* add an entry for Solaris reboot */
33807c478bd9Sstevel@tonic-gate 	(void) snprintf(kernbuf, sizeof (kernbuf),
33817c478bd9Sstevel@tonic-gate 	    "/platform/i86pc/multiboot %s", opt);
33827c478bd9Sstevel@tonic-gate 	entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf,
33837c478bd9Sstevel@tonic-gate 	    "/platform/i86pc/boot_archive");
33847c478bd9Sstevel@tonic-gate 	free(grubdisk);
33857c478bd9Sstevel@tonic-gate 
33867c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
3387b610f78eSvikram 		bam_error(REBOOT_WITH_ARGS_FAILED);
33887c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
33897c478bd9Sstevel@tonic-gate 	}
33908c1b6884Sszhou 
33918c1b6884Sszhou 	save_default_entry(mp);
33927c478bd9Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
33937c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
33947c478bd9Sstevel@tonic-gate }
33957c478bd9Sstevel@tonic-gate 
33967c478bd9Sstevel@tonic-gate static error_t
33977c478bd9Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val)
33987c478bd9Sstevel@tonic-gate {
33997c478bd9Sstevel@tonic-gate 	line_t *lp, *found, *last;
34007c478bd9Sstevel@tonic-gate 	char *cp, *str;
34017c478bd9Sstevel@tonic-gate 	char prefix[BAM_MAXLINE];
34027c478bd9Sstevel@tonic-gate 	size_t len;
34037c478bd9Sstevel@tonic-gate 
34047c478bd9Sstevel@tonic-gate 	assert(mp);
34057c478bd9Sstevel@tonic-gate 	assert(globalcmd);
34067c478bd9Sstevel@tonic-gate 
3407b610f78eSvikram 	if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) {
3408b610f78eSvikram 		if (val < 0 || mp->end == NULL || val > mp->end->entryNum) {
3409b610f78eSvikram 			(void) snprintf(prefix, sizeof (prefix), "%d", val);
3410b610f78eSvikram 			bam_error(INVALID_ENTRY, prefix);
3411b610f78eSvikram 			return (BAM_ERROR);
3412b610f78eSvikram 		}
3413b610f78eSvikram 	}
3414b610f78eSvikram 
34157c478bd9Sstevel@tonic-gate 	found = last = NULL;
34167c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
34177c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
34187c478bd9Sstevel@tonic-gate 			continue;
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate 		last = lp; /* track the last global found */
34217c478bd9Sstevel@tonic-gate 
34227c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
34237c478bd9Sstevel@tonic-gate 			bam_error(NO_CMD, lp->lineNum);
34247c478bd9Sstevel@tonic-gate 			continue;
34257c478bd9Sstevel@tonic-gate 		}
34267c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
34277c478bd9Sstevel@tonic-gate 			continue;
34287c478bd9Sstevel@tonic-gate 
34297c478bd9Sstevel@tonic-gate 		if (found) {
34307c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
34317c478bd9Sstevel@tonic-gate 		}
34327c478bd9Sstevel@tonic-gate 		found = lp;
34337c478bd9Sstevel@tonic-gate 	}
34347c478bd9Sstevel@tonic-gate 
34357c478bd9Sstevel@tonic-gate 	if (found == NULL) {
34367c478bd9Sstevel@tonic-gate 		lp = s_calloc(1, sizeof (line_t));
34377c478bd9Sstevel@tonic-gate 		if (last == NULL) {
34387c478bd9Sstevel@tonic-gate 			lp->next = mp->start;
34397c478bd9Sstevel@tonic-gate 			mp->start = lp;
34407c478bd9Sstevel@tonic-gate 			mp->end = (mp->end) ? mp->end : lp;
34417c478bd9Sstevel@tonic-gate 		} else {
34427c478bd9Sstevel@tonic-gate 			lp->next = last->next;
34437c478bd9Sstevel@tonic-gate 			last->next = lp;
34447c478bd9Sstevel@tonic-gate 			if (lp->next == NULL)
34457c478bd9Sstevel@tonic-gate 				mp->end = lp;
34467c478bd9Sstevel@tonic-gate 		}
34477c478bd9Sstevel@tonic-gate 		lp->flags = BAM_GLOBAL; /* other fields not needed for writes */
34487c478bd9Sstevel@tonic-gate 		len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]);
34497c478bd9Sstevel@tonic-gate 		len += 10;	/* val < 10 digits */
34507c478bd9Sstevel@tonic-gate 		lp->line = s_calloc(1, len);
34517c478bd9Sstevel@tonic-gate 		(void) snprintf(lp->line, len, "%s%s%d",
34527c478bd9Sstevel@tonic-gate 		    globalcmd, menu_cmds[SEP_CMD], val);
34537c478bd9Sstevel@tonic-gate 		return (BAM_WRITE);
34547c478bd9Sstevel@tonic-gate 	}
34557c478bd9Sstevel@tonic-gate 
34567c478bd9Sstevel@tonic-gate 	/*
34577c478bd9Sstevel@tonic-gate 	 * We are changing an existing entry. Retain any prefix whitespace,
34587c478bd9Sstevel@tonic-gate 	 * but overwrite everything else. This preserves tabs added for
34597c478bd9Sstevel@tonic-gate 	 * readability.
34607c478bd9Sstevel@tonic-gate 	 */
34617c478bd9Sstevel@tonic-gate 	str = found->line;
34627c478bd9Sstevel@tonic-gate 	cp = prefix;
34637c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
34647c478bd9Sstevel@tonic-gate 		*(cp++) = *(str++);
34657c478bd9Sstevel@tonic-gate 	*cp = '\0'; /* Terminate prefix */
34667c478bd9Sstevel@tonic-gate 	len = strlen(prefix) + strlen(globalcmd);
34677c478bd9Sstevel@tonic-gate 	len += strlen(menu_cmds[SEP_CMD]) + 10;
34687c478bd9Sstevel@tonic-gate 
34697c478bd9Sstevel@tonic-gate 	free(found->line);
34707c478bd9Sstevel@tonic-gate 	found->line = s_calloc(1, len);
34717c478bd9Sstevel@tonic-gate 	(void) snprintf(found->line, len,
34727c478bd9Sstevel@tonic-gate 		"%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val);
34737c478bd9Sstevel@tonic-gate 
34747c478bd9Sstevel@tonic-gate 	return (BAM_WRITE); /* need a write to menu */
34757c478bd9Sstevel@tonic-gate }
34767c478bd9Sstevel@tonic-gate 
34777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
34787c478bd9Sstevel@tonic-gate static error_t
34797c478bd9Sstevel@tonic-gate set_option(menu_t *mp, char *menu_path, char *opt)
34807c478bd9Sstevel@tonic-gate {
34817c478bd9Sstevel@tonic-gate 	int optnum, optval;
34827c478bd9Sstevel@tonic-gate 	char *val;
34837c478bd9Sstevel@tonic-gate 
34847c478bd9Sstevel@tonic-gate 	assert(mp);
34857c478bd9Sstevel@tonic-gate 	assert(opt);
34867c478bd9Sstevel@tonic-gate 
34877c478bd9Sstevel@tonic-gate 	val = strchr(opt, '=');
34887c478bd9Sstevel@tonic-gate 	if (val == NULL) {
34897c478bd9Sstevel@tonic-gate 		bam_error(INVALID_ENTRY, opt);
34907c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
34917c478bd9Sstevel@tonic-gate 	}
34927c478bd9Sstevel@tonic-gate 
34937c478bd9Sstevel@tonic-gate 	*val = '\0';
34947c478bd9Sstevel@tonic-gate 	if (strcmp(opt, "default") == 0) {
34957c478bd9Sstevel@tonic-gate 		optnum = DEFAULT_CMD;
34967c478bd9Sstevel@tonic-gate 	} else if (strcmp(opt, "timeout") == 0) {
34977c478bd9Sstevel@tonic-gate 		optnum = TIMEOUT_CMD;
34987c478bd9Sstevel@tonic-gate 	} else {
34997c478bd9Sstevel@tonic-gate 		bam_error(INVALID_ENTRY, opt);
35007c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
35017c478bd9Sstevel@tonic-gate 	}
35027c478bd9Sstevel@tonic-gate 
35037c478bd9Sstevel@tonic-gate 	optval = s_strtol(val + 1);
35047c478bd9Sstevel@tonic-gate 	*val = '=';
35057c478bd9Sstevel@tonic-gate 	return (set_global(mp, menu_cmds[optnum], optval));
35067c478bd9Sstevel@tonic-gate }
35077c478bd9Sstevel@tonic-gate 
35087c478bd9Sstevel@tonic-gate /*
35097c478bd9Sstevel@tonic-gate  * The quiet argument suppresses messages. This is used
35107c478bd9Sstevel@tonic-gate  * when invoked in the context of other commands (e.g. list_entry)
35117c478bd9Sstevel@tonic-gate  */
35127c478bd9Sstevel@tonic-gate static error_t
35137c478bd9Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet)
35147c478bd9Sstevel@tonic-gate {
35157c478bd9Sstevel@tonic-gate 	line_t *lp;
35167c478bd9Sstevel@tonic-gate 	char *arg;
35177c478bd9Sstevel@tonic-gate 	int done, ret = BAM_SUCCESS;
35187c478bd9Sstevel@tonic-gate 
35197c478bd9Sstevel@tonic-gate 	assert(mp);
35207c478bd9Sstevel@tonic-gate 	assert(menu_path);
35217c478bd9Sstevel@tonic-gate 	assert(globalcmd);
35227c478bd9Sstevel@tonic-gate 
35237c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
35247c478bd9Sstevel@tonic-gate 		if (!quiet)
35257c478bd9Sstevel@tonic-gate 			bam_error(NO_MENU, menu_path);
35267c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
35277c478bd9Sstevel@tonic-gate 	}
35287c478bd9Sstevel@tonic-gate 
35297c478bd9Sstevel@tonic-gate 	done = 0;
35307c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
35317c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
35327c478bd9Sstevel@tonic-gate 			continue;
35337c478bd9Sstevel@tonic-gate 
35347c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
35357c478bd9Sstevel@tonic-gate 			if (!quiet)
35367c478bd9Sstevel@tonic-gate 				bam_error(NO_CMD, lp->lineNum);
35377c478bd9Sstevel@tonic-gate 			continue;
35387c478bd9Sstevel@tonic-gate 		}
35397c478bd9Sstevel@tonic-gate 
35407c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
35417c478bd9Sstevel@tonic-gate 			continue;
35427c478bd9Sstevel@tonic-gate 
35437c478bd9Sstevel@tonic-gate 		/* Found global. Check for duplicates */
35447c478bd9Sstevel@tonic-gate 		if (done && !quiet) {
35457c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
35467c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
35477c478bd9Sstevel@tonic-gate 		}
35487c478bd9Sstevel@tonic-gate 
35497c478bd9Sstevel@tonic-gate 		arg = lp->arg ? lp->arg : "";
35507c478bd9Sstevel@tonic-gate 		bam_print(GLOBAL_CMD, globalcmd, arg);
35517c478bd9Sstevel@tonic-gate 		done = 1;
35527c478bd9Sstevel@tonic-gate 	}
35537c478bd9Sstevel@tonic-gate 
35547c478bd9Sstevel@tonic-gate 	if (!done && bam_verbose)
35557c478bd9Sstevel@tonic-gate 		bam_print(NO_ENTRY, globalcmd);
35567c478bd9Sstevel@tonic-gate 
35577c478bd9Sstevel@tonic-gate 	return (ret);
35587c478bd9Sstevel@tonic-gate }
35597c478bd9Sstevel@tonic-gate 
35607c478bd9Sstevel@tonic-gate static error_t
35617c478bd9Sstevel@tonic-gate menu_write(char *root, menu_t *mp)
35627c478bd9Sstevel@tonic-gate {
35637c478bd9Sstevel@tonic-gate 	return (list2file(root, MENU_TMP, GRUB_MENU, mp->start));
35647c478bd9Sstevel@tonic-gate }
35657c478bd9Sstevel@tonic-gate 
35667c478bd9Sstevel@tonic-gate static void
35677c478bd9Sstevel@tonic-gate line_free(line_t *lp)
35687c478bd9Sstevel@tonic-gate {
35697c478bd9Sstevel@tonic-gate 	if (lp == NULL)
35707c478bd9Sstevel@tonic-gate 		return;
35717c478bd9Sstevel@tonic-gate 
35727c478bd9Sstevel@tonic-gate 	if (lp->cmd)
35737c478bd9Sstevel@tonic-gate 		free(lp->cmd);
35747c478bd9Sstevel@tonic-gate 	if (lp->sep)
35757c478bd9Sstevel@tonic-gate 		free(lp->sep);
35767c478bd9Sstevel@tonic-gate 	if (lp->arg)
35777c478bd9Sstevel@tonic-gate 		free(lp->arg);
35787c478bd9Sstevel@tonic-gate 	if (lp->line)
35797c478bd9Sstevel@tonic-gate 		free(lp->line);
35807c478bd9Sstevel@tonic-gate 	free(lp);
35817c478bd9Sstevel@tonic-gate }
35827c478bd9Sstevel@tonic-gate 
35837c478bd9Sstevel@tonic-gate static void
35847c478bd9Sstevel@tonic-gate linelist_free(line_t *start)
35857c478bd9Sstevel@tonic-gate {
35867c478bd9Sstevel@tonic-gate 	line_t *lp;
35877c478bd9Sstevel@tonic-gate 
35887c478bd9Sstevel@tonic-gate 	while (start) {
35897c478bd9Sstevel@tonic-gate 		lp = start;
35907c478bd9Sstevel@tonic-gate 		start = start->next;
35917c478bd9Sstevel@tonic-gate 		line_free(lp);
35927c478bd9Sstevel@tonic-gate 	}
35937c478bd9Sstevel@tonic-gate }
35947c478bd9Sstevel@tonic-gate 
35957c478bd9Sstevel@tonic-gate static void
35967c478bd9Sstevel@tonic-gate filelist_free(filelist_t *flistp)
35977c478bd9Sstevel@tonic-gate {
35987c478bd9Sstevel@tonic-gate 	linelist_free(flistp->head);
35997c478bd9Sstevel@tonic-gate 	flistp->head = NULL;
36007c478bd9Sstevel@tonic-gate 	flistp->tail = NULL;
36017c478bd9Sstevel@tonic-gate }
36027c478bd9Sstevel@tonic-gate 
36037c478bd9Sstevel@tonic-gate static void
36047c478bd9Sstevel@tonic-gate menu_free(menu_t *mp)
36057c478bd9Sstevel@tonic-gate {
36068c1b6884Sszhou 	entry_t *ent, *tmp;
36077c478bd9Sstevel@tonic-gate 	assert(mp);
36087c478bd9Sstevel@tonic-gate 
36097c478bd9Sstevel@tonic-gate 	if (mp->start)
36107c478bd9Sstevel@tonic-gate 		linelist_free(mp->start);
36118c1b6884Sszhou 	ent = mp->entries;
36128c1b6884Sszhou 	while (ent) {
36138c1b6884Sszhou 		tmp = ent;
36148c1b6884Sszhou 		ent = tmp->next;
36158c1b6884Sszhou 		free(tmp);
36168c1b6884Sszhou 	}
36177c478bd9Sstevel@tonic-gate 
36188c1b6884Sszhou 	free(mp);
36197c478bd9Sstevel@tonic-gate }
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate /*
36227c478bd9Sstevel@tonic-gate  * Utility routines
36237c478bd9Sstevel@tonic-gate  */
36247c478bd9Sstevel@tonic-gate 
36257c478bd9Sstevel@tonic-gate 
36267c478bd9Sstevel@tonic-gate /*
36277c478bd9Sstevel@tonic-gate  * Returns 0 on success
36287c478bd9Sstevel@tonic-gate  * Any other value indicates an error
36297c478bd9Sstevel@tonic-gate  */
36307c478bd9Sstevel@tonic-gate static int
36317c478bd9Sstevel@tonic-gate exec_cmd(char *cmdline, char *output, int64_t osize)
36327c478bd9Sstevel@tonic-gate {
36337c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
36347c478bd9Sstevel@tonic-gate 	int ret;
36357c478bd9Sstevel@tonic-gate 	FILE *ptr;
36367c478bd9Sstevel@tonic-gate 	size_t len;
36377c478bd9Sstevel@tonic-gate 	sigset_t set;
36387c478bd9Sstevel@tonic-gate 	void (*disp)(int);
36397c478bd9Sstevel@tonic-gate 
36407c478bd9Sstevel@tonic-gate 	/*
36417c478bd9Sstevel@tonic-gate 	 * For security
36427c478bd9Sstevel@tonic-gate 	 * - only absolute paths are allowed
36437c478bd9Sstevel@tonic-gate 	 * - set IFS to space and tab
36447c478bd9Sstevel@tonic-gate 	 */
36457c478bd9Sstevel@tonic-gate 	if (*cmdline != '/') {
36467c478bd9Sstevel@tonic-gate 		bam_error(ABS_PATH_REQ, cmdline);
36477c478bd9Sstevel@tonic-gate 		return (-1);
36487c478bd9Sstevel@tonic-gate 	}
36497c478bd9Sstevel@tonic-gate 	(void) putenv("IFS= \t");
36507c478bd9Sstevel@tonic-gate 
36517c478bd9Sstevel@tonic-gate 	/*
36527c478bd9Sstevel@tonic-gate 	 * We may have been exec'ed with SIGCHLD blocked
36537c478bd9Sstevel@tonic-gate 	 * unblock it here
36547c478bd9Sstevel@tonic-gate 	 */
36557c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
36567c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
36577c478bd9Sstevel@tonic-gate 	if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
36587c478bd9Sstevel@tonic-gate 		bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno));
36597c478bd9Sstevel@tonic-gate 		return (-1);
36607c478bd9Sstevel@tonic-gate 	}
36617c478bd9Sstevel@tonic-gate 
36627c478bd9Sstevel@tonic-gate 	/*
36637c478bd9Sstevel@tonic-gate 	 * Set SIGCHLD disposition to SIG_DFL for popen/pclose
36647c478bd9Sstevel@tonic-gate 	 */
36657c478bd9Sstevel@tonic-gate 	disp = sigset(SIGCHLD, SIG_DFL);
36667c478bd9Sstevel@tonic-gate 	if (disp == SIG_ERR) {
36677c478bd9Sstevel@tonic-gate 		bam_error(FAILED_SIG, strerror(errno));
36687c478bd9Sstevel@tonic-gate 		return (-1);
36697c478bd9Sstevel@tonic-gate 	}
36707c478bd9Sstevel@tonic-gate 	if (disp == SIG_HOLD) {
36717c478bd9Sstevel@tonic-gate 		bam_error(BLOCKED_SIG, cmdline);
36727c478bd9Sstevel@tonic-gate 		return (-1);
36737c478bd9Sstevel@tonic-gate 	}
36747c478bd9Sstevel@tonic-gate 
36757c478bd9Sstevel@tonic-gate 	ptr = popen(cmdline, "r");
36767c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
36777c478bd9Sstevel@tonic-gate 		bam_error(POPEN_FAIL, cmdline, strerror(errno));
36787c478bd9Sstevel@tonic-gate 		return (-1);
36797c478bd9Sstevel@tonic-gate 	}
36807c478bd9Sstevel@tonic-gate 
36817c478bd9Sstevel@tonic-gate 	/*
36827c478bd9Sstevel@tonic-gate 	 * If we simply do a pclose() following a popen(), pclose()
36837c478bd9Sstevel@tonic-gate 	 * will close the reader end of the pipe immediately even
36847c478bd9Sstevel@tonic-gate 	 * if the child process has not started/exited. pclose()
36857c478bd9Sstevel@tonic-gate 	 * does wait for cmd to terminate before returning though.
36867c478bd9Sstevel@tonic-gate 	 * When the executed command writes its output to the pipe
36877c478bd9Sstevel@tonic-gate 	 * there is no reader process and the command dies with
36887c478bd9Sstevel@tonic-gate 	 * SIGPIPE. To avoid this we read repeatedly until read
36897c478bd9Sstevel@tonic-gate 	 * terminates with EOF. This indicates that the command
36907c478bd9Sstevel@tonic-gate 	 * (writer) has closed the pipe and we can safely do a
36917c478bd9Sstevel@tonic-gate 	 * pclose().
36927c478bd9Sstevel@tonic-gate 	 *
36937c478bd9Sstevel@tonic-gate 	 * Since pclose() does wait for the command to exit,
36947c478bd9Sstevel@tonic-gate 	 * we can safely reap the exit status of the command
36957c478bd9Sstevel@tonic-gate 	 * from the value returned by pclose()
36967c478bd9Sstevel@tonic-gate 	 */
36977c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), ptr) != NULL) {
36987c478bd9Sstevel@tonic-gate 		/* if (bam_verbose)  XXX */
36997c478bd9Sstevel@tonic-gate 			bam_print(PRINT_NO_NEWLINE, buf);
37007c478bd9Sstevel@tonic-gate 		if (output && osize > 0) {
37017c478bd9Sstevel@tonic-gate 			(void) snprintf(output, osize, "%s", buf);
37027c478bd9Sstevel@tonic-gate 			len = strlen(buf);
37037c478bd9Sstevel@tonic-gate 			output += len;
37047c478bd9Sstevel@tonic-gate 			osize -= len;
37057c478bd9Sstevel@tonic-gate 		}
37067c478bd9Sstevel@tonic-gate 	}
37077c478bd9Sstevel@tonic-gate 
37087c478bd9Sstevel@tonic-gate 	ret = pclose(ptr);
37097c478bd9Sstevel@tonic-gate 	if (ret == -1) {
37107c478bd9Sstevel@tonic-gate 		bam_error(PCLOSE_FAIL, cmdline, strerror(errno));
37117c478bd9Sstevel@tonic-gate 		return (-1);
37127c478bd9Sstevel@tonic-gate 	}
37137c478bd9Sstevel@tonic-gate 
37147c478bd9Sstevel@tonic-gate 	if (WIFEXITED(ret)) {
37157c478bd9Sstevel@tonic-gate 		return (WEXITSTATUS(ret));
37167c478bd9Sstevel@tonic-gate 	} else {
37177c478bd9Sstevel@tonic-gate 		bam_error(EXEC_FAIL, cmdline, ret);
37187c478bd9Sstevel@tonic-gate 		return (-1);
37197c478bd9Sstevel@tonic-gate 	}
37207c478bd9Sstevel@tonic-gate }
37217c478bd9Sstevel@tonic-gate 
37227c478bd9Sstevel@tonic-gate /*
37237c478bd9Sstevel@tonic-gate  * Since this function returns -1 on error
37247c478bd9Sstevel@tonic-gate  * it cannot be used to convert -1. However,
37257c478bd9Sstevel@tonic-gate  * that is sufficient for what we need.
37267c478bd9Sstevel@tonic-gate  */
37277c478bd9Sstevel@tonic-gate static long
37287c478bd9Sstevel@tonic-gate s_strtol(char *str)
37297c478bd9Sstevel@tonic-gate {
37307c478bd9Sstevel@tonic-gate 	long l;
37317c478bd9Sstevel@tonic-gate 	char *res = NULL;
37327c478bd9Sstevel@tonic-gate 
37337c478bd9Sstevel@tonic-gate 	if (str == NULL) {
37347c478bd9Sstevel@tonic-gate 		return (-1);
37357c478bd9Sstevel@tonic-gate 	}
37367c478bd9Sstevel@tonic-gate 
37377c478bd9Sstevel@tonic-gate 	errno = 0;
37387c478bd9Sstevel@tonic-gate 	l = strtol(str, &res, 10);
37397c478bd9Sstevel@tonic-gate 	if (errno || *res != '\0') {
37407c478bd9Sstevel@tonic-gate 		return (-1);
37417c478bd9Sstevel@tonic-gate 	}
37427c478bd9Sstevel@tonic-gate 
37437c478bd9Sstevel@tonic-gate 	return (l);
37447c478bd9Sstevel@tonic-gate }
37457c478bd9Sstevel@tonic-gate 
37467c478bd9Sstevel@tonic-gate /*
37477c478bd9Sstevel@tonic-gate  * Wrapper around fputs, that adds a newline (since fputs doesn't)
37487c478bd9Sstevel@tonic-gate  */
37497c478bd9Sstevel@tonic-gate static int
37507c478bd9Sstevel@tonic-gate s_fputs(char *str, FILE *fp)
37517c478bd9Sstevel@tonic-gate {
37527c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
37537c478bd9Sstevel@tonic-gate 
37547c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s\n", str);
37557c478bd9Sstevel@tonic-gate 	return (fputs(linebuf, fp));
37567c478bd9Sstevel@tonic-gate }
37577c478bd9Sstevel@tonic-gate 
37587c478bd9Sstevel@tonic-gate /*
37597c478bd9Sstevel@tonic-gate  * Wrapper around fgets, that strips newlines returned by fgets
37607c478bd9Sstevel@tonic-gate  */
37617c478bd9Sstevel@tonic-gate static char *
37627c478bd9Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp)
37637c478bd9Sstevel@tonic-gate {
37647c478bd9Sstevel@tonic-gate 	int n;
37657c478bd9Sstevel@tonic-gate 
37667c478bd9Sstevel@tonic-gate 	buf = fgets(buf, buflen, fp);
37677c478bd9Sstevel@tonic-gate 	if (buf) {
37687c478bd9Sstevel@tonic-gate 		n = strlen(buf);
37697c478bd9Sstevel@tonic-gate 		if (n == buflen - 1 && buf[n-1] != '\n')
37707c478bd9Sstevel@tonic-gate 			bam_error(TOO_LONG, buflen - 1, buf);
37717c478bd9Sstevel@tonic-gate 		buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1];
37727c478bd9Sstevel@tonic-gate 	}
37737c478bd9Sstevel@tonic-gate 
37747c478bd9Sstevel@tonic-gate 	return (buf);
37757c478bd9Sstevel@tonic-gate }
37767c478bd9Sstevel@tonic-gate 
37777c478bd9Sstevel@tonic-gate static void *
37787c478bd9Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz)
37797c478bd9Sstevel@tonic-gate {
37807c478bd9Sstevel@tonic-gate 	void *ptr;
37817c478bd9Sstevel@tonic-gate 
37827c478bd9Sstevel@tonic-gate 	ptr = calloc(nelem, sz);
37837c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
37847c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, nelem*sz);
37857c478bd9Sstevel@tonic-gate 		bam_exit(1);
37867c478bd9Sstevel@tonic-gate 	}
37877c478bd9Sstevel@tonic-gate 	return (ptr);
37887c478bd9Sstevel@tonic-gate }
37897c478bd9Sstevel@tonic-gate 
37907c478bd9Sstevel@tonic-gate static char *
37917c478bd9Sstevel@tonic-gate s_strdup(char *str)
37927c478bd9Sstevel@tonic-gate {
37937c478bd9Sstevel@tonic-gate 	char *ptr;
37947c478bd9Sstevel@tonic-gate 
37957c478bd9Sstevel@tonic-gate 	if (str == NULL)
37967c478bd9Sstevel@tonic-gate 		return (NULL);
37977c478bd9Sstevel@tonic-gate 
37987c478bd9Sstevel@tonic-gate 	ptr = strdup(str);
37997c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
38007c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, strlen(str) + 1);
38017c478bd9Sstevel@tonic-gate 		bam_exit(1);
38027c478bd9Sstevel@tonic-gate 	}
38037c478bd9Sstevel@tonic-gate 	return (ptr);
38047c478bd9Sstevel@tonic-gate }
38057c478bd9Sstevel@tonic-gate 
38067c478bd9Sstevel@tonic-gate /*
38077c478bd9Sstevel@tonic-gate  * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients)
38087c478bd9Sstevel@tonic-gate  * Returns 0 otherwise
38097c478bd9Sstevel@tonic-gate  */
38107c478bd9Sstevel@tonic-gate static int
38117c478bd9Sstevel@tonic-gate is_amd64(void)
38127c478bd9Sstevel@tonic-gate {
38137c478bd9Sstevel@tonic-gate 	static int amd64 = -1;
38147c478bd9Sstevel@tonic-gate 	char isabuf[257];	/* from sysinfo(2) manpage */
38157c478bd9Sstevel@tonic-gate 
38167c478bd9Sstevel@tonic-gate 	if (amd64 != -1)
38177c478bd9Sstevel@tonic-gate 		return (amd64);
38187c478bd9Sstevel@tonic-gate 
38197c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 &&
38207c478bd9Sstevel@tonic-gate 	    strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0)
38217c478bd9Sstevel@tonic-gate 		amd64 = 1;
38227c478bd9Sstevel@tonic-gate 	else if (strstr(isabuf, "i386") == NULL)
38237c478bd9Sstevel@tonic-gate 		amd64 = 1;		/* diskless server */
38247c478bd9Sstevel@tonic-gate 	else
38257c478bd9Sstevel@tonic-gate 		amd64 = 0;
38267c478bd9Sstevel@tonic-gate 
38277c478bd9Sstevel@tonic-gate 	return (amd64);
38287c478bd9Sstevel@tonic-gate }
38297c478bd9Sstevel@tonic-gate 
38307c478bd9Sstevel@tonic-gate static void
38317c478bd9Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s)
38327c478bd9Sstevel@tonic-gate {
38337c478bd9Sstevel@tonic-gate 	line_t *lp;
38347c478bd9Sstevel@tonic-gate 
38357c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
38367c478bd9Sstevel@tonic-gate 	lp->line = s_strdup(s);
38377c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL)
38387c478bd9Sstevel@tonic-gate 		flistp->head = lp;
38397c478bd9Sstevel@tonic-gate 	else
38407c478bd9Sstevel@tonic-gate 		flistp->tail->next = lp;
38417c478bd9Sstevel@tonic-gate 	flistp->tail = lp;
38427c478bd9Sstevel@tonic-gate }
3843