xref: /illumos-gate/usr/src/cmd/boot/bootadm/bootadm.c (revision d876c67d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5fbac2b2bSvikram  * Common Development and Distribution License (the "License").
6fbac2b2bSvikram  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*d876c67dSjg  * Copyright 2008 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>
532449e17fSsherrym #include <utime.h>
547c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
557c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
5658091fd8Ssetje #include <sys/param.h>
57986fd29aSsetje 
58986fd29aSsetje #if !defined(_OPB)
592449e17fSsherrym #include <sys/ucode.h>
602449e17fSsherrym #endif
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <pwd.h>
637c478bd9Sstevel@tonic-gate #include <grp.h>
647c478bd9Sstevel@tonic-gate #include <device_info.h>
657c478bd9Sstevel@tonic-gate #include <locale.h>
667c478bd9Sstevel@tonic-gate #include <assert.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #include "message.h"
69ae115bc7Smrj #include "bootadm.h"
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
727c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
737c478bd9Sstevel@tonic-gate #endif	/* TEXT_DOMAIN */
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /* Type definitions */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /* Primary subcmds */
787c478bd9Sstevel@tonic-gate typedef enum {
797c478bd9Sstevel@tonic-gate 	BAM_MENU = 3,
807c478bd9Sstevel@tonic-gate 	BAM_ARCHIVE
817c478bd9Sstevel@tonic-gate } subcmd_t;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate typedef enum {
847c478bd9Sstevel@tonic-gate     OPT_ABSENT = 0,	/* No option */
857c478bd9Sstevel@tonic-gate     OPT_REQ,		/* option required */
867c478bd9Sstevel@tonic-gate     OPT_OPTIONAL	/* option may or may not be present */
877c478bd9Sstevel@tonic-gate } option_t;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate typedef struct {
907c478bd9Sstevel@tonic-gate 	char	*subcmd;
917c478bd9Sstevel@tonic-gate 	option_t option;
927c478bd9Sstevel@tonic-gate 	error_t (*handler)();
931a97e40eSvikram 	int	unpriv;			/* is this an unprivileged command */
947c478bd9Sstevel@tonic-gate } subcmd_defn_t;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define	LINE_INIT	0	/* lineNum initial value */
977c478bd9Sstevel@tonic-gate #define	ENTRY_INIT	-1	/* entryNum initial value */
987c478bd9Sstevel@tonic-gate #define	ALL_ENTRIES	-2	/* selects all boot entries */
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #define	GRUB_DIR		"/boot/grub"
1017c478bd9Sstevel@tonic-gate #define	GRUB_MENU		"/boot/grub/menu.lst"
1027c478bd9Sstevel@tonic-gate #define	MENU_TMP		"/boot/grub/menu.lst.tmp"
1037c478bd9Sstevel@tonic-gate #define	RAMDISK_SPECIAL		"/ramdisk"
10440541d5dSvikram #define	STUBBOOT		"/stubboot"
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /* lock related */
1077c478bd9Sstevel@tonic-gate #define	BAM_LOCK_FILE		"/var/run/bootadm.lock"
1087c478bd9Sstevel@tonic-gate #define	LOCK_FILE_PERMS		(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
1097c478bd9Sstevel@tonic-gate 
110986fd29aSsetje #define	CREATE_RAMDISK		"boot/solaris/bin/create_ramdisk"
111986fd29aSsetje #define	CREATE_DISKMAP		"boot/solaris/bin/create_diskmap"
112986fd29aSsetje #define	EXTRACT_BOOT_FILELIST	"boot/solaris/bin/extract_boot_filelist"
1137c478bd9Sstevel@tonic-gate #define	GRUBDISK_MAP		"/var/run/solaris_grubdisk.map"
1147c478bd9Sstevel@tonic-gate 
115b610f78eSvikram #define	GRUB_slice		"/etc/lu/GRUB_slice"
116b610f78eSvikram #define	GRUB_root		"/etc/lu/GRUB_root"
117b610f78eSvikram #define	GRUB_backup_menu	"/etc/lu/GRUB_backup_menu"
118b610f78eSvikram #define	GRUB_slice_mntpt	"/tmp/GRUB_slice_mntpt"
119b610f78eSvikram #define	LU_ACTIVATE_FILE	"/etc/lu/DelayUpdate/activate.sh"
120fbac2b2bSvikram #define	GRUB_fdisk		"/etc/lu/GRUB_fdisk"
121fbac2b2bSvikram #define	GRUB_fdisk_target	"/etc/lu/GRUB_fdisk_target"
122b610f78eSvikram 
123b610f78eSvikram #define	INSTALLGRUB		"/sbin/installgrub"
124b610f78eSvikram #define	STAGE1			"/boot/grub/stage1"
125b610f78eSvikram #define	STAGE2			"/boot/grub/stage2"
126b610f78eSvikram 
12798892a30Snadkarni /*
12898892a30Snadkarni  * The following two defines are used to detect and create the correct
12998892a30Snadkarni  * boot archive  when safemode patching is underway.  LOFS_PATCH_FILE is a
13098892a30Snadkarni  * contracted private interface between bootadm and the install
13198892a30Snadkarni  * consolidation.  It is set by pdo.c when a patch with SUNW_PATCH_SAFEMODE
13298892a30Snadkarni  * is applied.
13398892a30Snadkarni  */
13498892a30Snadkarni 
13598892a30Snadkarni #define	LOFS_PATCH_FILE		"/var/run/.patch_loopback_mode"
13698892a30Snadkarni #define	LOFS_PATCH_MNT		"/var/run/.patch_root_loopbackmnt"
13798892a30Snadkarni 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * Default file attributes
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_MODE	0644	/* default permissions */
1427c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_UID		0	/* user root */
1437c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_GID		3	/* group sys */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * Menu related
1477c478bd9Sstevel@tonic-gate  * menu_cmd_t and menu_cmds must be kept in sync
1487c478bd9Sstevel@tonic-gate  */
149ae115bc7Smrj char *menu_cmds[] = {
1507c478bd9Sstevel@tonic-gate 	"default",	/* DEFAULT_CMD */
1517c478bd9Sstevel@tonic-gate 	"timeout",	/* TIMEOUT_CMD */
1527c478bd9Sstevel@tonic-gate 	"title",	/* TITLE_CMD */
1537c478bd9Sstevel@tonic-gate 	"root",		/* ROOT_CMD */
1547c478bd9Sstevel@tonic-gate 	"kernel",	/* KERNEL_CMD */
155ae115bc7Smrj 	"kernel$",	/* KERNEL_DOLLAR_CMD */
1567c478bd9Sstevel@tonic-gate 	"module",	/* MODULE_CMD */
157ae115bc7Smrj 	"module$",	/* MODULE_DOLLAR_CMD */
1587c478bd9Sstevel@tonic-gate 	" ",		/* SEP_CMD */
1597c478bd9Sstevel@tonic-gate 	"#",		/* COMMENT_CMD */
160ae115bc7Smrj 	"chainloader",	/* CHAINLOADER_CMD */
161ae115bc7Smrj 	"args",		/* ARGS_CMD */
1627c478bd9Sstevel@tonic-gate 	NULL
1637c478bd9Sstevel@tonic-gate };
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate #define	OPT_ENTRY_NUM	"entry"
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * archive related
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate typedef struct {
1717c478bd9Sstevel@tonic-gate 	line_t *head;
1727c478bd9Sstevel@tonic-gate 	line_t *tail;
1737c478bd9Sstevel@tonic-gate } filelist_t;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate #define	BOOT_FILE_LIST	"boot/solaris/filelist.ramdisk"
1767c478bd9Sstevel@tonic-gate #define	ETC_FILE_LIST	"etc/boot/solaris/filelist.ramdisk"
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate #define	FILE_STAT	"boot/solaris/filestat.ramdisk"
1797c478bd9Sstevel@tonic-gate #define	FILE_STAT_TMP	"boot/solaris/filestat.ramdisk.tmp"
1807c478bd9Sstevel@tonic-gate #define	DIR_PERMS	(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
1817c478bd9Sstevel@tonic-gate #define	FILE_STAT_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /* Globals */
184ae115bc7Smrj int bam_verbose;
185ae115bc7Smrj int bam_force;
1867c478bd9Sstevel@tonic-gate static char *prog;
1877c478bd9Sstevel@tonic-gate static subcmd_t bam_cmd;
1887c478bd9Sstevel@tonic-gate static char *bam_root;
1897c478bd9Sstevel@tonic-gate static int bam_rootlen;
1907c478bd9Sstevel@tonic-gate static int bam_root_readonly;
19140541d5dSvikram static int bam_alt_root;
1927c478bd9Sstevel@tonic-gate static char *bam_subcmd;
1937c478bd9Sstevel@tonic-gate static char *bam_opt;
1947c478bd9Sstevel@tonic-gate static int bam_debug;
1957c478bd9Sstevel@tonic-gate static char **bam_argv;
1967c478bd9Sstevel@tonic-gate static int bam_argc;
1977c478bd9Sstevel@tonic-gate static int bam_check;
1987c478bd9Sstevel@tonic-gate static int bam_smf_check;
1997c478bd9Sstevel@tonic-gate static int bam_lock_fd = -1;
2007c478bd9Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/";
201b610f78eSvikram static int bam_update_all;
202*d876c67dSjg static int bam_alt_platform;
203*d876c67dSjg static char *bam_platform;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /* function prototypes */
206986fd29aSsetje static void parse_args_internal(int, char *[]);
207986fd29aSsetje static void parse_args(int, char *argv[]);
208986fd29aSsetje static error_t bam_menu(char *, char *, int, char *[]);
209986fd29aSsetje static error_t bam_archive(char *, char *);
2107c478bd9Sstevel@tonic-gate 
211986fd29aSsetje static void bam_print(char *, ...);
212986fd29aSsetje static void bam_exit(int);
2137c478bd9Sstevel@tonic-gate static void bam_lock(void);
2147c478bd9Sstevel@tonic-gate static void bam_unlock(void);
2157c478bd9Sstevel@tonic-gate 
216986fd29aSsetje static int exec_cmd(char *, filelist_t *);
217986fd29aSsetje static error_t read_globals(menu_t *, char *, char *, int);
218986fd29aSsetje 
219986fd29aSsetje static menu_t *menu_read(char *);
220986fd29aSsetje static error_t menu_write(char *, menu_t *);
221986fd29aSsetje static void linelist_free(line_t *);
222986fd29aSsetje static void menu_free(menu_t *);
223986fd29aSsetje static void line_free(line_t *);
224986fd29aSsetje static void filelist_free(filelist_t *);
225986fd29aSsetje static error_t list2file(char *, char *, char *, line_t *);
226986fd29aSsetje static error_t list_entry(menu_t *, char *, char *);
227986fd29aSsetje static error_t delete_all_entries(menu_t *, char *, char *);
228986fd29aSsetje static error_t update_entry(menu_t *, char *, char *);
229986fd29aSsetje static error_t update_temp(menu_t *, char *, char *);
230986fd29aSsetje 
231986fd29aSsetje static error_t update_archive(char *, char *);
232986fd29aSsetje static error_t list_archive(char *, char *);
233986fd29aSsetje static error_t update_all(char *, char *);
234986fd29aSsetje static error_t read_list(char *, filelist_t *);
235986fd29aSsetje static error_t set_global(menu_t *, char *, int);
236986fd29aSsetje static error_t set_option(menu_t *, char *, char *);
237986fd29aSsetje static error_t set_kernel(menu_t *, menu_cmd_t, char *, char *, size_t);
238986fd29aSsetje static char *expand_path(const char *);
239986fd29aSsetje 
240986fd29aSsetje static long s_strtol(char *);
241986fd29aSsetje static int s_fputs(char *, FILE *);
242986fd29aSsetje 
243986fd29aSsetje static char *s_strdup(char *);
2447c478bd9Sstevel@tonic-gate static int is_readonly(char *);
2457c478bd9Sstevel@tonic-gate static int is_amd64(void);
246986fd29aSsetje static int is_sun4u(void);
247986fd29aSsetje static int is_sun4v(void);
2487c478bd9Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *);
2497c478bd9Sstevel@tonic-gate 
250986fd29aSsetje #if !defined(_OPB)
2512449e17fSsherrym static void ucode_install();
2522449e17fSsherrym #endif
2532449e17fSsherrym 
2547c478bd9Sstevel@tonic-gate /* Menu related sub commands */
2557c478bd9Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = {
2561a97e40eSvikram 	"set_option",		OPT_OPTIONAL,	set_option, 0,	/* PUB */
2571a97e40eSvikram 	"list_entry",		OPT_OPTIONAL,	list_entry, 1,	/* PUB */
2581a97e40eSvikram 	"delete_all_entries",	OPT_ABSENT,	delete_all_entries, 0, /* PVT */
2591a97e40eSvikram 	"update_entry",		OPT_REQ,	update_entry, 0, /* menu */
2601a97e40eSvikram 	"update_temp",		OPT_OPTIONAL,	update_temp, 0,	/* reboot */
261ae115bc7Smrj 	"upgrade",		OPT_ABSENT,	upgrade_menu, 0, /* menu */
2621a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
2637c478bd9Sstevel@tonic-gate };
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate /* Archive related sub commands */
2667c478bd9Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = {
2671a97e40eSvikram 	"update",		OPT_ABSENT,	update_archive, 0, /* PUB */
2681a97e40eSvikram 	"update_all",		OPT_ABSENT,	update_all, 0,	/* PVT */
2691a97e40eSvikram 	"list",			OPT_OPTIONAL,	list_archive, 1, /* PUB */
2701a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
2717c478bd9Sstevel@tonic-gate };
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate static struct {
2747c478bd9Sstevel@tonic-gate 	nvlist_t *new_nvlp;
2757c478bd9Sstevel@tonic-gate 	nvlist_t *old_nvlp;
2767c478bd9Sstevel@tonic-gate 	int need_update;
2777c478bd9Sstevel@tonic-gate } walk_arg;
2787c478bd9Sstevel@tonic-gate 
27958091fd8Ssetje 
2809632cfadSsetje struct safefile {
28158091fd8Ssetje 	char *name;
28258091fd8Ssetje 	struct safefile *next;
28358091fd8Ssetje };
28458091fd8Ssetje 
285ae115bc7Smrj static struct safefile *safefiles = NULL;
28658091fd8Ssetje #define	NEED_UPDATE_FILE "/etc/svc/volatile/boot_archive_needs_update"
28758091fd8Ssetje 
2887c478bd9Sstevel@tonic-gate static void
2897c478bd9Sstevel@tonic-gate usage(void)
2907c478bd9Sstevel@tonic-gate {
2917c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "USAGE:\n");
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	/* archive usage */
295*d876c67dSjg 	(void) fprintf(stderr,
296*d876c67dSjg 	    "\t%s update-archive [-vn] [-R altroot [-p platform>]]\n", prog);
297*d876c67dSjg 	(void) fprintf(stderr,
298*d876c67dSjg 	    "\t%s list-archive [-R altroot [-p platform>]]\n", prog);
299986fd29aSsetje #if !defined(_OPB)
3007c478bd9Sstevel@tonic-gate 	/* x86 only */
3017c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog);
3027c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog);
3037c478bd9Sstevel@tonic-gate #endif
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate int
3077c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	error_t ret;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3127c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
3157c478bd9Sstevel@tonic-gate 		prog = argv[0];
3167c478bd9Sstevel@tonic-gate 	} else {
3177c478bd9Sstevel@tonic-gate 		prog++;
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	/*
3227c478bd9Sstevel@tonic-gate 	 * Don't depend on caller's umask
3237c478bd9Sstevel@tonic-gate 	 */
3247c478bd9Sstevel@tonic-gate 	(void) umask(0022);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	switch (bam_cmd) {
3297c478bd9Sstevel@tonic-gate 		case BAM_MENU:
3307c478bd9Sstevel@tonic-gate 			ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv);
3317c478bd9Sstevel@tonic-gate 			break;
3327c478bd9Sstevel@tonic-gate 		case BAM_ARCHIVE:
3337c478bd9Sstevel@tonic-gate 			ret = bam_archive(bam_subcmd, bam_opt);
3347c478bd9Sstevel@tonic-gate 			break;
3357c478bd9Sstevel@tonic-gate 		default:
3367c478bd9Sstevel@tonic-gate 			usage();
3377c478bd9Sstevel@tonic-gate 			bam_exit(1);
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS)
3417c478bd9Sstevel@tonic-gate 		bam_exit(1);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	bam_unlock();
3447c478bd9Sstevel@tonic-gate 	return (0);
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /*
3487c478bd9Sstevel@tonic-gate  * Equivalence of public and internal commands:
3497c478bd9Sstevel@tonic-gate  *	update-archive  -- -a update
3507c478bd9Sstevel@tonic-gate  *	list-archive	-- -a list
3517c478bd9Sstevel@tonic-gate  *	set-menu	-- -m set_option
3527c478bd9Sstevel@tonic-gate  *	list-menu	-- -m list_entry
3537c478bd9Sstevel@tonic-gate  *	update-menu	-- -m update_entry
3547c478bd9Sstevel@tonic-gate  */
3557c478bd9Sstevel@tonic-gate static struct cmd_map {
3567c478bd9Sstevel@tonic-gate 	char *bam_cmdname;
3577c478bd9Sstevel@tonic-gate 	int bam_cmd;
3587c478bd9Sstevel@tonic-gate 	char *bam_subcmd;
3597c478bd9Sstevel@tonic-gate } cmd_map[] = {
3607c478bd9Sstevel@tonic-gate 	{ "update-archive",	BAM_ARCHIVE,	"update"},
3617c478bd9Sstevel@tonic-gate 	{ "list-archive",	BAM_ARCHIVE,	"list"},
3627c478bd9Sstevel@tonic-gate 	{ "set-menu",		BAM_MENU,	"set_option"},
3637c478bd9Sstevel@tonic-gate 	{ "list-menu",		BAM_MENU,	"list_entry"},
3647c478bd9Sstevel@tonic-gate 	{ "update-menu",	BAM_MENU,	"update_entry"},
3657c478bd9Sstevel@tonic-gate 	{ NULL,			0,		NULL}
3667c478bd9Sstevel@tonic-gate };
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate /*
3697c478bd9Sstevel@tonic-gate  * Commands syntax published in bootadm(1M) are parsed here
3707c478bd9Sstevel@tonic-gate  */
3717c478bd9Sstevel@tonic-gate static void
3727c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[])
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	struct cmd_map *cmp = cmd_map;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/* command conforming to the final spec */
3777c478bd9Sstevel@tonic-gate 	if (argc > 1 && argv[1][0] != '-') {
3787c478bd9Sstevel@tonic-gate 		/*
3797c478bd9Sstevel@tonic-gate 		 * Map commands to internal table.
3807c478bd9Sstevel@tonic-gate 		 */
3817c478bd9Sstevel@tonic-gate 		while (cmp->bam_cmdname) {
3827c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], cmp->bam_cmdname) == 0) {
3837c478bd9Sstevel@tonic-gate 				bam_cmd = cmp->bam_cmd;
3847c478bd9Sstevel@tonic-gate 				bam_subcmd = cmp->bam_subcmd;
3857c478bd9Sstevel@tonic-gate 				break;
3867c478bd9Sstevel@tonic-gate 			}
3877c478bd9Sstevel@tonic-gate 			cmp++;
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 		if (cmp->bam_cmdname == NULL) {
3907c478bd9Sstevel@tonic-gate 			usage();
3917c478bd9Sstevel@tonic-gate 			bam_exit(1);
3927c478bd9Sstevel@tonic-gate 		}
3937c478bd9Sstevel@tonic-gate 		argc--;
3947c478bd9Sstevel@tonic-gate 		argv++;
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	parse_args_internal(argc, argv);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate /*
4017c478bd9Sstevel@tonic-gate  * A combination of public and private commands are parsed here.
4027c478bd9Sstevel@tonic-gate  * The internal syntax and the corresponding functionality are:
4037c478bd9Sstevel@tonic-gate  *	-a update	-- update-archive
4047c478bd9Sstevel@tonic-gate  *	-a list		-- list-archive
4057c478bd9Sstevel@tonic-gate  *	-a update-all	-- (reboot to sync all mounted OS archive)
4067c478bd9Sstevel@tonic-gate  *	-m update_entry	-- update-menu
4077c478bd9Sstevel@tonic-gate  *	-m list_entry	-- list-menu
4087c478bd9Sstevel@tonic-gate  *	-m update_temp	-- (reboot -- [boot-args])
4097c478bd9Sstevel@tonic-gate  *	-m delete_all_entries -- (called from install)
4107c478bd9Sstevel@tonic-gate  */
4117c478bd9Sstevel@tonic-gate static void
4127c478bd9Sstevel@tonic-gate parse_args_internal(int argc, char *argv[])
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	int c, error;
4157c478bd9Sstevel@tonic-gate 	extern char *optarg;
4167c478bd9Sstevel@tonic-gate 	extern int optind, opterr;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	/* Suppress error message from getopt */
4197c478bd9Sstevel@tonic-gate 	opterr = 0;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	error = 0;
422*d876c67dSjg 	while ((c = getopt(argc, argv, "a:d:fm:no:vCR:p:")) != -1) {
4237c478bd9Sstevel@tonic-gate 		switch (c) {
4247c478bd9Sstevel@tonic-gate 		case 'a':
4257c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
4267c478bd9Sstevel@tonic-gate 				error = 1;
4277c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
4287c478bd9Sstevel@tonic-gate 			}
4297c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_ARCHIVE;
4307c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
4317c478bd9Sstevel@tonic-gate 			break;
4327c478bd9Sstevel@tonic-gate 		case 'd':
4337c478bd9Sstevel@tonic-gate 			if (bam_debug) {
4347c478bd9Sstevel@tonic-gate 				error = 1;
4357c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4367c478bd9Sstevel@tonic-gate 			}
4377c478bd9Sstevel@tonic-gate 			bam_debug = s_strtol(optarg);
4387c478bd9Sstevel@tonic-gate 			break;
4397c478bd9Sstevel@tonic-gate 		case 'f':
4407c478bd9Sstevel@tonic-gate 			if (bam_force) {
4417c478bd9Sstevel@tonic-gate 				error = 1;
4427c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4437c478bd9Sstevel@tonic-gate 			}
4447c478bd9Sstevel@tonic-gate 			bam_force = 1;
4457c478bd9Sstevel@tonic-gate 			break;
4467c478bd9Sstevel@tonic-gate 		case 'm':
4477c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
4487c478bd9Sstevel@tonic-gate 				error = 1;
4497c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
4507c478bd9Sstevel@tonic-gate 			}
4517c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_MENU;
4527c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
4537c478bd9Sstevel@tonic-gate 			break;
4547c478bd9Sstevel@tonic-gate 		case 'n':
4557c478bd9Sstevel@tonic-gate 			if (bam_check) {
4567c478bd9Sstevel@tonic-gate 				error = 1;
4577c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4587c478bd9Sstevel@tonic-gate 			}
4597c478bd9Sstevel@tonic-gate 			bam_check = 1;
4607c478bd9Sstevel@tonic-gate 			break;
4617c478bd9Sstevel@tonic-gate 		case 'o':
4627c478bd9Sstevel@tonic-gate 			if (bam_opt) {
4637c478bd9Sstevel@tonic-gate 				error = 1;
4647c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4657c478bd9Sstevel@tonic-gate 			}
4667c478bd9Sstevel@tonic-gate 			bam_opt = optarg;
4677c478bd9Sstevel@tonic-gate 			break;
4687c478bd9Sstevel@tonic-gate 		case 'v':
4697c478bd9Sstevel@tonic-gate 			if (bam_verbose) {
4707c478bd9Sstevel@tonic-gate 				error = 1;
4717c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4727c478bd9Sstevel@tonic-gate 			}
4737c478bd9Sstevel@tonic-gate 			bam_verbose = 1;
4747c478bd9Sstevel@tonic-gate 			break;
4758c1b6884Sszhou 		case 'C':
4768c1b6884Sszhou 			bam_smf_check = 1;
4778c1b6884Sszhou 			break;
4787c478bd9Sstevel@tonic-gate 		case 'R':
4797c478bd9Sstevel@tonic-gate 			if (bam_root) {
4807c478bd9Sstevel@tonic-gate 				error = 1;
4817c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4827c478bd9Sstevel@tonic-gate 				break;
4837c478bd9Sstevel@tonic-gate 			} else if (realpath(optarg, rootbuf) == NULL) {
4847c478bd9Sstevel@tonic-gate 				error = 1;
4857c478bd9Sstevel@tonic-gate 				bam_error(CANT_RESOLVE, optarg,
4867c478bd9Sstevel@tonic-gate 				    strerror(errno));
4877c478bd9Sstevel@tonic-gate 				break;
4887c478bd9Sstevel@tonic-gate 			}
48940541d5dSvikram 			bam_alt_root = 1;
4907c478bd9Sstevel@tonic-gate 			bam_root = rootbuf;
4917c478bd9Sstevel@tonic-gate 			bam_rootlen = strlen(rootbuf);
4927c478bd9Sstevel@tonic-gate 			break;
493*d876c67dSjg 		case 'p':
494*d876c67dSjg 			bam_alt_platform = 1;
495*d876c67dSjg 			bam_platform = optarg;
496*d876c67dSjg 			if ((strcmp(bam_platform, "i86pc") != 0) &&
497*d876c67dSjg 			    (strcmp(bam_platform, "sun4u") != 0) &&
498*d876c67dSjg 			    (strcmp(bam_platform, "sun4v") != 0)) {
499*d876c67dSjg 				error = 1;
500*d876c67dSjg 				bam_error(INVALID_PLAT, bam_platform);
501*d876c67dSjg 			}
502*d876c67dSjg 			break;
5037c478bd9Sstevel@tonic-gate 		case '?':
5047c478bd9Sstevel@tonic-gate 			error = 1;
5057c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, optopt);
5067c478bd9Sstevel@tonic-gate 			break;
5077c478bd9Sstevel@tonic-gate 		default :
5087c478bd9Sstevel@tonic-gate 			error = 1;
5097c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, c);
5107c478bd9Sstevel@tonic-gate 			break;
5117c478bd9Sstevel@tonic-gate 		}
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
514*d876c67dSjg 	/*
515*d876c67dSjg 	 * An alternate platform requires an alternate root
516*d876c67dSjg 	 */
517*d876c67dSjg 	if (bam_alt_platform && bam_alt_root == 0) {
518*d876c67dSjg 		usage();
519*d876c67dSjg 		bam_exit(0);
520*d876c67dSjg 	}
521*d876c67dSjg 
5227c478bd9Sstevel@tonic-gate 	/*
5237c478bd9Sstevel@tonic-gate 	 * A command option must be specfied
5247c478bd9Sstevel@tonic-gate 	 */
5257c478bd9Sstevel@tonic-gate 	if (!bam_cmd) {
5267c478bd9Sstevel@tonic-gate 		if (bam_opt && strcmp(bam_opt, "all") == 0) {
5277c478bd9Sstevel@tonic-gate 			usage();
5287c478bd9Sstevel@tonic-gate 			bam_exit(0);
5297c478bd9Sstevel@tonic-gate 		}
5307c478bd9Sstevel@tonic-gate 		bam_error(NEED_CMD);
5317c478bd9Sstevel@tonic-gate 		error = 1;
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	if (error) {
5357c478bd9Sstevel@tonic-gate 		usage();
5367c478bd9Sstevel@tonic-gate 		bam_exit(1);
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	if (optind > argc) {
5407c478bd9Sstevel@tonic-gate 		bam_error(INT_ERROR, "parse_args");
5417c478bd9Sstevel@tonic-gate 		bam_exit(1);
5427c478bd9Sstevel@tonic-gate 	} else if (optind < argc) {
5437c478bd9Sstevel@tonic-gate 		bam_argv = &argv[optind];
5447c478bd9Sstevel@tonic-gate 		bam_argc = argc - optind;
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/*
5487c478bd9Sstevel@tonic-gate 	 * -n implies verbose mode
5497c478bd9Sstevel@tonic-gate 	 */
5507c478bd9Sstevel@tonic-gate 	if (bam_check)
5517c478bd9Sstevel@tonic-gate 		bam_verbose = 1;
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate static error_t
5557c478bd9Sstevel@tonic-gate check_subcmd_and_options(
5567c478bd9Sstevel@tonic-gate 	char *subcmd,
5577c478bd9Sstevel@tonic-gate 	char *opt,
5587c478bd9Sstevel@tonic-gate 	subcmd_defn_t *table,
5597c478bd9Sstevel@tonic-gate 	error_t (**fp)())
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate 	int i;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	if (subcmd == NULL) {
5647c478bd9Sstevel@tonic-gate 		bam_error(NEED_SUBCMD);
5657c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 
5681a97e40eSvikram 	if (bam_argc != 0 || bam_argv) {
5691a97e40eSvikram 		if (strcmp(subcmd, "set_option") != 0 || bam_argc != 1) {
5701a97e40eSvikram 			bam_error(TRAILING_ARGS);
5711a97e40eSvikram 			usage();
5721a97e40eSvikram 			return (BAM_ERROR);
5731a97e40eSvikram 		}
5741a97e40eSvikram 	}
5751a97e40eSvikram 
5767c478bd9Sstevel@tonic-gate 	if (bam_root == NULL) {
5777c478bd9Sstevel@tonic-gate 		bam_root = rootbuf;
5787c478bd9Sstevel@tonic-gate 		bam_rootlen = 1;
5797c478bd9Sstevel@tonic-gate 	}
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	/* verify that subcmd is valid */
5827c478bd9Sstevel@tonic-gate 	for (i = 0; table[i].subcmd != NULL; i++) {
5837c478bd9Sstevel@tonic-gate 		if (strcmp(table[i].subcmd, subcmd) == 0)
5847c478bd9Sstevel@tonic-gate 			break;
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	if (table[i].subcmd == NULL) {
5887c478bd9Sstevel@tonic-gate 		bam_error(INVALID_SUBCMD, subcmd);
5897c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 
5921a97e40eSvikram 	if (table[i].unpriv == 0 && geteuid() != 0) {
5931a97e40eSvikram 		bam_error(MUST_BE_ROOT);
5941a97e40eSvikram 		return (BAM_ERROR);
5951a97e40eSvikram 	}
5961a97e40eSvikram 
5971a97e40eSvikram 	/*
5981a97e40eSvikram 	 * Currently only privileged commands need a lock
5991a97e40eSvikram 	 */
6001a97e40eSvikram 	if (table[i].unpriv == 0)
6011a97e40eSvikram 		bam_lock();
6021a97e40eSvikram 
6037c478bd9Sstevel@tonic-gate 	/* subcmd verifies that opt is appropriate */
6047c478bd9Sstevel@tonic-gate 	if (table[i].option != OPT_OPTIONAL) {
6057c478bd9Sstevel@tonic-gate 		if ((table[i].option == OPT_REQ) ^ (opt != NULL)) {
6067c478bd9Sstevel@tonic-gate 			if (opt)
6077c478bd9Sstevel@tonic-gate 				bam_error(NO_OPT_REQ, subcmd);
6087c478bd9Sstevel@tonic-gate 			else
6097c478bd9Sstevel@tonic-gate 				bam_error(MISS_OPT, subcmd);
6107c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
6117c478bd9Sstevel@tonic-gate 		}
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	*fp = table[i].handler;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate 
619b610f78eSvikram 
620b610f78eSvikram static char *
62140541d5dSvikram mount_grub_slice(int *mnted, char **physlice, char **logslice, char **fs_type)
622b610f78eSvikram {
623b610f78eSvikram 	struct extmnttab mnt;
624b610f78eSvikram 	struct stat sb;
625b610f78eSvikram 	char buf[BAM_MAXLINE], dev[PATH_MAX], phys[PATH_MAX], fstype[32];
626f0f2c544Svikram 	char cmd[PATH_MAX];
627b610f78eSvikram 	char *mntpt;
628b610f78eSvikram 	int p, l, f;
629b610f78eSvikram 	FILE *fp;
630b610f78eSvikram 
631b610f78eSvikram 	assert(mnted);
632b610f78eSvikram 	*mnted = 0;
633b610f78eSvikram 
634b610f78eSvikram 	/*
63540541d5dSvikram 	 * physlice, logslice, fs_type  args may be NULL
636b610f78eSvikram 	 */
637b610f78eSvikram 	if (physlice)
638b610f78eSvikram 		*physlice = NULL;
63940541d5dSvikram 	if (logslice)
64040541d5dSvikram 		*logslice = NULL;
64140541d5dSvikram 	if (fs_type)
64240541d5dSvikram 		*fs_type = NULL;
643b610f78eSvikram 
644b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
645b610f78eSvikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
646b610f78eSvikram 		return (NULL);
647b610f78eSvikram 	}
648b610f78eSvikram 
649b610f78eSvikram 	fp = fopen(GRUB_slice, "r");
650b610f78eSvikram 	if (fp == NULL) {
651b610f78eSvikram 		bam_error(OPEN_FAIL, GRUB_slice, strerror(errno));
652b610f78eSvikram 		return (NULL);
653b610f78eSvikram 	}
654b610f78eSvikram 
655b610f78eSvikram 	dev[0] = fstype[0] = phys[0] = '\0';
656b610f78eSvikram 	p = sizeof ("PHYS_SLICE=") - 1;
657b610f78eSvikram 	l = sizeof ("LOG_SLICE=") - 1;
658b610f78eSvikram 	f = sizeof ("LOG_FSTYP=") - 1;
659b610f78eSvikram 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
660b610f78eSvikram 		if (strncmp(buf, "PHYS_SLICE=", p) == 0) {
661b610f78eSvikram 			(void) strlcpy(phys, buf + p, sizeof (phys));
662b610f78eSvikram 			continue;
663b610f78eSvikram 		}
664b610f78eSvikram 		if (strncmp(buf, "LOG_SLICE=", l) == 0) {
665b610f78eSvikram 			(void) strlcpy(dev, buf + l, sizeof (dev));
666b610f78eSvikram 			continue;
667b610f78eSvikram 		}
668b610f78eSvikram 		if (strncmp(buf, "LOG_FSTYP=", f) == 0) {
669b610f78eSvikram 			(void) strlcpy(fstype, buf + f, sizeof (fstype));
670b610f78eSvikram 			continue;
671b610f78eSvikram 		}
672b610f78eSvikram 	}
673b610f78eSvikram 	(void) fclose(fp);
674b610f78eSvikram 
675b610f78eSvikram 	if (dev[0] == '\0' || fstype[0] == '\0' || phys[0] == '\0') {
676b610f78eSvikram 		bam_error(BAD_SLICE_FILE, GRUB_slice);
677b610f78eSvikram 		return (NULL);
678b610f78eSvikram 	}
679b610f78eSvikram 
680b610f78eSvikram 	if (physlice) {
681b610f78eSvikram 		*physlice = s_strdup(phys);
682b610f78eSvikram 	}
68340541d5dSvikram 	if (logslice) {
68440541d5dSvikram 		*logslice = s_strdup(dev);
68540541d5dSvikram 	}
68640541d5dSvikram 	if (fs_type) {
68740541d5dSvikram 		*fs_type = s_strdup(fstype);
68840541d5dSvikram 	}
689b610f78eSvikram 
690b610f78eSvikram 	/*
691b610f78eSvikram 	 * Check if the slice is already mounted
692b610f78eSvikram 	 */
693b610f78eSvikram 	fp = fopen(MNTTAB, "r");
694b610f78eSvikram 	if (fp == NULL) {
695b610f78eSvikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
69640541d5dSvikram 		goto error;
697b610f78eSvikram 	}
698b610f78eSvikram 
699b610f78eSvikram 	resetmnttab(fp);
700b610f78eSvikram 
701b610f78eSvikram 	mntpt = NULL;
702b610f78eSvikram 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
703b610f78eSvikram 		if (strcmp(mnt.mnt_special, dev) == 0) {
704b610f78eSvikram 			mntpt = s_strdup(mnt.mnt_mountp);
705b610f78eSvikram 			break;
706b610f78eSvikram 		}
707b610f78eSvikram 	}
708b610f78eSvikram 
709b610f78eSvikram 	(void) fclose(fp);
710b610f78eSvikram 
711b610f78eSvikram 	if (mntpt) {
712b610f78eSvikram 		return (mntpt);
713b610f78eSvikram 	}
714b610f78eSvikram 
715b610f78eSvikram 
716b610f78eSvikram 	/*
717b610f78eSvikram 	 * GRUB slice is not mounted, we need to mount it now.
718b610f78eSvikram 	 * First create the mountpoint
719b610f78eSvikram 	 */
720b610f78eSvikram 	mntpt = s_calloc(1, PATH_MAX);
721b610f78eSvikram 	(void) snprintf(mntpt, PATH_MAX, "%s.%d", GRUB_slice_mntpt, getpid());
722b610f78eSvikram 	if (mkdir(mntpt, 0755) == -1 && errno != EEXIST) {
723b610f78eSvikram 		bam_error(MKDIR_FAILED, mntpt, strerror(errno));
724b610f78eSvikram 		free(mntpt);
72540541d5dSvikram 		goto error;
726b610f78eSvikram 	}
727b610f78eSvikram 
728f0f2c544Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/mount -F %s %s %s",
729f0f2c544Svikram 	    fstype, dev, mntpt);
730f0f2c544Svikram 
731986fd29aSsetje 	if (exec_cmd(cmd, NULL) != 0) {
73240541d5dSvikram 		bam_error(MOUNT_FAILED, dev, fstype);
733b610f78eSvikram 		if (rmdir(mntpt) != 0) {
734b610f78eSvikram 			bam_error(RMDIR_FAILED, mntpt, strerror(errno));
735b610f78eSvikram 		}
736b610f78eSvikram 		free(mntpt);
73740541d5dSvikram 		goto error;
738b610f78eSvikram 	}
739b610f78eSvikram 
740b610f78eSvikram 	*mnted = 1;
741b610f78eSvikram 	return (mntpt);
74240541d5dSvikram 
74340541d5dSvikram error:
74440541d5dSvikram 	if (physlice) {
74540541d5dSvikram 		free(*physlice);
74640541d5dSvikram 		*physlice = NULL;
74740541d5dSvikram 	}
74840541d5dSvikram 	if (logslice) {
74940541d5dSvikram 		free(*logslice);
75040541d5dSvikram 		*logslice = NULL;
75140541d5dSvikram 	}
75240541d5dSvikram 	if (fs_type) {
75340541d5dSvikram 		free(*fs_type);
75440541d5dSvikram 		*fs_type = NULL;
75540541d5dSvikram 	}
75640541d5dSvikram 	return (NULL);
757b610f78eSvikram }
758b610f78eSvikram 
759b610f78eSvikram static void
76040541d5dSvikram umount_grub_slice(
76140541d5dSvikram 	int mnted,
76240541d5dSvikram 	char *mntpt,
76340541d5dSvikram 	char *physlice,
76440541d5dSvikram 	char *logslice,
76540541d5dSvikram 	char *fs_type)
766b610f78eSvikram {
767f0f2c544Svikram 	char cmd[PATH_MAX];
768f0f2c544Svikram 
769b610f78eSvikram 	/*
770b610f78eSvikram 	 * If we have not dealt with GRUB slice
771b610f78eSvikram 	 * we have nothing to do - just return.
772b610f78eSvikram 	 */
773b610f78eSvikram 	if (mntpt == NULL)
774b610f78eSvikram 		return;
775b610f78eSvikram 
776b610f78eSvikram 
777b610f78eSvikram 	/*
778b610f78eSvikram 	 * If we mounted the filesystem earlier in mount_grub_slice()
779b610f78eSvikram 	 * unmount it now.
780b610f78eSvikram 	 */
781b610f78eSvikram 	if (mnted) {
782f0f2c544Svikram 		(void) snprintf(cmd, sizeof (cmd), "/sbin/umount %s",
783f0f2c544Svikram 		    mntpt);
784986fd29aSsetje 		if (exec_cmd(cmd, NULL) != 0) {
785f0f2c544Svikram 			bam_error(UMOUNT_FAILED, mntpt);
786b610f78eSvikram 		}
787b610f78eSvikram 		if (rmdir(mntpt) != 0) {
788b610f78eSvikram 			bam_error(RMDIR_FAILED, mntpt, strerror(errno));
789b610f78eSvikram 		}
790b610f78eSvikram 	}
79140541d5dSvikram 
792b610f78eSvikram 	if (physlice)
793b610f78eSvikram 		free(physlice);
79440541d5dSvikram 	if (logslice)
79540541d5dSvikram 		free(logslice);
79640541d5dSvikram 	if (fs_type)
79740541d5dSvikram 		free(fs_type);
79840541d5dSvikram 
799b610f78eSvikram 	free(mntpt);
800b610f78eSvikram }
801b610f78eSvikram 
80240541d5dSvikram static char *
80340541d5dSvikram use_stubboot(void)
80440541d5dSvikram {
80540541d5dSvikram 	int mnted;
80640541d5dSvikram 	struct stat sb;
80740541d5dSvikram 	struct extmnttab mnt;
80840541d5dSvikram 	FILE *fp;
80940541d5dSvikram 	char cmd[PATH_MAX];
81040541d5dSvikram 
81140541d5dSvikram 	if (stat(STUBBOOT, &sb) != 0) {
81240541d5dSvikram 		bam_error(STUBBOOT_DIR_NOT_FOUND);
81340541d5dSvikram 		return (NULL);
81440541d5dSvikram 	}
81540541d5dSvikram 
81640541d5dSvikram 	/*
81740541d5dSvikram 	 * Check if stubboot is mounted. If not, mount it
81840541d5dSvikram 	 */
81940541d5dSvikram 	fp = fopen(MNTTAB, "r");
82040541d5dSvikram 	if (fp == NULL) {
82140541d5dSvikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
82240541d5dSvikram 		return (NULL);
82340541d5dSvikram 	}
82440541d5dSvikram 
82540541d5dSvikram 	resetmnttab(fp);
82640541d5dSvikram 
82740541d5dSvikram 	mnted = 0;
82840541d5dSvikram 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
82940541d5dSvikram 		if (strcmp(mnt.mnt_mountp, STUBBOOT) == 0) {
83040541d5dSvikram 			mnted = 1;
83140541d5dSvikram 			break;
83240541d5dSvikram 		}
83340541d5dSvikram 	}
83440541d5dSvikram 
83540541d5dSvikram 	(void) fclose(fp);
83640541d5dSvikram 
83740541d5dSvikram 	if (mnted)
83840541d5dSvikram 		return (STUBBOOT);
83940541d5dSvikram 
84040541d5dSvikram 	/*
84140541d5dSvikram 	 * Stubboot is not mounted, mount it now.
84240541d5dSvikram 	 * It should exist in /etc/vfstab
84340541d5dSvikram 	 */
84440541d5dSvikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/mount %s",
84540541d5dSvikram 	    STUBBOOT);
846986fd29aSsetje 	if (exec_cmd(cmd, NULL) != 0) {
84740541d5dSvikram 		bam_error(MOUNT_MNTPT_FAILED, STUBBOOT);
84840541d5dSvikram 		return (NULL);
84940541d5dSvikram 	}
85040541d5dSvikram 
85140541d5dSvikram 	return (STUBBOOT);
85240541d5dSvikram }
85340541d5dSvikram 
85440541d5dSvikram static void
85540541d5dSvikram disp_active_menu_locn(char *menu_path, char *logslice, char *fstype, int mnted)
85640541d5dSvikram {
85740541d5dSvikram 	/*
85840541d5dSvikram 	 * Check if we did a temp mount of an unmounted device.
85940541d5dSvikram 	 * If yes, print the block device and fstype for that device
86040541d5dSvikram 	 * else it is already mounted, so we print the path to the GRUB menu.
86140541d5dSvikram 	 */
86240541d5dSvikram 	if (mnted) {
86340541d5dSvikram 		bam_print(GRUB_MENU_DEVICE, logslice);
86440541d5dSvikram 		bam_print(GRUB_MENU_FSTYPE, fstype);
86540541d5dSvikram 	} else {
86640541d5dSvikram 		bam_print(GRUB_MENU_PATH, menu_path);
86740541d5dSvikram 	}
86840541d5dSvikram }
86940541d5dSvikram 
87040541d5dSvikram /*
87140541d5dSvikram  * NOTE: A single "/" is also considered a trailing slash and will
87240541d5dSvikram  * be deleted.
87340541d5dSvikram  */
87440541d5dSvikram static void
87540541d5dSvikram elide_trailing_slash(const char *src, char *dst, size_t dstsize)
87640541d5dSvikram {
87740541d5dSvikram 	size_t dstlen;
87840541d5dSvikram 
87940541d5dSvikram 	assert(src);
88040541d5dSvikram 	assert(dst);
88140541d5dSvikram 
88240541d5dSvikram 	(void) strlcpy(dst, src, dstsize);
88340541d5dSvikram 
88440541d5dSvikram 	dstlen = strlen(dst);
88540541d5dSvikram 	if (dst[dstlen - 1] == '/') {
88640541d5dSvikram 		dst[dstlen - 1] = '\0';
88740541d5dSvikram 	}
88840541d5dSvikram }
88940541d5dSvikram 
8907c478bd9Sstevel@tonic-gate static error_t
8917c478bd9Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[])
8927c478bd9Sstevel@tonic-gate {
8937c478bd9Sstevel@tonic-gate 	error_t ret;
8947c478bd9Sstevel@tonic-gate 	char menu_path[PATH_MAX];
895ae115bc7Smrj 	char path[PATH_MAX];
8967c478bd9Sstevel@tonic-gate 	menu_t *menu;
89740541d5dSvikram 	char *mntpt, *menu_root, *logslice, *fstype;
898b610f78eSvikram 	struct stat sb;
899b610f78eSvikram 	int mnted;	/* set if we did a mount */
9007c478bd9Sstevel@tonic-gate 	error_t (*f)(menu_t *mp, char *menu_path, char *opt);
901986fd29aSsetje 	char *rootpath;
902986fd29aSsetje 
903986fd29aSsetje 	/*
904986fd29aSsetje 	 * Menu sub-command only applies to GRUB (i.e. x86)
905986fd29aSsetje 	 */
906986fd29aSsetje 	rootpath = (bam_alt_root) ? bam_root : "/";
907986fd29aSsetje 	if (!is_grub((const char *)rootpath)) {
908986fd29aSsetje 		bam_error(NOT_ON_SPARC);
909986fd29aSsetje 		return (BAM_ERROR);
910986fd29aSsetje 	}
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	/*
9137c478bd9Sstevel@tonic-gate 	 * Check arguments
9147c478bd9Sstevel@tonic-gate 	 */
9157c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
9167c478bd9Sstevel@tonic-gate 	if (ret == BAM_ERROR) {
9177c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
9187c478bd9Sstevel@tonic-gate 	}
9197c478bd9Sstevel@tonic-gate 
920b610f78eSvikram 	mntpt = NULL;
921b610f78eSvikram 	mnted = 0;
92240541d5dSvikram 	logslice = fstype = NULL;
92340541d5dSvikram 
92440541d5dSvikram 	/*
925ae115bc7Smrj 	 * Check for the menu.list file:
926ae115bc7Smrj 	 *
927ae115bc7Smrj 	 * 1. Check for a GRUB_slice file, be it on / or
928ae115bc7Smrj 	 *    on the user-provided alternate root.
929ae115bc7Smrj 	 * 2. Use the alternate root, if given.
930ae115bc7Smrj 	 * 3. Check /stubboot
931ae115bc7Smrj 	 * 4. Use /
93240541d5dSvikram 	 */
93340541d5dSvikram 	if (bam_alt_root) {
934ae115bc7Smrj 		(void) snprintf(path, sizeof (path), "%s%s", bam_root,
935ae115bc7Smrj 		    GRUB_slice);
936ae115bc7Smrj 	} else {
937ae115bc7Smrj 		(void) snprintf(path, sizeof (path), "%s", GRUB_slice);
938ae115bc7Smrj 	}
939ae115bc7Smrj 
940ae115bc7Smrj 	if (stat(path, &sb) == 0) {
94140541d5dSvikram 		mntpt = mount_grub_slice(&mnted, NULL, &logslice, &fstype);
942b610f78eSvikram 		menu_root = mntpt;
943ae115bc7Smrj 	} else if (bam_alt_root) {
944ae115bc7Smrj 		menu_root = bam_root;
94540541d5dSvikram 	} else if (stat(STUBBOOT, &sb) == 0) {
94640541d5dSvikram 		menu_root = use_stubboot();
947b610f78eSvikram 	} else {
948b610f78eSvikram 		menu_root = bam_root;
949b610f78eSvikram 	}
950b610f78eSvikram 
95140541d5dSvikram 	if (menu_root == NULL) {
95240541d5dSvikram 		bam_error(CANNOT_LOCATE_GRUB_MENU);
95340541d5dSvikram 		return (BAM_ERROR);
95440541d5dSvikram 	}
95540541d5dSvikram 
95640541d5dSvikram 	elide_trailing_slash(menu_root, menu_path, sizeof (menu_path));
95740541d5dSvikram 	(void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path));
95840541d5dSvikram 
95940541d5dSvikram 	/*
96040541d5dSvikram 	 * If listing the menu, display the active menu
96140541d5dSvikram 	 * location
96240541d5dSvikram 	 */
96340541d5dSvikram 	if (strcmp(subcmd, "list_entry") == 0) {
96440541d5dSvikram 		disp_active_menu_locn(menu_path, logslice, fstype, mnted);
96540541d5dSvikram 	}
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	menu = menu_read(menu_path);
9687c478bd9Sstevel@tonic-gate 	assert(menu);
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	/*
9717c478bd9Sstevel@tonic-gate 	 * Special handling for setting timeout and default
9727c478bd9Sstevel@tonic-gate 	 */
9737c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "set_option") == 0) {
9747c478bd9Sstevel@tonic-gate 		if (largc != 1 || largv[0] == NULL) {
9757c478bd9Sstevel@tonic-gate 			usage();
97640541d5dSvikram 			menu_free(menu);
97740541d5dSvikram 			umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
9787c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
9797c478bd9Sstevel@tonic-gate 		}
9807c478bd9Sstevel@tonic-gate 		opt = largv[0];
9817c478bd9Sstevel@tonic-gate 	} else if (largc != 0) {
9827c478bd9Sstevel@tonic-gate 		usage();
98340541d5dSvikram 		menu_free(menu);
98440541d5dSvikram 		umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
9857c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
9867c478bd9Sstevel@tonic-gate 	}
9877c478bd9Sstevel@tonic-gate 
988ae115bc7Smrj 	ret = dboot_or_multiboot(bam_root);
989ae115bc7Smrj 	if (ret != BAM_SUCCESS)
990ae115bc7Smrj 		return (ret);
991ae115bc7Smrj 
9927c478bd9Sstevel@tonic-gate 	/*
9937c478bd9Sstevel@tonic-gate 	 * Once the sub-cmd handler has run
9947c478bd9Sstevel@tonic-gate 	 * only the line field is guaranteed to have valid values
9957c478bd9Sstevel@tonic-gate 	 */
996ae115bc7Smrj 	if ((strcmp(subcmd, "update_entry") == 0) ||
997ae115bc7Smrj 	    (strcmp(subcmd, "upgrade") == 0))
9987c478bd9Sstevel@tonic-gate 		ret = f(menu, bam_root, opt);
9997c478bd9Sstevel@tonic-gate 	else
10007c478bd9Sstevel@tonic-gate 		ret = f(menu, menu_path, opt);
10017c478bd9Sstevel@tonic-gate 	if (ret == BAM_WRITE) {
1002b610f78eSvikram 		ret = menu_write(menu_root, menu);
10037c478bd9Sstevel@tonic-gate 	}
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	menu_free(menu);
10067c478bd9Sstevel@tonic-gate 
100740541d5dSvikram 	umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
1008b610f78eSvikram 
10097c478bd9Sstevel@tonic-gate 	return (ret);
10107c478bd9Sstevel@tonic-gate }
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate static error_t
10147c478bd9Sstevel@tonic-gate bam_archive(
10157c478bd9Sstevel@tonic-gate 	char *subcmd,
10167c478bd9Sstevel@tonic-gate 	char *opt)
10177c478bd9Sstevel@tonic-gate {
10187c478bd9Sstevel@tonic-gate 	error_t ret;
10197c478bd9Sstevel@tonic-gate 	error_t (*f)(char *root, char *opt);
10207c478bd9Sstevel@tonic-gate 
10218c1b6884Sszhou 	/*
10228c1b6884Sszhou 	 * Add trailing / for archive subcommands
10238c1b6884Sszhou 	 */
10248c1b6884Sszhou 	if (rootbuf[strlen(rootbuf) - 1] != '/')
10258c1b6884Sszhou 		(void) strcat(rootbuf, "/");
10268c1b6884Sszhou 	bam_rootlen = strlen(rootbuf);
10278c1b6884Sszhou 
10287c478bd9Sstevel@tonic-gate 	/*
10297c478bd9Sstevel@tonic-gate 	 * Check arguments
10307c478bd9Sstevel@tonic-gate 	 */
10317c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f);
10327c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS) {
10337c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
10347c478bd9Sstevel@tonic-gate 	}
10357c478bd9Sstevel@tonic-gate 
1036ae115bc7Smrj 	ret = dboot_or_multiboot(rootbuf);
1037ae115bc7Smrj 	if (ret != BAM_SUCCESS)
1038ae115bc7Smrj 		return (ret);
1039ae115bc7Smrj 
10407c478bd9Sstevel@tonic-gate 	/*
10417c478bd9Sstevel@tonic-gate 	 * Check archive not supported with update_all
10427c478bd9Sstevel@tonic-gate 	 * since it is awkward to display out-of-sync
10437c478bd9Sstevel@tonic-gate 	 * information for each BE.
10447c478bd9Sstevel@tonic-gate 	 */
10457c478bd9Sstevel@tonic-gate 	if (bam_check && strcmp(subcmd, "update_all") == 0) {
10467c478bd9Sstevel@tonic-gate 		bam_error(CHECK_NOT_SUPPORTED, subcmd);
10477c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
10487c478bd9Sstevel@tonic-gate 	}
10497c478bd9Sstevel@tonic-gate 
1050b610f78eSvikram 	if (strcmp(subcmd, "update_all") == 0)
1051b610f78eSvikram 		bam_update_all = 1;
1052b610f78eSvikram 
1053986fd29aSsetje #if !defined(_OPB)
10542449e17fSsherrym 	ucode_install(bam_root);
10552449e17fSsherrym #endif
10562449e17fSsherrym 
1057b610f78eSvikram 	ret = f(bam_root, opt);
1058b610f78eSvikram 
1059b610f78eSvikram 	bam_update_all = 0;
1060b610f78eSvikram 
1061b610f78eSvikram 	return (ret);
10627c478bd9Sstevel@tonic-gate }
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1065ae115bc7Smrj void
10667c478bd9Sstevel@tonic-gate bam_error(char *format, ...)
10677c478bd9Sstevel@tonic-gate {
10687c478bd9Sstevel@tonic-gate 	va_list ap;
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	va_start(ap, format);
10717c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", prog);
10727c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
10737c478bd9Sstevel@tonic-gate 	va_end(ap);
10747c478bd9Sstevel@tonic-gate }
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
10777c478bd9Sstevel@tonic-gate static void
10787c478bd9Sstevel@tonic-gate bam_print(char *format, ...)
10797c478bd9Sstevel@tonic-gate {
10807c478bd9Sstevel@tonic-gate 	va_list ap;
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	va_start(ap, format);
10837c478bd9Sstevel@tonic-gate 	(void) vfprintf(stdout, format, ap);
10847c478bd9Sstevel@tonic-gate 	va_end(ap);
10857c478bd9Sstevel@tonic-gate }
10867c478bd9Sstevel@tonic-gate 
1087ae115bc7Smrj /*PRINTFLIKE1*/
1088ae115bc7Smrj void
1089ae115bc7Smrj bam_print_stderr(char *format, ...)
1090ae115bc7Smrj {
1091ae115bc7Smrj 	va_list ap;
1092ae115bc7Smrj 
1093ae115bc7Smrj 	va_start(ap, format);
1094ae115bc7Smrj 	(void) vfprintf(stderr, format, ap);
1095ae115bc7Smrj 	va_end(ap);
1096ae115bc7Smrj }
1097ae115bc7Smrj 
10987c478bd9Sstevel@tonic-gate static void
10997c478bd9Sstevel@tonic-gate bam_exit(int excode)
11007c478bd9Sstevel@tonic-gate {
11017c478bd9Sstevel@tonic-gate 	bam_unlock();
11027c478bd9Sstevel@tonic-gate 	exit(excode);
11037c478bd9Sstevel@tonic-gate }
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate static void
11067c478bd9Sstevel@tonic-gate bam_lock(void)
11077c478bd9Sstevel@tonic-gate {
11087c478bd9Sstevel@tonic-gate 	struct flock lock;
11097c478bd9Sstevel@tonic-gate 	pid_t pid;
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS);
11127c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
11137c478bd9Sstevel@tonic-gate 		/*
11147c478bd9Sstevel@tonic-gate 		 * We may be invoked early in boot for archive verification.
11157c478bd9Sstevel@tonic-gate 		 * In this case, root is readonly and /var/run may not exist.
11167c478bd9Sstevel@tonic-gate 		 * Proceed without the lock
11177c478bd9Sstevel@tonic-gate 		 */
11187c478bd9Sstevel@tonic-gate 		if (errno == EROFS || errno == ENOENT) {
11197c478bd9Sstevel@tonic-gate 			bam_root_readonly = 1;
11207c478bd9Sstevel@tonic-gate 			return;
11217c478bd9Sstevel@tonic-gate 		}
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno));
11247c478bd9Sstevel@tonic-gate 		bam_exit(1);
11257c478bd9Sstevel@tonic-gate 	}
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
11287c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
11297c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
11307c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) {
11337c478bd9Sstevel@tonic-gate 		if (errno != EACCES && errno != EAGAIN) {
11347c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11357c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
11367c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
11377c478bd9Sstevel@tonic-gate 			bam_exit(1);
11387c478bd9Sstevel@tonic-gate 		}
11397c478bd9Sstevel@tonic-gate 		pid = 0;
11407c478bd9Sstevel@tonic-gate 		(void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0);
11417c478bd9Sstevel@tonic-gate 		bam_print(FILE_LOCKED, pid);
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 		lock.l_type = F_WRLCK;
11447c478bd9Sstevel@tonic-gate 		lock.l_whence = SEEK_SET;
11457c478bd9Sstevel@tonic-gate 		lock.l_start = 0;
11467c478bd9Sstevel@tonic-gate 		lock.l_len = 0;
11477c478bd9Sstevel@tonic-gate 		if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) {
11487c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11497c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
11507c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
11517c478bd9Sstevel@tonic-gate 			bam_exit(1);
11527c478bd9Sstevel@tonic-gate 		}
11537c478bd9Sstevel@tonic-gate 	}
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	/* We own the lock now */
11567c478bd9Sstevel@tonic-gate 	pid = getpid();
11577c478bd9Sstevel@tonic-gate 	(void) write(bam_lock_fd, &pid, sizeof (pid));
11587c478bd9Sstevel@tonic-gate }
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate static void
11617c478bd9Sstevel@tonic-gate bam_unlock(void)
11627c478bd9Sstevel@tonic-gate {
11637c478bd9Sstevel@tonic-gate 	struct flock unlock;
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	/*
11667c478bd9Sstevel@tonic-gate 	 * NOP if we don't hold the lock
11677c478bd9Sstevel@tonic-gate 	 */
11687c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
11697c478bd9Sstevel@tonic-gate 		return;
11707c478bd9Sstevel@tonic-gate 	}
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
11737c478bd9Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
11747c478bd9Sstevel@tonic-gate 	unlock.l_start = 0;
11757c478bd9Sstevel@tonic-gate 	unlock.l_len = 0;
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) {
11787c478bd9Sstevel@tonic-gate 		bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
11797c478bd9Sstevel@tonic-gate 	}
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	if (close(bam_lock_fd) == -1) {
11827c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno));
11837c478bd9Sstevel@tonic-gate 	}
11847c478bd9Sstevel@tonic-gate 	bam_lock_fd = -1;
11857c478bd9Sstevel@tonic-gate }
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate static error_t
11887c478bd9Sstevel@tonic-gate list_archive(char *root, char *opt)
11897c478bd9Sstevel@tonic-gate {
11907c478bd9Sstevel@tonic-gate 	filelist_t flist;
11917c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
11927c478bd9Sstevel@tonic-gate 	line_t *lp;
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	assert(root);
11957c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
11987c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
11997c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12007c478bd9Sstevel@tonic-gate 	}
12017c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
12047c478bd9Sstevel@tonic-gate 		bam_print(PRINT, lp->line);
12057c478bd9Sstevel@tonic-gate 	}
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
12107c478bd9Sstevel@tonic-gate }
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate /*
12137c478bd9Sstevel@tonic-gate  * This routine writes a list of lines to a file.
12147c478bd9Sstevel@tonic-gate  * The list is *not* freed
12157c478bd9Sstevel@tonic-gate  */
12167c478bd9Sstevel@tonic-gate static error_t
12177c478bd9Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start)
12187c478bd9Sstevel@tonic-gate {
12197c478bd9Sstevel@tonic-gate 	char tmpfile[PATH_MAX];
12207c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
12217c478bd9Sstevel@tonic-gate 	FILE *fp;
12227c478bd9Sstevel@tonic-gate 	int ret;
12237c478bd9Sstevel@tonic-gate 	struct stat sb;
12247c478bd9Sstevel@tonic-gate 	mode_t mode;
12257c478bd9Sstevel@tonic-gate 	uid_t root_uid;
12267c478bd9Sstevel@tonic-gate 	gid_t sys_gid;
12277c478bd9Sstevel@tonic-gate 	struct passwd *pw;
12287c478bd9Sstevel@tonic-gate 	struct group *gp;
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, final);
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	if (start == NULL) {
12347c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != -1) {
12357c478bd9Sstevel@tonic-gate 			bam_print(UNLINK_EMPTY, path);
12367c478bd9Sstevel@tonic-gate 			if (unlink(path) != 0) {
12377c478bd9Sstevel@tonic-gate 				bam_error(UNLINK_FAIL, path, strerror(errno));
12387c478bd9Sstevel@tonic-gate 				return (BAM_ERROR);
12397c478bd9Sstevel@tonic-gate 			} else {
12407c478bd9Sstevel@tonic-gate 				return (BAM_SUCCESS);
12417c478bd9Sstevel@tonic-gate 			}
12427c478bd9Sstevel@tonic-gate 		}
12437c478bd9Sstevel@tonic-gate 	}
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	/*
12467c478bd9Sstevel@tonic-gate 	 * Preserve attributes of existing file if possible,
12477c478bd9Sstevel@tonic-gate 	 * otherwise ask the system for uid/gid of root/sys.
12487c478bd9Sstevel@tonic-gate 	 * If all fails, fall back on hard-coded defaults.
12497c478bd9Sstevel@tonic-gate 	 */
12507c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != -1) {
12517c478bd9Sstevel@tonic-gate 		mode = sb.st_mode;
12527c478bd9Sstevel@tonic-gate 		root_uid = sb.st_uid;
12537c478bd9Sstevel@tonic-gate 		sys_gid = sb.st_gid;
12547c478bd9Sstevel@tonic-gate 	} else {
12557c478bd9Sstevel@tonic-gate 		mode = DEFAULT_DEV_MODE;
12567c478bd9Sstevel@tonic-gate 		if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
12577c478bd9Sstevel@tonic-gate 			root_uid = pw->pw_uid;
12587c478bd9Sstevel@tonic-gate 		} else {
12597c478bd9Sstevel@tonic-gate 			if (bam_verbose)
12607c478bd9Sstevel@tonic-gate 				bam_error(CANT_FIND_USER,
12617c478bd9Sstevel@tonic-gate 				    DEFAULT_DEV_USER, DEFAULT_DEV_UID);
12627c478bd9Sstevel@tonic-gate 			root_uid = (uid_t)DEFAULT_DEV_UID;
12637c478bd9Sstevel@tonic-gate 		}
12647c478bd9Sstevel@tonic-gate 		if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
12657c478bd9Sstevel@tonic-gate 			sys_gid = gp->gr_gid;
12667c478bd9Sstevel@tonic-gate 		} else {
12677c478bd9Sstevel@tonic-gate 			if (bam_verbose)
12687c478bd9Sstevel@tonic-gate 				bam_error(CANT_FIND_GROUP,
12697c478bd9Sstevel@tonic-gate 				    DEFAULT_DEV_GROUP, DEFAULT_DEV_GID);
12707c478bd9Sstevel@tonic-gate 			sys_gid = (gid_t)DEFAULT_DEV_GID;
12717c478bd9Sstevel@tonic-gate 		}
12727c478bd9Sstevel@tonic-gate 	}
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	(void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp);
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 	/* Truncate tmpfile first */
12777c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "w");
12787c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
12797c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
12807c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12817c478bd9Sstevel@tonic-gate 	}
12827c478bd9Sstevel@tonic-gate 	ret = fclose(fp);
12837c478bd9Sstevel@tonic-gate 	if (ret == EOF) {
12847c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
12857c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12867c478bd9Sstevel@tonic-gate 	}
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 	/* Now open it in append mode */
12897c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "a");
12907c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
12917c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
12927c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12937c478bd9Sstevel@tonic-gate 	}
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	for (; start; start = start->next) {
12967c478bd9Sstevel@tonic-gate 		ret = s_fputs(start->line, fp);
12977c478bd9Sstevel@tonic-gate 		if (ret == EOF) {
12987c478bd9Sstevel@tonic-gate 			bam_error(WRITE_FAIL, tmpfile, strerror(errno));
12997c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
13007c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
13017c478bd9Sstevel@tonic-gate 		}
13027c478bd9Sstevel@tonic-gate 	}
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	ret = fclose(fp);
13057c478bd9Sstevel@tonic-gate 	if (ret == EOF) {
13067c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
13077c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13087c478bd9Sstevel@tonic-gate 	}
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	/*
1311de531be4Sjg 	 * Set up desired attributes.  Ignore failures on filesystems
1312de531be4Sjg 	 * not supporting these operations - pcfs reports unsupported
1313de531be4Sjg 	 * operations as EINVAL.
13147c478bd9Sstevel@tonic-gate 	 */
13157c478bd9Sstevel@tonic-gate 	ret = chmod(tmpfile, mode);
1316de531be4Sjg 	if (ret == -1 &&
1317de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
13187c478bd9Sstevel@tonic-gate 		bam_error(CHMOD_FAIL, tmpfile, strerror(errno));
13197c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13207c478bd9Sstevel@tonic-gate 	}
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	ret = chown(tmpfile, root_uid, sys_gid);
1323de531be4Sjg 	if (ret == -1 &&
1324de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
13257c478bd9Sstevel@tonic-gate 		bam_error(CHOWN_FAIL, tmpfile, strerror(errno));
13267c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13277c478bd9Sstevel@tonic-gate 	}
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	/*
13317c478bd9Sstevel@tonic-gate 	 * Do an atomic rename
13327c478bd9Sstevel@tonic-gate 	 */
13337c478bd9Sstevel@tonic-gate 	ret = rename(tmpfile, path);
13347c478bd9Sstevel@tonic-gate 	if (ret != 0) {
13357c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path, strerror(errno));
13367c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13377c478bd9Sstevel@tonic-gate 	}
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
13407c478bd9Sstevel@tonic-gate }
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate /*
13437c478bd9Sstevel@tonic-gate  * This function should always return 0 - since we want
13447c478bd9Sstevel@tonic-gate  * to create stat data for *all* files in the list.
13457c478bd9Sstevel@tonic-gate  */
13467c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13477c478bd9Sstevel@tonic-gate static int
13487c478bd9Sstevel@tonic-gate cmpstat(
13497c478bd9Sstevel@tonic-gate 	const char *file,
13507c478bd9Sstevel@tonic-gate 	const struct stat *stat,
13517c478bd9Sstevel@tonic-gate 	int flags,
13527c478bd9Sstevel@tonic-gate 	struct FTW *ftw)
13537c478bd9Sstevel@tonic-gate {
13547c478bd9Sstevel@tonic-gate 	uint_t sz;
13557c478bd9Sstevel@tonic-gate 	uint64_t *value;
13567c478bd9Sstevel@tonic-gate 	uint64_t filestat[2];
13577c478bd9Sstevel@tonic-gate 	int error;
13587c478bd9Sstevel@tonic-gate 
135958091fd8Ssetje 	struct safefile *safefilep;
136058091fd8Ssetje 	FILE *fp;
136158091fd8Ssetje 
13627c478bd9Sstevel@tonic-gate 	/*
13637c478bd9Sstevel@tonic-gate 	 * We only want regular files
13647c478bd9Sstevel@tonic-gate 	 */
13657c478bd9Sstevel@tonic-gate 	if (!S_ISREG(stat->st_mode))
13667c478bd9Sstevel@tonic-gate 		return (0);
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	/*
13697c478bd9Sstevel@tonic-gate 	 * new_nvlp may be NULL if there were errors earlier
13707c478bd9Sstevel@tonic-gate 	 * but this is not fatal to update determination.
13717c478bd9Sstevel@tonic-gate 	 */
13727c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp) {
13737c478bd9Sstevel@tonic-gate 		filestat[0] = stat->st_size;
13747c478bd9Sstevel@tonic-gate 		filestat[1] = stat->st_mtime;
13757c478bd9Sstevel@tonic-gate 		error = nvlist_add_uint64_array(walk_arg.new_nvlp,
13767c478bd9Sstevel@tonic-gate 		    file + bam_rootlen, filestat, 2);
13777c478bd9Sstevel@tonic-gate 		if (error)
13787c478bd9Sstevel@tonic-gate 			bam_error(NVADD_FAIL, file, strerror(error));
13797c478bd9Sstevel@tonic-gate 	}
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	/*
13827c478bd9Sstevel@tonic-gate 	 * The remaining steps are only required if we haven't made a
13837c478bd9Sstevel@tonic-gate 	 * decision about update or if we are checking (-n)
13847c478bd9Sstevel@tonic-gate 	 */
13857c478bd9Sstevel@tonic-gate 	if (walk_arg.need_update && !bam_check)
13867c478bd9Sstevel@tonic-gate 		return (0);
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	/*
1389*d876c67dSjg 	 * If we are invoked as part of system/filesystem/boot-archive, then
139058091fd8Ssetje 	 * there are a number of things we should not worry about
13917c478bd9Sstevel@tonic-gate 	 */
139258091fd8Ssetje 	if (bam_smf_check) {
139358091fd8Ssetje 		/* ignore amd64 modules unless we are booted amd64. */
139458091fd8Ssetje 		if (!is_amd64() && strstr(file, "/amd64/") != 0)
139558091fd8Ssetje 			return (0);
139658091fd8Ssetje 
139758091fd8Ssetje 		/* read in list of safe files */
139858091fd8Ssetje 		if (safefiles == NULL)
139958091fd8Ssetje 			if (fp = fopen("/boot/solaris/filelist.safe", "r")) {
140058091fd8Ssetje 				safefiles = s_calloc(1,
140158091fd8Ssetje 				    sizeof (struct safefile));
140258091fd8Ssetje 				safefilep = safefiles;
140358091fd8Ssetje 				safefilep->name = s_calloc(1, MAXPATHLEN +
140458091fd8Ssetje 				    MAXNAMELEN);
140558091fd8Ssetje 				safefilep->next = NULL;
140658091fd8Ssetje 				while (s_fgets(safefilep->name, MAXPATHLEN +
140758091fd8Ssetje 				    MAXNAMELEN, fp) != NULL) {
140858091fd8Ssetje 					safefilep->next = s_calloc(1,
140958091fd8Ssetje 					    sizeof (struct safefile));
141058091fd8Ssetje 					safefilep = safefilep->next;
141158091fd8Ssetje 					safefilep->name = s_calloc(1,
141258091fd8Ssetje 					    MAXPATHLEN + MAXNAMELEN);
141358091fd8Ssetje 					safefilep->next = NULL;
141458091fd8Ssetje 				}
141558091fd8Ssetje 				(void) fclose(fp);
141658091fd8Ssetje 			}
141758091fd8Ssetje 	}
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	/*
14207c478bd9Sstevel@tonic-gate 	 * We need an update if file doesn't exist in old archive
14217c478bd9Sstevel@tonic-gate 	 */
14227c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp == NULL ||
14237c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint64_array(walk_arg.old_nvlp,
14247c478bd9Sstevel@tonic-gate 	    file + bam_rootlen, &value, &sz) != 0) {
14257c478bd9Sstevel@tonic-gate 		if (bam_smf_check)	/* ignore new during smf check */
14267c478bd9Sstevel@tonic-gate 			return (0);
14277c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
14287c478bd9Sstevel@tonic-gate 		if (bam_verbose)
14297c478bd9Sstevel@tonic-gate 			bam_print(PARSEABLE_NEW_FILE, file);
14307c478bd9Sstevel@tonic-gate 		return (0);
14317c478bd9Sstevel@tonic-gate 	}
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	/*
14347c478bd9Sstevel@tonic-gate 	 * File exists in old archive. Check if file has changed
14357c478bd9Sstevel@tonic-gate 	 */
14367c478bd9Sstevel@tonic-gate 	assert(sz == 2);
14377c478bd9Sstevel@tonic-gate 	bcopy(value, filestat, sizeof (filestat));
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	if (filestat[0] != stat->st_size ||
14407c478bd9Sstevel@tonic-gate 	    filestat[1] != stat->st_mtime) {
14417c609327Ssetje 		if (bam_smf_check) {
14427c609327Ssetje 			safefilep = safefiles;
14437c609327Ssetje 			while (safefilep != NULL) {
14447c609327Ssetje 				if (strcmp(file + bam_rootlen,
14457c609327Ssetje 				    safefilep->name) == 0) {
14467c609327Ssetje 					(void) creat(NEED_UPDATE_FILE, 0644);
14477c609327Ssetje 					return (0);
14487c609327Ssetje 				}
14497c609327Ssetje 				safefilep = safefilep->next;
14507c609327Ssetje 			}
14517c609327Ssetje 		}
14527c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
14537c478bd9Sstevel@tonic-gate 		if (bam_verbose)
14547c478bd9Sstevel@tonic-gate 			if (bam_smf_check)
14557c478bd9Sstevel@tonic-gate 				bam_print("    %s\n", file);
14567c478bd9Sstevel@tonic-gate 			else
14577c478bd9Sstevel@tonic-gate 				bam_print(PARSEABLE_OUT_DATE, file);
14587c478bd9Sstevel@tonic-gate 	}
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 	return (0);
14617c478bd9Sstevel@tonic-gate }
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate /*
14647c478bd9Sstevel@tonic-gate  * Check flags and presence of required files.
14657c478bd9Sstevel@tonic-gate  * The force flag and/or absence of files should
14667c478bd9Sstevel@tonic-gate  * trigger an update.
14677c478bd9Sstevel@tonic-gate  * Suppress stdout output if check (-n) option is set
14687c478bd9Sstevel@tonic-gate  * (as -n should only produce parseable output.)
14697c478bd9Sstevel@tonic-gate  */
14707c478bd9Sstevel@tonic-gate static void
14717c478bd9Sstevel@tonic-gate check_flags_and_files(char *root)
14727c478bd9Sstevel@tonic-gate {
14737c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
14747c478bd9Sstevel@tonic-gate 	struct stat sb;
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	/*
14777c478bd9Sstevel@tonic-gate 	 * if force, create archive unconditionally
14787c478bd9Sstevel@tonic-gate 	 */
14797c478bd9Sstevel@tonic-gate 	if (bam_force) {
14807c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
14817c478bd9Sstevel@tonic-gate 		if (bam_verbose && !bam_check)
14827c478bd9Sstevel@tonic-gate 			bam_print(UPDATE_FORCE);
14837c478bd9Sstevel@tonic-gate 		return;
14847c478bd9Sstevel@tonic-gate 	}
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 	/*
14877c478bd9Sstevel@tonic-gate 	 * If archive is missing, create archive
14887c478bd9Sstevel@tonic-gate 	 */
1489*d876c67dSjg 	if (is_sun4u()) {
1490986fd29aSsetje 		(void) snprintf(path, sizeof (path), "%s%s", root,
1491*d876c67dSjg 		    SUN4U_ARCHIVE);
1492*d876c67dSjg 	} else if (is_sun4v()) {
1493986fd29aSsetje 		(void) snprintf(path, sizeof (path), "%s%s", root,
1494*d876c67dSjg 		    SUN4V_ARCHIVE);
1495*d876c67dSjg 	} else {
1496986fd29aSsetje 		if (bam_direct == BAM_DIRECT_DBOOT) {
1497986fd29aSsetje 			(void) snprintf(path, sizeof (path), "%s%s", root,
1498986fd29aSsetje 			    DIRECT_BOOT_ARCHIVE_64);
1499986fd29aSsetje 			if (stat(path, &sb) != 0) {
1500986fd29aSsetje 				if (bam_verbose && !bam_check)
1501986fd29aSsetje 					bam_print(UPDATE_ARCH_MISS, path);
1502986fd29aSsetje 				walk_arg.need_update = 1;
1503986fd29aSsetje 				return;
1504986fd29aSsetje 			}
1505986fd29aSsetje 		}
1506986fd29aSsetje 		(void) snprintf(path, sizeof (path), "%s%s", root,
1507986fd29aSsetje 		    DIRECT_BOOT_ARCHIVE_32);
1508986fd29aSsetje 	}
1509986fd29aSsetje 
15107c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
15117c478bd9Sstevel@tonic-gate 		if (bam_verbose && !bam_check)
15127c478bd9Sstevel@tonic-gate 			bam_print(UPDATE_ARCH_MISS, path);
15137c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
15147c478bd9Sstevel@tonic-gate 		return;
15157c478bd9Sstevel@tonic-gate 	}
15167c478bd9Sstevel@tonic-gate }
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate static error_t
15197c478bd9Sstevel@tonic-gate read_one_list(char *root, filelist_t  *flistp, char *filelist)
15207c478bd9Sstevel@tonic-gate {
15217c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
15227c478bd9Sstevel@tonic-gate 	FILE *fp;
15237c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE];
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, filelist);
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	fp = fopen(path, "r");
15287c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
15297c478bd9Sstevel@tonic-gate 		if (bam_debug)
15307c478bd9Sstevel@tonic-gate 			bam_error(FLIST_FAIL, path, strerror(errno));
15317c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15327c478bd9Sstevel@tonic-gate 	}
15337c478bd9Sstevel@tonic-gate 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
1534b610f78eSvikram 		/* skip blank lines */
1535b610f78eSvikram 		if (strspn(buf, " \t") == strlen(buf))
1536b610f78eSvikram 			continue;
15377c478bd9Sstevel@tonic-gate 		append_to_flist(flistp, buf);
15387c478bd9Sstevel@tonic-gate 	}
15397c478bd9Sstevel@tonic-gate 	if (fclose(fp) != 0) {
15407c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, path, strerror(errno));
15417c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15427c478bd9Sstevel@tonic-gate 	}
15437c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
15447c478bd9Sstevel@tonic-gate }
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate static error_t
15477c478bd9Sstevel@tonic-gate read_list(char *root, filelist_t  *flistp)
15487c478bd9Sstevel@tonic-gate {
1549986fd29aSsetje 	char path[PATH_MAX];
1550986fd29aSsetje 	char cmd[PATH_MAX];
1551986fd29aSsetje 	struct stat sb;
1552986fd29aSsetje 	int n, rval;
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	/*
1557986fd29aSsetje 	 * build and check path to extract_boot_filelist.ksh
15587c478bd9Sstevel@tonic-gate 	 */
1559986fd29aSsetje 	n = snprintf(path, sizeof (path), "%s%s", root, EXTRACT_BOOT_FILELIST);
1560986fd29aSsetje 	if (n >= sizeof (path)) {
1561986fd29aSsetje 		bam_error(NO_FLIST);
1562986fd29aSsetje 		return (BAM_ERROR);
1563986fd29aSsetje 	}
1564986fd29aSsetje 
1565986fd29aSsetje 	/*
1566986fd29aSsetje 	 * If extract_boot_filelist is present, exec it, otherwise read
1567986fd29aSsetje 	 * the filelists directly, for compatibility with older images.
1568986fd29aSsetje 	 */
1569986fd29aSsetje 	if (stat(path, &sb) == 0) {
1570986fd29aSsetje 		/*
1571986fd29aSsetje 		 * build arguments to exec extract_boot_filelist.ksh
1572986fd29aSsetje 		 */
1573*d876c67dSjg 		char *rootarg, *platarg;
1574*d876c67dSjg 		int platarglen = 1, rootarglen = 1;
1575*d876c67dSjg 		if (strlen(root) > 1)
1576*d876c67dSjg 			rootarglen += strlen(root) + strlen("-R ");
1577*d876c67dSjg 		if (bam_alt_platform)
1578*d876c67dSjg 			platarglen += strlen(bam_platform) + strlen("-p ");
1579*d876c67dSjg 		platarg = s_calloc(1, platarglen);
1580*d876c67dSjg 		rootarg = s_calloc(1, rootarglen);
1581*d876c67dSjg 		*platarg = 0;
1582*d876c67dSjg 		*rootarg = 0;
1583*d876c67dSjg 
1584986fd29aSsetje 		if (strlen(root) > 1) {
1585*d876c67dSjg 			(void) snprintf(rootarg, rootarglen,
1586*d876c67dSjg 			    "-R %s", root);
1587986fd29aSsetje 		}
1588*d876c67dSjg 		if (bam_alt_platform) {
1589*d876c67dSjg 			(void) snprintf(platarg, platarglen,
1590*d876c67dSjg 			    "-p %s", bam_platform);
1591*d876c67dSjg 		}
1592*d876c67dSjg 		n = snprintf(cmd, sizeof (cmd), "%s %s %s /%s /%s",
1593*d876c67dSjg 		    path, rootarg, platarg, BOOT_FILE_LIST, ETC_FILE_LIST);
1594*d876c67dSjg 		free(platarg);
1595*d876c67dSjg 		free(rootarg);
1596986fd29aSsetje 		if (n >= sizeof (cmd)) {
1597986fd29aSsetje 			bam_error(NO_FLIST);
1598986fd29aSsetje 			return (BAM_ERROR);
1599986fd29aSsetje 		}
1600986fd29aSsetje 		if (exec_cmd(cmd, flistp) != 0) {
1601986fd29aSsetje 			if (bam_debug)
1602986fd29aSsetje 				bam_error(FLIST_FAIL, path, strerror(errno));
1603986fd29aSsetje 			return (BAM_ERROR);
1604986fd29aSsetje 		}
1605986fd29aSsetje 	} else {
1606986fd29aSsetje 		/*
1607986fd29aSsetje 		 * Read current lists of files - only the first is mandatory
1608986fd29aSsetje 		 */
1609986fd29aSsetje 		rval = read_one_list(root, flistp, BOOT_FILE_LIST);
1610986fd29aSsetje 		if (rval != BAM_SUCCESS)
1611986fd29aSsetje 			return (rval);
1612986fd29aSsetje 		(void) read_one_list(root, flistp, ETC_FILE_LIST);
1613986fd29aSsetje 	}
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL) {
16167c478bd9Sstevel@tonic-gate 		bam_error(NO_FLIST);
16177c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
16187c478bd9Sstevel@tonic-gate 	}
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
16217c478bd9Sstevel@tonic-gate }
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate static void
16247c478bd9Sstevel@tonic-gate getoldstat(char *root)
16257c478bd9Sstevel@tonic-gate {
16267c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
16277c478bd9Sstevel@tonic-gate 	int fd, error;
16287c478bd9Sstevel@tonic-gate 	struct stat sb;
16297c478bd9Sstevel@tonic-gate 	char *ostat;
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
16327c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDONLY);
16337c478bd9Sstevel@tonic-gate 	if (fd == -1) {
16347c478bd9Sstevel@tonic-gate 		if (bam_verbose)
16357c478bd9Sstevel@tonic-gate 			bam_print(OPEN_FAIL, path, strerror(errno));
16367c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
16377c478bd9Sstevel@tonic-gate 		return;
16387c478bd9Sstevel@tonic-gate 	}
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	if (fstat(fd, &sb) != 0) {
16417c478bd9Sstevel@tonic-gate 		bam_error(STAT_FAIL, path, strerror(errno));
16427c478bd9Sstevel@tonic-gate 		(void) close(fd);
16437c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
16447c478bd9Sstevel@tonic-gate 		return;
16457c478bd9Sstevel@tonic-gate 	}
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	ostat = s_calloc(1, sb.st_size);
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	if (read(fd, ostat, sb.st_size) != sb.st_size) {
16507c478bd9Sstevel@tonic-gate 		bam_error(READ_FAIL, path, strerror(errno));
16517c478bd9Sstevel@tonic-gate 		(void) close(fd);
16527c478bd9Sstevel@tonic-gate 		free(ostat);
16537c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
16547c478bd9Sstevel@tonic-gate 		return;
16557c478bd9Sstevel@tonic-gate 	}
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate 	(void) close(fd);
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
16607c478bd9Sstevel@tonic-gate 	error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0);
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	free(ostat);
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	if (error) {
16657c478bd9Sstevel@tonic-gate 		bam_error(UNPACK_FAIL, path, strerror(error));
16667c478bd9Sstevel@tonic-gate 		walk_arg.old_nvlp = NULL;
16677c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
16687c478bd9Sstevel@tonic-gate 		return;
16697c478bd9Sstevel@tonic-gate 	}
16707c478bd9Sstevel@tonic-gate }
16717c478bd9Sstevel@tonic-gate 
1672a28d77b8Svikram /*
1673a28d77b8Svikram  * Checks if a file in the current (old) archive has
1674a28d77b8Svikram  * been deleted from the root filesystem. This is needed for
1675a28d77b8Svikram  * software like Trusted Extensions (TX) that switch early
1676a28d77b8Svikram  * in boot based on presence/absence of a kernel module.
1677a28d77b8Svikram  */
1678a28d77b8Svikram static void
1679a28d77b8Svikram check4stale(char *root)
1680a28d77b8Svikram {
1681a28d77b8Svikram 	nvpair_t	*nvp;
1682a28d77b8Svikram 	nvlist_t	*nvlp;
1683a28d77b8Svikram 	char 		*file;
1684a28d77b8Svikram 	char		path[PATH_MAX];
1685a28d77b8Svikram 	struct stat	sb;
1686a28d77b8Svikram 
1687a28d77b8Svikram 	/*
1688a28d77b8Svikram 	 * Skip stale file check during smf check
1689a28d77b8Svikram 	 */
1690a28d77b8Svikram 	if (bam_smf_check)
1691a28d77b8Svikram 		return;
1692a28d77b8Svikram 
1693a28d77b8Svikram 	/* Nothing to do if no old stats */
1694a28d77b8Svikram 	if ((nvlp = walk_arg.old_nvlp) == NULL)
1695a28d77b8Svikram 		return;
1696a28d77b8Svikram 
1697a28d77b8Svikram 	for (nvp = nvlist_next_nvpair(nvlp, NULL); nvp;
1698a28d77b8Svikram 	    nvp = nvlist_next_nvpair(nvlp, nvp)) {
1699a28d77b8Svikram 		file = nvpair_name(nvp);
1700a28d77b8Svikram 		if (file == NULL)
1701a28d77b8Svikram 			continue;
1702a28d77b8Svikram 		(void) snprintf(path, sizeof (path), "%s/%s",
1703a28d77b8Svikram 		    root, file);
1704a28d77b8Svikram 		if (stat(path, &sb) == -1) {
1705a28d77b8Svikram 			walk_arg.need_update = 1;
1706a28d77b8Svikram 			if (bam_verbose)
1707a28d77b8Svikram 				bam_print(PARSEABLE_STALE_FILE, path);
1708a28d77b8Svikram 		}
1709a28d77b8Svikram 	}
1710a28d77b8Svikram }
1711a28d77b8Svikram 
17127c478bd9Sstevel@tonic-gate static void
17137c478bd9Sstevel@tonic-gate create_newstat(void)
17147c478bd9Sstevel@tonic-gate {
17157c478bd9Sstevel@tonic-gate 	int error;
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0);
17187c478bd9Sstevel@tonic-gate 	if (error) {
17197c478bd9Sstevel@tonic-gate 		/*
17207c478bd9Sstevel@tonic-gate 		 * Not fatal - we can still create archive
17217c478bd9Sstevel@tonic-gate 		 */
17227c478bd9Sstevel@tonic-gate 		walk_arg.new_nvlp = NULL;
17237c478bd9Sstevel@tonic-gate 		bam_error(NVALLOC_FAIL, strerror(error));
17247c478bd9Sstevel@tonic-gate 	}
17257c478bd9Sstevel@tonic-gate }
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate static void
17287c478bd9Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp)
17297c478bd9Sstevel@tonic-gate {
17307c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
17317c478bd9Sstevel@tonic-gate 	line_t *lp;
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
1734986fd29aSsetje 		/*
1735986fd29aSsetje 		 * Don't follow symlinks.  A symlink must refer to
1736986fd29aSsetje 		 * a file that would appear in the archive through
1737986fd29aSsetje 		 * a direct reference.  This matches the archive
1738986fd29aSsetje 		 * construction behavior.
1739986fd29aSsetje 		 */
17407c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, lp->line);
1741986fd29aSsetje 		if (nftw(path, cmpstat, 20, FTW_PHYS) == -1) {
17427c478bd9Sstevel@tonic-gate 			/*
17437c478bd9Sstevel@tonic-gate 			 * Some files may not exist.
17447c478bd9Sstevel@tonic-gate 			 * For example: etc/rtc_config on a x86 diskless system
17457c478bd9Sstevel@tonic-gate 			 * Emit verbose message only
17467c478bd9Sstevel@tonic-gate 			 */
17477c478bd9Sstevel@tonic-gate 			if (bam_verbose)
17487c478bd9Sstevel@tonic-gate 				bam_print(NFTW_FAIL, path, strerror(errno));
17497c478bd9Sstevel@tonic-gate 		}
17507c478bd9Sstevel@tonic-gate 	}
17517c478bd9Sstevel@tonic-gate }
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate static void
17547c478bd9Sstevel@tonic-gate savenew(char *root)
17557c478bd9Sstevel@tonic-gate {
17567c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
17577c478bd9Sstevel@tonic-gate 	char path2[PATH_MAX];
17587c478bd9Sstevel@tonic-gate 	size_t sz;
17597c478bd9Sstevel@tonic-gate 	char *nstat;
17607c478bd9Sstevel@tonic-gate 	int fd, wrote, error;
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 	nstat = NULL;
17637c478bd9Sstevel@tonic-gate 	sz = 0;
17647c478bd9Sstevel@tonic-gate 	error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz,
17657c478bd9Sstevel@tonic-gate 	    NV_ENCODE_XDR, 0);
17667c478bd9Sstevel@tonic-gate 	if (error) {
17677c478bd9Sstevel@tonic-gate 		bam_error(PACK_FAIL, strerror(error));
17687c478bd9Sstevel@tonic-gate 		return;
17697c478bd9Sstevel@tonic-gate 	}
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP);
17727c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE);
17737c478bd9Sstevel@tonic-gate 	if (fd == -1) {
17747c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, path, strerror(errno));
17757c478bd9Sstevel@tonic-gate 		free(nstat);
17767c478bd9Sstevel@tonic-gate 		return;
17777c478bd9Sstevel@tonic-gate 	}
17787c478bd9Sstevel@tonic-gate 	wrote = write(fd, nstat, sz);
17797c478bd9Sstevel@tonic-gate 	if (wrote != sz) {
17807c478bd9Sstevel@tonic-gate 		bam_error(WRITE_FAIL, path, strerror(errno));
17817c478bd9Sstevel@tonic-gate 		(void) close(fd);
17827c478bd9Sstevel@tonic-gate 		free(nstat);
17837c478bd9Sstevel@tonic-gate 		return;
17847c478bd9Sstevel@tonic-gate 	}
17857c478bd9Sstevel@tonic-gate 	(void) close(fd);
17867c478bd9Sstevel@tonic-gate 	free(nstat);
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	(void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT);
17897c478bd9Sstevel@tonic-gate 	if (rename(path, path2) != 0) {
17907c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path2, strerror(errno));
17917c478bd9Sstevel@tonic-gate 	}
17927c478bd9Sstevel@tonic-gate }
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate static void
17957c478bd9Sstevel@tonic-gate clear_walk_args(void)
17967c478bd9Sstevel@tonic-gate {
17977c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp)
17987c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.old_nvlp);
17997c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp)
18007c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.new_nvlp);
18017c478bd9Sstevel@tonic-gate 	walk_arg.need_update = 0;
18027c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
18037c478bd9Sstevel@tonic-gate 	walk_arg.new_nvlp = NULL;
18047c478bd9Sstevel@tonic-gate }
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate /*
18077c478bd9Sstevel@tonic-gate  * Returns:
18087c478bd9Sstevel@tonic-gate  *	0 - no update necessary
18097c478bd9Sstevel@tonic-gate  *	1 - update required.
18107c478bd9Sstevel@tonic-gate  *	BAM_ERROR (-1) - An error occurred
18117c478bd9Sstevel@tonic-gate  *
18127c478bd9Sstevel@tonic-gate  * Special handling for check (-n):
18137c478bd9Sstevel@tonic-gate  * ================================
18147c478bd9Sstevel@tonic-gate  * The check (-n) option produces parseable output.
18157c478bd9Sstevel@tonic-gate  * To do this, we suppress all stdout messages unrelated
18167c478bd9Sstevel@tonic-gate  * to out of sync files.
18177c478bd9Sstevel@tonic-gate  * All stderr messages are still printed though.
18187c478bd9Sstevel@tonic-gate  *
18197c478bd9Sstevel@tonic-gate  */
18207c478bd9Sstevel@tonic-gate static int
18217c478bd9Sstevel@tonic-gate update_required(char *root)
18227c478bd9Sstevel@tonic-gate {
18237c478bd9Sstevel@tonic-gate 	struct stat sb;
18247c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
18257c478bd9Sstevel@tonic-gate 	filelist_t flist;
18267c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
18277c478bd9Sstevel@tonic-gate 	int need_update;
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	walk_arg.need_update = 0;
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	/*
18347c478bd9Sstevel@tonic-gate 	 * Without consulting stat data, check if we need update
18357c478bd9Sstevel@tonic-gate 	 */
18367c478bd9Sstevel@tonic-gate 	check_flags_and_files(root);
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 	/*
18397c478bd9Sstevel@tonic-gate 	 * In certain deployment scenarios, filestat may not
18407c478bd9Sstevel@tonic-gate 	 * exist. Ignore it during boot-archive SMF check.
18417c478bd9Sstevel@tonic-gate 	 */
18427c478bd9Sstevel@tonic-gate 	if (bam_smf_check) {
18437c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
18447c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != 0)
18457c478bd9Sstevel@tonic-gate 			return (0);
18467c478bd9Sstevel@tonic-gate 	}
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	/*
18497c478bd9Sstevel@tonic-gate 	 * consult stat data only if we haven't made a decision
18507c478bd9Sstevel@tonic-gate 	 * about update. If checking (-n) however, we always
18517c478bd9Sstevel@tonic-gate 	 * need stat data (since we want to compare old and new)
18527c478bd9Sstevel@tonic-gate 	 */
18537c478bd9Sstevel@tonic-gate 	if (!walk_arg.need_update || bam_check)
18547c478bd9Sstevel@tonic-gate 		getoldstat(root);
18557c478bd9Sstevel@tonic-gate 
1856a28d77b8Svikram 	/*
1857a28d77b8Svikram 	 * Check if the archive contains files that are no longer
1858a28d77b8Svikram 	 * present on the root filesystem.
1859a28d77b8Svikram 	 */
1860a28d77b8Svikram 	if (!walk_arg.need_update || bam_check)
1861a28d77b8Svikram 		check4stale(root);
1862a28d77b8Svikram 
18637c478bd9Sstevel@tonic-gate 	/*
18647c478bd9Sstevel@tonic-gate 	 * read list of files
18657c478bd9Sstevel@tonic-gate 	 */
18667c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
18677c478bd9Sstevel@tonic-gate 		clear_walk_args();
18687c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18697c478bd9Sstevel@tonic-gate 	}
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	/*
18747c478bd9Sstevel@tonic-gate 	 * At this point either the update is required
18757c478bd9Sstevel@tonic-gate 	 * or the decision is pending. In either case
18767c478bd9Sstevel@tonic-gate 	 * we need to create new stat nvlist
18777c478bd9Sstevel@tonic-gate 	 */
18787c478bd9Sstevel@tonic-gate 	create_newstat();
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	/*
18817c478bd9Sstevel@tonic-gate 	 * This walk does 2 things:
18827c478bd9Sstevel@tonic-gate 	 *  	- gets new stat data for every file
18837c478bd9Sstevel@tonic-gate 	 *	- (optional) compare old and new stat data
18847c478bd9Sstevel@tonic-gate 	 */
18857c478bd9Sstevel@tonic-gate 	walk_list(root, &flist);
18867c478bd9Sstevel@tonic-gate 
18877c478bd9Sstevel@tonic-gate 	/* done with the file list */
18887c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 	/*
18917c478bd9Sstevel@tonic-gate 	 * if we didn't succeed in  creating new stat data above
18927c478bd9Sstevel@tonic-gate 	 * just return result of update check so that archive is built.
18937c478bd9Sstevel@tonic-gate 	 */
18947c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp == NULL) {
18957c478bd9Sstevel@tonic-gate 		bam_error(NO_NEW_STAT);
18967c478bd9Sstevel@tonic-gate 		need_update = walk_arg.need_update;
18977c478bd9Sstevel@tonic-gate 		clear_walk_args();
18987c478bd9Sstevel@tonic-gate 		return (need_update ? 1 : 0);
18997c478bd9Sstevel@tonic-gate 	}
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate 	/*
19037c478bd9Sstevel@tonic-gate 	 * If no update required, discard newstat
19047c478bd9Sstevel@tonic-gate 	 */
19057c478bd9Sstevel@tonic-gate 	if (!walk_arg.need_update) {
19067c478bd9Sstevel@tonic-gate 		clear_walk_args();
19077c478bd9Sstevel@tonic-gate 		return (0);
19087c478bd9Sstevel@tonic-gate 	}
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 	return (1);
19117c478bd9Sstevel@tonic-gate }
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate static error_t
19147c478bd9Sstevel@tonic-gate create_ramdisk(char *root)
19157c478bd9Sstevel@tonic-gate {
19167c478bd9Sstevel@tonic-gate 	char *cmdline, path[PATH_MAX];
19177c478bd9Sstevel@tonic-gate 	size_t len;
19187c478bd9Sstevel@tonic-gate 	struct stat sb;
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 	/*
19217c478bd9Sstevel@tonic-gate 	 * Setup command args for create_ramdisk.ksh
19227c478bd9Sstevel@tonic-gate 	 */
1923986fd29aSsetje 	(void) snprintf(path, sizeof (path), "%s/%s", root, CREATE_RAMDISK);
19247c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
19257c478bd9Sstevel@tonic-gate 		bam_error(ARCH_EXEC_MISS, path, strerror(errno));
19267c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
19277c478bd9Sstevel@tonic-gate 	}
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 	len = strlen(path) + strlen(root) + 10;	/* room for space + -R */
1930*d876c67dSjg 	if (bam_alt_platform)
1931*d876c67dSjg 		len += strlen(bam_platform) + strlen("-p ");
19327c478bd9Sstevel@tonic-gate 	cmdline = s_calloc(1, len);
19337c478bd9Sstevel@tonic-gate 
1934*d876c67dSjg 	if (bam_alt_platform) {
1935*d876c67dSjg 		assert(strlen(root) > 1);
1936*d876c67dSjg 		(void) snprintf(cmdline, len, "%s -p %s -R %s",
1937*d876c67dSjg 		    path, bam_platform, root);
1938*d876c67dSjg 		/* chop off / at the end */
1939*d876c67dSjg 		cmdline[strlen(cmdline) - 1] = '\0';
1940*d876c67dSjg 	} else if (strlen(root) > 1) {
19417c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s -R %s", path, root);
19427c478bd9Sstevel@tonic-gate 		/* chop off / at the end */
19437c478bd9Sstevel@tonic-gate 		cmdline[strlen(cmdline) - 1] = '\0';
19447c478bd9Sstevel@tonic-gate 	} else
19457c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s", path);
19467c478bd9Sstevel@tonic-gate 
1947986fd29aSsetje 	if (exec_cmd(cmdline, NULL) != 0) {
19487c478bd9Sstevel@tonic-gate 		bam_error(ARCHIVE_FAIL, cmdline);
19497c478bd9Sstevel@tonic-gate 		free(cmdline);
19507c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
19517c478bd9Sstevel@tonic-gate 	}
19527c478bd9Sstevel@tonic-gate 	free(cmdline);
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	/*
1955986fd29aSsetje 	 * The existence of the expected archives used to be
1956986fd29aSsetje 	 * verified here. This check is done in create_ramdisk as
1957986fd29aSsetje 	 * it needs to be in sync with the altroot operated upon.
19587c478bd9Sstevel@tonic-gate 	 */
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
19617c478bd9Sstevel@tonic-gate }
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate /*
19647c478bd9Sstevel@tonic-gate  * Checks if target filesystem is on a ramdisk
19657c478bd9Sstevel@tonic-gate  * 1 - is miniroot
19667c478bd9Sstevel@tonic-gate  * 0 - is not
19677c478bd9Sstevel@tonic-gate  * When in doubt assume it is not a ramdisk.
19687c478bd9Sstevel@tonic-gate  */
19697c478bd9Sstevel@tonic-gate static int
19707c478bd9Sstevel@tonic-gate is_ramdisk(char *root)
19717c478bd9Sstevel@tonic-gate {
19727c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
19737c478bd9Sstevel@tonic-gate 	FILE *fp;
19747c478bd9Sstevel@tonic-gate 	int found;
1975b610f78eSvikram 	char mntpt[PATH_MAX];
1976b610f78eSvikram 	char *cp;
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate 	/*
19797c478bd9Sstevel@tonic-gate 	 * There are 3 situations where creating archive is
19807c478bd9Sstevel@tonic-gate 	 * of dubious value:
1981b610f78eSvikram 	 *	- create boot_archive on a lofi-mounted boot_archive
19827c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk which is the root filesystem
19837c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk mounted somewhere else
19847c478bd9Sstevel@tonic-gate 	 * The first is not easy to detect and checking for it is not
19857c478bd9Sstevel@tonic-gate 	 * worth it.
19867c478bd9Sstevel@tonic-gate 	 * The other two conditions are handled here
19877c478bd9Sstevel@tonic-gate 	 */
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
19907c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
19917c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
19927c478bd9Sstevel@tonic-gate 		return (0);
19937c478bd9Sstevel@tonic-gate 	}
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
19967c478bd9Sstevel@tonic-gate 
1997b610f78eSvikram 	/*
1998b610f78eSvikram 	 * Remove any trailing / from the mount point
1999b610f78eSvikram 	 */
2000b610f78eSvikram 	(void) strlcpy(mntpt, root, sizeof (mntpt));
2001b610f78eSvikram 	if (strcmp(root, "/") != 0) {
2002b610f78eSvikram 		cp = mntpt + strlen(mntpt) - 1;
2003b610f78eSvikram 		if (*cp == '/')
2004b610f78eSvikram 			*cp = '\0';
2005b610f78eSvikram 	}
20067c478bd9Sstevel@tonic-gate 	found = 0;
20077c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
2008b610f78eSvikram 		if (strcmp(mnt.mnt_mountp, mntpt) == 0) {
20097c478bd9Sstevel@tonic-gate 			found = 1;
20107c478bd9Sstevel@tonic-gate 			break;
20117c478bd9Sstevel@tonic-gate 		}
20127c478bd9Sstevel@tonic-gate 	}
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate 	if (!found) {
20157c478bd9Sstevel@tonic-gate 		if (bam_verbose)
2016b610f78eSvikram 			bam_error(NOT_IN_MNTTAB, mntpt);
20177c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
20187c478bd9Sstevel@tonic-gate 		return (0);
20197c478bd9Sstevel@tonic-gate 	}
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 	if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) {
20227c478bd9Sstevel@tonic-gate 		if (bam_verbose)
20237c478bd9Sstevel@tonic-gate 			bam_error(IS_RAMDISK, bam_root);
20247c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
20257c478bd9Sstevel@tonic-gate 		return (1);
20267c478bd9Sstevel@tonic-gate 	}
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 	return (0);
20317c478bd9Sstevel@tonic-gate }
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate static int
2034986fd29aSsetje is_boot_archive(char *root)
20357c478bd9Sstevel@tonic-gate {
20367c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
20377c478bd9Sstevel@tonic-gate 	struct stat sb;
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 	/*
2040986fd29aSsetje 	 * We can't create an archive without the create_ramdisk script
20417c478bd9Sstevel@tonic-gate 	 */
2042986fd29aSsetje 	(void) snprintf(path, sizeof (path), "%s/%s", root, CREATE_RAMDISK);
20437c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
20447c478bd9Sstevel@tonic-gate 		if (bam_verbose)
20457c478bd9Sstevel@tonic-gate 			bam_print(FILE_MISS, path);
20467c478bd9Sstevel@tonic-gate 		return (0);
20477c478bd9Sstevel@tonic-gate 	}
20487c478bd9Sstevel@tonic-gate 
2049986fd29aSsetje 	return (1);
2050986fd29aSsetje }
2051986fd29aSsetje 
2052986fd29aSsetje /*
2053986fd29aSsetje  * Need to call this for anything that operates on the GRUB menu
2054986fd29aSsetje  */
2055986fd29aSsetje int
2056986fd29aSsetje is_grub(const char *root)
2057986fd29aSsetje {
2058986fd29aSsetje 	char path[PATH_MAX];
2059986fd29aSsetje 	struct stat sb;
2060986fd29aSsetje 
20617c478bd9Sstevel@tonic-gate 	/*
2062986fd29aSsetje 	 * GRUB_DIR is required to modify the menu
20637c478bd9Sstevel@tonic-gate 	 */
20647c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, GRUB_DIR);
20657c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
2066986fd29aSsetje 		if (bam_debug)
20677c478bd9Sstevel@tonic-gate 			bam_print(DIR_MISS, path);
20687c478bd9Sstevel@tonic-gate 		return (0);
20697c478bd9Sstevel@tonic-gate 	}
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 	return (1);
20727c478bd9Sstevel@tonic-gate }
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate static int
20757c478bd9Sstevel@tonic-gate is_readonly(char *root)
20767c478bd9Sstevel@tonic-gate {
20777c478bd9Sstevel@tonic-gate 	struct statvfs vfs;
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 	/*
20807c478bd9Sstevel@tonic-gate 	 * Check for RDONLY filesystem
20817c478bd9Sstevel@tonic-gate 	 * When in doubt assume it is not readonly
20827c478bd9Sstevel@tonic-gate 	 */
20837c478bd9Sstevel@tonic-gate 	if (statvfs(root, &vfs) != 0) {
20847c478bd9Sstevel@tonic-gate 		if (bam_verbose)
20857c478bd9Sstevel@tonic-gate 			bam_error(STATVFS_FAIL, root, strerror(errno));
20867c478bd9Sstevel@tonic-gate 		return (0);
20877c478bd9Sstevel@tonic-gate 	}
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 	if (vfs.f_flag & ST_RDONLY) {
20907c478bd9Sstevel@tonic-gate 		return (1);
20917c478bd9Sstevel@tonic-gate 	}
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	return (0);
20947c478bd9Sstevel@tonic-gate }
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate static error_t
20977c478bd9Sstevel@tonic-gate update_archive(char *root, char *opt)
20987c478bd9Sstevel@tonic-gate {
20997c478bd9Sstevel@tonic-gate 	error_t ret;
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 	assert(root);
21027c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate 	/*
2105986fd29aSsetje 	 * root must belong to a boot archive based  OS,
21067c478bd9Sstevel@tonic-gate 	 */
2107986fd29aSsetje 	if (!is_boot_archive(root)) {
2108b610f78eSvikram 		/*
2109b610f78eSvikram 		 * Emit message only if not in context of update_all.
2110b610f78eSvikram 		 * If in update_all, emit only if verbose flag is set.
2111b610f78eSvikram 		 */
2112b610f78eSvikram 		if (!bam_update_all || bam_verbose)
2113b610f78eSvikram 			bam_print(NOT_GRUB_BOOT, root);
21147c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
21157c478bd9Sstevel@tonic-gate 	}
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 	/*
21188c1b6884Sszhou 	 * If smf check is requested when / is writable (can happen
21198c1b6884Sszhou 	 * on first reboot following an upgrade because service
21208c1b6884Sszhou 	 * dependency is messed up), skip the check.
21218c1b6884Sszhou 	 */
21228c1b6884Sszhou 	if (bam_smf_check && !bam_root_readonly)
21238c1b6884Sszhou 		return (BAM_SUCCESS);
21248c1b6884Sszhou 
21258c1b6884Sszhou 	/*
21268c1b6884Sszhou 	 * root must be writable. This check applies to alternate
21278c1b6884Sszhou 	 * root (-R option); bam_root_readonly applies to '/' only.
21287c478bd9Sstevel@tonic-gate 	 * Note: statvfs() does not always report the truth
21297c478bd9Sstevel@tonic-gate 	 */
213061cb17bdSsetje 	if (!bam_smf_check && !bam_check && is_readonly(root)) {
21318c1b6884Sszhou 		if (bam_verbose)
21327c478bd9Sstevel@tonic-gate 			bam_print(RDONLY_FS, root);
21337c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
21347c478bd9Sstevel@tonic-gate 	}
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate 	/*
21377c478bd9Sstevel@tonic-gate 	 * Don't generate archive on ramdisk
21387c478bd9Sstevel@tonic-gate 	 */
21397c478bd9Sstevel@tonic-gate 	if (is_ramdisk(root)) {
21407c478bd9Sstevel@tonic-gate 		if (bam_verbose)
21417c478bd9Sstevel@tonic-gate 			bam_print(SKIP_RAMDISK);
21427c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
21437c478bd9Sstevel@tonic-gate 	}
21447c478bd9Sstevel@tonic-gate 
21457c478bd9Sstevel@tonic-gate 	/*
21467c478bd9Sstevel@tonic-gate 	 * Now check if updated is really needed
21477c478bd9Sstevel@tonic-gate 	 */
21487c478bd9Sstevel@tonic-gate 	ret = update_required(root);
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	/*
21517c478bd9Sstevel@tonic-gate 	 * The check command (-n) is *not* a dry run
21527c478bd9Sstevel@tonic-gate 	 * It only checks if the archive is in sync.
21537c478bd9Sstevel@tonic-gate 	 */
21547c478bd9Sstevel@tonic-gate 	if (bam_check) {
21557c478bd9Sstevel@tonic-gate 		bam_exit((ret != 0) ? 1 : 0);
21567c478bd9Sstevel@tonic-gate 	}
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 	if (ret == 1) {
21597c478bd9Sstevel@tonic-gate 		/* create the ramdisk */
21607c478bd9Sstevel@tonic-gate 		ret = create_ramdisk(root);
21617c478bd9Sstevel@tonic-gate 	}
2162986fd29aSsetje 
2163986fd29aSsetje 	/* if the archive is updated, save the new stat data */
2164986fd29aSsetje 	if (ret == 0 && walk_arg.new_nvlp != NULL) {
2165986fd29aSsetje 		savenew(root);
2166986fd29aSsetje 	}
2167986fd29aSsetje 
2168986fd29aSsetje 	clear_walk_args();
2169986fd29aSsetje 
21707c478bd9Sstevel@tonic-gate 	return (ret);
21717c478bd9Sstevel@tonic-gate }
21727c478bd9Sstevel@tonic-gate 
2173fbac2b2bSvikram static void
2174fbac2b2bSvikram update_fdisk(void)
2175fbac2b2bSvikram {
2176fbac2b2bSvikram 	struct stat sb;
2177fbac2b2bSvikram 	char cmd[PATH_MAX];
2178fbac2b2bSvikram 	int ret1, ret2;
2179fbac2b2bSvikram 
2180fbac2b2bSvikram 	assert(stat(GRUB_fdisk, &sb) == 0);
2181fbac2b2bSvikram 	assert(stat(GRUB_fdisk_target, &sb) == 0);
2182fbac2b2bSvikram 
2183fbac2b2bSvikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/fdisk -F %s `/bin/cat %s`",
2184fbac2b2bSvikram 	    GRUB_fdisk, GRUB_fdisk_target);
2185fbac2b2bSvikram 
2186fbac2b2bSvikram 	bam_print(UPDATING_FDISK);
2187986fd29aSsetje 	if (exec_cmd(cmd, NULL) != 0) {
2188fbac2b2bSvikram 		bam_error(FDISK_UPDATE_FAILED);
2189fbac2b2bSvikram 	}
2190fbac2b2bSvikram 
2191fbac2b2bSvikram 	/*
2192fbac2b2bSvikram 	 * We are done, remove the files.
2193fbac2b2bSvikram 	 */
2194fbac2b2bSvikram 	ret1 = unlink(GRUB_fdisk);
2195fbac2b2bSvikram 	ret2 = unlink(GRUB_fdisk_target);
2196fbac2b2bSvikram 	if (ret1 != 0 || ret2 != 0) {
2197fbac2b2bSvikram 		bam_error(FILE_REMOVE_FAILED, GRUB_fdisk, GRUB_fdisk_target);
2198fbac2b2bSvikram 	}
2199fbac2b2bSvikram }
2200fbac2b2bSvikram 
2201b610f78eSvikram static void
2202b610f78eSvikram restore_grub_slice(void)
2203b610f78eSvikram {
2204b610f78eSvikram 	struct stat sb;
2205b610f78eSvikram 	char *mntpt, *physlice;
2206b610f78eSvikram 	int mnted;	/* set if we did a mount */
2207b610f78eSvikram 	char menupath[PATH_MAX], cmd[PATH_MAX];
2208b610f78eSvikram 
2209b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
2210b610f78eSvikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
2211b610f78eSvikram 		return;
2212b610f78eSvikram 	}
2213b610f78eSvikram 
2214b610f78eSvikram 	/*
2215b610f78eSvikram 	 * If we are doing an luactivate, don't attempt to restore GRUB or else
2216b610f78eSvikram 	 * we may not be able to get to DCA boot environments. Let luactivate
2217b610f78eSvikram 	 * handle GRUB/DCA installation
2218b610f78eSvikram 	 */
2219b610f78eSvikram 	if (stat(LU_ACTIVATE_FILE, &sb) == 0) {
2220b610f78eSvikram 		return;
2221b610f78eSvikram 	}
2222b610f78eSvikram 
2223b610f78eSvikram 	mnted = 0;
2224b610f78eSvikram 	physlice = NULL;
222540541d5dSvikram 	mntpt = mount_grub_slice(&mnted, &physlice, NULL, NULL);
2226b610f78eSvikram 	if (mntpt == NULL) {
2227b610f78eSvikram 		bam_error(CANNOT_RESTORE_GRUB_SLICE);
2228b610f78eSvikram 		return;
2229b610f78eSvikram 	}
2230b610f78eSvikram 
2231b610f78eSvikram 	(void) snprintf(menupath, sizeof (menupath), "%s%s", mntpt, GRUB_MENU);
2232b610f78eSvikram 	if (stat(menupath, &sb) == 0) {
223340541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2234b610f78eSvikram 		return;
2235b610f78eSvikram 	}
2236b610f78eSvikram 
2237b610f78eSvikram 	/*
2238b610f78eSvikram 	 * The menu is missing - we need to do a restore
2239b610f78eSvikram 	 */
2240b610f78eSvikram 	bam_print(RESTORING_GRUB);
2241b610f78eSvikram 
2242b610f78eSvikram 	(void) snprintf(cmd, sizeof (cmd), "%s %s %s %s",
2243b610f78eSvikram 	    INSTALLGRUB, STAGE1, STAGE2, physlice);
2244b610f78eSvikram 
2245986fd29aSsetje 	if (exec_cmd(cmd, NULL) != 0) {
2246b610f78eSvikram 		bam_error(RESTORE_GRUB_FAILED);
224740541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2248b610f78eSvikram 		return;
2249b610f78eSvikram 	}
2250b610f78eSvikram 
2251b610f78eSvikram 	if (stat(GRUB_backup_menu, &sb) != 0) {
2252b610f78eSvikram 		bam_error(MISSING_BACKUP_MENU,
2253b610f78eSvikram 		    GRUB_backup_menu, strerror(errno));
225440541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2255b610f78eSvikram 		return;
2256b610f78eSvikram 	}
2257b610f78eSvikram 
2258b610f78eSvikram 	(void) snprintf(cmd, sizeof (cmd), "/bin/cp %s %s",
2259b610f78eSvikram 	    GRUB_backup_menu, menupath);
2260b610f78eSvikram 
2261986fd29aSsetje 	if (exec_cmd(cmd, NULL) != 0) {
2262b610f78eSvikram 		bam_error(RESTORE_MENU_FAILED, menupath);
226340541d5dSvikram 		umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2264b610f78eSvikram 		return;
2265b610f78eSvikram 	}
2266b610f78eSvikram 
2267b610f78eSvikram 	/* Success */
226840541d5dSvikram 	umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
2269b610f78eSvikram }
2270b610f78eSvikram 
22717c478bd9Sstevel@tonic-gate static error_t
22727c478bd9Sstevel@tonic-gate update_all(char *root, char *opt)
22737c478bd9Sstevel@tonic-gate {
22747c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
22757c478bd9Sstevel@tonic-gate 	struct stat sb;
22767c478bd9Sstevel@tonic-gate 	FILE *fp;
22777c478bd9Sstevel@tonic-gate 	char multibt[PATH_MAX];
2278986fd29aSsetje 	char creatram[PATH_MAX];
22797c478bd9Sstevel@tonic-gate 	error_t ret = BAM_SUCCESS;
2280fbac2b2bSvikram 	int ret1, ret2;
22817c478bd9Sstevel@tonic-gate 
228240541d5dSvikram 	assert(root);
22837c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
22847c478bd9Sstevel@tonic-gate 
228540541d5dSvikram 	if (bam_rootlen != 1 || *root != '/') {
228640541d5dSvikram 		elide_trailing_slash(root, multibt, sizeof (multibt));
228740541d5dSvikram 		bam_error(ALT_ROOT_INVALID, multibt);
228840541d5dSvikram 		return (BAM_ERROR);
228940541d5dSvikram 	}
229040541d5dSvikram 
22917c478bd9Sstevel@tonic-gate 	/*
229298892a30Snadkarni 	 * Check to see if we are in the midst of safemode patching
229398892a30Snadkarni 	 * If so skip building the archive for /. Instead build it
229498892a30Snadkarni 	 * against the latest bits obtained by creating a fresh lofs
229598892a30Snadkarni 	 * mount of root.
22967c478bd9Sstevel@tonic-gate 	 */
229798892a30Snadkarni 	if (stat(LOFS_PATCH_FILE, &sb) == 0)  {
229898892a30Snadkarni 		if (mkdir(LOFS_PATCH_MNT, 0755) == -1 &&
229998892a30Snadkarni 		    errno != EEXIST) {
230098892a30Snadkarni 			bam_error(MKDIR_FAILED, "%s", LOFS_PATCH_MNT,
230198892a30Snadkarni 			    strerror(errno));
230298892a30Snadkarni 			ret = BAM_ERROR;
230398892a30Snadkarni 			goto out;
230498892a30Snadkarni 		}
230598892a30Snadkarni 		(void) snprintf(multibt, sizeof (multibt),
230698892a30Snadkarni 		    "/sbin/mount -F lofs -o nosub /  %s", LOFS_PATCH_MNT);
2307986fd29aSsetje 		if (exec_cmd(multibt, NULL) != 0) {
230898892a30Snadkarni 			bam_error(MOUNT_FAILED, LOFS_PATCH_MNT, "lofs");
230998892a30Snadkarni 			ret = BAM_ERROR;
231098892a30Snadkarni 		}
231198892a30Snadkarni 		if (ret != BAM_ERROR) {
231298892a30Snadkarni 			(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
231398892a30Snadkarni 			    LOFS_PATCH_MNT);
231498892a30Snadkarni 			bam_rootlen = strlen(rootbuf);
231598892a30Snadkarni 			if (update_archive(rootbuf, opt) != BAM_SUCCESS)
231698892a30Snadkarni 				ret = BAM_ERROR;
231795b1e0e9Snadkarni 			/*
231895b1e0e9Snadkarni 			 * unmount the lofs mount since there could be
231995b1e0e9Snadkarni 			 * multiple invocations of bootadm -a update_all
232095b1e0e9Snadkarni 			 */
232195b1e0e9Snadkarni 			(void) snprintf(multibt, sizeof (multibt),
232295b1e0e9Snadkarni 			    "/sbin/umount %s", LOFS_PATCH_MNT);
2323986fd29aSsetje 			if (exec_cmd(multibt, NULL) != 0) {
232495b1e0e9Snadkarni 				bam_error(UMOUNT_FAILED, LOFS_PATCH_MNT);
232595b1e0e9Snadkarni 				ret = BAM_ERROR;
232695b1e0e9Snadkarni 			}
232798892a30Snadkarni 		}
232898892a30Snadkarni 	} else {
232998892a30Snadkarni 		/*
233098892a30Snadkarni 		 * First update archive for current root
233198892a30Snadkarni 		 */
233298892a30Snadkarni 		if (update_archive(root, opt) != BAM_SUCCESS)
233398892a30Snadkarni 			ret = BAM_ERROR;
233498892a30Snadkarni 	}
233598892a30Snadkarni 
233698892a30Snadkarni 	if (ret == BAM_ERROR)
233798892a30Snadkarni 		goto out;
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 	/*
23407c478bd9Sstevel@tonic-gate 	 * Now walk the mount table, performing archive update
23417c478bd9Sstevel@tonic-gate 	 * for all mounted Newboot root filesystems
23427c478bd9Sstevel@tonic-gate 	 */
23437c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
23447c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
23457c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
2346b610f78eSvikram 		ret = BAM_ERROR;
2347b610f78eSvikram 		goto out;
23487c478bd9Sstevel@tonic-gate 	}
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
23537c478bd9Sstevel@tonic-gate 		if (mnt.mnt_special == NULL)
23547c478bd9Sstevel@tonic-gate 			continue;
23557c478bd9Sstevel@tonic-gate 		if (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0)
23567c478bd9Sstevel@tonic-gate 			continue;
23577c478bd9Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, "/") == 0)
23587c478bd9Sstevel@tonic-gate 			continue;
23597c478bd9Sstevel@tonic-gate 
2360986fd29aSsetje 		(void) snprintf(creatram, sizeof (creatram), "%s/%s",
2361986fd29aSsetje 		    mnt.mnt_mountp, CREATE_RAMDISK);
23627c478bd9Sstevel@tonic-gate 
2363986fd29aSsetje 		if (stat(creatram, &sb) == -1)
23647c478bd9Sstevel@tonic-gate 			continue;
23657c478bd9Sstevel@tonic-gate 
23667c478bd9Sstevel@tonic-gate 		/*
23677c478bd9Sstevel@tonic-gate 		 * We put a trailing slash to be consistent with root = "/"
23687c478bd9Sstevel@tonic-gate 		 * case, such that we don't have to print // in some cases.
23697c478bd9Sstevel@tonic-gate 		 */
23707c478bd9Sstevel@tonic-gate 		(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
23717c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp);
23727c478bd9Sstevel@tonic-gate 		bam_rootlen = strlen(rootbuf);
2373ae115bc7Smrj 
2374ae115bc7Smrj 		/*
2375ae115bc7Smrj 		 * It's possible that other mounts may be an alternate boot
2376ae115bc7Smrj 		 * architecture, so check it again.
2377ae115bc7Smrj 		 */
2378ae115bc7Smrj 		if ((dboot_or_multiboot(rootbuf) != BAM_SUCCESS) ||
2379ae115bc7Smrj 		    (update_archive(rootbuf, opt) != BAM_SUCCESS))
23807c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
23817c478bd9Sstevel@tonic-gate 	}
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
23847c478bd9Sstevel@tonic-gate 
2385b610f78eSvikram out:
2386b610f78eSvikram 	if (stat(GRUB_slice, &sb) == 0) {
2387b610f78eSvikram 		restore_grub_slice();
2388b610f78eSvikram 	}
2389b610f78eSvikram 
2390fbac2b2bSvikram 	/*
2391fbac2b2bSvikram 	 * Update fdisk table as we go down. Updating it when
2392fbac2b2bSvikram 	 * the system is running will confuse biosdev.
2393fbac2b2bSvikram 	 */
2394fbac2b2bSvikram 	ret1 = stat(GRUB_fdisk, &sb);
2395fbac2b2bSvikram 	ret2 = stat(GRUB_fdisk_target, &sb);
2396fbac2b2bSvikram 	if ((ret1 == 0) && (ret2 == 0)) {
2397fbac2b2bSvikram 		update_fdisk();
2398fbac2b2bSvikram 	} else if ((ret1 == 0) ^ (ret2 == 0)) {
2399fbac2b2bSvikram 		/*
2400fbac2b2bSvikram 		 * It is an error for one file to be
2401fbac2b2bSvikram 		 * present and the other absent.
2402fbac2b2bSvikram 		 * It is normal for both files to be
2403fbac2b2bSvikram 		 * absent - it indicates that no fdisk
2404fbac2b2bSvikram 		 * update is required.
2405fbac2b2bSvikram 		 */
2406fbac2b2bSvikram 		bam_error(MISSING_FDISK_FILE,
2407fbac2b2bSvikram 		    ret1 ? GRUB_fdisk : GRUB_fdisk_target);
2408fbac2b2bSvikram 		ret = BAM_ERROR;
2409fbac2b2bSvikram 	}
2410fbac2b2bSvikram 
24117c478bd9Sstevel@tonic-gate 	return (ret);
24127c478bd9Sstevel@tonic-gate }
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate static void
24157c478bd9Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp)
24167c478bd9Sstevel@tonic-gate {
24177c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
24187c478bd9Sstevel@tonic-gate 		mp->start = lp;
24197c478bd9Sstevel@tonic-gate 	} else {
24207c478bd9Sstevel@tonic-gate 		mp->end->next = lp;
24218c1b6884Sszhou 		lp->prev = mp->end;
24227c478bd9Sstevel@tonic-gate 	}
24237c478bd9Sstevel@tonic-gate 	mp->end = lp;
24247c478bd9Sstevel@tonic-gate }
24257c478bd9Sstevel@tonic-gate 
24268c1b6884Sszhou static void
24278c1b6884Sszhou unlink_line(menu_t *mp, line_t *lp)
24288c1b6884Sszhou {
24298c1b6884Sszhou 	/* unlink from list */
24308c1b6884Sszhou 	if (lp->prev)
24318c1b6884Sszhou 		lp->prev->next = lp->next;
24328c1b6884Sszhou 	else
24338c1b6884Sszhou 		mp->start = lp->next;
24348c1b6884Sszhou 	if (lp->next)
24358c1b6884Sszhou 		lp->next->prev = lp->prev;
24368c1b6884Sszhou 	else
24378c1b6884Sszhou 		mp->end = lp->prev;
24388c1b6884Sszhou }
24398c1b6884Sszhou 
24408c1b6884Sszhou static entry_t *
24418c1b6884Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end)
24428c1b6884Sszhou {
24438c1b6884Sszhou 	entry_t *ent, *prev;
24448c1b6884Sszhou 
24458c1b6884Sszhou 	ent = s_calloc(1, sizeof (entry_t));
24468c1b6884Sszhou 	ent->start = start;
24478c1b6884Sszhou 	ent->end = end;
24488c1b6884Sszhou 
24498c1b6884Sszhou 	if (mp->entries == NULL) {
24508c1b6884Sszhou 		mp->entries = ent;
24518c1b6884Sszhou 		return (ent);
24528c1b6884Sszhou 	}
24538c1b6884Sszhou 
24548c1b6884Sszhou 	prev = mp->entries;
24558c1b6884Sszhou 	while (prev->next)
24568c1b6884Sszhou 		prev = prev-> next;
24578c1b6884Sszhou 	prev->next = ent;
24588c1b6884Sszhou 	ent->prev = prev;
24598c1b6884Sszhou 	return (ent);
24608c1b6884Sszhou }
24618c1b6884Sszhou 
24628c1b6884Sszhou static void
24638c1b6884Sszhou boot_entry_addline(entry_t *ent, line_t *lp)
24648c1b6884Sszhou {
24658c1b6884Sszhou 	if (ent)
24668c1b6884Sszhou 		ent->end = lp;
24678c1b6884Sszhou }
24688c1b6884Sszhou 
2469843e1988Sjohnlev /*
2470843e1988Sjohnlev  * Check whether cmd matches the one indexed by which, and whether arg matches
2471843e1988Sjohnlev  * str.  which must be either KERNEL_CMD or MODULE_CMD, and a match to the
2472843e1988Sjohnlev  * respective *_DOLLAR_CMD is also acceptable.  The arg is searched using
2473843e1988Sjohnlev  * strstr(), so it can be a partial match.
2474843e1988Sjohnlev  */
2475843e1988Sjohnlev static int
2476843e1988Sjohnlev check_cmd(const char *cmd, const int which, const char *arg, const char *str)
2477843e1988Sjohnlev {
2478843e1988Sjohnlev 	if ((strcmp(cmd, menu_cmds[which]) != 0) &&
2479843e1988Sjohnlev 	    (strcmp(cmd, menu_cmds[which + 1]) != 0)) {
2480843e1988Sjohnlev 		return (0);
2481843e1988Sjohnlev 	}
2482843e1988Sjohnlev 	return (strstr(arg, str) != NULL);
2483843e1988Sjohnlev }
2484843e1988Sjohnlev 
24857c478bd9Sstevel@tonic-gate /*
24867c478bd9Sstevel@tonic-gate  * A line in menu.lst looks like
24877c478bd9Sstevel@tonic-gate  * [ ]*<cmd>[ \t=]*<arg>*
24887c478bd9Sstevel@tonic-gate  */
24897c478bd9Sstevel@tonic-gate static void
24907c478bd9Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum)
24917c478bd9Sstevel@tonic-gate {
24927c478bd9Sstevel@tonic-gate 	/*
24937c478bd9Sstevel@tonic-gate 	 * save state across calls. This is so that
24947c478bd9Sstevel@tonic-gate 	 * header gets the right entry# after title has
24957c478bd9Sstevel@tonic-gate 	 * been processed
24967c478bd9Sstevel@tonic-gate 	 */
24978c1b6884Sszhou 	static line_t *prev = NULL;
24988c1b6884Sszhou 	static entry_t *curr_ent = NULL;
2499ae115bc7Smrj 	static int in_liveupgrade = 0;
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 	line_t	*lp;
25027c478bd9Sstevel@tonic-gate 	char *cmd, *sep, *arg;
25037c478bd9Sstevel@tonic-gate 	char save, *cp, *line;
25047c478bd9Sstevel@tonic-gate 	menu_flag_t flag = BAM_INVALID;
25057c478bd9Sstevel@tonic-gate 
25067c478bd9Sstevel@tonic-gate 	if (str == NULL) {
25077c478bd9Sstevel@tonic-gate 		return;
25087c478bd9Sstevel@tonic-gate 	}
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate 	/*
25117c478bd9Sstevel@tonic-gate 	 * First save a copy of the entire line.
25127c478bd9Sstevel@tonic-gate 	 * We use this later to set the line field.
25137c478bd9Sstevel@tonic-gate 	 */
25147c478bd9Sstevel@tonic-gate 	line = s_strdup(str);
25157c478bd9Sstevel@tonic-gate 
25167c478bd9Sstevel@tonic-gate 	/* Eat up leading whitespace */
25177c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
25187c478bd9Sstevel@tonic-gate 		str++;
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 	if (*str == '#') {		/* comment */
25217c478bd9Sstevel@tonic-gate 		cmd = s_strdup("#");
25227c478bd9Sstevel@tonic-gate 		sep = NULL;
25237c478bd9Sstevel@tonic-gate 		arg = s_strdup(str + 1);
25247c478bd9Sstevel@tonic-gate 		flag = BAM_COMMENT;
2525ae115bc7Smrj 		if (strstr(arg, BAM_LU_HDR) != NULL) {
2526ae115bc7Smrj 			in_liveupgrade = 1;
2527ae115bc7Smrj 		} else if (strstr(arg, BAM_LU_FTR) != NULL) {
2528ae115bc7Smrj 			in_liveupgrade = 0;
2529ae115bc7Smrj 		}
25307c478bd9Sstevel@tonic-gate 	} else if (*str == '\0') {	/* blank line */
25317c478bd9Sstevel@tonic-gate 		cmd = sep = arg = NULL;
25327c478bd9Sstevel@tonic-gate 		flag = BAM_EMPTY;
25337c478bd9Sstevel@tonic-gate 	} else {
25347c478bd9Sstevel@tonic-gate 		/*
25357c478bd9Sstevel@tonic-gate 		 * '=' is not a documented separator in grub syntax.
25367c478bd9Sstevel@tonic-gate 		 * However various development bits use '=' as a
25377c478bd9Sstevel@tonic-gate 		 * separator. In addition, external users also
25387c478bd9Sstevel@tonic-gate 		 * use = as a separator. So we will allow that usage.
25397c478bd9Sstevel@tonic-gate 		 */
25407c478bd9Sstevel@tonic-gate 		cp = str;
25417c478bd9Sstevel@tonic-gate 		while (*str != ' ' && *str != '\t' && *str != '=') {
25427c478bd9Sstevel@tonic-gate 			if (*str == '\0') {
25437c478bd9Sstevel@tonic-gate 				cmd = s_strdup(cp);
25447c478bd9Sstevel@tonic-gate 				sep = arg = NULL;
25457c478bd9Sstevel@tonic-gate 				break;
25467c478bd9Sstevel@tonic-gate 			}
25477c478bd9Sstevel@tonic-gate 			str++;
25487c478bd9Sstevel@tonic-gate 		}
25497c478bd9Sstevel@tonic-gate 
25507c478bd9Sstevel@tonic-gate 		if (*str != '\0') {
25517c478bd9Sstevel@tonic-gate 			save = *str;
25527c478bd9Sstevel@tonic-gate 			*str = '\0';
25537c478bd9Sstevel@tonic-gate 			cmd = s_strdup(cp);
25547c478bd9Sstevel@tonic-gate 			*str = save;
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate 			str++;
25577c478bd9Sstevel@tonic-gate 			save = *str;
25587c478bd9Sstevel@tonic-gate 			*str = '\0';
25597c478bd9Sstevel@tonic-gate 			sep = s_strdup(str - 1);
25607c478bd9Sstevel@tonic-gate 			*str = save;
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 			while (*str == ' ' || *str == '\t')
25637c478bd9Sstevel@tonic-gate 				str++;
25647c478bd9Sstevel@tonic-gate 			if (*str == '\0')
25657c478bd9Sstevel@tonic-gate 				arg = NULL;
25667c478bd9Sstevel@tonic-gate 			else
25677c478bd9Sstevel@tonic-gate 				arg = s_strdup(str);
25687c478bd9Sstevel@tonic-gate 		}
25697c478bd9Sstevel@tonic-gate 	}
25707c478bd9Sstevel@tonic-gate 
25717c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 	lp->cmd = cmd;
25747c478bd9Sstevel@tonic-gate 	lp->sep = sep;
25757c478bd9Sstevel@tonic-gate 	lp->arg = arg;
25767c478bd9Sstevel@tonic-gate 	lp->line = line;
25777c478bd9Sstevel@tonic-gate 	lp->lineNum = ++(*lineNum);
25787c478bd9Sstevel@tonic-gate 	if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) {
25797c478bd9Sstevel@tonic-gate 		lp->entryNum = ++(*entryNum);
25807c478bd9Sstevel@tonic-gate 		lp->flags = BAM_TITLE;
25817c478bd9Sstevel@tonic-gate 		if (prev && prev->flags == BAM_COMMENT &&
2582ae115bc7Smrj 		    prev->arg && strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
25837c478bd9Sstevel@tonic-gate 			prev->entryNum = lp->entryNum;
25848c1b6884Sszhou 			curr_ent = boot_entry_new(mp, prev, lp);
2585ae115bc7Smrj 			curr_ent->flags = BAM_ENTRY_BOOTADM;
25868c1b6884Sszhou 		} else {
25878c1b6884Sszhou 			curr_ent = boot_entry_new(mp, lp, lp);
2588ae115bc7Smrj 			if (in_liveupgrade) {
2589ae115bc7Smrj 				curr_ent->flags = BAM_ENTRY_LU;
2590ae115bc7Smrj 			}
25918c1b6884Sszhou 		}
2592ae115bc7Smrj 		curr_ent->entryNum = *entryNum;
25937c478bd9Sstevel@tonic-gate 	} else if (flag != BAM_INVALID) {
25947c478bd9Sstevel@tonic-gate 		/*
25957c478bd9Sstevel@tonic-gate 		 * For header comments, the entry# is "fixed up"
25967c478bd9Sstevel@tonic-gate 		 * by the subsequent title
25977c478bd9Sstevel@tonic-gate 		 */
25987c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
25997c478bd9Sstevel@tonic-gate 		lp->flags = flag;
26007c478bd9Sstevel@tonic-gate 	} else {
26017c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
2602ae115bc7Smrj 
2603ae115bc7Smrj 		if (*entryNum == ENTRY_INIT) {
2604ae115bc7Smrj 			lp->flags = BAM_GLOBAL;
2605ae115bc7Smrj 		} else {
2606ae115bc7Smrj 			lp->flags = BAM_ENTRY;
2607ae115bc7Smrj 
2608ae115bc7Smrj 			if (cmd && arg) {
2609ae115bc7Smrj 				/*
2610ae115bc7Smrj 				 * We only compare for the length of "module"
2611ae115bc7Smrj 				 * so that "module$" will also match.
2612ae115bc7Smrj 				 */
2613843e1988Sjohnlev 				if (check_cmd(cmd, MODULE_CMD, arg, MINIROOT))
2614ae115bc7Smrj 					curr_ent->flags |= BAM_ENTRY_MINIROOT;
2615843e1988Sjohnlev 				else if (check_cmd(cmd, KERNEL_CMD, arg,
2616843e1988Sjohnlev 				    "xen.gz"))
2617843e1988Sjohnlev 					curr_ent->flags |= BAM_ENTRY_HV;
2618ae115bc7Smrj 				else if (strcmp(cmd, menu_cmds[ROOT_CMD]) == 0)
2619ae115bc7Smrj 					curr_ent->flags |= BAM_ENTRY_ROOT;
2620ae115bc7Smrj 				else if (strcmp(cmd,
2621ae115bc7Smrj 				    menu_cmds[CHAINLOADER_CMD]) == 0)
2622ae115bc7Smrj 					curr_ent->flags |=
2623ae115bc7Smrj 					    BAM_ENTRY_CHAINLOADER;
2624ae115bc7Smrj 			}
2625ae115bc7Smrj 		}
26267c478bd9Sstevel@tonic-gate 	}
26277c478bd9Sstevel@tonic-gate 
26288c1b6884Sszhou 	/* record default, old default, and entry line ranges */
26298c1b6884Sszhou 	if (lp->flags == BAM_GLOBAL &&
26308c1b6884Sszhou 	    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) {
26318c1b6884Sszhou 		mp->curdefault = lp;
26328c1b6884Sszhou 	} else if (lp->flags == BAM_COMMENT &&
26338c1b6884Sszhou 	    strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) {
26348c1b6884Sszhou 		mp->olddefault = lp;
2635ae115bc7Smrj 	} else if (lp->flags == BAM_COMMENT &&
2636ae115bc7Smrj 	    strncmp(lp->arg, BAM_OLD_RC_DEF, strlen(BAM_OLD_RC_DEF)) == 0) {
2637ae115bc7Smrj 		mp->old_rc_default = lp;
26388c1b6884Sszhou 	} else if (lp->flags == BAM_ENTRY ||
2639ae115bc7Smrj 	    (lp->flags == BAM_COMMENT &&
2640ae115bc7Smrj 	    strcmp(lp->arg, BAM_BOOTADM_FTR) == 0)) {
26418c1b6884Sszhou 		boot_entry_addline(curr_ent, lp);
26428c1b6884Sszhou 	}
26437c478bd9Sstevel@tonic-gate 	append_line(mp, lp);
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate 	prev = lp;
26467c478bd9Sstevel@tonic-gate }
26477c478bd9Sstevel@tonic-gate 
264840541d5dSvikram static void
264940541d5dSvikram update_numbering(menu_t *mp)
265040541d5dSvikram {
265140541d5dSvikram 	int lineNum;
265240541d5dSvikram 	int entryNum;
265340541d5dSvikram 	int old_default_value;
265440541d5dSvikram 	line_t *lp, *prev, *default_lp, *default_entry;
265540541d5dSvikram 	char buf[PATH_MAX];
265640541d5dSvikram 
265740541d5dSvikram 	if (mp->start == NULL) {
265840541d5dSvikram 		return;
265940541d5dSvikram 	}
266040541d5dSvikram 
266140541d5dSvikram 	lineNum = LINE_INIT;
266240541d5dSvikram 	entryNum = ENTRY_INIT;
266340541d5dSvikram 	old_default_value = ENTRY_INIT;
266440541d5dSvikram 	lp = default_lp = default_entry = NULL;
266540541d5dSvikram 
266640541d5dSvikram 	prev = NULL;
266740541d5dSvikram 	for (lp = mp->start; lp; prev = lp, lp = lp->next) {
266840541d5dSvikram 		lp->lineNum = ++lineNum;
266940541d5dSvikram 
267040541d5dSvikram 		/*
267140541d5dSvikram 		 * Get the value of the default command
267240541d5dSvikram 		 */
267340541d5dSvikram 		if (lp->entryNum == ENTRY_INIT && lp->cmd &&
267440541d5dSvikram 		    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 &&
267540541d5dSvikram 		    lp->arg) {
267640541d5dSvikram 			old_default_value = atoi(lp->arg);
267740541d5dSvikram 			default_lp = lp;
267840541d5dSvikram 		}
267940541d5dSvikram 
268040541d5dSvikram 		/*
268140541d5dSvikram 		 * If not boot entry, nothing else to fix for this
268240541d5dSvikram 		 * entry
268340541d5dSvikram 		 */
268440541d5dSvikram 		if (lp->entryNum == ENTRY_INIT)
268540541d5dSvikram 			continue;
268640541d5dSvikram 
268740541d5dSvikram 		/*
268840541d5dSvikram 		 * Record the position of the default entry.
268940541d5dSvikram 		 * The following works because global
269040541d5dSvikram 		 * commands like default and timeout should precede
269140541d5dSvikram 		 * actual boot entries, so old_default_value
269240541d5dSvikram 		 * is already known (or default cmd is missing).
269340541d5dSvikram 		 */
269440541d5dSvikram 		if (default_entry == NULL &&
269540541d5dSvikram 		    old_default_value != ENTRY_INIT &&
269640541d5dSvikram 		    lp->entryNum == old_default_value) {
269740541d5dSvikram 			default_entry = lp;
269840541d5dSvikram 		}
269940541d5dSvikram 
270040541d5dSvikram 		/*
270140541d5dSvikram 		 * Now fixup the entry number
270240541d5dSvikram 		 */
270340541d5dSvikram 		if (lp->cmd && strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) {
270440541d5dSvikram 			lp->entryNum = ++entryNum;
270540541d5dSvikram 			/* fixup the bootadm header */
270640541d5dSvikram 			if (prev && prev->flags == BAM_COMMENT &&
2707ae115bc7Smrj 			    prev->arg &&
2708ae115bc7Smrj 			    strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
270940541d5dSvikram 				prev->entryNum = lp->entryNum;
271040541d5dSvikram 			}
271140541d5dSvikram 		} else {
271240541d5dSvikram 			lp->entryNum = entryNum;
271340541d5dSvikram 		}
271440541d5dSvikram 	}
271540541d5dSvikram 
271640541d5dSvikram 	/*
271740541d5dSvikram 	 * No default command in menu, simply return
271840541d5dSvikram 	 */
271940541d5dSvikram 	if (default_lp == NULL) {
272040541d5dSvikram 		return;
272140541d5dSvikram 	}
272240541d5dSvikram 
272340541d5dSvikram 	free(default_lp->arg);
272440541d5dSvikram 	free(default_lp->line);
272540541d5dSvikram 
272640541d5dSvikram 	if (default_entry == NULL) {
272740541d5dSvikram 		default_lp->arg = s_strdup("0");
272840541d5dSvikram 	} else {
272940541d5dSvikram 		(void) snprintf(buf, sizeof (buf), "%d",
273040541d5dSvikram 		    default_entry->entryNum);
273140541d5dSvikram 		default_lp->arg = s_strdup(buf);
273240541d5dSvikram 	}
273340541d5dSvikram 
273440541d5dSvikram 	/*
273540541d5dSvikram 	 * The following is required since only the line field gets
273640541d5dSvikram 	 * written back to menu.lst
273740541d5dSvikram 	 */
273840541d5dSvikram 	(void) snprintf(buf, sizeof (buf), "%s%s%s",
273940541d5dSvikram 	    menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg);
274040541d5dSvikram 	default_lp->line = s_strdup(buf);
274140541d5dSvikram }
274240541d5dSvikram 
274340541d5dSvikram 
27447c478bd9Sstevel@tonic-gate static menu_t *
27457c478bd9Sstevel@tonic-gate menu_read(char *menu_path)
27467c478bd9Sstevel@tonic-gate {
27477c478bd9Sstevel@tonic-gate 	FILE *fp;
27487c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE], *cp;
27497c478bd9Sstevel@tonic-gate 	menu_t *mp;
27507c478bd9Sstevel@tonic-gate 	int line, entry, len, n;
27517c478bd9Sstevel@tonic-gate 
27527c478bd9Sstevel@tonic-gate 	mp = s_calloc(1, sizeof (menu_t));
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 	fp = fopen(menu_path, "r");
27557c478bd9Sstevel@tonic-gate 	if (fp == NULL) { /* Let the caller handle this error */
27567c478bd9Sstevel@tonic-gate 		return (mp);
27577c478bd9Sstevel@tonic-gate 	}
27587c478bd9Sstevel@tonic-gate 
27597c478bd9Sstevel@tonic-gate 
27607c478bd9Sstevel@tonic-gate 	/* Note: GRUB boot entry number starts with 0 */
27617c478bd9Sstevel@tonic-gate 	line = LINE_INIT;
27627c478bd9Sstevel@tonic-gate 	entry = ENTRY_INIT;
27637c478bd9Sstevel@tonic-gate 	cp = buf;
27647c478bd9Sstevel@tonic-gate 	len = sizeof (buf);
27657c478bd9Sstevel@tonic-gate 	while (s_fgets(cp, len, fp) != NULL) {
27667c478bd9Sstevel@tonic-gate 		n = strlen(cp);
27677c478bd9Sstevel@tonic-gate 		if (cp[n - 1] == '\\') {
27687c478bd9Sstevel@tonic-gate 			len -= n - 1;
27697c478bd9Sstevel@tonic-gate 			assert(len >= 2);
27707c478bd9Sstevel@tonic-gate 			cp += n - 1;
27717c478bd9Sstevel@tonic-gate 			continue;
27727c478bd9Sstevel@tonic-gate 		}
27737c478bd9Sstevel@tonic-gate 		line_parser(mp, buf, &line, &entry);
27747c478bd9Sstevel@tonic-gate 		cp = buf;
27757c478bd9Sstevel@tonic-gate 		len = sizeof (buf);
27767c478bd9Sstevel@tonic-gate 	}
27777c478bd9Sstevel@tonic-gate 
27787c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
27797c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, menu_path, strerror(errno));
27807c478bd9Sstevel@tonic-gate 	}
27817c478bd9Sstevel@tonic-gate 
27827c478bd9Sstevel@tonic-gate 	return (mp);
27837c478bd9Sstevel@tonic-gate }
27847c478bd9Sstevel@tonic-gate 
27857c478bd9Sstevel@tonic-gate static error_t
27867c478bd9Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title)
27877c478bd9Sstevel@tonic-gate {
27887c478bd9Sstevel@tonic-gate 	char *eq;
27897c478bd9Sstevel@tonic-gate 	char *opt_dup;
27907c478bd9Sstevel@tonic-gate 	int entryNum;
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate 	assert(mp);
27937c478bd9Sstevel@tonic-gate 	assert(mp->start);
27947c478bd9Sstevel@tonic-gate 	assert(opt);
27957c478bd9Sstevel@tonic-gate 
27967c478bd9Sstevel@tonic-gate 	opt_dup = s_strdup(opt);
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate 	if (entry)
27997c478bd9Sstevel@tonic-gate 		*entry = ENTRY_INIT;
28007c478bd9Sstevel@tonic-gate 	if (title)
28017c478bd9Sstevel@tonic-gate 		*title = NULL;
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 	eq = strchr(opt_dup, '=');
28047c478bd9Sstevel@tonic-gate 	if (eq == NULL) {
28057c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
28067c478bd9Sstevel@tonic-gate 		free(opt_dup);
28077c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
28087c478bd9Sstevel@tonic-gate 	}
28097c478bd9Sstevel@tonic-gate 
28107c478bd9Sstevel@tonic-gate 	*eq = '\0';
28117c478bd9Sstevel@tonic-gate 	if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) {
28127c478bd9Sstevel@tonic-gate 		assert(mp->end);
28137c478bd9Sstevel@tonic-gate 		entryNum = s_strtol(eq + 1);
28147c478bd9Sstevel@tonic-gate 		if (entryNum < 0 || entryNum > mp->end->entryNum) {
28157c478bd9Sstevel@tonic-gate 			bam_error(INVALID_ENTRY, eq + 1);
28167c478bd9Sstevel@tonic-gate 			free(opt_dup);
28177c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
28187c478bd9Sstevel@tonic-gate 		}
28197c478bd9Sstevel@tonic-gate 		*entry = entryNum;
28207c478bd9Sstevel@tonic-gate 	} else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) {
28217c478bd9Sstevel@tonic-gate 		*title = opt + (eq - opt_dup) + 1;
28227c478bd9Sstevel@tonic-gate 	} else {
28237c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
28247c478bd9Sstevel@tonic-gate 		free(opt_dup);
28257c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
28267c478bd9Sstevel@tonic-gate 	}
28277c478bd9Sstevel@tonic-gate 
28287c478bd9Sstevel@tonic-gate 	free(opt_dup);
28297c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
28307c478bd9Sstevel@tonic-gate }
28317c478bd9Sstevel@tonic-gate 
28327c478bd9Sstevel@tonic-gate /*
28337c478bd9Sstevel@tonic-gate  * If invoked with no titles/entries (opt == NULL)
28347c478bd9Sstevel@tonic-gate  * only title lines in file are printed.
28357c478bd9Sstevel@tonic-gate  *
28367c478bd9Sstevel@tonic-gate  * If invoked with a title or entry #, all
28377c478bd9Sstevel@tonic-gate  * lines in *every* matching entry are listed
28387c478bd9Sstevel@tonic-gate  */
28397c478bd9Sstevel@tonic-gate static error_t
28407c478bd9Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt)
28417c478bd9Sstevel@tonic-gate {
28427c478bd9Sstevel@tonic-gate 	line_t *lp;
28437c478bd9Sstevel@tonic-gate 	int entry = ENTRY_INIT;
28447c478bd9Sstevel@tonic-gate 	int found;
28457c478bd9Sstevel@tonic-gate 	char *title = NULL;
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 	assert(mp);
28487c478bd9Sstevel@tonic-gate 	assert(menu_path);
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
28517c478bd9Sstevel@tonic-gate 		bam_error(NO_MENU, menu_path);
28527c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
28537c478bd9Sstevel@tonic-gate 	}
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate 	if (opt != NULL) {
28567c478bd9Sstevel@tonic-gate 		if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
28577c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
28587c478bd9Sstevel@tonic-gate 		}
28597c478bd9Sstevel@tonic-gate 		assert((entry != ENTRY_INIT) ^ (title != NULL));
28607c478bd9Sstevel@tonic-gate 	} else {
28617c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0);
28627c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0);
28637c478bd9Sstevel@tonic-gate 	}
28647c478bd9Sstevel@tonic-gate 
28657c478bd9Sstevel@tonic-gate 	found = 0;
28667c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
28677c478bd9Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY)
28687c478bd9Sstevel@tonic-gate 			continue;
28697c478bd9Sstevel@tonic-gate 		if (opt == NULL && lp->flags == BAM_TITLE) {
28707c478bd9Sstevel@tonic-gate 			bam_print(PRINT_TITLE, lp->entryNum,
28717c478bd9Sstevel@tonic-gate 			    lp->arg);
28727c478bd9Sstevel@tonic-gate 			found = 1;
28737c478bd9Sstevel@tonic-gate 			continue;
28747c478bd9Sstevel@tonic-gate 		}
28757c478bd9Sstevel@tonic-gate 		if (entry != ENTRY_INIT && lp->entryNum == entry) {
28767c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
28777c478bd9Sstevel@tonic-gate 			found = 1;
28787c478bd9Sstevel@tonic-gate 			continue;
28797c478bd9Sstevel@tonic-gate 		}
28807c478bd9Sstevel@tonic-gate 
28817c478bd9Sstevel@tonic-gate 		/*
28827c478bd9Sstevel@tonic-gate 		 * We set the entry value here so that all lines
28837c478bd9Sstevel@tonic-gate 		 * in entry get printed. If we subsequently match
28847c478bd9Sstevel@tonic-gate 		 * title in other entries, all lines in those
28857c478bd9Sstevel@tonic-gate 		 * entries get printed as well.
28867c478bd9Sstevel@tonic-gate 		 */
28877c478bd9Sstevel@tonic-gate 		if (title && lp->flags == BAM_TITLE && lp->arg &&
28887c478bd9Sstevel@tonic-gate 		    strncmp(title, lp->arg, strlen(title)) == 0) {
28897c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
28907c478bd9Sstevel@tonic-gate 			entry = lp->entryNum;
28917c478bd9Sstevel@tonic-gate 			found = 1;
28927c478bd9Sstevel@tonic-gate 			continue;
28937c478bd9Sstevel@tonic-gate 		}
28947c478bd9Sstevel@tonic-gate 	}
28957c478bd9Sstevel@tonic-gate 
28967c478bd9Sstevel@tonic-gate 	if (!found) {
28977c478bd9Sstevel@tonic-gate 		bam_error(NO_MATCH_ENTRY);
28987c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
28997c478bd9Sstevel@tonic-gate 	}
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
29027c478bd9Sstevel@tonic-gate }
29037c478bd9Sstevel@tonic-gate 
2904843e1988Sjohnlev int
29057c478bd9Sstevel@tonic-gate add_boot_entry(menu_t *mp,
29067c478bd9Sstevel@tonic-gate 	char *title,
29077c478bd9Sstevel@tonic-gate 	char *root,
29087c478bd9Sstevel@tonic-gate 	char *kernel,
2909843e1988Sjohnlev 	char *mod_kernel,
29107c478bd9Sstevel@tonic-gate 	char *module)
29117c478bd9Sstevel@tonic-gate {
29127c478bd9Sstevel@tonic-gate 	int lineNum, entryNum;
29137c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
2914ae115bc7Smrj 	menu_cmd_t k_cmd, m_cmd;
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate 	assert(mp);
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate 	if (title == NULL) {
2919ee3b8144Sszhou 		title = "Solaris";	/* default to Solaris */
29207c478bd9Sstevel@tonic-gate 	}
29217c478bd9Sstevel@tonic-gate 	if (kernel == NULL) {
29227c478bd9Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]);
29237c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
29247c478bd9Sstevel@tonic-gate 	}
29257c478bd9Sstevel@tonic-gate 	if (module == NULL) {
2926ae115bc7Smrj 		if (bam_direct != BAM_DIRECT_DBOOT) {
2927ae115bc7Smrj 			bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]);
2928ae115bc7Smrj 			return (BAM_ERROR);
2929ae115bc7Smrj 		}
2930ae115bc7Smrj 
2931ae115bc7Smrj 		/* Figure the commands out from the kernel line */
2932ae115bc7Smrj 		if (strstr(kernel, "$ISADIR") != NULL) {
2933ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE;
2934ae115bc7Smrj 			k_cmd = KERNEL_DOLLAR_CMD;
2935ae115bc7Smrj 			m_cmd = MODULE_DOLLAR_CMD;
2936ae115bc7Smrj 		} else if (strstr(kernel, "amd64") != NULL) {
2937ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE_64;
2938ae115bc7Smrj 			k_cmd = KERNEL_CMD;
2939ae115bc7Smrj 			m_cmd = MODULE_CMD;
2940ae115bc7Smrj 		} else {
2941ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE_32;
2942ae115bc7Smrj 			k_cmd = KERNEL_CMD;
2943ae115bc7Smrj 			m_cmd = MODULE_CMD;
2944ae115bc7Smrj 		}
2945ae115bc7Smrj 	} else if ((bam_direct == BAM_DIRECT_DBOOT) &&
2946ae115bc7Smrj 	    (strstr(kernel, "$ISADIR") != NULL)) {
2947ae115bc7Smrj 		/*
2948ae115bc7Smrj 		 * If it's a non-failsafe dboot kernel, use the "kernel$"
2949ae115bc7Smrj 		 * command.  Otherwise, use "kernel".
2950ae115bc7Smrj 		 */
2951ae115bc7Smrj 		k_cmd = KERNEL_DOLLAR_CMD;
2952ae115bc7Smrj 		m_cmd = MODULE_DOLLAR_CMD;
2953ae115bc7Smrj 	} else {
2954ae115bc7Smrj 		k_cmd = KERNEL_CMD;
2955ae115bc7Smrj 		m_cmd = MODULE_CMD;
29567c478bd9Sstevel@tonic-gate 	}
29577c478bd9Sstevel@tonic-gate 
29587c478bd9Sstevel@tonic-gate 	if (mp->start) {
29597c478bd9Sstevel@tonic-gate 		lineNum = mp->end->lineNum;
29607c478bd9Sstevel@tonic-gate 		entryNum = mp->end->entryNum;
29617c478bd9Sstevel@tonic-gate 	} else {
29627c478bd9Sstevel@tonic-gate 		lineNum = LINE_INIT;
29637c478bd9Sstevel@tonic-gate 		entryNum = ENTRY_INIT;
29647c478bd9Sstevel@tonic-gate 	}
29657c478bd9Sstevel@tonic-gate 
29667c478bd9Sstevel@tonic-gate 	/*
29677c478bd9Sstevel@tonic-gate 	 * No separator for comment (HDR/FTR) commands
29687c478bd9Sstevel@tonic-gate 	 * The syntax for comments is #<comment>
29697c478bd9Sstevel@tonic-gate 	 */
29707c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
2971ae115bc7Smrj 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_HDR);
29728c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
29737c478bd9Sstevel@tonic-gate 
29747c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
29757c478bd9Sstevel@tonic-gate 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
29768c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
29777c478bd9Sstevel@tonic-gate 
29788c1b6884Sszhou 	if (root) {
29798c1b6884Sszhou 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
29808c1b6884Sszhou 		    menu_cmds[ROOT_CMD], menu_cmds[SEP_CMD], root);
29818c1b6884Sszhou 		line_parser(mp, linebuf, &lineNum, &entryNum);
29827c478bd9Sstevel@tonic-gate 	}
29837c478bd9Sstevel@tonic-gate 
29847c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
2985ae115bc7Smrj 	    menu_cmds[k_cmd], menu_cmds[SEP_CMD], kernel);
29868c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
29877c478bd9Sstevel@tonic-gate 
2988843e1988Sjohnlev 	if (mod_kernel != NULL) {
2989843e1988Sjohnlev 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
2990843e1988Sjohnlev 		    menu_cmds[m_cmd], menu_cmds[SEP_CMD], mod_kernel);
2991843e1988Sjohnlev 		line_parser(mp, linebuf, &lineNum, &entryNum);
2992843e1988Sjohnlev 	}
2993843e1988Sjohnlev 
29947c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
2995ae115bc7Smrj 	    menu_cmds[m_cmd], menu_cmds[SEP_CMD], module);
29968c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
29977c478bd9Sstevel@tonic-gate 
29987c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
2999ae115bc7Smrj 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_FTR);
30008c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
30017c478bd9Sstevel@tonic-gate 
30027c478bd9Sstevel@tonic-gate 	return (entryNum);
30037c478bd9Sstevel@tonic-gate }
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate static error_t
30067c478bd9Sstevel@tonic-gate do_delete(menu_t *mp, int entryNum)
30077c478bd9Sstevel@tonic-gate {
30088c1b6884Sszhou 	line_t *lp, *freed;
30098c1b6884Sszhou 	entry_t *ent, *tmp;
30107c478bd9Sstevel@tonic-gate 	int deleted;
30117c478bd9Sstevel@tonic-gate 
30127c478bd9Sstevel@tonic-gate 	assert(entryNum != ENTRY_INIT);
30137c478bd9Sstevel@tonic-gate 
30148c1b6884Sszhou 	ent = mp->entries;
30158c1b6884Sszhou 	while (ent) {
30168c1b6884Sszhou 		lp = ent->start;
30178c1b6884Sszhou 		/* check entry number and make sure it's a bootadm entry */
30188c1b6884Sszhou 		if (lp->flags != BAM_COMMENT ||
3019ae115bc7Smrj 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0 ||
30208c1b6884Sszhou 		    (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) {
30218c1b6884Sszhou 			ent = ent->next;
30227c478bd9Sstevel@tonic-gate 			continue;
30237c478bd9Sstevel@tonic-gate 		}
30247c478bd9Sstevel@tonic-gate 
30258c1b6884Sszhou 		/* free the entry content */
30268c1b6884Sszhou 		do {
30278c1b6884Sszhou 			freed = lp;
30288c1b6884Sszhou 			lp = lp->next;	/* prev stays the same */
30298c1b6884Sszhou 			unlink_line(mp, freed);
30308c1b6884Sszhou 			line_free(freed);
30318c1b6884Sszhou 		} while (freed != ent->end);
30328c1b6884Sszhou 
30338c1b6884Sszhou 		/* free the entry_t structure */
30348c1b6884Sszhou 		tmp = ent;
30358c1b6884Sszhou 		ent = ent->next;
30368c1b6884Sszhou 		if (tmp->prev)
30378c1b6884Sszhou 			tmp->prev->next = ent;
30387c478bd9Sstevel@tonic-gate 		else
30398c1b6884Sszhou 			mp->entries = ent;
30408c1b6884Sszhou 		if (ent)
30418c1b6884Sszhou 			ent->prev = tmp->prev;
30427c478bd9Sstevel@tonic-gate 		deleted = 1;
30437c478bd9Sstevel@tonic-gate 	}
30447c478bd9Sstevel@tonic-gate 
30457c478bd9Sstevel@tonic-gate 	if (!deleted && entryNum != ALL_ENTRIES) {
30467c478bd9Sstevel@tonic-gate 		bam_error(NO_BOOTADM_MATCH);
30477c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
30487c478bd9Sstevel@tonic-gate 	}
30497c478bd9Sstevel@tonic-gate 
305040541d5dSvikram 	/*
305140541d5dSvikram 	 * Now that we have deleted an entry, update
305240541d5dSvikram 	 * the entry numbering and the default cmd.
305340541d5dSvikram 	 */
305440541d5dSvikram 	update_numbering(mp);
305540541d5dSvikram 
30567c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
30577c478bd9Sstevel@tonic-gate }
30587c478bd9Sstevel@tonic-gate 
30597c478bd9Sstevel@tonic-gate static error_t
30607c478bd9Sstevel@tonic-gate delete_all_entries(menu_t *mp, char *menu_path, char *opt)
30617c478bd9Sstevel@tonic-gate {
30627c478bd9Sstevel@tonic-gate 	assert(mp);
30637c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
30647c478bd9Sstevel@tonic-gate 
30657c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
30667c478bd9Sstevel@tonic-gate 		bam_print(EMPTY_FILE, menu_path);
30677c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
30687c478bd9Sstevel@tonic-gate 	}
30697c478bd9Sstevel@tonic-gate 
30707c478bd9Sstevel@tonic-gate 	if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) {
30717c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
30727c478bd9Sstevel@tonic-gate 	}
30737c478bd9Sstevel@tonic-gate 
30747c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
30757c478bd9Sstevel@tonic-gate }
30767c478bd9Sstevel@tonic-gate 
30777c478bd9Sstevel@tonic-gate static FILE *
30788c1b6884Sszhou open_diskmap(char *root)
30797c478bd9Sstevel@tonic-gate {
30807c478bd9Sstevel@tonic-gate 	FILE *fp;
30817c478bd9Sstevel@tonic-gate 	char cmd[PATH_MAX];
30827c478bd9Sstevel@tonic-gate 
30837c478bd9Sstevel@tonic-gate 	/* make sure we have a map file */
30847c478bd9Sstevel@tonic-gate 	fp = fopen(GRUBDISK_MAP, "r");
30857c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
30867c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd),
3087986fd29aSsetje 		    "%s/%s > /dev/null", root, CREATE_DISKMAP);
30887c478bd9Sstevel@tonic-gate 		(void) system(cmd);
30897c478bd9Sstevel@tonic-gate 		fp = fopen(GRUBDISK_MAP, "r");
30907c478bd9Sstevel@tonic-gate 	}
30917c478bd9Sstevel@tonic-gate 	return (fp);
30927c478bd9Sstevel@tonic-gate }
30937c478bd9Sstevel@tonic-gate 
30947c478bd9Sstevel@tonic-gate #define	SECTOR_SIZE	512
30957c478bd9Sstevel@tonic-gate 
30967c478bd9Sstevel@tonic-gate static int
30977c478bd9Sstevel@tonic-gate get_partition(char *device)
30987c478bd9Sstevel@tonic-gate {
30997c478bd9Sstevel@tonic-gate 	int i, fd, is_pcfs, partno = -1;
31007c478bd9Sstevel@tonic-gate 	struct mboot *mboot;
31017c478bd9Sstevel@tonic-gate 	char boot_sect[SECTOR_SIZE];
31027c478bd9Sstevel@tonic-gate 	char *wholedisk, *slice;
31037c478bd9Sstevel@tonic-gate 
31047c478bd9Sstevel@tonic-gate 	/* form whole disk (p0) */
31057c478bd9Sstevel@tonic-gate 	slice = device + strlen(device) - 2;
31067c478bd9Sstevel@tonic-gate 	is_pcfs = (*slice != 's');
31077c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
31087c478bd9Sstevel@tonic-gate 		*slice = '\0';
31097c478bd9Sstevel@tonic-gate 	wholedisk = s_calloc(1, strlen(device) + 3);
31107c478bd9Sstevel@tonic-gate 	(void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device);
31117c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
31127c478bd9Sstevel@tonic-gate 		*slice = 's';
31137c478bd9Sstevel@tonic-gate 
31147c478bd9Sstevel@tonic-gate 	/* read boot sector */
31157c478bd9Sstevel@tonic-gate 	fd = open(wholedisk, O_RDONLY);
31167c478bd9Sstevel@tonic-gate 	free(wholedisk);
31177c478bd9Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
31187c478bd9Sstevel@tonic-gate 		return (partno);
31197c478bd9Sstevel@tonic-gate 	}
31207c478bd9Sstevel@tonic-gate 	(void) close(fd);
31217c478bd9Sstevel@tonic-gate 
31227c478bd9Sstevel@tonic-gate 	/* parse fdisk table */
31237c478bd9Sstevel@tonic-gate 	mboot = (struct mboot *)((void *)boot_sect);
31247c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
31257c478bd9Sstevel@tonic-gate 		struct ipart *part =
31267c478bd9Sstevel@tonic-gate 		    (struct ipart *)(uintptr_t)mboot->parts + i;
31277c478bd9Sstevel@tonic-gate 		if (is_pcfs) {	/* looking for solaris boot part */
31287c478bd9Sstevel@tonic-gate 			if (part->systid == 0xbe) {
31297c478bd9Sstevel@tonic-gate 				partno = i;
31307c478bd9Sstevel@tonic-gate 				break;
31317c478bd9Sstevel@tonic-gate 			}
31327c478bd9Sstevel@tonic-gate 		} else {	/* look for solaris partition, old and new */
31337c478bd9Sstevel@tonic-gate 			if (part->systid == SUNIXOS ||
31347c478bd9Sstevel@tonic-gate 			    part->systid == SUNIXOS2) {
31357c478bd9Sstevel@tonic-gate 				partno = i;
31367c478bd9Sstevel@tonic-gate 				break;
31377c478bd9Sstevel@tonic-gate 			}
31387c478bd9Sstevel@tonic-gate 		}
31397c478bd9Sstevel@tonic-gate 	}
31407c478bd9Sstevel@tonic-gate 	return (partno);
31417c478bd9Sstevel@tonic-gate }
31427c478bd9Sstevel@tonic-gate 
31437c478bd9Sstevel@tonic-gate static char *
31447c478bd9Sstevel@tonic-gate get_grubdisk(char *rootdev, FILE *fp, int on_bootdev)
31457c478bd9Sstevel@tonic-gate {
31467c478bd9Sstevel@tonic-gate 	char *grubdisk;	/* (hd#,#,#) */
31477c478bd9Sstevel@tonic-gate 	char *slice;
31487c478bd9Sstevel@tonic-gate 	char *grubhd;
31497c478bd9Sstevel@tonic-gate 	int fdiskpart;
31507c478bd9Sstevel@tonic-gate 	int found = 0;
31517c478bd9Sstevel@tonic-gate 	char *devname, *ctdname = strstr(rootdev, "dsk/");
31527c478bd9Sstevel@tonic-gate 	char linebuf[PATH_MAX];
31537c478bd9Sstevel@tonic-gate 
31547c478bd9Sstevel@tonic-gate 	if (ctdname == NULL)
31557c478bd9Sstevel@tonic-gate 		return (NULL);
31567c478bd9Sstevel@tonic-gate 
31577c478bd9Sstevel@tonic-gate 	ctdname += strlen("dsk/");
31587c478bd9Sstevel@tonic-gate 	slice = strrchr(ctdname, 's');
31597c478bd9Sstevel@tonic-gate 	if (slice)
31607c478bd9Sstevel@tonic-gate 		*slice = '\0';
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 	rewind(fp);
31637c478bd9Sstevel@tonic-gate 	while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) {
31647c478bd9Sstevel@tonic-gate 		grubhd = strtok(linebuf, " \t\n");
31657c478bd9Sstevel@tonic-gate 		if (grubhd)
31667c478bd9Sstevel@tonic-gate 			devname = strtok(NULL, " \t\n");
31677c478bd9Sstevel@tonic-gate 		else
31687c478bd9Sstevel@tonic-gate 			devname = NULL;
31697c478bd9Sstevel@tonic-gate 		if (devname && strcmp(devname, ctdname) == 0) {
31707c478bd9Sstevel@tonic-gate 			found = 1;
31717c478bd9Sstevel@tonic-gate 			break;
31727c478bd9Sstevel@tonic-gate 		}
31737c478bd9Sstevel@tonic-gate 	}
31747c478bd9Sstevel@tonic-gate 
31757c478bd9Sstevel@tonic-gate 	if (slice)
31767c478bd9Sstevel@tonic-gate 		*slice = 's';
31777c478bd9Sstevel@tonic-gate 
31787c478bd9Sstevel@tonic-gate 	if (found == 0) {
31797c478bd9Sstevel@tonic-gate 		if (bam_verbose)
31807c478bd9Sstevel@tonic-gate 			bam_print(DISKMAP_FAIL_NONFATAL, rootdev);
31817c478bd9Sstevel@tonic-gate 		grubhd = "0";	/* assume disk 0 if can't match */
31827c478bd9Sstevel@tonic-gate 	}
31837c478bd9Sstevel@tonic-gate 
31847c478bd9Sstevel@tonic-gate 	fdiskpart = get_partition(rootdev);
31857c478bd9Sstevel@tonic-gate 	if (fdiskpart == -1)
31867c478bd9Sstevel@tonic-gate 		return (NULL);
31877c478bd9Sstevel@tonic-gate 
31887c478bd9Sstevel@tonic-gate 	grubdisk = s_calloc(1, 10);
31897c478bd9Sstevel@tonic-gate 	if (slice) {
31907c478bd9Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d,%c)",
31917c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart, slice[1] + 'a' - '0');
31927c478bd9Sstevel@tonic-gate 	} else
31937c478bd9Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d)",
31947c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart);
31957c478bd9Sstevel@tonic-gate 
31967c478bd9Sstevel@tonic-gate 	/* if root not on bootdev, change GRUB disk to 0 */
31977c478bd9Sstevel@tonic-gate 	if (!on_bootdev)
31987c478bd9Sstevel@tonic-gate 		grubdisk[3] = '0';
31997c478bd9Sstevel@tonic-gate 	return (grubdisk);
32007c478bd9Sstevel@tonic-gate }
32017c478bd9Sstevel@tonic-gate 
3202ee3b8144Sszhou static char *
3203ee3b8144Sszhou get_title(char *rootdir)
32047c478bd9Sstevel@tonic-gate {
32057c478bd9Sstevel@tonic-gate 	static char title[80];	/* from /etc/release */
32068c1b6884Sszhou 	char *cp = NULL, release[PATH_MAX];
32077c478bd9Sstevel@tonic-gate 	FILE *fp;
32087c478bd9Sstevel@tonic-gate 
32097c478bd9Sstevel@tonic-gate 	/* open the /etc/release file */
32107c478bd9Sstevel@tonic-gate 	(void) snprintf(release, sizeof (release), "%s/etc/release", rootdir);
32117c478bd9Sstevel@tonic-gate 
32127c478bd9Sstevel@tonic-gate 	fp = fopen(release, "r");
32137c478bd9Sstevel@tonic-gate 	if (fp == NULL)
3214ee3b8144Sszhou 		return (NULL);
32157c478bd9Sstevel@tonic-gate 
32167c478bd9Sstevel@tonic-gate 	while (s_fgets(title, sizeof (title), fp) != NULL) {
32177c478bd9Sstevel@tonic-gate 		cp = strstr(title, "Solaris");
32187c478bd9Sstevel@tonic-gate 		if (cp)
32197c478bd9Sstevel@tonic-gate 			break;
32207c478bd9Sstevel@tonic-gate 	}
32217c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
32228c1b6884Sszhou 	return (cp == NULL ? "Solaris" : cp);
32237c478bd9Sstevel@tonic-gate }
32247c478bd9Sstevel@tonic-gate 
3225ae115bc7Smrj char *
32267c478bd9Sstevel@tonic-gate get_special(char *mountp)
32277c478bd9Sstevel@tonic-gate {
32287c478bd9Sstevel@tonic-gate 	FILE *mntfp;
32297c478bd9Sstevel@tonic-gate 	struct mnttab mp = {0}, mpref = {0};
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 	mntfp = fopen(MNTTAB, "r");
32327c478bd9Sstevel@tonic-gate 	if (mntfp == NULL) {
32337c478bd9Sstevel@tonic-gate 		return (0);
32347c478bd9Sstevel@tonic-gate 	}
32357c478bd9Sstevel@tonic-gate 
32367c478bd9Sstevel@tonic-gate 	if (*mountp == '\0')
32377c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = "/";
32387c478bd9Sstevel@tonic-gate 	else
32397c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = mountp;
32407c478bd9Sstevel@tonic-gate 	if (getmntany(mntfp, &mp, &mpref) != 0) {
32417c478bd9Sstevel@tonic-gate 		(void) fclose(mntfp);
32427c478bd9Sstevel@tonic-gate 		return (NULL);
32437c478bd9Sstevel@tonic-gate 	}
32447c478bd9Sstevel@tonic-gate 	(void) fclose(mntfp);
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate 	return (s_strdup(mp.mnt_special));
32477c478bd9Sstevel@tonic-gate }
32487c478bd9Sstevel@tonic-gate 
3249ae115bc7Smrj char *
32507c478bd9Sstevel@tonic-gate os_to_grubdisk(char *osdisk, int on_bootdev)
32517c478bd9Sstevel@tonic-gate {
32527c478bd9Sstevel@tonic-gate 	FILE *fp;
32537c478bd9Sstevel@tonic-gate 	char *grubdisk;
32547c478bd9Sstevel@tonic-gate 
32557c478bd9Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
32568c1b6884Sszhou 	fp = open_diskmap("");
32577c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
32587c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdisk);
32597c478bd9Sstevel@tonic-gate 		return (NULL);
32607c478bd9Sstevel@tonic-gate 	}
32617c478bd9Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdisk, fp, on_bootdev);
32627c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
32637c478bd9Sstevel@tonic-gate 	return (grubdisk);
32647c478bd9Sstevel@tonic-gate }
32657c478bd9Sstevel@tonic-gate 
32667c478bd9Sstevel@tonic-gate /*
32677c478bd9Sstevel@tonic-gate  * Check if root is on the boot device
32687c478bd9Sstevel@tonic-gate  * Return 0 (false) on error
32697c478bd9Sstevel@tonic-gate  */
32707c478bd9Sstevel@tonic-gate static int
32717c478bd9Sstevel@tonic-gate menu_on_bootdev(char *menu_root, FILE *fp)
32727c478bd9Sstevel@tonic-gate {
32737c478bd9Sstevel@tonic-gate 	int ret;
32747c478bd9Sstevel@tonic-gate 	char *grubhd, *bootp, *special;
32757c478bd9Sstevel@tonic-gate 
32767c478bd9Sstevel@tonic-gate 	special = get_special(menu_root);
32777c478bd9Sstevel@tonic-gate 	if (special == NULL)
32787c478bd9Sstevel@tonic-gate 		return (0);
32797c478bd9Sstevel@tonic-gate 	bootp = strstr(special, "p0:boot");
32807c478bd9Sstevel@tonic-gate 	if (bootp)
32817c478bd9Sstevel@tonic-gate 		*bootp = '\0';
32827c478bd9Sstevel@tonic-gate 	grubhd = get_grubdisk(special, fp, 1);
32837c478bd9Sstevel@tonic-gate 	free(special);
32847c478bd9Sstevel@tonic-gate 
32857c478bd9Sstevel@tonic-gate 	if (grubhd == NULL)
32867c478bd9Sstevel@tonic-gate 		return (0);
32877c478bd9Sstevel@tonic-gate 	ret = grubhd[3] == '0';
32887c478bd9Sstevel@tonic-gate 	free(grubhd);
32897c478bd9Sstevel@tonic-gate 	return (ret);
32907c478bd9Sstevel@tonic-gate }
32917c478bd9Sstevel@tonic-gate 
32928c1b6884Sszhou /*
32938c1b6884Sszhou  * look for matching bootadm entry with specified parameters
32948c1b6884Sszhou  * Here are the rules (based on existing usage):
32958c1b6884Sszhou  * - If title is specified, match on title only
3296843e1988Sjohnlev  * - Else, match on kernel, grubdisk and module.  Note that, if root_opt is
3297843e1988Sjohnlev  *   non-zero, the absence of root line is considered a match.
32988c1b6884Sszhou  */
32998c1b6884Sszhou static entry_t *
3300843e1988Sjohnlev find_boot_entry(menu_t *mp, char *title, char *kernel, char *root,
3301843e1988Sjohnlev     char *module, int root_opt, int *entry_num)
33028c1b6884Sszhou {
33038c1b6884Sszhou 	int i;
33048c1b6884Sszhou 	line_t *lp;
33058c1b6884Sszhou 	entry_t *ent;
33068c1b6884Sszhou 
33078c1b6884Sszhou 	/* find matching entry */
33088c1b6884Sszhou 	for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) {
33098c1b6884Sszhou 		lp = ent->start;
33108c1b6884Sszhou 
33118c1b6884Sszhou 		/* first line of entry must be bootadm comment */
33128c1b6884Sszhou 		lp = ent->start;
3313ae115bc7Smrj 		if (lp->flags != BAM_COMMENT ||
3314ae115bc7Smrj 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0) {
33158c1b6884Sszhou 			continue;
33168c1b6884Sszhou 		}
33178c1b6884Sszhou 
33188c1b6884Sszhou 		/* advance to title line */
33198c1b6884Sszhou 		lp = lp->next;
33208c1b6884Sszhou 		if (title) {
33218c1b6884Sszhou 			if (lp->flags == BAM_TITLE && lp->arg &&
33228c1b6884Sszhou 			    strcmp(lp->arg, title) == 0)
33238c1b6884Sszhou 				break;
33248c1b6884Sszhou 			continue;	/* check title only */
33258c1b6884Sszhou 		}
33268c1b6884Sszhou 
33278c1b6884Sszhou 		lp = lp->next;	/* advance to root line */
3328843e1988Sjohnlev 		if (lp == NULL) {
3329843e1988Sjohnlev 			continue;
3330843e1988Sjohnlev 		} else if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
33318c1b6884Sszhou 			/* root command found, match grub disk */
33328c1b6884Sszhou 			if (strcmp(lp->arg, root) != 0) {
33338c1b6884Sszhou 				continue;
33348c1b6884Sszhou 			}
33358c1b6884Sszhou 			lp = lp->next;	/* advance to kernel line */
33368c1b6884Sszhou 		} else {
33378c1b6884Sszhou 			/* no root command, see if root is optional */
33388c1b6884Sszhou 			if (root_opt == 0) {
33398c1b6884Sszhou 				continue;
33408c1b6884Sszhou 			}
33418c1b6884Sszhou 		}
33428c1b6884Sszhou 
33438c1b6884Sszhou 		if (lp == NULL || lp->next == NULL) {
33448c1b6884Sszhou 			continue;
33458c1b6884Sszhou 		}
33468c1b6884Sszhou 
3347843e1988Sjohnlev 		if (kernel &&
3348843e1988Sjohnlev 		    (!check_cmd(lp->cmd, KERNEL_CMD, lp->arg, kernel))) {
3349843e1988Sjohnlev 			continue;
3350843e1988Sjohnlev 		}
3351843e1988Sjohnlev 
33520d69385cSrscott 		/*
3353843e1988Sjohnlev 		 * Check for matching module entry (failsafe or normal).
3354843e1988Sjohnlev 		 * If it fails to match, we go around the loop again.
3355843e1988Sjohnlev 		 * For xpv entries, there are two module lines, so we
3356843e1988Sjohnlev 		 * do the check twice.
33570d69385cSrscott 		 */
33588c1b6884Sszhou 		lp = lp->next;	/* advance to module line */
3359843e1988Sjohnlev 		if (check_cmd(lp->cmd, MODULE_CMD, lp->arg, module) ||
3360843e1988Sjohnlev 		    (((lp = lp->next) != NULL) &&
3361843e1988Sjohnlev 		    check_cmd(lp->cmd, MODULE_CMD, lp->arg, module))) {
3362843e1988Sjohnlev 			/* match found */
3363843e1988Sjohnlev 			break;
33648c1b6884Sszhou 		}
33658c1b6884Sszhou 	}
33668c1b6884Sszhou 
3367843e1988Sjohnlev 	if (entry_num && ent) {
3368843e1988Sjohnlev 		*entry_num = i;
3369843e1988Sjohnlev 	}
33708c1b6884Sszhou 	return (ent);
33718c1b6884Sszhou }
33728c1b6884Sszhou 
33738c1b6884Sszhou static int
33748c1b6884Sszhou update_boot_entry(menu_t *mp, char *title, char *root, char *kernel,
3375843e1988Sjohnlev     char *mod_kernel, char *module, int root_opt)
33768c1b6884Sszhou {
3377ae115bc7Smrj 	int i, change_kernel = 0;
33788c1b6884Sszhou 	entry_t *ent;
33798c1b6884Sszhou 	line_t *lp;
33808c1b6884Sszhou 	char linebuf[BAM_MAXLINE];
33818c1b6884Sszhou 
33828c1b6884Sszhou 	/* note: don't match on title, it's updated on upgrade */
3383843e1988Sjohnlev 	ent = find_boot_entry(mp, NULL, kernel, root, module, root_opt, &i);
3384ae115bc7Smrj 	if ((ent == NULL) && (bam_direct == BAM_DIRECT_DBOOT)) {
3385ae115bc7Smrj 		/*
3386ae115bc7Smrj 		 * We may be upgrading a kernel from multiboot to
3387ae115bc7Smrj 		 * directboot.  Look for a multiboot entry.
3388ae115bc7Smrj 		 */
3389843e1988Sjohnlev 		ent = find_boot_entry(mp, NULL, "multiboot", root,
3390843e1988Sjohnlev 		    MULTI_BOOT_ARCHIVE, root_opt, NULL);
3391ae115bc7Smrj 		if (ent != NULL) {
3392ae115bc7Smrj 			change_kernel = 1;
3393ae115bc7Smrj 		}
3394ae115bc7Smrj 	}
33958c1b6884Sszhou 	if (ent == NULL)
33968c1b6884Sszhou 		return (add_boot_entry(mp, title, root_opt ? NULL : root,
3397843e1988Sjohnlev 		    kernel, mod_kernel, module));
33988c1b6884Sszhou 
33998c1b6884Sszhou 	/* replace title of exiting entry and delete root line */
34008c1b6884Sszhou 	lp = ent->start;
34018c1b6884Sszhou 	lp = lp->next;	/* title line */
34028c1b6884Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
34038c1b6884Sszhou 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
34048c1b6884Sszhou 	free(lp->arg);
34058c1b6884Sszhou 	free(lp->line);
34068c1b6884Sszhou 	lp->arg = s_strdup(title);
34078c1b6884Sszhou 	lp->line = s_strdup(linebuf);
34088c1b6884Sszhou 
34098c1b6884Sszhou 	lp = lp->next;	/* root line */
34108c1b6884Sszhou 	if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
34118c1b6884Sszhou 		if (root_opt) {		/* root line not needed */
34128c1b6884Sszhou 			line_t *tmp = lp;
34138c1b6884Sszhou 			lp = lp->next;
34148c1b6884Sszhou 			unlink_line(mp, tmp);
34158c1b6884Sszhou 			line_free(tmp);
34168c1b6884Sszhou 		} else
34178c1b6884Sszhou 			lp = lp->next;
34188c1b6884Sszhou 	}
3419ae115bc7Smrj 
3420ae115bc7Smrj 	if (change_kernel) {
3421ae115bc7Smrj 		/*
3422ae115bc7Smrj 		 * We're upgrading from multiboot to directboot.
3423ae115bc7Smrj 		 */
3424ae115bc7Smrj 		if (strcmp(lp->cmd, menu_cmds[KERNEL_CMD]) == 0) {
3425ae115bc7Smrj 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
3426ae115bc7Smrj 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
3427ae115bc7Smrj 			    kernel);
3428ae115bc7Smrj 			free(lp->arg);
3429ae115bc7Smrj 			free(lp->line);
3430ae115bc7Smrj 			lp->arg = s_strdup(kernel);
3431ae115bc7Smrj 			lp->line = s_strdup(linebuf);
3432ae115bc7Smrj 			lp = lp->next;
3433ae115bc7Smrj 		}
3434ae115bc7Smrj 		if (strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) {
3435ae115bc7Smrj 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
3436ae115bc7Smrj 			    menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD],
3437ae115bc7Smrj 			    module);
3438ae115bc7Smrj 			free(lp->arg);
3439ae115bc7Smrj 			free(lp->line);
3440ae115bc7Smrj 			lp->arg = s_strdup(module);
3441ae115bc7Smrj 			lp->line = s_strdup(linebuf);
3442ae115bc7Smrj 			lp = lp->next;
3443ae115bc7Smrj 		}
3444ae115bc7Smrj 	}
34458c1b6884Sszhou 	return (i);
34468c1b6884Sszhou }
34478c1b6884Sszhou 
34487c478bd9Sstevel@tonic-gate /*ARGSUSED*/
34497c478bd9Sstevel@tonic-gate static error_t
34507c478bd9Sstevel@tonic-gate update_entry(menu_t *mp, char *menu_root, char *opt)
34517c478bd9Sstevel@tonic-gate {
34527c478bd9Sstevel@tonic-gate 	FILE *fp;
34537c478bd9Sstevel@tonic-gate 	int entry;
345460d0a590Srscott 	char *grubdisk, *title, *osdev, *osroot, *failsafe_kernel = NULL;
34558c1b6884Sszhou 	struct stat sbuf;
34568c1b6884Sszhou 	char failsafe[256];
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate 	assert(mp);
34597c478bd9Sstevel@tonic-gate 	assert(opt);
34607c478bd9Sstevel@tonic-gate 
34617c478bd9Sstevel@tonic-gate 	osdev = strtok(opt, ",");
34627c478bd9Sstevel@tonic-gate 	osroot = strtok(NULL, ",");
34637c478bd9Sstevel@tonic-gate 	if (osroot == NULL)
34647c478bd9Sstevel@tonic-gate 		osroot = menu_root;
34657c478bd9Sstevel@tonic-gate 	title = get_title(osroot);
34667c478bd9Sstevel@tonic-gate 
34677c478bd9Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
34688c1b6884Sszhou 	fp = open_diskmap(osroot);
34697c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
34707c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
34717c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
34727c478bd9Sstevel@tonic-gate 	}
34737c478bd9Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdev, fp, menu_on_bootdev(menu_root, fp));
34747c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
34757c478bd9Sstevel@tonic-gate 	if (grubdisk == NULL) {
34767c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
34777c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
34787c478bd9Sstevel@tonic-gate 	}
34797c478bd9Sstevel@tonic-gate 
34807c478bd9Sstevel@tonic-gate 	/* add the entry for normal Solaris */
3481ae115bc7Smrj 	if (bam_direct == BAM_DIRECT_DBOOT) {
3482ae115bc7Smrj 		entry = update_boot_entry(mp, title, grubdisk,
3483843e1988Sjohnlev 		    DIRECT_BOOT_KERNEL, NULL, DIRECT_BOOT_ARCHIVE,
3484ae115bc7Smrj 		    osroot == menu_root);
3485843e1988Sjohnlev 		if ((entry != BAM_ERROR) && (bam_is_hv == BAM_HV_PRESENT)) {
3486843e1988Sjohnlev 			(void) update_boot_entry(mp, NEW_HV_ENTRY, grubdisk,
3487843e1988Sjohnlev 			    XEN_MENU, KERNEL_MODULE_LINE, DIRECT_BOOT_ARCHIVE,
3488843e1988Sjohnlev 			    osroot == menu_root);
3489843e1988Sjohnlev 		}
3490ae115bc7Smrj 	} else {
3491843e1988Sjohnlev 		entry = update_boot_entry(mp, title, grubdisk, MULTI_BOOT,
3492843e1988Sjohnlev 		    NULL, MULTI_BOOT_ARCHIVE, osroot == menu_root);
3493ae115bc7Smrj 	}
34947c478bd9Sstevel@tonic-gate 
3495843e1988Sjohnlev 	/*
3496843e1988Sjohnlev 	 * Add the entry for failsafe archive.  On a bfu'd system, the
3497843e1988Sjohnlev 	 * failsafe may be different than the installed kernel.
3498843e1988Sjohnlev 	 */
3499ae115bc7Smrj 	(void) snprintf(failsafe, sizeof (failsafe), "%s%s", osroot, MINIROOT);
3500ae115bc7Smrj 	if (stat(failsafe, &sbuf) == 0) {
350160d0a590Srscott 
350260d0a590Srscott 		/* Figure out where the kernel line should point */
350360d0a590Srscott 		(void) snprintf(failsafe, sizeof (failsafe), "%s%s", osroot,
350460d0a590Srscott 		    DIRECT_BOOT_FAILSAFE_KERNEL);
350560d0a590Srscott 		if (stat(failsafe, &sbuf) == 0) {
350660d0a590Srscott 			failsafe_kernel = DIRECT_BOOT_FAILSAFE_LINE;
350760d0a590Srscott 		} else {
350860d0a590Srscott 			(void) snprintf(failsafe, sizeof (failsafe), "%s%s",
350960d0a590Srscott 			    osroot, MULTI_BOOT_FAILSAFE);
351060d0a590Srscott 			if (stat(failsafe, &sbuf) == 0) {
351160d0a590Srscott 				failsafe_kernel = MULTI_BOOT_FAILSAFE_LINE;
351260d0a590Srscott 			}
351360d0a590Srscott 		}
351460d0a590Srscott 		if (failsafe_kernel != NULL) {
351560d0a590Srscott 			(void) update_boot_entry(mp, FAILSAFE_TITLE, grubdisk,
3516843e1988Sjohnlev 			    failsafe_kernel, NULL, MINIROOT,
3517843e1988Sjohnlev 			    osroot == menu_root);
351860d0a590Srscott 		}
3519ae115bc7Smrj 	}
35207c478bd9Sstevel@tonic-gate 	free(grubdisk);
35217c478bd9Sstevel@tonic-gate 
35227c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
35237c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
35247c478bd9Sstevel@tonic-gate 	}
35257c478bd9Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
35267c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
35277c478bd9Sstevel@tonic-gate }
35287c478bd9Sstevel@tonic-gate 
3529b610f78eSvikram static char *
3530b610f78eSvikram read_grub_root(void)
3531b610f78eSvikram {
3532b610f78eSvikram 	FILE *fp;
3533b610f78eSvikram 	struct stat sb;
3534b610f78eSvikram 	char buf[BAM_MAXLINE];
3535b610f78eSvikram 	char *rootstr;
3536b610f78eSvikram 
3537b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
3538b610f78eSvikram 		bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
3539b610f78eSvikram 		return (NULL);
3540b610f78eSvikram 	}
3541b610f78eSvikram 
3542b610f78eSvikram 	if (stat(GRUB_root, &sb) != 0) {
3543b610f78eSvikram 		bam_error(MISSING_ROOT_FILE, GRUB_root, strerror(errno));
3544b610f78eSvikram 		return (NULL);
3545b610f78eSvikram 	}
3546b610f78eSvikram 
3547b610f78eSvikram 	fp = fopen(GRUB_root, "r");
3548b610f78eSvikram 	if (fp == NULL) {
3549b610f78eSvikram 		bam_error(OPEN_FAIL, GRUB_root, strerror(errno));
3550b610f78eSvikram 		return (NULL);
3551b610f78eSvikram 	}
3552b610f78eSvikram 
3553b610f78eSvikram 	if (s_fgets(buf, sizeof (buf), fp) == NULL) {
3554b610f78eSvikram 		bam_error(EMPTY_FILE, GRUB_root, strerror(errno));
3555b610f78eSvikram 		(void) fclose(fp);
3556b610f78eSvikram 		return (NULL);
3557b610f78eSvikram 	}
3558b610f78eSvikram 
3559b610f78eSvikram 	/*
3560b610f78eSvikram 	 * Copy buf here as check below may trash the buffer
3561b610f78eSvikram 	 */
3562b610f78eSvikram 	rootstr = s_strdup(buf);
3563b610f78eSvikram 
3564b610f78eSvikram 	if (s_fgets(buf, sizeof (buf), fp) != NULL) {
3565b610f78eSvikram 		bam_error(BAD_ROOT_FILE, GRUB_root);
3566b610f78eSvikram 		free(rootstr);
3567b610f78eSvikram 		rootstr = NULL;
3568b610f78eSvikram 	}
3569b610f78eSvikram 
3570b610f78eSvikram 	(void) fclose(fp);
3571b610f78eSvikram 
3572b610f78eSvikram 	return (rootstr);
3573b610f78eSvikram }
3574b610f78eSvikram 
35758c1b6884Sszhou static void
3576ae115bc7Smrj save_default_entry(menu_t *mp, const char *which)
35778c1b6884Sszhou {
35788c1b6884Sszhou 	int lineNum, entryNum;
35798c1b6884Sszhou 	int entry = 0;	/* default is 0 */
35808c1b6884Sszhou 	char linebuf[BAM_MAXLINE];
35818c1b6884Sszhou 	line_t *lp = mp->curdefault;
35828c1b6884Sszhou 
3583bff269d0Svikram 	if (mp->start) {
3584bff269d0Svikram 		lineNum = mp->end->lineNum;
3585bff269d0Svikram 		entryNum = mp->end->entryNum;
3586bff269d0Svikram 	} else {
3587bff269d0Svikram 		lineNum = LINE_INIT;
3588bff269d0Svikram 		entryNum = ENTRY_INIT;
3589bff269d0Svikram 	}
3590bff269d0Svikram 
35918c1b6884Sszhou 	if (lp)
35928c1b6884Sszhou 		entry = s_strtol(lp->arg);
35938c1b6884Sszhou 
3594ae115bc7Smrj 	(void) snprintf(linebuf, sizeof (linebuf), "#%s%d", which, entry);
35958c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
35968c1b6884Sszhou }
35978c1b6884Sszhou 
35988c1b6884Sszhou static void
3599ae115bc7Smrj restore_default_entry(menu_t *mp, const char *which, line_t *lp)
36008c1b6884Sszhou {
36018c1b6884Sszhou 	int entry;
36028c1b6884Sszhou 	char *str;
36038c1b6884Sszhou 
36048c1b6884Sszhou 	if (lp == NULL)
36058c1b6884Sszhou 		return;		/* nothing to restore */
36068c1b6884Sszhou 
3607ae115bc7Smrj 	str = lp->arg + strlen(which);
36088c1b6884Sszhou 	entry = s_strtol(str);
36098c1b6884Sszhou 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
36108c1b6884Sszhou 
36118c1b6884Sszhou 	/* delete saved old default line */
36128c1b6884Sszhou 	unlink_line(mp, lp);
36138c1b6884Sszhou 	line_free(lp);
36148c1b6884Sszhou }
36158c1b6884Sszhou 
36167c478bd9Sstevel@tonic-gate /*
36177c478bd9Sstevel@tonic-gate  * This function is for supporting reboot with args.
36187c478bd9Sstevel@tonic-gate  * The opt value can be:
36197c478bd9Sstevel@tonic-gate  * NULL		delete temp entry, if present
36207c478bd9Sstevel@tonic-gate  * entry=#	switches default entry to 1
36217c478bd9Sstevel@tonic-gate  * else		treated as boot-args and setup a temperary menu entry
36227c478bd9Sstevel@tonic-gate  *		and make it the default
36237c478bd9Sstevel@tonic-gate  */
36247c478bd9Sstevel@tonic-gate #define	REBOOT_TITLE	"Solaris_reboot_transient"
36257c478bd9Sstevel@tonic-gate 
36268c1b6884Sszhou /*ARGSUSED*/
36277c478bd9Sstevel@tonic-gate static error_t
36287c478bd9Sstevel@tonic-gate update_temp(menu_t *mp, char *menupath, char *opt)
36297c478bd9Sstevel@tonic-gate {
36307c478bd9Sstevel@tonic-gate 	int entry;
3631455710d3Srscott 	char *grubdisk, *rootdev, *path, *opt_ptr;
3632ae115bc7Smrj 	char kernbuf[BUFSIZ];
3633ae115bc7Smrj 	char args_buf[BUFSIZ];
3634b610f78eSvikram 	struct stat sb;
36357c478bd9Sstevel@tonic-gate 
36367c478bd9Sstevel@tonic-gate 	assert(mp);
36377c478bd9Sstevel@tonic-gate 
36388c1b6884Sszhou 	/* If no option, delete exiting reboot menu entry */
36398c1b6884Sszhou 	if (opt == NULL) {
36408c1b6884Sszhou 		entry_t *ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL,
3641843e1988Sjohnlev 		    NULL, 0, &entry);
36428c1b6884Sszhou 		if (ent == NULL)	/* not found is ok */
36438c1b6884Sszhou 			return (BAM_SUCCESS);
36448c1b6884Sszhou 		(void) do_delete(mp, entry);
3645ae115bc7Smrj 		restore_default_entry(mp, BAM_OLDDEF, mp->olddefault);
3646ae115bc7Smrj 		mp->olddefault = NULL;
36478c1b6884Sszhou 		return (BAM_WRITE);
36488c1b6884Sszhou 	}
36498c1b6884Sszhou 
36508c1b6884Sszhou 	/* if entry= is specified, set the default entry */
36518c1b6884Sszhou 	if (strncmp(opt, "entry=", strlen("entry=")) == 0 &&
36527c478bd9Sstevel@tonic-gate 	    selector(mp, opt, &entry, NULL) == BAM_SUCCESS) {
36537c478bd9Sstevel@tonic-gate 		/* this is entry=# option */
36547c478bd9Sstevel@tonic-gate 		return (set_global(mp, menu_cmds[DEFAULT_CMD], entry));
36557c478bd9Sstevel@tonic-gate 	}
36567c478bd9Sstevel@tonic-gate 
36577c478bd9Sstevel@tonic-gate 	/*
36587c478bd9Sstevel@tonic-gate 	 * add a new menu entry base on opt and make it the default
36597c478bd9Sstevel@tonic-gate 	 */
3660b610f78eSvikram 	grubdisk = NULL;
3661b610f78eSvikram 	if (stat(GRUB_slice, &sb) != 0) {
3662b610f78eSvikram 		/*
3663b610f78eSvikram 		 * 1. First get root disk name from mnttab
3664b610f78eSvikram 		 * 2. Translate disk name to grub name
3665b610f78eSvikram 		 * 3. Add the new menu entry
3666b610f78eSvikram 		 */
3667b610f78eSvikram 		rootdev = get_special("/");
3668b610f78eSvikram 		if (rootdev) {
3669b610f78eSvikram 			grubdisk = os_to_grubdisk(rootdev, 1);
3670b610f78eSvikram 			free(rootdev);
3671b610f78eSvikram 		}
3672b610f78eSvikram 	} else {
3673b610f78eSvikram 		/*
3674b610f78eSvikram 		 * This is an LU BE. The GRUB_root file
3675b610f78eSvikram 		 * contains entry for GRUB's "root" cmd.
3676b610f78eSvikram 		 */
3677b610f78eSvikram 		grubdisk = read_grub_root();
36787c478bd9Sstevel@tonic-gate 	}
36797c478bd9Sstevel@tonic-gate 	if (grubdisk == NULL) {
3680b610f78eSvikram 		bam_error(REBOOT_WITH_ARGS_FAILED);
36817c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
36827c478bd9Sstevel@tonic-gate 	}
36837c478bd9Sstevel@tonic-gate 
36847c478bd9Sstevel@tonic-gate 	/* add an entry for Solaris reboot */
3685ae115bc7Smrj 	if (bam_direct == BAM_DIRECT_DBOOT) {
3686ae115bc7Smrj 		if (opt[0] == '-') {
3687ae115bc7Smrj 			/* It's an option - first see if boot-file is set */
3688ae115bc7Smrj 			if (set_kernel(mp, KERNEL_CMD, NULL, kernbuf, BUFSIZ)
3689ae115bc7Smrj 			    != BAM_SUCCESS)
3690ae115bc7Smrj 				return (BAM_ERROR);
3691ae115bc7Smrj 			if (kernbuf[0] == '\0')
3692ae115bc7Smrj 				(void) strncpy(kernbuf, DIRECT_BOOT_KERNEL,
3693ae115bc7Smrj 				    BUFSIZ);
3694ae115bc7Smrj 			(void) strlcat(kernbuf, " ", BUFSIZ);
3695ae115bc7Smrj 			(void) strlcat(kernbuf, opt, BUFSIZ);
3696ae115bc7Smrj 		} else if (opt[0] == '/') {
3697455710d3Srscott 			/* It's a full path, so write it out. */
3698ae115bc7Smrj 			(void) strlcpy(kernbuf, opt, BUFSIZ);
3699455710d3Srscott 
3700455710d3Srscott 			/*
3701455710d3Srscott 			 * If someone runs:
3702455710d3Srscott 			 *
3703455710d3Srscott 			 *	# eeprom boot-args='-kd'
3704455710d3Srscott 			 *	# reboot /platform/i86pc/kernel/unix
3705455710d3Srscott 			 *
3706455710d3Srscott 			 * we want to use the boot-args as part of the boot
3707455710d3Srscott 			 * line.  On the other hand, if someone runs:
3708455710d3Srscott 			 *
3709455710d3Srscott 			 *	# reboot "/platform/i86pc/kernel/unix -kd"
3710455710d3Srscott 			 *
3711455710d3Srscott 			 * we don't need to mess with boot-args.  If there's
3712455710d3Srscott 			 * no space in the options string, assume we're in the
3713455710d3Srscott 			 * first case.
3714455710d3Srscott 			 */
3715455710d3Srscott 			if (strchr(opt, ' ') == NULL) {
3716455710d3Srscott 				if (set_kernel(mp, ARGS_CMD, NULL, args_buf,
3717455710d3Srscott 				    BUFSIZ) != BAM_SUCCESS)
3718455710d3Srscott 					return (BAM_ERROR);
3719455710d3Srscott 
3720455710d3Srscott 				if (args_buf[0] != '\0') {
3721455710d3Srscott 					(void) strlcat(kernbuf, " ", BUFSIZ);
3722455710d3Srscott 					(void) strlcat(kernbuf, args_buf,
3723455710d3Srscott 					    BUFSIZ);
3724455710d3Srscott 				}
3725455710d3Srscott 			}
3726ae115bc7Smrj 		} else {
3727455710d3Srscott 			/*
3728455710d3Srscott 			 * It may be a partial path, or it may be a partial
3729455710d3Srscott 			 * path followed by options.  Assume that only options
3730455710d3Srscott 			 * follow a space.  If someone sends us a kernel path
3731455710d3Srscott 			 * that includes a space, they deserve to be broken.
3732455710d3Srscott 			 */
3733455710d3Srscott 			opt_ptr = strchr(opt, ' ');
3734455710d3Srscott 			if (opt_ptr != NULL) {
3735455710d3Srscott 				*opt_ptr = '\0';
3736455710d3Srscott 			}
3737455710d3Srscott 
3738ae115bc7Smrj 			path = expand_path(opt);
3739ae115bc7Smrj 			if (path != NULL) {
3740ae115bc7Smrj 				(void) strlcpy(kernbuf, path, BUFSIZ);
3741ae115bc7Smrj 				free(path);
3742455710d3Srscott 
3743455710d3Srscott 				/*
3744455710d3Srscott 				 * If there were options given, use those.
3745455710d3Srscott 				 * Otherwise, copy over the default options.
3746455710d3Srscott 				 */
3747455710d3Srscott 				if (opt_ptr != NULL) {
3748455710d3Srscott 					/* Restore the space in opt string */
3749455710d3Srscott 					*opt_ptr = ' ';
3750455710d3Srscott 					(void) strlcat(kernbuf, opt_ptr,
3751455710d3Srscott 					    BUFSIZ);
3752455710d3Srscott 				} else {
3753ae115bc7Smrj 					if (set_kernel(mp, ARGS_CMD, NULL,
3754ae115bc7Smrj 					    args_buf, BUFSIZ) != BAM_SUCCESS)
3755ae115bc7Smrj 						return (BAM_ERROR);
3756ae115bc7Smrj 
3757ae115bc7Smrj 					if (args_buf[0] != '\0') {
3758ae115bc7Smrj 						(void) strlcat(kernbuf, " ",
3759ae115bc7Smrj 						    BUFSIZ);
3760ae115bc7Smrj 						(void) strlcat(kernbuf,
3761ae115bc7Smrj 						    args_buf, BUFSIZ);
3762ae115bc7Smrj 					}
3763ae115bc7Smrj 				}
3764455710d3Srscott 			} else {
3765455710d3Srscott 				bam_error(UNKNOWN_KERNEL, opt);
3766455710d3Srscott 				bam_print_stderr(UNKNOWN_KERNEL_REBOOT);
3767455710d3Srscott 				return (BAM_ERROR);
3768ae115bc7Smrj 			}
3769ae115bc7Smrj 		}
3770ae115bc7Smrj 		entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf,
3771843e1988Sjohnlev 		    NULL, NULL);
3772ae115bc7Smrj 	} else {
3773ae115bc7Smrj 		(void) snprintf(kernbuf, sizeof (kernbuf), "%s %s",
3774ae115bc7Smrj 		    MULTI_BOOT, opt);
3775ae115bc7Smrj 		entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf,
3776843e1988Sjohnlev 		    NULL, MULTI_BOOT_ARCHIVE);
3777ae115bc7Smrj 	}
37787c478bd9Sstevel@tonic-gate 	free(grubdisk);
37797c478bd9Sstevel@tonic-gate 
37807c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
3781b610f78eSvikram 		bam_error(REBOOT_WITH_ARGS_FAILED);
37827c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
37837c478bd9Sstevel@tonic-gate 	}
37848c1b6884Sszhou 
3785ae115bc7Smrj 	save_default_entry(mp, BAM_OLDDEF);
37867c478bd9Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
37877c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
37887c478bd9Sstevel@tonic-gate }
37897c478bd9Sstevel@tonic-gate 
37907c478bd9Sstevel@tonic-gate static error_t
37917c478bd9Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val)
37927c478bd9Sstevel@tonic-gate {
37937c478bd9Sstevel@tonic-gate 	line_t *lp, *found, *last;
37947c478bd9Sstevel@tonic-gate 	char *cp, *str;
37957c478bd9Sstevel@tonic-gate 	char prefix[BAM_MAXLINE];
37967c478bd9Sstevel@tonic-gate 	size_t len;
37977c478bd9Sstevel@tonic-gate 
37987c478bd9Sstevel@tonic-gate 	assert(mp);
37997c478bd9Sstevel@tonic-gate 	assert(globalcmd);
38007c478bd9Sstevel@tonic-gate 
3801b610f78eSvikram 	if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) {
3802b610f78eSvikram 		if (val < 0 || mp->end == NULL || val > mp->end->entryNum) {
3803b610f78eSvikram 			(void) snprintf(prefix, sizeof (prefix), "%d", val);
3804b610f78eSvikram 			bam_error(INVALID_ENTRY, prefix);
3805b610f78eSvikram 			return (BAM_ERROR);
3806b610f78eSvikram 		}
3807b610f78eSvikram 	}
3808b610f78eSvikram 
38097c478bd9Sstevel@tonic-gate 	found = last = NULL;
38107c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
38117c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
38127c478bd9Sstevel@tonic-gate 			continue;
38137c478bd9Sstevel@tonic-gate 
38147c478bd9Sstevel@tonic-gate 		last = lp; /* track the last global found */
38157c478bd9Sstevel@tonic-gate 
38167c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
38177c478bd9Sstevel@tonic-gate 			bam_error(NO_CMD, lp->lineNum);
38187c478bd9Sstevel@tonic-gate 			continue;
38197c478bd9Sstevel@tonic-gate 		}
38207c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
38217c478bd9Sstevel@tonic-gate 			continue;
38227c478bd9Sstevel@tonic-gate 
38237c478bd9Sstevel@tonic-gate 		if (found) {
38247c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
38257c478bd9Sstevel@tonic-gate 		}
38267c478bd9Sstevel@tonic-gate 		found = lp;
38277c478bd9Sstevel@tonic-gate 	}
38287c478bd9Sstevel@tonic-gate 
38297c478bd9Sstevel@tonic-gate 	if (found == NULL) {
38307c478bd9Sstevel@tonic-gate 		lp = s_calloc(1, sizeof (line_t));
38317c478bd9Sstevel@tonic-gate 		if (last == NULL) {
38327c478bd9Sstevel@tonic-gate 			lp->next = mp->start;
38337c478bd9Sstevel@tonic-gate 			mp->start = lp;
38347c478bd9Sstevel@tonic-gate 			mp->end = (mp->end) ? mp->end : lp;
38357c478bd9Sstevel@tonic-gate 		} else {
38367c478bd9Sstevel@tonic-gate 			lp->next = last->next;
38377c478bd9Sstevel@tonic-gate 			last->next = lp;
38387c478bd9Sstevel@tonic-gate 			if (lp->next == NULL)
38397c478bd9Sstevel@tonic-gate 				mp->end = lp;
38407c478bd9Sstevel@tonic-gate 		}
38417c478bd9Sstevel@tonic-gate 		lp->flags = BAM_GLOBAL; /* other fields not needed for writes */
38427c478bd9Sstevel@tonic-gate 		len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]);
38437c478bd9Sstevel@tonic-gate 		len += 10;	/* val < 10 digits */
38447c478bd9Sstevel@tonic-gate 		lp->line = s_calloc(1, len);
38457c478bd9Sstevel@tonic-gate 		(void) snprintf(lp->line, len, "%s%s%d",
38467c478bd9Sstevel@tonic-gate 		    globalcmd, menu_cmds[SEP_CMD], val);
38477c478bd9Sstevel@tonic-gate 		return (BAM_WRITE);
38487c478bd9Sstevel@tonic-gate 	}
38497c478bd9Sstevel@tonic-gate 
38507c478bd9Sstevel@tonic-gate 	/*
38517c478bd9Sstevel@tonic-gate 	 * We are changing an existing entry. Retain any prefix whitespace,
38527c478bd9Sstevel@tonic-gate 	 * but overwrite everything else. This preserves tabs added for
38537c478bd9Sstevel@tonic-gate 	 * readability.
38547c478bd9Sstevel@tonic-gate 	 */
38557c478bd9Sstevel@tonic-gate 	str = found->line;
38567c478bd9Sstevel@tonic-gate 	cp = prefix;
38577c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
38587c478bd9Sstevel@tonic-gate 		*(cp++) = *(str++);
38597c478bd9Sstevel@tonic-gate 	*cp = '\0'; /* Terminate prefix */
38607c478bd9Sstevel@tonic-gate 	len = strlen(prefix) + strlen(globalcmd);
38617c478bd9Sstevel@tonic-gate 	len += strlen(menu_cmds[SEP_CMD]) + 10;
38627c478bd9Sstevel@tonic-gate 
38637c478bd9Sstevel@tonic-gate 	free(found->line);
38647c478bd9Sstevel@tonic-gate 	found->line = s_calloc(1, len);
38657c478bd9Sstevel@tonic-gate 	(void) snprintf(found->line, len,
3866455710d3Srscott 	    "%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val);
38677c478bd9Sstevel@tonic-gate 
38687c478bd9Sstevel@tonic-gate 	return (BAM_WRITE); /* need a write to menu */
38697c478bd9Sstevel@tonic-gate }
38707c478bd9Sstevel@tonic-gate 
3871ae115bc7Smrj /*
3872ae115bc7Smrj  * partial_path may be anything like "kernel/unix" or "kmdb".  Try to
3873455710d3Srscott  * expand it to a full unix path.  The calling function is expected to
3874455710d3Srscott  * output a message if an error occurs and NULL is returned.
3875ae115bc7Smrj  */
3876ae115bc7Smrj static char *
3877ae115bc7Smrj expand_path(const char *partial_path)
3878ae115bc7Smrj {
3879ae115bc7Smrj 	int new_path_len;
3880ae115bc7Smrj 	char *new_path, new_path2[PATH_MAX];
3881ae115bc7Smrj 	struct stat sb;
3882ae115bc7Smrj 
3883ae115bc7Smrj 	new_path_len = strlen(partial_path) + 64;
3884ae115bc7Smrj 	new_path = s_calloc(1, new_path_len);
3885ae115bc7Smrj 
3886ae115bc7Smrj 	/* First, try the simplest case - something like "kernel/unix" */
3887ae115bc7Smrj 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s",
3888ae115bc7Smrj 	    partial_path);
3889ae115bc7Smrj 	if (stat(new_path, &sb) == 0) {
3890ae115bc7Smrj 		return (new_path);
3891ae115bc7Smrj 	}
3892ae115bc7Smrj 
3893ae115bc7Smrj 	if (strcmp(partial_path, "kmdb") == 0) {
3894ae115bc7Smrj 		(void) snprintf(new_path, new_path_len, "%s -k",
3895ae115bc7Smrj 		    DIRECT_BOOT_KERNEL);
3896ae115bc7Smrj 		return (new_path);
3897ae115bc7Smrj 	}
3898ae115bc7Smrj 
3899ae115bc7Smrj 	/*
3900ae115bc7Smrj 	 * We've quickly reached unsupported usage.  Try once more to
3901ae115bc7Smrj 	 * see if we were just given a glom name.
3902ae115bc7Smrj 	 */
3903ae115bc7Smrj 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s/unix",
3904ae115bc7Smrj 	    partial_path);
3905ae115bc7Smrj 	(void) snprintf(new_path2, PATH_MAX, "/platform/i86pc/%s/amd64/unix",
3906ae115bc7Smrj 	    partial_path);
3907ae115bc7Smrj 	if (stat(new_path, &sb) == 0) {
3908ae115bc7Smrj 		if (stat(new_path2, &sb) == 0) {
3909ae115bc7Smrj 			/*
3910ae115bc7Smrj 			 * We matched both, so we actually
3911ae115bc7Smrj 			 * want to write the $ISADIR version.
3912ae115bc7Smrj 			 */
3913ae115bc7Smrj 			(void) snprintf(new_path, new_path_len,
3914ae115bc7Smrj 			    "/platform/i86pc/kernel/%s/$ISADIR/unix",
3915ae115bc7Smrj 			    partial_path);
3916ae115bc7Smrj 		}
3917ae115bc7Smrj 		return (new_path);
3918ae115bc7Smrj 	}
3919ae115bc7Smrj 
3920ae115bc7Smrj 	free(new_path);
3921ae115bc7Smrj 	return (NULL);
3922ae115bc7Smrj }
3923ae115bc7Smrj 
3924ae115bc7Smrj /*
3925ae115bc7Smrj  * The kernel cmd and arg have been changed, so
3926ae115bc7Smrj  * check whether the archive line needs to change.
3927ae115bc7Smrj  */
3928ae115bc7Smrj static void
3929ae115bc7Smrj set_archive_line(entry_t *entryp, line_t *kernelp)
3930ae115bc7Smrj {
3931ae115bc7Smrj 	line_t *lp = entryp->start;
3932ae115bc7Smrj 	char *new_archive;
3933ae115bc7Smrj 	menu_cmd_t m_cmd;
3934ae115bc7Smrj 
3935ae115bc7Smrj 	for (; lp != NULL; lp = lp->next) {
3936ae115bc7Smrj 		if (strncmp(lp->cmd, menu_cmds[MODULE_CMD],
3937ae115bc7Smrj 		    sizeof (menu_cmds[MODULE_CMD]) - 1) == 0) {
3938ae115bc7Smrj 			break;
3939ae115bc7Smrj 		}
3940ae115bc7Smrj 		if (lp == entryp->end)
3941ae115bc7Smrj 			return;
3942ae115bc7Smrj 	}
3943ae115bc7Smrj 	if (lp == NULL)
3944ae115bc7Smrj 		return;
3945ae115bc7Smrj 
3946ae115bc7Smrj 	if (strstr(kernelp->arg, "$ISADIR") != NULL) {
3947ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE;
3948ae115bc7Smrj 		m_cmd = MODULE_DOLLAR_CMD;
3949ae115bc7Smrj 	} else if (strstr(kernelp->arg, "amd64") != NULL) {
3950ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE_64;
3951ae115bc7Smrj 		m_cmd = MODULE_CMD;
3952ae115bc7Smrj 	} else {
3953ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE_32;
3954ae115bc7Smrj 		m_cmd = MODULE_CMD;
3955ae115bc7Smrj 	}
3956ae115bc7Smrj 
3957ae115bc7Smrj 	if (strcmp(lp->arg, new_archive) == 0)
3958ae115bc7Smrj 		return;
3959ae115bc7Smrj 
3960ae115bc7Smrj 	if (strcmp(lp->cmd, menu_cmds[m_cmd]) != 0) {
3961ae115bc7Smrj 		free(lp->cmd);
3962ae115bc7Smrj 		lp->cmd = s_strdup(menu_cmds[m_cmd]);
3963ae115bc7Smrj 	}
3964ae115bc7Smrj 
3965ae115bc7Smrj 	free(lp->arg);
3966ae115bc7Smrj 	lp->arg = s_strdup(new_archive);
3967ae115bc7Smrj 	update_line(lp);
3968ae115bc7Smrj }
3969ae115bc7Smrj 
3970ae115bc7Smrj /*
3971ae115bc7Smrj  * Title for an entry to set properties that once went in bootenv.rc.
3972ae115bc7Smrj  */
3973ae115bc7Smrj #define	BOOTENV_RC_TITLE	"Solaris bootenv rc"
3974ae115bc7Smrj 
3975ae115bc7Smrj /*
3976ae115bc7Smrj  * If path is NULL, return the kernel (optnum == KERNEL_CMD) or arguments
3977ae115bc7Smrj  * (optnum == ARGS_CMD) in the argument buf.  If path is a zero-length
3978ae115bc7Smrj  * string, reset the value to the default.  If path is a non-zero-length
3979ae115bc7Smrj  * string, set the kernel or arguments.
3980ae115bc7Smrj  */
3981ae115bc7Smrj static error_t
3982ae115bc7Smrj set_kernel(menu_t *mp, menu_cmd_t optnum, char *path, char *buf, size_t bufsize)
3983ae115bc7Smrj {
3984ae115bc7Smrj 	int entryNum, rv = BAM_SUCCESS, free_new_path = 0;
3985ae115bc7Smrj 	entry_t *entryp;
3986ae115bc7Smrj 	line_t *ptr, *kernelp;
3987ae115bc7Smrj 	char *new_arg, *old_args, *space;
3988ae115bc7Smrj 	char *grubdisk, *rootdev, *new_path;
3989ae115bc7Smrj 	char old_space;
3990ae115bc7Smrj 	size_t old_kernel_len, new_str_len;
3991ae115bc7Smrj 	struct stat sb;
3992ae115bc7Smrj 
3993ae115bc7Smrj 	assert(bufsize > 0);
3994ae115bc7Smrj 
3995ae115bc7Smrj 	ptr = kernelp = NULL;
3996ae115bc7Smrj 	new_arg = old_args = space = NULL;
3997ae115bc7Smrj 	grubdisk = rootdev = new_path = NULL;
3998ae115bc7Smrj 	buf[0] = '\0';
3999ae115bc7Smrj 
4000ae115bc7Smrj 	if (bam_direct != BAM_DIRECT_DBOOT) {
4001ae115bc7Smrj 		bam_error(NOT_DBOOT, optnum == KERNEL_CMD ? "kernel" : "args");
4002ae115bc7Smrj 		return (BAM_ERROR);
4003ae115bc7Smrj 	}
4004ae115bc7Smrj 
4005ae115bc7Smrj 	/*
4006ae115bc7Smrj 	 * If a user changed the default entry to a non-bootadm controlled
4007ae115bc7Smrj 	 * one, we don't want to mess with it.  Just print an error and
4008ae115bc7Smrj 	 * return.
4009ae115bc7Smrj 	 */
4010ae115bc7Smrj 	if (mp->curdefault) {
4011ae115bc7Smrj 		entryNum = s_strtol(mp->curdefault->arg);
4012ae115bc7Smrj 		for (entryp = mp->entries; entryp; entryp = entryp->next) {
4013ae115bc7Smrj 			if (entryp->entryNum == entryNum)
4014ae115bc7Smrj 				break;
4015ae115bc7Smrj 		}
4016ae115bc7Smrj 		if ((entryp != NULL) &&
4017ae115bc7Smrj 		    ((entryp->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU)) == 0)) {
4018ae115bc7Smrj 			bam_error(DEFAULT_NOT_BAM);
4019ae115bc7Smrj 			return (BAM_ERROR);
4020ae115bc7Smrj 		}
4021ae115bc7Smrj 	}
4022ae115bc7Smrj 
4023ae115bc7Smrj 	entryNum = -1;
4024843e1988Sjohnlev 	entryp = find_boot_entry(mp, BOOTENV_RC_TITLE, NULL, NULL, NULL, 0,
4025ae115bc7Smrj 	    &entryNum);
4026ae115bc7Smrj 
4027ae115bc7Smrj 	if (entryp != NULL) {
4028ae115bc7Smrj 		for (ptr = entryp->start; ptr && ptr != entryp->end;
4029ae115bc7Smrj 		    ptr = ptr->next) {
4030ae115bc7Smrj 			if (strncmp(ptr->cmd, menu_cmds[KERNEL_CMD],
4031ae115bc7Smrj 			    sizeof (menu_cmds[KERNEL_CMD]) - 1) == 0) {
4032ae115bc7Smrj 				kernelp = ptr;
4033ae115bc7Smrj 				break;
4034ae115bc7Smrj 			}
4035ae115bc7Smrj 		}
4036ae115bc7Smrj 		if (kernelp == NULL) {
4037ae115bc7Smrj 			bam_error(NO_KERNEL, entryNum);
4038ae115bc7Smrj 			return (BAM_ERROR);
4039ae115bc7Smrj 		}
4040ae115bc7Smrj 
4041ae115bc7Smrj 		old_kernel_len = strcspn(kernelp->arg, " \t");
4042ae115bc7Smrj 		space = old_args = kernelp->arg + old_kernel_len;
4043ae115bc7Smrj 		while ((*old_args == ' ') || (*old_args == '\t'))
4044ae115bc7Smrj 			old_args++;
4045ae115bc7Smrj 	}
4046ae115bc7Smrj 
4047ae115bc7Smrj 	if (path == NULL) {
4048ae115bc7Smrj 		/* Simply report what was found */
4049ae115bc7Smrj 		if (kernelp == NULL)
4050ae115bc7Smrj 			return (BAM_SUCCESS);
4051ae115bc7Smrj 
4052ae115bc7Smrj 		if (optnum == ARGS_CMD) {
4053ae115bc7Smrj 			if (old_args[0] != '\0')
4054ae115bc7Smrj 				(void) strlcpy(buf, old_args, bufsize);
4055ae115bc7Smrj 		} else {
4056ae115bc7Smrj 			/*
4057ae115bc7Smrj 			 * We need to print the kernel, so we just turn the
4058ae115bc7Smrj 			 * first space into a '\0' and print the beginning.
4059ae115bc7Smrj 			 * We don't print anything if it's the default kernel.
4060ae115bc7Smrj 			 */
4061ae115bc7Smrj 			old_space = *space;
4062ae115bc7Smrj 			*space = '\0';
4063ae115bc7Smrj 			if (strcmp(kernelp->arg, DIRECT_BOOT_KERNEL) != 0)
4064ae115bc7Smrj 				(void) strlcpy(buf, kernelp->arg, bufsize);
4065ae115bc7Smrj 			*space = old_space;
4066ae115bc7Smrj 		}
4067ae115bc7Smrj 		return (BAM_SUCCESS);
4068ae115bc7Smrj 	}
4069ae115bc7Smrj 
4070ae115bc7Smrj 	/*
4071ae115bc7Smrj 	 * First, check if we're resetting an entry to the default.
4072ae115bc7Smrj 	 */
4073ae115bc7Smrj 	if ((path[0] == '\0') ||
4074ae115bc7Smrj 	    ((optnum == KERNEL_CMD) &&
4075ae115bc7Smrj 	    (strcmp(path, DIRECT_BOOT_KERNEL) == 0))) {
4076ae115bc7Smrj 		if ((entryp == NULL) || (kernelp == NULL)) {
4077ae115bc7Smrj 			/* No previous entry, it's already the default */
4078ae115bc7Smrj 			return (BAM_SUCCESS);
4079ae115bc7Smrj 		}
4080ae115bc7Smrj 
4081ae115bc7Smrj 		/*
4082ae115bc7Smrj 		 * Check if we can delete the entry.  If we're resetting the
4083ae115bc7Smrj 		 * kernel command, and the args is already empty, or if we're
4084ae115bc7Smrj 		 * resetting the args command, and the kernel is already the
4085ae115bc7Smrj 		 * default, we can restore the old default and delete the entry.
4086ae115bc7Smrj 		 */
4087ae115bc7Smrj 		if (((optnum == KERNEL_CMD) &&
4088ae115bc7Smrj 		    ((old_args == NULL) || (old_args[0] == '\0'))) ||
4089ae115bc7Smrj 		    ((optnum == ARGS_CMD) &&
4090ae115bc7Smrj 		    (strncmp(kernelp->arg, DIRECT_BOOT_KERNEL,
4091ae115bc7Smrj 		    sizeof (DIRECT_BOOT_KERNEL) - 1) == 0))) {
4092ae115bc7Smrj 			kernelp = NULL;
4093ae115bc7Smrj 			(void) do_delete(mp, entryNum);
4094ae115bc7Smrj 			restore_default_entry(mp, BAM_OLD_RC_DEF,
4095ae115bc7Smrj 			    mp->old_rc_default);
4096ae115bc7Smrj 			mp->old_rc_default = NULL;
4097ae115bc7Smrj 			rv = BAM_WRITE;
4098ae115bc7Smrj 			goto done;
4099ae115bc7Smrj 		}
4100ae115bc7Smrj 
4101ae115bc7Smrj 		if (optnum == KERNEL_CMD) {
4102ae115bc7Smrj 			/*
4103ae115bc7Smrj 			 * At this point, we've already checked that old_args
4104ae115bc7Smrj 			 * and entryp are valid pointers.  The "+ 2" is for
4105ae115bc7Smrj 			 * a space a the string termination character.
4106ae115bc7Smrj 			 */
4107ae115bc7Smrj 			new_str_len = (sizeof (DIRECT_BOOT_KERNEL) - 1) +
4108ae115bc7Smrj 			    strlen(old_args) + 2;
4109ae115bc7Smrj 			new_arg = s_calloc(1, new_str_len);
4110ae115bc7Smrj 			(void) snprintf(new_arg, new_str_len, "%s %s",
4111ae115bc7Smrj 			    DIRECT_BOOT_KERNEL, old_args);
4112ae115bc7Smrj 			free(kernelp->arg);
4113ae115bc7Smrj 			kernelp->arg = new_arg;
4114ae115bc7Smrj 
4115ae115bc7Smrj 			/*
4116ae115bc7Smrj 			 * We have changed the kernel line, so we may need
4117ae115bc7Smrj 			 * to update the archive line as well.
4118ae115bc7Smrj 			 */
4119ae115bc7Smrj 			set_archive_line(entryp, kernelp);
4120ae115bc7Smrj 		} else {
4121ae115bc7Smrj 			/*
4122ae115bc7Smrj 			 * We're resetting the boot args to nothing, so
4123ae115bc7Smrj 			 * we only need to copy the kernel.  We've already
4124ae115bc7Smrj 			 * checked that the kernel is not the default.
4125ae115bc7Smrj 			 */
4126ae115bc7Smrj 			new_arg = s_calloc(1, old_kernel_len + 1);
4127ae115bc7Smrj 			(void) snprintf(new_arg, old_kernel_len + 1, "%s",
4128ae115bc7Smrj 			    kernelp->arg);
4129ae115bc7Smrj 			free(kernelp->arg);
4130ae115bc7Smrj 			kernelp->arg = new_arg;
4131ae115bc7Smrj 		}
4132ae115bc7Smrj 		rv = BAM_WRITE;
4133ae115bc7Smrj 		goto done;
4134ae115bc7Smrj 	}
4135ae115bc7Smrj 
4136ae115bc7Smrj 	/*
4137ae115bc7Smrj 	 * Expand the kernel file to a full path, if necessary
4138ae115bc7Smrj 	 */
4139ae115bc7Smrj 	if ((optnum == KERNEL_CMD) && (path[0] != '/')) {
4140ae115bc7Smrj 		new_path = expand_path(path);
4141ae115bc7Smrj 		if (new_path == NULL) {
4142455710d3Srscott 			bam_error(UNKNOWN_KERNEL, path);
4143ae115bc7Smrj 			return (BAM_ERROR);
4144ae115bc7Smrj 		}
4145ae115bc7Smrj 		free_new_path = 1;
4146ae115bc7Smrj 	} else {
4147ae115bc7Smrj 		new_path = path;
4148ae115bc7Smrj 		free_new_path = 0;
4149ae115bc7Smrj 	}
4150ae115bc7Smrj 
4151ae115bc7Smrj 	/*
4152ae115bc7Smrj 	 * At this point, we know we're setting a new value.  First, take care
4153ae115bc7Smrj 	 * of the case where there was no previous entry.
4154ae115bc7Smrj 	 */
4155ae115bc7Smrj 	if (entryp == NULL) {
4156ae115bc7Smrj 		/* Similar to code in update_temp */
4157ae115bc7Smrj 		if (stat(GRUB_slice, &sb) != 0) {
4158ae115bc7Smrj 			/*
4159ae115bc7Smrj 			 * 1. First get root disk name from mnttab
4160ae115bc7Smrj 			 * 2. Translate disk name to grub name
4161ae115bc7Smrj 			 * 3. Add the new menu entry
4162ae115bc7Smrj 			 */
4163ae115bc7Smrj 			rootdev = get_special("/");
4164ae115bc7Smrj 			if (rootdev) {
4165ae115bc7Smrj 				grubdisk = os_to_grubdisk(rootdev, 1);
4166ae115bc7Smrj 				free(rootdev);
4167ae115bc7Smrj 			}
4168ae115bc7Smrj 		} else {
4169ae115bc7Smrj 			/*
4170ae115bc7Smrj 			 * This is an LU BE. The GRUB_root file
4171ae115bc7Smrj 			 * contains entry for GRUB's "root" cmd.
4172ae115bc7Smrj 			 */
4173ae115bc7Smrj 			grubdisk = read_grub_root();
4174ae115bc7Smrj 		}
4175ae115bc7Smrj 		if (grubdisk == NULL) {
4176ae115bc7Smrj 			bam_error(REBOOT_WITH_ARGS_FAILED);
4177ae115bc7Smrj 			rv = BAM_ERROR;
4178ae115bc7Smrj 			goto done;
4179ae115bc7Smrj 		}
4180ae115bc7Smrj 		if (optnum == KERNEL_CMD) {
4181ae115bc7Smrj 			entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
4182843e1988Sjohnlev 			    grubdisk, new_path, NULL, NULL);
4183ae115bc7Smrj 		} else {
4184ae115bc7Smrj 			new_str_len = strlen(DIRECT_BOOT_KERNEL) +
4185ae115bc7Smrj 			    strlen(path) + 8;
4186ae115bc7Smrj 			new_arg = s_calloc(1, new_str_len);
4187ae115bc7Smrj 
4188ae115bc7Smrj 			(void) snprintf(new_arg, new_str_len, "%s %s",
4189ae115bc7Smrj 			    DIRECT_BOOT_KERNEL, path);
4190ae115bc7Smrj 			entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
4191843e1988Sjohnlev 			    grubdisk, new_arg, NULL, DIRECT_BOOT_ARCHIVE);
4192ae115bc7Smrj 		}
4193ae115bc7Smrj 		save_default_entry(mp, BAM_OLD_RC_DEF);
4194ae115bc7Smrj 		(void) set_global(mp, menu_cmds[DEFAULT_CMD], entryNum);
4195ae115bc7Smrj 		rv = BAM_WRITE;
4196ae115bc7Smrj 		goto done;
4197ae115bc7Smrj 	}
4198ae115bc7Smrj 
4199ae115bc7Smrj 	/*
4200ae115bc7Smrj 	 * There was already an bootenv entry which we need to edit.
4201ae115bc7Smrj 	 */
4202ae115bc7Smrj 	if (optnum == KERNEL_CMD) {
4203ae115bc7Smrj 		new_str_len = strlen(new_path) + strlen(old_args) + 2;
4204ae115bc7Smrj 		new_arg = s_calloc(1, new_str_len);
4205ae115bc7Smrj 		(void) snprintf(new_arg, new_str_len, "%s %s", new_path,
4206ae115bc7Smrj 		    old_args);
4207ae115bc7Smrj 		free(kernelp->arg);
4208ae115bc7Smrj 		kernelp->arg = new_arg;
4209ae115bc7Smrj 
4210ae115bc7Smrj 		/*
4211ae115bc7Smrj 		 * If we have changed the kernel line, we may need to update
4212ae115bc7Smrj 		 * the archive line as well.
4213ae115bc7Smrj 		 */
4214ae115bc7Smrj 		set_archive_line(entryp, kernelp);
4215ae115bc7Smrj 	} else {
4216ae115bc7Smrj 		new_str_len = old_kernel_len + strlen(path) + 8;
4217ae115bc7Smrj 		new_arg = s_calloc(1, new_str_len);
4218ae115bc7Smrj 		(void) strncpy(new_arg, kernelp->arg, old_kernel_len);
4219ae115bc7Smrj 		(void) strlcat(new_arg, " ", new_str_len);
4220ae115bc7Smrj 		(void) strlcat(new_arg, path, new_str_len);
4221ae115bc7Smrj 		free(kernelp->arg);
4222ae115bc7Smrj 		kernelp->arg = new_arg;
4223ae115bc7Smrj 	}
4224ae115bc7Smrj 	rv = BAM_WRITE;
4225ae115bc7Smrj 
4226ae115bc7Smrj done:
4227ae115bc7Smrj 	if ((rv == BAM_WRITE) && kernelp)
4228ae115bc7Smrj 		update_line(kernelp);
4229ae115bc7Smrj 	if (free_new_path)
4230ae115bc7Smrj 		free(new_path);
4231ae115bc7Smrj 	return (rv);
4232ae115bc7Smrj }
4233ae115bc7Smrj 
42347c478bd9Sstevel@tonic-gate /*ARGSUSED*/
42357c478bd9Sstevel@tonic-gate static error_t
42367c478bd9Sstevel@tonic-gate set_option(menu_t *mp, char *menu_path, char *opt)
42377c478bd9Sstevel@tonic-gate {
42387c478bd9Sstevel@tonic-gate 	int optnum, optval;
42397c478bd9Sstevel@tonic-gate 	char *val;
4240ae115bc7Smrj 	char buf[BUFSIZ] = "";
4241ae115bc7Smrj 	error_t rv;
42427c478bd9Sstevel@tonic-gate 
42437c478bd9Sstevel@tonic-gate 	assert(mp);
42447c478bd9Sstevel@tonic-gate 	assert(opt);
42457c478bd9Sstevel@tonic-gate 
42467c478bd9Sstevel@tonic-gate 	val = strchr(opt, '=');
4247ae115bc7Smrj 	if (val != NULL) {
4248ae115bc7Smrj 		*val = '\0';
42497c478bd9Sstevel@tonic-gate 	}
42507c478bd9Sstevel@tonic-gate 
42517c478bd9Sstevel@tonic-gate 	if (strcmp(opt, "default") == 0) {
42527c478bd9Sstevel@tonic-gate 		optnum = DEFAULT_CMD;
42537c478bd9Sstevel@tonic-gate 	} else if (strcmp(opt, "timeout") == 0) {
42547c478bd9Sstevel@tonic-gate 		optnum = TIMEOUT_CMD;
4255ae115bc7Smrj 	} else if (strcmp(opt, menu_cmds[KERNEL_CMD]) == 0) {
4256ae115bc7Smrj 		optnum = KERNEL_CMD;
4257ae115bc7Smrj 	} else if (strcmp(opt, menu_cmds[ARGS_CMD]) == 0) {
4258ae115bc7Smrj 		optnum = ARGS_CMD;
42597c478bd9Sstevel@tonic-gate 	} else {
42607c478bd9Sstevel@tonic-gate 		bam_error(INVALID_ENTRY, opt);
42617c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
42627c478bd9Sstevel@tonic-gate 	}
42637c478bd9Sstevel@tonic-gate 
4264ae115bc7Smrj 	/*
4265ae115bc7Smrj 	 * kernel and args are allowed without "=new_value" strings.  All
4266ae115bc7Smrj 	 * others cause errors
4267ae115bc7Smrj 	 */
4268ae115bc7Smrj 	if ((val == NULL) && (optnum != KERNEL_CMD) && (optnum != ARGS_CMD)) {
4269ae115bc7Smrj 		bam_error(INVALID_ENTRY, opt);
4270ae115bc7Smrj 		return (BAM_ERROR);
4271ae115bc7Smrj 	} else if (val != NULL) {
4272ae115bc7Smrj 		*val = '=';
4273ae115bc7Smrj 	}
4274ae115bc7Smrj 
4275ae115bc7Smrj 	if ((optnum == KERNEL_CMD) || (optnum == ARGS_CMD)) {
4276ae115bc7Smrj 		rv = set_kernel(mp, optnum, val ? val + 1 : NULL, buf, BUFSIZ);
4277ae115bc7Smrj 		if ((rv == BAM_SUCCESS) && (buf[0] != '\0'))
4278ae115bc7Smrj 			(void) printf("%s\n", buf);
4279ae115bc7Smrj 		return (rv);
4280ae115bc7Smrj 	} else {
4281ae115bc7Smrj 		optval = s_strtol(val + 1);
4282ae115bc7Smrj 		return (set_global(mp, menu_cmds[optnum], optval));
4283ae115bc7Smrj 	}
42847c478bd9Sstevel@tonic-gate }
42857c478bd9Sstevel@tonic-gate 
42867c478bd9Sstevel@tonic-gate /*
42877c478bd9Sstevel@tonic-gate  * The quiet argument suppresses messages. This is used
42887c478bd9Sstevel@tonic-gate  * when invoked in the context of other commands (e.g. list_entry)
42897c478bd9Sstevel@tonic-gate  */
42907c478bd9Sstevel@tonic-gate static error_t
42917c478bd9Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet)
42927c478bd9Sstevel@tonic-gate {
42937c478bd9Sstevel@tonic-gate 	line_t *lp;
42947c478bd9Sstevel@tonic-gate 	char *arg;
42957c478bd9Sstevel@tonic-gate 	int done, ret = BAM_SUCCESS;
42967c478bd9Sstevel@tonic-gate 
42977c478bd9Sstevel@tonic-gate 	assert(mp);
42987c478bd9Sstevel@tonic-gate 	assert(menu_path);
42997c478bd9Sstevel@tonic-gate 	assert(globalcmd);
43007c478bd9Sstevel@tonic-gate 
43017c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
43027c478bd9Sstevel@tonic-gate 		if (!quiet)
43037c478bd9Sstevel@tonic-gate 			bam_error(NO_MENU, menu_path);
43047c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
43057c478bd9Sstevel@tonic-gate 	}
43067c478bd9Sstevel@tonic-gate 
43077c478bd9Sstevel@tonic-gate 	done = 0;
43087c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
43097c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
43107c478bd9Sstevel@tonic-gate 			continue;
43117c478bd9Sstevel@tonic-gate 
43127c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
43137c478bd9Sstevel@tonic-gate 			if (!quiet)
43147c478bd9Sstevel@tonic-gate 				bam_error(NO_CMD, lp->lineNum);
43157c478bd9Sstevel@tonic-gate 			continue;
43167c478bd9Sstevel@tonic-gate 		}
43177c478bd9Sstevel@tonic-gate 
43187c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
43197c478bd9Sstevel@tonic-gate 			continue;
43207c478bd9Sstevel@tonic-gate 
43217c478bd9Sstevel@tonic-gate 		/* Found global. Check for duplicates */
43227c478bd9Sstevel@tonic-gate 		if (done && !quiet) {
43237c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
43247c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
43257c478bd9Sstevel@tonic-gate 		}
43267c478bd9Sstevel@tonic-gate 
43277c478bd9Sstevel@tonic-gate 		arg = lp->arg ? lp->arg : "";
43287c478bd9Sstevel@tonic-gate 		bam_print(GLOBAL_CMD, globalcmd, arg);
43297c478bd9Sstevel@tonic-gate 		done = 1;
43307c478bd9Sstevel@tonic-gate 	}
43317c478bd9Sstevel@tonic-gate 
43327c478bd9Sstevel@tonic-gate 	if (!done && bam_verbose)
43337c478bd9Sstevel@tonic-gate 		bam_print(NO_ENTRY, globalcmd);
43347c478bd9Sstevel@tonic-gate 
43357c478bd9Sstevel@tonic-gate 	return (ret);
43367c478bd9Sstevel@tonic-gate }
43377c478bd9Sstevel@tonic-gate 
43387c478bd9Sstevel@tonic-gate static error_t
43397c478bd9Sstevel@tonic-gate menu_write(char *root, menu_t *mp)
43407c478bd9Sstevel@tonic-gate {
43417c478bd9Sstevel@tonic-gate 	return (list2file(root, MENU_TMP, GRUB_MENU, mp->start));
43427c478bd9Sstevel@tonic-gate }
43437c478bd9Sstevel@tonic-gate 
43447c478bd9Sstevel@tonic-gate static void
43457c478bd9Sstevel@tonic-gate line_free(line_t *lp)
43467c478bd9Sstevel@tonic-gate {
43477c478bd9Sstevel@tonic-gate 	if (lp == NULL)
43487c478bd9Sstevel@tonic-gate 		return;
43497c478bd9Sstevel@tonic-gate 
43507c478bd9Sstevel@tonic-gate 	if (lp->cmd)
43517c478bd9Sstevel@tonic-gate 		free(lp->cmd);
43527c478bd9Sstevel@tonic-gate 	if (lp->sep)
43537c478bd9Sstevel@tonic-gate 		free(lp->sep);
43547c478bd9Sstevel@tonic-gate 	if (lp->arg)
43557c478bd9Sstevel@tonic-gate 		free(lp->arg);
43567c478bd9Sstevel@tonic-gate 	if (lp->line)
43577c478bd9Sstevel@tonic-gate 		free(lp->line);
43587c478bd9Sstevel@tonic-gate 	free(lp);
43597c478bd9Sstevel@tonic-gate }
43607c478bd9Sstevel@tonic-gate 
43617c478bd9Sstevel@tonic-gate static void
43627c478bd9Sstevel@tonic-gate linelist_free(line_t *start)
43637c478bd9Sstevel@tonic-gate {
43647c478bd9Sstevel@tonic-gate 	line_t *lp;
43657c478bd9Sstevel@tonic-gate 
43667c478bd9Sstevel@tonic-gate 	while (start) {
43677c478bd9Sstevel@tonic-gate 		lp = start;
43687c478bd9Sstevel@tonic-gate 		start = start->next;
43697c478bd9Sstevel@tonic-gate 		line_free(lp);
43707c478bd9Sstevel@tonic-gate 	}
43717c478bd9Sstevel@tonic-gate }
43727c478bd9Sstevel@tonic-gate 
43737c478bd9Sstevel@tonic-gate static void
43747c478bd9Sstevel@tonic-gate filelist_free(filelist_t *flistp)
43757c478bd9Sstevel@tonic-gate {
43767c478bd9Sstevel@tonic-gate 	linelist_free(flistp->head);
43777c478bd9Sstevel@tonic-gate 	flistp->head = NULL;
43787c478bd9Sstevel@tonic-gate 	flistp->tail = NULL;
43797c478bd9Sstevel@tonic-gate }
43807c478bd9Sstevel@tonic-gate 
43817c478bd9Sstevel@tonic-gate static void
43827c478bd9Sstevel@tonic-gate menu_free(menu_t *mp)
43837c478bd9Sstevel@tonic-gate {
43848c1b6884Sszhou 	entry_t *ent, *tmp;
43857c478bd9Sstevel@tonic-gate 	assert(mp);
43867c478bd9Sstevel@tonic-gate 
43877c478bd9Sstevel@tonic-gate 	if (mp->start)
43887c478bd9Sstevel@tonic-gate 		linelist_free(mp->start);
43898c1b6884Sszhou 	ent = mp->entries;
43908c1b6884Sszhou 	while (ent) {
43918c1b6884Sszhou 		tmp = ent;
43928c1b6884Sszhou 		ent = tmp->next;
43938c1b6884Sszhou 		free(tmp);
43948c1b6884Sszhou 	}
43957c478bd9Sstevel@tonic-gate 
43968c1b6884Sszhou 	free(mp);
43977c478bd9Sstevel@tonic-gate }
43987c478bd9Sstevel@tonic-gate 
43997c478bd9Sstevel@tonic-gate /*
44007c478bd9Sstevel@tonic-gate  * Utility routines
44017c478bd9Sstevel@tonic-gate  */
44027c478bd9Sstevel@tonic-gate 
44037c478bd9Sstevel@tonic-gate 
44047c478bd9Sstevel@tonic-gate /*
44057c478bd9Sstevel@tonic-gate  * Returns 0 on success
44067c478bd9Sstevel@tonic-gate  * Any other value indicates an error
44077c478bd9Sstevel@tonic-gate  */
44087c478bd9Sstevel@tonic-gate static int
4409986fd29aSsetje exec_cmd(char *cmdline, filelist_t *flistp)
44107c478bd9Sstevel@tonic-gate {
44117c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
44127c478bd9Sstevel@tonic-gate 	int ret;
44137c478bd9Sstevel@tonic-gate 	FILE *ptr;
44147c478bd9Sstevel@tonic-gate 	sigset_t set;
44157c478bd9Sstevel@tonic-gate 	void (*disp)(int);
44167c478bd9Sstevel@tonic-gate 
44177c478bd9Sstevel@tonic-gate 	/*
44187c478bd9Sstevel@tonic-gate 	 * For security
44197c478bd9Sstevel@tonic-gate 	 * - only absolute paths are allowed
44207c478bd9Sstevel@tonic-gate 	 * - set IFS to space and tab
44217c478bd9Sstevel@tonic-gate 	 */
44227c478bd9Sstevel@tonic-gate 	if (*cmdline != '/') {
44237c478bd9Sstevel@tonic-gate 		bam_error(ABS_PATH_REQ, cmdline);
44247c478bd9Sstevel@tonic-gate 		return (-1);
44257c478bd9Sstevel@tonic-gate 	}
44267c478bd9Sstevel@tonic-gate 	(void) putenv("IFS= \t");
44277c478bd9Sstevel@tonic-gate 
44287c478bd9Sstevel@tonic-gate 	/*
44297c478bd9Sstevel@tonic-gate 	 * We may have been exec'ed with SIGCHLD blocked
44307c478bd9Sstevel@tonic-gate 	 * unblock it here
44317c478bd9Sstevel@tonic-gate 	 */
44327c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
44337c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
44347c478bd9Sstevel@tonic-gate 	if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
44357c478bd9Sstevel@tonic-gate 		bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno));
44367c478bd9Sstevel@tonic-gate 		return (-1);
44377c478bd9Sstevel@tonic-gate 	}
44387c478bd9Sstevel@tonic-gate 
44397c478bd9Sstevel@tonic-gate 	/*
44407c478bd9Sstevel@tonic-gate 	 * Set SIGCHLD disposition to SIG_DFL for popen/pclose
44417c478bd9Sstevel@tonic-gate 	 */
44427c478bd9Sstevel@tonic-gate 	disp = sigset(SIGCHLD, SIG_DFL);
44437c478bd9Sstevel@tonic-gate 	if (disp == SIG_ERR) {
44447c478bd9Sstevel@tonic-gate 		bam_error(FAILED_SIG, strerror(errno));
44457c478bd9Sstevel@tonic-gate 		return (-1);
44467c478bd9Sstevel@tonic-gate 	}
44477c478bd9Sstevel@tonic-gate 	if (disp == SIG_HOLD) {
44487c478bd9Sstevel@tonic-gate 		bam_error(BLOCKED_SIG, cmdline);
44497c478bd9Sstevel@tonic-gate 		return (-1);
44507c478bd9Sstevel@tonic-gate 	}
44517c478bd9Sstevel@tonic-gate 
44527c478bd9Sstevel@tonic-gate 	ptr = popen(cmdline, "r");
44537c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
44547c478bd9Sstevel@tonic-gate 		bam_error(POPEN_FAIL, cmdline, strerror(errno));
44557c478bd9Sstevel@tonic-gate 		return (-1);
44567c478bd9Sstevel@tonic-gate 	}
44577c478bd9Sstevel@tonic-gate 
44587c478bd9Sstevel@tonic-gate 	/*
44597c478bd9Sstevel@tonic-gate 	 * If we simply do a pclose() following a popen(), pclose()
44607c478bd9Sstevel@tonic-gate 	 * will close the reader end of the pipe immediately even
44617c478bd9Sstevel@tonic-gate 	 * if the child process has not started/exited. pclose()
44627c478bd9Sstevel@tonic-gate 	 * does wait for cmd to terminate before returning though.
44637c478bd9Sstevel@tonic-gate 	 * When the executed command writes its output to the pipe
44647c478bd9Sstevel@tonic-gate 	 * there is no reader process and the command dies with
44657c478bd9Sstevel@tonic-gate 	 * SIGPIPE. To avoid this we read repeatedly until read
44667c478bd9Sstevel@tonic-gate 	 * terminates with EOF. This indicates that the command
44677c478bd9Sstevel@tonic-gate 	 * (writer) has closed the pipe and we can safely do a
44687c478bd9Sstevel@tonic-gate 	 * pclose().
44697c478bd9Sstevel@tonic-gate 	 *
44707c478bd9Sstevel@tonic-gate 	 * Since pclose() does wait for the command to exit,
44717c478bd9Sstevel@tonic-gate 	 * we can safely reap the exit status of the command
44727c478bd9Sstevel@tonic-gate 	 * from the value returned by pclose()
44737c478bd9Sstevel@tonic-gate 	 */
4474986fd29aSsetje 	while (s_fgets(buf, sizeof (buf), ptr) != NULL) {
4475986fd29aSsetje 		if (flistp == NULL) {
4476986fd29aSsetje 			/* s_fgets strips newlines, so insert them at the end */
4477986fd29aSsetje 			bam_print(PRINT, buf);
4478986fd29aSsetje 		} else {
4479986fd29aSsetje 			append_to_flist(flistp, buf);
44807c478bd9Sstevel@tonic-gate 		}
44817c478bd9Sstevel@tonic-gate 	}
44827c478bd9Sstevel@tonic-gate 
44837c478bd9Sstevel@tonic-gate 	ret = pclose(ptr);
44847c478bd9Sstevel@tonic-gate 	if (ret == -1) {
44857c478bd9Sstevel@tonic-gate 		bam_error(PCLOSE_FAIL, cmdline, strerror(errno));
44867c478bd9Sstevel@tonic-gate 		return (-1);
44877c478bd9Sstevel@tonic-gate 	}
44887c478bd9Sstevel@tonic-gate 
44897c478bd9Sstevel@tonic-gate 	if (WIFEXITED(ret)) {
44907c478bd9Sstevel@tonic-gate 		return (WEXITSTATUS(ret));
44917c478bd9Sstevel@tonic-gate 	} else {
44927c478bd9Sstevel@tonic-gate 		bam_error(EXEC_FAIL, cmdline, ret);
44937c478bd9Sstevel@tonic-gate 		return (-1);
44947c478bd9Sstevel@tonic-gate 	}
44957c478bd9Sstevel@tonic-gate }
44967c478bd9Sstevel@tonic-gate 
44977c478bd9Sstevel@tonic-gate /*
44987c478bd9Sstevel@tonic-gate  * Since this function returns -1 on error
44997c478bd9Sstevel@tonic-gate  * it cannot be used to convert -1. However,
45007c478bd9Sstevel@tonic-gate  * that is sufficient for what we need.
45017c478bd9Sstevel@tonic-gate  */
45027c478bd9Sstevel@tonic-gate static long
45037c478bd9Sstevel@tonic-gate s_strtol(char *str)
45047c478bd9Sstevel@tonic-gate {
45057c478bd9Sstevel@tonic-gate 	long l;
45067c478bd9Sstevel@tonic-gate 	char *res = NULL;
45077c478bd9Sstevel@tonic-gate 
45087c478bd9Sstevel@tonic-gate 	if (str == NULL) {
45097c478bd9Sstevel@tonic-gate 		return (-1);
45107c478bd9Sstevel@tonic-gate 	}
45117c478bd9Sstevel@tonic-gate 
45127c478bd9Sstevel@tonic-gate 	errno = 0;
45137c478bd9Sstevel@tonic-gate 	l = strtol(str, &res, 10);
45147c478bd9Sstevel@tonic-gate 	if (errno || *res != '\0') {
45157c478bd9Sstevel@tonic-gate 		return (-1);
45167c478bd9Sstevel@tonic-gate 	}
45177c478bd9Sstevel@tonic-gate 
45187c478bd9Sstevel@tonic-gate 	return (l);
45197c478bd9Sstevel@tonic-gate }
45207c478bd9Sstevel@tonic-gate 
45217c478bd9Sstevel@tonic-gate /*
45227c478bd9Sstevel@tonic-gate  * Wrapper around fputs, that adds a newline (since fputs doesn't)
45237c478bd9Sstevel@tonic-gate  */
45247c478bd9Sstevel@tonic-gate static int
45257c478bd9Sstevel@tonic-gate s_fputs(char *str, FILE *fp)
45267c478bd9Sstevel@tonic-gate {
45277c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
45287c478bd9Sstevel@tonic-gate 
45297c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s\n", str);
45307c478bd9Sstevel@tonic-gate 	return (fputs(linebuf, fp));
45317c478bd9Sstevel@tonic-gate }
45327c478bd9Sstevel@tonic-gate 
45337c478bd9Sstevel@tonic-gate /*
45347c478bd9Sstevel@tonic-gate  * Wrapper around fgets, that strips newlines returned by fgets
45357c478bd9Sstevel@tonic-gate  */
4536ae115bc7Smrj char *
45377c478bd9Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp)
45387c478bd9Sstevel@tonic-gate {
45397c478bd9Sstevel@tonic-gate 	int n;
45407c478bd9Sstevel@tonic-gate 
45417c478bd9Sstevel@tonic-gate 	buf = fgets(buf, buflen, fp);
45427c478bd9Sstevel@tonic-gate 	if (buf) {
45437c478bd9Sstevel@tonic-gate 		n = strlen(buf);
45447c478bd9Sstevel@tonic-gate 		if (n == buflen - 1 && buf[n-1] != '\n')
45457c478bd9Sstevel@tonic-gate 			bam_error(TOO_LONG, buflen - 1, buf);
45467c478bd9Sstevel@tonic-gate 		buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1];
45477c478bd9Sstevel@tonic-gate 	}
45487c478bd9Sstevel@tonic-gate 
45497c478bd9Sstevel@tonic-gate 	return (buf);
45507c478bd9Sstevel@tonic-gate }
45517c478bd9Sstevel@tonic-gate 
4552ae115bc7Smrj void *
45537c478bd9Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz)
45547c478bd9Sstevel@tonic-gate {
45557c478bd9Sstevel@tonic-gate 	void *ptr;
45567c478bd9Sstevel@tonic-gate 
45577c478bd9Sstevel@tonic-gate 	ptr = calloc(nelem, sz);
45587c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
45597c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, nelem*sz);
45607c478bd9Sstevel@tonic-gate 		bam_exit(1);
45617c478bd9Sstevel@tonic-gate 	}
45627c478bd9Sstevel@tonic-gate 	return (ptr);
45637c478bd9Sstevel@tonic-gate }
45647c478bd9Sstevel@tonic-gate 
4565ae115bc7Smrj void *
4566ae115bc7Smrj s_realloc(void *ptr, size_t sz)
4567ae115bc7Smrj {
4568ae115bc7Smrj 	ptr = realloc(ptr, sz);
4569ae115bc7Smrj 	if (ptr == NULL) {
4570ae115bc7Smrj 		bam_error(NO_MEM, sz);
4571ae115bc7Smrj 		bam_exit(1);
4572ae115bc7Smrj 	}
4573ae115bc7Smrj 	return (ptr);
4574ae115bc7Smrj }
4575ae115bc7Smrj 
45767c478bd9Sstevel@tonic-gate static char *
45777c478bd9Sstevel@tonic-gate s_strdup(char *str)
45787c478bd9Sstevel@tonic-gate {
45797c478bd9Sstevel@tonic-gate 	char *ptr;
45807c478bd9Sstevel@tonic-gate 
45817c478bd9Sstevel@tonic-gate 	if (str == NULL)
45827c478bd9Sstevel@tonic-gate 		return (NULL);
45837c478bd9Sstevel@tonic-gate 
45847c478bd9Sstevel@tonic-gate 	ptr = strdup(str);
45857c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
45867c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, strlen(str) + 1);
45877c478bd9Sstevel@tonic-gate 		bam_exit(1);
45887c478bd9Sstevel@tonic-gate 	}
45897c478bd9Sstevel@tonic-gate 	return (ptr);
45907c478bd9Sstevel@tonic-gate }
45917c478bd9Sstevel@tonic-gate 
45927c478bd9Sstevel@tonic-gate /*
45937c478bd9Sstevel@tonic-gate  * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients)
45947c478bd9Sstevel@tonic-gate  * Returns 0 otherwise
45957c478bd9Sstevel@tonic-gate  */
45967c478bd9Sstevel@tonic-gate static int
45977c478bd9Sstevel@tonic-gate is_amd64(void)
45987c478bd9Sstevel@tonic-gate {
45997c478bd9Sstevel@tonic-gate 	static int amd64 = -1;
46007c478bd9Sstevel@tonic-gate 	char isabuf[257];	/* from sysinfo(2) manpage */
46017c478bd9Sstevel@tonic-gate 
46027c478bd9Sstevel@tonic-gate 	if (amd64 != -1)
46037c478bd9Sstevel@tonic-gate 		return (amd64);
46047c478bd9Sstevel@tonic-gate 
4605*d876c67dSjg 	if (bam_alt_platform) {
4606*d876c67dSjg 		if (strcmp(bam_platform, "i86pc") == 0) {
4607*d876c67dSjg 			amd64 = 1;		/* diskless server */
4608*d876c67dSjg 		}
4609*d876c67dSjg 	} else {
4610*d876c67dSjg 		if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 &&
4611*d876c67dSjg 		    strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0) {
4612*d876c67dSjg 			amd64 = 1;
4613*d876c67dSjg 		} else if (strstr(isabuf, "i386") == NULL) {
4614*d876c67dSjg 			amd64 = 1;		/* diskless server */
4615*d876c67dSjg 		}
4616*d876c67dSjg 	}
4617*d876c67dSjg 	if (amd64 == -1)
46187c478bd9Sstevel@tonic-gate 		amd64 = 0;
46197c478bd9Sstevel@tonic-gate 
46207c478bd9Sstevel@tonic-gate 	return (amd64);
46217c478bd9Sstevel@tonic-gate }
46227c478bd9Sstevel@tonic-gate 
4623986fd29aSsetje static int
4624986fd29aSsetje is_sun4u(void)
4625986fd29aSsetje {
4626*d876c67dSjg 	static int sun4u = -1;
4627986fd29aSsetje 	char mbuf[257];	/* from sysinfo(2) manpage */
4628986fd29aSsetje 
4629*d876c67dSjg 	if (sun4u != -1)
4630*d876c67dSjg 		return (sun4u);
4631*d876c67dSjg 
4632*d876c67dSjg 	if (bam_alt_platform) {
4633*d876c67dSjg 		if (strcmp(bam_platform, "sun4u") == 0) {
4634*d876c67dSjg 			sun4u = 1;
4635*d876c67dSjg 		}
4636*d876c67dSjg 	} else {
4637*d876c67dSjg 		if (sysinfo(SI_MACHINE, mbuf, sizeof (mbuf)) > 0 &&
4638*d876c67dSjg 		    strncmp(mbuf, "sun4u", strlen("sun4u")) == 0) {
4639*d876c67dSjg 			sun4u = 1;
4640*d876c67dSjg 		}
4641*d876c67dSjg 	}
4642*d876c67dSjg 	if (sun4u == -1)
4643*d876c67dSjg 		sun4u = 0;
4644986fd29aSsetje 
4645986fd29aSsetje 	return (sun4u);
4646986fd29aSsetje }
4647986fd29aSsetje 
4648986fd29aSsetje static int
4649986fd29aSsetje is_sun4v(void)
4650986fd29aSsetje {
4651*d876c67dSjg 	static int sun4v = -1;
4652986fd29aSsetje 	char mbuf[257];	/* from sysinfo(2) manpage */
4653986fd29aSsetje 
4654*d876c67dSjg 	if (sun4v != -1)
4655*d876c67dSjg 		return (sun4v);
4656*d876c67dSjg 
4657*d876c67dSjg 	if (bam_alt_platform) {
4658*d876c67dSjg 		if (strcmp(bam_platform, "sun4v") == 0) {
4659*d876c67dSjg 			sun4v = 1;
4660*d876c67dSjg 		}
4661*d876c67dSjg 	} else {
4662*d876c67dSjg 		if (sysinfo(SI_MACHINE, mbuf, sizeof (mbuf)) > 0 &&
4663*d876c67dSjg 		    strncmp(mbuf, "sun4v", strlen("sun4v")) == 0) {
4664*d876c67dSjg 			sun4v = 1;
4665*d876c67dSjg 		}
4666*d876c67dSjg 	}
4667*d876c67dSjg 	if (sun4v == -1)
4668*d876c67dSjg 		sun4v = 0;
4669986fd29aSsetje 
4670986fd29aSsetje 	return (sun4v);
4671986fd29aSsetje }
4672986fd29aSsetje 
4673986fd29aSsetje 
46747c478bd9Sstevel@tonic-gate static void
46757c478bd9Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s)
46767c478bd9Sstevel@tonic-gate {
46777c478bd9Sstevel@tonic-gate 	line_t *lp;
46787c478bd9Sstevel@tonic-gate 
46797c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
46807c478bd9Sstevel@tonic-gate 	lp->line = s_strdup(s);
46817c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL)
46827c478bd9Sstevel@tonic-gate 		flistp->head = lp;
46837c478bd9Sstevel@tonic-gate 	else
46847c478bd9Sstevel@tonic-gate 		flistp->tail->next = lp;
46857c478bd9Sstevel@tonic-gate 	flistp->tail = lp;
46867c478bd9Sstevel@tonic-gate }
46872449e17fSsherrym 
4688986fd29aSsetje #if !defined(_OPB)
46892449e17fSsherrym 
46902449e17fSsherrym UCODE_VENDORS;
46912449e17fSsherrym 
46922449e17fSsherrym /*ARGSUSED*/
46932449e17fSsherrym static void
46942449e17fSsherrym ucode_install(char *root)
46952449e17fSsherrym {
46962449e17fSsherrym 	int i;
46972449e17fSsherrym 
46982449e17fSsherrym 	for (i = 0; ucode_vendors[i].filestr != NULL; i++) {
46992449e17fSsherrym 		int cmd_len = PATH_MAX + 256;
47002449e17fSsherrym 		char cmd[PATH_MAX + 256];
47012449e17fSsherrym 		char file[PATH_MAX];
47022449e17fSsherrym 		char timestamp[PATH_MAX];
47032449e17fSsherrym 		struct stat fstatus, tstatus;
47042449e17fSsherrym 		struct utimbuf u_times;
47052449e17fSsherrym 
47062449e17fSsherrym 		(void) snprintf(file, PATH_MAX, "%s/%s/%s-ucode.txt",
47072449e17fSsherrym 		    bam_root, UCODE_INSTALL_PATH, ucode_vendors[i].filestr);
47082449e17fSsherrym 
47092449e17fSsherrym 		if (stat(file, &fstatus) != 0 || !(S_ISREG(fstatus.st_mode)))
47102449e17fSsherrym 			continue;
47112449e17fSsherrym 
47122449e17fSsherrym 		(void) snprintf(timestamp, PATH_MAX, "%s.ts", file);
47132449e17fSsherrym 
47142449e17fSsherrym 		if (stat(timestamp, &tstatus) == 0 &&
47152449e17fSsherrym 		    fstatus.st_mtime <= tstatus.st_mtime)
47162449e17fSsherrym 			continue;
47172449e17fSsherrym 
47182449e17fSsherrym 		(void) snprintf(cmd, cmd_len, "/usr/sbin/ucodeadm -i -R "
47192449e17fSsherrym 		    "%s/%s/%s %s > /dev/null 2>&1", bam_root,
47202449e17fSsherrym 		    UCODE_INSTALL_PATH, ucode_vendors[i].vendorstr, file);
47212449e17fSsherrym 		if (system(cmd) != 0)
47222449e17fSsherrym 			return;
47232449e17fSsherrym 
47242449e17fSsherrym 		if (creat(timestamp, S_IRUSR | S_IWUSR) == -1)
47252449e17fSsherrym 			return;
47262449e17fSsherrym 
47272449e17fSsherrym 		u_times.actime = fstatus.st_atime;
47282449e17fSsherrym 		u_times.modtime = fstatus.st_mtime;
47292449e17fSsherrym 		(void) utime(timestamp, &u_times);
47302449e17fSsherrym 	}
47312449e17fSsherrym }
47322449e17fSsherrym #endif
4733