xref: /titanic_50/usr/src/cmd/boot/bootadm/bootadm.c (revision fbac2b2bf0e15dca847ad945116925885f7bf83a)
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
5*fbac2b2bSvikram  * Common Development and Distribution License (the "License").
6*fbac2b2bSvikram  * 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 /*
22*fbac2b2bSvikram  * 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>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include <pwd.h>
577c478bd9Sstevel@tonic-gate #include <grp.h>
587c478bd9Sstevel@tonic-gate #include <device_info.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <libintl.h>
617c478bd9Sstevel@tonic-gate #include <locale.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include <assert.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #include "message.h"
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
687c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
697c478bd9Sstevel@tonic-gate #endif	/* TEXT_DOMAIN */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /* Type definitions */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /* Primary subcmds */
747c478bd9Sstevel@tonic-gate typedef enum {
757c478bd9Sstevel@tonic-gate 	BAM_MENU = 3,
767c478bd9Sstevel@tonic-gate 	BAM_ARCHIVE
777c478bd9Sstevel@tonic-gate } subcmd_t;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /* GRUB menu per-line classification */
807c478bd9Sstevel@tonic-gate typedef enum {
817c478bd9Sstevel@tonic-gate 	BAM_INVALID = 0,
827c478bd9Sstevel@tonic-gate 	BAM_EMPTY,
837c478bd9Sstevel@tonic-gate 	BAM_COMMENT,
847c478bd9Sstevel@tonic-gate 	BAM_GLOBAL,
857c478bd9Sstevel@tonic-gate 	BAM_ENTRY,
867c478bd9Sstevel@tonic-gate 	BAM_TITLE
877c478bd9Sstevel@tonic-gate } menu_flag_t;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /* struct for menu.lst contents */
907c478bd9Sstevel@tonic-gate typedef struct line {
917c478bd9Sstevel@tonic-gate 	int  lineNum;	/* Line number in menu.lst */
927c478bd9Sstevel@tonic-gate 	int  entryNum;	/* menu boot entry #. ENTRY_INIT if not applicable */
937c478bd9Sstevel@tonic-gate 	char *cmd;
947c478bd9Sstevel@tonic-gate 	char *sep;
957c478bd9Sstevel@tonic-gate 	char *arg;
967c478bd9Sstevel@tonic-gate 	char *line;
977c478bd9Sstevel@tonic-gate 	menu_flag_t flags;
987c478bd9Sstevel@tonic-gate 	struct line *next;
998c1b6884Sszhou 	struct line *prev;
1007c478bd9Sstevel@tonic-gate } line_t;
1017c478bd9Sstevel@tonic-gate 
1028c1b6884Sszhou typedef struct entry {
1038c1b6884Sszhou 	struct entry *next;
1048c1b6884Sszhou 	struct entry *prev;
1058c1b6884Sszhou 	line_t *start;
1068c1b6884Sszhou 	line_t *end;
1078c1b6884Sszhou } entry_t;
1088c1b6884Sszhou 
1097c478bd9Sstevel@tonic-gate typedef struct {
1107c478bd9Sstevel@tonic-gate 	line_t *start;
1117c478bd9Sstevel@tonic-gate 	line_t *end;
1128c1b6884Sszhou 	line_t *curdefault;	/* line containing default */
1138c1b6884Sszhou 	line_t *olddefault;	/* old default line (commented) */
1148c1b6884Sszhou 	entry_t *entries;	/* os entries */
1157c478bd9Sstevel@tonic-gate } menu_t;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate typedef enum {
1187c478bd9Sstevel@tonic-gate     OPT_ABSENT = 0,	/* No option */
1197c478bd9Sstevel@tonic-gate     OPT_REQ,		/* option required */
1207c478bd9Sstevel@tonic-gate     OPT_OPTIONAL	/* option may or may not be present */
1217c478bd9Sstevel@tonic-gate } option_t;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate typedef enum {
124b610f78eSvikram     BAM_ERROR = -1,	/* Must be negative. add_boot_entry() depends on it */
1257c478bd9Sstevel@tonic-gate     BAM_SUCCESS = 0,
1267c478bd9Sstevel@tonic-gate     BAM_WRITE = 2
1277c478bd9Sstevel@tonic-gate } error_t;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate typedef struct {
1307c478bd9Sstevel@tonic-gate 	char	*subcmd;
1317c478bd9Sstevel@tonic-gate 	option_t option;
1327c478bd9Sstevel@tonic-gate 	error_t (*handler)();
1337c478bd9Sstevel@tonic-gate } subcmd_defn_t;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #define	BAM_MAXLINE	8192
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate #define	LINE_INIT	0	/* lineNum initial value */
1397c478bd9Sstevel@tonic-gate #define	ENTRY_INIT	-1	/* entryNum initial value */
1407c478bd9Sstevel@tonic-gate #define	ALL_ENTRIES	-2	/* selects all boot entries */
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate #define	GRUB_DIR		"/boot/grub"
1437c478bd9Sstevel@tonic-gate #define	MULTI_BOOT		"/platform/i86pc/multiboot"
1447c478bd9Sstevel@tonic-gate #define	BOOT_ARCHIVE		"/platform/i86pc/boot_archive"
1457c478bd9Sstevel@tonic-gate #define	GRUB_MENU		"/boot/grub/menu.lst"
1467c478bd9Sstevel@tonic-gate #define	MENU_TMP		"/boot/grub/menu.lst.tmp"
1477c478bd9Sstevel@tonic-gate #define	RAMDISK_SPECIAL		"/ramdisk"
14840541d5dSvikram #define	STUBBOOT		"/stubboot"
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /* lock related */
1517c478bd9Sstevel@tonic-gate #define	BAM_LOCK_FILE		"/var/run/bootadm.lock"
1527c478bd9Sstevel@tonic-gate #define	LOCK_FILE_PERMS		(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate #define	CREATE_RAMDISK		"/boot/solaris/bin/create_ramdisk"
1557c478bd9Sstevel@tonic-gate #define	CREATE_DISKMAP		"/boot/solaris/bin/create_diskmap"
1567c478bd9Sstevel@tonic-gate #define	GRUBDISK_MAP		"/var/run/solaris_grubdisk.map"
1577c478bd9Sstevel@tonic-gate 
158b610f78eSvikram #define	GRUB_slice		"/etc/lu/GRUB_slice"
159b610f78eSvikram #define	GRUB_root		"/etc/lu/GRUB_root"
160b610f78eSvikram #define	GRUB_backup_menu	"/etc/lu/GRUB_backup_menu"
161b610f78eSvikram #define	GRUB_slice_mntpt	"/tmp/GRUB_slice_mntpt"
162b610f78eSvikram #define	LU_ACTIVATE_FILE	"/etc/lu/DelayUpdate/activate.sh"
163*fbac2b2bSvikram #define	GRUB_fdisk		"/etc/lu/GRUB_fdisk"
164*fbac2b2bSvikram #define	GRUB_fdisk_target	"/etc/lu/GRUB_fdisk_target"
165b610f78eSvikram 
166b610f78eSvikram #define	INSTALLGRUB		"/sbin/installgrub"
167b610f78eSvikram #define	STAGE1			"/boot/grub/stage1"
168b610f78eSvikram #define	STAGE2			"/boot/grub/stage2"
169b610f78eSvikram 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * Default file attributes
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_MODE	0644	/* default permissions */
1747c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_UID		0	/* user root */
1757c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_GID		3	/* group sys */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate  * Menu related
1797c478bd9Sstevel@tonic-gate  * menu_cmd_t and menu_cmds must be kept in sync
1807c478bd9Sstevel@tonic-gate  */
1817c478bd9Sstevel@tonic-gate typedef enum {
1827c478bd9Sstevel@tonic-gate 	DEFAULT_CMD = 0,
1837c478bd9Sstevel@tonic-gate 	TIMEOUT_CMD,
1847c478bd9Sstevel@tonic-gate 	TITLE_CMD,
1857c478bd9Sstevel@tonic-gate 	ROOT_CMD,
1867c478bd9Sstevel@tonic-gate 	KERNEL_CMD,
1877c478bd9Sstevel@tonic-gate 	MODULE_CMD,
1887c478bd9Sstevel@tonic-gate 	SEP_CMD,
1897c478bd9Sstevel@tonic-gate 	COMMENT_CMD
1907c478bd9Sstevel@tonic-gate } menu_cmd_t;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate static char *menu_cmds[] = {
1937c478bd9Sstevel@tonic-gate 	"default",	/* DEFAULT_CMD */
1947c478bd9Sstevel@tonic-gate 	"timeout",	/* TIMEOUT_CMD */
1957c478bd9Sstevel@tonic-gate 	"title",	/* TITLE_CMD */
1967c478bd9Sstevel@tonic-gate 	"root",		/* ROOT_CMD */
1977c478bd9Sstevel@tonic-gate 	"kernel",	/* KERNEL_CMD */
1987c478bd9Sstevel@tonic-gate 	"module",	/* MODULE_CMD */
1997c478bd9Sstevel@tonic-gate 	" ",		/* SEP_CMD */
2007c478bd9Sstevel@tonic-gate 	"#",		/* COMMENT_CMD */
2017c478bd9Sstevel@tonic-gate 	NULL
2027c478bd9Sstevel@tonic-gate };
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate #define	OPT_ENTRY_NUM	"entry"
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * archive related
2087c478bd9Sstevel@tonic-gate  */
2097c478bd9Sstevel@tonic-gate typedef struct {
2107c478bd9Sstevel@tonic-gate 	line_t *head;
2117c478bd9Sstevel@tonic-gate 	line_t *tail;
2127c478bd9Sstevel@tonic-gate } filelist_t;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate #define	BOOT_FILE_LIST	"boot/solaris/filelist.ramdisk"
2157c478bd9Sstevel@tonic-gate #define	ETC_FILE_LIST	"etc/boot/solaris/filelist.ramdisk"
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate #define	FILE_STAT	"boot/solaris/filestat.ramdisk"
2187c478bd9Sstevel@tonic-gate #define	FILE_STAT_TMP	"boot/solaris/filestat.ramdisk.tmp"
2197c478bd9Sstevel@tonic-gate #define	DIR_PERMS	(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
2207c478bd9Sstevel@tonic-gate #define	FILE_STAT_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate #define	BAM_HDR		"---------- ADDED BY BOOTADM - DO NOT EDIT ----------"
2237c478bd9Sstevel@tonic-gate #define	BAM_FTR		"---------------------END BOOTADM--------------------"
2248c1b6884Sszhou #define	BAM_OLDDEF	"BOOTADM SAVED DEFAULT: "
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /* Globals */
2277c478bd9Sstevel@tonic-gate static char *prog;
2287c478bd9Sstevel@tonic-gate static subcmd_t bam_cmd;
2297c478bd9Sstevel@tonic-gate static char *bam_root;
2307c478bd9Sstevel@tonic-gate static int bam_rootlen;
2317c478bd9Sstevel@tonic-gate static int bam_root_readonly;
23240541d5dSvikram static int bam_alt_root;
2337c478bd9Sstevel@tonic-gate static char *bam_subcmd;
2347c478bd9Sstevel@tonic-gate static char *bam_opt;
2357c478bd9Sstevel@tonic-gate static int bam_debug;
2367c478bd9Sstevel@tonic-gate static char **bam_argv;
2377c478bd9Sstevel@tonic-gate static int bam_argc;
2387c478bd9Sstevel@tonic-gate static int bam_force;
2397c478bd9Sstevel@tonic-gate static int bam_verbose;
2407c478bd9Sstevel@tonic-gate static int bam_check;
2417c478bd9Sstevel@tonic-gate static int bam_smf_check;
2427c478bd9Sstevel@tonic-gate static int bam_lock_fd = -1;
2437c478bd9Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/";
244b610f78eSvikram static int bam_update_all;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /* function prototypes */
2477c478bd9Sstevel@tonic-gate static void parse_args_internal(int argc, char *argv[]);
2487c478bd9Sstevel@tonic-gate static void parse_args(int argc, char *argv[]);
2497c478bd9Sstevel@tonic-gate static error_t bam_menu(char *subcmd, char *opt, int argc, char *argv[]);
2507c478bd9Sstevel@tonic-gate static error_t bam_archive(char *subcmd, char *opt);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate static void bam_error(char *format, ...);
2537c478bd9Sstevel@tonic-gate static void bam_print(char *format, ...);
2547c478bd9Sstevel@tonic-gate static void bam_exit(int excode);
2557c478bd9Sstevel@tonic-gate static void bam_lock(void);
2567c478bd9Sstevel@tonic-gate static void bam_unlock(void);
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate static int exec_cmd(char *cmdline, char *output, int64_t osize);
2597c478bd9Sstevel@tonic-gate static error_t read_globals(menu_t *mp, char *menu_path,
2607c478bd9Sstevel@tonic-gate     char *globalcmd, int quiet);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate static menu_t *menu_read(char *menu_path);
2637c478bd9Sstevel@tonic-gate static error_t menu_write(char *root, menu_t *mp);
2647c478bd9Sstevel@tonic-gate static void linelist_free(line_t *start);
2657c478bd9Sstevel@tonic-gate static void menu_free(menu_t *mp);
2667c478bd9Sstevel@tonic-gate static void line_free(line_t *lp);
2677c478bd9Sstevel@tonic-gate static void filelist_free(filelist_t *flistp);
2687c478bd9Sstevel@tonic-gate static error_t list2file(char *root, char *tmp,
2697c478bd9Sstevel@tonic-gate     char *final, line_t *start);
2707c478bd9Sstevel@tonic-gate static error_t list_entry(menu_t *mp, char *menu_path, char *opt);
2717c478bd9Sstevel@tonic-gate static error_t delete_all_entries(menu_t *mp, char *menu_path, char *opt);
2727c478bd9Sstevel@tonic-gate static error_t update_entry(menu_t *mp, char *root, char *opt);
2737c478bd9Sstevel@tonic-gate static error_t update_temp(menu_t *mp, char *root, char *opt);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate static error_t update_archive(char *root, char *opt);
2767c478bd9Sstevel@tonic-gate static error_t list_archive(char *root, char *opt);
2777c478bd9Sstevel@tonic-gate static error_t update_all(char *root, char *opt);
2787c478bd9Sstevel@tonic-gate static error_t read_list(char *root, filelist_t  *flistp);
2797c478bd9Sstevel@tonic-gate static error_t set_global(menu_t *mp, char *globalcmd, int val);
2807c478bd9Sstevel@tonic-gate static error_t set_option(menu_t *mp, char *globalcmd, char *opt);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate static long s_strtol(char *str);
2837c478bd9Sstevel@tonic-gate static char *s_fgets(char *buf, int n, FILE *fp);
2847c478bd9Sstevel@tonic-gate static int s_fputs(char *str, FILE *fp);
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate static void *s_calloc(size_t nelem, size_t sz);
2877c478bd9Sstevel@tonic-gate static char *s_strdup(char *str);
2887c478bd9Sstevel@tonic-gate static int is_readonly(char *);
2897c478bd9Sstevel@tonic-gate static int is_amd64(void);
2907c478bd9Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *);
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate #if defined(__sparc)
2937c478bd9Sstevel@tonic-gate static void sparc_abort(void);
2947c478bd9Sstevel@tonic-gate #endif
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /* Menu related sub commands */
2977c478bd9Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = {
2987c478bd9Sstevel@tonic-gate 	"set_option",		OPT_OPTIONAL,	set_option,	/* PUB */
2997c478bd9Sstevel@tonic-gate 	"list_entry",		OPT_OPTIONAL,	list_entry,	/* PUB */
3007c478bd9Sstevel@tonic-gate 	"delete_all_entries",	OPT_ABSENT,	delete_all_entries, /* PVT */
3017c478bd9Sstevel@tonic-gate 	"update_entry",		OPT_REQ,	update_entry,	/* menu */
3027c478bd9Sstevel@tonic-gate 	"update_temp",		OPT_OPTIONAL,	update_temp,	/* reboot */
3037c478bd9Sstevel@tonic-gate 	NULL,			0,		NULL	/* must be last */
3047c478bd9Sstevel@tonic-gate };
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate /* Archive related sub commands */
3077c478bd9Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = {
3087c478bd9Sstevel@tonic-gate 	"update",		OPT_ABSENT,	update_archive, /* PUB */
3097c478bd9Sstevel@tonic-gate 	"update_all",		OPT_ABSENT,	update_all,	/* PVT */
3107c478bd9Sstevel@tonic-gate 	"list",			OPT_OPTIONAL,	list_archive,	/* PUB */
3117c478bd9Sstevel@tonic-gate 	NULL,			0,		NULL	/* must be last */
3127c478bd9Sstevel@tonic-gate };
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate static struct {
3157c478bd9Sstevel@tonic-gate 	nvlist_t *new_nvlp;
3167c478bd9Sstevel@tonic-gate 	nvlist_t *old_nvlp;
3177c478bd9Sstevel@tonic-gate 	int need_update;
3187c478bd9Sstevel@tonic-gate } walk_arg;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate static void
3217c478bd9Sstevel@tonic-gate usage(void)
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "USAGE:\n");
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/* archive usage */
3277c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s update-archive [-vn] [-R altroot]\n",
3287c478bd9Sstevel@tonic-gate 	    prog);
3297c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-archive [-R altroot]\n", prog);
3307c478bd9Sstevel@tonic-gate #ifndef __sparc
3317c478bd9Sstevel@tonic-gate 	/* x86 only */
3327c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog);
3337c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog);
3347c478bd9Sstevel@tonic-gate #endif
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate int
3387c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	error_t ret;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3437c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
3467c478bd9Sstevel@tonic-gate 		prog = argv[0];
3477c478bd9Sstevel@tonic-gate 	} else {
3487c478bd9Sstevel@tonic-gate 		prog++;
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if (geteuid() != 0) {
3527c478bd9Sstevel@tonic-gate 		bam_error(MUST_BE_ROOT);
3537c478bd9Sstevel@tonic-gate 		bam_exit(1);
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	bam_lock();
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/*
3597c478bd9Sstevel@tonic-gate 	 * Don't depend on caller's umask
3607c478bd9Sstevel@tonic-gate 	 */
3617c478bd9Sstevel@tonic-gate 	(void) umask(0022);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate #if defined(__sparc)
3667c478bd9Sstevel@tonic-gate 	/*
3677c478bd9Sstevel@tonic-gate 	 * There are only two valid invocations of bootadm
3687c478bd9Sstevel@tonic-gate 	 * on SPARC:
3697c478bd9Sstevel@tonic-gate 	 *
3707c478bd9Sstevel@tonic-gate 	 *	- SPARC diskless server creating boot_archive for i386 clients
3717c478bd9Sstevel@tonic-gate 	 *	- archive creation call during reboot of a SPARC system
3727c478bd9Sstevel@tonic-gate 	 *
3737c478bd9Sstevel@tonic-gate 	 *	The latter should be a NOP
3747c478bd9Sstevel@tonic-gate 	 */
3757c478bd9Sstevel@tonic-gate 	if (bam_cmd != BAM_ARCHIVE) {
3767c478bd9Sstevel@tonic-gate 		sparc_abort();
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate #endif
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	switch (bam_cmd) {
3817c478bd9Sstevel@tonic-gate 		case BAM_MENU:
3827c478bd9Sstevel@tonic-gate 			ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv);
3837c478bd9Sstevel@tonic-gate 			break;
3847c478bd9Sstevel@tonic-gate 		case BAM_ARCHIVE:
3857c478bd9Sstevel@tonic-gate 			ret = bam_archive(bam_subcmd, bam_opt);
3867c478bd9Sstevel@tonic-gate 			break;
3877c478bd9Sstevel@tonic-gate 		default:
3887c478bd9Sstevel@tonic-gate 			usage();
3897c478bd9Sstevel@tonic-gate 			bam_exit(1);
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS)
3937c478bd9Sstevel@tonic-gate 		bam_exit(1);
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	bam_unlock();
3967c478bd9Sstevel@tonic-gate 	return (0);
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate #if defined(__sparc)
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate static void
4027c478bd9Sstevel@tonic-gate sparc_abort(void)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	bam_error(NOT_ON_SPARC);
4057c478bd9Sstevel@tonic-gate 	bam_exit(1);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate #endif
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate /*
4117c478bd9Sstevel@tonic-gate  * Equivalence of public and internal commands:
4127c478bd9Sstevel@tonic-gate  *	update-archive  -- -a update
4137c478bd9Sstevel@tonic-gate  *	list-archive	-- -a list
4147c478bd9Sstevel@tonic-gate  *	set-menu	-- -m set_option
4157c478bd9Sstevel@tonic-gate  *	list-menu	-- -m list_entry
4167c478bd9Sstevel@tonic-gate  *	update-menu	-- -m update_entry
4177c478bd9Sstevel@tonic-gate  */
4187c478bd9Sstevel@tonic-gate static struct cmd_map {
4197c478bd9Sstevel@tonic-gate 	char *bam_cmdname;
4207c478bd9Sstevel@tonic-gate 	int bam_cmd;
4217c478bd9Sstevel@tonic-gate 	char *bam_subcmd;
4227c478bd9Sstevel@tonic-gate } cmd_map[] = {
4237c478bd9Sstevel@tonic-gate 	{ "update-archive",	BAM_ARCHIVE,	"update"},
4247c478bd9Sstevel@tonic-gate 	{ "list-archive",	BAM_ARCHIVE,	"list"},
4257c478bd9Sstevel@tonic-gate 	{ "set-menu",		BAM_MENU,	"set_option"},
4267c478bd9Sstevel@tonic-gate 	{ "list-menu",		BAM_MENU,	"list_entry"},
4277c478bd9Sstevel@tonic-gate 	{ "update-menu",	BAM_MENU,	"update_entry"},
4287c478bd9Sstevel@tonic-gate 	{ NULL,			0,		NULL}
4297c478bd9Sstevel@tonic-gate };
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate /*
4327c478bd9Sstevel@tonic-gate  * Commands syntax published in bootadm(1M) are parsed here
4337c478bd9Sstevel@tonic-gate  */
4347c478bd9Sstevel@tonic-gate static void
4357c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[])
4367c478bd9Sstevel@tonic-gate {
4377c478bd9Sstevel@tonic-gate 	struct cmd_map *cmp = cmd_map;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	/* command conforming to the final spec */
4407c478bd9Sstevel@tonic-gate 	if (argc > 1 && argv[1][0] != '-') {
4417c478bd9Sstevel@tonic-gate 		/*
4427c478bd9Sstevel@tonic-gate 		 * Map commands to internal table.
4437c478bd9Sstevel@tonic-gate 		 */
4447c478bd9Sstevel@tonic-gate 		while (cmp->bam_cmdname) {
4457c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], cmp->bam_cmdname) == 0) {
4467c478bd9Sstevel@tonic-gate 				bam_cmd = cmp->bam_cmd;
4477c478bd9Sstevel@tonic-gate 				bam_subcmd = cmp->bam_subcmd;
4487c478bd9Sstevel@tonic-gate 				break;
4497c478bd9Sstevel@tonic-gate 			}
4507c478bd9Sstevel@tonic-gate 			cmp++;
4517c478bd9Sstevel@tonic-gate 		}
4527c478bd9Sstevel@tonic-gate 		if (cmp->bam_cmdname == NULL) {
4537c478bd9Sstevel@tonic-gate 			usage();
4547c478bd9Sstevel@tonic-gate 			bam_exit(1);
4557c478bd9Sstevel@tonic-gate 		}
4567c478bd9Sstevel@tonic-gate 		argc--;
4577c478bd9Sstevel@tonic-gate 		argv++;
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	parse_args_internal(argc, argv);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate /*
4647c478bd9Sstevel@tonic-gate  * A combination of public and private commands are parsed here.
4657c478bd9Sstevel@tonic-gate  * The internal syntax and the corresponding functionality are:
4667c478bd9Sstevel@tonic-gate  *	-a update	-- update-archive
4677c478bd9Sstevel@tonic-gate  *	-a list		-- list-archive
4687c478bd9Sstevel@tonic-gate  *	-a update-all	-- (reboot to sync all mounted OS archive)
4697c478bd9Sstevel@tonic-gate  *	-m update_entry	-- update-menu
4707c478bd9Sstevel@tonic-gate  *	-m list_entry	-- list-menu
4717c478bd9Sstevel@tonic-gate  *	-m update_temp	-- (reboot -- [boot-args])
4727c478bd9Sstevel@tonic-gate  *	-m delete_all_entries -- (called from install)
4737c478bd9Sstevel@tonic-gate  */
4747c478bd9Sstevel@tonic-gate static void
4757c478bd9Sstevel@tonic-gate parse_args_internal(int argc, char *argv[])
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate 	int c, error;
4787c478bd9Sstevel@tonic-gate 	extern char *optarg;
4797c478bd9Sstevel@tonic-gate 	extern int optind, opterr;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	/* Suppress error message from getopt */
4827c478bd9Sstevel@tonic-gate 	opterr = 0;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	error = 0;
4858c1b6884Sszhou 	while ((c = getopt(argc, argv, "a:d:fm:no:vCR:")) != -1) {
4867c478bd9Sstevel@tonic-gate 		switch (c) {
4877c478bd9Sstevel@tonic-gate 		case 'a':
4887c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
4897c478bd9Sstevel@tonic-gate 				error = 1;
4907c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
4917c478bd9Sstevel@tonic-gate 			}
4927c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_ARCHIVE;
4937c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
4947c478bd9Sstevel@tonic-gate 			break;
4957c478bd9Sstevel@tonic-gate 		case 'd':
4967c478bd9Sstevel@tonic-gate 			if (bam_debug) {
4977c478bd9Sstevel@tonic-gate 				error = 1;
4987c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4997c478bd9Sstevel@tonic-gate 			}
5007c478bd9Sstevel@tonic-gate 			bam_debug = s_strtol(optarg);
5017c478bd9Sstevel@tonic-gate 			break;
5027c478bd9Sstevel@tonic-gate 		case 'f':
5037c478bd9Sstevel@tonic-gate 			if (bam_force) {
5047c478bd9Sstevel@tonic-gate 				error = 1;
5057c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5067c478bd9Sstevel@tonic-gate 			}
5077c478bd9Sstevel@tonic-gate 			bam_force = 1;
5087c478bd9Sstevel@tonic-gate 			break;
5097c478bd9Sstevel@tonic-gate 		case 'm':
5107c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
5117c478bd9Sstevel@tonic-gate 				error = 1;
5127c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
5137c478bd9Sstevel@tonic-gate 			}
5147c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_MENU;
5157c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
5167c478bd9Sstevel@tonic-gate 			break;
5177c478bd9Sstevel@tonic-gate 		case 'n':
5187c478bd9Sstevel@tonic-gate 			if (bam_check) {
5197c478bd9Sstevel@tonic-gate 				error = 1;
5207c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5217c478bd9Sstevel@tonic-gate 			}
5227c478bd9Sstevel@tonic-gate 			bam_check = 1;
5237c478bd9Sstevel@tonic-gate 			break;
5247c478bd9Sstevel@tonic-gate 		case 'o':
5257c478bd9Sstevel@tonic-gate 			if (bam_opt) {
5267c478bd9Sstevel@tonic-gate 				error = 1;
5277c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5287c478bd9Sstevel@tonic-gate 			}
5297c478bd9Sstevel@tonic-gate 			bam_opt = optarg;
5307c478bd9Sstevel@tonic-gate 			break;
5317c478bd9Sstevel@tonic-gate 		case 'v':
5327c478bd9Sstevel@tonic-gate 			if (bam_verbose) {
5337c478bd9Sstevel@tonic-gate 				error = 1;
5347c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5357c478bd9Sstevel@tonic-gate 			}
5367c478bd9Sstevel@tonic-gate 			bam_verbose = 1;
5377c478bd9Sstevel@tonic-gate 			break;
5388c1b6884Sszhou 		case 'C':
5398c1b6884Sszhou 			bam_smf_check = 1;
5408c1b6884Sszhou 			break;
5417c478bd9Sstevel@tonic-gate 		case 'R':
5427c478bd9Sstevel@tonic-gate 			if (bam_root) {
5437c478bd9Sstevel@tonic-gate 				error = 1;
5447c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5457c478bd9Sstevel@tonic-gate 				break;
5467c478bd9Sstevel@tonic-gate 			} else if (realpath(optarg, rootbuf) == NULL) {
5477c478bd9Sstevel@tonic-gate 				error = 1;
5487c478bd9Sstevel@tonic-gate 				bam_error(CANT_RESOLVE, optarg,
5497c478bd9Sstevel@tonic-gate 				    strerror(errno));
5507c478bd9Sstevel@tonic-gate 				break;
5517c478bd9Sstevel@tonic-gate 			}
55240541d5dSvikram 			bam_alt_root = 1;
5537c478bd9Sstevel@tonic-gate 			bam_root = rootbuf;
5547c478bd9Sstevel@tonic-gate 			bam_rootlen = strlen(rootbuf);
5557c478bd9Sstevel@tonic-gate 			break;
5567c478bd9Sstevel@tonic-gate 		case '?':
5577c478bd9Sstevel@tonic-gate 			error = 1;
5587c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, optopt);
5597c478bd9Sstevel@tonic-gate 			break;
5607c478bd9Sstevel@tonic-gate 		default :
5617c478bd9Sstevel@tonic-gate 			error = 1;
5627c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, c);
5637c478bd9Sstevel@tonic-gate 			break;
5647c478bd9Sstevel@tonic-gate 		}
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	/*
5687c478bd9Sstevel@tonic-gate 	 * A command option must be specfied
5697c478bd9Sstevel@tonic-gate 	 */
5707c478bd9Sstevel@tonic-gate 	if (!bam_cmd) {
5717c478bd9Sstevel@tonic-gate 		if (bam_opt && strcmp(bam_opt, "all") == 0) {
5727c478bd9Sstevel@tonic-gate 			usage();
5737c478bd9Sstevel@tonic-gate 			bam_exit(0);
5747c478bd9Sstevel@tonic-gate 		}
5757c478bd9Sstevel@tonic-gate 		bam_error(NEED_CMD);
5767c478bd9Sstevel@tonic-gate 		error = 1;
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	if (error) {
5807c478bd9Sstevel@tonic-gate 		usage();
5817c478bd9Sstevel@tonic-gate 		bam_exit(1);
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	if (optind > argc) {
5857c478bd9Sstevel@tonic-gate 		bam_error(INT_ERROR, "parse_args");
5867c478bd9Sstevel@tonic-gate 		bam_exit(1);
5877c478bd9Sstevel@tonic-gate 	} else if (optind < argc) {
5887c478bd9Sstevel@tonic-gate 		bam_argv = &argv[optind];
5897c478bd9Sstevel@tonic-gate 		bam_argc = argc - optind;
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	/*
5937c478bd9Sstevel@tonic-gate 	 * -n implies verbose mode
5947c478bd9Sstevel@tonic-gate 	 */
5957c478bd9Sstevel@tonic-gate 	if (bam_check)
5967c478bd9Sstevel@tonic-gate 		bam_verbose = 1;
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate static error_t
6007c478bd9Sstevel@tonic-gate check_subcmd_and_options(
6017c478bd9Sstevel@tonic-gate 	char *subcmd,
6027c478bd9Sstevel@tonic-gate 	char *opt,
6037c478bd9Sstevel@tonic-gate 	subcmd_defn_t *table,
6047c478bd9Sstevel@tonic-gate 	error_t (**fp)())
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	int i;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	if (subcmd == NULL) {
6097c478bd9Sstevel@tonic-gate 		bam_error(NEED_SUBCMD);
6107c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	if (bam_root == NULL) {
6147c478bd9Sstevel@tonic-gate 		bam_root = rootbuf;
6157c478bd9Sstevel@tonic-gate 		bam_rootlen = 1;
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	/* verify that subcmd is valid */
6197c478bd9Sstevel@tonic-gate 	for (i = 0; table[i].subcmd != NULL; i++) {
6207c478bd9Sstevel@tonic-gate 		if (strcmp(table[i].subcmd, subcmd) == 0)
6217c478bd9Sstevel@tonic-gate 			break;
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	if (table[i].subcmd == NULL) {
6257c478bd9Sstevel@tonic-gate 		bam_error(INVALID_SUBCMD, subcmd);
6267c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	/* subcmd verifies that opt is appropriate */
6307c478bd9Sstevel@tonic-gate 	if (table[i].option != OPT_OPTIONAL) {
6317c478bd9Sstevel@tonic-gate 		if ((table[i].option == OPT_REQ) ^ (opt != NULL)) {
6327c478bd9Sstevel@tonic-gate 			if (opt)
6337c478bd9Sstevel@tonic-gate 				bam_error(NO_OPT_REQ, subcmd);
6347c478bd9Sstevel@tonic-gate 			else
6357c478bd9Sstevel@tonic-gate 				bam_error(MISS_OPT, subcmd);
6367c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
6377c478bd9Sstevel@tonic-gate 		}
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	*fp = table[i].handler;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate 
645b610f78eSvikram 
646b610f78eSvikram static char *
64740541d5dSvikram mount_grub_slice(int *mnted, char **physlice, char **logslice, char **fs_type)
648b610f78eSvikram {
649b610f78eSvikram 	struct extmnttab mnt;
650b610f78eSvikram 	struct stat sb;
651b610f78eSvikram 	char buf[BAM_MAXLINE], dev[PATH_MAX], phys[PATH_MAX], fstype[32];
652f0f2c544Svikram 	char cmd[PATH_MAX];
653b610f78eSvikram 	char *mntpt;
654b610f78eSvikram 	int p, l, f;
655b610f78eSvikram 	FILE *fp;
656b610f78eSvikram 
657b610f78eSvikram 	assert(mnted);
658b610f78eSvikram 	*mnted = 0;
659b610f78eSvikram 
660b610f78eSvikram 	/*
66140541d5dSvikram 	 * physlice, logslice, fs_type  args may be NULL
662b610f78eSvikram 	 */
663b610f78eSvikram 	if (physlice)
664b610f78eSvikram 		*physlice = NULL;
66540541d5dSvikram 	if (logslice)
66640541d5dSvikram 		*logslice = NULL;
66740541d5dSvikram 	if (fs_type)
66840541d5dSvikram 		*fs_type = NULL;
669b610f78eSvikram 
670b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
671b610f78eSvikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
672b610f78eSvikram 		return (NULL);
673b610f78eSvikram 	}
674b610f78eSvikram 
675b610f78eSvikram 	fp = fopen(GRUB_slice, "r");
676b610f78eSvikram 	if (fp == NULL) {
677b610f78eSvikram 		bam_error(OPEN_FAIL, GRUB_slice, strerror(errno));
678b610f78eSvikram 		return (NULL);
679b610f78eSvikram 	}
680b610f78eSvikram 
681b610f78eSvikram 	dev[0] = fstype[0] = phys[0] = '\0';
682b610f78eSvikram 	p = sizeof ("PHYS_SLICE=") - 1;
683b610f78eSvikram 	l = sizeof ("LOG_SLICE=") - 1;
684b610f78eSvikram 	f = sizeof ("LOG_FSTYP=") - 1;
685b610f78eSvikram 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
686b610f78eSvikram 		if (strncmp(buf, "PHYS_SLICE=", p) == 0) {
687b610f78eSvikram 			(void) strlcpy(phys, buf + p, sizeof (phys));
688b610f78eSvikram 			continue;
689b610f78eSvikram 		}
690b610f78eSvikram 		if (strncmp(buf, "LOG_SLICE=", l) == 0) {
691b610f78eSvikram 			(void) strlcpy(dev, buf + l, sizeof (dev));
692b610f78eSvikram 			continue;
693b610f78eSvikram 		}
694b610f78eSvikram 		if (strncmp(buf, "LOG_FSTYP=", f) == 0) {
695b610f78eSvikram 			(void) strlcpy(fstype, buf + f, sizeof (fstype));
696b610f78eSvikram 			continue;
697b610f78eSvikram 		}
698b610f78eSvikram 	}
699b610f78eSvikram 	(void) fclose(fp);
700b610f78eSvikram 
701b610f78eSvikram 	if (dev[0] == '\0' || fstype[0] == '\0' || phys[0] == '\0') {
702b610f78eSvikram 		bam_error(BAD_SLICE_FILE, GRUB_slice);
703b610f78eSvikram 		return (NULL);
704b610f78eSvikram 	}
705b610f78eSvikram 
706b610f78eSvikram 	if (physlice) {
707b610f78eSvikram 		*physlice = s_strdup(phys);
708b610f78eSvikram 	}
70940541d5dSvikram 	if (logslice) {
71040541d5dSvikram 		*logslice = s_strdup(dev);
71140541d5dSvikram 	}
71240541d5dSvikram 	if (fs_type) {
71340541d5dSvikram 		*fs_type = s_strdup(fstype);
71440541d5dSvikram 	}
715b610f78eSvikram 
716b610f78eSvikram 	/*
717b610f78eSvikram 	 * Check if the slice is already mounted
718b610f78eSvikram 	 */
719b610f78eSvikram 	fp = fopen(MNTTAB, "r");
720b610f78eSvikram 	if (fp == NULL) {
721b610f78eSvikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
72240541d5dSvikram 		goto error;
723b610f78eSvikram 	}
724b610f78eSvikram 
725b610f78eSvikram 	resetmnttab(fp);
726b610f78eSvikram 
727b610f78eSvikram 	mntpt = NULL;
728b610f78eSvikram 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
729b610f78eSvikram 		if (strcmp(mnt.mnt_special, dev) == 0) {
730b610f78eSvikram 			mntpt = s_strdup(mnt.mnt_mountp);
731b610f78eSvikram 			break;
732b610f78eSvikram 		}
733b610f78eSvikram 	}
734b610f78eSvikram 
735b610f78eSvikram 	(void) fclose(fp);
736b610f78eSvikram 
737b610f78eSvikram 	if (mntpt) {
738b610f78eSvikram 		return (mntpt);
739b610f78eSvikram 	}
740b610f78eSvikram 
741b610f78eSvikram 
742b610f78eSvikram 	/*
743b610f78eSvikram 	 * GRUB slice is not mounted, we need to mount it now.
744b610f78eSvikram 	 * First create the mountpoint
745b610f78eSvikram 	 */
746b610f78eSvikram 	mntpt = s_calloc(1, PATH_MAX);
747b610f78eSvikram 	(void) snprintf(mntpt, PATH_MAX, "%s.%d", GRUB_slice_mntpt, getpid());
748b610f78eSvikram 	if (mkdir(mntpt, 0755) == -1 && errno != EEXIST) {
749b610f78eSvikram 		bam_error(MKDIR_FAILED, mntpt, strerror(errno));
750b610f78eSvikram 		free(mntpt);
75140541d5dSvikram 		goto error;
752b610f78eSvikram 	}
753b610f78eSvikram 
754f0f2c544Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/mount -F %s %s %s",
755f0f2c544Svikram 	    fstype, dev, mntpt);
756f0f2c544Svikram 
757f0f2c544Svikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
75840541d5dSvikram 		bam_error(MOUNT_FAILED, dev, fstype);
759b610f78eSvikram 		if (rmdir(mntpt) != 0) {
760b610f78eSvikram 			bam_error(RMDIR_FAILED, mntpt, strerror(errno));
761b610f78eSvikram 		}
762b610f78eSvikram 		free(mntpt);
76340541d5dSvikram 		goto error;
764b610f78eSvikram 	}
765b610f78eSvikram 
766b610f78eSvikram 	*mnted = 1;
767b610f78eSvikram 	return (mntpt);
76840541d5dSvikram 
76940541d5dSvikram error:
77040541d5dSvikram 	if (physlice) {
77140541d5dSvikram 		free(*physlice);
77240541d5dSvikram 		*physlice = NULL;
77340541d5dSvikram 	}
77440541d5dSvikram 	if (logslice) {
77540541d5dSvikram 		free(*logslice);
77640541d5dSvikram 		*logslice = NULL;
77740541d5dSvikram 	}
77840541d5dSvikram 	if (fs_type) {
77940541d5dSvikram 		free(*fs_type);
78040541d5dSvikram 		*fs_type = NULL;
78140541d5dSvikram 	}
78240541d5dSvikram 	return (NULL);
783b610f78eSvikram }
784b610f78eSvikram 
785b610f78eSvikram static void
78640541d5dSvikram umount_grub_slice(
78740541d5dSvikram 	int mnted,
78840541d5dSvikram 	char *mntpt,
78940541d5dSvikram 	char *physlice,
79040541d5dSvikram 	char *logslice,
79140541d5dSvikram 	char *fs_type)
792b610f78eSvikram {
793f0f2c544Svikram 	char cmd[PATH_MAX];
794f0f2c544Svikram 
795b610f78eSvikram 	/*
796b610f78eSvikram 	 * If we have not dealt with GRUB slice
797b610f78eSvikram 	 * we have nothing to do - just return.
798b610f78eSvikram 	 */
799b610f78eSvikram 	if (mntpt == NULL)
800b610f78eSvikram 		return;
801b610f78eSvikram 
802b610f78eSvikram 
803b610f78eSvikram 	/*
804b610f78eSvikram 	 * If we mounted the filesystem earlier in mount_grub_slice()
805b610f78eSvikram 	 * unmount it now.
806b610f78eSvikram 	 */
807b610f78eSvikram 	if (mnted) {
808f0f2c544Svikram 		(void) snprintf(cmd, sizeof (cmd), "/sbin/umount %s",
809f0f2c544Svikram 		    mntpt);
810f0f2c544Svikram 		if (exec_cmd(cmd, NULL, 0) != 0) {
811f0f2c544Svikram 			bam_error(UMOUNT_FAILED, mntpt);
812b610f78eSvikram 		}
813b610f78eSvikram 		if (rmdir(mntpt) != 0) {
814b610f78eSvikram 			bam_error(RMDIR_FAILED, mntpt, strerror(errno));
815b610f78eSvikram 		}
816b610f78eSvikram 	}
81740541d5dSvikram 
818b610f78eSvikram 	if (physlice)
819b610f78eSvikram 		free(physlice);
82040541d5dSvikram 	if (logslice)
82140541d5dSvikram 		free(logslice);
82240541d5dSvikram 	if (fs_type)
82340541d5dSvikram 		free(fs_type);
82440541d5dSvikram 
825b610f78eSvikram 	free(mntpt);
826b610f78eSvikram }
827b610f78eSvikram 
82840541d5dSvikram static char *
82940541d5dSvikram use_stubboot(void)
83040541d5dSvikram {
83140541d5dSvikram 	int mnted;
83240541d5dSvikram 	struct stat sb;
83340541d5dSvikram 	struct extmnttab mnt;
83440541d5dSvikram 	FILE *fp;
83540541d5dSvikram 	char cmd[PATH_MAX];
83640541d5dSvikram 
83740541d5dSvikram 	if (stat(STUBBOOT, &sb) != 0) {
83840541d5dSvikram 		bam_error(STUBBOOT_DIR_NOT_FOUND);
83940541d5dSvikram 		return (NULL);
84040541d5dSvikram 	}
84140541d5dSvikram 
84240541d5dSvikram 	/*
84340541d5dSvikram 	 * Check if stubboot is mounted. If not, mount it
84440541d5dSvikram 	 */
84540541d5dSvikram 	fp = fopen(MNTTAB, "r");
84640541d5dSvikram 	if (fp == NULL) {
84740541d5dSvikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
84840541d5dSvikram 		return (NULL);
84940541d5dSvikram 	}
85040541d5dSvikram 
85140541d5dSvikram 	resetmnttab(fp);
85240541d5dSvikram 
85340541d5dSvikram 	mnted = 0;
85440541d5dSvikram 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
85540541d5dSvikram 		if (strcmp(mnt.mnt_mountp, STUBBOOT) == 0) {
85640541d5dSvikram 			mnted = 1;
85740541d5dSvikram 			break;
85840541d5dSvikram 		}
85940541d5dSvikram 	}
86040541d5dSvikram 
86140541d5dSvikram 	(void) fclose(fp);
86240541d5dSvikram 
86340541d5dSvikram 	if (mnted)
86440541d5dSvikram 		return (STUBBOOT);
86540541d5dSvikram 
86640541d5dSvikram 	/*
86740541d5dSvikram 	 * Stubboot is not mounted, mount it now.
86840541d5dSvikram 	 * It should exist in /etc/vfstab
86940541d5dSvikram 	 */
87040541d5dSvikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/mount %s",
87140541d5dSvikram 	    STUBBOOT);
87240541d5dSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
87340541d5dSvikram 		bam_error(MOUNT_MNTPT_FAILED, STUBBOOT);
87440541d5dSvikram 		return (NULL);
87540541d5dSvikram 	}
87640541d5dSvikram 
87740541d5dSvikram 	return (STUBBOOT);
87840541d5dSvikram }
87940541d5dSvikram 
88040541d5dSvikram static void
88140541d5dSvikram disp_active_menu_locn(char *menu_path, char *logslice, char *fstype, int mnted)
88240541d5dSvikram {
88340541d5dSvikram 	/*
88440541d5dSvikram 	 * Check if we did a temp mount of an unmounted device.
88540541d5dSvikram 	 * If yes, print the block device and fstype for that device
88640541d5dSvikram 	 * else it is already mounted, so we print the path to the GRUB menu.
88740541d5dSvikram 	 */
88840541d5dSvikram 	if (mnted) {
88940541d5dSvikram 		bam_print(GRUB_MENU_DEVICE, logslice);
89040541d5dSvikram 		bam_print(GRUB_MENU_FSTYPE, fstype);
89140541d5dSvikram 	} else {
89240541d5dSvikram 		bam_print(GRUB_MENU_PATH, menu_path);
89340541d5dSvikram 	}
89440541d5dSvikram }
89540541d5dSvikram 
89640541d5dSvikram /*
89740541d5dSvikram  * NOTE: A single "/" is also considered a trailing slash and will
89840541d5dSvikram  * be deleted.
89940541d5dSvikram  */
90040541d5dSvikram static void
90140541d5dSvikram elide_trailing_slash(const char *src, char *dst, size_t dstsize)
90240541d5dSvikram {
90340541d5dSvikram 	size_t dstlen;
90440541d5dSvikram 
90540541d5dSvikram 	assert(src);
90640541d5dSvikram 	assert(dst);
90740541d5dSvikram 
90840541d5dSvikram 	(void) strlcpy(dst, src, dstsize);
90940541d5dSvikram 
91040541d5dSvikram 	dstlen = strlen(dst);
91140541d5dSvikram 	if (dst[dstlen - 1] == '/') {
91240541d5dSvikram 		dst[dstlen - 1] = '\0';
91340541d5dSvikram 	}
91440541d5dSvikram }
91540541d5dSvikram 
9167c478bd9Sstevel@tonic-gate static error_t
9177c478bd9Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[])
9187c478bd9Sstevel@tonic-gate {
9197c478bd9Sstevel@tonic-gate 	error_t ret;
9207c478bd9Sstevel@tonic-gate 	char menu_path[PATH_MAX];
9217c478bd9Sstevel@tonic-gate 	menu_t *menu;
92240541d5dSvikram 	char *mntpt, *menu_root, *logslice, *fstype;
923b610f78eSvikram 	struct stat sb;
924b610f78eSvikram 	int mnted;	/* set if we did a mount */
9257c478bd9Sstevel@tonic-gate 	error_t (*f)(menu_t *mp, char *menu_path, char *opt);
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	/*
9287c478bd9Sstevel@tonic-gate 	 * Check arguments
9297c478bd9Sstevel@tonic-gate 	 */
9307c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
9317c478bd9Sstevel@tonic-gate 	if (ret == BAM_ERROR) {
9327c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate 
935b610f78eSvikram 	mntpt = NULL;
936b610f78eSvikram 	mnted = 0;
93740541d5dSvikram 	logslice = fstype = NULL;
93840541d5dSvikram 
93940541d5dSvikram 	/*
94040541d5dSvikram 	 * If the user provides an alternate root, we
94140541d5dSvikram 	 * assume they know what they are doing and we
94240541d5dSvikram 	 * use it. Else we check if there is an
94340541d5dSvikram 	 * alternate location (other than /boot/grub)
94440541d5dSvikram 	 * for the GRUB menu
94540541d5dSvikram 	 */
94640541d5dSvikram 	if (bam_alt_root) {
94740541d5dSvikram 		menu_root = bam_root;
94840541d5dSvikram 	} else if (stat(GRUB_slice, &sb) == 0) {
94940541d5dSvikram 		mntpt = mount_grub_slice(&mnted, NULL, &logslice, &fstype);
950b610f78eSvikram 		menu_root = mntpt;
95140541d5dSvikram 	} else if (stat(STUBBOOT, &sb) == 0) {
95240541d5dSvikram 		menu_root = use_stubboot();
953b610f78eSvikram 	} else {
954b610f78eSvikram 		menu_root = bam_root;
955b610f78eSvikram 	}
956b610f78eSvikram 
95740541d5dSvikram 	if (menu_root == NULL) {
95840541d5dSvikram 		bam_error(CANNOT_LOCATE_GRUB_MENU);
95940541d5dSvikram 		return (BAM_ERROR);
96040541d5dSvikram 	}
96140541d5dSvikram 
96240541d5dSvikram 	elide_trailing_slash(menu_root, menu_path, sizeof (menu_path));
96340541d5dSvikram 	(void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path));
96440541d5dSvikram 
96540541d5dSvikram 	/*
96640541d5dSvikram 	 * If listing the menu, display the active menu
96740541d5dSvikram 	 * location
96840541d5dSvikram 	 */
96940541d5dSvikram 	if (strcmp(subcmd, "list_entry") == 0) {
97040541d5dSvikram 		disp_active_menu_locn(menu_path, logslice, fstype, mnted);
97140541d5dSvikram 	}
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	menu = menu_read(menu_path);
9747c478bd9Sstevel@tonic-gate 	assert(menu);
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	/*
9777c478bd9Sstevel@tonic-gate 	 * Special handling for setting timeout and default
9787c478bd9Sstevel@tonic-gate 	 */
9797c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "set_option") == 0) {
9807c478bd9Sstevel@tonic-gate 		if (largc != 1 || largv[0] == NULL) {
9817c478bd9Sstevel@tonic-gate 			usage();
98240541d5dSvikram 			menu_free(menu);
98340541d5dSvikram 			umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
9847c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
9857c478bd9Sstevel@tonic-gate 		}
9867c478bd9Sstevel@tonic-gate 		opt = largv[0];
9877c478bd9Sstevel@tonic-gate 	} else if (largc != 0) {
9887c478bd9Sstevel@tonic-gate 		usage();
98940541d5dSvikram 		menu_free(menu);
99040541d5dSvikram 		umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
9917c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
9927c478bd9Sstevel@tonic-gate 	}
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	/*
9957c478bd9Sstevel@tonic-gate 	 * Once the sub-cmd handler has run
9967c478bd9Sstevel@tonic-gate 	 * only the line field is guaranteed to have valid values
9977c478bd9Sstevel@tonic-gate 	 */
9987c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "update_entry") == 0)
9997c478bd9Sstevel@tonic-gate 		ret = f(menu, bam_root, opt);
10007c478bd9Sstevel@tonic-gate 	else
10017c478bd9Sstevel@tonic-gate 		ret = f(menu, menu_path, opt);
10027c478bd9Sstevel@tonic-gate 	if (ret == BAM_WRITE) {
1003b610f78eSvikram 		ret = menu_write(menu_root, menu);
10047c478bd9Sstevel@tonic-gate 	}
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	menu_free(menu);
10077c478bd9Sstevel@tonic-gate 
100840541d5dSvikram 	umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
1009b610f78eSvikram 
10107c478bd9Sstevel@tonic-gate 	return (ret);
10117c478bd9Sstevel@tonic-gate }
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate static error_t
10157c478bd9Sstevel@tonic-gate bam_archive(
10167c478bd9Sstevel@tonic-gate 	char *subcmd,
10177c478bd9Sstevel@tonic-gate 	char *opt)
10187c478bd9Sstevel@tonic-gate {
10197c478bd9Sstevel@tonic-gate 	error_t ret;
10207c478bd9Sstevel@tonic-gate 	error_t (*f)(char *root, char *opt);
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	/*
10238c1b6884Sszhou 	 * Add trailing / for archive subcommands
10248c1b6884Sszhou 	 */
10258c1b6884Sszhou 	if (rootbuf[strlen(rootbuf) - 1] != '/')
10268c1b6884Sszhou 		(void) strcat(rootbuf, "/");
10278c1b6884Sszhou 	bam_rootlen = strlen(rootbuf);
10288c1b6884Sszhou 
10298c1b6884Sszhou 	/*
10307c478bd9Sstevel@tonic-gate 	 * Check arguments
10317c478bd9Sstevel@tonic-gate 	 */
10327c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f);
10337c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS) {
10347c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
10357c478bd9Sstevel@tonic-gate 	}
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate #if defined(__sparc)
10387c478bd9Sstevel@tonic-gate 	/*
10397c478bd9Sstevel@tonic-gate 	 * A NOP if called on SPARC during reboot
10407c478bd9Sstevel@tonic-gate 	 */
10417c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "update_all") == 0)
10427c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
10437c478bd9Sstevel@tonic-gate 	else if (strcmp(subcmd, "update") != 0)
10447c478bd9Sstevel@tonic-gate 		sparc_abort();
10457c478bd9Sstevel@tonic-gate #endif
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	/*
10487c478bd9Sstevel@tonic-gate 	 * Check archive not supported with update_all
10497c478bd9Sstevel@tonic-gate 	 * since it is awkward to display out-of-sync
10507c478bd9Sstevel@tonic-gate 	 * information for each BE.
10517c478bd9Sstevel@tonic-gate 	 */
10527c478bd9Sstevel@tonic-gate 	if (bam_check && strcmp(subcmd, "update_all") == 0) {
10537c478bd9Sstevel@tonic-gate 		bam_error(CHECK_NOT_SUPPORTED, subcmd);
10547c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
10557c478bd9Sstevel@tonic-gate 	}
10567c478bd9Sstevel@tonic-gate 
1057b610f78eSvikram 	if (strcmp(subcmd, "update_all") == 0)
1058b610f78eSvikram 		bam_update_all = 1;
1059b610f78eSvikram 
1060b610f78eSvikram 	ret = f(bam_root, opt);
1061b610f78eSvikram 
1062b610f78eSvikram 	bam_update_all = 0;
1063b610f78eSvikram 
1064b610f78eSvikram 	return (ret);
10657c478bd9Sstevel@tonic-gate }
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
10687c478bd9Sstevel@tonic-gate static void
10697c478bd9Sstevel@tonic-gate bam_error(char *format, ...)
10707c478bd9Sstevel@tonic-gate {
10717c478bd9Sstevel@tonic-gate 	va_list ap;
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	va_start(ap, format);
10747c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", prog);
10757c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
10767c478bd9Sstevel@tonic-gate 	va_end(ap);
10777c478bd9Sstevel@tonic-gate }
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
10807c478bd9Sstevel@tonic-gate static void
10817c478bd9Sstevel@tonic-gate bam_print(char *format, ...)
10827c478bd9Sstevel@tonic-gate {
10837c478bd9Sstevel@tonic-gate 	va_list ap;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	va_start(ap, format);
10867c478bd9Sstevel@tonic-gate 	(void) vfprintf(stdout, format, ap);
10877c478bd9Sstevel@tonic-gate 	va_end(ap);
10887c478bd9Sstevel@tonic-gate }
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate static void
10917c478bd9Sstevel@tonic-gate bam_exit(int excode)
10927c478bd9Sstevel@tonic-gate {
10937c478bd9Sstevel@tonic-gate 	bam_unlock();
10947c478bd9Sstevel@tonic-gate 	exit(excode);
10957c478bd9Sstevel@tonic-gate }
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate static void
10987c478bd9Sstevel@tonic-gate bam_lock(void)
10997c478bd9Sstevel@tonic-gate {
11007c478bd9Sstevel@tonic-gate 	struct flock lock;
11017c478bd9Sstevel@tonic-gate 	pid_t pid;
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS);
11047c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
11057c478bd9Sstevel@tonic-gate 		/*
11067c478bd9Sstevel@tonic-gate 		 * We may be invoked early in boot for archive verification.
11077c478bd9Sstevel@tonic-gate 		 * In this case, root is readonly and /var/run may not exist.
11087c478bd9Sstevel@tonic-gate 		 * Proceed without the lock
11097c478bd9Sstevel@tonic-gate 		 */
11107c478bd9Sstevel@tonic-gate 		if (errno == EROFS || errno == ENOENT) {
11117c478bd9Sstevel@tonic-gate 			bam_root_readonly = 1;
11127c478bd9Sstevel@tonic-gate 			return;
11137c478bd9Sstevel@tonic-gate 		}
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno));
11167c478bd9Sstevel@tonic-gate 		bam_exit(1);
11177c478bd9Sstevel@tonic-gate 	}
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
11207c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
11217c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
11227c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) {
11257c478bd9Sstevel@tonic-gate 		if (errno != EACCES && errno != EAGAIN) {
11267c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11277c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
11287c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
11297c478bd9Sstevel@tonic-gate 			bam_exit(1);
11307c478bd9Sstevel@tonic-gate 		}
11317c478bd9Sstevel@tonic-gate 		pid = 0;
11327c478bd9Sstevel@tonic-gate 		(void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0);
11337c478bd9Sstevel@tonic-gate 		bam_print(FILE_LOCKED, pid);
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 		lock.l_type = F_WRLCK;
11367c478bd9Sstevel@tonic-gate 		lock.l_whence = SEEK_SET;
11377c478bd9Sstevel@tonic-gate 		lock.l_start = 0;
11387c478bd9Sstevel@tonic-gate 		lock.l_len = 0;
11397c478bd9Sstevel@tonic-gate 		if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) {
11407c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11417c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
11427c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
11437c478bd9Sstevel@tonic-gate 			bam_exit(1);
11447c478bd9Sstevel@tonic-gate 		}
11457c478bd9Sstevel@tonic-gate 	}
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	/* We own the lock now */
11487c478bd9Sstevel@tonic-gate 	pid = getpid();
11497c478bd9Sstevel@tonic-gate 	(void) write(bam_lock_fd, &pid, sizeof (pid));
11507c478bd9Sstevel@tonic-gate }
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate static void
11537c478bd9Sstevel@tonic-gate bam_unlock(void)
11547c478bd9Sstevel@tonic-gate {
11557c478bd9Sstevel@tonic-gate 	struct flock unlock;
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	/*
11587c478bd9Sstevel@tonic-gate 	 * NOP if we don't hold the lock
11597c478bd9Sstevel@tonic-gate 	 */
11607c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
11617c478bd9Sstevel@tonic-gate 		return;
11627c478bd9Sstevel@tonic-gate 	}
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
11657c478bd9Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
11667c478bd9Sstevel@tonic-gate 	unlock.l_start = 0;
11677c478bd9Sstevel@tonic-gate 	unlock.l_len = 0;
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) {
11707c478bd9Sstevel@tonic-gate 		bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11717c478bd9Sstevel@tonic-gate 	}
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	if (close(bam_lock_fd) == -1) {
11747c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno));
11757c478bd9Sstevel@tonic-gate 	}
11767c478bd9Sstevel@tonic-gate 	bam_lock_fd = -1;
11777c478bd9Sstevel@tonic-gate }
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate static error_t
11807c478bd9Sstevel@tonic-gate list_archive(char *root, char *opt)
11817c478bd9Sstevel@tonic-gate {
11827c478bd9Sstevel@tonic-gate 	filelist_t flist;
11837c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
11847c478bd9Sstevel@tonic-gate 	line_t *lp;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	assert(root);
11877c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
11907c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
11917c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
11927c478bd9Sstevel@tonic-gate 	}
11937c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
11967c478bd9Sstevel@tonic-gate 		bam_print(PRINT, lp->line);
11977c478bd9Sstevel@tonic-gate 	}
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
12027c478bd9Sstevel@tonic-gate }
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate /*
12057c478bd9Sstevel@tonic-gate  * This routine writes a list of lines to a file.
12067c478bd9Sstevel@tonic-gate  * The list is *not* freed
12077c478bd9Sstevel@tonic-gate  */
12087c478bd9Sstevel@tonic-gate static error_t
12097c478bd9Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start)
12107c478bd9Sstevel@tonic-gate {
12117c478bd9Sstevel@tonic-gate 	char tmpfile[PATH_MAX];
12127c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
12137c478bd9Sstevel@tonic-gate 	FILE *fp;
12147c478bd9Sstevel@tonic-gate 	int ret;
12157c478bd9Sstevel@tonic-gate 	struct stat sb;
12167c478bd9Sstevel@tonic-gate 	mode_t mode;
12177c478bd9Sstevel@tonic-gate 	uid_t root_uid;
12187c478bd9Sstevel@tonic-gate 	gid_t sys_gid;
12197c478bd9Sstevel@tonic-gate 	struct passwd *pw;
12207c478bd9Sstevel@tonic-gate 	struct group *gp;
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, final);
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	if (start == NULL) {
12267c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != -1) {
12277c478bd9Sstevel@tonic-gate 			bam_print(UNLINK_EMPTY, path);
12287c478bd9Sstevel@tonic-gate 			if (unlink(path) != 0) {
12297c478bd9Sstevel@tonic-gate 				bam_error(UNLINK_FAIL, path, strerror(errno));
12307c478bd9Sstevel@tonic-gate 				return (BAM_ERROR);
12317c478bd9Sstevel@tonic-gate 			} else {
12327c478bd9Sstevel@tonic-gate 				return (BAM_SUCCESS);
12337c478bd9Sstevel@tonic-gate 			}
12347c478bd9Sstevel@tonic-gate 		}
12357c478bd9Sstevel@tonic-gate 	}
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	/*
12387c478bd9Sstevel@tonic-gate 	 * Preserve attributes of existing file if possible,
12397c478bd9Sstevel@tonic-gate 	 * otherwise ask the system for uid/gid of root/sys.
12407c478bd9Sstevel@tonic-gate 	 * If all fails, fall back on hard-coded defaults.
12417c478bd9Sstevel@tonic-gate 	 */
12427c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != -1) {
12437c478bd9Sstevel@tonic-gate 		mode = sb.st_mode;
12447c478bd9Sstevel@tonic-gate 		root_uid = sb.st_uid;
12457c478bd9Sstevel@tonic-gate 		sys_gid = sb.st_gid;
12467c478bd9Sstevel@tonic-gate 	} else {
12477c478bd9Sstevel@tonic-gate 		mode = DEFAULT_DEV_MODE;
12487c478bd9Sstevel@tonic-gate 		if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
12497c478bd9Sstevel@tonic-gate 			root_uid = pw->pw_uid;
12507c478bd9Sstevel@tonic-gate 		} else {
12517c478bd9Sstevel@tonic-gate 			if (bam_verbose)
12527c478bd9Sstevel@tonic-gate 				bam_error(CANT_FIND_USER,
12537c478bd9Sstevel@tonic-gate 				    DEFAULT_DEV_USER, DEFAULT_DEV_UID);
12547c478bd9Sstevel@tonic-gate 			root_uid = (uid_t)DEFAULT_DEV_UID;
12557c478bd9Sstevel@tonic-gate 		}
12567c478bd9Sstevel@tonic-gate 		if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
12577c478bd9Sstevel@tonic-gate 			sys_gid = gp->gr_gid;
12587c478bd9Sstevel@tonic-gate 		} else {
12597c478bd9Sstevel@tonic-gate 			if (bam_verbose)
12607c478bd9Sstevel@tonic-gate 				bam_error(CANT_FIND_GROUP,
12617c478bd9Sstevel@tonic-gate 				    DEFAULT_DEV_GROUP, DEFAULT_DEV_GID);
12627c478bd9Sstevel@tonic-gate 			sys_gid = (gid_t)DEFAULT_DEV_GID;
12637c478bd9Sstevel@tonic-gate 		}
12647c478bd9Sstevel@tonic-gate 	}
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	(void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp);
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	/* Truncate tmpfile first */
12697c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "w");
12707c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
12717c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
12727c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12737c478bd9Sstevel@tonic-gate 	}
12747c478bd9Sstevel@tonic-gate 	ret = fclose(fp);
12757c478bd9Sstevel@tonic-gate 	if (ret == EOF) {
12767c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
12777c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12787c478bd9Sstevel@tonic-gate 	}
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	/* Now open it in append mode */
12817c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "a");
12827c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
12837c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
12847c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12857c478bd9Sstevel@tonic-gate 	}
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	for (; start; start = start->next) {
12887c478bd9Sstevel@tonic-gate 		ret = s_fputs(start->line, fp);
12897c478bd9Sstevel@tonic-gate 		if (ret == EOF) {
12907c478bd9Sstevel@tonic-gate 			bam_error(WRITE_FAIL, tmpfile, strerror(errno));
12917c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
12927c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
12937c478bd9Sstevel@tonic-gate 		}
12947c478bd9Sstevel@tonic-gate 	}
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 	ret = fclose(fp);
12977c478bd9Sstevel@tonic-gate 	if (ret == EOF) {
12987c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
12997c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13007c478bd9Sstevel@tonic-gate 	}
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 	/*
1303de531be4Sjg 	 * Set up desired attributes.  Ignore failures on filesystems
1304de531be4Sjg 	 * not supporting these operations - pcfs reports unsupported
1305de531be4Sjg 	 * operations as EINVAL.
13067c478bd9Sstevel@tonic-gate 	 */
13077c478bd9Sstevel@tonic-gate 	ret = chmod(tmpfile, mode);
1308de531be4Sjg 	if (ret == -1 &&
1309de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
13107c478bd9Sstevel@tonic-gate 		bam_error(CHMOD_FAIL, tmpfile, strerror(errno));
13117c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13127c478bd9Sstevel@tonic-gate 	}
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	ret = chown(tmpfile, root_uid, sys_gid);
1315de531be4Sjg 	if (ret == -1 &&
1316de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
13177c478bd9Sstevel@tonic-gate 		bam_error(CHOWN_FAIL, tmpfile, strerror(errno));
13187c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13197c478bd9Sstevel@tonic-gate 	}
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	/*
13237c478bd9Sstevel@tonic-gate 	 * Do an atomic rename
13247c478bd9Sstevel@tonic-gate 	 */
13257c478bd9Sstevel@tonic-gate 	ret = rename(tmpfile, path);
13267c478bd9Sstevel@tonic-gate 	if (ret != 0) {
13277c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path, strerror(errno));
13287c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13297c478bd9Sstevel@tonic-gate 	}
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
13327c478bd9Sstevel@tonic-gate }
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate /*
13357c478bd9Sstevel@tonic-gate  * This function should always return 0 - since we want
13367c478bd9Sstevel@tonic-gate  * to create stat data for *all* files in the list.
13377c478bd9Sstevel@tonic-gate  */
13387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13397c478bd9Sstevel@tonic-gate static int
13407c478bd9Sstevel@tonic-gate cmpstat(
13417c478bd9Sstevel@tonic-gate 	const char *file,
13427c478bd9Sstevel@tonic-gate 	const struct stat *stat,
13437c478bd9Sstevel@tonic-gate 	int flags,
13447c478bd9Sstevel@tonic-gate 	struct FTW *ftw)
13457c478bd9Sstevel@tonic-gate {
13467c478bd9Sstevel@tonic-gate 	uint_t sz;
13477c478bd9Sstevel@tonic-gate 	uint64_t *value;
13487c478bd9Sstevel@tonic-gate 	uint64_t filestat[2];
13497c478bd9Sstevel@tonic-gate 	int error;
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	/*
13527c478bd9Sstevel@tonic-gate 	 * We only want regular files
13537c478bd9Sstevel@tonic-gate 	 */
13547c478bd9Sstevel@tonic-gate 	if (!S_ISREG(stat->st_mode))
13557c478bd9Sstevel@tonic-gate 		return (0);
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 	/*
13587c478bd9Sstevel@tonic-gate 	 * new_nvlp may be NULL if there were errors earlier
13597c478bd9Sstevel@tonic-gate 	 * but this is not fatal to update determination.
13607c478bd9Sstevel@tonic-gate 	 */
13617c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp) {
13627c478bd9Sstevel@tonic-gate 		filestat[0] = stat->st_size;
13637c478bd9Sstevel@tonic-gate 		filestat[1] = stat->st_mtime;
13647c478bd9Sstevel@tonic-gate 		error = nvlist_add_uint64_array(walk_arg.new_nvlp,
13657c478bd9Sstevel@tonic-gate 		    file + bam_rootlen, filestat, 2);
13667c478bd9Sstevel@tonic-gate 		if (error)
13677c478bd9Sstevel@tonic-gate 			bam_error(NVADD_FAIL, file, strerror(error));
13687c478bd9Sstevel@tonic-gate 	}
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	/*
13717c478bd9Sstevel@tonic-gate 	 * The remaining steps are only required if we haven't made a
13727c478bd9Sstevel@tonic-gate 	 * decision about update or if we are checking (-n)
13737c478bd9Sstevel@tonic-gate 	 */
13747c478bd9Sstevel@tonic-gate 	if (walk_arg.need_update && !bam_check)
13757c478bd9Sstevel@tonic-gate 		return (0);
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	/*
13787c478bd9Sstevel@tonic-gate 	 * If we are invoked as part of system/filesyste/boot-archive
13797c478bd9Sstevel@tonic-gate 	 * SMF service, ignore amd64 modules unless we are booted amd64.
13807c478bd9Sstevel@tonic-gate 	 */
13818c1b6884Sszhou 	if (bam_smf_check && !is_amd64() && strstr(file, "/amd64/") != 0)
13827c478bd9Sstevel@tonic-gate 		return (0);
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	/*
13857c478bd9Sstevel@tonic-gate 	 * We need an update if file doesn't exist in old archive
13867c478bd9Sstevel@tonic-gate 	 */
13877c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp == NULL ||
13887c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint64_array(walk_arg.old_nvlp,
13897c478bd9Sstevel@tonic-gate 	    file + bam_rootlen, &value, &sz) != 0) {
13907c478bd9Sstevel@tonic-gate 		if (bam_smf_check)	/* ignore new during smf check */
13917c478bd9Sstevel@tonic-gate 			return (0);
13927c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
13937c478bd9Sstevel@tonic-gate 		if (bam_verbose)
13947c478bd9Sstevel@tonic-gate 			bam_print(PARSEABLE_NEW_FILE, file);
13957c478bd9Sstevel@tonic-gate 		return (0);
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	/*
13997c478bd9Sstevel@tonic-gate 	 * File exists in old archive. Check if file has changed
14007c478bd9Sstevel@tonic-gate 	 */
14017c478bd9Sstevel@tonic-gate 	assert(sz == 2);
14027c478bd9Sstevel@tonic-gate 	bcopy(value, filestat, sizeof (filestat));
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	if (filestat[0] != stat->st_size ||
14057c478bd9Sstevel@tonic-gate 	    filestat[1] != stat->st_mtime) {
14067c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
14077c478bd9Sstevel@tonic-gate 		if (bam_verbose)
14087c478bd9Sstevel@tonic-gate 			if (bam_smf_check)
14097c478bd9Sstevel@tonic-gate 				bam_print("    %s\n", file);
14107c478bd9Sstevel@tonic-gate 			else
14117c478bd9Sstevel@tonic-gate 				bam_print(PARSEABLE_OUT_DATE, file);
14127c478bd9Sstevel@tonic-gate 	}
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	return (0);
14157c478bd9Sstevel@tonic-gate }
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate /*
14187c478bd9Sstevel@tonic-gate  * Check flags and presence of required files.
14197c478bd9Sstevel@tonic-gate  * The force flag and/or absence of files should
14207c478bd9Sstevel@tonic-gate  * trigger an update.
14217c478bd9Sstevel@tonic-gate  * Suppress stdout output if check (-n) option is set
14227c478bd9Sstevel@tonic-gate  * (as -n should only produce parseable output.)
14237c478bd9Sstevel@tonic-gate  */
14247c478bd9Sstevel@tonic-gate static void
14257c478bd9Sstevel@tonic-gate check_flags_and_files(char *root)
14267c478bd9Sstevel@tonic-gate {
14277c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
14287c478bd9Sstevel@tonic-gate 	struct stat sb;
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 	/*
14317c478bd9Sstevel@tonic-gate 	 * if force, create archive unconditionally
14327c478bd9Sstevel@tonic-gate 	 */
14337c478bd9Sstevel@tonic-gate 	if (bam_force) {
14347c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
14357c478bd9Sstevel@tonic-gate 		if (bam_verbose && !bam_check)
14367c478bd9Sstevel@tonic-gate 			bam_print(UPDATE_FORCE);
14377c478bd9Sstevel@tonic-gate 		return;
14387c478bd9Sstevel@tonic-gate 	}
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	/*
14417c478bd9Sstevel@tonic-gate 	 * If archive is missing, create archive
14427c478bd9Sstevel@tonic-gate 	 */
14437c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE);
14447c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
14457c478bd9Sstevel@tonic-gate 		if (bam_verbose && !bam_check)
14467c478bd9Sstevel@tonic-gate 			bam_print(UPDATE_ARCH_MISS, path);
14477c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
14487c478bd9Sstevel@tonic-gate 		return;
14497c478bd9Sstevel@tonic-gate 	}
14507c478bd9Sstevel@tonic-gate }
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate static error_t
14537c478bd9Sstevel@tonic-gate read_one_list(char *root, filelist_t  *flistp, char *filelist)
14547c478bd9Sstevel@tonic-gate {
14557c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
14567c478bd9Sstevel@tonic-gate 	FILE *fp;
14577c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE];
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, filelist);
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	fp = fopen(path, "r");
14627c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
14637c478bd9Sstevel@tonic-gate 		if (bam_debug)
14647c478bd9Sstevel@tonic-gate 			bam_error(FLIST_FAIL, path, strerror(errno));
14657c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
14667c478bd9Sstevel@tonic-gate 	}
14677c478bd9Sstevel@tonic-gate 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
1468b610f78eSvikram 		/* skip blank lines */
1469b610f78eSvikram 		if (strspn(buf, " \t") == strlen(buf))
1470b610f78eSvikram 			continue;
14717c478bd9Sstevel@tonic-gate 		append_to_flist(flistp, buf);
14727c478bd9Sstevel@tonic-gate 	}
14737c478bd9Sstevel@tonic-gate 	if (fclose(fp) != 0) {
14747c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, path, strerror(errno));
14757c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
14767c478bd9Sstevel@tonic-gate 	}
14777c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
14787c478bd9Sstevel@tonic-gate }
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate static error_t
14817c478bd9Sstevel@tonic-gate read_list(char *root, filelist_t  *flistp)
14827c478bd9Sstevel@tonic-gate {
14837c478bd9Sstevel@tonic-gate 	int rval;
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	/*
14887c478bd9Sstevel@tonic-gate 	 * Read current lists of files - only the first is mandatory
14897c478bd9Sstevel@tonic-gate 	 */
14907c478bd9Sstevel@tonic-gate 	rval = read_one_list(root, flistp, BOOT_FILE_LIST);
14917c478bd9Sstevel@tonic-gate 	if (rval != BAM_SUCCESS)
14927c478bd9Sstevel@tonic-gate 		return (rval);
14937c478bd9Sstevel@tonic-gate 	(void) read_one_list(root, flistp, ETC_FILE_LIST);
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL) {
14967c478bd9Sstevel@tonic-gate 		bam_error(NO_FLIST);
14977c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
14987c478bd9Sstevel@tonic-gate 	}
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
15017c478bd9Sstevel@tonic-gate }
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate static void
15047c478bd9Sstevel@tonic-gate getoldstat(char *root)
15057c478bd9Sstevel@tonic-gate {
15067c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
15077c478bd9Sstevel@tonic-gate 	int fd, error;
15087c478bd9Sstevel@tonic-gate 	struct stat sb;
15097c478bd9Sstevel@tonic-gate 	char *ostat;
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
15127c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDONLY);
15137c478bd9Sstevel@tonic-gate 	if (fd == -1) {
15147c478bd9Sstevel@tonic-gate 		if (bam_verbose)
15157c478bd9Sstevel@tonic-gate 			bam_print(OPEN_FAIL, path, strerror(errno));
15167c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
15177c478bd9Sstevel@tonic-gate 		return;
15187c478bd9Sstevel@tonic-gate 	}
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 	if (fstat(fd, &sb) != 0) {
15217c478bd9Sstevel@tonic-gate 		bam_error(STAT_FAIL, path, strerror(errno));
15227c478bd9Sstevel@tonic-gate 		(void) close(fd);
15237c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
15247c478bd9Sstevel@tonic-gate 		return;
15257c478bd9Sstevel@tonic-gate 	}
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	ostat = s_calloc(1, sb.st_size);
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 	if (read(fd, ostat, sb.st_size) != sb.st_size) {
15307c478bd9Sstevel@tonic-gate 		bam_error(READ_FAIL, path, strerror(errno));
15317c478bd9Sstevel@tonic-gate 		(void) close(fd);
15327c478bd9Sstevel@tonic-gate 		free(ostat);
15337c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
15347c478bd9Sstevel@tonic-gate 		return;
15357c478bd9Sstevel@tonic-gate 	}
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	(void) close(fd);
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
15407c478bd9Sstevel@tonic-gate 	error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0);
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 	free(ostat);
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	if (error) {
15457c478bd9Sstevel@tonic-gate 		bam_error(UNPACK_FAIL, path, strerror(error));
15467c478bd9Sstevel@tonic-gate 		walk_arg.old_nvlp = NULL;
15477c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
15487c478bd9Sstevel@tonic-gate 		return;
15497c478bd9Sstevel@tonic-gate 	}
15507c478bd9Sstevel@tonic-gate }
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate static void
15537c478bd9Sstevel@tonic-gate create_newstat(void)
15547c478bd9Sstevel@tonic-gate {
15557c478bd9Sstevel@tonic-gate 	int error;
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0);
15587c478bd9Sstevel@tonic-gate 	if (error) {
15597c478bd9Sstevel@tonic-gate 		/*
15607c478bd9Sstevel@tonic-gate 		 * Not fatal - we can still create archive
15617c478bd9Sstevel@tonic-gate 		 */
15627c478bd9Sstevel@tonic-gate 		walk_arg.new_nvlp = NULL;
15637c478bd9Sstevel@tonic-gate 		bam_error(NVALLOC_FAIL, strerror(error));
15647c478bd9Sstevel@tonic-gate 	}
15657c478bd9Sstevel@tonic-gate }
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate static void
15687c478bd9Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp)
15697c478bd9Sstevel@tonic-gate {
15707c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
15717c478bd9Sstevel@tonic-gate 	line_t *lp;
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
15747c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, lp->line);
15757c478bd9Sstevel@tonic-gate 		/* XXX shouldn't we use FTW_MOUNT ? */
15767c478bd9Sstevel@tonic-gate 		if (nftw(path, cmpstat, 20, 0) == -1) {
15777c478bd9Sstevel@tonic-gate 			/*
15787c478bd9Sstevel@tonic-gate 			 * Some files may not exist.
15797c478bd9Sstevel@tonic-gate 			 * For example: etc/rtc_config on a x86 diskless system
15807c478bd9Sstevel@tonic-gate 			 * Emit verbose message only
15817c478bd9Sstevel@tonic-gate 			 */
15827c478bd9Sstevel@tonic-gate 			if (bam_verbose)
15837c478bd9Sstevel@tonic-gate 				bam_print(NFTW_FAIL, path, strerror(errno));
15847c478bd9Sstevel@tonic-gate 		}
15857c478bd9Sstevel@tonic-gate 	}
15867c478bd9Sstevel@tonic-gate }
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate static void
15897c478bd9Sstevel@tonic-gate savenew(char *root)
15907c478bd9Sstevel@tonic-gate {
15917c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
15927c478bd9Sstevel@tonic-gate 	char path2[PATH_MAX];
15937c478bd9Sstevel@tonic-gate 	size_t sz;
15947c478bd9Sstevel@tonic-gate 	char *nstat;
15957c478bd9Sstevel@tonic-gate 	int fd, wrote, error;
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	nstat = NULL;
15987c478bd9Sstevel@tonic-gate 	sz = 0;
15997c478bd9Sstevel@tonic-gate 	error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz,
16007c478bd9Sstevel@tonic-gate 	    NV_ENCODE_XDR, 0);
16017c478bd9Sstevel@tonic-gate 	if (error) {
16027c478bd9Sstevel@tonic-gate 		bam_error(PACK_FAIL, strerror(error));
16037c478bd9Sstevel@tonic-gate 		return;
16047c478bd9Sstevel@tonic-gate 	}
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP);
16077c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE);
16087c478bd9Sstevel@tonic-gate 	if (fd == -1) {
16097c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, path, strerror(errno));
16107c478bd9Sstevel@tonic-gate 		free(nstat);
16117c478bd9Sstevel@tonic-gate 		return;
16127c478bd9Sstevel@tonic-gate 	}
16137c478bd9Sstevel@tonic-gate 	wrote = write(fd, nstat, sz);
16147c478bd9Sstevel@tonic-gate 	if (wrote != sz) {
16157c478bd9Sstevel@tonic-gate 		bam_error(WRITE_FAIL, path, strerror(errno));
16167c478bd9Sstevel@tonic-gate 		(void) close(fd);
16177c478bd9Sstevel@tonic-gate 		free(nstat);
16187c478bd9Sstevel@tonic-gate 		return;
16197c478bd9Sstevel@tonic-gate 	}
16207c478bd9Sstevel@tonic-gate 	(void) close(fd);
16217c478bd9Sstevel@tonic-gate 	free(nstat);
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	(void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT);
16247c478bd9Sstevel@tonic-gate 	if (rename(path, path2) != 0) {
16257c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path2, strerror(errno));
16267c478bd9Sstevel@tonic-gate 	}
16277c478bd9Sstevel@tonic-gate }
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate static void
16307c478bd9Sstevel@tonic-gate clear_walk_args(void)
16317c478bd9Sstevel@tonic-gate {
16327c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp)
16337c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.old_nvlp);
16347c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp)
16357c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.new_nvlp);
16367c478bd9Sstevel@tonic-gate 	walk_arg.need_update = 0;
16377c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
16387c478bd9Sstevel@tonic-gate 	walk_arg.new_nvlp = NULL;
16397c478bd9Sstevel@tonic-gate }
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate /*
16427c478bd9Sstevel@tonic-gate  * Returns:
16437c478bd9Sstevel@tonic-gate  *	0 - no update necessary
16447c478bd9Sstevel@tonic-gate  *	1 - update required.
16457c478bd9Sstevel@tonic-gate  *	BAM_ERROR (-1) - An error occurred
16467c478bd9Sstevel@tonic-gate  *
16477c478bd9Sstevel@tonic-gate  * Special handling for check (-n):
16487c478bd9Sstevel@tonic-gate  * ================================
16497c478bd9Sstevel@tonic-gate  * The check (-n) option produces parseable output.
16507c478bd9Sstevel@tonic-gate  * To do this, we suppress all stdout messages unrelated
16517c478bd9Sstevel@tonic-gate  * to out of sync files.
16527c478bd9Sstevel@tonic-gate  * All stderr messages are still printed though.
16537c478bd9Sstevel@tonic-gate  *
16547c478bd9Sstevel@tonic-gate  */
16557c478bd9Sstevel@tonic-gate static int
16567c478bd9Sstevel@tonic-gate update_required(char *root)
16577c478bd9Sstevel@tonic-gate {
16587c478bd9Sstevel@tonic-gate 	struct stat sb;
16597c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
16607c478bd9Sstevel@tonic-gate 	filelist_t flist;
16617c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
16627c478bd9Sstevel@tonic-gate 	int need_update;
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	walk_arg.need_update = 0;
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 	/*
16697c478bd9Sstevel@tonic-gate 	 * Without consulting stat data, check if we need update
16707c478bd9Sstevel@tonic-gate 	 */
16717c478bd9Sstevel@tonic-gate 	check_flags_and_files(root);
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	/*
16747c478bd9Sstevel@tonic-gate 	 * In certain deployment scenarios, filestat may not
16757c478bd9Sstevel@tonic-gate 	 * exist. Ignore it during boot-archive SMF check.
16767c478bd9Sstevel@tonic-gate 	 */
16777c478bd9Sstevel@tonic-gate 	if (bam_smf_check) {
16787c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
16797c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != 0)
16807c478bd9Sstevel@tonic-gate 			return (0);
16817c478bd9Sstevel@tonic-gate 	}
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 	/*
16847c478bd9Sstevel@tonic-gate 	 * consult stat data only if we haven't made a decision
16857c478bd9Sstevel@tonic-gate 	 * about update. If checking (-n) however, we always
16867c478bd9Sstevel@tonic-gate 	 * need stat data (since we want to compare old and new)
16877c478bd9Sstevel@tonic-gate 	 */
16887c478bd9Sstevel@tonic-gate 	if (!walk_arg.need_update || bam_check)
16897c478bd9Sstevel@tonic-gate 		getoldstat(root);
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 	/*
16927c478bd9Sstevel@tonic-gate 	 * read list of files
16937c478bd9Sstevel@tonic-gate 	 */
16947c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
16957c478bd9Sstevel@tonic-gate 		clear_walk_args();
16967c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
16977c478bd9Sstevel@tonic-gate 	}
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	/*
17027c478bd9Sstevel@tonic-gate 	 * At this point either the update is required
17037c478bd9Sstevel@tonic-gate 	 * or the decision is pending. In either case
17047c478bd9Sstevel@tonic-gate 	 * we need to create new stat nvlist
17057c478bd9Sstevel@tonic-gate 	 */
17067c478bd9Sstevel@tonic-gate 	create_newstat();
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	/*
17097c478bd9Sstevel@tonic-gate 	 * This walk does 2 things:
17107c478bd9Sstevel@tonic-gate 	 *  	- gets new stat data for every file
17117c478bd9Sstevel@tonic-gate 	 *	- (optional) compare old and new stat data
17127c478bd9Sstevel@tonic-gate 	 */
17137c478bd9Sstevel@tonic-gate 	walk_list(root, &flist);
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 	/* done with the file list */
17167c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 	/*
17197c478bd9Sstevel@tonic-gate 	 * if we didn't succeed in  creating new stat data above
17207c478bd9Sstevel@tonic-gate 	 * just return result of update check so that archive is built.
17217c478bd9Sstevel@tonic-gate 	 */
17227c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp == NULL) {
17237c478bd9Sstevel@tonic-gate 		bam_error(NO_NEW_STAT);
17247c478bd9Sstevel@tonic-gate 		need_update = walk_arg.need_update;
17257c478bd9Sstevel@tonic-gate 		clear_walk_args();
17267c478bd9Sstevel@tonic-gate 		return (need_update ? 1 : 0);
17277c478bd9Sstevel@tonic-gate 	}
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	/*
17317c478bd9Sstevel@tonic-gate 	 * If no update required, discard newstat
17327c478bd9Sstevel@tonic-gate 	 */
17337c478bd9Sstevel@tonic-gate 	if (!walk_arg.need_update) {
17347c478bd9Sstevel@tonic-gate 		clear_walk_args();
17357c478bd9Sstevel@tonic-gate 		return (0);
17367c478bd9Sstevel@tonic-gate 	}
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 	/*
17397c478bd9Sstevel@tonic-gate 	 * At this point we need an update - so save new stat data
17407c478bd9Sstevel@tonic-gate 	 * However, if only checking (-n), don't save new stat data.
17417c478bd9Sstevel@tonic-gate 	 */
17427c478bd9Sstevel@tonic-gate 	if (!bam_check)
17437c478bd9Sstevel@tonic-gate 		savenew(root);
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 	clear_walk_args();
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 	return (1);
17487c478bd9Sstevel@tonic-gate }
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate static error_t
17517c478bd9Sstevel@tonic-gate create_ramdisk(char *root)
17527c478bd9Sstevel@tonic-gate {
17537c478bd9Sstevel@tonic-gate 	char *cmdline, path[PATH_MAX];
17547c478bd9Sstevel@tonic-gate 	size_t len;
17557c478bd9Sstevel@tonic-gate 	struct stat sb;
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	/*
17587c478bd9Sstevel@tonic-gate 	 * Setup command args for create_ramdisk.ksh
17597c478bd9Sstevel@tonic-gate 	 */
17607c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, CREATE_RAMDISK);
17617c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
17627c478bd9Sstevel@tonic-gate 		bam_error(ARCH_EXEC_MISS, path, strerror(errno));
17637c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
17647c478bd9Sstevel@tonic-gate 	}
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	len = strlen(path) + strlen(root) + 10;	/* room for space + -R */
17677c478bd9Sstevel@tonic-gate 	cmdline = s_calloc(1, len);
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	if (strlen(root) > 1) {
17707c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s -R %s", path, root);
17717c478bd9Sstevel@tonic-gate 		/* chop off / at the end */
17727c478bd9Sstevel@tonic-gate 		cmdline[strlen(cmdline) - 1] = '\0';
17737c478bd9Sstevel@tonic-gate 	} else
17747c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s", path);
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	if (exec_cmd(cmdline, NULL, 0) != 0) {
17777c478bd9Sstevel@tonic-gate 		bam_error(ARCHIVE_FAIL, cmdline);
17787c478bd9Sstevel@tonic-gate 		free(cmdline);
17797c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
17807c478bd9Sstevel@tonic-gate 	}
17817c478bd9Sstevel@tonic-gate 	free(cmdline);
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate 	/*
17847c478bd9Sstevel@tonic-gate 	 * Verify that the archive has been created
17857c478bd9Sstevel@tonic-gate 	 */
17867c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE);
17877c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
17887c478bd9Sstevel@tonic-gate 		bam_error(ARCHIVE_NOT_CREATED, path);
17897c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
17907c478bd9Sstevel@tonic-gate 	}
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
17937c478bd9Sstevel@tonic-gate }
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate /*
17967c478bd9Sstevel@tonic-gate  * Checks if target filesystem is on a ramdisk
17977c478bd9Sstevel@tonic-gate  * 1 - is miniroot
17987c478bd9Sstevel@tonic-gate  * 0 - is not
17997c478bd9Sstevel@tonic-gate  * When in doubt assume it is not a ramdisk.
18007c478bd9Sstevel@tonic-gate  */
18017c478bd9Sstevel@tonic-gate static int
18027c478bd9Sstevel@tonic-gate is_ramdisk(char *root)
18037c478bd9Sstevel@tonic-gate {
18047c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
18057c478bd9Sstevel@tonic-gate 	FILE *fp;
18067c478bd9Sstevel@tonic-gate 	int found;
1807b610f78eSvikram 	char mntpt[PATH_MAX];
1808b610f78eSvikram 	char *cp;
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	/*
18117c478bd9Sstevel@tonic-gate 	 * There are 3 situations where creating archive is
18127c478bd9Sstevel@tonic-gate 	 * of dubious value:
1813b610f78eSvikram 	 *	- create boot_archive on a lofi-mounted boot_archive
18147c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk which is the root filesystem
18157c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk mounted somewhere else
18167c478bd9Sstevel@tonic-gate 	 * The first is not easy to detect and checking for it is not
18177c478bd9Sstevel@tonic-gate 	 * worth it.
18187c478bd9Sstevel@tonic-gate 	 * The other two conditions are handled here
18197c478bd9Sstevel@tonic-gate 	 */
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
18227c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
18237c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
18247c478bd9Sstevel@tonic-gate 		return (0);
18257c478bd9Sstevel@tonic-gate 	}
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
18287c478bd9Sstevel@tonic-gate 
1829b610f78eSvikram 	/*
1830b610f78eSvikram 	 * Remove any trailing / from the mount point
1831b610f78eSvikram 	 */
1832b610f78eSvikram 	(void) strlcpy(mntpt, root, sizeof (mntpt));
1833b610f78eSvikram 	if (strcmp(root, "/") != 0) {
1834b610f78eSvikram 		cp = mntpt + strlen(mntpt) - 1;
1835b610f78eSvikram 		if (*cp == '/')
1836b610f78eSvikram 			*cp = '\0';
1837b610f78eSvikram 	}
18387c478bd9Sstevel@tonic-gate 	found = 0;
18397c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
1840b610f78eSvikram 		if (strcmp(mnt.mnt_mountp, mntpt) == 0) {
18417c478bd9Sstevel@tonic-gate 			found = 1;
18427c478bd9Sstevel@tonic-gate 			break;
18437c478bd9Sstevel@tonic-gate 		}
18447c478bd9Sstevel@tonic-gate 	}
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 	if (!found) {
18477c478bd9Sstevel@tonic-gate 		if (bam_verbose)
1848b610f78eSvikram 			bam_error(NOT_IN_MNTTAB, mntpt);
18497c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
18507c478bd9Sstevel@tonic-gate 		return (0);
18517c478bd9Sstevel@tonic-gate 	}
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) {
18547c478bd9Sstevel@tonic-gate 		if (bam_verbose)
18557c478bd9Sstevel@tonic-gate 			bam_error(IS_RAMDISK, bam_root);
18567c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
18577c478bd9Sstevel@tonic-gate 		return (1);
18587c478bd9Sstevel@tonic-gate 	}
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 	return (0);
18637c478bd9Sstevel@tonic-gate }
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate static int
18667c478bd9Sstevel@tonic-gate is_newboot(char *root)
18677c478bd9Sstevel@tonic-gate {
18687c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
18697c478bd9Sstevel@tonic-gate 	struct stat sb;
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	/*
18727c478bd9Sstevel@tonic-gate 	 * We can't boot without MULTI_BOOT
18737c478bd9Sstevel@tonic-gate 	 */
18747c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, MULTI_BOOT);
18757c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
18767c478bd9Sstevel@tonic-gate 		if (bam_verbose)
18777c478bd9Sstevel@tonic-gate 			bam_print(FILE_MISS, path);
18787c478bd9Sstevel@tonic-gate 		return (0);
18797c478bd9Sstevel@tonic-gate 	}
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 	/*
18827c478bd9Sstevel@tonic-gate 	 * We can't generate archive without GRUB_DIR
18837c478bd9Sstevel@tonic-gate 	 */
18847c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, GRUB_DIR);
18857c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
18867c478bd9Sstevel@tonic-gate 		if (bam_verbose)
18877c478bd9Sstevel@tonic-gate 			bam_print(DIR_MISS, path);
18887c478bd9Sstevel@tonic-gate 		return (0);
18897c478bd9Sstevel@tonic-gate 	}
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	return (1);
18927c478bd9Sstevel@tonic-gate }
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate static int
18957c478bd9Sstevel@tonic-gate is_readonly(char *root)
18967c478bd9Sstevel@tonic-gate {
18977c478bd9Sstevel@tonic-gate 	struct statvfs vfs;
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 	/*
19007c478bd9Sstevel@tonic-gate 	 * Check for RDONLY filesystem
19017c478bd9Sstevel@tonic-gate 	 * When in doubt assume it is not readonly
19027c478bd9Sstevel@tonic-gate 	 */
19037c478bd9Sstevel@tonic-gate 	if (statvfs(root, &vfs) != 0) {
19047c478bd9Sstevel@tonic-gate 		if (bam_verbose)
19057c478bd9Sstevel@tonic-gate 			bam_error(STATVFS_FAIL, root, strerror(errno));
19067c478bd9Sstevel@tonic-gate 		return (0);
19077c478bd9Sstevel@tonic-gate 	}
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	if (vfs.f_flag & ST_RDONLY) {
19107c478bd9Sstevel@tonic-gate 		return (1);
19117c478bd9Sstevel@tonic-gate 	}
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate 	return (0);
19147c478bd9Sstevel@tonic-gate }
19157c478bd9Sstevel@tonic-gate 
19167c478bd9Sstevel@tonic-gate static error_t
19177c478bd9Sstevel@tonic-gate update_archive(char *root, char *opt)
19187c478bd9Sstevel@tonic-gate {
19197c478bd9Sstevel@tonic-gate 	error_t ret;
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	assert(root);
19227c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 	/*
1925b610f78eSvikram 	 * root must belong to a GRUB boot OS,
19267c478bd9Sstevel@tonic-gate 	 * don't care on sparc except for diskless clients
19277c478bd9Sstevel@tonic-gate 	 */
19287c478bd9Sstevel@tonic-gate 	if (!is_newboot(root)) {
1929b610f78eSvikram 		/*
1930b610f78eSvikram 		 * Emit message only if not in context of update_all.
1931b610f78eSvikram 		 * If in update_all, emit only if verbose flag is set.
1932b610f78eSvikram 		 */
1933b610f78eSvikram 		if (!bam_update_all || bam_verbose)
1934b610f78eSvikram 			bam_print(NOT_GRUB_BOOT, root);
19357c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
19367c478bd9Sstevel@tonic-gate 	}
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 	/*
19398c1b6884Sszhou 	 * If smf check is requested when / is writable (can happen
19408c1b6884Sszhou 	 * on first reboot following an upgrade because service
19418c1b6884Sszhou 	 * dependency is messed up), skip the check.
19428c1b6884Sszhou 	 */
19438c1b6884Sszhou 	if (bam_smf_check && !bam_root_readonly)
19448c1b6884Sszhou 		return (BAM_SUCCESS);
19458c1b6884Sszhou 
19468c1b6884Sszhou 	/*
19478c1b6884Sszhou 	 * root must be writable. This check applies to alternate
19488c1b6884Sszhou 	 * root (-R option); bam_root_readonly applies to '/' only.
19497c478bd9Sstevel@tonic-gate 	 * Note: statvfs() does not always report the truth
19507c478bd9Sstevel@tonic-gate 	 */
195161cb17bdSsetje 	if (!bam_smf_check && !bam_check && is_readonly(root)) {
19528c1b6884Sszhou 		if (bam_verbose)
19537c478bd9Sstevel@tonic-gate 			bam_print(RDONLY_FS, root);
19547c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
19557c478bd9Sstevel@tonic-gate 	}
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 	/*
19587c478bd9Sstevel@tonic-gate 	 * Don't generate archive on ramdisk
19597c478bd9Sstevel@tonic-gate 	 */
19607c478bd9Sstevel@tonic-gate 	if (is_ramdisk(root)) {
19617c478bd9Sstevel@tonic-gate 		if (bam_verbose)
19627c478bd9Sstevel@tonic-gate 			bam_print(SKIP_RAMDISK);
19637c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
19647c478bd9Sstevel@tonic-gate 	}
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 	/*
19677c478bd9Sstevel@tonic-gate 	 * Now check if updated is really needed
19687c478bd9Sstevel@tonic-gate 	 */
19697c478bd9Sstevel@tonic-gate 	ret = update_required(root);
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	/*
19727c478bd9Sstevel@tonic-gate 	 * The check command (-n) is *not* a dry run
19737c478bd9Sstevel@tonic-gate 	 * It only checks if the archive is in sync.
19747c478bd9Sstevel@tonic-gate 	 */
19757c478bd9Sstevel@tonic-gate 	if (bam_check) {
19767c478bd9Sstevel@tonic-gate 		bam_exit((ret != 0) ? 1 : 0);
19777c478bd9Sstevel@tonic-gate 	}
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 	if (ret == 1) {
19807c478bd9Sstevel@tonic-gate 		/* create the ramdisk */
19817c478bd9Sstevel@tonic-gate 		ret = create_ramdisk(root);
19827c478bd9Sstevel@tonic-gate 	}
19837c478bd9Sstevel@tonic-gate 	return (ret);
19847c478bd9Sstevel@tonic-gate }
19857c478bd9Sstevel@tonic-gate 
1986b610f78eSvikram static void
1987*fbac2b2bSvikram update_fdisk(void)
1988*fbac2b2bSvikram {
1989*fbac2b2bSvikram 	struct stat sb;
1990*fbac2b2bSvikram 	char cmd[PATH_MAX];
1991*fbac2b2bSvikram 	int ret1, ret2;
1992*fbac2b2bSvikram 
1993*fbac2b2bSvikram 	assert(stat(GRUB_fdisk, &sb) == 0);
1994*fbac2b2bSvikram 	assert(stat(GRUB_fdisk_target, &sb) == 0);
1995*fbac2b2bSvikram 
1996*fbac2b2bSvikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/fdisk -F %s `/bin/cat %s`",
1997*fbac2b2bSvikram 	    GRUB_fdisk, GRUB_fdisk_target);
1998*fbac2b2bSvikram 
1999*fbac2b2bSvikram 	bam_print(UPDATING_FDISK);
2000*fbac2b2bSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2001*fbac2b2bSvikram 		bam_error(FDISK_UPDATE_FAILED);
2002*fbac2b2bSvikram 	}
2003*fbac2b2bSvikram 
2004*fbac2b2bSvikram 	/*
2005*fbac2b2bSvikram 	 * We are done, remove the files.
2006*fbac2b2bSvikram 	 */
2007*fbac2b2bSvikram 	ret1 = unlink(GRUB_fdisk);
2008*fbac2b2bSvikram 	ret2 = unlink(GRUB_fdisk_target);
2009*fbac2b2bSvikram 	if (ret1 != 0 || ret2 != 0) {
2010*fbac2b2bSvikram 		bam_error(FILE_REMOVE_FAILED, GRUB_fdisk, GRUB_fdisk_target);
2011*fbac2b2bSvikram 	}
2012*fbac2b2bSvikram }
2013*fbac2b2bSvikram 
2014*fbac2b2bSvikram static void
2015b610f78eSvikram restore_grub_slice(void)
2016b610f78eSvikram {
2017b610f78eSvikram 	struct stat sb;
2018b610f78eSvikram 	char *mntpt, *physlice;
2019b610f78eSvikram 	int mnted;	/* set if we did a mount */
2020b610f78eSvikram 	char menupath[PATH_MAX], cmd[PATH_MAX];
2021b610f78eSvikram 
2022b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
2023b610f78eSvikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
2024b610f78eSvikram 		return;
2025b610f78eSvikram 	}
2026b610f78eSvikram 
2027b610f78eSvikram 	/*
2028b610f78eSvikram 	 * If we are doing an luactivate, don't attempt to restore GRUB or else
2029b610f78eSvikram 	 * we may not be able to get to DCA boot environments. Let luactivate
2030b610f78eSvikram 	 * handle GRUB/DCA installation
2031b610f78eSvikram 	 */
2032b610f78eSvikram 	if (stat(LU_ACTIVATE_FILE, &sb) == 0) {
2033b610f78eSvikram 		return;
2034b610f78eSvikram 	}
2035b610f78eSvikram 
2036b610f78eSvikram 	mnted = 0;
2037b610f78eSvikram 	physlice = NULL;
203840541d5dSvikram 	mntpt = mount_grub_slice(&mnted, &physlice, NULL, NULL);
2039b610f78eSvikram 	if (mntpt == NULL) {
2040b610f78eSvikram 		bam_error(CANNOT_RESTORE_GRUB_SLICE);
2041b610f78eSvikram 		return;
2042b610f78eSvikram 	}
2043b610f78eSvikram 
2044b610f78eSvikram 	(void) snprintf(menupath, sizeof (menupath), "%s%s", mntpt, GRUB_MENU);
2045b610f78eSvikram 	if (stat(menupath, &sb) == 0) {
204640541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2047b610f78eSvikram 		return;
2048b610f78eSvikram 	}
2049b610f78eSvikram 
2050b610f78eSvikram 	/*
2051b610f78eSvikram 	 * The menu is missing - we need to do a restore
2052b610f78eSvikram 	 */
2053b610f78eSvikram 	bam_print(RESTORING_GRUB);
2054b610f78eSvikram 
2055b610f78eSvikram 	(void) snprintf(cmd, sizeof (cmd), "%s %s %s %s",
2056b610f78eSvikram 	    INSTALLGRUB, STAGE1, STAGE2, physlice);
2057b610f78eSvikram 
2058b610f78eSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2059b610f78eSvikram 		bam_error(RESTORE_GRUB_FAILED);
206040541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2061b610f78eSvikram 		return;
2062b610f78eSvikram 	}
2063b610f78eSvikram 
2064b610f78eSvikram 	if (stat(GRUB_backup_menu, &sb) != 0) {
2065b610f78eSvikram 		bam_error(MISSING_BACKUP_MENU,
2066b610f78eSvikram 		    GRUB_backup_menu, strerror(errno));
206740541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2068b610f78eSvikram 		return;
2069b610f78eSvikram 	}
2070b610f78eSvikram 
2071b610f78eSvikram 	(void) snprintf(cmd, sizeof (cmd), "/bin/cp %s %s",
2072b610f78eSvikram 	    GRUB_backup_menu, menupath);
2073b610f78eSvikram 
2074b610f78eSvikram 	if (exec_cmd(cmd, NULL, 0) != 0) {
2075b610f78eSvikram 		bam_error(RESTORE_MENU_FAILED, menupath);
207640541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2077b610f78eSvikram 		return;
2078b610f78eSvikram 	}
2079b610f78eSvikram 
2080b610f78eSvikram 	/* Success */
208140541d5dSvikram 	umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2082b610f78eSvikram }
2083b610f78eSvikram 
20847c478bd9Sstevel@tonic-gate static error_t
20857c478bd9Sstevel@tonic-gate update_all(char *root, char *opt)
20867c478bd9Sstevel@tonic-gate {
20877c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
20887c478bd9Sstevel@tonic-gate 	struct stat sb;
20897c478bd9Sstevel@tonic-gate 	FILE *fp;
20907c478bd9Sstevel@tonic-gate 	char multibt[PATH_MAX];
20917c478bd9Sstevel@tonic-gate 	error_t ret = BAM_SUCCESS;
2092*fbac2b2bSvikram 	int ret1, ret2;
20937c478bd9Sstevel@tonic-gate 
209440541d5dSvikram 	assert(root);
20957c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
20967c478bd9Sstevel@tonic-gate 
209740541d5dSvikram 	if (bam_rootlen != 1 || *root != '/') {
209840541d5dSvikram 		elide_trailing_slash(root, multibt, sizeof (multibt));
209940541d5dSvikram 		bam_error(ALT_ROOT_INVALID, multibt);
210040541d5dSvikram 		return (BAM_ERROR);
210140541d5dSvikram 	}
210240541d5dSvikram 
21037c478bd9Sstevel@tonic-gate 	/*
21047c478bd9Sstevel@tonic-gate 	 * First update archive for current root
21057c478bd9Sstevel@tonic-gate 	 */
21067c478bd9Sstevel@tonic-gate 	if (update_archive(root, opt) != BAM_SUCCESS)
21077c478bd9Sstevel@tonic-gate 		ret = BAM_ERROR;
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 	/*
21107c478bd9Sstevel@tonic-gate 	 * Now walk the mount table, performing archive update
21117c478bd9Sstevel@tonic-gate 	 * for all mounted Newboot root filesystems
21127c478bd9Sstevel@tonic-gate 	 */
21137c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
21147c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
21157c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
2116b610f78eSvikram 		ret = BAM_ERROR;
2117b610f78eSvikram 		goto out;
21187c478bd9Sstevel@tonic-gate 	}
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
21237c478bd9Sstevel@tonic-gate 		if (mnt.mnt_special == NULL)
21247c478bd9Sstevel@tonic-gate 			continue;
21257c478bd9Sstevel@tonic-gate 		if (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0)
21267c478bd9Sstevel@tonic-gate 			continue;
21277c478bd9Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, "/") == 0)
21287c478bd9Sstevel@tonic-gate 			continue;
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 		(void) snprintf(multibt, sizeof (multibt), "%s%s",
21317c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp, MULTI_BOOT);
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate 		if (stat(multibt, &sb) == -1)
21347c478bd9Sstevel@tonic-gate 			continue;
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate 		/*
21377c478bd9Sstevel@tonic-gate 		 * We put a trailing slash to be consistent with root = "/"
21387c478bd9Sstevel@tonic-gate 		 * case, such that we don't have to print // in some cases.
21397c478bd9Sstevel@tonic-gate 		 */
21407c478bd9Sstevel@tonic-gate 		(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
21417c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp);
21427c478bd9Sstevel@tonic-gate 		bam_rootlen = strlen(rootbuf);
21437c478bd9Sstevel@tonic-gate 		if (update_archive(rootbuf, opt) != BAM_SUCCESS)
21447c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
21457c478bd9Sstevel@tonic-gate 	}
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
21487c478bd9Sstevel@tonic-gate 
2149b610f78eSvikram out:
2150b610f78eSvikram 	if (stat(GRUB_slice, &sb) == 0) {
2151b610f78eSvikram 		restore_grub_slice();
2152b610f78eSvikram 	}
2153b610f78eSvikram 
2154*fbac2b2bSvikram 	/*
2155*fbac2b2bSvikram 	 * Update fdisk table as we go down. Updating it when
2156*fbac2b2bSvikram 	 * the system is running will confuse biosdev.
2157*fbac2b2bSvikram 	 */
2158*fbac2b2bSvikram 	ret1 = stat(GRUB_fdisk, &sb);
2159*fbac2b2bSvikram 	ret2 = stat(GRUB_fdisk_target, &sb);
2160*fbac2b2bSvikram 	if ((ret1 == 0) && (ret2 == 0)) {
2161*fbac2b2bSvikram 		update_fdisk();
2162*fbac2b2bSvikram 	} else if ((ret1 == 0) ^ (ret2 == 0)) {
2163*fbac2b2bSvikram 		/*
2164*fbac2b2bSvikram 		 * It is an error for one file to be
2165*fbac2b2bSvikram 		 * present and the other absent.
2166*fbac2b2bSvikram 		 * It is normal for both files to be
2167*fbac2b2bSvikram 		 * absent - it indicates that no fdisk
2168*fbac2b2bSvikram 		 * update is required.
2169*fbac2b2bSvikram 		 */
2170*fbac2b2bSvikram 		bam_error(MISSING_FDISK_FILE,
2171*fbac2b2bSvikram 		    ret1 ? GRUB_fdisk : GRUB_fdisk_target);
2172*fbac2b2bSvikram 		ret = BAM_ERROR;
2173*fbac2b2bSvikram 	}
2174*fbac2b2bSvikram 
21757c478bd9Sstevel@tonic-gate 	return (ret);
21767c478bd9Sstevel@tonic-gate }
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate static void
21797c478bd9Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp)
21807c478bd9Sstevel@tonic-gate {
21817c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
21827c478bd9Sstevel@tonic-gate 		mp->start = lp;
21837c478bd9Sstevel@tonic-gate 	} else {
21847c478bd9Sstevel@tonic-gate 		mp->end->next = lp;
21858c1b6884Sszhou 		lp->prev = mp->end;
21867c478bd9Sstevel@tonic-gate 	}
21877c478bd9Sstevel@tonic-gate 	mp->end = lp;
21887c478bd9Sstevel@tonic-gate }
21897c478bd9Sstevel@tonic-gate 
21908c1b6884Sszhou static void
21918c1b6884Sszhou unlink_line(menu_t *mp, line_t *lp)
21928c1b6884Sszhou {
21938c1b6884Sszhou 	/* unlink from list */
21948c1b6884Sszhou 	if (lp->prev)
21958c1b6884Sszhou 		lp->prev->next = lp->next;
21968c1b6884Sszhou 	else
21978c1b6884Sszhou 		mp->start = lp->next;
21988c1b6884Sszhou 	if (lp->next)
21998c1b6884Sszhou 		lp->next->prev = lp->prev;
22008c1b6884Sszhou 	else
22018c1b6884Sszhou 		mp->end = lp->prev;
22028c1b6884Sszhou }
22038c1b6884Sszhou 
22048c1b6884Sszhou static entry_t *
22058c1b6884Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end)
22068c1b6884Sszhou {
22078c1b6884Sszhou 	entry_t *ent, *prev;
22088c1b6884Sszhou 
22098c1b6884Sszhou 	ent = s_calloc(1, sizeof (entry_t));
22108c1b6884Sszhou 	ent->start = start;
22118c1b6884Sszhou 	ent->end = end;
22128c1b6884Sszhou 
22138c1b6884Sszhou 	if (mp->entries == NULL) {
22148c1b6884Sszhou 		mp->entries = ent;
22158c1b6884Sszhou 		return (ent);
22168c1b6884Sszhou 	}
22178c1b6884Sszhou 
22188c1b6884Sszhou 	prev = mp->entries;
22198c1b6884Sszhou 	while (prev->next)
22208c1b6884Sszhou 		prev = prev-> next;
22218c1b6884Sszhou 	prev->next = ent;
22228c1b6884Sszhou 	ent->prev = prev;
22238c1b6884Sszhou 	return (ent);
22248c1b6884Sszhou }
22258c1b6884Sszhou 
22268c1b6884Sszhou static void
22278c1b6884Sszhou boot_entry_addline(entry_t *ent, line_t *lp)
22288c1b6884Sszhou {
22298c1b6884Sszhou 	if (ent)
22308c1b6884Sszhou 		ent->end = lp;
22318c1b6884Sszhou }
22328c1b6884Sszhou 
22337c478bd9Sstevel@tonic-gate /*
22347c478bd9Sstevel@tonic-gate  * A line in menu.lst looks like
22357c478bd9Sstevel@tonic-gate  * [ ]*<cmd>[ \t=]*<arg>*
22367c478bd9Sstevel@tonic-gate  */
22377c478bd9Sstevel@tonic-gate static void
22387c478bd9Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum)
22397c478bd9Sstevel@tonic-gate {
22407c478bd9Sstevel@tonic-gate 	/*
22417c478bd9Sstevel@tonic-gate 	 * save state across calls. This is so that
22427c478bd9Sstevel@tonic-gate 	 * header gets the right entry# after title has
22437c478bd9Sstevel@tonic-gate 	 * been processed
22447c478bd9Sstevel@tonic-gate 	 */
22458c1b6884Sszhou 	static line_t *prev = NULL;
22468c1b6884Sszhou 	static entry_t *curr_ent = NULL;
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 	line_t	*lp;
22497c478bd9Sstevel@tonic-gate 	char *cmd, *sep, *arg;
22507c478bd9Sstevel@tonic-gate 	char save, *cp, *line;
22517c478bd9Sstevel@tonic-gate 	menu_flag_t flag = BAM_INVALID;
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate 	if (str == NULL) {
22547c478bd9Sstevel@tonic-gate 		return;
22557c478bd9Sstevel@tonic-gate 	}
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate 	/*
22587c478bd9Sstevel@tonic-gate 	 * First save a copy of the entire line.
22597c478bd9Sstevel@tonic-gate 	 * We use this later to set the line field.
22607c478bd9Sstevel@tonic-gate 	 */
22617c478bd9Sstevel@tonic-gate 	line = s_strdup(str);
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate 	/* Eat up leading whitespace */
22647c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
22657c478bd9Sstevel@tonic-gate 		str++;
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 	if (*str == '#') {		/* comment */
22687c478bd9Sstevel@tonic-gate 		cmd = s_strdup("#");
22697c478bd9Sstevel@tonic-gate 		sep = NULL;
22707c478bd9Sstevel@tonic-gate 		arg = s_strdup(str + 1);
22717c478bd9Sstevel@tonic-gate 		flag = BAM_COMMENT;
22727c478bd9Sstevel@tonic-gate 	} else if (*str == '\0') {	/* blank line */
22737c478bd9Sstevel@tonic-gate 		cmd = sep = arg = NULL;
22747c478bd9Sstevel@tonic-gate 		flag = BAM_EMPTY;
22757c478bd9Sstevel@tonic-gate 	} else {
22767c478bd9Sstevel@tonic-gate 		/*
22777c478bd9Sstevel@tonic-gate 		 * '=' is not a documented separator in grub syntax.
22787c478bd9Sstevel@tonic-gate 		 * However various development bits use '=' as a
22797c478bd9Sstevel@tonic-gate 		 * separator. In addition, external users also
22807c478bd9Sstevel@tonic-gate 		 * use = as a separator. So we will allow that usage.
22817c478bd9Sstevel@tonic-gate 		 */
22827c478bd9Sstevel@tonic-gate 		cp = str;
22837c478bd9Sstevel@tonic-gate 		while (*str != ' ' && *str != '\t' && *str != '=') {
22847c478bd9Sstevel@tonic-gate 			if (*str == '\0') {
22857c478bd9Sstevel@tonic-gate 				cmd = s_strdup(cp);
22867c478bd9Sstevel@tonic-gate 				sep = arg = NULL;
22877c478bd9Sstevel@tonic-gate 				break;
22887c478bd9Sstevel@tonic-gate 			}
22897c478bd9Sstevel@tonic-gate 			str++;
22907c478bd9Sstevel@tonic-gate 		}
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 		if (*str != '\0') {
22937c478bd9Sstevel@tonic-gate 			save = *str;
22947c478bd9Sstevel@tonic-gate 			*str = '\0';
22957c478bd9Sstevel@tonic-gate 			cmd = s_strdup(cp);
22967c478bd9Sstevel@tonic-gate 			*str = save;
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 			str++;
22997c478bd9Sstevel@tonic-gate 			save = *str;
23007c478bd9Sstevel@tonic-gate 			*str = '\0';
23017c478bd9Sstevel@tonic-gate 			sep = s_strdup(str - 1);
23027c478bd9Sstevel@tonic-gate 			*str = save;
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate 			while (*str == ' ' || *str == '\t')
23057c478bd9Sstevel@tonic-gate 				str++;
23067c478bd9Sstevel@tonic-gate 			if (*str == '\0')
23077c478bd9Sstevel@tonic-gate 				arg = NULL;
23087c478bd9Sstevel@tonic-gate 			else
23097c478bd9Sstevel@tonic-gate 				arg = s_strdup(str);
23107c478bd9Sstevel@tonic-gate 		}
23117c478bd9Sstevel@tonic-gate 	}
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate 	lp->cmd = cmd;
23167c478bd9Sstevel@tonic-gate 	lp->sep = sep;
23177c478bd9Sstevel@tonic-gate 	lp->arg = arg;
23187c478bd9Sstevel@tonic-gate 	lp->line = line;
23197c478bd9Sstevel@tonic-gate 	lp->lineNum = ++(*lineNum);
23207c478bd9Sstevel@tonic-gate 	if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) {
23217c478bd9Sstevel@tonic-gate 		lp->entryNum = ++(*entryNum);
23227c478bd9Sstevel@tonic-gate 		lp->flags = BAM_TITLE;
23237c478bd9Sstevel@tonic-gate 		if (prev && prev->flags == BAM_COMMENT &&
23248c1b6884Sszhou 		    prev->arg && strcmp(prev->arg, BAM_HDR) == 0) {
23257c478bd9Sstevel@tonic-gate 			prev->entryNum = lp->entryNum;
23268c1b6884Sszhou 			curr_ent = boot_entry_new(mp, prev, lp);
23278c1b6884Sszhou 		} else {
23288c1b6884Sszhou 			curr_ent = boot_entry_new(mp, lp, lp);
23298c1b6884Sszhou 		}
23307c478bd9Sstevel@tonic-gate 	} else if (flag != BAM_INVALID) {
23317c478bd9Sstevel@tonic-gate 		/*
23327c478bd9Sstevel@tonic-gate 		 * For header comments, the entry# is "fixed up"
23337c478bd9Sstevel@tonic-gate 		 * by the subsequent title
23347c478bd9Sstevel@tonic-gate 		 */
23357c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
23367c478bd9Sstevel@tonic-gate 		lp->flags = flag;
23377c478bd9Sstevel@tonic-gate 	} else {
23387c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
23397c478bd9Sstevel@tonic-gate 		lp->flags = (*entryNum == ENTRY_INIT) ? BAM_GLOBAL : BAM_ENTRY;
23407c478bd9Sstevel@tonic-gate 	}
23417c478bd9Sstevel@tonic-gate 
23428c1b6884Sszhou 	/* record default, old default, and entry line ranges */
23438c1b6884Sszhou 	if (lp->flags == BAM_GLOBAL &&
23448c1b6884Sszhou 	    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) {
23458c1b6884Sszhou 		mp->curdefault = lp;
23468c1b6884Sszhou 	} else if (lp->flags == BAM_COMMENT &&
23478c1b6884Sszhou 	    strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) {
23488c1b6884Sszhou 		mp->olddefault = lp;
23498c1b6884Sszhou 	} else if (lp->flags == BAM_ENTRY ||
23508c1b6884Sszhou 	    (lp->flags == BAM_COMMENT && strcmp(lp->arg, BAM_FTR) == 0)) {
23518c1b6884Sszhou 		boot_entry_addline(curr_ent, lp);
23528c1b6884Sszhou 	}
23537c478bd9Sstevel@tonic-gate 	append_line(mp, lp);
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 	prev = lp;
23567c478bd9Sstevel@tonic-gate }
23577c478bd9Sstevel@tonic-gate 
235840541d5dSvikram static void
235940541d5dSvikram update_numbering(menu_t *mp)
236040541d5dSvikram {
236140541d5dSvikram 	int lineNum;
236240541d5dSvikram 	int entryNum;
236340541d5dSvikram 	int old_default_value;
236440541d5dSvikram 	line_t *lp, *prev, *default_lp, *default_entry;
236540541d5dSvikram 	char buf[PATH_MAX];
236640541d5dSvikram 
236740541d5dSvikram 	if (mp->start == NULL) {
236840541d5dSvikram 		return;
236940541d5dSvikram 	}
237040541d5dSvikram 
237140541d5dSvikram 	lineNum = LINE_INIT;
237240541d5dSvikram 	entryNum = ENTRY_INIT;
237340541d5dSvikram 	old_default_value = ENTRY_INIT;
237440541d5dSvikram 	lp = default_lp = default_entry = NULL;
237540541d5dSvikram 
237640541d5dSvikram 	prev = NULL;
237740541d5dSvikram 	for (lp = mp->start; lp; prev = lp, lp = lp->next) {
237840541d5dSvikram 		lp->lineNum = ++lineNum;
237940541d5dSvikram 
238040541d5dSvikram 		/*
238140541d5dSvikram 		 * Get the value of the default command
238240541d5dSvikram 		 */
238340541d5dSvikram 		if (lp->entryNum == ENTRY_INIT && lp->cmd &&
238440541d5dSvikram 		    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 &&
238540541d5dSvikram 		    lp->arg) {
238640541d5dSvikram 			old_default_value = atoi(lp->arg);
238740541d5dSvikram 			default_lp = lp;
238840541d5dSvikram 		}
238940541d5dSvikram 
239040541d5dSvikram 		/*
239140541d5dSvikram 		 * If not boot entry, nothing else to fix for this
239240541d5dSvikram 		 * entry
239340541d5dSvikram 		 */
239440541d5dSvikram 		if (lp->entryNum == ENTRY_INIT)
239540541d5dSvikram 			continue;
239640541d5dSvikram 
239740541d5dSvikram 		/*
239840541d5dSvikram 		 * Record the position of the default entry.
239940541d5dSvikram 		 * The following works because global
240040541d5dSvikram 		 * commands like default and timeout should precede
240140541d5dSvikram 		 * actual boot entries, so old_default_value
240240541d5dSvikram 		 * is already known (or default cmd is missing).
240340541d5dSvikram 		 */
240440541d5dSvikram 		if (default_entry == NULL &&
240540541d5dSvikram 		    old_default_value != ENTRY_INIT &&
240640541d5dSvikram 		    lp->entryNum == old_default_value) {
240740541d5dSvikram 			default_entry = lp;
240840541d5dSvikram 		}
240940541d5dSvikram 
241040541d5dSvikram 		/*
241140541d5dSvikram 		 * Now fixup the entry number
241240541d5dSvikram 		 */
241340541d5dSvikram 		if (lp->cmd && strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) {
241440541d5dSvikram 			lp->entryNum = ++entryNum;
241540541d5dSvikram 			/* fixup the bootadm header */
241640541d5dSvikram 			if (prev && prev->flags == BAM_COMMENT &&
241740541d5dSvikram 			    prev->arg && strcmp(prev->arg, BAM_HDR) == 0) {
241840541d5dSvikram 				prev->entryNum = lp->entryNum;
241940541d5dSvikram 			}
242040541d5dSvikram 		} else {
242140541d5dSvikram 			lp->entryNum = entryNum;
242240541d5dSvikram 		}
242340541d5dSvikram 	}
242440541d5dSvikram 
242540541d5dSvikram 	/*
242640541d5dSvikram 	 * No default command in menu, simply return
242740541d5dSvikram 	 */
242840541d5dSvikram 	if (default_lp == NULL) {
242940541d5dSvikram 		return;
243040541d5dSvikram 	}
243140541d5dSvikram 
243240541d5dSvikram 	free(default_lp->arg);
243340541d5dSvikram 	free(default_lp->line);
243440541d5dSvikram 
243540541d5dSvikram 	if (default_entry == NULL) {
243640541d5dSvikram 		default_lp->arg = s_strdup("0");
243740541d5dSvikram 	} else {
243840541d5dSvikram 		(void) snprintf(buf, sizeof (buf), "%d",
243940541d5dSvikram 		    default_entry->entryNum);
244040541d5dSvikram 		default_lp->arg = s_strdup(buf);
244140541d5dSvikram 	}
244240541d5dSvikram 
244340541d5dSvikram 	/*
244440541d5dSvikram 	 * The following is required since only the line field gets
244540541d5dSvikram 	 * written back to menu.lst
244640541d5dSvikram 	 */
244740541d5dSvikram 	(void) snprintf(buf, sizeof (buf), "%s%s%s",
244840541d5dSvikram 	    menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg);
244940541d5dSvikram 	default_lp->line = s_strdup(buf);
245040541d5dSvikram }
245140541d5dSvikram 
245240541d5dSvikram 
24537c478bd9Sstevel@tonic-gate static menu_t *
24547c478bd9Sstevel@tonic-gate menu_read(char *menu_path)
24557c478bd9Sstevel@tonic-gate {
24567c478bd9Sstevel@tonic-gate 	FILE *fp;
24577c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE], *cp;
24587c478bd9Sstevel@tonic-gate 	menu_t *mp;
24597c478bd9Sstevel@tonic-gate 	int line, entry, len, n;
24607c478bd9Sstevel@tonic-gate 
24617c478bd9Sstevel@tonic-gate 	mp = s_calloc(1, sizeof (menu_t));
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate 	fp = fopen(menu_path, "r");
24647c478bd9Sstevel@tonic-gate 	if (fp == NULL) { /* Let the caller handle this error */
24657c478bd9Sstevel@tonic-gate 		return (mp);
24667c478bd9Sstevel@tonic-gate 	}
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 
24697c478bd9Sstevel@tonic-gate 	/* Note: GRUB boot entry number starts with 0 */
24707c478bd9Sstevel@tonic-gate 	line = LINE_INIT;
24717c478bd9Sstevel@tonic-gate 	entry = ENTRY_INIT;
24727c478bd9Sstevel@tonic-gate 	cp = buf;
24737c478bd9Sstevel@tonic-gate 	len = sizeof (buf);
24747c478bd9Sstevel@tonic-gate 	while (s_fgets(cp, len, fp) != NULL) {
24757c478bd9Sstevel@tonic-gate 		n = strlen(cp);
24767c478bd9Sstevel@tonic-gate 		if (cp[n - 1] == '\\') {
24777c478bd9Sstevel@tonic-gate 			len -= n - 1;
24787c478bd9Sstevel@tonic-gate 			assert(len >= 2);
24797c478bd9Sstevel@tonic-gate 			cp += n - 1;
24807c478bd9Sstevel@tonic-gate 			continue;
24817c478bd9Sstevel@tonic-gate 		}
24827c478bd9Sstevel@tonic-gate 		line_parser(mp, buf, &line, &entry);
24837c478bd9Sstevel@tonic-gate 		cp = buf;
24847c478bd9Sstevel@tonic-gate 		len = sizeof (buf);
24857c478bd9Sstevel@tonic-gate 	}
24867c478bd9Sstevel@tonic-gate 
24877c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
24887c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, menu_path, strerror(errno));
24897c478bd9Sstevel@tonic-gate 	}
24907c478bd9Sstevel@tonic-gate 
24917c478bd9Sstevel@tonic-gate 	return (mp);
24927c478bd9Sstevel@tonic-gate }
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate static error_t
24957c478bd9Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title)
24967c478bd9Sstevel@tonic-gate {
24977c478bd9Sstevel@tonic-gate 	char *eq;
24987c478bd9Sstevel@tonic-gate 	char *opt_dup;
24997c478bd9Sstevel@tonic-gate 	int entryNum;
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 	assert(mp);
25027c478bd9Sstevel@tonic-gate 	assert(mp->start);
25037c478bd9Sstevel@tonic-gate 	assert(opt);
25047c478bd9Sstevel@tonic-gate 
25057c478bd9Sstevel@tonic-gate 	opt_dup = s_strdup(opt);
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 	if (entry)
25087c478bd9Sstevel@tonic-gate 		*entry = ENTRY_INIT;
25097c478bd9Sstevel@tonic-gate 	if (title)
25107c478bd9Sstevel@tonic-gate 		*title = NULL;
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate 	eq = strchr(opt_dup, '=');
25137c478bd9Sstevel@tonic-gate 	if (eq == NULL) {
25147c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
25157c478bd9Sstevel@tonic-gate 		free(opt_dup);
25167c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
25177c478bd9Sstevel@tonic-gate 	}
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 	*eq = '\0';
25207c478bd9Sstevel@tonic-gate 	if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) {
25217c478bd9Sstevel@tonic-gate 		assert(mp->end);
25227c478bd9Sstevel@tonic-gate 		entryNum = s_strtol(eq + 1);
25237c478bd9Sstevel@tonic-gate 		if (entryNum < 0 || entryNum > mp->end->entryNum) {
25247c478bd9Sstevel@tonic-gate 			bam_error(INVALID_ENTRY, eq + 1);
25257c478bd9Sstevel@tonic-gate 			free(opt_dup);
25267c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
25277c478bd9Sstevel@tonic-gate 		}
25287c478bd9Sstevel@tonic-gate 		*entry = entryNum;
25297c478bd9Sstevel@tonic-gate 	} else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) {
25307c478bd9Sstevel@tonic-gate 		*title = opt + (eq - opt_dup) + 1;
25317c478bd9Sstevel@tonic-gate 	} else {
25327c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
25337c478bd9Sstevel@tonic-gate 		free(opt_dup);
25347c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
25357c478bd9Sstevel@tonic-gate 	}
25367c478bd9Sstevel@tonic-gate 
25377c478bd9Sstevel@tonic-gate 	free(opt_dup);
25387c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
25397c478bd9Sstevel@tonic-gate }
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate /*
25427c478bd9Sstevel@tonic-gate  * If invoked with no titles/entries (opt == NULL)
25437c478bd9Sstevel@tonic-gate  * only title lines in file are printed.
25447c478bd9Sstevel@tonic-gate  *
25457c478bd9Sstevel@tonic-gate  * If invoked with a title or entry #, all
25467c478bd9Sstevel@tonic-gate  * lines in *every* matching entry are listed
25477c478bd9Sstevel@tonic-gate  */
25487c478bd9Sstevel@tonic-gate static error_t
25497c478bd9Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt)
25507c478bd9Sstevel@tonic-gate {
25517c478bd9Sstevel@tonic-gate 	line_t *lp;
25527c478bd9Sstevel@tonic-gate 	int entry = ENTRY_INIT;
25537c478bd9Sstevel@tonic-gate 	int found;
25547c478bd9Sstevel@tonic-gate 	char *title = NULL;
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate 	assert(mp);
25577c478bd9Sstevel@tonic-gate 	assert(menu_path);
25587c478bd9Sstevel@tonic-gate 
25597c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
25607c478bd9Sstevel@tonic-gate 		bam_error(NO_MENU, menu_path);
25617c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
25627c478bd9Sstevel@tonic-gate 	}
25637c478bd9Sstevel@tonic-gate 
25647c478bd9Sstevel@tonic-gate 	if (opt != NULL) {
25657c478bd9Sstevel@tonic-gate 		if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
25667c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
25677c478bd9Sstevel@tonic-gate 		}
25687c478bd9Sstevel@tonic-gate 		assert((entry != ENTRY_INIT) ^ (title != NULL));
25697c478bd9Sstevel@tonic-gate 	} else {
25707c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0);
25717c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0);
25727c478bd9Sstevel@tonic-gate 	}
25737c478bd9Sstevel@tonic-gate 
25747c478bd9Sstevel@tonic-gate 	found = 0;
25757c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
25767c478bd9Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY)
25777c478bd9Sstevel@tonic-gate 			continue;
25787c478bd9Sstevel@tonic-gate 		if (opt == NULL && lp->flags == BAM_TITLE) {
25797c478bd9Sstevel@tonic-gate 			bam_print(PRINT_TITLE, lp->entryNum,
25807c478bd9Sstevel@tonic-gate 			    lp->arg);
25817c478bd9Sstevel@tonic-gate 			found = 1;
25827c478bd9Sstevel@tonic-gate 			continue;
25837c478bd9Sstevel@tonic-gate 		}
25847c478bd9Sstevel@tonic-gate 		if (entry != ENTRY_INIT && lp->entryNum == entry) {
25857c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
25867c478bd9Sstevel@tonic-gate 			found = 1;
25877c478bd9Sstevel@tonic-gate 			continue;
25887c478bd9Sstevel@tonic-gate 		}
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate 		/*
25917c478bd9Sstevel@tonic-gate 		 * We set the entry value here so that all lines
25927c478bd9Sstevel@tonic-gate 		 * in entry get printed. If we subsequently match
25937c478bd9Sstevel@tonic-gate 		 * title in other entries, all lines in those
25947c478bd9Sstevel@tonic-gate 		 * entries get printed as well.
25957c478bd9Sstevel@tonic-gate 		 */
25967c478bd9Sstevel@tonic-gate 		if (title && lp->flags == BAM_TITLE && lp->arg &&
25977c478bd9Sstevel@tonic-gate 		    strncmp(title, lp->arg, strlen(title)) == 0) {
25987c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
25997c478bd9Sstevel@tonic-gate 			entry = lp->entryNum;
26007c478bd9Sstevel@tonic-gate 			found = 1;
26017c478bd9Sstevel@tonic-gate 			continue;
26027c478bd9Sstevel@tonic-gate 		}
26037c478bd9Sstevel@tonic-gate 	}
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 	if (!found) {
26067c478bd9Sstevel@tonic-gate 		bam_error(NO_MATCH_ENTRY);
26077c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
26087c478bd9Sstevel@tonic-gate 	}
26097c478bd9Sstevel@tonic-gate 
26107c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
26117c478bd9Sstevel@tonic-gate }
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate static int
26147c478bd9Sstevel@tonic-gate add_boot_entry(menu_t *mp,
26157c478bd9Sstevel@tonic-gate 	char *title,
26167c478bd9Sstevel@tonic-gate 	char *root,
26177c478bd9Sstevel@tonic-gate 	char *kernel,
26187c478bd9Sstevel@tonic-gate 	char *module)
26197c478bd9Sstevel@tonic-gate {
26207c478bd9Sstevel@tonic-gate 	int lineNum, entryNum;
26217c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate 	assert(mp);
26247c478bd9Sstevel@tonic-gate 
26257c478bd9Sstevel@tonic-gate 	if (title == NULL) {
2626ee3b8144Sszhou 		title = "Solaris";	/* default to Solaris */
26277c478bd9Sstevel@tonic-gate 	}
26287c478bd9Sstevel@tonic-gate 	if (kernel == NULL) {
26297c478bd9Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]);
26307c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
26317c478bd9Sstevel@tonic-gate 	}
26327c478bd9Sstevel@tonic-gate 	if (module == NULL) {
26337c478bd9Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]);
26347c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
26357c478bd9Sstevel@tonic-gate 	}
26367c478bd9Sstevel@tonic-gate 
26377c478bd9Sstevel@tonic-gate 	if (mp->start) {
26387c478bd9Sstevel@tonic-gate 		lineNum = mp->end->lineNum;
26397c478bd9Sstevel@tonic-gate 		entryNum = mp->end->entryNum;
26407c478bd9Sstevel@tonic-gate 	} else {
26417c478bd9Sstevel@tonic-gate 		lineNum = LINE_INIT;
26427c478bd9Sstevel@tonic-gate 		entryNum = ENTRY_INIT;
26437c478bd9Sstevel@tonic-gate 	}
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate 	/*
26467c478bd9Sstevel@tonic-gate 	 * No separator for comment (HDR/FTR) commands
26477c478bd9Sstevel@tonic-gate 	 * The syntax for comments is #<comment>
26487c478bd9Sstevel@tonic-gate 	 */
26497c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
26507c478bd9Sstevel@tonic-gate 	    menu_cmds[COMMENT_CMD], BAM_HDR);
26518c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
26547c478bd9Sstevel@tonic-gate 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
26558c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
26567c478bd9Sstevel@tonic-gate 
26578c1b6884Sszhou 	if (root) {
26587c478bd9Sstevel@tonic-gate 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
26597c478bd9Sstevel@tonic-gate 		    menu_cmds[ROOT_CMD], menu_cmds[SEP_CMD], root);
26608c1b6884Sszhou 		line_parser(mp, linebuf, &lineNum, &entryNum);
26617c478bd9Sstevel@tonic-gate 	}
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
26647c478bd9Sstevel@tonic-gate 	    menu_cmds[KERNEL_CMD], menu_cmds[SEP_CMD], kernel);
26658c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
26667c478bd9Sstevel@tonic-gate 
26677c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
26687c478bd9Sstevel@tonic-gate 	    menu_cmds[MODULE_CMD], menu_cmds[SEP_CMD], module);
26698c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
26707c478bd9Sstevel@tonic-gate 
26717c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
26727c478bd9Sstevel@tonic-gate 	    menu_cmds[COMMENT_CMD], BAM_FTR);
26738c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
26747c478bd9Sstevel@tonic-gate 
26757c478bd9Sstevel@tonic-gate 	return (entryNum);
26767c478bd9Sstevel@tonic-gate }
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate static error_t
26797c478bd9Sstevel@tonic-gate do_delete(menu_t *mp, int entryNum)
26807c478bd9Sstevel@tonic-gate {
26818c1b6884Sszhou 	line_t *lp, *freed;
26828c1b6884Sszhou 	entry_t *ent, *tmp;
26837c478bd9Sstevel@tonic-gate 	int deleted;
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate 	assert(entryNum != ENTRY_INIT);
26867c478bd9Sstevel@tonic-gate 
26878c1b6884Sszhou 	ent = mp->entries;
26888c1b6884Sszhou 	while (ent) {
26898c1b6884Sszhou 		lp = ent->start;
26908c1b6884Sszhou 		/* check entry number and make sure it's a bootadm entry */
26918c1b6884Sszhou 		if (lp->flags != BAM_COMMENT ||
26928c1b6884Sszhou 		    strcmp(lp->arg, BAM_HDR) != 0 ||
26938c1b6884Sszhou 		    (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) {
26948c1b6884Sszhou 			ent = ent->next;
26957c478bd9Sstevel@tonic-gate 			continue;
26967c478bd9Sstevel@tonic-gate 		}
26977c478bd9Sstevel@tonic-gate 
26988c1b6884Sszhou 		/* free the entry content */
26998c1b6884Sszhou 		do {
27008c1b6884Sszhou 			freed = lp;
27018c1b6884Sszhou 			lp = lp->next;	/* prev stays the same */
27028c1b6884Sszhou 			unlink_line(mp, freed);
27038c1b6884Sszhou 			line_free(freed);
27048c1b6884Sszhou 		} while (freed != ent->end);
27057c478bd9Sstevel@tonic-gate 
27068c1b6884Sszhou 		/* free the entry_t structure */
27078c1b6884Sszhou 		tmp = ent;
27088c1b6884Sszhou 		ent = ent->next;
27098c1b6884Sszhou 		if (tmp->prev)
27108c1b6884Sszhou 			tmp->prev->next = ent;
27117c478bd9Sstevel@tonic-gate 		else
27128c1b6884Sszhou 			mp->entries = ent;
27138c1b6884Sszhou 		if (ent)
27148c1b6884Sszhou 			ent->prev = tmp->prev;
27157c478bd9Sstevel@tonic-gate 		deleted = 1;
27167c478bd9Sstevel@tonic-gate 	}
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate 	if (!deleted && entryNum != ALL_ENTRIES) {
27197c478bd9Sstevel@tonic-gate 		bam_error(NO_BOOTADM_MATCH);
27207c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27217c478bd9Sstevel@tonic-gate 	}
27227c478bd9Sstevel@tonic-gate 
272340541d5dSvikram 	/*
272440541d5dSvikram 	 * Now that we have deleted an entry, update
272540541d5dSvikram 	 * the entry numbering and the default cmd.
272640541d5dSvikram 	 */
272740541d5dSvikram 	update_numbering(mp);
272840541d5dSvikram 
27297c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
27307c478bd9Sstevel@tonic-gate }
27317c478bd9Sstevel@tonic-gate 
27327c478bd9Sstevel@tonic-gate static error_t
27337c478bd9Sstevel@tonic-gate delete_all_entries(menu_t *mp, char *menu_path, char *opt)
27347c478bd9Sstevel@tonic-gate {
27357c478bd9Sstevel@tonic-gate 	assert(mp);
27367c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
27397c478bd9Sstevel@tonic-gate 		bam_print(EMPTY_FILE, menu_path);
27407c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
27417c478bd9Sstevel@tonic-gate 	}
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate 	if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) {
27447c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
27457c478bd9Sstevel@tonic-gate 	}
27467c478bd9Sstevel@tonic-gate 
27477c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
27487c478bd9Sstevel@tonic-gate }
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate static FILE *
27518c1b6884Sszhou open_diskmap(char *root)
27527c478bd9Sstevel@tonic-gate {
27537c478bd9Sstevel@tonic-gate 	FILE *fp;
27547c478bd9Sstevel@tonic-gate 	char cmd[PATH_MAX];
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate 	/* make sure we have a map file */
27577c478bd9Sstevel@tonic-gate 	fp = fopen(GRUBDISK_MAP, "r");
27587c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
27597c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd),
27608c1b6884Sszhou 		    "%s%s > /dev/null", root, CREATE_DISKMAP);
27617c478bd9Sstevel@tonic-gate 		(void) system(cmd);
27627c478bd9Sstevel@tonic-gate 		fp = fopen(GRUBDISK_MAP, "r");
27637c478bd9Sstevel@tonic-gate 	}
27647c478bd9Sstevel@tonic-gate 	return (fp);
27657c478bd9Sstevel@tonic-gate }
27667c478bd9Sstevel@tonic-gate 
27677c478bd9Sstevel@tonic-gate #define	SECTOR_SIZE	512
27687c478bd9Sstevel@tonic-gate 
27697c478bd9Sstevel@tonic-gate static int
27707c478bd9Sstevel@tonic-gate get_partition(char *device)
27717c478bd9Sstevel@tonic-gate {
27727c478bd9Sstevel@tonic-gate 	int i, fd, is_pcfs, partno = -1;
27737c478bd9Sstevel@tonic-gate 	struct mboot *mboot;
27747c478bd9Sstevel@tonic-gate 	char boot_sect[SECTOR_SIZE];
27757c478bd9Sstevel@tonic-gate 	char *wholedisk, *slice;
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 	/* form whole disk (p0) */
27787c478bd9Sstevel@tonic-gate 	slice = device + strlen(device) - 2;
27797c478bd9Sstevel@tonic-gate 	is_pcfs = (*slice != 's');
27807c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
27817c478bd9Sstevel@tonic-gate 		*slice = '\0';
27827c478bd9Sstevel@tonic-gate 	wholedisk = s_calloc(1, strlen(device) + 3);
27837c478bd9Sstevel@tonic-gate 	(void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device);
27847c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
27857c478bd9Sstevel@tonic-gate 		*slice = 's';
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 	/* read boot sector */
27887c478bd9Sstevel@tonic-gate 	fd = open(wholedisk, O_RDONLY);
27897c478bd9Sstevel@tonic-gate 	free(wholedisk);
27907c478bd9Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
27917c478bd9Sstevel@tonic-gate 		return (partno);
27927c478bd9Sstevel@tonic-gate 	}
27937c478bd9Sstevel@tonic-gate 	(void) close(fd);
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate 	/* parse fdisk table */
27967c478bd9Sstevel@tonic-gate 	mboot = (struct mboot *)((void *)boot_sect);
27977c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
27987c478bd9Sstevel@tonic-gate 		struct ipart *part =
27997c478bd9Sstevel@tonic-gate 		    (struct ipart *)(uintptr_t)mboot->parts + i;
28007c478bd9Sstevel@tonic-gate 		if (is_pcfs) {	/* looking for solaris boot part */
28017c478bd9Sstevel@tonic-gate 			if (part->systid == 0xbe) {
28027c478bd9Sstevel@tonic-gate 				partno = i;
28037c478bd9Sstevel@tonic-gate 				break;
28047c478bd9Sstevel@tonic-gate 			}
28057c478bd9Sstevel@tonic-gate 		} else {	/* look for solaris partition, old and new */
28067c478bd9Sstevel@tonic-gate 			if (part->systid == SUNIXOS ||
28077c478bd9Sstevel@tonic-gate 			    part->systid == SUNIXOS2) {
28087c478bd9Sstevel@tonic-gate 				partno = i;
28097c478bd9Sstevel@tonic-gate 				break;
28107c478bd9Sstevel@tonic-gate 			}
28117c478bd9Sstevel@tonic-gate 		}
28127c478bd9Sstevel@tonic-gate 	}
28137c478bd9Sstevel@tonic-gate 	return (partno);
28147c478bd9Sstevel@tonic-gate }
28157c478bd9Sstevel@tonic-gate 
28167c478bd9Sstevel@tonic-gate static char *
28177c478bd9Sstevel@tonic-gate get_grubdisk(char *rootdev, FILE *fp, int on_bootdev)
28187c478bd9Sstevel@tonic-gate {
28197c478bd9Sstevel@tonic-gate 	char *grubdisk;	/* (hd#,#,#) */
28207c478bd9Sstevel@tonic-gate 	char *slice;
28217c478bd9Sstevel@tonic-gate 	char *grubhd;
28227c478bd9Sstevel@tonic-gate 	int fdiskpart;
28237c478bd9Sstevel@tonic-gate 	int found = 0;
28247c478bd9Sstevel@tonic-gate 	char *devname, *ctdname = strstr(rootdev, "dsk/");
28257c478bd9Sstevel@tonic-gate 	char linebuf[PATH_MAX];
28267c478bd9Sstevel@tonic-gate 
28277c478bd9Sstevel@tonic-gate 	if (ctdname == NULL)
28287c478bd9Sstevel@tonic-gate 		return (NULL);
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate 	ctdname += strlen("dsk/");
28317c478bd9Sstevel@tonic-gate 	slice = strrchr(ctdname, 's');
28327c478bd9Sstevel@tonic-gate 	if (slice)
28337c478bd9Sstevel@tonic-gate 		*slice = '\0';
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 	rewind(fp);
28367c478bd9Sstevel@tonic-gate 	while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) {
28377c478bd9Sstevel@tonic-gate 		grubhd = strtok(linebuf, " \t\n");
28387c478bd9Sstevel@tonic-gate 		if (grubhd)
28397c478bd9Sstevel@tonic-gate 			devname = strtok(NULL, " \t\n");
28407c478bd9Sstevel@tonic-gate 		else
28417c478bd9Sstevel@tonic-gate 			devname = NULL;
28427c478bd9Sstevel@tonic-gate 		if (devname && strcmp(devname, ctdname) == 0) {
28437c478bd9Sstevel@tonic-gate 			found = 1;
28447c478bd9Sstevel@tonic-gate 			break;
28457c478bd9Sstevel@tonic-gate 		}
28467c478bd9Sstevel@tonic-gate 	}
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate 	if (slice)
28497c478bd9Sstevel@tonic-gate 		*slice = 's';
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 	if (found == 0) {
28527c478bd9Sstevel@tonic-gate 		if (bam_verbose)
28537c478bd9Sstevel@tonic-gate 			bam_print(DISKMAP_FAIL_NONFATAL, rootdev);
28547c478bd9Sstevel@tonic-gate 		grubhd = "0";	/* assume disk 0 if can't match */
28557c478bd9Sstevel@tonic-gate 	}
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate 	fdiskpart = get_partition(rootdev);
28587c478bd9Sstevel@tonic-gate 	if (fdiskpart == -1)
28597c478bd9Sstevel@tonic-gate 		return (NULL);
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate 	grubdisk = s_calloc(1, 10);
28627c478bd9Sstevel@tonic-gate 	if (slice) {
28637c478bd9Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d,%c)",
28647c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart, slice[1] + 'a' - '0');
28657c478bd9Sstevel@tonic-gate 	} else
28667c478bd9Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d)",
28677c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart);
28687c478bd9Sstevel@tonic-gate 
28697c478bd9Sstevel@tonic-gate 	/* if root not on bootdev, change GRUB disk to 0 */
28707c478bd9Sstevel@tonic-gate 	if (!on_bootdev)
28717c478bd9Sstevel@tonic-gate 		grubdisk[3] = '0';
28727c478bd9Sstevel@tonic-gate 	return (grubdisk);
28737c478bd9Sstevel@tonic-gate }
28747c478bd9Sstevel@tonic-gate 
2875ee3b8144Sszhou static char *
2876ee3b8144Sszhou get_title(char *rootdir)
28777c478bd9Sstevel@tonic-gate {
28787c478bd9Sstevel@tonic-gate 	static char title[80];	/* from /etc/release */
28798c1b6884Sszhou 	char *cp = NULL, release[PATH_MAX];
28807c478bd9Sstevel@tonic-gate 	FILE *fp;
28817c478bd9Sstevel@tonic-gate 
28827c478bd9Sstevel@tonic-gate 	/* open the /etc/release file */
28837c478bd9Sstevel@tonic-gate 	(void) snprintf(release, sizeof (release), "%s/etc/release", rootdir);
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate 	fp = fopen(release, "r");
28867c478bd9Sstevel@tonic-gate 	if (fp == NULL)
2887ee3b8144Sszhou 		return (NULL);
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate 	while (s_fgets(title, sizeof (title), fp) != NULL) {
28907c478bd9Sstevel@tonic-gate 		cp = strstr(title, "Solaris");
28917c478bd9Sstevel@tonic-gate 		if (cp)
28927c478bd9Sstevel@tonic-gate 			break;
28937c478bd9Sstevel@tonic-gate 	}
28947c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
28958c1b6884Sszhou 	return (cp == NULL ? "Solaris" : cp);
28967c478bd9Sstevel@tonic-gate }
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate static char *
28997c478bd9Sstevel@tonic-gate get_special(char *mountp)
29007c478bd9Sstevel@tonic-gate {
29017c478bd9Sstevel@tonic-gate 	FILE *mntfp;
29027c478bd9Sstevel@tonic-gate 	struct mnttab mp = {0}, mpref = {0};
29037c478bd9Sstevel@tonic-gate 
29047c478bd9Sstevel@tonic-gate 	mntfp = fopen(MNTTAB, "r");
29057c478bd9Sstevel@tonic-gate 	if (mntfp == NULL) {
29067c478bd9Sstevel@tonic-gate 		return (0);
29077c478bd9Sstevel@tonic-gate 	}
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate 	if (*mountp == '\0')
29107c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = "/";
29117c478bd9Sstevel@tonic-gate 	else
29127c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = mountp;
29137c478bd9Sstevel@tonic-gate 	if (getmntany(mntfp, &mp, &mpref) != 0) {
29147c478bd9Sstevel@tonic-gate 		(void) fclose(mntfp);
29157c478bd9Sstevel@tonic-gate 		return (NULL);
29167c478bd9Sstevel@tonic-gate 	}
29177c478bd9Sstevel@tonic-gate 	(void) fclose(mntfp);
29187c478bd9Sstevel@tonic-gate 
29197c478bd9Sstevel@tonic-gate 	return (s_strdup(mp.mnt_special));
29207c478bd9Sstevel@tonic-gate }
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate static char *
29237c478bd9Sstevel@tonic-gate os_to_grubdisk(char *osdisk, int on_bootdev)
29247c478bd9Sstevel@tonic-gate {
29257c478bd9Sstevel@tonic-gate 	FILE *fp;
29267c478bd9Sstevel@tonic-gate 	char *grubdisk;
29277c478bd9Sstevel@tonic-gate 
29287c478bd9Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
29298c1b6884Sszhou 	fp = open_diskmap("");
29307c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
29317c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdisk);
29327c478bd9Sstevel@tonic-gate 		return (NULL);
29337c478bd9Sstevel@tonic-gate 	}
29347c478bd9Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdisk, fp, on_bootdev);
29357c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
29367c478bd9Sstevel@tonic-gate 	return (grubdisk);
29377c478bd9Sstevel@tonic-gate }
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate /*
29407c478bd9Sstevel@tonic-gate  * Check if root is on the boot device
29417c478bd9Sstevel@tonic-gate  * Return 0 (false) on error
29427c478bd9Sstevel@tonic-gate  */
29437c478bd9Sstevel@tonic-gate static int
29447c478bd9Sstevel@tonic-gate menu_on_bootdev(char *menu_root, FILE *fp)
29457c478bd9Sstevel@tonic-gate {
29467c478bd9Sstevel@tonic-gate 	int ret;
29477c478bd9Sstevel@tonic-gate 	char *grubhd, *bootp, *special;
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate 	special = get_special(menu_root);
29507c478bd9Sstevel@tonic-gate 	if (special == NULL)
29517c478bd9Sstevel@tonic-gate 		return (0);
29527c478bd9Sstevel@tonic-gate 	bootp = strstr(special, "p0:boot");
29537c478bd9Sstevel@tonic-gate 	if (bootp)
29547c478bd9Sstevel@tonic-gate 		*bootp = '\0';
29557c478bd9Sstevel@tonic-gate 	grubhd = get_grubdisk(special, fp, 1);
29567c478bd9Sstevel@tonic-gate 	free(special);
29577c478bd9Sstevel@tonic-gate 
29587c478bd9Sstevel@tonic-gate 	if (grubhd == NULL)
29597c478bd9Sstevel@tonic-gate 		return (0);
29607c478bd9Sstevel@tonic-gate 	ret = grubhd[3] == '0';
29617c478bd9Sstevel@tonic-gate 	free(grubhd);
29627c478bd9Sstevel@tonic-gate 	return (ret);
29637c478bd9Sstevel@tonic-gate }
29647c478bd9Sstevel@tonic-gate 
29658c1b6884Sszhou /*
29668c1b6884Sszhou  * look for matching bootadm entry with specified parameters
29678c1b6884Sszhou  * Here are the rules (based on existing usage):
29688c1b6884Sszhou  * - If title is specified, match on title only
29698c1b6884Sszhou  * - Else, match on grubdisk and module (don't care about kernel line).
29708c1b6884Sszhou  *   note that, if root_opt is non-zero, the absence of root line is
29718c1b6884Sszhou  *   considered a match.
29728c1b6884Sszhou  */
29738c1b6884Sszhou static entry_t *
29748c1b6884Sszhou find_boot_entry(menu_t *mp, char *title, char *root, char *module,
29758c1b6884Sszhou     int root_opt, int *entry_num)
29768c1b6884Sszhou {
29778c1b6884Sszhou 	int i;
29788c1b6884Sszhou 	line_t *lp;
29798c1b6884Sszhou 	entry_t *ent;
29808c1b6884Sszhou 
29818c1b6884Sszhou 	/* find matching entry */
29828c1b6884Sszhou 	for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) {
29838c1b6884Sszhou 		lp = ent->start;
29848c1b6884Sszhou 
29858c1b6884Sszhou 		/* first line of entry must be bootadm comment */
29868c1b6884Sszhou 		lp = ent->start;
29878c1b6884Sszhou 		if (lp->flags != BAM_COMMENT || strcmp(lp->arg, BAM_HDR) != 0) {
29888c1b6884Sszhou 			continue;
29898c1b6884Sszhou 		}
29908c1b6884Sszhou 
29918c1b6884Sszhou 		/* advance to title line */
29928c1b6884Sszhou 		lp = lp->next;
29938c1b6884Sszhou 		if (title) {
29948c1b6884Sszhou 			if (lp->flags == BAM_TITLE && lp->arg &&
29958c1b6884Sszhou 			    strcmp(lp->arg, title) == 0)
29968c1b6884Sszhou 				break;
29978c1b6884Sszhou 			continue;	/* check title only */
29988c1b6884Sszhou 		}
29998c1b6884Sszhou 
30008c1b6884Sszhou 		lp = lp->next;	/* advance to root line */
30018c1b6884Sszhou 		if (lp == NULL || strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
30028c1b6884Sszhou 			/* root command found, match grub disk */
30038c1b6884Sszhou 			if (strcmp(lp->arg, root) != 0) {
30048c1b6884Sszhou 				continue;
30058c1b6884Sszhou 			}
30068c1b6884Sszhou 			lp = lp->next;	/* advance to kernel line */
30078c1b6884Sszhou 		} else {
30088c1b6884Sszhou 			/* no root command, see if root is optional */
30098c1b6884Sszhou 			if (root_opt == 0) {
30108c1b6884Sszhou 				continue;
30118c1b6884Sszhou 			}
30128c1b6884Sszhou 		}
30138c1b6884Sszhou 
30148c1b6884Sszhou 		if (lp == NULL || lp->next == NULL) {
30158c1b6884Sszhou 			continue;
30168c1b6884Sszhou 		}
30178c1b6884Sszhou 
30188c1b6884Sszhou 		/* check for matching module entry (failsafe or normal) */
30198c1b6884Sszhou 		lp = lp->next;	/* advance to module line */
30208c1b6884Sszhou 		if (strcmp(lp->cmd, menu_cmds[MODULE_CMD]) != 0 ||
30218c1b6884Sszhou 		    strcmp(lp->arg, module) != 0) {
30228c1b6884Sszhou 			continue;
30238c1b6884Sszhou 		}
30248c1b6884Sszhou 		break;	/* match found */
30258c1b6884Sszhou 	}
30268c1b6884Sszhou 
30278c1b6884Sszhou 	*entry_num = i;
30288c1b6884Sszhou 	return (ent);
30298c1b6884Sszhou }
30308c1b6884Sszhou 
30318c1b6884Sszhou static int
30328c1b6884Sszhou update_boot_entry(menu_t *mp, char *title, char *root, char *kernel,
30338c1b6884Sszhou     char *module, int root_opt)
30348c1b6884Sszhou {
30358c1b6884Sszhou 	int i;
30368c1b6884Sszhou 	entry_t *ent;
30378c1b6884Sszhou 	line_t *lp;
30388c1b6884Sszhou 	char linebuf[BAM_MAXLINE];
30398c1b6884Sszhou 
30408c1b6884Sszhou 	/* note: don't match on title, it's updated on upgrade */
30418c1b6884Sszhou 	ent = find_boot_entry(mp, NULL, root, module, root_opt, &i);
30428c1b6884Sszhou 	if (ent == NULL)
30438c1b6884Sszhou 		return (add_boot_entry(mp, title, root_opt ? NULL : root,
30448c1b6884Sszhou 		    kernel, module));
30458c1b6884Sszhou 
30468c1b6884Sszhou 	/* replace title of exiting entry and delete root line */
30478c1b6884Sszhou 	lp = ent->start;
30488c1b6884Sszhou 	lp = lp->next;	/* title line */
30498c1b6884Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
30508c1b6884Sszhou 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
30518c1b6884Sszhou 	free(lp->arg);
30528c1b6884Sszhou 	free(lp->line);
30538c1b6884Sszhou 	lp->arg = s_strdup(title);
30548c1b6884Sszhou 	lp->line = s_strdup(linebuf);
30558c1b6884Sszhou 
30568c1b6884Sszhou 	lp = lp->next;	/* root line */
30578c1b6884Sszhou 	if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
30588c1b6884Sszhou 		if (root_opt) {		/* root line not needed */
30598c1b6884Sszhou 			line_t *tmp = lp;
30608c1b6884Sszhou 			lp = lp->next;
30618c1b6884Sszhou 			unlink_line(mp, tmp);
30628c1b6884Sszhou 			line_free(tmp);
30638c1b6884Sszhou 		} else
30648c1b6884Sszhou 			lp = lp->next;
30658c1b6884Sszhou 	}
30668c1b6884Sszhou 	return (i);
30678c1b6884Sszhou }
30688c1b6884Sszhou 
30697c478bd9Sstevel@tonic-gate /*ARGSUSED*/
30707c478bd9Sstevel@tonic-gate static error_t
30717c478bd9Sstevel@tonic-gate update_entry(menu_t *mp, char *menu_root, char *opt)
30727c478bd9Sstevel@tonic-gate {
30737c478bd9Sstevel@tonic-gate 	FILE *fp;
30747c478bd9Sstevel@tonic-gate 	int entry;
30757c478bd9Sstevel@tonic-gate 	char *grubdisk, *title, *osdev, *osroot;
30768c1b6884Sszhou 	struct stat sbuf;
30778c1b6884Sszhou 	char failsafe[256];
30787c478bd9Sstevel@tonic-gate 
30797c478bd9Sstevel@tonic-gate 	assert(mp);
30807c478bd9Sstevel@tonic-gate 	assert(opt);
30817c478bd9Sstevel@tonic-gate 
30827c478bd9Sstevel@tonic-gate 	osdev = strtok(opt, ",");
30837c478bd9Sstevel@tonic-gate 	osroot = strtok(NULL, ",");
30847c478bd9Sstevel@tonic-gate 	if (osroot == NULL)
30857c478bd9Sstevel@tonic-gate 		osroot = menu_root;
30867c478bd9Sstevel@tonic-gate 	title = get_title(osroot);
30877c478bd9Sstevel@tonic-gate 
30887c478bd9Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
30898c1b6884Sszhou 	fp = open_diskmap(osroot);
30907c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
30917c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
30927c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
30937c478bd9Sstevel@tonic-gate 	}
30947c478bd9Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdev, fp, menu_on_bootdev(menu_root, fp));
30957c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
30967c478bd9Sstevel@tonic-gate 	if (grubdisk == NULL) {
30977c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
30987c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
30997c478bd9Sstevel@tonic-gate 	}
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 	/* add the entry for normal Solaris */
31028c1b6884Sszhou 	entry = update_boot_entry(mp, title, grubdisk,
31037c478bd9Sstevel@tonic-gate 	    "/platform/i86pc/multiboot",
31048c1b6884Sszhou 	    "/platform/i86pc/boot_archive",
31058c1b6884Sszhou 	    osroot == menu_root);
31067c478bd9Sstevel@tonic-gate 
31077c478bd9Sstevel@tonic-gate 	/* add the entry for failsafe archive */
31088c1b6884Sszhou 	(void) snprintf(failsafe, sizeof (failsafe),
31098c1b6884Sszhou 	    "%s/boot/x86.miniroot-safe", osroot);
31108c1b6884Sszhou 	if (stat(failsafe, &sbuf) == 0)
31118c1b6884Sszhou 		(void) update_boot_entry(mp, "Solaris failsafe", grubdisk,
31127c478bd9Sstevel@tonic-gate 		    "/boot/multiboot kernel/unix -s",
31138c1b6884Sszhou 		    "/boot/x86.miniroot-safe",
31148c1b6884Sszhou 		    osroot == menu_root);
31157c478bd9Sstevel@tonic-gate 	free(grubdisk);
31167c478bd9Sstevel@tonic-gate 
31177c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
31187c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
31197c478bd9Sstevel@tonic-gate 	}
31207c478bd9Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
31217c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
31227c478bd9Sstevel@tonic-gate }
31237c478bd9Sstevel@tonic-gate 
3124b610f78eSvikram static char *
3125b610f78eSvikram read_grub_root(void)
3126b610f78eSvikram {
3127b610f78eSvikram 	FILE *fp;
3128b610f78eSvikram 	struct stat sb;
3129b610f78eSvikram 	char buf[BAM_MAXLINE];
3130b610f78eSvikram 	char *rootstr;
3131b610f78eSvikram 
3132b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
3133b610f78eSvikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
3134b610f78eSvikram 		return (NULL);
3135b610f78eSvikram 	}
3136b610f78eSvikram 
3137b610f78eSvikram 	if (stat(GRUB_root, &sb) != 0) {
3138b610f78eSvikram 		bam_error(MISSING_ROOT_FILE, GRUB_root, strerror(errno));
3139b610f78eSvikram 		return (NULL);
3140b610f78eSvikram 	}
3141b610f78eSvikram 
3142b610f78eSvikram 	fp = fopen(GRUB_root, "r");
3143b610f78eSvikram 	if (fp == NULL) {
3144b610f78eSvikram 		bam_error(OPEN_FAIL, GRUB_root, strerror(errno));
3145b610f78eSvikram 		return (NULL);
3146b610f78eSvikram 	}
3147b610f78eSvikram 
3148b610f78eSvikram 	if (s_fgets(buf, sizeof (buf), fp) == NULL) {
3149b610f78eSvikram 		bam_error(EMPTY_FILE, GRUB_root, strerror(errno));
3150b610f78eSvikram 		(void) fclose(fp);
3151b610f78eSvikram 		return (NULL);
3152b610f78eSvikram 	}
3153b610f78eSvikram 
3154b610f78eSvikram 	/*
3155b610f78eSvikram 	 * Copy buf here as check below may trash the buffer
3156b610f78eSvikram 	 */
3157b610f78eSvikram 	rootstr = s_strdup(buf);
3158b610f78eSvikram 
3159b610f78eSvikram 	if (s_fgets(buf, sizeof (buf), fp) != NULL) {
3160b610f78eSvikram 		bam_error(BAD_ROOT_FILE, GRUB_root);
3161b610f78eSvikram 		free(rootstr);
3162b610f78eSvikram 		rootstr = NULL;
3163b610f78eSvikram 	}
3164b610f78eSvikram 
3165b610f78eSvikram 	(void) fclose(fp);
3166b610f78eSvikram 
3167b610f78eSvikram 	return (rootstr);
3168b610f78eSvikram }
3169b610f78eSvikram 
31708c1b6884Sszhou static void
31718c1b6884Sszhou save_default_entry(menu_t *mp)
31728c1b6884Sszhou {
31738c1b6884Sszhou 	int lineNum, entryNum;
31748c1b6884Sszhou 	int entry = 0;	/* default is 0 */
31758c1b6884Sszhou 	char linebuf[BAM_MAXLINE];
31768c1b6884Sszhou 	line_t *lp = mp->curdefault;
31778c1b6884Sszhou 
31788c1b6884Sszhou 	if (lp)
31798c1b6884Sszhou 		entry = s_strtol(lp->arg);
31808c1b6884Sszhou 
31818c1b6884Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "#%s%d", BAM_OLDDEF, entry);
31828c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
31838c1b6884Sszhou }
31848c1b6884Sszhou 
31858c1b6884Sszhou static void
31868c1b6884Sszhou restore_default_entry(menu_t *mp)
31878c1b6884Sszhou {
31888c1b6884Sszhou 	int entry;
31898c1b6884Sszhou 	char *str;
31908c1b6884Sszhou 	line_t *lp = mp->olddefault;
31918c1b6884Sszhou 
31928c1b6884Sszhou 	if (lp == NULL)
31938c1b6884Sszhou 		return;		/* nothing to restore */
31948c1b6884Sszhou 
31958c1b6884Sszhou 	str = lp->arg + strlen(BAM_OLDDEF);
31968c1b6884Sszhou 	entry = s_strtol(str);
31978c1b6884Sszhou 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
31988c1b6884Sszhou 
31998c1b6884Sszhou 	/* delete saved old default line */
32008c1b6884Sszhou 	mp->olddefault = NULL;
32018c1b6884Sszhou 	unlink_line(mp, lp);
32028c1b6884Sszhou 	line_free(lp);
32038c1b6884Sszhou }
32048c1b6884Sszhou 
32057c478bd9Sstevel@tonic-gate /*
32067c478bd9Sstevel@tonic-gate  * This function is for supporting reboot with args.
32077c478bd9Sstevel@tonic-gate  * The opt value can be:
32087c478bd9Sstevel@tonic-gate  * NULL		delete temp entry, if present
32097c478bd9Sstevel@tonic-gate  * entry=#	switches default entry to 1
32107c478bd9Sstevel@tonic-gate  * else		treated as boot-args and setup a temperary menu entry
32117c478bd9Sstevel@tonic-gate  *		and make it the default
32127c478bd9Sstevel@tonic-gate  */
32137c478bd9Sstevel@tonic-gate #define	REBOOT_TITLE	"Solaris_reboot_transient"
32147c478bd9Sstevel@tonic-gate 
32158c1b6884Sszhou /*ARGSUSED*/
32167c478bd9Sstevel@tonic-gate static error_t
32177c478bd9Sstevel@tonic-gate update_temp(menu_t *mp, char *menupath, char *opt)
32187c478bd9Sstevel@tonic-gate {
32197c478bd9Sstevel@tonic-gate 	int entry;
32207c478bd9Sstevel@tonic-gate 	char *grubdisk, *rootdev;
32217c478bd9Sstevel@tonic-gate 	char kernbuf[1024];
3222b610f78eSvikram 	struct stat sb;
32237c478bd9Sstevel@tonic-gate 
32247c478bd9Sstevel@tonic-gate 	assert(mp);
32257c478bd9Sstevel@tonic-gate 
32268c1b6884Sszhou 	/* If no option, delete exiting reboot menu entry */
32278c1b6884Sszhou 	if (opt == NULL) {
32288c1b6884Sszhou 		entry_t *ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL,
32298c1b6884Sszhou 		    0, &entry);
32308c1b6884Sszhou 		if (ent == NULL)	/* not found is ok */
32318c1b6884Sszhou 			return (BAM_SUCCESS);
32328c1b6884Sszhou 		(void) do_delete(mp, entry);
32338c1b6884Sszhou 		restore_default_entry(mp);
32348c1b6884Sszhou 		return (BAM_WRITE);
32358c1b6884Sszhou 	}
32368c1b6884Sszhou 
32378c1b6884Sszhou 	/* if entry= is specified, set the default entry */
32388c1b6884Sszhou 	if (strncmp(opt, "entry=", strlen("entry=")) == 0 &&
32397c478bd9Sstevel@tonic-gate 	    selector(mp, opt, &entry, NULL) == BAM_SUCCESS) {
32407c478bd9Sstevel@tonic-gate 		/* this is entry=# option */
32417c478bd9Sstevel@tonic-gate 		return (set_global(mp, menu_cmds[DEFAULT_CMD], entry));
32427c478bd9Sstevel@tonic-gate 	}
32437c478bd9Sstevel@tonic-gate 
32447c478bd9Sstevel@tonic-gate 	/*
32457c478bd9Sstevel@tonic-gate 	 * add a new menu entry base on opt and make it the default
3246b610f78eSvikram 	 */
3247b610f78eSvikram 	grubdisk = NULL;
3248b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
3249b610f78eSvikram 		/*
32507c478bd9Sstevel@tonic-gate 		 * 1. First get root disk name from mnttab
32517c478bd9Sstevel@tonic-gate 		 * 2. Translate disk name to grub name
32527c478bd9Sstevel@tonic-gate 		 * 3. Add the new menu entry
32537c478bd9Sstevel@tonic-gate 		 */
32547c478bd9Sstevel@tonic-gate 		rootdev = get_special("/");
32557c478bd9Sstevel@tonic-gate 		if (rootdev) {
32567c478bd9Sstevel@tonic-gate 			grubdisk = os_to_grubdisk(rootdev, 1);
32577c478bd9Sstevel@tonic-gate 			free(rootdev);
32587c478bd9Sstevel@tonic-gate 		}
3259b610f78eSvikram 	} else {
3260b610f78eSvikram 		/*
3261b610f78eSvikram 		 * This is an LU BE. The GRUB_root file
3262b610f78eSvikram 		 * contains entry for GRUB's "root" cmd.
3263b610f78eSvikram 		 */
3264b610f78eSvikram 		grubdisk = read_grub_root();
3265b610f78eSvikram 	}
32667c478bd9Sstevel@tonic-gate 	if (grubdisk == NULL) {
3267b610f78eSvikram 		bam_error(REBOOT_WITH_ARGS_FAILED);
32687c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
32697c478bd9Sstevel@tonic-gate 	}
32707c478bd9Sstevel@tonic-gate 
32717c478bd9Sstevel@tonic-gate 	/* add an entry for Solaris reboot */
32727c478bd9Sstevel@tonic-gate 	(void) snprintf(kernbuf, sizeof (kernbuf),
32737c478bd9Sstevel@tonic-gate 	    "/platform/i86pc/multiboot %s", opt);
32747c478bd9Sstevel@tonic-gate 	entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf,
32757c478bd9Sstevel@tonic-gate 	    "/platform/i86pc/boot_archive");
32767c478bd9Sstevel@tonic-gate 	free(grubdisk);
32777c478bd9Sstevel@tonic-gate 
32787c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
3279b610f78eSvikram 		bam_error(REBOOT_WITH_ARGS_FAILED);
32807c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
32817c478bd9Sstevel@tonic-gate 	}
32828c1b6884Sszhou 
32838c1b6884Sszhou 	save_default_entry(mp);
32847c478bd9Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
32857c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
32867c478bd9Sstevel@tonic-gate }
32877c478bd9Sstevel@tonic-gate 
32887c478bd9Sstevel@tonic-gate static error_t
32897c478bd9Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val)
32907c478bd9Sstevel@tonic-gate {
32917c478bd9Sstevel@tonic-gate 	line_t *lp, *found, *last;
32927c478bd9Sstevel@tonic-gate 	char *cp, *str;
32937c478bd9Sstevel@tonic-gate 	char prefix[BAM_MAXLINE];
32947c478bd9Sstevel@tonic-gate 	size_t len;
32957c478bd9Sstevel@tonic-gate 
32967c478bd9Sstevel@tonic-gate 	assert(mp);
32977c478bd9Sstevel@tonic-gate 	assert(globalcmd);
32987c478bd9Sstevel@tonic-gate 
3299b610f78eSvikram 	if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) {
3300b610f78eSvikram 		if (val < 0 || mp->end == NULL || val > mp->end->entryNum) {
3301b610f78eSvikram 			(void) snprintf(prefix, sizeof (prefix), "%d", val);
3302b610f78eSvikram 			bam_error(INVALID_ENTRY, prefix);
3303b610f78eSvikram 			return (BAM_ERROR);
3304b610f78eSvikram 		}
3305b610f78eSvikram 	}
3306b610f78eSvikram 
33077c478bd9Sstevel@tonic-gate 	found = last = NULL;
33087c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
33097c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
33107c478bd9Sstevel@tonic-gate 			continue;
33117c478bd9Sstevel@tonic-gate 
33127c478bd9Sstevel@tonic-gate 		last = lp; /* track the last global found */
33137c478bd9Sstevel@tonic-gate 
33147c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
33157c478bd9Sstevel@tonic-gate 			bam_error(NO_CMD, lp->lineNum);
33167c478bd9Sstevel@tonic-gate 			continue;
33177c478bd9Sstevel@tonic-gate 		}
33187c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
33197c478bd9Sstevel@tonic-gate 			continue;
33207c478bd9Sstevel@tonic-gate 
33217c478bd9Sstevel@tonic-gate 		if (found) {
33227c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
33237c478bd9Sstevel@tonic-gate 		}
33247c478bd9Sstevel@tonic-gate 		found = lp;
33257c478bd9Sstevel@tonic-gate 	}
33267c478bd9Sstevel@tonic-gate 
33277c478bd9Sstevel@tonic-gate 	if (found == NULL) {
33287c478bd9Sstevel@tonic-gate 		lp = s_calloc(1, sizeof (line_t));
33297c478bd9Sstevel@tonic-gate 		if (last == NULL) {
33307c478bd9Sstevel@tonic-gate 			lp->next = mp->start;
33317c478bd9Sstevel@tonic-gate 			mp->start = lp;
33327c478bd9Sstevel@tonic-gate 			mp->end = (mp->end) ? mp->end : lp;
33337c478bd9Sstevel@tonic-gate 		} else {
33347c478bd9Sstevel@tonic-gate 			lp->next = last->next;
33357c478bd9Sstevel@tonic-gate 			last->next = lp;
33367c478bd9Sstevel@tonic-gate 			if (lp->next == NULL)
33377c478bd9Sstevel@tonic-gate 				mp->end = lp;
33387c478bd9Sstevel@tonic-gate 		}
33397c478bd9Sstevel@tonic-gate 		lp->flags = BAM_GLOBAL; /* other fields not needed for writes */
33407c478bd9Sstevel@tonic-gate 		len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]);
33417c478bd9Sstevel@tonic-gate 		len += 10;	/* val < 10 digits */
33427c478bd9Sstevel@tonic-gate 		lp->line = s_calloc(1, len);
33437c478bd9Sstevel@tonic-gate 		(void) snprintf(lp->line, len, "%s%s%d",
33447c478bd9Sstevel@tonic-gate 		    globalcmd, menu_cmds[SEP_CMD], val);
33457c478bd9Sstevel@tonic-gate 		return (BAM_WRITE);
33467c478bd9Sstevel@tonic-gate 	}
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate 	/*
33497c478bd9Sstevel@tonic-gate 	 * We are changing an existing entry. Retain any prefix whitespace,
33507c478bd9Sstevel@tonic-gate 	 * but overwrite everything else. This preserves tabs added for
33517c478bd9Sstevel@tonic-gate 	 * readability.
33527c478bd9Sstevel@tonic-gate 	 */
33537c478bd9Sstevel@tonic-gate 	str = found->line;
33547c478bd9Sstevel@tonic-gate 	cp = prefix;
33557c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
33567c478bd9Sstevel@tonic-gate 		*(cp++) = *(str++);
33577c478bd9Sstevel@tonic-gate 	*cp = '\0'; /* Terminate prefix */
33587c478bd9Sstevel@tonic-gate 	len = strlen(prefix) + strlen(globalcmd);
33597c478bd9Sstevel@tonic-gate 	len += strlen(menu_cmds[SEP_CMD]) + 10;
33607c478bd9Sstevel@tonic-gate 
33617c478bd9Sstevel@tonic-gate 	free(found->line);
33627c478bd9Sstevel@tonic-gate 	found->line = s_calloc(1, len);
33637c478bd9Sstevel@tonic-gate 	(void) snprintf(found->line, len,
33647c478bd9Sstevel@tonic-gate 		"%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val);
33657c478bd9Sstevel@tonic-gate 
33667c478bd9Sstevel@tonic-gate 	return (BAM_WRITE); /* need a write to menu */
33677c478bd9Sstevel@tonic-gate }
33687c478bd9Sstevel@tonic-gate 
33697c478bd9Sstevel@tonic-gate /*ARGSUSED*/
33707c478bd9Sstevel@tonic-gate static error_t
33717c478bd9Sstevel@tonic-gate set_option(menu_t *mp, char *menu_path, char *opt)
33727c478bd9Sstevel@tonic-gate {
33737c478bd9Sstevel@tonic-gate 	int optnum, optval;
33747c478bd9Sstevel@tonic-gate 	char *val;
33757c478bd9Sstevel@tonic-gate 
33767c478bd9Sstevel@tonic-gate 	assert(mp);
33777c478bd9Sstevel@tonic-gate 	assert(opt);
33787c478bd9Sstevel@tonic-gate 
33797c478bd9Sstevel@tonic-gate 	val = strchr(opt, '=');
33807c478bd9Sstevel@tonic-gate 	if (val == NULL) {
33817c478bd9Sstevel@tonic-gate 		bam_error(INVALID_ENTRY, opt);
33827c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
33837c478bd9Sstevel@tonic-gate 	}
33847c478bd9Sstevel@tonic-gate 
33857c478bd9Sstevel@tonic-gate 	*val = '\0';
33867c478bd9Sstevel@tonic-gate 	if (strcmp(opt, "default") == 0) {
33877c478bd9Sstevel@tonic-gate 		optnum = DEFAULT_CMD;
33887c478bd9Sstevel@tonic-gate 	} else if (strcmp(opt, "timeout") == 0) {
33897c478bd9Sstevel@tonic-gate 		optnum = TIMEOUT_CMD;
33907c478bd9Sstevel@tonic-gate 	} else {
33917c478bd9Sstevel@tonic-gate 		bam_error(INVALID_ENTRY, opt);
33927c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
33937c478bd9Sstevel@tonic-gate 	}
33947c478bd9Sstevel@tonic-gate 
33957c478bd9Sstevel@tonic-gate 	optval = s_strtol(val + 1);
33967c478bd9Sstevel@tonic-gate 	*val = '=';
33977c478bd9Sstevel@tonic-gate 	return (set_global(mp, menu_cmds[optnum], optval));
33987c478bd9Sstevel@tonic-gate }
33997c478bd9Sstevel@tonic-gate 
34007c478bd9Sstevel@tonic-gate /*
34017c478bd9Sstevel@tonic-gate  * The quiet argument suppresses messages. This is used
34027c478bd9Sstevel@tonic-gate  * when invoked in the context of other commands (e.g. list_entry)
34037c478bd9Sstevel@tonic-gate  */
34047c478bd9Sstevel@tonic-gate static error_t
34057c478bd9Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet)
34067c478bd9Sstevel@tonic-gate {
34077c478bd9Sstevel@tonic-gate 	line_t *lp;
34087c478bd9Sstevel@tonic-gate 	char *arg;
34097c478bd9Sstevel@tonic-gate 	int done, ret = BAM_SUCCESS;
34107c478bd9Sstevel@tonic-gate 
34117c478bd9Sstevel@tonic-gate 	assert(mp);
34127c478bd9Sstevel@tonic-gate 	assert(menu_path);
34137c478bd9Sstevel@tonic-gate 	assert(globalcmd);
34147c478bd9Sstevel@tonic-gate 
34157c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
34167c478bd9Sstevel@tonic-gate 		if (!quiet)
34177c478bd9Sstevel@tonic-gate 			bam_error(NO_MENU, menu_path);
34187c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
34197c478bd9Sstevel@tonic-gate 	}
34207c478bd9Sstevel@tonic-gate 
34217c478bd9Sstevel@tonic-gate 	done = 0;
34227c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
34237c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
34247c478bd9Sstevel@tonic-gate 			continue;
34257c478bd9Sstevel@tonic-gate 
34267c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
34277c478bd9Sstevel@tonic-gate 			if (!quiet)
34287c478bd9Sstevel@tonic-gate 				bam_error(NO_CMD, lp->lineNum);
34297c478bd9Sstevel@tonic-gate 			continue;
34307c478bd9Sstevel@tonic-gate 		}
34317c478bd9Sstevel@tonic-gate 
34327c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
34337c478bd9Sstevel@tonic-gate 			continue;
34347c478bd9Sstevel@tonic-gate 
34357c478bd9Sstevel@tonic-gate 		/* Found global. Check for duplicates */
34367c478bd9Sstevel@tonic-gate 		if (done && !quiet) {
34377c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
34387c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
34397c478bd9Sstevel@tonic-gate 		}
34407c478bd9Sstevel@tonic-gate 
34417c478bd9Sstevel@tonic-gate 		arg = lp->arg ? lp->arg : "";
34427c478bd9Sstevel@tonic-gate 		bam_print(GLOBAL_CMD, globalcmd, arg);
34437c478bd9Sstevel@tonic-gate 		done = 1;
34447c478bd9Sstevel@tonic-gate 	}
34457c478bd9Sstevel@tonic-gate 
34467c478bd9Sstevel@tonic-gate 	if (!done && bam_verbose)
34477c478bd9Sstevel@tonic-gate 		bam_print(NO_ENTRY, globalcmd);
34487c478bd9Sstevel@tonic-gate 
34497c478bd9Sstevel@tonic-gate 	return (ret);
34507c478bd9Sstevel@tonic-gate }
34517c478bd9Sstevel@tonic-gate 
34527c478bd9Sstevel@tonic-gate static error_t
34537c478bd9Sstevel@tonic-gate menu_write(char *root, menu_t *mp)
34547c478bd9Sstevel@tonic-gate {
34557c478bd9Sstevel@tonic-gate 	return (list2file(root, MENU_TMP, GRUB_MENU, mp->start));
34567c478bd9Sstevel@tonic-gate }
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate static void
34597c478bd9Sstevel@tonic-gate line_free(line_t *lp)
34607c478bd9Sstevel@tonic-gate {
34617c478bd9Sstevel@tonic-gate 	if (lp == NULL)
34627c478bd9Sstevel@tonic-gate 		return;
34637c478bd9Sstevel@tonic-gate 
34647c478bd9Sstevel@tonic-gate 	if (lp->cmd)
34657c478bd9Sstevel@tonic-gate 		free(lp->cmd);
34667c478bd9Sstevel@tonic-gate 	if (lp->sep)
34677c478bd9Sstevel@tonic-gate 		free(lp->sep);
34687c478bd9Sstevel@tonic-gate 	if (lp->arg)
34697c478bd9Sstevel@tonic-gate 		free(lp->arg);
34707c478bd9Sstevel@tonic-gate 	if (lp->line)
34717c478bd9Sstevel@tonic-gate 		free(lp->line);
34727c478bd9Sstevel@tonic-gate 	free(lp);
34737c478bd9Sstevel@tonic-gate }
34747c478bd9Sstevel@tonic-gate 
34757c478bd9Sstevel@tonic-gate static void
34767c478bd9Sstevel@tonic-gate linelist_free(line_t *start)
34777c478bd9Sstevel@tonic-gate {
34787c478bd9Sstevel@tonic-gate 	line_t *lp;
34797c478bd9Sstevel@tonic-gate 
34807c478bd9Sstevel@tonic-gate 	while (start) {
34817c478bd9Sstevel@tonic-gate 		lp = start;
34827c478bd9Sstevel@tonic-gate 		start = start->next;
34837c478bd9Sstevel@tonic-gate 		line_free(lp);
34847c478bd9Sstevel@tonic-gate 	}
34857c478bd9Sstevel@tonic-gate }
34867c478bd9Sstevel@tonic-gate 
34877c478bd9Sstevel@tonic-gate static void
34887c478bd9Sstevel@tonic-gate filelist_free(filelist_t *flistp)
34897c478bd9Sstevel@tonic-gate {
34907c478bd9Sstevel@tonic-gate 	linelist_free(flistp->head);
34917c478bd9Sstevel@tonic-gate 	flistp->head = NULL;
34927c478bd9Sstevel@tonic-gate 	flistp->tail = NULL;
34937c478bd9Sstevel@tonic-gate }
34947c478bd9Sstevel@tonic-gate 
34957c478bd9Sstevel@tonic-gate static void
34967c478bd9Sstevel@tonic-gate menu_free(menu_t *mp)
34977c478bd9Sstevel@tonic-gate {
34988c1b6884Sszhou 	entry_t *ent, *tmp;
34997c478bd9Sstevel@tonic-gate 	assert(mp);
35007c478bd9Sstevel@tonic-gate 
35017c478bd9Sstevel@tonic-gate 	if (mp->start)
35027c478bd9Sstevel@tonic-gate 		linelist_free(mp->start);
35038c1b6884Sszhou 	ent = mp->entries;
35048c1b6884Sszhou 	while (ent) {
35058c1b6884Sszhou 		tmp = ent;
35068c1b6884Sszhou 		ent = tmp->next;
35078c1b6884Sszhou 		free(tmp);
35088c1b6884Sszhou 	}
35097c478bd9Sstevel@tonic-gate 
35108c1b6884Sszhou 	free(mp);
35117c478bd9Sstevel@tonic-gate }
35127c478bd9Sstevel@tonic-gate 
35137c478bd9Sstevel@tonic-gate /*
35147c478bd9Sstevel@tonic-gate  * Utility routines
35157c478bd9Sstevel@tonic-gate  */
35167c478bd9Sstevel@tonic-gate 
35177c478bd9Sstevel@tonic-gate 
35187c478bd9Sstevel@tonic-gate /*
35197c478bd9Sstevel@tonic-gate  * Returns 0 on success
35207c478bd9Sstevel@tonic-gate  * Any other value indicates an error
35217c478bd9Sstevel@tonic-gate  */
35227c478bd9Sstevel@tonic-gate static int
35237c478bd9Sstevel@tonic-gate exec_cmd(char *cmdline, char *output, int64_t osize)
35247c478bd9Sstevel@tonic-gate {
35257c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
35267c478bd9Sstevel@tonic-gate 	int ret;
35277c478bd9Sstevel@tonic-gate 	FILE *ptr;
35287c478bd9Sstevel@tonic-gate 	size_t len;
35297c478bd9Sstevel@tonic-gate 	sigset_t set;
35307c478bd9Sstevel@tonic-gate 	void (*disp)(int);
35317c478bd9Sstevel@tonic-gate 
35327c478bd9Sstevel@tonic-gate 	/*
35337c478bd9Sstevel@tonic-gate 	 * For security
35347c478bd9Sstevel@tonic-gate 	 * - only absolute paths are allowed
35357c478bd9Sstevel@tonic-gate 	 * - set IFS to space and tab
35367c478bd9Sstevel@tonic-gate 	 */
35377c478bd9Sstevel@tonic-gate 	if (*cmdline != '/') {
35387c478bd9Sstevel@tonic-gate 		bam_error(ABS_PATH_REQ, cmdline);
35397c478bd9Sstevel@tonic-gate 		return (-1);
35407c478bd9Sstevel@tonic-gate 	}
35417c478bd9Sstevel@tonic-gate 	(void) putenv("IFS= \t");
35427c478bd9Sstevel@tonic-gate 
35437c478bd9Sstevel@tonic-gate 	/*
35447c478bd9Sstevel@tonic-gate 	 * We may have been exec'ed with SIGCHLD blocked
35457c478bd9Sstevel@tonic-gate 	 * unblock it here
35467c478bd9Sstevel@tonic-gate 	 */
35477c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
35487c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
35497c478bd9Sstevel@tonic-gate 	if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
35507c478bd9Sstevel@tonic-gate 		bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno));
35517c478bd9Sstevel@tonic-gate 		return (-1);
35527c478bd9Sstevel@tonic-gate 	}
35537c478bd9Sstevel@tonic-gate 
35547c478bd9Sstevel@tonic-gate 	/*
35557c478bd9Sstevel@tonic-gate 	 * Set SIGCHLD disposition to SIG_DFL for popen/pclose
35567c478bd9Sstevel@tonic-gate 	 */
35577c478bd9Sstevel@tonic-gate 	disp = sigset(SIGCHLD, SIG_DFL);
35587c478bd9Sstevel@tonic-gate 	if (disp == SIG_ERR) {
35597c478bd9Sstevel@tonic-gate 		bam_error(FAILED_SIG, strerror(errno));
35607c478bd9Sstevel@tonic-gate 		return (-1);
35617c478bd9Sstevel@tonic-gate 	}
35627c478bd9Sstevel@tonic-gate 	if (disp == SIG_HOLD) {
35637c478bd9Sstevel@tonic-gate 		bam_error(BLOCKED_SIG, cmdline);
35647c478bd9Sstevel@tonic-gate 		return (-1);
35657c478bd9Sstevel@tonic-gate 	}
35667c478bd9Sstevel@tonic-gate 
35677c478bd9Sstevel@tonic-gate 	ptr = popen(cmdline, "r");
35687c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
35697c478bd9Sstevel@tonic-gate 		bam_error(POPEN_FAIL, cmdline, strerror(errno));
35707c478bd9Sstevel@tonic-gate 		return (-1);
35717c478bd9Sstevel@tonic-gate 	}
35727c478bd9Sstevel@tonic-gate 
35737c478bd9Sstevel@tonic-gate 	/*
35747c478bd9Sstevel@tonic-gate 	 * If we simply do a pclose() following a popen(), pclose()
35757c478bd9Sstevel@tonic-gate 	 * will close the reader end of the pipe immediately even
35767c478bd9Sstevel@tonic-gate 	 * if the child process has not started/exited. pclose()
35777c478bd9Sstevel@tonic-gate 	 * does wait for cmd to terminate before returning though.
35787c478bd9Sstevel@tonic-gate 	 * When the executed command writes its output to the pipe
35797c478bd9Sstevel@tonic-gate 	 * there is no reader process and the command dies with
35807c478bd9Sstevel@tonic-gate 	 * SIGPIPE. To avoid this we read repeatedly until read
35817c478bd9Sstevel@tonic-gate 	 * terminates with EOF. This indicates that the command
35827c478bd9Sstevel@tonic-gate 	 * (writer) has closed the pipe and we can safely do a
35837c478bd9Sstevel@tonic-gate 	 * pclose().
35847c478bd9Sstevel@tonic-gate 	 *
35857c478bd9Sstevel@tonic-gate 	 * Since pclose() does wait for the command to exit,
35867c478bd9Sstevel@tonic-gate 	 * we can safely reap the exit status of the command
35877c478bd9Sstevel@tonic-gate 	 * from the value returned by pclose()
35887c478bd9Sstevel@tonic-gate 	 */
35897c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), ptr) != NULL) {
35907c478bd9Sstevel@tonic-gate 		/* if (bam_verbose)  XXX */
35917c478bd9Sstevel@tonic-gate 			bam_print(PRINT_NO_NEWLINE, buf);
35927c478bd9Sstevel@tonic-gate 		if (output && osize > 0) {
35937c478bd9Sstevel@tonic-gate 			(void) snprintf(output, osize, "%s", buf);
35947c478bd9Sstevel@tonic-gate 			len = strlen(buf);
35957c478bd9Sstevel@tonic-gate 			output += len;
35967c478bd9Sstevel@tonic-gate 			osize -= len;
35977c478bd9Sstevel@tonic-gate 		}
35987c478bd9Sstevel@tonic-gate 	}
35997c478bd9Sstevel@tonic-gate 
36007c478bd9Sstevel@tonic-gate 	ret = pclose(ptr);
36017c478bd9Sstevel@tonic-gate 	if (ret == -1) {
36027c478bd9Sstevel@tonic-gate 		bam_error(PCLOSE_FAIL, cmdline, strerror(errno));
36037c478bd9Sstevel@tonic-gate 		return (-1);
36047c478bd9Sstevel@tonic-gate 	}
36057c478bd9Sstevel@tonic-gate 
36067c478bd9Sstevel@tonic-gate 	if (WIFEXITED(ret)) {
36077c478bd9Sstevel@tonic-gate 		return (WEXITSTATUS(ret));
36087c478bd9Sstevel@tonic-gate 	} else {
36097c478bd9Sstevel@tonic-gate 		bam_error(EXEC_FAIL, cmdline, ret);
36107c478bd9Sstevel@tonic-gate 		return (-1);
36117c478bd9Sstevel@tonic-gate 	}
36127c478bd9Sstevel@tonic-gate }
36137c478bd9Sstevel@tonic-gate 
36147c478bd9Sstevel@tonic-gate /*
36157c478bd9Sstevel@tonic-gate  * Since this function returns -1 on error
36167c478bd9Sstevel@tonic-gate  * it cannot be used to convert -1. However,
36177c478bd9Sstevel@tonic-gate  * that is sufficient for what we need.
36187c478bd9Sstevel@tonic-gate  */
36197c478bd9Sstevel@tonic-gate static long
36207c478bd9Sstevel@tonic-gate s_strtol(char *str)
36217c478bd9Sstevel@tonic-gate {
36227c478bd9Sstevel@tonic-gate 	long l;
36237c478bd9Sstevel@tonic-gate 	char *res = NULL;
36247c478bd9Sstevel@tonic-gate 
36257c478bd9Sstevel@tonic-gate 	if (str == NULL) {
36267c478bd9Sstevel@tonic-gate 		return (-1);
36277c478bd9Sstevel@tonic-gate 	}
36287c478bd9Sstevel@tonic-gate 
36297c478bd9Sstevel@tonic-gate 	errno = 0;
36307c478bd9Sstevel@tonic-gate 	l = strtol(str, &res, 10);
36317c478bd9Sstevel@tonic-gate 	if (errno || *res != '\0') {
36327c478bd9Sstevel@tonic-gate 		return (-1);
36337c478bd9Sstevel@tonic-gate 	}
36347c478bd9Sstevel@tonic-gate 
36357c478bd9Sstevel@tonic-gate 	return (l);
36367c478bd9Sstevel@tonic-gate }
36377c478bd9Sstevel@tonic-gate 
36387c478bd9Sstevel@tonic-gate /*
36397c478bd9Sstevel@tonic-gate  * Wrapper around fputs, that adds a newline (since fputs doesn't)
36407c478bd9Sstevel@tonic-gate  */
36417c478bd9Sstevel@tonic-gate static int
36427c478bd9Sstevel@tonic-gate s_fputs(char *str, FILE *fp)
36437c478bd9Sstevel@tonic-gate {
36447c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
36457c478bd9Sstevel@tonic-gate 
36467c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s\n", str);
36477c478bd9Sstevel@tonic-gate 	return (fputs(linebuf, fp));
36487c478bd9Sstevel@tonic-gate }
36497c478bd9Sstevel@tonic-gate 
36507c478bd9Sstevel@tonic-gate /*
36517c478bd9Sstevel@tonic-gate  * Wrapper around fgets, that strips newlines returned by fgets
36527c478bd9Sstevel@tonic-gate  */
36537c478bd9Sstevel@tonic-gate static char *
36547c478bd9Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp)
36557c478bd9Sstevel@tonic-gate {
36567c478bd9Sstevel@tonic-gate 	int n;
36577c478bd9Sstevel@tonic-gate 
36587c478bd9Sstevel@tonic-gate 	buf = fgets(buf, buflen, fp);
36597c478bd9Sstevel@tonic-gate 	if (buf) {
36607c478bd9Sstevel@tonic-gate 		n = strlen(buf);
36617c478bd9Sstevel@tonic-gate 		if (n == buflen - 1 && buf[n-1] != '\n')
36627c478bd9Sstevel@tonic-gate 			bam_error(TOO_LONG, buflen - 1, buf);
36637c478bd9Sstevel@tonic-gate 		buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1];
36647c478bd9Sstevel@tonic-gate 	}
36657c478bd9Sstevel@tonic-gate 
36667c478bd9Sstevel@tonic-gate 	return (buf);
36677c478bd9Sstevel@tonic-gate }
36687c478bd9Sstevel@tonic-gate 
36697c478bd9Sstevel@tonic-gate static void *
36707c478bd9Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz)
36717c478bd9Sstevel@tonic-gate {
36727c478bd9Sstevel@tonic-gate 	void *ptr;
36737c478bd9Sstevel@tonic-gate 
36747c478bd9Sstevel@tonic-gate 	ptr = calloc(nelem, sz);
36757c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
36767c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, nelem*sz);
36777c478bd9Sstevel@tonic-gate 		bam_exit(1);
36787c478bd9Sstevel@tonic-gate 	}
36797c478bd9Sstevel@tonic-gate 	return (ptr);
36807c478bd9Sstevel@tonic-gate }
36817c478bd9Sstevel@tonic-gate 
36827c478bd9Sstevel@tonic-gate static char *
36837c478bd9Sstevel@tonic-gate s_strdup(char *str)
36847c478bd9Sstevel@tonic-gate {
36857c478bd9Sstevel@tonic-gate 	char *ptr;
36867c478bd9Sstevel@tonic-gate 
36877c478bd9Sstevel@tonic-gate 	if (str == NULL)
36887c478bd9Sstevel@tonic-gate 		return (NULL);
36897c478bd9Sstevel@tonic-gate 
36907c478bd9Sstevel@tonic-gate 	ptr = strdup(str);
36917c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
36927c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, strlen(str) + 1);
36937c478bd9Sstevel@tonic-gate 		bam_exit(1);
36947c478bd9Sstevel@tonic-gate 	}
36957c478bd9Sstevel@tonic-gate 	return (ptr);
36967c478bd9Sstevel@tonic-gate }
36977c478bd9Sstevel@tonic-gate 
36987c478bd9Sstevel@tonic-gate /*
36997c478bd9Sstevel@tonic-gate  * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients)
37007c478bd9Sstevel@tonic-gate  * Returns 0 otherwise
37017c478bd9Sstevel@tonic-gate  */
37027c478bd9Sstevel@tonic-gate static int
37037c478bd9Sstevel@tonic-gate is_amd64(void)
37047c478bd9Sstevel@tonic-gate {
37057c478bd9Sstevel@tonic-gate 	static int amd64 = -1;
37067c478bd9Sstevel@tonic-gate 	char isabuf[257];	/* from sysinfo(2) manpage */
37077c478bd9Sstevel@tonic-gate 
37087c478bd9Sstevel@tonic-gate 	if (amd64 != -1)
37097c478bd9Sstevel@tonic-gate 		return (amd64);
37107c478bd9Sstevel@tonic-gate 
37117c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 &&
37127c478bd9Sstevel@tonic-gate 	    strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0)
37137c478bd9Sstevel@tonic-gate 		amd64 = 1;
37147c478bd9Sstevel@tonic-gate 	else if (strstr(isabuf, "i386") == NULL)
37157c478bd9Sstevel@tonic-gate 		amd64 = 1;		/* diskless server */
37167c478bd9Sstevel@tonic-gate 	else
37177c478bd9Sstevel@tonic-gate 		amd64 = 0;
37187c478bd9Sstevel@tonic-gate 
37197c478bd9Sstevel@tonic-gate 	return (amd64);
37207c478bd9Sstevel@tonic-gate }
37217c478bd9Sstevel@tonic-gate 
37227c478bd9Sstevel@tonic-gate static void
37237c478bd9Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s)
37247c478bd9Sstevel@tonic-gate {
37257c478bd9Sstevel@tonic-gate 	line_t *lp;
37267c478bd9Sstevel@tonic-gate 
37277c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
37287c478bd9Sstevel@tonic-gate 	lp->line = s_strdup(s);
37297c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL)
37307c478bd9Sstevel@tonic-gate 		flistp->head = lp;
37317c478bd9Sstevel@tonic-gate 	else
37327c478bd9Sstevel@tonic-gate 		flistp->tail->next = lp;
37337c478bd9Sstevel@tonic-gate 	flistp->tail = lp;
37347c478bd9Sstevel@tonic-gate }
3735